1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

77
1 Chapter 6 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation

description

3 Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables; SELECT title, length FROM movies WHERE year =1994; For example:

Transcript of 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

Page 1: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

1

Chapter 6 Introduction to SQL

Select-From-Where Statements Subqueries Grouping and Aggregation

Page 2: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

2

Why SQL?

SQL is a very-high-level language. Say “what to do” rather than “how to do it.” Avoid a lot of data-manipulation details

needed in procedural languages like C++ or Java.

Database management system figures out “best” way to execute query. Called “query optimization.”

Page 3: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

3

Select-From-Where Statements

SELECT desired attributesFROM one or more tablesWHERE condition about tuples of the tables;

SELECT title, lengthFROM moviesWHERE year =1994;

For example:

Page 4: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

4

Our Running Example

All our SQL queries will be based on the following database schema:

Movie(title, year, length, inColor, studioName, producerC#)

StarsIn(movieTitle, movieYear, starName) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth) Studio(name, address, preaC#)

Page 5: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

5

Example

Using Movie, what movies were produced by Disney Studios in 1990? SELECT title, length FROM Movie WHERE studioName = ‘Disney’ AND year = 1990;

Notice SQL uses single-quotes for strings.SQL is case-insensitive, except inside strings.

Page 6: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

6

Result of Query

title length

Pretty Women…

119

Page 7: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

7

Meaning of Single-Relation Query

Begin with the relation in the FROM clause.

Apply the selection indicated by the WHERE clause.

Apply the extended projection indicated by the SELECT clause.

Page 8: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

8

Operational Semantics

To implement this algorithm think of a tuple variable ranging over each tuple of the relation mentioned in FROM.

Check if the “current” tuple satisfies the WHERE clause.

If so, compute the attributes or expressions of the SELECT clause using the components of this tuple.

Page 9: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

9

* In SELECT clauses

When there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.”

Example using Movie: SELECT * FROM Movie WHERE studioName = ‘Disney’ AND year = 1990;

Page 10: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

10

Result of Query:

title year length inColor studioName procucerC#

Pretty Women

1990 119 true Disney 999

Page 11: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

11

Renaming Attributes

If you want the result to have different attribute names, use “AS <new name>” to rename an attribute. Keyword AS is optional.

Example based on Movie: SELECT title As name, length As duration FROM Movie WHERE studioName = ‘Disney’ AND year = 1990;

Page 12: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

12

Result of Query:

name DurationPretty Women

…119

Page 13: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

13

Expressions in SELECT Clauses

Any expression that makes sense can appear as an element of a SELECT clause.

Example: from Movie:SELECT title As name, length*0.016667 As lengthInHoursFROM Movie;

Page 14: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

14

Constant Expressions

From Movie :

SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHoursFROM MovieWHERE studioName = ‘Disney’ AND year = 1990;

Page 15: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

15

Result of Query

title length inHours

Pretty Women…

1.98334 hrs.

Page 16: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

16

Complex Conditions in WHERE Clause

From Movie, find all the movies made after 1970 that are in black-and-white:

SELECT titleFROM MovieWHERE year > 1970 AND NOT

inColor;

Page 17: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

17

Patterns

WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.

General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>

Pattern is a quoted string with % (any string) or _ (any character)

Page 18: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

18

Example

From Movie find all the movies have the title as “Star something”, and we remember that something has four letters.:

SELECT titleFROM MovieWHERE title LIKE ‘Star _ _ _ _’;

A ‘_’ just represent half Chinese characters

Page 19: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

19

Escape characters in LIKE expressions

If the pattern we wish to use in a LIKE expression involves the characters % or _. We can follow the pattern by the keyword ESCAPE and choose a escape character.

For example:a LIKE ‘x%%x%’ ESCAPE ‘x’

Page 20: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

20

Comparing dates and times

A date is represented by the keyword DATE followed by a quoted string of a special form. For example, DATE ‘1948-05-14’ follows the required form.

A time is represented similarly by the keyword TIME and a quoted string. For instance, TIME ‘ 15:00:02.5’

We can compare dates or times using the same comparison operators we use for numbers or strings.

DateTime is represented like this”2008-08-18 14:20:00” in SQL server

Page 21: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

21

Ordering the output

To get output in sorted order, we add to the select-from-where statement a clause:ORDER BY <list of attributes>

The order is by default ascending (ASC), but we can get the output highest-first by appending the keyword DESC.

Page 22: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

22

Example

To get the movies listed by length, shortest first, and among movies of equal length, alphabetically, we can say: SELECT * FROM Movie WHERE studioName = ‘Disney’ AND year = 1990 ORDER BY length, title;

Page 23: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

23

Deciding Range

Query condition <attribute> [NOT]BETWEEN <value1> AND <value2>

ExampleSELECT *FROM MovieWHERE year BETWEEN 1990 AND 2000ORDER BY length, title;

Page 24: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

24

Deciding Set

Query condition <attribute> [NOT]IN {<value lists>}

ExampleSELECT *FROM MovieWHERE year NOT IN {2006,2007,2008};

Page 25: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

25

NULL Values Tuples in SQL relations can have NULL

as a value for one or more components.

Meaning depends on context. Two common cases: Missing value : e.g., we know Joe’s Bar has

some address, but we don’t know what it is.

Inapplicable : e.g., the value of attribute spouse for an unmarried person.

Page 26: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

26

Comparing NULL’s to Values

The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN.

When any value is compared with NULL, the truth value is UNKNOWN.

But a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN).

