SQL Queries
SQL Queries
SQL Queries
Murali Mani
SELECT-FROM-WHERE
SELECT * FROM Student WHERE sName=Greg AND address=320 FL Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 2 sName Greg address 320FL professor MM
Project
SELECT sNumber, sName FROM Student
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 1 2 3 sName Dave Greg Matt
Extended Projection
SELECT sNumber || sName AS info FROM Student WHERE address=320 FL
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER info 1Dave 2Greg 3Matt
Murali Mani
Renaming
SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sNumber >= 1;
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER num 1 2 3
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
Murali Mani
Murali Mani
EXCEPT - Example
(SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=320 FL) Student
sNumber 1 2 3 4 sName Dave Greg Matt Matt address 320FL 320FL 320FL 300FL professor MM MM ER ER sNumber sName address professor
Murali Mani
Joins
SELECT sName FROM Student, Professor WHERE pName=MM AND pNumber=professor;
Murali Mani
Joins - example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor 1 1 2 1 2
Professor
pNumber pName MM ER address 141FL 201FL
Professor
pNumber 1 2 pName MM ER address 141FL 201FL
pNumber 1 2 1 2 1 2
pName MM ER MM ER MM ER
Theta Join
SELECT * FROM Student JOIN Professor ON professor=pNumber;
Student (professor=pNumber) Professor
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
Professor
pNumber pName MM ER address 141FL 201FL
Murali Mani
Outer Joins
SELECT * FROM Student NATURAL FULL OUTER JOIN Professor
Student o Professor
Murali Mani
10
Outer Joins
SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor
Student oR Professor
Murali Mani
Professor
pNumber 1 2 3 pName MM ER MW address 141FL 201FL 168FL
pName MM MM ER Null MW
11
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
12
Subqueries - Example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER
SELECT * FROM Student WHERE professor = (SELECT pName FROM Professor WHERE pNumber=1)
Professor
pNumber 1 2 pName MM ER address 141FL 201FL sNumber 1 2 sName Dave Greg address 320FL 320FL professor MM MM
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
13
Subqueries - Example
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER
Professor
pNumber 1 2 pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE (sNumber, professor) IN (SELECT pNumber, pName FROM Professor)
sNumber 1
sName Dave
address 320FL
professor MM
Murali Mani
Subqueries: EXISTS
SELECT * FROM Student WHERE EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName)
Professor
pNumber 1 2 pName MM ER address 141FL 201FL
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER
Murali Mani
14
Professor
pNumber 1 2 pName MM ER address 141FL 201FL
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sNumber 2 3 sName Greg Matt address professor 320FL 320FL MM ER
Murali Mani
Professor
pNumber pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE NOT EXISTS (SELECT pName FROM Professor WHERE Student.professor=pName)
sNumber sName address professor
Murali Mani
15
Professor
pNumber pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE sNumber > ALL (SELECT pNumber FROM Professor)
sNumber 3
sName Matt
Murali Mani
16
Professor
pNumber pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE sNumber = ANY (SELECT pNumber FROM Professor)
sNumber 1 2
professor MM MM
Murali Mani
Professor
pNumber pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE NOT sNumber > ALL (SELECT pNumber FROM Professor)
sNumber 1 2
professor MM MM
Murali Mani
17
Professor
pNumber pName MM ER address 141FL 201FL
SELECT * FROM Student WHERE NOT sNumber = ANY (SELECT pNumber FROM Professor)
sNumber 3
sName Matt
Murali Mani
Subqueries: Tip
Murali Mani
18
Professor
pNumber 1 2 pName MM ER address 141FL 201FL
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL professor MM MM ER sName Dave Greg pName MM MM
Murali Mani
Duplicate Elimination
SELECT DISTINCT * FROM Student;
(Student)
Student
sNumber 1 2 3
professor MM MM ER
address 320FL
Murali Mani
19
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))
Student
sNumber 1 2 3 sName Dave Greg Matt address 320FL 320FL 320FL
Murali Mani
20
Grouping
SELECT address, COUNT (sNumber) FROM Student WHERE sNumber > 1 GROUP BY address HAVING COUNT (sNumber) > 1; Student
sNumber 1 2 3 4 sName Dave Greg Matt Ben address 320FL 320FL 320FL 300FL professor MM MM ER ER
Murali Mani
address 320FL
COUNT (sNumber) 2
Murali Mani
21
Murali Mani
22