220 likes | 371 Views
SQL. Neyha Amar CS 157A, Fall 2006. 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. Example of Insertion. insert into Employee
E N D
SQL Neyha Amar CS 157A, Fall 2006
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
Example of Insertion insert into Employee values(3, ‘Steve’, ‘Glasses’) OR insert into Employee(EmpID, name, DeptID) values(3, ‘Steve’, ‘Glasses’) OR insert into Employee(name, DeptID, EmpID) values(‘Steve’, ‘Glasses’, 3) Row inserted
Updating • The update statement is used to change a value in a tuple that matches a specified criteria. General Form: update tablename set column = new value, nextcolumn = new value2, … where somecolumn [and | or othercolumn]OPERATOR value Example1 (from textbook): update account set balance = balance * 1.05 where balance >= 1000 • Without the WHERE all values under the specified column will be updated
Example 2: Updating Employee updateEmployee set DeptID = ‘Shoes’ wherename = ‘Steve’ updated row
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 fromaccount wherebranch_name = ‘Perryridge’ • w/o the WHERE all records will be deleted !!!
Example 2: Deletion Employee delete fromEmployee where EmpID = 3
Set Operations • SQL operations Union,Intersect,andExcept 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. (selectcustomer_name fromdepositor) union (selectcustomer_name fromborrower) • 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.
Continue… Union Operation • If you want to retain all duplicates, you must write UNION ALL in place of UNION (selectcustomer_name fromdepositor) union all (selectcustomer_name fromborrower)
Intersect Operation Example: Find all the bank customers having a loan AND an account at the bank. (select distinctcustomer_name fromdepositor) intersect (selectdistinct customer_name fromborrower) • automatically eliminates duplicates • to retain them use INTERSECT ALLin place of INTERSECT
Except Operation Example: Find all the bank customers having an account but NO loan at the bank. (selectdistinct customer_name fromdepositor) except (selectdistinct customer_name fromborrower) • automatically eliminates duplicates, • To retain use EXCEPT ALLin place of EXCEPT
Aggregate Functions • Functions that take a collection (a set or multiset) of values as input and return a single value. • SQL has five built-in aggregate functions: - avg ( [distinct | all] n) – returns average value of n - min ( [distinct | all] expr) – returns minimum value of expr - max ( [distinct | all] expr) – returns maximum value of expr - sum ( [distinct | all] n) – returns total sum of values in expr -count (* | [distinct | all] expr) - returns # of rows * - return # of rows including NULL values from relation distinct - return # of rows eliminating duplicates and NULL values from expr all - return # of rows including duplicates but no NULL values from expr • The input to sum and avg must be a collection of numbers • Others can operate on collections of nonnumeric data types, such as strings, as well.
Example: Aggregate Function Example: Find the average account balance at the Perryridge branch. selectavg (balance) from account [as average] wherebranch_name = ‘Perryridge’ • Result will be a relation with a single attribute, containing a single tuple that equals the average account balance at Perryridge • 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 Consider the following relation schema: Store_Information
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:
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.
Example: HAVING Query: Find the total sales for each store that has a total sale greater than $1500. Answer: select Store_name, sum(Sales) from Store_Information group by store_name having sum(Sales) > 1500 Result:
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 nullin 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 amountis 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 amountis not null
References • Database System Concepts, 5th edition, Silberschatz, Korth, Sudarshan • http://www.nd.edu/~pmiller/capp_sql/sql_group_functions.ppt • http://databases.about.com/od/sql/l/aaaggregate2.htm