database design and - global edulink · 2018. 10. 31. · appropriate to use a subquery rather than...
TRANSCRIPT
0 | P a g e
Database Design and Implementation Module 14
1 | P a g e
14. Module 14: Sub queries
Table of Contents
14. Module 14: Sub queries ............................................................................................ 1
14.1 Subqueries and group functions ...................................................................................... 2
14.2 Single-row subquery ........................................................................................................ 2
14.3 Multiple-row subquery .................................................................................................... 3
14.4 Group functions ............................................................................................................... 5
2 | P a g e
14.1 Subqueries and group functions
On completion of this chapter you should be able to:
use a sub query
Apply group functions.
When trying to extract data which involves using a number of tables it is sometimes appropriate to use a subquery rather than a select command with lots of joins. Subqueries are also referred to as nested queries. A subquery contains at least one nested query. The innermost query is executed first and the value returned is then used at the next level up.
There are two types of sub query
Single Row – returns only one row of data
Multiple Row – returns more than one row of data.
14.2 Single-row subquery
The general format of a subquery is:
SELECT column name,
FROM table name
WHERE column name
Relational operator
(SELECT column name…
FROM table name
WHERE condition);
The subquery must be enclosed within ( ) The ORDER BY clause cannot be used.
The relational operators =, >, <, >=, <=, <>! = can be used.
An error will occur if more than one row is returned from a subquery, and if no rows are returned the value will be a NULL.
A subquery may be used on a single table. For example suppose you wanted to select the track name and its music category where the track music category description was Pop. The following can be used:
SELECT track title, track_cat_ID
FROM track
WHERE track_cat_ID =
(SELECT cat_ID FROM category
WHERE cat description = ‘Pop’);
3 | P a g e
14.3 Multiple-row subquery
A multiple row subquery returns more than one row therefore you need to use the following
operators, not the ones used for the single row queries:
Operator Use
IN Matches any value in a list
ALL Compares the given value with every returned subquery value
ANY or SOME Compares the given value with each returned subquery value
Using the Order System specification in Appendix C let us look at an example. Suppose you
wanted to select the product descriptions for items on order Z01. The item table product
numbers for the chosen order would be returned and then used to match with the product
table product numbers in order to select the product descriptions.
SELECT Prod_desc FROM product
WHERE product.Prod_no
IN (SELECT item.Prod_no FROM item
WHERE Order no = ‘Z01’);
4 | P a g e
Exercise 1
Set up the Order System tables and insert the sample data as shown in Appendix C.
Answer the following using sub queries:
a. Name the products that “Ads” has ordered.
b. Name the customers who have placed an order in 2012.
c. Name the customers who have ordered “Chocolate”.
5 | P a g e
14.4 Group functions
A group function operates on a group of rows and returns a single result. Suppose you wanted
to find out which cd cost the most, or what the total value of all your cds is, these queries can
be answered using the group functions.
Function Description
SUM(column name) Calculates the total value of a column, null values are
ignored.
AVG(column name) Finds the average of all the values in a column, null values
are ignored.
MAX(column name |
expression)
Finds the maximum value in a column or expression, null
values are ignored.
MIN(column name |
expression)
Finds the minimum value in a column or expression, null
values are ignored.
COUNT(* | column name |
expression)
* Counts the number of rows, including nulls
If a column or expression is used counts non null values.
Note: The | separates the optional values
Here is an example showing the average, maximum, minimum and total for the cd prices:
SELECT AVG (cd_price), MAX (cd_price), MIN (cd_price), and SUM (cd_price) FROM cd;
6 | P a g e
Sometimes it is necessary to apply group functions to separate groups of rows rather than all
the rows. The GROUP BY clause can be used to group the rows. The format is as follows:
SELECT column name, group function (column name)
FROM table name
[WHERE condition(s)]
[GROUP BY column name | expression]
[ORDER BY column name | expression [ASC | DESC]];
When using the GROUP BY clause the columns in SELECT must also appear in the GROUP BY.
The WHERE clause cannot be used to restrict groups, but it can be used to restrict the data
before the grouping. By default GROUP BY when used with a column will output results in
ascending order.
Here is an example of a GROUP BY which will tell you how many tracks you have for each
music category:
SELECT track_cat_ID, COUNT(*) “no of tracks”
FROM track
GROUP BY track_cat_ID;
In order to restrict groups, the HAVING clause can be used, so suppose you only wanted to
see the number of tracks for music categories that had more than 16 tracks, the following
could be used:
SELECT track_cat_ID, COUNT (*) “no of tracks”
FROM track
GROUP BY track_cat_ID
HAVING COUNT (*) > 16;
7 | P a g e
You have now been introduced to the main ORACLE SQL commands and some of the built-in
functions, so you should now be able to search for and try some of the other SQL commands
and functions.
There are many SQL resources available on the web which can assist you to develop your skills
including:-
An Oracle database SQL language reference can be found here:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/toc.htm
For more help with Oracle APEX try here:
https://community.oracle.com/community/database/developer-
tools/application_express
Exercise 2
Write SELECT statements to answer the following:
a. Count the total number of tracks.
b. Show the maximum and minimum track lengths.
c. Display the average cd price by company for any companies with an average price
< 10.