0% found this document useful (0 votes)
7 views

Lecture 6 SQL Part 2

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)
7 views

Lecture 6 SQL Part 2

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
You are on page 1/ 56

International University, VNU-HCMC

School of Computer Science and Engineering

Lecture 6: SQL part 2

Instructor: Nguyen Thi Thuy Loan


[email protected], [email protected]
https://nttloan.wordpress.com/

International University, VNU-HCMC

Recap: Lecture 3
• SQL Data types
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Create database, tables


• Create constraints

– Reading material: [RG] Chapters 3 and 5


– Additional reading for practice: [GUW]
Chapter 6

Duke CS, Fall 2021 2

1
International University, VNU-HCMC

Acknowledgement

• The following slides have been created adapting the


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

instructor material of the [RG] book provided by the


authors Dr. Ramakrishnan and Dr. Gehrke.
• Other slides are referenced from Dr. Sudeepa Roy,
Duke University and Prof. Jeffrey D. Ullman.

Duke CS, Fall 2021 3

International University, VNU-HCMC

Today’s topic

• More SQL
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Extended algebra
– Semantic
– Joins
– Group by and aggregates
– Nested queries

Duke CS, Fall 2021 4

2
International University, VNU-HCMC

The Extended Algebra

δ = eliminate duplicates from bags.


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

τ = sort tuples.
γ = grouping and aggregation.
Outerjoin: avoids “dangling tuples” =
tuples that do not join with anything.

Duke CS, Fall 2021 5

International University, VNU-HCMC

Duplicate Elimination
• R1 := δ(R2).
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• R1 consists of one copy of each tuple that


appears in R2 one or more times.

Duke CS, Fall 2021 6

3
International University, VNU-HCMC

Example: Duplicate Elimination


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

R= (A B)
1 2
3 4
1 2

δ(R) = A B
1 2
3 4

Duke CS, Fall 2021 7

International University, VNU-HCMC

Sorting

• R1 := τL (R2).
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– L is a list of some of the attributes of R2.


• R1 is the list of tuples of R2 sorted first on
the value of the first attribute on L, then on
the second attribute of L, and so on.
– Break ties arbitrarily.
• τ is the only operator whose result is neither
a set nor a bag.

Duke CS, Fall 2021 8

4
International University, VNU-HCMC

Example: Sorting
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

R= (A B)
1 2
3 4
5 2

τB (R) = [(5,2), (1,2), (3,4)]

Duke CS, Fall 2021 9

International University, VNU-HCMC

Aggregation Operators
• Aggregation operators are not operators of
relational algebra.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Rather, they apply to entire columns of a


table and produce a single result.
• The most important examples: SUM, AVG,
COUNT, MIN, and MAX.

Duke CS, Fall 2021 10

5
International University, VNU-HCMC

Example: Aggregation
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

R= (A B)
1 3
3 4
3 2

SUM(A) = 7
COUNT(A) = 3
MAX(B) = 4
AVG(B) = 3

Duke CS, Fall 2021 11

International University, VNU-HCMC

Grouping Operator
• R1 := γL (R2). L is a list of elements
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

that are either:


1. Individual (grouping) attributes.
2. AGG(A), where AGG is one of the aggregation
operators and A is an attribute.
• An arrow and a new attribute name rename the
component.

Duke CS, Fall 2021 12

6
International University, VNU-HCMC

Applying γL(R)
• Group R according to all the grouping
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

attributes on list L.
– That is: form one group for each distinct list
of values for those attributes in R.
• Within each group, compute AGG(A) for
each aggregation on list L.
• Result has one tuple for each group:
1. The grouping attributes and
2. Their group’s aggregations.

Duke CS, Fall 2021 13

International University, VNU-HCMC

Example: Grouping/Aggregation

R= (A B C)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

1 2 3 Then, average C
4 5 6 within groups:
1 2 5
A B X
γA,B,AVG(C)->X (R) = ?? 1 2 4
First, group R by A and B : 4 5 6
A B C
1 2 3
1 2 5
4 5 6

Duke CS, Fall 2021 14

7
International University, VNU-HCMC

Outerjoin

• Suppose we join R ⋈C S.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• A tuple of R that has no tuple of S with


which it joins is said to be dangling.
– Similarly for a tuple of S.
• Outerjoin preserves dangling tuples by
padding them NULL.

Duke CS, Fall 2021 15

International University, VNU-HCMC

Example: Outerjoin
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

R= (A B) S= (B C)
1 2 2 3
4 5 6 7

(1,2) joins with (2,3), but the other two tuples


are dangling.
R OUTERJOIN S = A B C
1 2 3
4 5 NULL
NULL 6 7

Duke CS, Fall 2021 16

8
International University, VNU-HCMC

Basic SQL Query


SELECT [DISTINCT] <target-list>
FROM <relation-list>
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE <qualification>

• relation-list A list of relation names


– possibly with a “range variable” after each name
• target-list A list of attributes of relations in relation-list
• Qualification Comparisons
– (Attr op const) or (Attr1 op Attr2)
– where op is one of = , <, >, <=, >= combined using AND, OR and
NOT
• DISTINCT is an optional keyword indicating that the
answer should not contain duplicates
– Default is that duplicates are not eliminated!
Duke CS, Fall 2021 17

International University, VNU-HCMC

Basic SQL Query


SQL SELECT Statement
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT column1, column2....columnN


FROM table_name

SQL DISTINCT Clause


SELECT DISTINCT column1, column2....columnN
FROM table_name

SQL WHERE Clause


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;
Duke CS, Fall 2021 18

9
International University, VNU-HCMC

Basic SQL Query

SQL AND/OR Clause


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT column1, column2....columnN


FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;

SQL IN Clause
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);

Duke CS, Fall 2021 19

International University, VNU-HCMC

Basic SQL Query

SQL BETWEEN Clause


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT column1, column2....columnN


FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;

SQL LIKE Clause


SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };

Duke CS, Fall 2021 20

10
International University, VNU-HCMC

Basic SQL Query


SQL ORDER BY Clause
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT column1, column2....columnN


FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY Clause


