From 372ad450cfdd856dedd202f00798fa0ffe909781 Mon Sep 17 00:00:00 2001 From: Beatriz Veiga Date: Sat, 21 Dec 2024 18:11:07 +0000 Subject: [PATCH 1/2] SQL Subqueries Lab --- lab-sql-subqueries.sql | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lab-sql-subqueries.sql diff --git a/lab-sql-subqueries.sql b/lab-sql-subqueries.sql new file mode 100644 index 0000000..5bb70db --- /dev/null +++ b/lab-sql-subqueries.sql @@ -0,0 +1,58 @@ +-- Write SQL queries to perform the following tasks using the Sakila database: +USE sakila; + +-- 1. Determine the number of copies of the film "Hunchback Impossible" that exist in the inventory system. +SELECT + COUNT(title) +FROM + inventory AS i + JOIN + film AS f ON f.film_id = i.film_id +WHERE + title = 'Hunchback Impossible'; + +-- 2. List all films whose length is longer than the average length of all the films in the Sakila database. +SELECT * FROM film +WHERE length > (SELECT AVG(length) FROM film); + +-- 3. Use a subquery to display all actors who appear in the film "Alone Trip". +SELECT * FROM actor; +SELECT * FROM film_actor; + +SELECT + a.first_name, a.last_name, f.title +FROM + actor AS a + JOIN + film_actor AS fa ON a.actor_id = fa.actor_id + JOIN + film AS f ON fa.film_id = f.film_id +WHERE + f.film_id IN (SELECT + film_id + FROM + film + WHERE + title = 'Alone Trip'); + +-- 4. Sales have been lagging among young families, and you want to target family movies for a promotion. Identify all movies categorized as family films. + +SELECT + f.title, c.name +FROM + film AS f + JOIN + film_category AS fc ON f.film_id = fc.film_id + JOIN + category AS c ON c.category_id = fc.category_id +WHERE + name = 'Family'; + +-- 5. Retrieve the name and email of customers from Canada using both subqueries and joins. To use joins, you will need to identify the relevant tables and their primary and foreign keys. + +SELECT CONCAT(c.first_name, ' ', c.last_name) AS full_name, c.email +FROM customer AS c JOIN address AS a ON c.address_id = a.address_id +JOIN city AS ci ON a.city_id = ci.city_id +WHERE ci.country_id IN (SELECT co.country_id FROM country AS co WHERE co.country = 'Canada'); + + From 20a21161d8c357c7ffbecbe0d202b7524045fc71 Mon Sep 17 00:00:00 2001 From: Beatriz Veiga Date: Sat, 21 Dec 2024 18:13:15 +0000 Subject: [PATCH 2/2] SQL Subqueries Lab --- lab-sql-subqueries.sql | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lab-sql-subqueries.sql b/lab-sql-subqueries.sql index 5bb70db..57539e8 100644 --- a/lab-sql-subqueries.sql +++ b/lab-sql-subqueries.sql @@ -50,9 +50,36 @@ WHERE -- 5. Retrieve the name and email of customers from Canada using both subqueries and joins. To use joins, you will need to identify the relevant tables and their primary and foreign keys. -SELECT CONCAT(c.first_name, ' ', c.last_name) AS full_name, c.email -FROM customer AS c JOIN address AS a ON c.address_id = a.address_id -JOIN city AS ci ON a.city_id = ci.city_id -WHERE ci.country_id IN (SELECT co.country_id FROM country AS co WHERE co.country = 'Canada'); +SELECT + CONCAT(c.first_name, ' ', c.last_name) AS full_name, c.email +FROM + customer AS c + JOIN + address AS a ON c.address_id = a.address_id + JOIN + city AS ci ON a.city_id = ci.city_id +WHERE + ci.country_id IN (SELECT + co.country_id + FROM + country AS co + WHERE + co.country = 'Canada'); + +-- 6. Determine which films were starred by the most prolific actor in the Sakila database. A prolific actor is defined as the actor who has acted in the most number of films. First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in. +SELECT + f.title +FROM + film AS f + JOIN + film_actor AS fa ON f.film_id = fa.film_id +WHERE + fa.actor_id = (SELECT + actor_id + FROM + film_actor + GROUP BY actor_id + ORDER BY COUNT(*) DESC + LIMIT 1);