Lab#05
Lab#05
Lab#05
• The first query in the SQL statement is known as the outer query
• The query inside the SQL statement is known as the inner query
• The output of an inner query is used as the input for the outer query
A subquery can return one value or multiple values. To be precise, the subquery can
return:
• One single value (one column and one row). This subquery is used anywhere
value, not a list of values. Therefore, the subquery must return only one value
1
(One column, one row). If the query returns multiple values, the DBMS
• A list of values (one column and multiple rows). This type of subquery is
used anywhere a list of values is expected, such as when using the IN clause.
subquery can be used anywhere a table is expected, such as when using the
FROM clause.
It’s important to note that a subquery can return no values at all; it is a NULL. In such
cases, the output of the outer query may result in an error or a null empty set depending
In the following sections, you will learn how to write subqueries within the SELECT
The most common type of subquery uses an inner SELECT subquery on the right side of
a WHERE comparison expression. For example, to find the prices of all tickets with a
price greater than or equal to the average ticket price, you write the following query:
2
SELECT TICKET_NO, TICKET_TYPE, TICKET_PRICE
FROM TICKET
Note that this type of query, when used in a >, <, =, >=, or <= conditional expression,
requires a subquery that returns only one single value (one column, one row). The value
generated by the subquery must be of a “comparable” data type; if the attribute to the left of
the comparison symbol is a character type, the subquery must return a character string. Also,
if the query returns more than a single value, the DBMS will generate an error.
5.2 IN Subqueries
The following query displays all employees who work in a Theme Park that has the word
‘Fairy’ in its name. As there are a number of different Theme Parks that match this
3
criteria you need to compare the PARK_CODE not to one park code (single value), but to
a list of park codes. When you want to compare a single attribute to a list of values, you
use the IN operator. When the PARK_CODE values are not known beforehand but they
can be derived using a query, you must use an IN subquery. The following example lists
Task 5.1 Enter and execute the above query and compare your output with that shown in
Figure 71.
4
5.3 HAVING Subqueries
A subquery can also be used with a HAVING clause. Remember that the HAVING
conditional criteria to the grouped rows. For example, to list all PARK_CODEs
where the total quantity of tickets sold is greater than the average quantity sold, you
GROUP BY PARK_CODE
Task 5.2 Using the query above as a guide, write a new query to display the first and last
names of all employees who have worked in total greater that the average number of
5
hours in total during May 2007. Your output should match that shown in Figure 73.
Group by x means put all those with the same value for X in the one group).
Group By x, y means put all those with the same values for both X and Y in the one
group.
To illustrate using an example, let's say we have the following table, to do with who is
attending what subject at a university:
6
Because there are 5 entries for ITB001, and 2 for MKB114
This is because, when we group by two columns, it is saying "Group them so that all of
those with the same Subject and Semester are in the same group, and then calculate all the
aggregate functions (Count, Sum, Average, etc.) for each of those groups". In this example,
this is demonstrated by the fact that, when we count them, there are three people doing ITB001
in semester 1, and two doing it in semester 2. Both of the people doing MKB114 are in semester
1, so there is no row for semester 2 (no data fits into the group "MKB114, Semester 2")
So far, you have learned that you must use an IN subquery when you need to compare a
value to a list of values. But the IN subquery uses an equality operator; that is, it selects
only those rows that match (are equal to) at least one of the values in the list. What
happens if you need to do an inequality comparison (> or <) of one value to a list of
values? For example, to find the ticket_numbers and corresponding park_codes of the
7
tickets that are priced higher than the highest-priced ‘Child’ ticket you could write the
following query.
FROM TICKET
This query is a typical example of a nested query. The use of the ALL operator allows
you to compare a single value (TICKET_PRICE) with a list of values returned by the
nested query, using a comparison operator other than equals. For a row to appear in
the result set, it has to meet the criterion TICKET_PRICE > ALL of the individual
8
5.5 Attribute list Subqueries
The SELECT statement uses the attribute list to indicate what columns to project in the
resulting set. Those columns can be attributes of base tables or computed attributes or the
result of an aggregate function. The attribute list can also include a subquery expression,
also known as an inline subquery. A subquery in the attribute list must return one single
value; otherwise, an error code is raised. For example, a simple inline query can be used
to list the difference between each tickets’ price and the average ticket price:
FROM TICKET;
9
Figure 75 Displaying the difference in ticket prices.
This inline query output returns one single value (the average ticket’s price) and that the
value is the same in every row. Note also that the query used the full expression instead of
the column aliases when computing the difference. In fact, if you try to use the alias in the
difference expression, you will get an error message. The column alias cannot be used in
computations in the attribute list when the alias is defined in the same attribute list.
A correlated subquery is a subquery that executes once for each row in the outer query.
The relational DBMS uses the same sequence to produce correlated subquery results:
2. For each row of the outer query result set, it executes the inner query by passing
That process is the opposite of the subqueries you have seen so far. The query is called a
correlated subquery because the inner query is related to the outer query because the
inner query references a column of the outer subquery. For example, suppose you want
to know all the ticket sales in which the quantity sold value is greater than the average
quantity sold value for that ticket (as opposed to the average for all tickets). The
FROM SALES_LINE SL
10
WHERE SL.LINE_QTY > (SELECT AVG(LINE_QTY)
FROM SALES_LINE SA
As you examine the output shown in figure 76, note that the SALES_LINE table is
Correlated subqueries can also be used with the EXISTS special operator. For example,
suppose you want to know all the names of all Theme Parks where tickets have been
recently sold. In that case, you could use a correlated subquery as follows:
FROM THEMEPARK
11
The output for this query is shown in figure 77.
12
This order is:
13
Exercises
E 5.1 Write a query that displays the first name, last name of all employees who earn
more than the average hourly rate. Do not display duplicate rows. Your output should
E.5.2 Write a query to display an employee’s first name, last name and date worked
which lists the difference between the number of hours an employee has worked on an
attraction and the average hours worked on that attraction. Label this column
E 5.3 Type in and execute the two correlated subqueries in section 5.6 and check
E 5.4 Modify the second query you entered in E 5.3 to display all the theme parks
E 5.5 Write a query to find the attract capacity, with a attract capacity less than or equal
14
References:
https://www.sisense.com/blog/sql-query-order-of-operations/
https://sqlbolt.com/lesson/select_queries_order_of_execution
15