SQL Presentation 1
SQL Presentation 1
Overview
Introduction DDL Commands DML Commands SQL Statements, Operators, Clauses Aggregate Functions
First Version was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce. [SQL]
Developed using Dr. E.F. Codd's paper, A Relational Model of Data for Large Shared Data Banks. SQL query includes references to tuples variables and the attributes of those variables
ALTER TABLE statement syntax: ALTER TABLE <table name> ADD attr datatype; or DROP COLUMN attr;
Example: CREATE TABLE FoodCart ( date varchar(10), food varchar(20), profit float );
FoodCart
FoodCart
ALTER TABLE FoodCart ( ADD sold int ); ALTER TABLE FoodCart( DROP COLUMN profit ); DROP TABLE FoodCart;
sold 349 70
Note: If the WHERE clause is omitted all rows of data are deleted from the table.
FROM
Specifies the tables that serve as the input to the statement
WHERE
Specifies the selection condition, including the join condition.
Example: Person
Name Harry Sally George Age 34 28 29 Weight 80 64 70
Helena
Peter
54
34
54
80
80
State CA MA TN
Emp.State CA MA TN
Emp.State MA TN null
SELECT food, sum(sold) as totalSold FROM FoodCart group by food having sum(sold) > 450;
FoodCart
COUNT(attr) -> return # of rows that are not null Ex: COUNT(distinct food) from FoodCart; -> 2
SUM(attr) -> return the sum of values in the attr Ex: SUM(sold) from FoodCart; -> 919
MAX(attr) -> return the highest value from the attr Ex: MAX(sold) from FoodCart; -> 500
MIN(attr) -> return the lowest value from the attr Ex: MIN(sold) from FoodCart; -> 70
AVG(attr) -> return the average value from the attr Ex: AVG(sold) from FoodCart; -> 306.33 Note: value is rounded to the precision of the datatype
References
Riccardi, Greg. Principles of Database Systems with Internet and Java Applications. Addison Wesley, 2001.
Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition. Sams Publishing, 2003.
SQL
http://en.wikipedia.org/wiki/SQL