Download presentation
Presentation is loading. Please wait.
1
Murali Mani SQL
2
Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL” (sName=“Greg” AND address=“320 FL”) (Student) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student sNumbersNameaddressprofessor 2Greg320FLMM
3
Murali Mani Project SELECT sNumber, sName FROM Student (sNumber, sName) (Student) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student sNumbersName 1Dave 2Greg 3Matt
4
Murali Mani Extended Projection SELECT sNumber || sName AS info FROM Student WHERE address=“320 FL” (sNumber||sName info) ( (address=“320 FL”) (Student)) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student info 1Dave 2Greg 3Matt
5
Murali Mani SQL and Relational Algebra In short, L ( C (R)) becomes SELECT L FROM R WHERE C
6
Murali Mani Renaming SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sNumber >= 1; (s1.sNumber num) ( (s1.sNumber >= 1) ( S1 (Student))) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student num 1 2 3
7
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’
8
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.
9
Murali Mani Truth table with UNKNOWN UNKNOWN AND TRUE = UNKNOWN UNKNOWN OR TRUE = TRUE UNKNOWN AND FALSE = FALSE UNKNOWN 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.
10
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’)
11
Murali Mani EXCEPT - Example sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER 4Matt300FLER Student (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) sNumbersNameaddressprofessor
12
Murali Mani Joins SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; (sName) (Student ⋈ (pName=‘MM’ and pNumber=professor) Professor)
13
Murali Mani Joins - example sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sName Dave Greg SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor;
14
Murali Mani Cross Product (Cartesian Product) SELECT * FROM Student CROSS JOIN Professor; can also be written as: SELECT * FROM Student, Professor Student X Professor
15
Murali Mani Cross Product - Example sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessorpNumberpNameaddress 1Dave320FL11MM141FL 1Dave320FL12ER201FL 2Greg320FL11MM141FL 2Greg320FL12ER201FL 3Matt320FL21MM141FL 3Matt320FL22ER201FL
16
Murali Mani Theta Join SELECT * FROM Student JOIN Professor ON professor=pNumber; Student ⋈ (professor=pNumber) Professor SELECT * FROM Student, Professor WHERE professor=pNumber;
17
Murali Mani Theta Join Example sNumbersNameaddressprofessorpNumberpNameaddress 1Dave320FL11MM141FL 2Greg320FL11MM141FL 3Matt320FL22ER201FL
18
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
19
Murali Mani Natural Join - Example sNumbersNameaddresspNumber 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddresspNumberpNameaddress 1Dave320FL1MM141FL 2Greg320FL1MM141FL 3Matt320FL2ER201FL
20
Murali Mani Outer Joins SELECT * FROM Student NATURAL FULL OUTER JOIN Professor Student ⋈ o Professor SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor Student ⋈ o L Professor
21
Murali Mani Outer Joins SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈ o R Professor
22
Murali Mani Outer Joins - Example sNumbersNameaddresspNumber 1Dave320FL1 2Greg320FL1 3Matt320FL2 4Ben320FL4 Student pNumberpNameaddress 1MM141FL 2ER201FL 3MW168FL Professor sNumbersNameaddresspNumberpNameaddress 1Dave320FL1MM141FL 2Greg320FL1MM141FL 3Matt320FL2ER201FL 4Ben320FL4Null 3MW168FL
23
Murali Mani Sorting: ORDER BY clause SELECT * FROM Student WHERE sNumber >= 1 ORDER BY sNumber, sName (sNumber, sName) ( (sNumber >= 1) (Student))
24
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.
25
Murali Mani Subqueries - Example sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1)
26
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)
27
Murali Mani Subqueries - Example sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor) sNumbersNameaddressprofessor 1Dave320FLMM
28
Murali Mani Subqueries: EXISTS SELECT * FROM Student WHERE EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER
29
Murali Mani Subqueries with negation SELECT * FROM Student WHERE (sNumber, professor) NOT IN (SELECT pNumber, pName FROM Professor) pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student sNumbersNameaddressprofessor 2Greg320FLMM 3Matt320FLER
30
Murali Mani Subqueries with negation SELECT * FROM Student WHERE NOT EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor
31
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)
32
Murali Mani Subqueries: ALL - Example SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 3Matt320FLER
33
Murali Mani Subqueries: ANY - Example SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM
34
Murali Mani Subqueries: NOT ALL - Example SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM
35
Murali Mani Subqueries: NOT ANY - Example SELECT * FROM Student WHERE NOT sNumber = ANY (SELECT pNumber FROM Professor) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 3Matt320FLER
36
Murali Mani Subqueries: Tip
37
Murali Mani Subqueries in FROM clause SELECT sName, pName FROM Student, (SELECT * FROM Professor WHERE pNumber=1) WHERE professor=pName; pNumberpNameaddress 1MM141FL 2ER201FL Professor sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student sNamepName DaveMM GregMM
38
Murali Mani Duplicate Elimination SELECT DISTINCT * FROM Student; (Student) SELECT DISTINCT address FROM Student WHERE sNumber >= 1; ( (address) ( (sNumber >= 1) (Student))) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student address 320FL
39
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
40
Murali Mani Grouping SELECT COUNT (sName) FROM Student GROUP BY address; (COUNT (sName)) ( (address, COUNT (sName)) (Student)) sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER Student COUNT (sName) 3
41
Murali Mani Grouping SELECT address, COUNT (sNumber) FROM Student WHERE sNumber > 1 GROUP BY address HAVING COUNT (sNumber) > 1; sNumbersNameaddressprofessor 1Dave320FLMM 2Greg320FLMM 3Matt320FLER 4Ben300FLER Student addressCOUNT (sNumber) 320FL2
42
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.
43
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]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.