diff --git a/lab-aggregation.sql b/lab-aggregation.sql new file mode 100644 index 0000000..cf1fd91 --- /dev/null +++ b/lab-aggregation.sql @@ -0,0 +1,81 @@ +-- CHALLENGE 1 +-- 1.You need to use SQL built-in functions to gain insights relating to the duration of movies: +-- 1.1 Determine the shortest and longest movie durations and name the values as max_duration and min_duration. +USE sakila; + +SELECT MIN(length) AS min_duration, MAX(length) AS max_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) / 60) AS avg_hours, + ROUND(AVG(length) % 60) AS avg_minutes +FROM sakila.film; + +-- 2. You need to gain insights related to rental dates: +-- 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 MIN(rental_date) AS first_rental, + MAX(rental_date) AS last_rental, + DATEDIFF(MAX(rental_date), MIN(rental_date)) AS days_operating +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 *, + MONTH(rental_date) AS 'month', + DAYNAME(rental_date) AS 'day' +FROM sakila.rental; + +-- 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 *, + MONTH(rental_date) AS 'month', + DAYNAME(rental_date) AS 'day', + CASE + WHEN DAYNAME(rental_date) IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday') THEN 'workday' + WHEN DAYNAME(rental_date) IN ('Saturday', 'Sunday') THEN 'weekend' + ELSE 'Not found' +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, + IFNULL(rental_duration, 'Not Available') AS rental_duration +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. + + +-- CHALLENGE 2 +-- 1. 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(film_id) FROM sakila.film +WHERE release_year IS NOT NULL; + +-- 1.2 The number of films for each rating. +SELECT COUNT(film_id), 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. This will help you to better understand the popularity of different film ratings and adjust purchasing decisions accordingly. +SELECT COUNT(film_id), rating FROM sakila.film +GROUP BY rating +ORDER BY COUNT(film_id) DESC; + +-- 2. 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 * FROM sakila.film; + +SELECT AVG(length), rating FROM sakila.film +GROUP BY rating +ORDER BY AVG(length) 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 AVG(length), rating FROM sakila.film +GROUP BY rating +HAVING AVG(length) > 120 +ORDER BY AVG(length) DESC; + +-- 3. Bonus: determine which last names are not repeated in the table actor. +SELECT DISTINCT(last_name) AS not_repeated FROM sakila.actor; \ No newline at end of file diff --git a/lab-sql-basic-queries.sql b/lab-sql-basic-queries.sql new file mode 100644 index 0000000..c2d638e --- /dev/null +++ b/lab-sql-basic-queries.sql @@ -0,0 +1,62 @@ +USE sakila; + +-- 1. Display all available tables in the Sakila database. + +SHOW TABLES; + +-- 2. Retrieve all the data from the tables actor, film and customer. + +SELECT * FROM sakila.actor; +SELECT * FROM sakila.film; +SELECT * FROM sakila.customer; + +-- 3. Retrieve the following columns from their respective tables: +-- 3.1 Titles of all films from the film table +SELECT title FROM sakila.film; + +-- 3.2 List of languages used in films, with the column aliased as language from the language table +SELECT name AS 'language' FROM sakila.language; + +-- 3.3 List of first names of all employees from the staff table +SELECT first_name FROM sakila.staff; + +-- 4. Retrieve unique release years. +SELECT DISTINCT release_year FROM sakila.film; + +-- 5. Counting records for database insights: +-- 5.1 Determine the number of stores that the company has. +SELECT COUNT(store_id) FROM sakila.store; + +-- 5.2 Determine the number of employees that the company has. +SELECT COUNT(staff_id) FROM sakila.staff; + +-- 5.3 Determine how many films are available for rent and how many have been rented. +SELECT COUNT(*) AS films_available_for_rent FROM sakila.inventory; +SELECT COUNT(DISTINCT inventory_id) AS films_rented FROM sakila.rental; + +-- 5.4 Determine the number of distinct last names of the actors in the database. +SELECT COUNT(DISTINCT last_name) FROM sakila.actor; + +-- 6. Retrieve the 10 longest films. +SELECT * FROM sakila.film +ORDER BY length DESC +LIMIT 10; + +-- 7. Use filtering techniques in order to: +-- 7.1 Retrieve all actors with the first name "SCARLETT". +SELECT * FROM sakila.actor +WHERE first_name='SCARLETT'; + +-- BONUS: +-- 7.2 Retrieve all movies that have ARMAGEDDON in their title and have a duration longer than 100 minutes. +SELECT * FROM sakila.film +WHERE title LIKE '%ARMAGEDDON%' AND length > 100; + +-- 7.3 Determine the number of films that include Behind the Scenes content +SELECT * FROM sakila.film +WHERE special_features LIKE '%Behind the Scenes%'; + + + + +