劉 志 俊 (chih-chin liu) 中華大學 資訊工程系 october 2001 chap 9 sql (iii): advanced...
TRANSCRIPT
劉 志 俊 (Chih-Chin Liu)
中華大學 資訊工程系
October 2001
Chap 9 SQL (III): Advanced Queries
Chap 9 SQL (III): Advanced Queries
Assistant Prof. Chih-Chin Liu Page 2
Outline
Joins
Set Operations
Subqueries
Subtotals and GROUP BY
CONNECT BY
Assistant Prof. Chih-Chin Liu Page 3
Joins
JOIN operations can combine data stored in
more than two tables as a single table.
In SQL-89, join conditions is specified in the
WHERE clauses.
In SQL-92, Join conditions can be specified in
either the FROM or WHERE clauses; specifying
them in the FROM clause is recommended.
Assistant Prof. Chih-Chin Liu Page 4
Joins
SQL-89 JOIN Syntax
SQL-92 JOIN Syntax (Oracle Do Not Support)
FROM table1INNER JOIN table2ON table1.column = table2.column
FROM table1, table2WHERE table1.column = table2.column
Assistant Prof. Chih-Chin Liu Page 5
Joins
Joins can be categorized as: Inner joins (the typical join operation, which uses
some comparison operator like = or <>). These
include equi-joins and natural joins.
Outer joins. Outer joins can be a left, right, or full
outer join.
Cross joins return all rows from the left table, each
row from the left table is combined with all rows from
the right table. Cross joins are also called Cartesian
products.
Assistant Prof. Chih-Chin Liu Page 9
Joins
Joins can be categorized as: Inner joins (the typical join operation, which uses
some comparison operator like = or <>). These
include equi-joins and natural joins.
Outer joins. Outer joins can be a left, right, or full
outer join.
Cross joins return all rows from the left table, each
row from the left table is combined with all rows from
the right table. Cross joins are also called Cartesian
products.
Assistant Prof. Chih-Chin Liu Page 11
Joins
INNER JOINAnimalID Name
1 Leisha
2 Gene
3 Vivian
AnimalID Category
2 Dog
3 Cat
4 Dog
INNER JOIN
AnimalID Name AnimalID Category
2 Gene 2 Dog
3 Vivian 3 Cat
Assistant Prof. Chih-Chin Liu Page 13
Joins
LEFT JOINAnimalID Name
1 Leisha
2 Gene
3 Vivian
AnimalID Category
2 Dog
3 Cat
4 Dog
LEFT JOIN
AnimalID Name AnimalID Category
1 Leisha Null Null
2 Gene 2 Dog
3 Vivian 3 Cat
Assistant Prof. Chih-Chin Liu Page 15
Joins
RIGHT JOINAnimalID Name
1 Leisha
2 Gene
3 Vivian
AnimalID Category
2 Dog
3 Cat
4 Dog
RIGHT JOIN
AnimalID Name AnimalID Category
2 Gene 2 Dog
3 Vivian 3 CatNull Null 4 Dog
Assistant Prof. Chih-Chin Liu Page 17
Joins
FULL JOINAnimalID Name
1 Leisha
2 Gene
3 Vivian
AnimalID Category
2 Dog
3 Cat
4 Dog
FULL JOIN
AnimalID Name AnimalID Category
1 Leisha Null Null
2 Gene 2 Dog
3 Vivian 3 CatNull Null 4 Dog
Assistant Prof. Chih-Chin Liu Page 18
Joins
CROSS JOINAnimalID Name
1 Leisha
2 Gene
3 Vivian
AnimalID Category
2 Dog
3 Cat
4 Dog
CROSS JOIN
AnimalID Name AnimalID Category
1 Leisha 2 Dog
1 Leisha 3 Cat
1 Leisha 4 Dog
2 Gene 2 Dog
2 Gene 3 Cat
2 Gene 4 Dog
3 Vivian 2 Dog
3 Vivian 3 Cat
3 Vivian 4 Dog
Assistant Prof. Chih-Chin Liu Page 20
Set Operations
A table is a set of tuples.
Set operations can be applied on two
tables.
Set operations: UNION
INTERSECT
MINUS
Assistant Prof. Chih-Chin Liu Page 21
Set Operations
A B C
T1 UNION T2 A + B + CT1 INTERSECT T2 B
T1 MINUS T2 A
T1 T2
Assistant Prof. Chih-Chin Liu Page 25
Subqueries
Which cats sold for more than the average
price of cats ?
Case 1: If we know the average price is $170.
SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice
FROM Animal
INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID
WHERE ((Animal.Category = “Cat”) AND (SaleAnimal.SalePrice >170))
Assistant Prof. Chih-Chin Liu Page 26
Subqueries
Which cats sold for more than the average
price of cats ?
Case 2: If we don’t know the average price.
SQL Query to compute the average price:
SELECT AVG(SalePrice)
FROM Animal
INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID
WHERE (Animal.Category = “Cat”)
Assistant Prof. Chih-Chin Liu Page 27
Subqueries
Combine these two queries:
SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice
FROM Animal
INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID
WHERE ((Animal.Category = “Cat”) AND (SaleAnimal.SalePrice >
(
SELECT AVG(SalePrice)
FROM Animal
INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID
WHERE (Animal.Category = “Cat”)
)))
subquery
Assistant Prof. Chih-Chin Liu Page 28
Subqueries
The query used for the comparison is defined directly in the internal predicate of the where clause and is known as a nested query.
The query used in the where clause of a nested query is called a subquery.
Some special SQL operators (IN, ALL, ANY, EXISTS) are often used with subqueries.
Assistant Prof. Chih-Chin Liu Page 35
Subtotals and GROUP BY
GROUP BY specifies the groups into which
output rows are to be placed and, if aggregate
functions are included in the SELECT clause
<select list>, calculates a summary value for
each group.
When a GROUP BY clause is used, each item in
the select list must produce a single value for
each group.
Assistant Prof. Chih-Chin Liu Page 37
Subtotals and GROUP BY
HAVING specifies a search condition for a group
or an aggregate.
HAVING is usually used with the GROUP BY
clause.
When GROUP BY is not used, HAVING
behaves like a WHERE clause.