t3 Simple SQL
t3 Simple SQL
Simple SQL
• Question
– Given the relation schemas, write the following queries in SQL.
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: string, room: string, fid: integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
– Q1: Find the department id of the faculty member named I. Teach.
– Q2: Find the names of all junior students (level=‘JR’), and list in the order of age.
– Q3: Find the number of classes that have an enrolment greater than 0.
• Key points
– Understand the semantics
• Entities: Student, Class, Faculty; Relationships: Enrolment, Teaching (where is the schema for it?)
• Meaning of attributes, keys, foreign keys, …
• Answer
Q1: Q2: Q3:
SELECT deptid SELECT S.sname SELECT COUNT(DISTINCT E.cname)
FROM Faculty FROM Student S FROM Enrolled as E
WHERE fname = ‘I.Teach’ WHERE S.level = ‘JR’
ORDERED BY S.age
1
Simple SQL: Join
• Question
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: string, room: string, fid: integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
– Q4: Find names and majors of students who have enrolled in at least
one class.
– Q5: Find the number of students who have enrolled in at least two
classes.
• Answer
Q4: Q5:
SELECT S.sname, S.major SELECT COUNT(DISTINCT S.sname)
FROM Student S, Enrolled E FROM Student S, Enrolled E1, Enrolled E2
WHERE S.snum = E.snum WHERE E1.snum = E2.snum AND
E1.cnum <> E2.cnum AND
S.snum = E1.snum
– (E5.1.1) Q6: Find distinct names of all Juniors (level = JR) enrolled in a
class taught by I. Teach.
• Answer
Q6: specify what is asked (2)
SELECT DISTINCT S.sname as Student_Name
FROM Student S, Class C, Enrolled E, Faculty F All relations involved (1, 2, 3)
WHERE S.snum = E.snum AND E.cname = C.name AND C.fid = F.fid Specify connections (3)
AND F.fname = ‘I.Teach’ AND S.level = ‘JR’ Specifying what is given (1)
CSC343: Intro. to Databases 4
2
Simple SQL: Join
• Question
– Given following database instance, answer Q6.
Student Enrolled Class
snum sname major level age snum cname name meets_at room fid
101 Helen CS JR 19 101 CSC343 CSC343 W1 BA1180 201
102 Charles CS SR 21 101 CSC443 CSC443 T2 BA1170 202
103 Andy CS GR 25 101 ECE300 ECE300 M1 BA1180 203
104 Bob CS SR 23 102 CSC343 ECE201 F12 BA1160 203
105 Zorba CS GR 31 102 ECE300 CSC165 R3 BA1170 202
103 CSC343 Faculty
Q6:
SELECT DISTINCT S.sname as Student_Name 103 CSC443 fid fname deptid
FROM Student S, Class C, Enrolled E, Faculty F 103 ECE300 201 S. Jackson 301
WHERE S.snum = E.snum AND E.cname = C.name 103 ECE201 202 M. Shanks 301
AND C.fid = F.fid
AND F.fname = ‘I.Teach’ AND S.level = ‘JR’ 105 CSC343 203 I. Teach 302
• Answer
Student_Name
Helen
• Answer
Student_Name
Charles
Zorba
CSC343: Intro. to Databases 6
3
Simple SQL: Set Operation
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: string, room: string, fid: integer)
• Question Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
– Q7: Find the names of all students who have enrolled in both CSC343 and CSC443.
• Answer
Q7 Sol#1:
SELECT DISTINCT S.sname as Student_Name
FROM Student S, Enrolled E
WHERE S.snum = E.snum AND E.cnum = ‘CSC343’
INTERSECT
SELECT DISTINCT S2.sname as Student_Name Q7 Sol#2:
FROM Student S2, Enrolled E2 SELECT DISTINCT S.sname
WHERE S2.snum = E2.snum AND E2.cnum = ‘CSC443’ FROM Student S, Enrolled E1, Enrolled E2
WHERE S.snum = E1.snum AND
E1.snum = E2.snum AND
E1.cnum = ‘CSC343’ AND
E2.cnum = ‘CSC443’
• Answer
4
Simple SQL: Outer Join
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets_at: string, room: string, fid: integer)
• Question Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
– Q9: Find the names of all students and the names of all classes they are enrolled in (if
any)
• Key points
– Some students may have not been enrolled in any course
– we cannot exclude them from the list
– use left outer join the handle this situation.
• Answer
Q9:
SELECT S.sname, E.cname
FROM Student S LEFT JOIN Enrolled E ON S.snum = E.snum