0% found this document useful (0 votes)
88 views5 pages

Simple SQL Queries Tutorial

This document provides examples of simple SQL queries to retrieve information from given database schemas. It includes 8 questions with corresponding SQL queries to: 1) find a faculty member's department id; 2) find names and order of junior students; 3) count classes with enrollments; 4) find names and majors of enrolled students; 5) count students enrolled in two classes; 6) find names of juniors enrolled in a specific class; 7) find names of students enrolled in two specific classes; and 8) print levels and average ages for all levels except junior. The document demonstrates basic SQL queries using selection, projection, joins, grouping and aggregation.

Uploaded by

farhan mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views5 pages

Simple SQL Queries Tutorial

This document provides examples of simple SQL queries to retrieve information from given database schemas. It includes 8 questions with corresponding SQL queries to: 1) find a faculty member's department id; 2) find names and order of junior students; 3) count classes with enrollments; 4) find names and majors of enrolled students; 5) count students enrolled in two classes; 6) find names of juniors enrolled in a specific class; 7) find names of students enrolled in two specific classes; and 8) print levels and average ages for all levels except junior. The document demonstrates basic SQL queries using selection, projection, joins, grouping and aggregation.

Uploaded by

farhan mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 3 – Simple SQL

CSC343 - Introduction to Databases


Fall 2008

TA: Lei Jiang

CSC343: Intro. to Databases 1

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 [Link] SELECT COUNT(DISTINCT [Link])
FROM Faculty FROM Student S FROM Enrolled as E
WHERE fname = ‘[Link]’ WHERE [Link] = ‘JR’
ORDERED BY [Link]

CSC343: Intro. to Databases 2

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 [Link], [Link] SELECT COUNT(DISTINCT [Link])
FROM Student S, Enrolled E FROM Student S, Enrolled E1, Enrolled E2
WHERE [Link] = [Link] WHERE [Link] = [Link] AND
[Link] <> [Link] AND
[Link] = [Link]

CSC343: Intro. to Databases 3

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)

– (E5.1.1) Q6: Find distinct names of all Juniors (level = JR) enrolled in a
class taught by I. Teach.

• The way of thinking


• Given: [Link] = ‘JR’ and [Link] = ‘[Link]’ …………………………… (1)
• Asked: distinct [Link] values ……………………………………….………….. (2)
• Connection: Student <~> Enrolled <~> Course <~> Faculty ………………..... (3)

• Answer
Q6: specify what is asked (2)
SELECT DISTINCT [Link] as Student_Name
FROM Student S, Class C, Enrolled E, Faculty F All relations involved (1, 2, 3)
WHERE [Link] = [Link] AND [Link] = [Link] AND [Link] = [Link] Specify connections (3)
AND [Link] = ‘[Link]’ AND [Link] = ‘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 [Link] as Student_Name 103 CSC443 fid fname deptid
FROM Student S, Class C, Enrolled E, Faculty F 103 ECE300 201 S. Jackson 301
WHERE [Link] = [Link] AND [Link] = [Link] 103 ECE201 202 M. Shanks 301
AND [Link] = [Link]
AND [Link] = ‘[Link]’ AND [Link] = ‘JR’ 105 CSC343 203 I. Teach 302

• Answer
Student_Name
Helen

CSC343: Intro. to Databases 5

Simple SQL: Set Operation


• Question
– Given following database instance, answer Q7.
Student Enrolled Class
snum sname major level age snum cname name meets_at room fid
101 Helen CS JR 19 101 CSC343 CSC343 W1 BA1080 201
102 Charles CS SR 21 101 CSC443 CSC443 T2 BA1170 202
103 Andy CS GR 25 101 ECE300 ECE300 M1 BA1080 203
104 Bob CS SR 23 102 CSC343 ECE201 F12 BA1060 203
105 Zorba CS GR 31 102 ECE201 CSC165 R3 BA1170 202
Q7: 103 CSC343 Faculty
SELECT DISTINCT [Link] as Student_Name 103 CSC443 fid fname deptid
FROM Student S, Enrolled E
103 ECE300 201 S. Jackson 301
WHERE [Link] = [Link] AND [Link] = ‘CSC343’
EXCEPT 103 ECE201 202 M. Shanks 301
SELECT DISTINCT [Link] 105 CSC343 203 I. Teach 302
FROM Student S2, Enrolled E2
WHERE [Link] = [Link] AND [Link] = ‘CSC443’

• 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 [Link] as Student_Name
FROM Student S, Enrolled E
WHERE [Link] = [Link] AND [Link] = ‘CSC343’
INTERSECT
SELECT DISTINCT [Link] as Student_Name Q7 Sol#2:
FROM Student S2, Enrolled E2 SELECT DISTINCT [Link]
WHERE [Link] = [Link] AND [Link] = ‘CSC443’ FROM Student S, Enrolled E1, Enrolled E2
WHERE [Link] = [Link] AND
[Link] = [Link] AND
[Link] = ‘CSC343’ AND
[Link] = ‘CSC443’

CSC343: Intro. to Databases 7

Simple SQL: Aggregation


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)
– (E5.1.8) Q8: For all levels except JR, print the level and the average age of students for
that level.
• The way of thinking

snum level age level snum age


101 JR 19 Group by JR 101 19 Take average level age snum
102 SR 21 level SR 102 21 on age JR 19 ?
103 GR 25 104 23 SR 22 ?
104 SR 23 GR 103 25 GR 28 ?
105 GR 31 105 31

• Answer

Q8: 1. An attribute attr is allowed in the select clause if


(a) it appears in the group by clause or
SELECT [Link], AVG([Link]) (b) It’s used with an aggregation function
FROM Student S 2. One exception: if you group by the primary key of a
WHERE [Link] <> ‘JR’ relation
GROUP BY [Link] (a) Every attribute of that relation can be used
(b) Why?

CSC343: Intro. to Databases 8

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 [Link], [Link]
FROM Student S LEFT JOIN Enrolled E ON [Link] = [Link]

CSC343: Intro. to Databases 9

You might also like