SELECT Count(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;
Duke CS, Fall 2021 21

International University, VNU-HCMC

Basic SQL Query


SQL COUNT Clause
SELECT COUNT(column_name)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM table_name
WHERE CONDITION;

SQL HAVING Clause


SELECT Count(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithmetic function condition);

Duke CS, Fall 2021 22

11
International University, VNU-HCMC

Conceptual Evaluation Strategy


SELECT [DISTINCT] <target-list>
FROM <relation-list>
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE <qualification>

• Semantics of an SQL query defined in terms of the


following conceptual evaluation strategy:
– Compute the cross-product of <relation-list>
– Discard resulting tuples if they fail <qualifications>
– Delete attributes that are not in <target-list>
– If DISTINCT is specified, eliminate duplicate rows
• This strategy is probably the least efficient way to compute
a query!
– An optimizer will find more efficient strategies to
compute the same answers
Duke CS, Fall 2021 23

International University, VNU-HCMC

Conceptual Evaluation Strategy


Find sailor names who’ve reserved a boat #103 Sailor
sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.sname 22 dustin 7 45


FROM Sailors S, Reserves R 31 lubber 8 55
WHERE S.sid=R.sid AND R.bid=103
58 rusty 10 35
Step 1: Form cross product of Sailor and Reserves
sid sname rating age sid bid day Reserves
22 dustin 7 45 22 101 10/10/96 sid bid day
22 dustin 7 45 58 103 11/12/96 22 101 10/10/96
31 lubber 8 55 22 101 10/10/96 58 103 11/12/96
31 lubber 8 55 58 103 11/12/96
58 rusty 10 35 22 101 10/10/96
58 rusty 10 35 58 103 11/12/96
Duke CS, Fall 2021 24

12
International University, VNU-HCMC

Conceptual Evaluation Strategy


Sailor
SELECT S.sname sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S, Reserves R 22 dustin 7 45


WHERE S.sid=R.sid AND R.bid=103
31 lubber 8 55
58 rusty 10 35
Step 2: Discard tuples that do not satisfy <qualification>
sid sname rating age sid bid day Reserves
22 dustin 7 45 22 101 10/10/96 sid bid day
22 dustin 7 45 58 103 11/12/96 22 101 10/10/96
31 lubber 8 55 22 101 10/10/96 58 103 11/12/96
31 lubber 8 55 58 103 11/12/96
58 rusty 10 35 22 101 10/10/96
58 rusty 10 35 58 103 11/12/96
Duke CS, Fall 2021 25

International University, VNU-HCMC

Conceptual Evaluation Strategy


Sailor
SELECT S.sname sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S, Reserves R 22 dustin 7 45


WHERE S.sid=R.sid AND R.bid=103
31 lubber 8 55
58 rusty 10 35
Step 3: Select the specified attribute(s)
sid sname rating age sid bid day Reserves
22 dustin 7 45 22 101 10/10/96 sid bid day
22 dustin 7 45 58 103 11/12/96 22 101 10/10/96
31 lubber 8 55 22 101 10/10/96 58 103 11/12/96
31 lubber 8 55 58 103 11/12/96
58 rusty 10 35 22 101 10/10/96
58 rusty 10 35 58 103 11/12/96
Duke CS, Fall 2021 26

13
International University, VNU-HCMC

A Note on “Alias”
• You can rename a table or a column temporarily by
giving another name known as Alias
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Really needed only if the same relation appears twice


in the FROM clause
– sometimes used as a short-name
• The previous query can also be written as:
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND bid=103
OR It is good style,
SELECT sname however, to use
FROM Sailors, Reserves alias (range
variables) always!
WHERE Sailors.sid=Reserves.sid
AND bid=103
Duke CS, Fall 2021 27

International University, VNU-HCMC

Find sailor ids who’ve reserved at least one boat


Sailor
SELECT ???? sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S, Reserves R 22 dustin 7 45


WHERE S.sid=R.sid
31 lubber 8 55
58 rusty 10 35

Reserves
sid bid day
22 101 10/10/19
58 103 11/12/19

Duke CS, Fall 2021 28

14
International University, VNU-HCMC

Find sailor ids who’ve reserved at least one boat


Sailor
SELECT S.sid sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S, Reserves R 22 dustin 7 45


WHERE S.sid=R.sid
31 lubber 8 55
58 rusty 10 35
• Would adding DISTINCT to this
query make a difference? Reserves
sid bid day
22 101 10/10/19
58 103 11/12/19

Duke CS, Fall 2021 29

International University, VNU-HCMC

Find sailor ids who’ve reserved at least one boat


Sailor
SELECT S.sid
FROM Sailors S, Reserves R sid sname rating age
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.sid=R.sid 22 dustin 7 45


• Would adding DISTINCT to this query 31 lubber 8 55
make a difference? 58 rusty 10 35
– Note that if there are multiple bids for
the same sid, you get multiple output
Reserves
tuples for the same sid
– Without distinct, you get them sid bid day
multiple times 22 101 10/10/19
• What is the effect of replacing S.sid by 58 103 11/12/19
S.sname in the SELECT clause?
– Would adding DISTINCT to this variant of
the query make a difference even if one
sid reserves at most one bid?
Duke CS, Fall 2021 30

15
International University, VNU-HCMC

Example
• To find all 18 year old students, we can write:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

all attributes

SELECT * sid name login age gpa


FROM Students S 53666 Jones jones@cs 18 3.4
WHERE S.age=18
53688 Smith smith@ee 19 3.2

•To find just names and logins, replace the first line:

SELECT S.name, S.login

Duke CS, Fall 2021 31

International University, VNU-HCMC

Querying Multiple Relations


SELECT S.name, E.cid
• What does the following FROM Students S, Enrolled E
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

query compute? WHERE S.sid=E.sid AND E.grade=‘A'


• Given the following Enrolled
instances of Enrolled
sid cid grade
and Students:
53831 Carnatic101 C
Students 53831 Reggae203 B
53650 Topology112 A
sid name login age gpa
53666 History105 B
53666 Jones jones@cs 18 3.4
we get: ??
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Duke CS, Fall 2021 32

16
International University, VNU-HCMC

Querying Multiple Relations


SELECT S.name, E.cid
• What does the following FROM Students S, Enrolled E
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

query compute? WHERE S.sid=E.sid AND E.grade=‘A'


• Given the following Enrolled
instances of Enrolled
sid cid grade
and Students:
53831 Carnatic101 C
Students 53831 Reggae203 B
53650 Topology112 A
sid name login age gpa
53666 History105 B
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2 we get:

53650 Smith smith@math 19 3.8 S.name E.cid


Smith Topology112

Duke CS, Fall 2021 33

International University, VNU-HCMC

Joins
sid sname rating age
• Condition/Theta-Join 22 dustin 7 45
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

31 lubber 8 55
• Equi-Join
58 rusty 10 35
• Natural-Join
• (Left/Right/Full) Outer-
Join sid bid day
22 101 10/10/19
58 103 11/12/19

Duke CS, Fall 2021 34

17
International University, VNU-HCMC

Condition/Theta Join
sid sname rating age
SELECT *
FROM Sailors S, Reserves R 22 dustin 7 45
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.sid=R.sid and age >= 40 31 lubber 8 55


58 rusty 10 35
Form cross-product, discard rows that do not satisfy the condition

sid sname rating age sid bid day sid bid day
22 dustin 7 45 22 101 10/10/19 22 101 10/10/19
22 dustin 7 45 58 103 11/12/19 58 103 11/12/19
31 lubber 8 55 22 101 10/10/19
31 lubber 8 55 58 103 11/12/19
58 rusty 10 35 22 101 10/10/19
58 rusty 10 35 58 103 11/12/19

Duke CS, Fall 2021 35

International University, VNU-HCMC

Equi Join
sid sname rating age
SELECT *
FROM Sailors S, Reserves R 22 dustin 7 45
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.sid=R.sid and age = 45 31 lubber 8 55


58 rusty 10 35
A special case of theta join
Join condition only has equality predicate =

sid sname rating age sid bid Day sid bid day
22 dustin 7 45 22 101 10/10/19 22 101 10/10/19
22 dustin 7 45 58 103 11/12/19 58 103 11/12/19
31 lubber 8 55 22 101 10/10/19
31 lubber 8 55 58 103 11/12/19
58 rusty 10 35 22 101 10/10/19
58 rusty 10 35 58 103 11/12/19

Duke CS, Fall 2021 36

18
International University, VNU-HCMC

Natural Join
sid sname rating age
SELECT *
22 dustin 7 45
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S NATURAL JOIN Reserves R


31 lubber 8 55
58 rusty 10 35
A special case of equi- join
Equality condition on ALL common predicates (sid)
Duplicate columns are eliminated
sid bid day
sid sname rating age bid day 22 101 10/10/19
22 dustin 7 45 101 10/10/19 58 103 11/12/19
22 dustin 7 45 103 11/12/19
31 lubber 8 55 101 10/10/19
31 lubber 8 55 103 11/12/19
58 rusty 10 35 101 10/10/19
58 rusty 10 35 103 11/12/19
Duke CS, Fall 2021 37

International University, VNU-HCMC

Outer Join
sid sname rating age
SELECT S.sid, R. bid
22 dustin 7 45
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S LEFT OUTER JOIN Reserves R


ON S.sid=R.sid 31 lubber 8 55
58 rusty 10 35
Preserves all tuples from the left table
whether or not there is a match if no sid bid day
match, fill attributes from right with null
22 101 10/10/19
Similarly RIGHT/FULL outer join
58 103 11/12/19
sid bid
22 101
31 null
58 103

Duke CS, Fall 2021 38

19
International University, VNU-HCMC

Expressions and Strings


SELECT S.age, age1=S.age-5, 2*S.age AS age2
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

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
• LIKE is used for string matching. ‘_’ stands for any
one character and ‘%’ stands for 0 or more arbitrary
characters
– You will need these often
Duke CS, Fall 2021 39

International University, VNU-HCMC

Like Operator
There are two wildcards often used in
conjunction with the LIKE operator:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• The percent sign (%) represents zero, one, or


multiple characters
• The underscore sign (_) represents one, single
character

• SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

Duke CS, Fall 2021 40

20
International University, VNU-HCMC

Example
• SELECT * FROM Customers
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE CustomerName LIKE 'a%’;


• SELECT * FROM Customers
WHERE CustomerName LIKE '%a’;
• SELECT * FROM Customers
WHERE CustomerName LIKE ’_u%’;
• SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';

Duke CS, Fall 2021 41

International University, VNU-HCMC

UNION Clause/Operator
The SQL UNION clause/operator is used to combine the
results of two or more SELECT statements without
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

returning any duplicate rows.


UNION ALL operator is used to combine the results of two
SELECT statements including duplicate rows

To use this UNION clause, each SELECT statement must


have
§ The same number of column
§ The same data type and
§ Have them in the same order

But they need not have to be in the same length.

Duke CS, Fall 2021 42

21
International University, VNU-HCMC

Syntax of a UNION
SELECT column1 [, column2 ]
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM table1 [, table2 ]


[WHERE condition]

UNION/ UNION ALL

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]

