From c4ef217121058a4670d781c7b63d41be0df2d8cf Mon Sep 17 00:00:00 2001 From: VeliaAlam Date: Thu, 14 Aug 2025 10:11:23 +0200 Subject: [PATCH] Solved lab --- Data_agg-transformation.sql | 115 ++++++++++++++++++++++++++++++++++++ README.md | 2 +- 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 Data_agg-transformation.sql diff --git a/Data_agg-transformation.sql b/Data_agg-transformation.sql new file mode 100644 index 0000000..fb621dd --- /dev/null +++ b/Data_agg-transformation.sql @@ -0,0 +1,115 @@ +-- 1.1 Determine the **shortest and longest movie durations** and name the values as `max_duration` and `min_duration`. +SELECT * FROM sakila.film; + +SELECT title, length AS min_duration +FROM sakila.film +WHERE length = (SELECT MIN(length) FROM sakila.film); + +SELECT title, length AS max_duration +FROM sakila.film +WHERE length = (SELECT MAX(length) FROM sakila.film); + +-- 1.2. Express the **average movie duration in hours and minutes**. Don't use decimals. +SELECT +FLOOR(AVG(length) / 60) AS hours, +FLOOR(MOD(AVG(length), 60)) AS minutes +FROM sakila.film; + +-- 2.1 Calculate the **number of days that the company has been operating**. +SELECT + DATEDIFF(MAX(rental_date), MIN(rental_date)) AS operating_days +FROM sakila.rental; + +-- 2.2 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, + MONTHNAME(rental_date) AS rental_month, + DAYNAME(rental_date) AS rental_weekday, + inventory_id, + customer_id, + return_date, + staff_id +FROM sakila.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, + MONTHNAME(rental_date) AS rental_month, + DAYNAME(rental_date) AS rental_weekday, + CASE + WHEN DAYOFWEEK(rental_date) IN (1,7) THEN 'weekend' -- 1 = Sunday, 7 = Saturday + ELSE 'workday' + END AS DAY_TYPE, + inventory_id, + customer_id, + return_date, + staff_id +FROM sakila.rental +LIMIT 20; + +-- 3. You need to ensure that customers can easily access information about the movie collection. +-- To achieve this, 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(length, 'Not Available') AS rental_duration +FROM sakila.film +ORDER BY title ASC; + +-- 4. You need to retrieve the **concatenated first and last names of customers**, along with the +-- **first 3 characters of their email** address, so that you can address them by their first name +-- and use their email address to send personalized recommendations. The results should be ordered +-- by last name in ascending order to make it easier to use the data.* + +SELECT + CONCAT(first_name, ' ', last_name) AS full_name, + LEFT(email, 3) AS email_prefix +FROM sakila.customer +ORDER BY last_name ASC; + +-- Challenge 2 +-- 1.1 The **total number of films** that have been released. +SELECT COUNT(*) AS total_films +FROM sakila.film; + +-- 1.2 The **number of films for each rating**. +SELECT rating, COUNT(*) AS films_per_rating +FROM sakila.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 films_per_rating +FROM sakila.film +GROUP BY rating +ORDER BY films_per_rating DESC; + +-- 2.1 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. This will help identify popular movie lengths for each category. +SELECT + rating, + ROUND(AVG(length), 2) AS avg_duration +FROM sakila.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 sakila.film +GROUP BY rating +HAVING AVG(length) > 120; + +-- 3. *Bonus: determine which last names are not repeated in the table `actor`.* +SELECT last_name +FROM sakila.actor +GROUP BY last_name +HAVING COUNT(*) = 1; + + + + + diff --git a/README.md b/README.md index 61abd5c..dc5b8a9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) +r![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) # Lab | SQL Data Aggregation and Transformation