Comp101 Lect08
Comp101 Lect08
Foundations
of Information
Systems
Grant Dick
Department of
Lecture 8:
Information Science
SQL (IV)
More querying
Topics for today
1
Recap of last lecture
• Relational operators
• Project, Restrict and Join
2
Operators in SQL
SELECT *
FROM Paper
WHERE Dept_Code =
'INFO';
Restrict
SELECT *
FROM Paper INNER JOIN
Department USING Inner Join
(Dept_Code);
3
Additional SQL features
Some handy tips and tools
4
Sorting the output: ORDER BY
SELECT Surname,
Mobile_Phone FROM Scientist
ORDER BY Surname;
5
Subqueries
6
Aggregate queries
7
SQL aggregate functions
9
Filtering aggregation: HAVING
10
(Re)naming columns using AS
11
Complex subqueries – WITH clauses
WITH Region_Sampled AS (
SELECT Site_ID, Region, Scientist_Num
FROM Site INNER JOIN Sample USING (Site_ID)
) SELECT Scientist_Num, Region, COUNT(*) AS
Number_Sampled FROM Scientist INNER JOIN
Region_Sampled USING (Scientist_Num) GROUP BY
Scientist_Num, Region
12
Re-using queries: Views
13
Example of a view
15
Uses for views
16
Thanks!
Questions?