0% found this document useful (0 votes)
47 views

COSC 2307: Sub-Queries

This document discusses subqueries in SQL. It provides examples of using subqueries to find customers who placed orders in a given month, products ordered in a given month, products not ordered in a given month, and customers who did not place any orders. The key aspects covered are using IN, NOT IN, and DISTINCT in subqueries to filter the rows returned from the main query based on the results of an inner query.

Uploaded by

BhavikDave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

COSC 2307: Sub-Queries

This document discusses subqueries in SQL. It provides examples of using subqueries to find customers who placed orders in a given month, products ordered in a given month, products not ordered in a given month, and customers who did not place any orders. The key aspects covered are using IN, NOT IN, and DISTINCT in subqueries to filter the rows returned from the main query based on the results of an inner query.

Uploaded by

BhavikDave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

COSC 2307

Module 10
SUB-QUERIES
customer orders orderline
OrderNum ProductNum Quantity SalesPrice
CustNum CustName City CreditLimit OrderNum OrderDate Filled CustNum
1 7 2 34.99
3 Kent BURLINGTON 2000 1 05/09/1997 1 2
2 6 1 980
4 Moore BRAMPTON 1000 2 05/09/1997 1 9
3 4 16 25
5 Felipe MISSISSAUGA 4500 3 05/09/1997 0 6
4 1 1 44.95
7 Khoury MISSISSAUGA 1000 4 01/10/1997 0 10
5 2 1 99.99
8 Brant OAKVILLE 1500 5 08/10/1997 1 3
5 8 1 25.99
9 Wells BURLINGTON 0 6 19/10/1997 1 5
6 7 1 34.99
13 Ironwood OAKVILLE 100 7 15/10/1997 1 5
7 3 1 299.99
14 Joe TORONTO 4000 8 17/10/1997 1 8
8 2 1 99.99
15 Test22 TORONTO 22 9 04/11/1997 1 9
9 5 2 29.99
16 Joe G. BRAMPTON 1000 10 06/11/1997 1 3
10 5 11 27.5
11 01/12/1997 0 7
11 3 1 295
12 04/12/1997 0 1
11 4 3 42
13 08/12/1997 0 6
11 5 4 27.5
14 15/12/1997 0 4
12 8 1 25.99
15 17/12/1997 0 5
13 6 1 975
product 14 7 2 34.99
15 2 1 105
ProductNum Descr ProductType MSRP Onhand
15 7 1 34.99
1 Swiss army knife Sports 51.69 22
2 Electric heater Hardware 99.99 10
3 Snowboard Sports 344.99 37
4 Hockey stick Sports 31.63 172
5 Snow shovel Hardware 29.99 53
6 Stove Appliance 989.99 17
7 Toaster Appliance 34.99 12
8 Jumper cables Hardware 25.99 38
9 test test 30 1
SELECT Statement
• Used for queries on single or multiple tables
• So far we have learned the following clauses of the SELECT
statement:
– SELECT
• List the columns (and expressions) that should be returned from the query
– FROM
• Indicate the table(s) or view(s) from which data will be obtained
– WHERE
• Indicate the conditions under which a row will be included in the result
– ORDER BY
• Sorts the result according to specified criteria
Sample query
• Show all the customers, sort by customer city and customer name:
SELECT *
FROM customer
ORDER BY city, custname;
Sample query
• Show all the orders placed by customers in October 1997
SELECT *
FROM orders
WHERE orderdate BETWEEN '1997-10-1' AND '1997-10-31';
COMPOUND QUERIES
• We can use queries within queries
• From time to time, we may want to write more
complicated SELECT queries that have the results
of other SELECT queries as part of their WHERE
clause
COMPOUND QUERIES
• Note: compound queries are not always the most practical way of
writing a query – there are probably easier ways! They do have their
uses though
• A few conditions we can use in compound queries are IN, NOT IN
• We’ll start with IN – so what is it?
COMPOUND QUERIES example: sub-query
• Show customers who placed an order in October 1997.
• Step 1: get a list of all the customer numbers for orders placed in
October 1997
SELECT custnum
FROM orders
WHERE orderdate BETWEEN '1997-10-1' AND '1997-10-31';
COMPOUND QUERIES example
• Show customers who placed an order in October 1997.
• Step 1 - improved: get a list of all the customer numbers for orders
placed in October 1997, but show no duplicates
SELECT DISTINCT custnum
FROM orders
WHERE orderdate BETWEEN '1997-10-1' AND '1997-10-31';
COMPOUND QUERIES example
• Show customers who placed an order in October 1997.
• Step 2 - get a list of all the customers who are on the list
SELECT *
FROM customer
WHERE custnum IN (10,3,5,8);
Sub-query
• Show customers who placed an order in October 1997.
SELECT * SELECT DISTINCT custnum
FROM orders
FROM customer WHERE orderdate BETWEEN '1997-10-1' AND
'1997-10-31';
WHERE custnum IN (10,3,5,8);
• And now automate it – replace hard-coded list with a sub-query
SELECT *
FROM customer
WHERE custnum IN (SELECT DISTINCT custnum
FROM orders
WHERE orderdate BETWEEN '1997-10-1' AND '1997-10-31 ' );
Your turn
• Using sub-query, show all the customers who have placed an order in
September 1997
Your turn - SOLUTION
• Using sub-query, show all the customers who have placed an order in
September 1997

