430 likes | 570 Views
SQL. SELECT-FROM-WHERE. SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”. Student. (sName=“Greg” AND address=“320 FL”) (Student). Project. SELECT sNumber, sName FROM Student. Student. (sNumber, sName) (Student). Extended Projection. SELECT sNumber || sName AS info
E N D
SQL Murali Mani
SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL” Student (sName=“Greg” AND address=“320 FL”) (Student) Murali Mani
Project SELECT sNumber, sName FROM Student Student (sNumber, sName) (Student) Murali Mani
Extended Projection SELECT sNumber || sName AS info FROM Student WHERE address=“320 FL” Student (sNumber||sNameinfo) ( (address=“320 FL”) (Student)) Murali Mani
SQL and Relational Algebra In short, L ( C (R)) becomes SELECT L FROM R WHERE C Murali Mani
Renaming SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sNumber >= 1; Student (s1.sNumbernum) ( (s1.sNumber >= 1) ( S1 (Student))) Murali Mani
String operators • Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= • Concatenation operator: || • ‘ represented in strings with two consecutive ‘ • Pattern match: s LIKE p • p = pattern • % : any sequence of 0 or more characters • - : matches 1 character • Patterns can explicitly declare escape characters as: s LIKE ‘x%%am%’ ESCAPE ‘x’ Murali Mani
Comparison with NULL values • Arithmetic operations on NULL return NULL. • Comparison operators on NULL return UNKNOWN. • We can explicitly check whether a value is null or not, by IS NULL, IS NOT NULL. Murali Mani
Truth table with UNKNOWN UNKNOWN AND TRUE = UNKNOWN UNKNOWN OR TRUE = TRUE UNKNOWN AND FALSE = FALSEUNKNOWN OR FALSE = UNKNOWN UNKNOWN AND UNKNOWN = UNKNOWN UNKNOWN OR UNKNOWN = UNKNOWN NOT UNKNOWN = UNKNOWN A WHERE clause is satisfied only when it evaluates to TRUE. Murali Mani
UNION, INTERSECT, EXCEPT • UNION, INTERSECT, EXCEPT have set semantics. • For bag semantics, use UNION ALL, INTERSECT ALL, EXCEPT ALL (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) Murali Mani
EXCEPT - Example (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) Student Murali Mani
Joins SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; (sName)(Student ⋈(pName=‘MM’ and pNumber=professor) Professor) Murali Mani
Joins - example Student SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; Professor Murali Mani
Cross Product (Cartesian Product) SELECT * FROM Student CROSS JOIN Professor; Student X Professor can also be written as: SELECT * FROM Student, Professor Murali Mani
Cross Product - Example Student Professor Murali Mani
Theta Join SELECT * FROM Student JOIN Professor ON professor=pNumber; Student ⋈(professor=pNumber) Professor SELECT * FROM Student, Professor WHERE professor=pNumber; Murali Mani
Theta Join Example Murali Mani
Natural Join SELECT * FROM Student NATURAL JOIN Professor (Note: This requires the columns on which the join should be done should have the same names for Student and Professor). Student ⋈ Professor Murali Mani
Natural Join - Example Student Professor Murali Mani
Outer Joins SELECT * FROM Student NATURAL FULL OUTER JOIN Professor Student ⋈oProfessor SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor Student ⋈oLProfessor Murali Mani
Outer Joins SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈oRProfessor Murali Mani
Outer Joins - Example Student Professor Murali Mani
Sorting: ORDER BY clause SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName (sNumber, sName) ( (sNumber >= 1) (Student)) Murali Mani
Subqueries SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1) Note: the inner subquery returns a relation, but SQL runtime ensures that the subquery returns a relation with one column and with one row, otherwise it is a run-time error. Murali Mani
Subqueries - Example Student SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1) Professor Murali Mani
Subqueries • We can use IN, EXISTS (also NOT IN, NOT EXISTS) • ALL, ANY can be used with comparisons SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor) Murali Mani
Subqueries - Example Student Professor SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor) Murali Mani
Subqueries: EXISTS Professor SELECT * FROM Student WHERE EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) Student Murali Mani
Subqueries with negation Professor SELECT * FROM Student WHERE (sNumber, professor) NOT IN (SELECT pNumber, pName FROM Professor) Student Murali Mani
Subqueries with negation Student Professor SELECT * FROM Student WHERE NOT EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) Murali Mani
Subqueries: ALL, ANY SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor) SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor) Murali Mani
Subqueries: ALL - Example Student Professor SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor) Murali Mani
Subqueries: ANY - Example Student Professor SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor) Murali Mani
Subqueries: NOT ALL - Example Student Professor SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor) Murali Mani
Subqueries: NOT ANY - Example Student Professor SELECT * FROM Student WHERE NOT sNumber = ANY (SELECT pNumber FROM Professor) Murali Mani
Subqueries: Tip Murali Mani
Subqueries in FROM clause Professor SELECT sName, pName FROM Student, (SELECT * FROM Professor WHERE pNumber=1) WHERE professor=pName; Student Murali Mani
Duplicate Elimination SELECT DISTINCT address FROM Student WHERE sNumber >= 1; SELECT DISTINCT * FROM Student; • (Student) • ( (address) ( (sNumber >= 1) (Student))) Student Murali Mani
Aggregation SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student; We can have distinct such as: SELECT COUNT (DISTINCT sNumber) FROM Student Murali Mani
Grouping SELECT COUNT (sName) FROM Student GROUP BY address; • (COUNT (sName)) ( (address, COUNT (sName)) (Student)) Student Murali Mani
Grouping SELECT address, COUNT (sNumber) FROM Student WHERE sNumber > 1 GROUP BY address HAVING COUNT (sNumber) > 1; Student Murali Mani
Aggregation and NULLs • NULLs are ignored in any aggregation; except COUNT (*) • However if the set of attributes to be grouped on has null values, then grouping is done on the null values as well. Murali Mani
SQL Queries - Summary SELECT [DISTINCT] a1, a2, …, an FROM R1, R2, …, Rm [WHERE C1] [GROUP BY g1, g2, …, gl [HAVING C2]] [ORDER BY o1, o2, …, oj] Murali Mani