Wk7_sub Queries & Query Expressions
Wk7_sub Queries & Query Expressions
3
Week 7 Database Management II
Learning Outcomes/Objectives
4
Week 7 Database Management II
What is a Sub-query?
5
Week 7 Database Management II
Using Sub-queries
6
Week 7 Database Management II
Sub-query Basic Syntax
SELECT ALL|DISTINCT sub-query_select_list
FROM table_name(s)
[WHERE] search_condition
[GROUP BY] column_name(s)
[HAVING] search_condition
7
Week 7 Database Management II
Features of Subqueries
Subquery – one select statement inside another
Used in the WHERE clause
Subqueries can return many rows.
Subqueries can only return 1 column.
Subqueries can be used as a replacement for view or self-join.
Some programmers see them as easier to understand than other options.
The main drawback is that they can be much slower than self-join or view.
8
Week 7 Database Management II
Simple Example
Who in the database is older than Jim Smith?
Dob
11 Jan 1980
SELECT name FROM driver WHERE dob > ’11 Jan 1980’;
name
Bob Smith
Bob Jones
10
Week 7 Database Management II
With a subquery…
Combined together:
SELECT name
FROM driver
WHERE dob > (SELECT dob
FROM driver
WHERE name = ‘Jim Smith’);
11
Week 7 Database Management II
ANY and ALL
To support subqueries which return more than 1 row we need some additional
operators… ANY and ALL.
ANY – changes the rule so that it must be true for at least one of the rows
returned from the subquery.
ALL – changes the rule so that it must be true for each and every row
returned from the subquery.
The ANY or ALL operator goes immediately before the open bracket of the
subquery.
12
Week 7 Database Management II
Example 1
What cars are the same colour as a car owned by Jim Smith?
Jim owns 2 cars, one is RED and the other BLUE. We are looking for cars which
are either RED or BLUE.
13
Week 7 Database Management II
Example 2
List the drivers younger than all the people who own a blue car.
We are looking for the age of drivers who own a BLUE car, and listing drivers
who are younger than all of those ages.
14
Week 7 Database Management II
IN and NOT IN
We earlier saw IN working for sets like (‘A’,’B’).
A subquery itself returns its result as a set.
Therefore we can use IN and NOT IN on subqueries.
Question: Which cars are the same colour as one of Jim Smith’s cars?
15
Week 7 Database Management II
Example of NOT IN
Question: Which cars DO NOT have the same colour as one of Jim
Smith’s cars?
16
Week 7 Database Management II
Differences between an actual SELECT statement and a
sub-query
A sub-query produces a single column of data as its query result. This
implies that most times, a sub-query has a single select item as it’s sub-
query select list in a SELECT clause.
As you must have observed from the syntax, the ORDER BY clause
cannot be used in a sub-query. This is because the sub-query result is
never visible to the user but rather used internally by the main query.
Column names that may appear in a sub-query may refer to columns of
the table in the main (outer) query.
Only a single SELECT statement is allowed in most implementation of
sub-queries. A UNION of several SELECT statements is not allowed.
19
Week 7 Database Management II
Outer References
Within the body of the sub-query, a column name that does not
refer to any of the tables named in the FROM clause but is rather
from the table named in the FROM clause of the main (outer)
query is called an outer reference.
20
Week 7 Database Management II
Sub-query Search Conditions
Recall that a sub-query usually appears in the WHERE or HAVING
clause as part of the search condition of a query. Most SQL
products offer the following sub-query search conditions:
Sub-query comparison test
Sub-query set membership test
Existence test
Quantified comparison test
21
Week 7 Database Management II
The Sub-query Comparison Test (=, <>, <, >,
<=, >=)
22
Week 7 Database Management II
Syntax for the Comparison Test
SELECT select_list
FROM table_name(s)
WHERE expression{=,<>,<,>,<=,>=} (sub-query)
23
Week 7 Database Management II
The Set Membership Test
24
Week 7 Database Management II
The Existence Test
25
Week 7 Database Management II
Quantified Test
The quantified test (ANY and ALL) use one of the simple
comparison operators to compare a test value to all of the values
returned by a sub-query, checking to see whether the comparison
holds for some or all of the values.
The syntax is as follows:
SELECT select_list
FROM table_name(s)
WHERE test_expression {=,<>,<,>,<=,>=}[ANY|ALL]
(sub-query)
26
Week 7 Database Management II
Sub-Queries and Joins
27
Week 7 Database Management II
Nested Sub-queries
28
Week 7 Database Management II
Correlated Sub-queries
29
Week 7 Database Management II
Sub-queries in the HAVING Clause
Many sub-queries are often found in the WHERE clause of the
SELECT statement.
They can however be used in the HAVING clause of a query.
When a sub-query appears in the HAVING clause, the results of
the sub-query are used to select rows groups that contribute data
to the query results.
30
Week 7 Database Management II