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

solved #322

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

# LAB | SQL Basic Queries

<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 SQL queries to extract insights from the data and answer research questions or problem statements, using techniques such as selecting columns with SELECT clause, filtering with WHERE clause, sorting data with ORDER BY clause, limiting results with LIMIT, using DISTINCT to retrieve unique values, and counting records with COUNT.


<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 clauses.
- DISTINCT keyword to return only unique values, AS keyword for using aliases, COUNT function for counting records.
<br>
<hr>

</details>


## Introduction

Welcome to the SQL Basic Queries lab!

In this lab, you will practice how to use SQL queries to extract insights from the [Sakila](https://dev.mysql.com/doc/sakila/en/) database which contains information about movie rentals.

In this lab, you'll practice the basics of SQL, including how to select data from tables, filter results using the WHERE clause, aggregate data with functions like COUNT, and sort results using the ORDER BY clause.

Throughout the lab, you will work with two SQL query files: `sakila-schema.sql`, which creates the database schema, and `sakila-data.sql`, which inserts the data into the database. You can download the necessary files locally by following the steps listed in [Sakila sample database - installation](https://dev.mysql.com/doc/sakila/en/sakila-installation.html).

You can also refer to the Entity Relationship Diagram (ERD) of the database to guide your analysis:

<br>

![DB schema](https://education-team-2020.s3-eu-west-1.amazonaws.com/data-analytics/database-sakila-schema.png)

<br><br>


## Challenge
Use the sakila database to do the following tasks:

1. Display all available tables in the Sakila database.
2. Retrieve all the data from the tables `actor`, `film` and `customer`.
3. Retrieve the following columns from their respective tables:
- 3.1 Titles of all films from the `film` table
- 3.2 List of languages used in films, with the column aliased as `language` from the `language` table
- 3.3 List of first names of all employees from the `staff` table

4. Retrieve unique release years.
5. Counting records for database insights:
- 5.1 Determine the number of stores that the company has.
- 5.2 Determine the number of employees that the company has.
- 5.3 Determine how many films are available for rent and how many have been rented.
- 5.4 Determine the number of distinct last names of the actors in the database.
6. Retrieve the 10 longest films.
7. Use filtering techniques in order to:
- 7.1 Retrieve all actors with the first name "SCARLETT".

BONUS:
- 7.2 Retrieve all movies that have ARMAGEDDON in their title and have a duration longer than 100 minutes.

- *Hint: use `LIKE` operator. [More information here.](https://www.w3schools.com/sql/sql_like.asp)*
- 7.3 Determine the number of films that include Behind the Scenes content

## 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.
30 changes: 30 additions & 0 deletions basic_queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- 1. Display all available tables
SHOW TABLES IN sakila;

-- 2. Retrieve all data from actor, film, and customer
SELECT * FROM actor;
SELECT * FROM film;
SELECT * FROM customer;

-- 3. Retrieve specific columns
SELECT title FROM film;
SELECT name AS language FROM language;
SELECT first_name FROM staff;

-- 4. Retrieve unique release years
SELECT DISTINCT release_year FROM film;

-- 5. Counting records
SELECT COUNT(*) AS number_of_stores FROM store;
SELECT COUNT(*) AS number_of_employees FROM staff;
SELECT COUNT(*) AS films_available_for_rent FROM inventory WHERE inventory_id NOT IN (SELECT inventory_id FROM rental WHERE return_date IS NOT NULL);
SELECT COUNT(*) AS films_rented FROM rental WHERE return_date IS NOT NULL;
SELECT COUNT(DISTINCT last_name) AS distinct_actor_last_names FROM actor;

-- 6. Retrieve the 10 longest films
SELECT title, length FROM film ORDER BY length DESC LIMIT 10;

-- 7. Use filtering techniques
SELECT * FROM actor WHERE first_name = 'SCARLETT';
SELECT title, length FROM film WHERE title LIKE '%ARMAGEDDON%' AND length > 100;
SELECT COUNT(*) AS films_with_behind_the_scenes FROM film WHERE description LIKE '%Behind the Scenes%';