Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Imagine you work at a movie rental company as an analyst. By using SQL in the ch

## Challenge 2

1. Next, you need to analyze the films in the collection to gain some more insights. Using the `film` table, determine:
1. Next, you need to analyze the films in the collection to gain so me more insights. Using the `film` table, determine:
- 1.1 The **total number of films** that have been released.
- 1.2 The **number of films for each rating**.
- 1.3 The **number of films for each rating, sorting** the results in descending order of the number of films.
Expand Down
92 changes: 92 additions & 0 deletions SQL-Data-Aggregation-and-Transformation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
USE sakila;

-- Challenge 1

-- 1.1
SELECT
MIN(length) AS min_duration,
MAX(length) AS max_duration
FROM film;

-- 1.2
SELECT
FLOOR(AVG(length) / 60) AS avg_hours,
FLOOR(AVG(length)% 60 ) AS avg_minutes
FROM film;

-- 2.1
SELECT
DATEDIFF(MAX(rental_date), MIN(rental_date)) AS days_operating
FROM rental;


-- 2.2
Select
rental_id,
rental_date,
MONTHNAME(rental_date) AS rental_mounth,
DAYNAME(rental_date) AS rental_weekday
FROM rental
LIMIT 20;

-- 2.3
SELECT
rental_id,
rental_date,
DAYNAME(rental_date),

CASE
WHEN DAYNAME(rental_date) IN ('Saturdat', 'Sunday') THEN 'Weekend'
ELSE 'Workday'
END AS day_type
FROM rental
LIMIT 20;

-- 3
SELECT
title,
IFNULL(rental_duration, 'NOT AVAILABLE') as rental_duration
FROM film
ORDER BY title ASC;


-- Challenge 2

-- 1.1
SELECT COUNT(*) AS total_films
FROM film;

-- 1.2
SELECT rating, COUNT(*) AS film_count
FROM film
GROUP BY rating;

-- 1.3
SELECT rating, COUNT(*) AS film_count
FROM film
GROUP BY rating
ORDER BY film_count DESC;

-- 2.1
SELECT
rating,
ROUND(AVG(length), 2) AS avg_duration
FROM film
GROUP BY rating
ORDER BY avg_duration DESC;

-- 2.2
SELECT
rating,
ROUND(AVG(length),2) AS avg_duration
FROM film
GROUP BY rating
HAVING avg_duration > 120;