Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first trial #301

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
84 changes: 84 additions & 0 deletions .ipynb_checkpoints/README-checkpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)

# LAB | SQL Subqueries

<details>
<summary>
<h2>Learning Goals</h2>
</summary>

This lab allows you to practice and apply the concepts and techniques taught in class.

Upon completion of this lab, you will be able to:

- Use advanced SQL queries (e.g., subqueries, window functions) to perform more complex data manipulations and analysis.

<br>
<hr>

</details>

<details>
<summary>
<h2>Prerequisites</h2>
</summary>

Before this starting this lab, you should have learnt about:

- SELECT, FROM, ORDER BY, LIMIT, WHERE, GROUP BY, and HAVING clauses. DISTINCT, AS keywords.
- Built-in SQL functions such as COUNT, MAX, MIN, AVG, ROUND, DATEDIFF, or DATE_FORMAT.
- JOIN to combine data from multiple tables.
- Subqueries

<br>
<hr>

</details>


## Introduction

Welcome to the SQL Subqueries lab!

In this lab, you will be working with the [Sakila](https://dev.mysql.com/doc/sakila/en/) database on movie rentals. Specifically, you will be practicing how to perform subqueries, which are queries embedded within other queries. Subqueries allow you to retrieve data from one or more tables and use that data in a separate query to retrieve more specific information.

## Challenge

Write SQL queries to perform the following tasks using the Sakila database:

1. Determine the number of copies of the film "Hunchback Impossible" that exist in the inventory system.
2. List all films whose length is longer than the average length of all the films in the Sakila database.
3. Use a subquery to display all actors who appear in the film "Alone Trip".

**Bonus**:

4. Sales have been lagging among young families, and you want to target family movies for a promotion. Identify all movies categorized as family films.
5. Retrieve the name and email of customers from Canada using both subqueries and joins. To use joins, you will need to identify the relevant tables and their primary and foreign keys.
6. Determine which films were starred by the most prolific actor in the Sakila database. A prolific actor is defined as the actor who has acted in the most number of films. First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in.
7. Find the films rented by the most profitable customer in the Sakila database. You can use the customer and payment tables to find the most profitable customer, i.e., the customer who has made the largest sum of payments.
8. Retrieve the client_id and the total_amount_spent of those clients who spent more than the average of the total_amount spent by each client. You can use subqueries to accomplish this.

## Requirements

- Fork this repo
- Clone it to your machine


## Getting Started

Complete the challenges in this readme in a `.sql`file.

## Submission

- Upon completion, run the following commands:

```bash
git add .
git commit -m "Solved lab"
git push origin master
```

- Paste the link of your lab in Student Portal.



54 changes: 54 additions & 0 deletions lab-sql-subqueries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
USE sakila;
-- Challenge: Write SQL queries to perform the following tasks using the Sakila database:
-- 1. Determine the number of copies of the film "Hunchback Impossible" that exist in the inventory system.
SELECT title, film_id, COUNT(inventory_id) AS nr_copies FROM inventory INNER JOIN film USING (film_id)
WHERE film_id=(SELECT film_id FROM film WHERE title="HUNCHBACK IMPOSSIBLE") GROUP BY film_id;

-- 2. List all films whose length is longer than the average length of all the films in the Sakila database.
SELECT title, length,
(SELECT ROUND(AVG(length), 2) FROM film AS avg_length)
FROM film WHERE length > (SELECT AVG(length) FROM film) ORDER BY title;
-- OR:
SELECT title, length FROM film
WHERE length > (SELECT AVG(length) FROM film);

-- 3. Use a subquery to display all actors who appear in the film "Alone Trip".
SELECT film_id, title, concat(first_name, " ", last_name) AS name FROM actor
INNER JOIN film_actor USING (actor_id) INNER JOIN film USING (film_id)
WHERE film_id IN (SELECT film_id FROM film WHERE title="ALONE TRIP") ORDER BY name;

-- Bonus:
-- 4. Sales have been lagging among young families, and you want to target family movies for a promotion.
-- Identify all movies categorized as family films.
SELECT title, name AS category_name FROM film INNER JOIN film_category USING (film_id) INNER JOIN category USING (category_id)
WHERE name = (SELECT name FROM category WHERE name="Family") ORDER BY name;

-- 5. Retrieve the name and email of customers from Canada using both subqueries and joins.
-- To use joins, you will need to identify the relevant tables and their primary and foreign keys.
SELECT country, concat(first_name, " ", last_name) AS name, email FROM customer
INNER JOIN address USING (address_id) INNER JOIN city USING (city_id) INNER JOIN country USING (country_id)
WHERE country= (SELECT country from country WHERE country = "CANADA") ORDER BY name;

-- 6. Determine which films were starred by the most prolific actor in the Sakila database.
-- A prolific actor is defined as the actor who has acted in the most number of films.
-- First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in.

-- 7. Find the films rented by the most profitable customer in the Sakila database.
-- You can use the customer and payment tables to find the most profitable customer,
-- i.e., the customer who has made the largest sum of payments.
SELECT DISTINCT title FROM film WHERE film_id IN
(SELECT film_id FROM inventory WHERE inventory_id IN
(SELECT inventory_id FROM rental WHERE customer_id =
(SELECT customer_id FROM payment
GROUP BY customer_id ORDER BY SUM(amount) LIMIT 1)));

-- 8. Retrieve the client_id and the total_amount_spent of those clients
-- who spent more than the average of the total_amount spent by each client.
-- You can use subqueries to accomplish this.
SELECT customer_id, sum(amount) AS total_amount FROM payment GROUP BY customer_id ORDER BY total_amount;
SELECT avg(total_spent) AS avg_total_spent
FROM (SELECT customer_id, sum(amount) AS total_spent FROM payment GROUP BY customer_id) AS total_spent;
SELECT customer_id, sum(amount) AS total_spent FROM payment GROUP BY customer_id
HAVING total_spent > (SELECT avg(total_spent) AS avg_total_spent
FROM (SELECT customer_id, sum(amount) AS total_spent FROM payment GROUP BY customer_id) AS total_spent) ORDER BY total_spent;