Download presentation
Presentation is loading. Please wait.
Published byStephen Hodges Modified over 9 years ago
1
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries
2
Assistant Prof. Chih-Chin Liu Page 2 Outline Joins Set Operations Subqueries Subtotals and GROUP BY CONNECT BY
3
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.
4
Assistant Prof. Chih-Chin Liu Page 4 Joins SQL-89 JOIN Syntax SQL-92 JOIN Syntax (Oracle Do Not Support) FROM table1 INNER JOIN table2 ON table1.column = table2.column FROM table1, table2 WHERE table1.column = table2.column
5
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.
6
Assistant Prof. Chih-Chin Liu Page 6 Joins Example 1: Inner Join
7
Assistant Prof. Chih-Chin Liu Page 7 Joins Example 2: Table Alias (Reflexive Join)
8
Assistant Prof. Chih-Chin Liu Page 8 Joins Example 3: Inequality Join
9
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.
10
Assistant Prof. Chih-Chin Liu Page 10 Joins Two View Examples
11
Assistant Prof. Chih-Chin Liu Page 11 Joins INNER JOIN
12
Assistant Prof. Chih-Chin Liu Page 12 Joins Example 4: Inner Join
13
Assistant Prof. Chih-Chin Liu Page 13 Joins LEFT JOIN
14
Assistant Prof. Chih-Chin Liu Page 14 Joins Example 5: Left Outer Join
15
Assistant Prof. Chih-Chin Liu Page 15 Joins RIGHT JOIN
16
Assistant Prof. Chih-Chin Liu Page 16 Joins Example 6: Right Outer Join
17
Assistant Prof. Chih-Chin Liu Page 17 Joins FULL JOIN
18
Assistant Prof. Chih-Chin Liu Page 18 Joins CROSS JOIN
19
Assistant Prof. Chih-Chin Liu Page 19 Joins Example 7: Cross Join (Cartesian Products)
20
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
21
Assistant Prof. Chih-Chin Liu Page 21 Set Operations AB C T1T2
22
Assistant Prof. Chih-Chin Liu Page 22 Set Operations Example 8: UNION
23
Assistant Prof. Chih-Chin Liu Page 23 Set Operations Example 9: INTERSECT
24
Assistant Prof. Chih-Chin Liu Page 24 Set Operations Example 10: MINUS
25
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))
26
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”)
27
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
28
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.
29
Assistant Prof. Chih-Chin Liu Page 29 Subqueries Example 11: Subquery
30
Assistant Prof. Chih-Chin Liu Page 30 Subqueries Example 12: IN
31
Assistant Prof. Chih-Chin Liu Page 31 Subqueries Example 13: ANY
32
Assistant Prof. Chih-Chin Liu Page 32 Subqueries Example 14: ALL
33
Assistant Prof. Chih-Chin Liu Page 33 Subqueries Example 15: EXISTS
34
Assistant Prof. Chih-Chin Liu Page 34 Subqueries Example 16: NOT EXISTS
35
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, 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.
36
Assistant Prof. Chih-Chin Liu Page 36 Subtotals and GROUP BY Example 17: GROUP BY
37
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.
38
Assistant Prof. Chih-Chin Liu Page 38 Subtotals and GROUP BY Example 18: HAVING
39
Assistant Prof. Chih-Chin Liu Page 39 CONNECT BY Example 19: CONNECT BY
40
Assistant Prof. Chih-Chin Liu Page 40 DUAL Example 20: DUAL
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.