Page 27: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

27

Three-Valued Logic

To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½.

AND = MIN; OR = MAX, NOT(x) = 1-x. Example:TRUE AND (FALSE OR NOT(UNKNOWN)) =

MIN(1, MAX(0, (1 - ½ ))) =MIN(1, MAX(0, ½ ) = MIN(1, ½ ) = ½.

Page 28: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

28

Surprising Example

From the following Sells relation:bar beer priceJoe’s Bar Bud NULLSELECT barFROM SellsWHERE price < 2.00 OR price >= 2.00;

UNKNOWN UNKNOWNUNKNOWN

Page 29: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

29

Reason: 2-Valued Laws != 3-Valued Laws

Some common laws, like commutativity (交换性 ) of AND, hold in 3-valued logic.

But not others, e.g., the “law of the excluded middle”: p OR NOT p = TRUE. When p = UNKNOWN, the left side is

MAX( ½, (1 – ½ )) = ½ != 1.

Page 30: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

30

Query involving NULL

If you want to find the tuples which some attribute is null or not null, you can like this: <attribute> IS [NOT] NULL

ExampleSELECT *FROM MovieWHERE studioName is NULLORDER BY Title

Page 31: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

31

练习 图书 ( 书号 , 书名 , 作者 , 出版社 , 单价)

查询“数据库”一书的书号和单价 查询所有图书的名称和单价,并按单价从大到小排序 查询单价在 20 至 50元之间的图书信息 查询北京某出版社出版的图书信息 查询作者是张一,王二,刘三的书的信息 查询所有图书的书号,书名和半价信息 查询缺少出版社信息的图书的书号和书名

Page 32: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

32

Multirelation Queries

Interesting queries often combine data from more than one relation.

We can address several relations in one query by listing them all in the FROM clause.

Distinguish attributes of the same name by “<relation>.<attribute>”

Page 33: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

33

Example

Using relations Movie and MovieExec, find the name of the producer of Star War.SELECT nameFROM Movie, MovieExecWHERE title = ‘Star War’ AND producerC# = cert#;

Page 34: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

34

Formal Semantics

Almost the same as for single-relation queries:

1. Start with the product of all the relations in the FROM clause.

2. Apply the selection condition from the WHERE clause.

3. Project onto the list of attributes and expressions in the SELECT clause.

Page 35: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

35

Operational Semantics

Imagine one tuple-variable for each relation in the FROM clause. These tuple-variables visit each combination

of tuples, one from each relation. If the tuple-variables are pointing to

tuples that satisfy the WHERE clause, send these tuples to the SELECT clause.

Page 36: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

36

Disambiguating Attributes

If a query involving several relations, and among these relations are two or more attributes with the same name, you need indicate which table these attributes belong to.

Page 37: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

37

Example

MovieStar(name, address, gender, birthdate)MovieExec(name, address, cert#, netWorth)

SELECT MovieStar.name,MovieExec.nameFROM MovieStar, MovieExecWHERE MovieStar.address=MovieExec.address

Page 38: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

38

Explicit Tuple-Variables

Sometimes, a query needs to use two copies of the same relation.

Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause.

It’s always an option to rename relations this way, even when not essential.

Page 39: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

39

Example

From MovieStar, find all pairs of stars who share an address.SELECT Star1.name, Star2.nameFROM MovieStar AS Star1, MovieStar AS Star2WHERE Star1.address = Star2.address AND

Star1.name <> Star2.name;

Page 40: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

40

Union, Intersection, and Difference

Union, intersection, and difference of relations are expressed by the following forms, each involving subqueries: ( subquery ) UNION ( subquery ) ( subquery ) INTERSECT ( subquery ) ( subquery ) EXCEPT ( subquery )

Page 41: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

41

Example Using MovieStar and MovieExec, suppose we wanted

the names and addresses of all female movie stars who are also movie executives with a net worth over $10,000,000.

(SELECT name, addressFROM MovieStarWHERE gender = ‘F’) INTERSECT (SELECT name, address FROM MovieExec WHERE netWorth > 10000000);

Page 42: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

42

练习读者 ( 读者编号,姓名,电话 )图书 ( 书号 , 书名 , 作者 , 出版社 , 单价)借阅 ( 书号,读者编号,借阅日期 )

查询借阅过书号为‘ J0004’图书的读者姓名 查询王明所借阅的所有图书的书名和借阅日期

Page 43: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

43

练习 图书馆管理数据库

读者 ( 读者编号 , 姓名 , 单位 ) 图书 ( 书号 , 书名 , 作者 , 出版社 , 单价 , 类型 ) 借阅记录 ( 读者编号 , 书号 , 借阅日期 , 应还日期 ) 还书记录 ( 读者编号 , 书号 , 归还日期 )

用关系代数描述以下查询要求 查询“人民邮电出版社”出版的所有图书的相关信息 查询单价在 15元以上的书名和作者 查询8 号读者 2003 年 3 月 10日所借图书的相关信息 查询超期归还图书的读者姓名和单位 查询借阅过《天龙八部》的读者的信息 查询借阅过“金庸”所有著作的读者的姓名 查询没有借阅过任何图书的读者的姓名

Page 44: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

44

Subqueries

A parenthesized SELECT-FROM-WHERE statement (subquery ) can be used as a value in a number of places, including FROM and WHERE clauses.

Example: in place of a relation in the FROM clause, we can place another query, and then query its result. Better use a tuple-variable to name tuples of

the result.

Page 45: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

45

Subqueries That Return One Tuple

If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Usually, the tuple has one component. A run-time error occurs if there is no tuple

or more than one tuple.

Page 46: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

46

Example

From Movie, MovieExec, find the name of the producer of Star War.

Two queries would surely work:1. Find the certificate number for the producer

of Star War.2. Find the name of the person with this

certificate.

Page 47: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

47

Query + Subquery Solution

SELECT nameFROM MovieExecWHERE cert# =

(SELECT producerC# FROM Movie WHERE title = ‘Star

Wars’);

Page 48: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

48

The IN Operator

<tuple> IN <relation> is true if and only if the tuple is a member of the relation. <tuple> NOT IN <relation> means the

opposite. IN-expressions can appear in WHERE

clauses. The <relation> is often a subquery.

Page 49: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

49

Example From Movie, MovieExec and StarsIn, find all the producers

of movies in which Harrison Ford stars. SELECT name FROM MovieExec WHERE cert# IN

(SELECT producerC# FROM Movie WHERE (title, year) IN

(SELECT movieTitle, movieYear FROM StarsIn WHERE starName = ‘Harrison Ford’));

The nested query can be written as a single select-from-where expression?

Page 50: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

50

The Exists Operator

EXISTS( <relation> ) is true if and only if the <relation> is not empty.

Example: From Beers(name, manf) , find those beers that are the unique beer by their manufacturer.

Page 51: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

51

Example Query with EXISTS

SELECT nameFROM Beers b1WHERE NOT EXISTS(

SELECT *FROM BeersWHERE manf = b1.manf AND name <>

b1.name);

Page 52: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

52

The Operator ANY

x = ANY( <relation> ) is a boolean condition true if x equals at least one tuple in the relation.

Similarly, = can be replaced by any of the comparison operators.

Example: x >= ANY( <relation> ) means x is not the smallest tuple in the relation. Note tuples must have one component only.

Page 53: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

53

The Operator ALL

Similarly, x <> ALL( <relation> ) is true if and only if for every tuple t in the relation, x is not equal to t. That is, x is not a member of the relation.

The <> can be replaced by any comparison operator.

Example: x >= ALL( <relation> ) means there is no tuple larger than x in the relation.

Page 54: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

54

Example

From Movie, find the titles that have been used for two or more movies.

SELECT titleFROM Movie As oldWHERE year < ANY(

SELECT yearFROM Movie

WHERE title = old.title);

Page 55: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

55

Join Expressions

SQL provides several versions of joins. These expressions can be stand-alone

queries or used in place of relations in a FROM clause.

Page 56: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

56

Example

SELECT title, year, length, inColor, studioName, producer#, starNameFROM Movie JOIN StarsIn ON title =movieTitle AND year= moviewYear;

SELECT title, year, length, inColor, studioName, producer#, starNameFROM Movie , StarsIn WHERE title =movieTitle AND year= moviewYear;

Page 57: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

57

Outerjoins

R OUTER JOIN S is the core of an outerjoin expression. It is modified by:

1. Optional NATURAL in front of OUTER.2. Optional ON <condition> after JOIN.3. Optional LEFT, RIGHT, or FULL before OUTER.

LEFT = pad dangling tuples of R only. RIGHT = pad dangling tuples of S only. FULL = pad both; this choice is the default.

Page 58: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

58

Outerjoins (Cont.)

Suppose we wish to take the outerjoin of the two relations MovieStar(name, address, gender, birthdate) MovieExec(name, address, cert#, netWorth)

Page 59: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

59

Full Outer joinSELECT MovieStar .name AS name, MovieStar .address AS

address, gender, birthday, cert#,networthFROM MovieStar FULL OUTER JOIN MovieExec ON MovieStar.name=MovieExec.name;

name address gender

birthday

cert# networth

Mary Tyler MooreTom HanksGeorge Lucas

Maple St.Cherry Ln.Oak Rd.

‘F’‘M’NULL

9/9/998/8/88NULL

123456NULL23456

$100…NULL$200…

Tip: Keyword OUTER can be omitted.

Page 60: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

60

LEFT Outer joinSELECT MovieStar .name AS name, MovieStar .address AS

address, gender, birthday, cert#,networthFROM MovieStar LEFT OUTER JOIN MovieExec ON MovieStar.name=MovieExec.name;

name address gender

birthday

cert# networth

Mary Tyler MooreTom Hanks

Maple St.Cherry Ln.

‘F’‘M’

9/9/998/8/88

123456NULL

$100…NULL

Tip: Keyword OUTER can be omitted.

Page 61: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

61

Right Outer joinSELECT MovieStar .name AS name, MovieStar .address AS

address, gender, birthday, cert#,networthFROM MovieStar RIGHT OUTER JOIN MovieExec ON MovieStar.name=MovieExec.name;

name address gender

birthday

cert# networth

Mary Tyler MooreGeorge Lucas

Maple St.Oak Rd.

‘F’NULL

9/9/99NULL

12345623456

$100…$200…

Tip: Keyword OUTER can be omitted.

Page 62: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

62

Controlling Duplicate Elimination

Force the result to be a set by SELECT DISTINCT . . .

Page 63: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

63

Example: DISTINCT

From Movie, MovieExec, StarIn, find all the producers of movies in which Harrison Ford stars :

SELECT DISTINCT nameFROM Movie, MovieExec, StarsIn

WHERE cert# = producerC# AND tile = movieTitle AND

year = moiveYear AND starName = ‘Harrison Ford’;

Notice that without DISTINCT, each name would be listed many times.

Page 64: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

64

Aggregations

SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column.

Also, COUNT(*) counts the number of tuples.

Page 65: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

65

Example: Aggregation

From MovieExec, find the average net worth of all movie executives:

SELECT AVG(netWorth)FROM MovieExec;

Page 66: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

66

Eliminating Duplicates in an Aggregation

Use DISTINCT inside an aggregation. Example: find the number of different

name in MovieExec:SELECT COUNT(DISTINCT name)FROM MovieExec;

Page 67: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

67

NULL’s Ignored in Aggregation

NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column.

But if there are no non-NULL values in a column, then the result of the aggregation is NULL.

Page 68: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

68

Example: Effect of NULL’s

SELECT count(*)FROM MovieExec;

SELECT count(name)FROM MovieExec;

The number of tuplesin MovieExec.

The number of tuplesthat name is not NULLin MovieExec.

Page 69: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

69

Grouping

We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes.

The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.

Page 70: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

70

Example: Grouping

From Movie, the sum of the lengths of all movies for each studio is expressed by:

SELECT studioName, SUM(length)FROM MovieGROUP BY studioName;

Page 71: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

71

Example: Grouping

From Movie and MovieExec, find each producer’s total length of film produced.

SELECT name, SUM(length)FROM MovieExec, MovieWHERE producerC# = cert#GROUP BY name;

Page 72: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

72

Restriction on SELECT Lists With Aggregation

If any aggregation is used, then each element of the SELECT list must be either:

1. Aggregated, or2. An attribute on the GROUP BY list.

Page 73: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

73

Illegal Query Example

You might think you could find the movie that is the longest in length:

SELECT title, MAX(length)FROM Movie

But this query is illegal in SQL.

Page 74: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

74

HAVING Clauses

HAVING <condition> may follow a GROUP BY clause.

If so, the condition applies to each group, and groups not satisfying the condition are eliminated.

Page 75: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

75

Example: HAVING

From Movie and MovieExec, find the total film length for only those producers who made at least one film prior to 1930.

SELECT name, SUM(length)FROM MovieExec, MovieWHERE producerC# = cert#GROUP BY nameHAVING MIN(year) < 1930;

Page 76: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

76

Requirements on HAVING Conditions

These conditions may refer to any relation or tuple-variable in the FROM clause.

They may refer to attributes of those relations, as long as the attribute makes sense within a group; i.e., it is either:

1. A grouping attribute, or2. Aggregated.

Page 77: 1 Chapter 6 Introduction to SQL uSelect-From-Where Statements uSubqueries uGrouping and Aggregation.

77

练习读者 ( 读者编号,姓名,电话 )图书 ( 书号 , 书名 , 作者 , 出版社 , 单价)借阅 ( 书号,读者编号,借阅日期 )

查询借阅过图书的读者姓名 查询图书总数 查询每天的图书借阅量(图书借阅记录数)