Lab09 Mysql
Lab09 Mysql
SUBQUERY
v Main contents: Concept and use of subqueries, correlated and non-correlated subqueries.
1. Concept of Subquery
To combine data tables together, in addition to joins and set operators, SQL provides another
way to return data from multiple tables called subquery. When one SELECT statement is used
in another, the inner SELECT is called a subquery, another way is called a nested query, or an
inner query. Basically, a subquery can be used anywhere an expression can be used.
SELECT MAX(orderDate) FROM orders return the most recent date in the orders and
this value will be used in the WHERE clause of the outer query. Combining the two queries above
will return a list of orders for the most recent day.
Subqueries are divided into two categories: non-correlated subqueries and correlated
subqueries.
2. Non-correlated subquery
Example: Get products that are not included in any orders. The inner subquery will return the
product codes included in the orderdetails table. The external subquery will return products
whose codes are not in the list of product codes.
SELECT *
FROM products
WHERE productCode NOT IN
(SELECT productCode
FROM orderdetails
);
3. Correlated subquery
Example: Get the products with a quantity in stock greater than the average quantity of
products of the same product line.
SELECT * FROM products p
WHERE quantityInStock >
(SELECt avg(quantityInStock)
FROM products
WHERE productLine = p.productLine
);
The query execution process is as follows: for each product line of an external query, the internal
query statement finds the average quantity of products of the same product line. The subquery
will be put into the WHERE clause to examine.
Example: Get the products included in the order. Use the EXISTS operator to check for
existence.
In the example above the name of the product is the result of the subquery on the products
table.
Example: For each product, include the total number of products that were ordered
SELECT productName,
(SELECT sum(quantityOrdered) FROM orderdetails WHERE
productCode = p.productCode) as totalQuantityOrderd
FROM products as p
ORDER BY totalQuantityOrderd desc
In the example above the total quantity value is set as the result of the query from the
orderDetails table
The example above can be rewritten by treating the result of a subquery as a data table, and
then connecting the products table to this result table.
The result of the query gives the same results as the previous query.
v Practical Exercises:
1. Use the subquery to list products that were ordered in March 2005. Compare to using
JOIN instead of using subqueries.
2. Use the subquery to display information about orders in the most recent month (using
information from the orders table).
3. Use subqueries to give information about orders and total value of this order (using
information from orders and orderdetails tables). Compare to using JOIN instead of
using subqueries.
4. Use the subquery get the customer's name and the total amount they have to pay.