Question: Review the SQL below and execute it on the sakila database to see the results. Then answer the following questions: How many subqueries are used?

Review the SQL below and execute it on the sakila database to see the results. Then answer the following questions:
How many subqueries are used?
How many different types of subqueries are used? Describe each type. (Note that some subqueries may be examples of more than one type.)
What information is the query providing? (Ignore the LIMIT 25 because it is only there for convenience.)
WITH unreturned_film_list AS
(
SELECT DISTINCT f.film_id
FROM rental r
JOIN inventory i
ON r.inventory_id=i.inventory_id
JOIN film f
ON i.film_id=f.film_id
WHERE
r.return_date IS NULL
),
inventory_list AS
(
SELECT DISTINCT f.film_id, i.inventory_id
FROM inventory i
JOIN film f
ON i.film_id=f.film_id
WHERE
f.film_id IN(SELECT f.film_id FROM unreturned_film_list)
),
unreturned_inventory_list AS
(
SELECT DISTINCT f.film_id, i.inventory_id
FROM rental r
JOIN inventory i
ON r.inventory_id=i.inventory_id
JOIN film f
ON i.film_id=f.film_id
WHERE
r.return_date IS NULL
)
SELECT
f.film_id AS `Film ID`,
f.title AS Title,
(SELECT COUNT(DISTINCT il.inventory_id) FROM inventory_list il WHERE il.film_id=f.film_id) AS `In Inventory`,
(SELECT COUNT(DISTINCT uil.inventory_id) FROM unreturned_inventory_list uil WHERE uil.film_id=f.film_id) AS `Rented Out`,
(SELECT COUNT(DISTINCT il.inventory_id) FROM inventory_list il WHERE il.film_id=f.film_id)
-(SELECT COUNT(DISTINCT uil.inventory_id) FROM unreturned_inventory_list uil WHERE uil.film_id=f.film_id) AS `In Stock`
FROM
film f
LIMIT 25;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!