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
17 changes: 17 additions & 0 deletions HOW TO SQL V2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- ===============================================================================================================
-- HOW TO --------------------------------------------------------------------------------------------------------
-- How to display all availables tables in a database
USE database_name;
SHOW TABLES;
-- ---------------------------------------------------------------------------------------------------------------
-- How to display all availables data in a table
USE database_name;
SELECT * FROM table_name;
-- ---------------------------------------------------------------------------------------------------------------
-- How to
DESCRIBE table_name;
SHOW COLUMNS FROM table_name;
-- ===============================================================================================================
-- QUESTIONS!

-- ===============================================================================================================
38 changes: 38 additions & 0 deletions HOW TO SQL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- ===============================================================================================================
-- HOW TO --------------------------------------------------------------------------------------------------------
-- How to drop a table from a database
DROP TABLE `table_name`;
-- ---------------------------------------------------------------------------------------------------------------
-- How to alter a table and drop a foreign key
ALTER TABLE `table_name`
DROP FOREIGN KEY `foreign_key_name`;
-- ---------------------------------------------------------------------------------------------------------------

-- How to add constraints (Foreign keys to a table)
-- only add to the one that has a many or depends on it
-- Alter the table with constraints (relationships)
ALTER TABLE `table_name`
ADD CONSTRAINT `constraint_name` FOREIGN KEY (`foreign key name`) REFERENCES source_table(`foreign key name`),
ADD CONSTRAINT `constraint_name` FOREIGN KEY (`foreign key name`) REFERENCES source_table(`foreign key name`),
ADD CONSTRAINT `constraint_name` FOREIGN KEY (`foreign key name`) REFERENCES source_table(`foreign key name`);
-- ---------------------------------------------------------------------------------------------------------------
-- How to seed data into a table
-- download the table you want to seed from as a .csv file
-- import it into your database
-- take data columns from it to the column of the table u wanna seed it in
-- INSERT DATA INTO TABLE INVOICES from different tables
INSERT INTO TABLE ('column_1', 'column_2', 'column_3')
SELECT 'column_1' FROM 'source_1,
SELECT 'column_2' FROM 'source_2,
SELECT 'column_3' FROM 'source_3,
GROUP BY
'column_1',
'column_2',
'column_3';
-- ===============================================================================================================
-- QUESTIONS!
-- When to use VARCHAR or CHAR?
-- What's the difference between CHAR, VARCHAR, TEXT?
-- Storage engine must support FK? What does it mean exactly?
SHOW TABLE STATUS WHERE Name = 'table_name';
-- ===============================================================================================================
84 changes: 84 additions & 0 deletions challenge solved.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
-- ===============================================================================================================
-- 1. Display all available tables in the Sakila database.
USE sakila;
SHOW TABLES;
-- ===============================================================================================================
-- 2. Retrieve all the data from the tables `actor`, `film` and `customer`.
SELECT * FROM actor;
SELECT * FROM film; -- original_language_id is null?
SELECT * FROM customer;
-- ===============================================================================================================
-- 3. Retrieve the following columns from their respective tables:
--
-- 3.1 Titles of all films from the `film` table
SELECT title FROM film;
--
-- 3.2 List of languages used in films, with the column aliased as `language` from the `language` table
SELECT language_id AS language FROM film;
--
-- 3.3 List of first names of all employees from the `staff` table
SELECT first_name AS first_name FROM staff;

-- ===============================================================================================================
-- 4. Retrieve unique release years.
SELECT DISTINCT release_year FROM film; -- same as python df[column].unique()
-- ===============================================================================================================
-- 5. Counting records for database insights: COUNT
--
-- 5.1 Determine the number of stores that the company has.
SELECT * FROM store;
SELECT COUNT(store_id) AS nbr_stores FROM store;
--
-- 5.2 Determine the number of employees that the company has.
SELECT * FROM staff;
SELECT COUNT(staff_id) AS nbr_employees FROM staff;
--
-- 5.3 Determine how many films are available for rent and how many have been rented.
SELECT * FROM film;
DESCRIBE film;

-- struggled a bit with this one
--
-- Number of all films
SELECT COUNT(*) AS nbr_films_all FROM film;
SELECT * FROM film;
--
-- Number of films available for rent
SELECT * FROM inventory;
SELECT DISTINCT film_id FROM inventory;
SELECT COUNT(DISTINCT film_id) AS nbr_films_available_for_rent FROM inventory;
--
-- Number of films that have been rented
SELECT * FROM rental;
SELECT COUNT(DISTINCT rental_id) AS number_of_films_rented
FROM rental;

--
-- 5.4 Determine the number of distinct last names of the actors in the database.
SELECT DISTINCT last_name AS actors_last_name FROM actor;
SELECT COUNT(DISTINCT last_name) AS nbr_actors_last_name FROM actor; -- i mixed up the distinct before count
-- ===============================================================================================================
-- 6. Retrieve the 10 longest films.
-- method 1 : ORDER BY / DESC LIMIT 10
SELECT * FROM film ORDER BY length DESC LIMIT 10;
-- method 2 : WHERE column = (SELECT MAX(column)FROM table_name)
SELECT * FROM film WHERE length = (SELECT MAX(length) FROM film);
-- ===============================================================================================================
-- 7. Use filtering techniques in order to:
--
-- 7.1 Retrieve all actors with the first name "SCARLETT".
SELECT * FROM actor;
SELECT * FROM actor WHERE first_name = 'SCARLETT';
--
-- 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)**
SELECT * FROM film WHERE title LIKE 'ARMAGEDDON'; -- false gotta use regex
SELECT * FROM film WHERE title LIKE '%ARMAGEDDON%';
SELECT * FROM film WHERE title LIKE '%ARMAGEDDON%' AND length > 100;
--
-- 7.3 Determine the number of films that include Behind the Scenes content
SELECT * FROM film;
SELECT * FROM film WHERE special_features LIKE '%Behind the Scenes%';
SELECT COUNT(*) FROM film WHERE special_features LIKE '%Behind the Scenes%';
SELECT COUNT(*) AS films_behind_the_scenes FROM film WHERE special_features LIKE '%Behind the Scenes%';
-- ===============================================================================================================
Loading