From b570596c4662034f1555da4ca03a2e93231fafa3 Mon Sep 17 00:00:00 2001 From: levgirgin Date: Fri, 15 Aug 2025 16:24:53 +0200 Subject: [PATCH 1/4] Solved Lab --- sql_agg_transform.sql | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 sql_agg_transform.sql diff --git a/sql_agg_transform.sql b/sql_agg_transform.sql new file mode 100644 index 0000000..c4c61e9 --- /dev/null +++ b/sql_agg_transform.sql @@ -0,0 +1,85 @@ +USE sakila; + +-- Determine the shortest and longest movie durations and name the values as max_duration and min_duration + +SELECT MIN(length) as min_duration, + MAX(length) as max_duration +FROM film; + +-- 1.2. Express the average movie duration in hours and minutes. Don't use decimals + +SELECT FLOOR(AVG(length)/60) AS hours, + MOD(ROUND(AVG(length)), 60) AS minutes +FROM film; + +-- 2.1 Calculate the number of days that the company has been operating + +SELECT DATEDIFF(MAX(rental_date), MIN(rental_date)) AS days_operating +FROM rental; + +-- Retrieve rental information and add two additional columns to show the month and weekday of the rental. Return 20 rows of results + +SELECT rental_id, rental_date, inventory_id, customer_id, staff_id, + MONTH(rental_date) AS rental_month, + DAYNAME(rental_date) AS rental_weekday +FROM rental +LIMIT 20; + +-- 2.3 Bonus: Retrieve rental information and add an additional column called DAY_TYPE with values +-- 'weekend' or 'workday', depending on the day of the week + +SELECT rental_id, rental_date, inventory_id, customer_id, staff_id, + DAYNAME(rental_date) AS rental_weekday, + CASE + WHEN DAYOFWEEK(rental_date) IN (1,7) THEN 'weekend' + ELSE 'workday' + END AS DAY_TYPE +FROM rental +LIMIT 20; + +-- retrieve the film titles and their rental duration. If any rental duration value is NULL, replace it with the string 'Not Available'. +-- Sort the results of the film title in ascending order + +SELECT title, IFNULL(rental_duration, 'Not Available') AS rental_duration +FROM film +ORDER BY title ASC; + +-- Bonus + +SELECT CONCAT(first_name, ' ', last_name) AS full_name, + LEFT(email, 3) AS email_prefix +FROM customer +ORDER BY last_name ASC; + +-- 1.1 The total number of films that have been released. +SELECT COUNT(*) AS total_films +FROM film; + +-- 1.2 The number of films for each rating. +SELECT rating, COUNT(*) AS num_films +FROM film +GROUP BY rating; +-- 1.3 The number of films for each rating, sorting the results in descending order of the number of films +SELECT rating, COUNT(*) AS num_films +FROM film +GROUP BY rating +ORDER BY num_films DESC; +-- The mean film duration for each rating, and sort the results in descending order of the mean duration. Round off the average lengths to two decimal places + +SELECT rating, ROUND(AVG(length), 2) AS avg_duration +FROM film +GROUP BY rating +ORDER BY avg_duration DESC; + +-- 2.2 Identify which ratings have a mean duration of over two hours in order to help select films for customers who prefer longer movies + +SELECT rating, ROUND(AVG(length), 2) AS avg_duration +FROM film +GROUP BY rating +HAVING AVG(length) > 120; +-- Bonus: determine which last names are not repeated in the table actor + +SELECT last_name +FROM actor +GROUP BY last_name +HAVING COUNT(*) = 1; \ No newline at end of file From d5065530930871274af4d2e7e8e8797a13469f52 Mon Sep 17 00:00:00 2001 From: levgirgin Date: Fri, 15 Aug 2025 16:36:11 +0200 Subject: [PATCH 2/4] Lab Solved -Edit --- sql_agg_transform.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sql_agg_transform.sql b/sql_agg_transform.sql index c4c61e9..75be763 100644 --- a/sql_agg_transform.sql +++ b/sql_agg_transform.sql @@ -2,8 +2,9 @@ USE sakila; -- Determine the shortest and longest movie durations and name the values as max_duration and min_duration -SELECT MIN(length) as min_duration, - MAX(length) as max_duration +SELECT + MAX(length) AS max_duration, + MIN(length) AS min_duration FROM film; -- 1.2. Express the average movie duration in hours and minutes. Don't use decimals From 5b281556cfd8c68a6978c42e3ff86119f6bd704c Mon Sep 17 00:00:00 2001 From: levgirgin Date: Fri, 15 Aug 2025 16:43:05 +0200 Subject: [PATCH 3/4] Solved Lab - Edit2 --- sql_agg_transform.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql_agg_transform.sql b/sql_agg_transform.sql index 75be763..b6a7282 100644 --- a/sql_agg_transform.sql +++ b/sql_agg_transform.sql @@ -2,9 +2,9 @@ USE sakila; -- Determine the shortest and longest movie durations and name the values as max_duration and min_duration -SELECT - MAX(length) AS max_duration, - MIN(length) AS min_duration +SELECT + MAX(length) AS Longest_Movie_Duration, + MIN(length) AS Shortest_Movie_Duration FROM film; -- 1.2. Express the average movie duration in hours and minutes. Don't use decimals From 92990716909a1806142649290ecd551648c02249 Mon Sep 17 00:00:00 2001 From: levgirgin Date: Fri, 15 Aug 2025 16:49:52 +0200 Subject: [PATCH 4/4] Solved Lab- 3rd --- sql_agg_transform.sql => sql_agg_transform_new.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename sql_agg_transform.sql => sql_agg_transform_new.sql (96%) diff --git a/sql_agg_transform.sql b/sql_agg_transform_new.sql similarity index 96% rename from sql_agg_transform.sql rename to sql_agg_transform_new.sql index b6a7282..75be763 100644 --- a/sql_agg_transform.sql +++ b/sql_agg_transform_new.sql @@ -2,9 +2,9 @@ USE sakila; -- Determine the shortest and longest movie durations and name the values as max_duration and min_duration -SELECT - MAX(length) AS Longest_Movie_Duration, - MIN(length) AS Shortest_Movie_Duration +SELECT + MAX(length) AS max_duration, + MIN(length) AS min_duration FROM film; -- 1.2. Express the average movie duration in hours and minutes. Don't use decimals