Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions sql-aggregation-and-transformation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
USE sakila;

-- 1.1 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 FROM sakila.film;

-- 1.2. Express the average movie duration in hours and minutes. Don't use decimals.
-- Hint: Look for floor and round functions.
SELECT FLOOR(AVG(length)) AS average_duration FROM sakila.film;

-- 2.1 Calculate the number of days that the company has been operating.
-- Hint: To do this, use the rental table, and the DATEDIFF() function to subtract the earliest date in the rental_date column from the latest date.
SELECT DATEDIFF(MAX(rental_date), MIN(rental_date)) 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 *, MONTHNAME(rental_date), DAYNAME(rental_date) 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.
-- Hint: use a conditional expression.
SELECT *, DAYNAME(rental_date) AS Day_of_Week,
CASE
WHEN DAYNAME(rental_date) = "Monday" THEN "Weekday"
WHEN DAYNAME(rental_date) = "Tuesday" THEN "Weekday"
WHEN DAYNAME(rental_date) = "Wednesday" THEN "Weekday"
WHEN DAYNAME(rental_date) = "Thursday" THEN "Weekday"
WHEN DAYNAME(rental_date) = "Friday" THEN "Weekday"
ELSE "Weekend"
END AS DAY_TYPE
FROM sakila.rental;

-- 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.
-- Please note that even if there are currently no null values in the rental duration column, the query should still be written to handle such cases in the future.
-- Hint: Look for the IFNULL() function.

SELECT title, length,
CASE
WHEN ISNULL(rental_duration) THEN "Not Available"
ELSE "Available"
END AS Information
FROM sakila.film
ORDER BY title ASC;

-- 4. Bonus: The marketing team for the movie rental company now needs to create a personalized email campaign for customers.
-- To achieve this, 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 "concat", LEFT(email,3) FROM sakila.customer
ORDER BY last_name ASC;

-- Challenge 2

-- Next, you need to analyze the films in the collection to gain some more insights. Using the film table, determine:
-- 1.1 The total number of films that have been released.
SELECT count(distinct film_id) as number_of_films FROM sakila.film;

-- 1.2 The number of films for each rating.
SELECT rating, COUNT(*) AS film_count
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.
-- This will help you to better understand the popularity of different film ratings and adjust purchasing decisions accordingly.alter
SELECT rating, COUNT(*) AS film_count
FROM sakila.film
GROUP BY rating
ORDER BY film_count DESC;

-- Using the film table, determine:
-- 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 film_duration FROM sakila.film
GROUP BY rating
ORDER BY film_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 film_duration FROM sakila.film
GROUP BY rating
HAVING ROUND(AVG(length),2) > 120
ORDER BY film_duration DESC;

-- Bonus: determine which last names are not repeated in the table actor.
SELECT distinct last_name FROM sakila.actor;