20
Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co.

Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Embed Size (px)

Citation preview

Page 1: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Chapter 13 Subqueries and Views

Part C. SQL

Copyright 2005 Radian Publishing Co.

Page 2: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.2/20

Contents

Chapter 13 Subqueries and Views

13.1 Introduction to Subqueries13.2 Type 1: Single-valued Subqueries• 13.2 A. Subqueries with

Aggregate Functions• 13.2 B. Involving two different

tables

13.3 Type 2: Multi-valued Subqueries• 13.3 A. The IN Operator• 13.3 B. The = ANY Operator• 13.3 C. The EXISTS Operator

13.4 Further Examples

13.5 Views• 13.5 A. Creating a Simple view• 13.5 B. Finding the Mode

Page 3: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.3/20

Chapter 13 Subqueries and Views

A subquery is a query which is embedded in another query.

A view is a subset of the database that is presented to some users.

It is a virtual table that does not take up as much physical space as an

ordinary table does, yet it allows you to perform query and updating to

the database.

Page 4: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.4/20

13.1 Introduction to Subqueries (1/2)

If a query consists of a subquery, the subquery will always be executed

first. There are several variations in subqueries:

1. The subquery and the main query may refer to the same table, or different tables.

2. The result of a subquery may be a single value, or a set of values.

3. The subquery may be placed in the WHERE clause, or in the FROM clause.

4. A subquery may consist of another subquery, i.e. multi-sub-level.

Page 5: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.5/20

13.1 Introduction to Subqueries (2/2)

Singled-valued subqueries need relational operators,

like =, >, >=, <>, <, <=;

Multi-valued subqueries need operators like IN, ANY and EXISTS.

Page 6: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.6/20

13.2 Type 1: Single-valued Subqueries

Single-valued subquery:

SELECT ... FROM Table1

WHERE FieldName1 =|>|>=|<|<=|<>

(SELECT ... from Table2)

The query and the subquery may use the same table or different tables.

Page 7: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.7/20

13.2 A. Subqueries with Aggregate Functions

A subquery with an aggregate function returns a single value.

Page 8: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.8/20

13.2 B. Involving two different tables

The query and the subquery may use different tables.

Page 9: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.9/20

13.3 Type 2: Multi-valued Subqueries

Some subqueries return a set of records. We cannot use simple

relational operators, like =, >, <, <> etc. Special operators, like IN and

EXISTS, are designed for multi-valued subqueries.

Multi-valued subquery:

SELECT ... FROM Table1

WHERE FieldName1 [IN | = ANY | EXISTS]

(SELECT ... from Table2)

Page 10: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.10/20

13.3 A. The IN Operator

The IN operator compares a piece of data with a set of values and

returns true if any one of the values in the set matches with the data.

Page 11: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.11/20

13.3 B. The = ANY Operator

The operator = ANY is the same as the IN operator.

Page 12: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.12/20

13.3 C. The EXISTS Operator

The function EXISTS() is a boolean function which tells whether the

result of a subquery is successful or not.

Page 13: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.13/20

13.4 Further Examples

Refer to textbook P. 314

Page 14: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.14/20

13.5 Views (1/2)

A view is a named query result from one or more tables in a database.

A view is often called a virtual table. You can perform queries and

updating on a view, yet it does not take up as much physical space as

an ordinary table.

When a view is changed, the tables that the view is built from will be

changed.

Page 15: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.15/20

13.5 Views (2/2)

The major advantages of views are:

1. A view can save the effort of entering the same complicated SQL statement every time.

2. A view can enforce security. It can prevent users from viewing other data that may be private or confidential.

Page 16: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.16/20

13.5 A. Creating a Simple view (1/2)

The basic syntax creating a view is

CREATE VIEW ViewName AS SELECT ...

Page 17: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.17/20

13.5 A. Creating a Simple view (2/2)

To make a query on the view SALESVIEW

Page 18: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.18/20

13.5 B. Finding the Mode (1/3)

We may use View to help finding the mode, which tells the most

popular quantity.

For example:

Page 19: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.19/20

13.5 B. Finding the Mode (2/3)

It is clear that “Central”, “Tai Kok Tsui” and “Tai Po” are popular district.

Page 20: Chapter 13 Subqueries and Views Part C. SQL Copyright 2005 Radian Publishing Co

Copyright 2005 Radian Publishing Co.20/20

13.5 B. Finding the Mode (3/3)

The results of the query show the most popular districts.