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
103 changes: 103 additions & 0 deletions lab-sql-basic-queries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
![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
```

- Please only include a .sql file with your solution, not the sakila files
- Paste the link of your lab in Student Portal.
46 changes: 46 additions & 0 deletions lab-sql-basic-queries/lab_bsic_queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
USE sakila;
SHOW TABLES;

SELECT * FROM actor;
SELECT * FROM film;
SELECT * FROM customer;

SELECT title FROM film;

SELECT name AS language FROM language;

SELECT first_name FROM staff;

SELECT DISTINCT release_year FROM film;

SELECT COUNT(*) AS employee_count FROM store;
SELECT COUNT(*) AS employee_count FROM staff;

SELECT COUNT(*) AS availables_films FROM inventory;
SELECT COUNT(DISTINCT inventory_id) AS rented_films FROM rental;

SELECT COUNT(DISTINCT last_name) AS distinct_last_names FROM actor;

SELECT title, length
FROM film
ORDER BY length DESC
LIMIT 10;

SELECT *FROM actor
WHERE first_name = 'SCARLETT';

SELECT title, length
FROM film
WHERE title LIKE '%ARMAGEDDON%'
AND length > 100;

SELECT COUNT(*) AS films_with_bts
FROM film
WHERE special_features LIKE '%Behind the Scenes%';