Duke CS, Fall 2021 43

International University, VNU-HCMC

INTERSECT Clause/Operator

The SQL INTERSECT returns only common rows returned


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

by the two SELECT statements.

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]

INTERSECT

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]

Duke CS, Fall 2021 44

22
International University, VNU-HCMC

EXCEPT Clause/Operator
The SQL EXCEPT returns only rows, which are
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

not available in the second SELECT statement.

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]
EXCEPT
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

Duke CS, Fall 2021 45

International University, VNU-HCMC

SQL set and bag operations


• UNION, EXCEPT, INTERSECR
– Set semantics
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Exactly like set È, -, and Ç in relational algebra.


• UNION ALL, EXCEPT ALL, INTERSECT ALL
– Bag semantics
– Think of each row as having implicit count (the
number of times it appears in the table)
– Bag union: sum up the counts from two tables
– Bag difference: proper-subtract the two counts
– Bag intersection: take the minimum of the two
counts.

Duke CS, Fall 2021 46

23
International University, VNU-HCMC

Examples of bag operations


Bag1 Bag2
fruit fruit
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

apple apple
apple orange
orange orange

(SELECT * FROM Bag1) (SELECT * FROM Bag1) (SELECT * FROM Bag1)


UNION ALL EXCEPT ALL INTERSECT ALL
(SELECT * FROM Bag2); (SELECT * FROM Bag2); (SELECT * FROM Bag2);
fruit fruit fruit
apple apple apple
apple orange
orange
apple
orange
orange

Duke CS, Fall 2021 47

International University, VNU-HCMC


