From 14342a4bdfc863bd308df8299ad5a8c0973296d3 Mon Sep 17 00:00:00 2001 From: Anne Leschallier de Lisle Date: Sat, 7 Mar 2026 18:53:55 +0100 Subject: [PATCH 1/2] Solved lab --- sql_joins_myfile.sql | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sql_joins_myfile.sql diff --git a/sql_joins_myfile.sql b/sql_joins_myfile.sql new file mode 100644 index 0000000..1bc1c5b --- /dev/null +++ b/sql_joins_myfile.sql @@ -0,0 +1,42 @@ +-- 1. List the number of films per category. + +SELECT c.category_id as cat, COUNT(c.film_id) as number_of_films FROM sakila.film AS f +JOIN sakila.film_category AS c +ON c.film_id = f.film_id +GROUP BY cat; + + +-- 2. Retrieve the store ID, city, and country for each store. + +SELECT +s.store_id as store, +c.city_id as city, +co.country_id as country +FROM sakila.store AS s +JOIN sakila.address as a +ON s.address_id = a.address_id +JOIN sakila.city AS c +ON a.city_id = c.city_id +JOIN sakila.country AS co +ON c.country_id = co.country_id; + + + +-- 3. Calculate the total revenue generated by each store in dollars. + +SELECT +store_id as store, sum(p.amount) as total_revenue +FROM payment as p +JOIN customer as cust +ON p.customer_id = cust.customer_id +GROUP BY store_id; + + + +-- 4. Determine the average running time of films for each category. + + +SELECT c.category_id as cat, avg(f.length) as running_time FROM sakila.film AS f +JOIN sakila.film_category AS c +ON c.film_id = f.film_id +GROUP BY cat; \ No newline at end of file From bdff01d935f23e18906a26c2028530bee35cbd8f Mon Sep 17 00:00:00 2001 From: Anne Leschallier de Lisle Date: Sat, 7 Mar 2026 18:59:46 +0100 Subject: [PATCH 2/2] Solved lab --- sql_joins_myfile.sql | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/sql_joins_myfile.sql b/sql_joins_myfile.sql index 1bc1c5b..2825056 100644 --- a/sql_joins_myfile.sql +++ b/sql_joins_myfile.sql @@ -24,19 +24,19 @@ ON c.country_id = co.country_id; -- 3. Calculate the total revenue generated by each store in dollars. -SELECT -store_id as store, sum(p.amount) as total_revenue -FROM payment as p -JOIN customer as cust -ON p.customer_id = cust.customer_id -GROUP BY store_id; +SELECT store.store_id, SUM(payment.amount) AS total_revenue +FROM store +JOIN staff ON store.store_id = staff.store_id +JOIN payment ON staff.staff_id = payment.staff_id +GROUP BY store.store_id; -- 4. Determine the average running time of films for each category. -SELECT c.category_id as cat, avg(f.length) as running_time FROM sakila.film AS f -JOIN sakila.film_category AS c -ON c.film_id = f.film_id -GROUP BY cat; \ No newline at end of file +SELECT category.name, ROUND(AVG(film.length), 2) AS average_running_time +FROM category +JOIN film_category ON category.category_id = film_category.category_id +JOIN film ON film_category.film_id = film.film_id +GROUP BY category.name; \ No newline at end of file