Lecture 4 More SQL
Lecture 4 More SQL
DATABASES
More Queries in SQL
NGUYEN Hoang Ha
Email: [email protected]
Recall: SELECT Statements →
π σ ρ ⋈c
Projection:You can use the projection capability in SQL to choose the columns in
a table that you want returned by your query.
Selection:You can use the selection capability in SQL to choose the rows in a table
that you want returned by a query (with WHERE clause)
Joining:You can use the join capability in SQL to bring together data that is stored
in
2
different tables by creating a link between them.
QUERIES INVOLVING MORE THAN
ONE TABLES
Reference: Section 6.2 Jeffrey D. Ullman, Jennifer Widom: A First Course in Database
Systems, Pearson, 3rd Edition (2007)
A Cartesian Products example
4
INNER JOIN
SELECT *
FROM student A, class B
WHERE A.class_id = B.class_id
SELECT *
FROM student A INNER JOIN class B ON A.class_id = B.class_id
To determine a student’s class name, you compare the value in the CLASS_ID
column in the CLASS table with the CLASS_ID values in the STUDENT table.
The relationship between the STUDENT and CLASS tables is an INNER JOIN,
a.k.a equijoin — that is, values in the CLASS_ID column on both tables must be
equal.
5
Express INNER JOIN in RA
δA.class_id = B.class_id(A x B)
SELECT *
FROM student A, class B
WHERE A.class_id = B.class_id
6
Non-INNER JOIN
8
Outer Joins
If a row does not satisfy a join condition, the row will not appear in the query
result.
The missing rows can be returned if an outer join operator is used in the join
condition.
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 Lop 202 107 4D
5E
6F
7G
8H
9
Left Outer Join
SELECT A.class_name
FROM class A LEFT OUTER JOIN student B
10 ON A.class_id = B.class_id
Output of LEFT OUTER JOIN
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
106 Lop 106 106 2B
107 Lop 107 107 3C
107 Lop 107 107 4D
201 Lop 201
202 Lop 202
SELECT B.name
FROM class A RIGHT OUTER JOIN student B
12
ON A.class_id = B.class_id
Output of RIGHT OUTER JOIN
CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
201 Lop 201
202 Lop 202
What is the
Domain
Values of
A.class_id?
16
Self Joins
18
Disambiguating attributes
Sometimes we ask a query involving several relations, and
among these relations are two or more attributes with the
same name. If so, we need a way to indicate which of these
attributes is meant by a use of their shared name.
SQL solves this problem by allowing us to place a relation
name and a dot in front of an attribute, thus R.A refers to
the attribute A of relation R
19
Disambiguating attributes example
Two relations:
MovieStar (name, address, gender, birthdate)
MovieExec (name, address, cert#, netWorth)
Both relations have attributes “name” and “address”. Look at the
following query to see the way of dis-ambiguating:
20
SET operators
21
UNION & UNION ALL
The UNION operator eliminates any duplicated rows.
22
INTERSECT
{A ∩ B} = {a | a is in A and B}
23
Difference
SQL Server
Oracle
24
FULL RELATION OPERATIONS
Reference: Section 6.4 Jeffrey D. Ullman, Jennifer Widom: A First Course in Database
Systems, Pearson, 3rd Edition (2007)
SELECT distinct - Eliminating duplicates
When we do projection or select data from a bag-relation,
the output may have duplicate tuples
If we do not want duplicates in the result, then we may
follow the key-word SELECT by the key-word DISTINCT
26
SELECT distinct - Eliminating duplicates
27
Duplicates in Set operators
Unlike the SELECT statement, which preserves duplicates as
a default and only eliminates them when instructed to by the
DISTINCT keyword, the set operators (union, intersection,
difference) normally eliminate duplicates
In order to prevent the elimination of duplicates, we must
follow the operator UNION, INTERSECT or EXCEPT by
the keyword ALL
28
Duplicates in Set operators
The UNION, INTERSECT, EXCEPT operator eliminates any
duplicated rows.
29
Grouping and Aggregation Operators
STUDENT
CLASS_ID ID NAME
106 1A
106 2B
107 3C
107 4D
5E
6F
7G
8H
AVG
COUNT
MAX
MIN
SUM
STDDEV
VARIANCE
32
Example: Aggregate Functions and DISTINCT
33
Grouping
Until now, all group functions (demonstrated in above
examples) have treated the table as one large group of
information.
At times, you need to divide the table of information into
smaller groups. This can be done by using the GROUP BY
clause
The keyword GROUP BY is followed by a list of grouping
attributes.
34
Grouping
36
Example: Aggregate Functions and GROUP BY
37
Example: Aggregate Functions and GROUP BY
38
Grouping: rules to remember
Syntax:
1
Select ROWS (with WHERE clause) first
2
ROWS are grouped (GROUP BY clause)
45
Example: HAVING
46
Example: HAVING
47