Example: Find sid’s of sailors who’ve reserved a red or a
green boat

Sailors (sid, sname, rating, age) SELECT S.sid


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Reserves(sid, bid, day) FROM Sailors S, Boats B, Reserves R


Boats(bid, bname, color) WHERE S.sid=R.sid AND R.bid=B.bid AND
(B.color=‘red’ OR B.color=‘green’)
• Assume a Boats relation
• If we replace OR by AND in
SELECT S.sid
the first version, what do FROM Sailors S, Boats B, Reserves R
we get? WHERE S.sid=R.sid AND R.bid=B.bid
AND B.color=‘red’
• Also available: EXCEPT
UNION
(What do we get if we SELECT S.sid
replace UNION by EXCEPT?) FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid
AND B.color=‘green’

Duke CS, Fall 2021 48

24
International University, VNU-HCMC Sailors (sid,sname,rating,age)
Reserves(sid, bid, day)
INTERSECT Clause/Operator Boats(bid, bname, color)

Find sname’s of sailors who’ve


reserved a red and a green boat
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 49

International University, VNU-HCMC Sailors (sid,sname,rating,age)


Reserves(sid, bid, day)
INTERSECT Clause/Operator Boats(bid, bname, color)

Find sid’s of sailors SELECT S.sid


who’ve reserved a FROM Sailors S, Boats B1, Reserves R1,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Boats B2, Reserves R2


red and a green boat WHERE S.sid=R1.sid AND R1.bid=B1.bid
AND S.sid=R2.sid AND R2.bid=B2.bid
AND (B1.color=‘red’ AND B2.color=‘green’)
Key field!

SELECT S.sid
• INTERSECT: Can be used to
FROM Sailors S, Boats B, Reserves R
compute the intersection of
any two union-compatible WHERE S.sid=R.sid AND R.bid=B.bid
sets of tuples. AND B.color=‘red’
– Included in the SQL/92 INTERSECT
SELECT S.sid
standard, but some
FROM Sailors S, Boats B, Reserves R
systems don’t support it
WHERE S.sid=R.sid AND R.bid=B.bid
AND B.color=‘green’
Duke CS, Fall 2021 50

25
International University, VNU-HCMC

Nested Queries
A Subquery or Inner query or a Nested query is a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

query within another SQL query and embedded


within the WHERE clause.

A subquery is used to return data that will be


used in the main query as a condition to further
restrict the data to be retrieved.

Subqueries must be enclosed within parentheses.

Duke CS, Fall 2021 51

International University, VNU-HCMC

In and Exists Operator


• The IN operator allows you to specify multiple values in
a WHERE clause.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• The IN operator is a shorthand for


multiple OR conditions.

• The EXISTS operator is used to test for the existence of


any record in a subquery.
• The EXISTS operator returns TRUE if the subquery
returns one or more records.

Duke CS, Fall 2021 52

26
International University, VNU-HCMC

In syntax
SELECT column_name(s)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM table_name
WHERE column_name IN (value1, value2, ...);

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

Duke CS, Fall 2021 53

International University, VNU-HCMC

Exists syntax
SELECT column_name(s)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE
condition);

Duke CS, Fall 2021 54

27
International University, VNU-HCMC
9

IN subqueries
User(uid, name, age, pop)
• 𝑥 IN (𝑠𝑢𝑏𝑞𝑢𝑒𝑟𝑦) checks if 𝑥 is in the result of
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

𝑠𝑢𝑏𝑞𝑢𝑒𝑟𝑦
• Example: users (all columns) at the same age as
(some) Long
Let’s first try without sub-queries

• SELECT * FROM
You can use NOT IN too
User
WHERE age IN (SELECT age
FROM User
WHERE name = ‘Long');

Duke CS, Fall 2021 55

International University, VNU-HCMC


10

EXISTS subqueries
User(uid, name, age, pop)

• EXISTS (𝑠𝑢𝑏𝑞𝑢𝑒𝑟𝑦) checks if the result of 𝑠𝑢𝑏𝑞𝑢𝑒𝑟𝑦


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

is non-empty
• Example: users at the same age as (some) Bart
• SELECT *
FROM User AS u
WHERE EXISTS (SELECT * FROM User
WHERE name = 'Bart'
AND age = u.age);

• This happens to be a correlated subquery - a subquery


that references tuple variables in surrounding queries
You can use NOT EXISTS too
• How about the previous one with “IN”?

Duke CS, Fall 2021 56

28
International University, VNU-HCMC

Find names of sailors who’ve reserved boat #103


SELECT S.sname Sailors (sid, sname, rating, age)
FROM Sailors S Reserves(sid, bid, day)
Boats(bid, bname, color)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.sid IN (SELECT R.sid


FROM Reserves R
WHERE R.bid=103)

• A very powerful feature of SQL


– a WHERE/FROM/HAVING clause can itself contain an
SQL query
• To find sailors who’ve not reserved #103, use NOT IN.
• To understand semantics of nested queries, think of a
nested loops evaluation
– For each Sailors tuple, check the qualification by
computing the subquery
Duke CS, Fall 2021 57

International University, VNU-HCMC

Nested Queries with Correlation


Find names of sailors who’ve reserved boat #103:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)

• EXISTS is another set comparison operator, like IN


• Illustrates why, in general, subquery must be re-
computed for each Sailors tuple

Duke CS, Fall 2021 58

29
International University, VNU-HCMC

Nested Queries with Correlation


Find names of sailors who’ve reserved boat #103:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.sname
FROM Sailors S
WHERE UNIQUE (SELECT R.bid
FROM Reserves R
WHERE R.bid=103 AND S.sid=R.sid)

• If UNIQUE is used, and * is replaced by R.bid, finds sailors with


at most one reservation for boat #103
– UNIQUE checks for duplicate tuples

Duke CS, Fall 2021 59

International University, VNU-HCMC


15

Another example
User(uid, name, pop)
Member(uid, gid)
• SELECT * FROM User u Group(gid, name)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE EXISTS
(SELECT * FROM Member m
WHERE m.uid = u.uid
AND EXISTS
(SELECT * FROM Member n
WHERE n.uid = u.uid
AND n.gid <> m.gid));

• What does this query return?


• Users who join at least two groups

Duke CS, Fall 2021 60

30
International University, VNU-HCMC

More on Set-Comparison Operators


• We’ve already seen IN, EXISTS and UNIQUE
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Can also use NOT IN, NOT EXISTS and NOT UNIQUE.
• Also available: op ANY, op ALL, op IN
– where op : >, <, =, <=, >=
• Find sailors whose rating is greater than that of some
sailor called Horatio

SELECT *
– similarly ALL FROM Sailors S
WHERE S.rating > ANY (SELECT S2.rating
FROM Sailors S2
WHERE S2.sname=‘Horatio’)

Duke CS, Fall 2021 61

International University, VNU-HCMC

Aggregate Operators
COUNT (*)
Check yourself: COUNT ([DISTINCT] A)
What do these queries compute?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SUM ([DISTINCT] A)
AVG ([DISTINCT] A)
SELECT COUNT (*) MAX (A)
FROM Sailors S MIN (A)
single column

SELECT AVG (S.age) SELECT S.sname


FROM Sailors S FROM Sailors S
WHERE S.rating=10 WHERE S.rating= (SELECT MAX(S2.rating)
FROM Sailors S2)

SELECT COUNT (DISTINCT S.rating) SELECT AVG ( DISTINCT S.age)


FROM Sailors S FROM Sailors S
WHERE S.sname=‘Bob’ WHERE S.rating=10

Duke CS, Fall 2021 62

31
International University, VNU-HCMC

Motivation for Grouping


• So far, we’ve applied aggregate operators to all
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

(qualifying) tuples
– Sometimes, we want to apply them to each of several groups of
tuples
• Consider: Find the age of the youngest sailor for each
rating level
– In general, we don’t know how many rating levels exist, and
what the rating values for these levels are!
– Suppose we know that rating values go from 1 to 10; we can write
10 queries that look like this (need to replace i by num):

SELECT MIN (S.age)


For i = 1, 2, ... , 10: FROM Sailors S
WHERE S.rating = i

Duke CS, Fall 2021 64

International University, VNU-HCMC

Queries With GROUP BY and HAVING


SELECT [DISTINCT] target
FROM relation-list First go over the examples in the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

following slides. Then come back to this


WHERE qualification slide and study yourself
GROUP BY grouping-list
HAVING group-qualification

• The target-list contains


– (i) attribute names
– (ii) terms with aggregate operations (e.g., MIN (S.age))
• The attribute list (i) must be a subset of grouping-list
– Intuitively, each answer tuple corresponds to a group, and
these attributes must have a single value per group
– Here a group is a set of tuples that have the same value for all
attributes in grouping-list

Duke CS, Fall 2021 65

32
International University, VNU-HCMC

Conceptual Evaluation
• The cross-product of relation-list is computed
• Tuples that fail qualification are discarded
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• `Unnecessary’ fields are deleted


• The remaining tuples are partitioned into groups by
the value of attributes in grouping-list
• The group-qualification is then applied to eliminate
some groups
• Expressions in group-qualification must have a
single value per group
– In effect, an attribute in group-qualification that is not an
argument of an aggregate op also appears in grouping-list
– like “…GROUP BY bid, sid HAVING bid = 3”
• One answer tuple is generated per qualifying group
Duke CS, Fall 2021 66

International University, VNU-HCMC


Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors.

Sailors instance:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age


22 dustin 7 45.0
29 brutus 1 33.0
31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35.0
64 horatio 7 35.0
71 zorba 10 16.0
74 horatio 9 35.0
85 art 3 25.5
95 bob 3 63.5
96 frodo 3 25.5

Duke CS, Fall 2021 67

33
International University, VNU-HCMC
Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors.
SELECT S.rating, MIN (S.age) AS minage Sailors instance:
FROM Sailors S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.age >= 18 sid sname rating age


GROUP BY S.rating 22 dustin 7 45.0
HAVING COUNT (*) > 1 29 brutus 1 33.0
31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35.0
Answer relation: rating minage 64 horatio 7 35.0
3 25.5 71 zorba 10 16.0
7 35.0 74 horatio 9 35.0
8 25.5 85 art 3 25.5
95 bob 3 63.5
96 frodo 3 25.5

Duke CS, Fall 2021 68

International University, VNU-HCMC


Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors. SELECT S.rating,
MIN (S.age) AS minage
Step 1: Form the cross product: FROM clause FROM Sailors S
(some attributes are omitted for simplicity) WHERE S.age >= 18
GROUP BY S.rating
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

HAVING COUNT (*) > 1


rating age
7 45.0
1 33.0
8 55.5
8 25.5
10 35.0
7 35.0
10 16.0
9 35.0
3 25.5
3 63.5
3 25.5
Duke CS, Fall 2021 69

34
International University, VNU-HCMC
Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors. SELECT S.rating,
MIN (S.age) AS minage
Step 2: Apply WHERE clause FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

HAVING COUNT (*) > 1


rating age rating age
7 45.0 7 45.0
1 33.0 1 33.0
8 55.5 8 55.5
8 25.5 8 25.5
10 35.0 10 35.0
7 35.0 7 35.0
10 16.0 10 16.0
9 35.0 9 35.0
3 25.5 3 25.5
3 63.5 3 63.5
3 25.5 3 25.5
Duke CS, Fall 2021 70

International University, VNU-HCMC


Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors. SELECT S.rating,
MIN (S.age) AS minage
Step 3: Apply GROUP BY according to the FROM Sailors S
listed attributes WHERE S.age >= 18
GROUP BY S.rating
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

HAVING COUNT (*) > 1


rating age rating age rating age
7 45.0 7 45.0 1 33.0
1 33.0 1 33.0 3 25.5
8 55.5 8 55.5 3 63.5
8 25.5 8 25.5 3 25.5
10 35.0 10 35.0 7 45.0
7 35.0 7 35.0 7 35.0
10 16.0 10 16.0 8 55.5
9 35.0 9 35.0 8 25.5
3 25.5 3 25.5 9 35.0
3 63.5 3 63.5 10 35.0
3 25.5 3 25.5
Duke CS, Fall 2021 71

35
International University, VNU-HCMC
Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors. SELECT S.rating,
MIN (S.age) AS minage
Step 4: Apply HAVING clause FROM Sailors S
The group-qualification is applied to eliminate some groups WHERE S.age >= 18
GROUP BY S.rating
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

HAVING COUNT (*) > 1


rating age rating age rating age
rating age
7 45.0 7 45.0 1 33.0
1 33.0
1 33.0 1 33.0 3 25.5
3 25.5
8 55.5 8 55.5 3 63.5
3 63.5
8 25.5 8 25.5 3 25.5
3 35.5
10 35.0 10 35.0 7 45.0
7 45.0
7 35.0 7 35.0 7 35.0
7 35.0
10 16.0 10 16.0 8 55.5
8 55.5
9 35.0 9 35.0 8 25.5
8 25.5
3 25.5 3 25.5 9 35.0
9 35.0
3 63.5 3 63.5 10 35.0
10 35.0
3 25.5 3 25.5
Duke CS, Fall 2021 72

International University, VNU-HCMC


Find age of the youngest sailor with age >= 18, for each rating with
at least 2 such sailors. SELECT S.rating,
MIN (S.age) AS minage
Step 5: Apply SELECT clause FROM Sailors S
Apply the aggregate operator, At the end, one tuple per WHERE S.age >= 18
group GROUP BY S.rating
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

HAVING COUNT (*) > 1


rating age rating age rating age
7 45.0 7 45.0 1 33.0
1 33.0 1 33.0 3 25.5
8 55.5 8 55.5 3 25.5
rating minage
8 25.5 8 25.5 3 63.5
3 25.5
10 35.0 10 35.0 7 35.0
7 35.0
7 35.0 7 35.0 7 45.0
8 25.5
10 16.0 10 16.0 8 25.5
9 35.0 9 35.0 8 55.5
3 25.5 3 25.5 9 35.0
3 63.5 3 63.5 10 35.0
3 25.5 3 25.5
Duke CS, Fall 2021 73

36
International University, VNU-HCMC

Top Clause
The SQL TOP clause is used to fetch a TOP N
number or X percent records from a table.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT TOP number|percent column_name(s)


FROM table_name
WHERE [condition];

MySQL syntax
SELECT column_name(s)
FROM table_name
WHERE [condition]
LIMIT number;
Duke CS, Fall 2021 74

International University, VNU-HCMC

Demo database
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT TOP 3 * FROM Customers;

Duke CS, Fall 2021 75

37
International University, VNU-HCMC

Order by syntax
SELECT column1, column2, ...
FROM table_name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ORDER BY column1, column2, ... ASC|DESC;

SELECT *
FROM Customers
ORDER BY Country;

Duke CS, Fall 2021 76

International University, VNU-HCMC

Case syntax

CASE
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHEN condition1 THEN result1


WHEN condition2 THEN result2

WHEN conditionN THEN resultN
ELSE result
END;

Duke CS, Fall 2021 77

38
International University, VNU-HCMC

Demo Database
OrderDatails table
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 78

International University, VNU-HCMC

Case example
SELECT OrderID, Quantity,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

CASE
WHEN Quantity > 30 THEN 'The
quantity is greater than 30'
WHEN Quantity = 30 THEN 'The
quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

Duke CS, Fall 2021 79

39
International University, VNU-HCMC

Example
• The following SQL will order the customers
by City. However, if City is NULL, then order
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

by Country

SELECT CustomerName, City, Country


FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);

Duke CS, Fall 2021 80

International University, VNU-HCMC


12

“WITH” clause – very useful!


• You will find “WITH” clause very useful!
WITH Temp1 AS
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

(SELECT ….. ..),


Temp2 AS
(SELECT ….. ..)
SELECT X, Y
FROM TEMP1, TEMP2
WHERE…. User(uid, name, age, pop)
• Can simplify complex nested queries
Example: users at the same age as (some) Long
WITH LongAge AS WITH clause
(SELECT age not really needed
FROM User for this query!
WHERE name = ‘Long’)
SELECT U.uid, U.name, U.age, U.pop
FROM User U, LongAge L
WHERE U.age = L.age

Duke CS, Fall 2021 81

40
International University, VNU-HCMC

WITH CLAUSE
WITH Cus_CTE (sid, sname, sage, searning)
AS
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Customer(id, name, age,


-- Define the CTE query. salary, address)
(
SELECT id, name, age, salary AS earning
FROM CUSTOMERS
WHERE ADDRESS IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT searning, COUNT(sname) AS Totalname
FROM Cus_CTE
GROUP BY searning
ORDER BY searning

Duke CS, Fall 2021 82

International University, VNU-HCMC

Rewriting INTERSECT Queries Using IN


Find sid’s of sailors who’ve reserved both a red and a green boat:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE 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 R2
WHERE S2.sid=R2.sid AND R2.bid=B2.bid
AND B2.color=‘green’)

• Similarly, EXCEPT queries re-written using NOT IN.


• To find names (not sid’s) of Sailors who’ve reserved both
red and green boats, just replace S.sid by S.sname in
SELECT clause

Duke CS, Fall 2021 84

41
International University, VNU-HCMC

Division in SQL
Find sailors who’ve reserved all boats.
• Option 1:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Option 2: Let’s do it the hard way, without EXCEPT:


SELECT S.sname
option 1
FROM Sailors S
WHERE NOT EXISTS
((SELECT B.bid
FROM Boats B) EXCEPT (SELECT R.bid
FROM Reserves R
WHERE R.sid=S.sid))
SELECT S.sname Sailors S such that ...
option 2
FROM Sailors S
WHERE NOT EXISTS (SELECT B.bid there is no boat B...
FROM Boats B
WHERE NOT EXISTS (SELECT R.bid …without ...
WHERE R.bid=B.bid
…a Reserves tuple showing S reserved B AND R.sid=S.sid))
Duke CS, Fall 2021 85

International University, VNU-HCMC

Example
• Find the names of students who’ve enrolled a
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Database and AI (Artificial Intelligence) course.


• Assume a Student database:

Students (sid, sname, address, age)


Enrolled (sid, cid, year, semester)
Courses (cid, cname, credit)

Duke CS, Fall 2021 86

42
International University, VNU-HCMC

Find name and age of the oldest sailor(s)


• The first query is illegal! SELECT S.sname, MAX (S.age)
FROM Sailors S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Recall the semantic of


GROUP BY SELECT S.sname, S.age
FROM Sailors S
WHERE S.age =
• The third query is
equivalent to the second (SELECT MAX (S2.age)
query FROM Sailors S2)
– and is allowed in the SELECT S.sname, S.age
SQL/92 standard, but is FROM Sailors S
not supported in some
WHERE (SELECT MAX (S2.age)
systems
FROM Sailors S2) = S.age

Duke CS, Fall 2021 87

International University, VNU-HCMC


Find age of the youngest sailor with age >= 18, for each rating
with at least 2 such sailors and with every sailor under 60.
SELECT S.rating,
MIN (S.age) AS minage
FROM Sailors S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

rating age rating age WHERE S.age >= 18


7 45.0 1 33.0 GROUP BY S.rating
1 33.0 HAVING COUNT (*) > 1 AND
3 25.5 EVERY (S.age <60)
8 55.5
3 63.5
8 25.5
3 25.5
10 35.0 rating minage
7 35.0 7 45.0
7 35.0
10 16.0 7 35.0
8 25.5
9 35.0 8 55.5
3 25.5 8 25.5 What is the result of
3 63.5 9 35.0 changing EVERY to ANY?
3 25.5 10 35.0

Duke CS, Fall 2021 88

43
International University, VNU-HCMC
Find age of the youngest sailor with age >= 18, for each
rating with at least 2 sailors between 18 and 60.

SELECT S.rating, MIN (S.age) AS minage


Sailors instance:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S sid sname rating age


WHERE S.age >= 18 AND S.age <= 60
GROUP BY S.rating 22 dustin 7 45.0
HAVING COUNT (*) > 1 29 brutus 1 33.0
31 lubber 8 55.5
32 andy 8 25.5
Answer relation: 58 rusty 10 35.0
64 horatio 7 35.0
rating minage 71 zorba 10 16.0
3 25.5 74 horatio 9 35.0
7 35.0 85 art 3 25.5
8 25.5 95 bob 3 63.5
96 frodo 3 25.5

Duke CS, Fall 2021 89

International University, VNU-HCMC


For each red boat, find the number of
reservations for this boat
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 90

44
International University, VNU-HCMC
For each red boat, find the number of
reservations for this boat
SELECT B.bid, COUNT (*) AS scount
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

FROM Sailors S, Boats B, Reserves R


WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’
GROUP BY B.bid

• Grouping over a join of three relations.


• What do we get if we remove B.color=‘red’
from the WHERE clause and add a HAVING clause
with this condition?
• What if we drop Sailors and the condition
involving S.sid?

Duke CS, Fall 2021 91

International University, VNU-HCMC


Find age of the youngest sailor with age > 18, for
each rating with at least 2 sailors (of any age)
Sailors (sid, sname, rating, age)
Reserves(sid, bid, day)
Boats(bid, bname, color)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 92

45
International University, VNU-HCMC
Find age of the youngest sailor with age > 18, for
each rating with at least 2 sailors (of any age)
SELECT S.rating, MIN (S.age)
FROM Sailors S
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

WHERE S.age > 18


GROUP BY S.rating
HAVING 1 < (SELECT COUNT (*)
FROM Sailors S2
WHERE S.rating=S2.rating)

• Shows HAVING clause can also contain a subquery.


• Compare this with the query where we considered only
ratings with 2 sailors over 18!
• What if HAVING clause is replaced by:
– HAVING COUNT(*) >1
Duke CS, Fall 2021 93

International University, VNU-HCMC


Find those ratings for which the average age is
the minimum over all ratings
• Aggregate operations cannot be nested!
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.rating
FROM Sailors S WRONG
WHERE S.age = (SELECT MIN (AVG (S2.age))
FROM Sailors S2)
• Correct solution (in SQL/92):
SELECT Temp.rating, Temp.avgage
FROM (SELECT S.rating, AVG (S.age) AS avgage
FROM Sailors S
GROUP BY S.rating) AS Temp
WHERE Temp.avgage = (SELECT MIN (Temp.avgage)
FROM Temp)
Duke CS, Fall 2021 94

46
International University, VNU-HCMC

Triggers
• Trigger: procedure that starts automatically if specified
changes occur to the DBMS
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should run)
– Action (what happens if the trigger runs)

CREATE TRIGGER youngSailorUpdate AFTER INSERT


ON SAILORS REFERENCING NEW TABLE NewSailors
FOR EACH STATEMENT
INSERT INTO YoungSailors(sid, name, age, rating)
SELECT sid, name, age, rating
FROM NewSailors N
WHERE N.age <= 18
Duke CS, Fall 2021 95

International University, VNU-HCMC

Nulls and Views in SQL


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 96

47
International University, VNU-HCMC

Null Values
• Field values in a tuple are sometimes
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– unknown, e.g., a rating has not been assigned, or


– inapplicable, e.g., no spouse’s name
– SQL provides a special value null for such situations.

Duke CS, Fall 2021 97

International University, VNU-HCMC

Standard Boolean 2-valued logic


• True = 1, False = 0
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Suppose X = 5
– (X < 100) AND (X >= 1) is T ∧T = T
– (X > 100) OR (X >= 1) is F ∨T = T
– (X > 100) AND (X >= 1) is F ∧T = F
– NOT(X = 5) is ¬T = F

• Intuitively,
– T = 1, F = 0
– For V1, V2 ∈{1, 0}
– V1 ∧V2 = MIN (V1, V2)
– V1 ∨V2 = MAX(V1, V2)
– ¬(V1) = 1 – V1

Duke CS, Fall 2021 98

48
International University, VNU-HCMC

2-valued logic does not work for nulls


• Suppose rating = null, X = 5
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Is rating>8 true or false?


• What about AND, OR and NOT connectives?
– (rating > 8) AND (X = 5)?
• What if we have such a condition in the
WHERE clause?

Duke CS, Fall 2021 99

International University, VNU-HCMC

3-Valued Logic For Null


• TRUE (= 1), FALSE (= 0), UNKNOWN (= 0.5)
– unknown is treated as 0.5
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Now you can apply rules from 2-valued logic!


– For V1, V2 ∈{1, 0, 0.5}
– V1 ∧V2 = MIN (V1, V2)
– V1 ∨V2 = MAX(V1, V2)
– ¬(V1) = 1 – V1

• Therefore,
– NOT UNKNOWN = UNKNOWN
– UNKNOWN OR TRUE = TRUE
– UNKNOWN AND TRUE = UNKNOWN
– UNKNOWN AND FALSE = FALSE
– UNKNOWN OR FALSE = UNKNOWN

Duke CS, Fall 2021 100

49
International University, VNU-HCMC

New issues for Null


• The presence of null complicates many issues. E.g.:
– Special operators needed to check if value IS/IS NOT NULL
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Be careful!
– “WHERE X = NULL” does not work!
– Need to write “WHERE X IS NULL”

• Meaning of constructs must be defined carefully


– e.g., WHERE clause eliminates rows that don’t evaluate to true
– So not only FALSE, but UNKNOWNs are eliminated too
– very important to remember!

• But NULL allows new operators (e.g. outer joins)


• Arithmetic with NULL
– all of +, -, *, / return null if any argument is null

• Can force ”no nulls” while creating a table


– sname char(20) NOT NULL
– primary key is always not null
Duke CS, Fall 2021 101

International University, VNU-HCMC

Aggregates with NULL


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age • What do you get for


22 dustin 7 45
• SELECT count(*) from R1?
31 lubber 8 55
• SELECT count(rating) from R1?
58 rusty 10 35

R1

Duke CS, Fall 2021 102

50
International University, VNU-HCMC

Aggregates with NULL


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age • What do you get for


22 dustin 7 45
• SELECT count(*) from R1?
31 lubber 8 55
• SELECT count(rating) from R1?
58 rusty 10 35
• Answer:
R1

Duke CS, Fall 2021 103

International University, VNU-HCMC

Aggregates with NULL


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age • What do you get for


22 dustin 7 45
• SELECT count(*) from R1?
31 lubber 8 55
• SELECT count(rating) from R1?
58 rusty 10 35
• Answer:
R1
sid sname rating age
• What do you get for
22 dustin 7 45
• SELECT count(*) from R2?
31 lubber null 55
58 rusty 10 35
• SELECT count(rating) from R2?

R2

Duke CS, Fall 2021 104

51
International University, VNU-HCMC

Aggregates with NULL


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age • What do you get for


22 dustin 7 45
• SELECT count(*) from R1?
31 lubber 8 55
• SELECT count(rating) from R1?
58 rusty 10 35
• Answer:
R1
sid sname rating age
• What do you get for
22 dustin 7 45
• SELECT count(*) from R2?
31 lubber null 55
58 rusty 10 35
• SELECT count(rating) from R2?
• Answer:
R2

Duke CS, Fall 2021 105

International University, VNU-HCMC

Aggregates with NULL


• COUNT, SUM, AVG, MIN, MAX (with or without DISTINCT)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Discards null values first


– Then applies the aggregate
– Except count(*)
• If only applied to null values, the result is null

sid sname rating age sid sname rating age


22 dustin 7 45 22 dustin null 45
31 lubber null 55 31 lubber null 55
58 rusty 10 35 58 rusty null 35
R2 R3
• SELECT sum(rating) from R2? • SELECT sum(rating) from R3?

Duke CS, Fall 2021 106

52
International University, VNU-HCMC

Aggregates with NULL


• COUNT, SUM, AVG, MIN, MAX (with or without DISTINCT)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Discards null values first


– Then applies the aggregate
– Except count(*)
• If only applied to null values, the result is null

sid sname rating age sid sname rating age


22 dustin 7 45 22 dustin null 45
31 lubber null 55 31 lubber null 55
58 rusty 10 35 58 rusty null 35
R2 R3
• SELECT sum(rating) from R2? • SELECT sum(rating) from R3?
• Answer: • Answer:

Duke CS, Fall 2021 107

International University, VNU-HCMC

Overview: General Constraints


• Useful when more general ICs than keys are involved
• There are also ASSERTIONS to specify constraints that span
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

across multiple tables


• There are TRIGGERS too: procedure that starts
automatically if specified changes occur to the DBMS
– see additional slides at the end
CREATE TABLE Reserves
CREATE TABLE Sailors ( sname CHAR(10),
( sid INTEGER, bid INTEGER,
sname CHAR(10), day DATE,
rating INTEGER, PRIMARY KEY (bid,day),
age REAL, CONSTRAINT noInterlakeRes
PRIMARY KEY (sid), CHECK (`Interlake’ <>
CHECK (rating >= 1 ( SELECT B.bname
AND rating <= 10 ) FROM Boats B
WHERE B.bid=bid)))
Duke CS, Fall 2021 108

