diff --git a/your-code/Day1 - SQL Intro.docx b/your-code/Day1 - SQL Intro.docx new file mode 100644 index 0000000..06f4901 Binary files /dev/null and b/your-code/Day1 - SQL Intro.docx differ diff --git a/your-code/Day1 - queries.sql b/your-code/Day1 - queries.sql new file mode 100644 index 0000000..92acafb --- /dev/null +++ b/your-code/Day1 - queries.sql @@ -0,0 +1,162 @@ + +/* my first sql query select all columns */ +SELECT * +FROM sakila.film; /* first you select what you want then you say from wich database and table you want */ + +/* my first sql query select all columns and only first 20 rows */ +SELECT * +FROM sakila.film +LIMIT 20; /* this can be done with the statement LIMIT */ + +/*select only some columns */ + +SELECT +title , +description , +rating +FROM sakila.film; + +/* CHALLENGE: Select from the table adress the column adress, district, and phone the top 5 rows */ + +SELECT address, district, phone +FROM sakila.address +LIMIT 5; + + +/*select the distinct values in a particular column */ + +SELECT DISTINCT rental_duration /* we want to select here the distict values for rental duration */ +FROM sakila.film; + + +/*... or multiple */ + +SELECT DISTINCT rental_duration, language_id +FROM sakila.film; + +/*Check the longest films */ + +SELECT title, rental_rate, length +FROM sakila.film +ORDER BY length DESC; /* Order by sets how we want to order the data from a certain table */ + +/*Check the longest films, tiebreak by most expensive rental */ + +SELECT title, rental_rate, length +FROM sakila.film +ORDER BY length DESC, rental_rate DESC; + + +/* Aliasing */ +SELECT title, rental_rate AS cost, length +FROM sakila.film; + +/* Computations */ +SELECT title, rental_rate/length AS price_per_min /* what is the least expessive rental by unite of time */ +FROM sakila.film +ORDER BY price_per_min ASC; + +/*String Computations */ + +SELECT CONCAT(title,', rating:',rating) AS descriptor +FROM sakila.film; + + + +/* perform a query with a condition --|> are filter */ + +SELECT * +FROM sakila.film +WHERE sakila.film.rental_duration = 6; /* the where statement occures before the selection and aggregations so it's a good praticice saying from wich db and table you want to filter the columns */ + +/* plus some variations */ + +WHERE sakila.film.rental_duration > 6; +WHERE sakila.film.rental_duration >= 6; +WHERE sakila.film.rental_duration <> 6; +WHERE sakila.film.rental_duration in (3,4,5,6); + +WHERE sakila.film.special_features = 'Deleted Scenes'; +WHERE sakila.film.special_features LIKE '%Deleted Scenes%'; +WHERE sakila.film.special_features NOT LIKE '%Deleted Scenes%'; + +/* Sample aggregations */ + +SELECT COUNT(*),MAX(rental_duration),AVG(replacement_cost),AVG(rental_duration) +FROM sakila.film; /* we can agregate if all the columns are agregated the same way */ + + +/* Aggregations with group by */ +SELECT rating, COUNT(rating), AVG(rental_rate) +FROM sakila.film +GROUP BY rating; /* you group by what the column you are not agregating */ + +/* Aggregations with group by multiple */ + + +SELECT rating, rental_duration, COUNT(rating), AVG(rental_rate) +FROM sakila.film +GROUP BY rating, rental_duration; + +/* SHOW REVERSE ENGENEERE ERD and talk about keys */ + +/* bringing tables together -> join them -> one step inner join*/ + +SELECT * +FROM world.country; + +SELECT * +FROM world.city; + +/*notice duplications*/ +SELECT * +FROM world.country +INNER JOIN world.city +ON world.country.Code = world.city.CountryCode; + + +/*find Product per capita*/ + +/* GNP per capita in each city */ + +SELECT world.country.Name AS Country_Name, world.city.Name AS City_Name, world.country.GNP/world.country.Population AS GNP_per_capita +FROM world.country +INNER JOIN world.city +ON world.country.Code = world.city.CountryCode; + + + +/* bringing tables together -> join them -> 2 step inner join*/ + +SELECT * FROM sakila.actor; + +SELECT * FROM sakila.film_actor; + +SELECT * FROM sakila.film; + +/* many 2 many relations are well handled by a "bridge" table */ + +SELECT sakila.actor.first_name AS Fname, sakila.actor.last_name AS Lname, sakila.film_actor.film_id +FROM sakila.actor +INNER JOIN sakila.film_actor +ON sakila.actor.actor_id = sakila.film_actor.actor_id; + +/*an now this new table can be used as a subtable -> ADVANCED TOPIC */ + +SELECT new_table.Fname, new_table.Lname, sakila.film.title +FROM +(SELECT sakila.actor.first_name AS Fname, sakila.actor.last_name AS Lname, sakila.film_actor.film_id +FROM sakila.actor +INNER JOIN sakila.film_actor +ON sakila.actor.actor_id = sakila.film_actor.actor_id) AS new_table +INNER JOIN sakila.film +ON new_table.film_id = sakila.film.film_id; + + + + + + + + + diff --git a/your-code/Day2 - queries.sql b/your-code/Day2 - queries.sql new file mode 100644 index 0000000..5d1a291 --- /dev/null +++ b/your-code/Day2 - queries.sql @@ -0,0 +1,59 @@ + +/*Find the list of actors which starred in movies with lengths higher or equal to the average length of all the movies*/ + +/*average length of all the movies*/ +SELECT AVG(length) AS average FROM film; + +/*movies with lengths higher or equal to the average length of all the movies*/ +SELECT * +FROM sakila.film +WHERE length >= (SELECT AVG(length) AS average FROM film); + +/*movies with lengths higher or equal to the average length of all the movies*/ +SELECT film_id +FROM sakila.film +WHERE length >= (SELECT AVG(length) AS average FROM film); + + +/*list of actors which starred in movies with lengths higher or equal to the average length of all the movies*/ +SELECT DISTINCT actor_id +FROM sakila.film_actor +INNER JOIN (SELECT film_id FROM sakila.film WHERE length >= (SELECT AVG(length) AS average FROM film)) AS selected_films_id +ON sakila.film_actor.film_id = selected_films_id.film_id; + + +/*list of actors which starred in movies with lengths higher or equal to the average length of all the movies*/ +SELECT first_name, last_name +FROM sakila.actor +INNER JOIN ( +SELECT DISTINCT actor_id +FROM sakila.film_actor +INNER JOIN (SELECT film_id FROM sakila.film WHERE length >= (SELECT AVG(length) AS average FROM film)) AS selected_films_id +ON sakila.film_actor.film_id = selected_films_id.film_id) AS selected_actors +ON sakila.actor.actor_id = selected_actors.actor_id; + + +/* Temporary tables to store intermediate result */ + +CREATE TEMPORARY TABLE sakila.selected_films +SELECT film_id +FROM sakila.film +WHERE length >= (SELECT AVG(length) AS average FROM film); + +SELECT * FROM selected_films; + +CREATE TEMPORARY TABLE sakila.selected_actors +SELECT DISTINCT actor_id +FROM sakila.film_actor +INNER JOIN sakila.selected_films +ON sakila.film_actor.film_id = selected_films.film_id; + +SELECT first_name, last_name +FROM sakila.actor +INNER JOIN sakila.selected_actors +ON sakila.actor.actor_id = selected_actors.actor_id; + + + + + diff --git a/your-code/ERD.mwb b/your-code/ERD.mwb new file mode 100644 index 0000000..d4ba1ac Binary files /dev/null and b/your-code/ERD.mwb differ diff --git a/your-code/ERD.mwb.bak b/your-code/ERD.mwb.bak new file mode 100644 index 0000000..dc99f11 Binary files /dev/null and b/your-code/ERD.mwb.bak differ diff --git a/your-code/ERD.mwb.beforefix b/your-code/ERD.mwb.beforefix new file mode 100644 index 0000000..d4ba1ac Binary files /dev/null and b/your-code/ERD.mwb.beforefix differ diff --git a/your-code/create.sql b/your-code/create.sql index e69de29..26d5282 100644 --- a/your-code/create.sql +++ b/your-code/create.sql @@ -0,0 +1,80 @@ +# CUSTOMERS +# EACH CUSTOMER IS UNIQUE +# ONE CUSTOMER CAN BE INVOICED (i) ONE MODEL/ONE CAR, (ii) MULTIPLE CARS FROM SAME MODEL and (iii) SEVERAL MODELS WITH SINGLE OR MULTIPLE CARS + +# SALESPERSONS +# EACH SALESPERSON IS UNIQUE +# ONE SALESPERSON CAN INVOICE (i) ONE MODEL/ONE CAR, (ii) MULTIPLE CARS FROM SAME MODEL and (iii) MULTIPLE MODELS WITH SINGLE OR MULTIPLE CARS + +# INVOICES +# EACH INVOICE IS UNIQUES +# ONE INVOICE REFERS TO ONE CUSTOMER (NOTE: CUSTOMER CAN BE DIFERENT FROM ONE INVOICE TO ANOTHER INVOICE) +# ONE INVOICE REFERS TO ONE SALESPERSON (NOTE: SALESPERSON CAN BE DIFERENT FROM ONE INVOICE TO ANOTHER INVOICE) +# ONE INVOICE CAN REFERS TO (i) ONE MODEL /ONE CAR OR (ii) MULTIPLE CARS FROM SAME MODEL OR (iii) MULTIPLE MODELS WITH SINGLE OR MULTIPLE CARS + +# EACH CAR IS UNIQUE +# ONE CAR IS RELATED TO A ONE SINGLE MODEL +# EACH CAR CAN BE INVOICED ONLY ONCE TO A SINGLE CUSTOMER BY A SINGLE SALESPERSON +# ONE MODEL CAN BE INVOICED ONE TIME IN THE SAME INVOICE +# DIFERENT MODELS CAN BE INVOICED ONE TIME IN THE SAME INVOICE +# ONE MODEL CAN BE INVOICED MULTIPLE TIMES IN THE SAME INVOICE +# DIFERENT MODELS CAN BE INVOICED MULTIPLE TIMES IN THE SAME INVOICE +# ONE MODEL CAN BE INVOICED MULTIPLE TIMES IN DIFFERENT INVOICES +# DIFERENT MODELS CAN BE INVOICED MULTIPLE TIMES IN DIFFERENT INVOICES + +CREATE DATABASE IF NOT EXISTS lab_mysql; + +USE lab_mysql; + +CREATE TABLE IF NOT EXISTS Cars ( + carID INT NOT NULL AUTO_INCREMENT, + VIN VARCHAR(52), + Manufacturer VARCHAR(52), + Model VARCHAR(52), + Year INT, + Color VARCHAR(52), + PRIMARY KEY (CarID) +); +USE lab_mysql; +ALTER TABLE cars AUTO_INCREMENT=0, algorithm=inplace; + +CREATE TABLE IF NOT EXISTS Customers ( + customerID INT NOT NULL AUTO_INCREMENT, + ClientID INT, + Name VARCHAR(52), + Phone VARCHAR(52), + Email VARCHAR(52) DEFAULT 'General@cars.com', + Address VARCHAR(52), + City VARCHAR(52), + StateProvince VARCHAR(52), + Country VARCHAR(52), + PostalCode VARCHAR(52), + PRIMARY KEY (CustomerID) +); +USE lab_mysql; +ALTER TABLE customers AUTO_INCREMENT=0, algorithm=inplace; + +CREATE TABLE IF NOT EXISTS Salespersons ( + SalespersonID INT NOT NULL AUTO_INCREMENT, + StaffID INT, + Name VARCHAR(52), + Store VARCHAR(52), + PRIMARY KEY (SalespersonID) + ); + USE lab_mysql; + ALTER TABLE salespersons AUTO_INCREMENT=0, algorithm=inplace; + +CREATE TABLE IF NOT EXISTS Invoices ( + InvoiceID INT NOT NULL AUTO_INCREMENT, + InvoiceNumber INT, + Date DATE, + CarID INT, + CustomerID INT, + SalespersonID INT, + PRIMARY KEY (InvoiceID), + FOREIGN KEY (CarID) REFERENCES Cars(CarID), + FOREIGN KEY (SalespersonID) REFERENCES Salespersons(SalespersonID), + FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) + ); + USE lab_mysql; + ALTER TABLE invoices AUTO_INCREMENT=0, algorithm=inplace; \ No newline at end of file diff --git a/your-code/database structure design diagram.jpg b/your-code/database structure design diagram.jpg new file mode 100644 index 0000000..716fbd8 Binary files /dev/null and b/your-code/database structure design diagram.jpg differ diff --git a/your-code/seeding.sql b/your-code/seeding.sql index e69de29..5e8b54e 100644 --- a/your-code/seeding.sql +++ b/your-code/seeding.sql @@ -0,0 +1,44 @@ +USE lab_mysql; + +SET sql_mode = "NO_AUTO_VALUE_ON_ZERO"; + +INSERT INTO Cars (VIN, Manufacturer, Model, Year, Color) +VALUES ('3K096I98581DHSNUP', 'Volkswagen', 'Tiguan', 2019, 'Blue'), +('ZM8G7BEUQZ97IH46V', 'Peugeot', 'Rifter', 2019, 'Red'), +('RKXVNNIHLVVZOUB4M', 'Ford', 'Fusion', 2018, 'White'), +('HKNDGS7CU31E9Z7JW', 'Toyota', 'RAV4', 2018, 'Silver'), +('DAM41UDN3CHU2WVF6', 'Volvo', 'V60', 2019, 'Gray'), +('DAM41UDN3CHU2WVF6', 'Volvo', 'V60 Cross Country', 2019, 'Gray'); + +INSERT INTO Customers (ClientID, Name, Phone, Address, City, StateProvince, Country, PostalCode) +VALUES ('10001', 'Pablo Picasso', '+34 636 17 63 82', 'Paseo de la Chopera, 14', 'Madrid', 'Madrid', 'Spain', '28045'), +('20001', 'Abraham Lincoln', '+1 305 907 7086', '120 SW 8th St', 'Miami', 'Florida', 'United States', '33130'), +('30001', 'Napoléon Bonaparte', '+33 1 79 75 40 00', '40 Rue du Colisée', 'Paris', 'Île-de-France', 'France', '75008'); + +INSERT INTO Salespersons (StaffID, Name, Store) +VALUES ('00001', 'Petey Cruiser', 'Madrid'), +('00002', 'Anna Sthesia', 'Barcelona'), +('00003', 'Paul Molive', 'Berlin'), +('00004', 'Gail Forcewind', 'Paris'), +('00005', 'Paige Turner', 'Mimia'), +('00006', 'Bob Frapples', 'Mexico City'), +('00007', 'Walter Melon', 'Amsterdam'), +('00008', 'Shonda Leer', 'São Paulo'); + +insert into Invoices(InvoiceNumber, date, CarID, CustomerID, SalespersonID) +VALUES (852399038, '2018-08-22', 0, 1, 3), +(731166526, '2018-12-31', 3, 0, 5), +(271135104, '2019-01-22', 2, 2, 7); + + +select * +from Cars; + +select * +from Customers; + +select * +from Salespersons; + +select * +from Invoices; \ No newline at end of file