-
Notifications
You must be signed in to change notification settings - Fork 878
Expand file tree
/
Copy pathlab_sql_basic_queries.sql
More file actions
148 lines (104 loc) · 3.29 KB
/
lab_sql_basic_queries.sql
File metadata and controls
148 lines (104 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- ==========================================================
-- Lab · SQL Basic Queries (Sakila)
-- Objetivo: rellenar cada apartado con tu query (sin resolver aquí)
-- ==========================================================
USE sakila;
-- ==========================================================
-- 1) Display all available tables in the Sakila database
-- ==========================================================
-- TODO: escribe aquí tu query:
SHOW TABLES;
-- ==========================================================
-- 2) Retrieve all the data from the tables actor, film and customer
-- ==========================================================
-- TODO: actor
SELECT *
FROM actor;
-- TODO: film
SELECT *
FROM film;
-- TODO: customer
SELECT *
FROM customer;
-- ==========================================================
-- 3) Retrieve specific columns (selección de columnas)
-- ==========================================================
-- 3.1 Titles of all films from the film table
-- TODO:
SELECT
title
FROM film;
-- 3.2 List of languages used in films, aliased as language (from language table)
-- TODO:
SELECT *
FROM language;
-- 3.3 List of first names of all employees from the staff table
-- TODO:
SELECT first_name
FROM staff;
-- ==========================================================
-- 4) Retrieve unique release years
-- ==========================================================
-- TODO:
SELECT
DISTINCT release_year
FROM film;
-- ==========================================================
-- 5) COUNT y DISTINCT (cuántos valores / cuántos únicos)
-- ==========================================================
-- 5.1 Determine the number of stores that the company has
-- TODO:
SELECT COUNT(store_id) as number_of_stores
FROM store;
-- 5.2 Determine the number of employees that the company has
-- TODO:
SELECT COUNT(staff_id) as number_of_employees
FROM staff;
-- 5.3 Determine how many films are available for rent and how many have been rented
-- TODO:
SELECT
COUNT(DISTINCT film_id) as films_available_for_rent
FROM inventory;
SELECT
COUNT(DISTINCT inventory_id) as copies_rented
FROM rental;
-- 5.4 Determine the number of distinct last names of the actors in the database
-- TODO:
SELECT
COUNT(DISTINCT last_name) as number_of_distinct_last_names
FROM actor;
-- ==========================================================
-- 6) ORDER BY y LIMIT (top-N): Retrieve the 10 longest films
-- ==========================================================
-- TODO:
SELECT
title,
length
FROM film
ORDER BY length DESC
LIMIT 10;
-- ==========================================================
-- 7) WHERE (filtrar filas) · LIKE / IN / AND / OR
-- ==========================================================
-- 7.1 Retrieve all actors with the first name "SCARLETT"
-- TODO:
SELECT
first_name,
last_name
FROM actor
WHERE first_name IN ('SCARLETT');
-- BONUS 7.2 Retrieve all movies with ARMAGEDDON in the title and duration > 100
-- Hint: LIKE
-- TODO:
SELECT
title,
length
FROM film
WHERE title LIKE '%armageddon%' AND length > 100
ORDER BY length;
-- BONUS 7.3 Determine the number of films that include Behind the Scenes content
-- TODO:
SELECT
COUNT(DISTINCT film_id) as num_films_with_behind_the_scenes
FROM film
WHERE special_features LIKE '%behind the scenes%';