53
International University, VNU-HCMC

VIEWS
Views, which are a type of virtual tables. Views can be
created from a single table, multiple tables or another
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

view.

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE [condition];

CREATE VIEW CUSTOMERS_VIEW AS


SELECT name, age
FROM CUSTOMERS
WHERE age <=30

SELECT * FROM CUSTOMERS_VIEW

Duke CS, Fall 2021 109

International University, VNU-HCMC

VIEWS
Views can be dropped using the DROP VIEW command
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

DROP VIEW view_name

DROP VIEW CUSTOMERS_VIEW;

Views and Security: Views can be used to present


necessary information (or a summary), while hiding
details in underlying relation(s)
• The above view hides customers “salary” from
CUSTOMERS

Duke CS, Fall 2021 110

54
International University, VNU-HCMC

UPDATING A VIEW
A view can be updated under certain conditions which are
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

given below −
§ The SELECT clause may not contain the keyword DISTINCT.
§ The SELECT clause may not contain summary functions.
§ The SELECT clause may not contain set functions.
§ The SELECT clause may not contain set operators.
§ The SELECT clause may not contain an ORDER BY clause.
§ The FROM clause may not contain multiple tables.
§ The WHERE clause may not contain subqueries.
§ The query may not contain GROUP BY or HAVING.
§ Calculated columns may not be updated.

Duke CS, Fall 2021 111

International University, VNU-HCMC


Can create a new table from a query on other
tables too

SELECT… INTO.... FROM.... WHERE


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SELECT S.name, E.grade


INTO YoungActiveStudents
FROM Students S, Enrolled E
WHERE S.sid = E.sid and S.age<21

Duke CS, Fall 2021 112

55
International University, VNU-HCMC

Summary
• SQL has a huge number of constructs and possibilities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– You need to learn and practice it on your own


– Given a problem, you should be able to write a SQL
query and verify whether a given one is correct

• Pay attention to NULLs

Duke CS, Fall 2021 113

International University, VNU-HCMC


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Thank you for your attention!

Duke CS, Fall 2021 114

56

You might also like