SQL Slides Updated
SQL Slides Updated
SELECT sname
FROM Sailors, Reserves
WHERE Sailors.sid=Reserves.sid AND bid=103
gpa
sid
Student
supervise supervisee
supervise
STUDENTS(SID, SNAME, GPA, SUPERID)
SUPERID is a foreign key referencing SID in
STUDENTS
Query: Find names of students
who
are a supervisor of some student
Expressions and Strings
SELECT S.age, age1=S.age-5, 2*S.age AS age2
FROM Sailors S
WHERE S.sname LIKE ‘B_%B’
Illustrates use of arithmetic expressions and string
pattern matching: Find triples (of ages of sailors and
two fields defined by expressions) for sailors whose
names begin and end with B and contain at least three
characters.
AS and = are two ways to name fields in result.
LIKE is used for string matching. `_’ stands for any one
character and `%’ stands for 0 or more arbitrary
characters.
Find names of sailors who’ve reserved a red or
a green boat
SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid
AND (B.color=‘red’ OR B.color=‘green’
SELECT R.sid
UNION: Can be used to FROM Boats B, Reserves R
compute the union of any WHERE R.bid=B.bid
two union-compatible sets of AND B.color=‘red’
tuples (which are themselves UNION
the result of SQL queries). SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid
AND B.color=‘green’
SELECT R.sid
FROM Boats B, Reserves R
EXCEPT : Used to compute the
WHERE R.bid=B.bid
set difference of two union-
AND B.color=‘red’
compatible sets of tuples EXCEPT
What do we get if we replace SELECT R.sid
UNION with EXCEPT in the FROM Boats B, Reserves R
previous SQL query? WHERE R.bid=B.bid
AND B.color=‘green’
Find sid’s of sailors who’ve reserved a red and a green boat
B1 X R1
R1
B1 BID Colr SID BID
SID BID b1 red s1 b1
BID Colr
s1 b1 b1 red s1 b2
b1 red
b2 grn
s1 b2 b1 red s2 b1
s2 b1 b2 grn s1 b1
b2 grn s1 b2
b2 grn s2 b1
Find sid’s of sailors who’ve reserved
a R2 X B1 X R1
red and a green boat SID BID BID Colr SID BID
S1 b1 b1 red s1 b1
s1 b1 b1 red s1 b2
S1 b1 b1 red s2 b1
s1 b1 b2 grn s1 b1
R2 B1 X R1
s1 b1 b2 grn s1 b2
SID BID BID Colr SID BID
s1 b1 b2 grn s2 b1
b1 red s1 b1
s1 b1
b1 red s1 b2 s1 b2 b1 red s1 b1
s1 b2
b1 red s2 b1 s1 b2 b1 red s1 b2
s2 b1
b2 grn s1 b1 s1 b2 b1 red s2 b1
b2 grn s1 b2 s1 b2 b2 grn s1 b1
b2 grn s2 b1 s1 b2 b2 grn s1 b2
s1 b2 b2 grn s2 b1
s2 b1 b1 red s1 b1
s2 b1 b1 red s1 b2
s2 b1 b1 red s2 b1
s2 b1 b2 grn s1 b1
s2 b1 b2 grn s1 b2
s2 b1 b2 grn s2 b1
B2X R2 X B1 X R1
BID Colr SID BID BID Colr SID BID
SID BID BID Colr SID BID b1 red s1 b1 b1 red s1 b1
b2 grn s1 b1 b2 grn s1 b1
b2 grn s1 b1 b2 grn s1 b2
b2 grn s1 b1 b2 grn s2 b1
b2 grn s1 b2 b1 red s1 b1
b2 grn s1 b2 b1 red s1 b2
b2 grn s1 b2 b1 red s2 b1
b2 grn s1 b2 b2 grn s1 b1
b2 grn s1 b2 b2 grn s1 b2
b2 grn s1 b2 b2 grn s2 b1
b2 grn s2 b1 b1 red s1 b1
b2 grn s2 b1 b1 red s1 b2
b2 grn s2 b1 b1 red s2 b1
b2 grn s2 b1 b2 grn s1 b1
b2 grn s2 b1 b2 grn s1 b2
b2 grn s2 b1 b2 grn s2 b1
Find sid’s of sailors who’ve reserved a red and a
green boat SELECT R.sid
FROM Boats B1, Reserves R1,
Boats B2, Reserves R2
WHERE R1.sid = R2.sid AND
R1.bid=B1.bid AND R2.bid=B2.bid
AND (B1.color=‘red’ AND B2.color=‘green’)
INTERSECT: Can be used
to compute the
intersection of any two
SELECT R.sid
union-compatible sets of
FROM Boats B, Reserves R
tuples.
WHERE R.bid=B.bid
Included in the SQL/92 AND B.color=‘red’
standard, but some INTERSECT
systems don’t support it. SELECT R.sid
FROM Boats B, Reserves R
WHERE R.bid=B.bid
AND B.color=‘green’
Nested Queries
Find names of sailors who’ve reserved boat #
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid=103)
WHERE clause can itself contain an SQL query! (As well as FROM and
HAVING clauses which we will see later on.)
To understand semantics of nested queries, think of a nested loops
evaluation: For each Sailors tuple, check the qualification by computing
the subquery.
For (i=1…10) do {
x=x+1;
For (j=1…5) do {
y=y+1;
}
}
Nested Queries SELECT S.sname
FROM Sailors S
names of sailors who’ve reserved boat #103:
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid=103)
Nested Queries Find names of sailors
who did NOT reserve boat #103:
SELECT S.sname
FROM Sailors S
WHERE S.sid NOT IN (SELECT R.sid
FROM Reserves R
WHERE R.bid=103)
Nested Queries
Find names of sailors
with Correlation who’ve reserved boat #103:
We can use EXISTS operator which is another set comparison operator, like
IN.
SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)
Nested Queries with Correlation
d names of sailors who reserved a boat at most once
SELECT S.sname
FROM Sailors S
WHERE UNIQUE ( SELECT R.bid
FROM Reserves R
WHERE S.sid=R.sid)
More on Set-Comparison
Operators
We’ve already seen IN, EXISTS and UNIQUE. Can also use NO
IN, NOT EXISTS and NOT UNIQUE.
Also available: op ANY, op ALL
Find sailors whose rating is greater than that of some
sailor called Horatio:
SELECT *
FROM Sailors S
WHERE S.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=‘Horatio’)
Rewriting INTERSECT Queries Using
IN
Find names of sailors who’ve reserved both a red
and a green boat:
ECT S.name
OM Sailors S, Boats B, Reserves R
ERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
AND S.sid IN (SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R
WHERE S2.sid=R2.sid AND R2.bid=B2.b
AND B2.color=‘green’)
How would you rewrite queries with
EXCEPT construct?
EXCEPT queries can be re-written using NOT IN.
Division in SQL
ind sailors who’ve reserved all boats.
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS
((SELECT B.bid
FROM Boats B)
EXCEPT
(SELECT R.bid
FROM Reserves R
WHERE R.sid = S.sid))
Division in SQL
ind sailors who’ve reserved all boats.
How can we do it without EXCEPT?
LECT S.sname
OM Sailors S
HERE NOT EXISTS (SELECT B.bid
FROM Boats B
WHERE NOT EXISTS (SELECT R.bid
FROM Reserves R
WHERE R.bid=B.b
AND R.sid=S.
JOINS (SQL 2003 std, source:
wikipedia)
Employee Table
Department Table LastName DepartmentID
Jasper 36
Inner Joins
SELECT *
FROM employee INNER JOIN department
ON employee.DepartmentID = department.DepartmentID
SELECT *
FROM employee, department
WHERE employee.DepartmentID = department.DepartmentID
Inner Joins
Employee.LastName Employee.DepartmentID Department.DepartmentName Department.DepartmentID
Smith 34 Clerical 34
Jones 33 Engineering 33
Robinson 34 Clerical 34
Steinberg 33 Engineering 33
Rafferty 31 Sales 31
Joins
SELECT *
FROM employee NATURAL JOIN department
Smith 34 Clerical
Jones 33 Engineering
Robinson 34 Clerical
Steinberg 33 Engineering
Rafferty 31 Sales
Joins
SELECT *
FROM employee CROSS JOIN department
SELECT *
FROM employee, department;
Joins
SELECT *
FROM employee LEFT OUTER JOIN department
ON department.DepartmentID = employee.DepartmentID
COUNT (*)
COUNT ( [DISTINCT] A)
SUM ( [DISTINCT] A)
AVG ( [DISTINCT] A)
MAX (A)
MIN (A)
COUNT (*)
Aggregate Operators COUNT ( [DISTINCT] A)
SUM ( [DISTINCT] A)
AVG ( [DISTINCT] A)
MAX (A)
Total number of sailors in the club? MIN (A)
Front Back
Write down the following schema
(mindfully)
HAVING group-qualification
GROUP BY grouping-list
gr1
gr2
RESULT
gr3
gr4
gr5
Conceptual SELECT S.rating
FROM Sailors S
Evaluation WHERE S.age >= 18
SELECT S.rating
FROM Sailors S GROUP BY S.rating
WHERE S.age >= 18 HAVING COUNT (*) > 1
Age = 20
Age = 25
HAVING COUNT (*) > 1
Age = 19 GROUP BY S.rating
Rating=1
Age=70 gr1 Rating=1
Rating = 4
Age = 60 Rating =2
Rating =3 gr2 Rating=2
Age = 40 Rating=2 Rating=2
Rating=3 RESULT
Age = 33 Rating=5 Rating=3
Rating=1 Rating=3 Rating = 1
Age = 32 Rating=4 gr3 Rating = 2
Rating=3
Rating=3 Rating = 3
Rating=4 Rating = 4
Age = 18 Rating=1
Rating=4
gr4 Rating=4
Age = 22 Rating=4
Age = 39
gr5 Rating=5
Ecercises on SQL with Group
By
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q17: What is the number of students S2 CS307 A
enrolled in each course? S3 CS300 B
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q18: What is the average GPA of students S2 CS307 A
enrolled in each course? S3 CS300 B
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q19: For each student display the number S2 CS307 A
of courses enrolled in (by that student)? S3 CS300 B
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q19: For each student with GPA greater S2 CS307 A
than 3, display the number of courses S3 CS300 B
enrolled in (by that student)?
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q20: Display the number of students S2 CS307 A
enrolled in each course if it is a large S3 CS300 B
course with more than 2 students.
SID SNAME GPA CID CNAME SID CID GRADE
S1 Ali 2.0 CS306 DB S1 CS306 A
S2 Sara 3.5 CS307 OS S1 CS307 B
S3 Ahmet 2.5 CS300 DS S1 CS300 C
S4 Ayse 4.0 S2 CS300 A
S2 CS306 F
Q21: Display the number of high GPA S2 CS307 A
(greater than 3.5) students enrolled in S3 CS300 B
each course.
Find the age of the youngest sailor with age
18, for each rating with at least 2 such sailors
⋈
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
SELECT EMPLOYEE.NAME
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.MGRSSN = EMPLOYEE.SSN AND
EMPLOYEE.SSN IN
(SELECT DISTINCT ESSN
FROM DEPENDENT)
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Q12: List the names of employees who do not work on a project controlled by
department no 5.
SELECT NAME
FROM EMPLOYEE
WHERE SSN NOT IN (SELECT WORKSON.ESSN
FROM WORKSON, PROJECT
WHERE WORKSON.PNO =
PROJECT.PNUMBER
AND PROJECT.DNUM = 5)
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Q13: List the names of employees who do not work on all projects controlled by
department no 5.
SELECT E.NAME
FROM WORKSON W, EMPLOYEE E
WHERE E.SSN = W.ESSN
AND EXISTS ((SELECT P.PNUMBER
FROM PROJECT P
WHERE P.DNUM = 5)
EXCEPT (SELECT W1.PNO
FROM WORKSON W1
WHERE W1.ESSN = W.ESSN))
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Q13: List the names of employees who do not work on all projects controlled by
department no 5.
SELECT E.NAME
FROM WORKSON W, EMPLOYEE E
WHERE E.SSN = W.ESSN
AND EXISTS (SELECT P.PNUMBER
FROM PROJECT P
WHERE P.DNUM = 5
AND NOT EXISTS (SELECT W1.ESSN
FROM WORKSON W1
WHERE W1.ESSN = W.ESSN
AND W1.PNO =
P.PNUMBER))
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
SELECT NAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Q15: Find the SUM of the salaries of all employees, the max salary,
min salary and average salary.
Q16: Find the SUM of the salaries of all employees, the max salary,
min salary and average salary for research department.
Q19: Display the names of the employees who do not practice birth control
(more than 5 children).
SELECT NAME
FROM EMPLOYEE
WHERE SSN IN (SELECT ESSN
FROM DEPENDENT
WHERE RELATIONSHIP = 'Child'
GROUP BY ESSN
HAVING COUNT(*) > 5)
EMPLOYEE(NAME, SSN, BDATE, ADDRESS, SALARY, SUPERSSN, DNO)
DEPARTMENT(DNAME, DNUMBER, MGRSSN, MGRSTARTDATE)
DEPT_LOCATIONS(DNUMBER, DLOCATION)
PROJECT(PNAME, PNUMBER, PLOCATION, DNUM)
WORKSON(ESSN, PNO, HOURS)
DEPENDENT(ESSN, DEPENDENT_NAME, SEX, BDATE, RELATIONSHIP)
Q20: For each department, retrieve the department number, number of employees
in that department and the average salary.
Q21: For each project, retrieve the project number, the project name, and the number
of employees who work for that project.
Q21: For each project, retrieve the project number, the project name, and the number
of employees who work for that project.
Q22: For each project on which more than two employees work,
retrieve the project number, the project name, and the number
of employees who work on the project.
Q23: For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than 40000.
Number of boats
plus number of
sailors is < 100
X Y Z
t1 1 a p
2 b q
t2 1 a r
2 b p
Example: