0% found this document useful (0 votes)
5 views18 pages

Comp101 Lect08

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

Comp101 Lect08

COMP101 Otago Uni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

COMP101:

Foundations
of Information
Systems

Grant Dick
Department of

Lecture 8:
Information Science

SQL (IV)
More querying
Topics for today

• Recap of last lecture

• Last bits of SQL SELECT

• Building applications supported by databases

1
Recap of last lecture

• Relational operators
• Project, Restrict and Join

• Project: extract some subset of attributes

• Restrict: extract some subset of tuples that match


specified criteria

• Join: combine related tuples of two relations


• Inner vs. outer joins

2
Operators in SQL

SELECT Period, Project


Dept_Code FROM Paper;

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

• Sorting results in ascending or descending order

SELECT Surname,
Mobile_Phone FROM Scientist
ORDER BY Surname;

SELECT Description, Latitude, Longitude


FROM Site
ORDER BY Latitude DESC, Longitude;

5
Subqueries

SELECT * FROM Site WHERE Site_ID NOT IN (


SELECT Site_ID FROM Enrolment
)

• Most frequently used within IN clauses


(see above)

• Can be used to “nest” two complex queries


together (e.g., joining)

6
Aggregate queries

• We often want to compute some sort of aggregate


value from data, e.g.:
• Counting or totalling (sums)
• Statistics (min, max, mean, …)
• String or list concatenation

• SQL provides various aggregate functions

• We often want to aggregate by groups


(e.g., average E. coli reading by day vs. overall mean reading)

7
SQL aggregate functions

SELECT COUNT(*) FROM Scientist;


(counts number of rows in table)

SELECT COUNT(DISTINCT Surname) FROM Scientist;


(counts number of unique values in column)

SELECT SUM(<column name>) FROM

...; SELECT MIN(<column name>)

FROM ...; SELECT MAX(<column

name>) FROM ...; SELECT

AVG(<column name>) FROM ...;


8
Grouping the output: GROUP BY

How many samples has each scientist collected


from each site?

SELECT Site_ID, Scientist_Num, COUNT(*)


FROM Sample
GROUP BY Site_ID, Scientist_Num;

• Groups rows with equal values in specified columns


• Requires an aggregate function (but not vice versa)
• One row in result for each group (i.e., summarised output)

9
Filtering aggregation: HAVING

SELECT Site_ID, COUNT(*)


FROM Site INNER JOIN Sample USING (Site_ID)
GROUP BY Site_ID
HAVING COUNT(*) > 20

• Like a where clause for aggregation queries

10
(Re)naming columns using AS

SELECT Site_ID, COUNT(*) AS


Num_Samples FROM Sample
GROUP BY Site_ID;

• Assign names to on-the-fly columns (e.g., aggregates)

• Useful if you join two tables with


different columns of the same name

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

• Named queries to be used within queries as if


a table.
• Simplifies queries by decomposing queries into
logical subqueries.

12
Re-using queries: Views

• A stored, named query


• aka virtual or derived table

• View body is a SELECT expression


• Only this is stored; no physical data

• Can query like “normal” (base) tables


• Result is generated from the underlying query

13
Example of a view

CREA O REPLA VIE Dunedin_Samp A


TE R CE W les S
SELEC *
T
FROM Sample INNER JOIN Site USING (Site_ID)
WHERE Region = 'Dunedin';

• And then to query the view:


SELECT *
FROM Dunedin_Samples
14
WHERE Recorded_On >= DATE '2022-01-
01';

15
Uses for views

• Offer similar functionality as a WITH clause, but


with greater flexibility

• Encapsulate commonly-executed or complex


queries
• Simplifies application development
• Only exposes necessary data (basic security —
especially if the underlying tables are locked down)

• Break a complex query into simpler re-usable


parts

16
Thanks!
Questions?

You might also like