SELECT *
FROM customer
WHERE custnum IN (SELECT DISTINCT custnum
FROM orders
WHERE orderdate BETWEEN '1997-9-1' AND '1997-9-30 ' );
Sub-query example 2
• Show products ordered in September 1997
Sub-query example 2 - SOLUTION
• Show products ordered in September 1997
• Step 1 – show order numbers placed in September 1997 (no
duplicates)
SELECT ordernum
FROM orders
WHERE orderdate BETWEEN '1997-9-1' AND '1997-9-30 ';
Sub-query example 2 - SOLUTION
• Show products ordered in September 1997
• Step 2 – show product numbers for orders placed in September 1997
(no duplicates)
SELECT DISTINCT productnum
FROM orderline
WHERE ordernum IN(SELECT ordernum
FROM orders
WHERE orderdate BETWEEN '1997-9-1' AND '1997-9-30 ');
Sub-query example 2 - SOLUTION
• Show products ordered in September 1997
• Finally – show product information for product numbers for orders
placed in September 1997
SELECT *
FROM product
WHERE productnum IN(SELECT DISTINCT productnum
FROM orderline
WHERE ordernum IN(SELECT ordernum
FROM orders
WHERE orderdate BETWEEN '1997-9-1' AND '1997-9-30 '));
Sub-query - example 3
• Show products NOT ordered by anyone in September 1997
SELECT *
FROM product
WHERE productnum NOT IN(SELECT DISTINCT productnum
FROM orderline
WHERE ordernum IN(SELECT ordernum
FROM orders
WHERE orderdate BETWEEN '1997-9-1' AND '1997-9-30 '));
Sub-query - example 4
• Show customers who did not place orders in October 1997.
SELECT *
FROM customer
WHERE custnum NOT IN (SELECT DISTINCT custnum
FROM orders
WHERE orderdate BETWEEN '1997-10-1' AND '1997-10-31 ' );
Your turn
• Show products nobody has ever ordered
Your turn - SOLUTION
• Show products nobody has ever ordered

SELECT *
FROM product
WHERE productnum NOT IN(SELECT DISTINCT productnum
FROM orderline);
Your turn
• Show all the customers from Toronto who never placed any orders
Your turn - solution
• Show all the customers from Toronto who never placed any orders

SELECT *
FROM customer
WHERE city ='Toronto'
AND custnum NOT IN (SELECT DISTINCT custnum FROM orders);

You might also like