Download presentation
Presentation is loading. Please wait.
1
Chapter 13 Subqueries and Views
Part C. SQL Chapter 13 Subqueries and Views Copyright 2005 Radian Publishing Co.
2
Copyright 2005 Radian Publishing Co.
Contents Chapter 13 Subqueries and Views 13.1 Introduction to Subqueries 13.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 Copyright 2005 Radian Publishing Co.
3
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. Copyright 2005 Radian Publishing Co.
4
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: The subquery and the main query may refer to the same table, or different tables. The result of a subquery may be a single value, or a set of values. The subquery may be placed in the WHERE clause, or in the FROM clause. A subquery may consist of another subquery, i.e. multi-sub-level. Copyright 2005 Radian Publishing Co.
5
13.1 Introduction to Subqueries (2/2)
Singled-valued subqueries need relational operators, like =, >, >=, <>, <, <=; Multi-valued subqueries need operators like IN, ANY and EXISTS. Copyright 2005 Radian Publishing Co.
6
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. Copyright 2005 Radian Publishing Co.
7
13.2 A. Subqueries with Aggregate Functions
A subquery with an aggregate function returns a single value. Copyright 2005 Radian Publishing Co.
8
13.2 B. Involving two different tables
The query and the subquery may use different tables. Copyright 2005 Radian Publishing Co.
9
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) Copyright 2005 Radian Publishing Co.
10
Copyright 2005 Radian Publishing Co.
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. Copyright 2005 Radian Publishing Co.
11
Copyright 2005 Radian Publishing Co.
13.3 B. The = ANY Operator The operator = ANY is the same as the IN operator. Copyright 2005 Radian Publishing Co.
12
Copyright 2005 Radian Publishing Co.
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. Copyright 2005 Radian Publishing Co.
13
Copyright 2005 Radian Publishing Co.
13.4 Further Examples Refer to textbook P. 314 Copyright 2005 Radian Publishing Co.
14
Copyright 2005 Radian Publishing Co.
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. Copyright 2005 Radian Publishing Co.
15
Copyright 2005 Radian Publishing Co.
13.5 Views (2/2) The major advantages of views are: A view can save the effort of entering the same complicated SQL statement every time. A view can enforce security. It can prevent users from viewing other data that may be private or confidential. Copyright 2005 Radian Publishing Co.
16
13.5 A. Creating a Simple view (1/2)
The basic syntax creating a view is CREATE VIEW ViewName AS SELECT ... Copyright 2005 Radian Publishing Co.
17
13.5 A. Creating a Simple view (2/2)
To make a query on the view SALESVIEW Copyright 2005 Radian Publishing Co.
18
Copyright 2005 Radian Publishing Co.
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: Copyright 2005 Radian Publishing Co.
19
Copyright 2005 Radian Publishing Co.
13.5 B. Finding the Mode (2/3) It is clear that “Central”, “Tai Kok Tsui” and “Tai Po” are popular district. Copyright 2005 Radian Publishing Co.
20
Copyright 2005 Radian Publishing Co.
13.5 B. Finding the Mode (3/3) The results of the query show the most popular districts. Copyright 2005 Radian Publishing Co.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.