Skip to content

jecastrom/lab-sql-8

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab SQL Queries 8

40541063 a07a0a8a 601a 11e8 91b5 2f13e4e6b441

Jorge Castro DAPT NOV2021


In this lab, you will be using the Sakila database of movie rentals. You have been using this database for a couple labs already, but if you need to get the data again, refer to the official installation link.

The database is structured as follows:

147394200 2fdeec2e a41a 4094 b544 dede2b263d96

Instructions:

1. Rank films by length

  • (filter out the rows that have nulls or 0s in length column). In your output, only select the columns title, length, and the rank.

Answer:

SELECT
    title,
    `length`,
    rank() over(
        ORDER BY
            `length` DESC
    ) AS ranking
FROM
    film
WHERE
    `length` != 0
    OR NOT NULL;
148272842 f1628185 280c 4b58 93a4 f6b3d52215c8

2. Rank films by length within the rating category

  • (filter out the rows that have nulls or 0s in length column). In your output, only select the columns title, length, rating and the rank.

Answer:

SELECT
    title,
    `length`,
    rating,
    dense_rank() over(
        ORDER BY
            `length` DESC
    ) AS ranking
FROM
    film
WHERE
    `length` NOT IN (0, '', ' ')
ORDER BY
    3;
148276964 2558ed20 fa66 4695 b49b d0fda6a6f017

3. How many films are there for each of the categories in the category table?

  • Use appropriate join to write this query

Answer:

SELECT
    a.`name` AS film_category,
    count(b.film_id) AS number_of_films
FROM
    category a
    INNER JOIN film_category b ON a.category_id = b.category_id
GROUP BY
    1
ORDER BY
    2 DESC;
148281739 01319743 ad93 4309 954e 7f22ca948a44

4. Which actor has appeared in the most films?

Answer:

SELECT
    concat((a.first_name), ' ', (a.last_name)) actor_name,
    count(b.film_id) AS actors_appearances
FROM
    actor a
    INNER JOIN film_actor b ON a.actor_id = b.actor_id
GROUP BY
    1
ORDER BY
    2 DESC
LIMIT
    1;
148309559 08d56cf9 7831 43ad 8fc6 efcbf4a518dc

5. Most active customer

  • (the customer that has rented the most number of films)

Answer:

SELECT
    concat((a.first_name), ' ', (a.last_name)) customer_name,
    count(b.rental_id) AS rented_films
FROM
    customer a
    INNER JOIN rental b ON a.customer_id = b.customer_id
GROUP BY
    1
ORDER BY
    2 DESC
LIMIT
    1;
148309349 a82cb5bb 9fa3 4960 9906 aee12f7104f4

6. Find which is the most rented film

  • The answer is Bucket Brotherhood This query might require using more than one join statement. Give it a try. We will talk about queries with multiple join statements later in the lessons.

Answer:

SELECT
    b.title,
    count(c.rental_id) AS number_of_rents
FROM
    inventory a
    INNER JOIN film b ON a.film_id = b.film_id
    INNER JOIN rental c ON a.inventory_id = c.inventory_id
GROUP BY
    1
ORDER BY
    2 DESC
LIMIT
    1;
148315353 95fde644 9d28 4602 8a0e 833810167b83
💡

To do any join, I find it helpful to think of it as a 3 step process:

  • Step 1: To think or draw the actual table we require as an outcome. In my case, I draw them if they join several tables.

Then build the SELECT statement: This is where we define the actual layout of the table, the order in which the columns are in the table from left to right and using the aggregations and functions if need be.

  • Step 2 is to map how we will get from table A to table B, C, or D. To have a logical path that links the tables. Here we see which primary keys and foreign keys will help us do so.

  • Step 3: We filter and sort the information we select with the WHERE, Group By, Having and Order by clauses. Note that in some cases, the query that we have just built becomes a sub-query, so we can refine the outcome to fit what we ask.

148324695 4b6b9200 1b85 41fa 88bf 54599179d5ee

sakila%20join
1. A triple inner join. Notice how the tables are connected so we could build our join. The film table is connected to the inventory table, then from another key it links to the rental table. This interactive EDR is a very useful feature of MySQL Workbench.

148328643 18876341 5bb3 4c88 98bd 504e39535265
2. Translating the desired outcome into building the query.



Releases

No releases published

Packages

No packages published