Docsity

SQL Basics: Insertions, Updates, Deletions, Set Operations, and Aggregate Functions, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Basics: Insertions, Updates, Deletions, Set Operations, and Aggregate Functions and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Inserting

  • The insert statement is used to add a row of data

into a table

  • Strings should be enclosed in single quotes, and

numbers should not.

  • Consider the following table:

Employee

EmpID name DeptID 1 John Hats

2 David Shoes

Example of Insertion

EmpID name DeptID

1 John Hats

2 David Shoes

3 Steve Glasses (^)  Row inserted

Example 2: Updating

update Employee set DeptID = ‘Shoes’ where name = ‘Steve’

EmpID name DeptID 1 John Hats 2 David Shoes 3 Steve Glasses

Employee

EmpID name DeptID 1 John Hats 2 David Shoes updated row3 Steve Shoes

Deletion

  • The delete statement is used to delete tuples from the

table

  • We can only delete whole tuples, not values in only

particular attributes

General Form: delete from tablename where somecolumn [and | or othercolumn ] OPERATOR value Example 1: delete from account where branch_name = ‘Perryridge’

w/o the WHERE all records will be deleted !!!

Set Operations

  • SQL operations Union , Intersect , and Except

operate on relations and correspond to relational

algebra operations union, intersection and set

difference

  • Relations participating in operations must be

compatible, that is they must have the same

attributes

Union Operation

Example: Find all the bank customers having a loan, an

account, or both at the bank.

( select customer_name

from depositor )

union

( select customer_name

from borrower )

o Union operation automatically eliminates duplicates

If customer has several accounts or loans (or both)

at the bank then he/she will appear only ONCE in

the result.

Intersect Operation

Example: Find all the bank customers having a loan AND an

account at the bank.

( select distinct customer_name

from depositor )

intersect

( select distinct customer_name

from borrower )

o automatically eliminates duplicates

o to retain them use INTERSECT ALL in place of INTERSECT

Except Operation

Example: Find all the bank customers having an

account but NO loan at the bank.

( select distinct customer_name

from depositor )

except

( select distinct customer_name

from borrower )

o automatically eliminates duplicates,

o To retain use EXCEPT ALL in place of EXCEPT

Example: Aggregate Function

Example: Find the average account balance at the

Perryridge branch.

select avg ( balance )

from account [as average ]

where branch_name = ‘Perryridge’

o Result will be a relation with a single attribute,

containing a single tuple that equals the average

account balance at Perryridge

o Can give a name to the attribute by using the AS

clause

Group By Clause

  • GROUP BY is used when we are selecting

multiple columns from a table (or tables) and at least one arithmetic operator appears in the SELECT statement.

  • When that happens, we need to GROUP BY all

the columns except the one(s) operated on by the arithmetic operator

Example GROUP BY…

Query: Find the total sales for each store.

Answer:

select Store_name, sum (Sales)

from Store_Information

group by store_name

Result:

Store_name sum (Sales)

Los Angeles $

San Diego $

Boston $

HAVING clause

  • HAVING serves as the WHERE clause for

grouped data

  • This condition does not apply to a single tuple;

it applies to each group constructed by the group by clause.

Null Values

  • SQL allows the use of null values to indicate

absence of information about the value of an attribute.

  • We can use the special keyword null in a

predicate to test for a null value

Examples: Null Values

Query: Find all loan numbers that appear in the loan

relation with null values for amount.

Answer:

select loan_number from loan where amount is null

Query: Find all loan numbers that appear in the loan

relation that do not have null values for amount.

Answer:

select loan_number from loan where amount is not null