diff --git a/README.md b/README.md deleted file mode 100644 index 648766e..0000000 --- a/README.md +++ /dev/null @@ -1,140 +0,0 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) - -# LAB | SQL Normalization, DDL & Aggregation - -### Instructions - -1. Fork this repo. -2. Clone your fork to your local machine. -3. Solve the challenges. - - -## Deliverables - -- Upon completion, add your solution to git. -- Then commit to git and push to your repo on GitHub. -- Make a pull request and paste the pull request link in the submission field in the Student Portal. - - -## Exercise 1: Normalize a Blog Database - -**Step 1:** Given the following raw dataset, identify redundancies and normalize the data (at least to 3NF). - -
- -| **author** | **title** | **word_count** | **views** | -|----------------|-----------------------------|------------|-------| -| Maria Charlotte| Best Paint Colors | 814 | 14 | -| Juan Perez | Small Space Decorating Tips | 1146 | 221 | -| Maria Charlotte| Hot Accessories | 986 | 105 | -| Maria Charlotte| Mixing Textures | 765 | 22 | -| Juan Perez | Kitchen Refresh | 1242 | 307 | -| Maria Charlotte| Homemade Art Hacks | 1002 | 193 | -| Gemma Alcocer | Refinishing Wood Floors | 1571 | 7542 | - -
- -**Step 2:** Write the DDL (`CREATE TABLE` statements) to implement your normalized schema. - -**Step 3 (Optional):** Insert the sample data using `INSERT INTO`. - -## Exercise 2: Normalize an Airline Database - -**Step 1:** Normalize the following data (again, at least up to 3NF): - -
- -| **Customer Name** | **Customer Status** | **Flight Number** | **Aircraft** | **Total Aircraft Seats** | **Flight Mileage** | **Total Customer Mileage** | -| ---------------- | --------------- | ------------- | ----------- | -------------------- | -------------- | ---------------------- | -| Agustine Riviera | Silver | DL143 | Boeing 747 | 400 | 135 | 115235 | -| Agustine Riviera | Silver | DL122 | Airbus A330 | 236 | 4370 | 115235 | -| Alaina Sepulvida | None | DL122 | Airbus A330 | 236 | 4370 | 6008 | -| Agustine Riviera | Silver | DL143 | Boeing 747 | 400 | 135 | 115235 | -| Tom Jones | Gold | DL122 | Airbus A330 | 236 | 4370 | 205767 | -| Tom Jones | Gold | DL53 | Boeing 777 | 264 | 2078 | 205767 | -| Agustine Riviera | Silver | DL143 | Boeing 747 | 400 | 135 | 115235 | -| Sam Rio | None | DL143 | Boeing 747 | 400 | 135 | 2653 | -| Agustine Riviera | Silver | DL143 | Boeing 747 | 400 | 135 | 115235 | -| Tom Jones | Gold | DL222 | Boeing 777 | 264 | 1765 | 205767 | -| Jessica James | Silver | DL143 | Boeing 747 | 400 | 135 | 127656 | -| Sam Rio | None | DL143 | Boeing 747 | 400 | 135 | 2653 | -| Ana Janco | Silver | DL222 | Boeing 777 | 264 | 1765 | 136773 | -| Jennifer Cortez | Gold | DL222 | Boeing 777 | 264 | 1765 | 300582 | -| Jessica James | Silver | DL122 | Airbus A330 | 236 | 4370 | 127656 | -| Sam Rio | None | DL37 | Boeing 747 | 400 | 531 | 2653 | -| Christian Janco | Silver | DL222 | Boeing 777 | 264 | 1765 | 14642 | - -
- -**Step 2:** Identify functional dependencies and decompose into smaller, related tables: -- `customers` -- `flights` -- `aircrafts` -- `bookings` - -**Step 3:** Write the DDL scripts (`CREATE TABLE` + `FOREIGN KEY` constraints). - -**Step 4:** Insert the sample data. - -## Exercise 3: Write SQL Queries on the Airline Database - -Use the schema you created in Exercise 2 to answer the following: - -1. Total number of flights: -```sql -SELECT COUNT(DISTINCT flight_number) FROM flights; -``` -2. Average flight distance: -```sql -SELECT AVG(mileage) FROM flights; -``` -3. Average number of seats per aircraft: -```sql -SELECT AVG(total_seats) FROM aircrafts; -``` -4. Average miles flown by customers, grouped by status: -```sql -SELECT status, AVG(total_mileage) FROM customers GROUP BY status; -``` -5. Max miles flown by customers, grouped by status: -```sql -SELECT status, MAX(total_mileage) FROM customers GROUP BY status; -``` -6. Number of aircrafts with "Boeing" in their name: -```sql -SELECT COUNT(*) FROM aircrafts WHERE name LIKE '%Boeing%'; -``` -7. Flights with distance between 300 and 2000 miles: -```sql -SELECT * FROM flights WHERE mileage BETWEEN 300 AND 2000; -``` -8. Average flight distance **booked**, grouped by customer status: -```sql -SELECT c.status, AVG(f.mileage) -FROM bookings b -JOIN customers c ON b.customer_id = c.id -JOIN flights f ON b.flight_number = f.flight_number -GROUP BY c.status; -``` -9. Most booked aircraft among Gold status members: -```sql -SELECT a.name, COUNT(*) AS total_bookings -FROM bookings b -JOIN customers c ON b.customer_id = c.id -JOIN flights f ON b.flight_number = f.flight_number -JOIN aircrafts a ON f.aircraft_id = a.id -WHERE c.status = 'Gold' -GROUP BY a.name -ORDER BY total_bookings DESC -LIMIT 1; -``` - -## Extra Challenge - -- Create `ERD` diagrams to visualize both schemas. -- Use `CHECK`, `NOT NULL`, and `UNIQUE` constraints where appropriate. -- Add indexes where you think performance can improve. - -
- -**Have fun querying!** :dart: \ No newline at end of file diff --git a/erd.mwb b/erd.mwb new file mode 100644 index 0000000..8dbdfb4 Binary files /dev/null and b/erd.mwb differ diff --git a/lab2.sql b/lab2.sql new file mode 100644 index 0000000..dab1a0a --- /dev/null +++ b/lab2.sql @@ -0,0 +1,180 @@ +use lab_2; + + + +-- Exercise 1: Normalize a Blog Database +-- in this exercise relation beween title and author is one to one. + +create table titles( + + title_id int auto_increment primary key, + title_name varchar(200), + word_count int, + views int +); + +create table authors ( + + author_id int auto_increment primary key, + author_fname varchar(100), + + title_id int, + + FOREIGN KEY (title_id) REFERENCES titles(title_id) + + +); + +insert into titles(title_name, word_count, views) values +('Best Paint Colors', 814, 14), +('Small Space Decorating Tips', 1146, 221), +('Hot Accessories', 986, 105), +('Mixing Textures', 765, 22), +('Kitchen Refresh', 1242, 307), +('Homemade Art Hacks', 1002, 193), +('Refinishing Wood Floors', 1571, 7542); + + +insert into authors (author_fname, title_id) values +('Maria Charlotte', 1), +('Juan Perez', 2), +('Maria Charlotte', 3), +('Maria Charlotte', 4), +('Juan Perez', 5), +('Maria Charlotte', 6), +('Gemma Alcocer', 7); + + + + +-- Exercise 2: Normalize an Airline Database + + +CREATE TABLE customers ( + customer_id int NOT NULL AUTO_INCREMENT, + customer_name varchar(200) DEFAULT NULL, + customer_status varchar(200) DEFAULT 'None', + total_customer_mileage int DEFAULT NULL, + PRIMARY KEY (customer_id) + ); + +INSERT INTO customers (customer_name, customer_status, total_customer_mileage) +VALUES +('Agustine Riviera', 'Silver', 115235), +('Alaina Sepulvida', 'None', 6008), +('Tom Jones', 'Gold', 205767), +('Sam Rio', 'None', 2653), +('Jessica James', 'Silver', 127656), +('Ana Janco', 'Silver', 136773), +('Jennifer Cortez', 'Gold', 300582), +('Christian Janco', 'Silver', 14642); + +CREATE TABLE aircrafts ( + aircraft_name varchar(45) NOT NULL, + total_aircraft_seats int DEFAULT NULL, + PRIMARY KEY (aircraft_name), + UNIQUE KEY aircraft_name_UNIQUE (aircraft_name) + ); + + +INSERT INTO aircrafts (aircraft_name, total_aircraft_seats) +VALUES +('Boeing 747', 400), +('Airbus A330', 236), +('Boeing 777', 264); + + +CREATE TABLE flights ( + flight_number varchar(300) NOT NULL, + aircraft varchar(45) NOT NULL, + flight_mileage int DEFAULT NULL, + PRIMARY KEY (flight_number), + KEY aircraft_idx (aircraft), + CONSTRAINT aircraft FOREIGN KEY (aircraft) REFERENCES aircrafts (aircraft_name) + ); + +INSERT INTO flights (flight_number, aircraft, flight_mileage) +VALUES +('DL143', 'Boeing 747', 135), +('DL122', 'Airbus A330', 4370), +('DL53', 'Boeing 777', 2078), +('DL222', 'Boeing 777', 1765), +('DL37', 'Boeing 747', 531); + +CREATE TABLE bookings ( + customer_id int DEFAULT NULL, + flight_number varchar(300) DEFAULT NULL, + KEY customer_id_idx (customer_id), + KEY flight_number_idx (flight_number), + CONSTRAINT customer_id FOREIGN KEY (customer_id) REFERENCES customers (customer_id), + CONSTRAINT flight_number FOREIGN KEY (flight_number) REFERENCES flights (flight_number) + ); + +INSERT INTO bookings (customer_id, flight_number) +VALUES +(1, 'DL143'), +(1, 'DL122'), +(2, 'DL122'), +(3, 'DL122'), +(3, 'DL53'), +(3, 'DL222'); + + +-- Exercise 3: Write SQL Queries on the Airline Database + +SELECT COUNT(DISTINCT flight_number) FROM flights; -- 5 + +SELECT AVG(flight_mileage) FROM flights; -- 1775.8000 + +SELECT AVG(total_aircraft_seats) FROM aircrafts; -- 300.0000 + +SELECT customer_status, AVG(total_customer_mileage) FROM customers GROUP BY customer_status; +-- Silver 98576.5000 +-- None 4330.5000 +-- Gold 253174.5000 + +SELECT customer_status, MAX(total_customer_mileage) FROM customers GROUP BY customer_status; +-- Silver 136773 +-- None 6008 +-- Gold 300582 + +SELECT COUNT(*) FROM aircrafts WHERE aircraft_name LIKE '%Boeing%'; -- 2 + +SELECT * FROM flights WHERE flight_mileage BETWEEN 300 AND 2000; +-- DL222 Boeing 777 1765 +-- DL37 Boeing 747 531 + + +SELECT c.status, AVG(f.flight_mileage) +FROM bookings b +JOIN customers c ON b.customer_id = c.customer_id +JOIN flights f ON b.flight_number = f.flight_number +GROUP BY c.customer_status; +-- DL222 Boeing 777 1765 +-- DL37 Boeing 747 531 + + +SELECT a.aircraft_name, COUNT(*) AS total_bookings +FROM bookings b +JOIN customers c ON b.customer_id = c.customer_id +JOIN flights f ON b.flight_number = f.flight_number +JOIN aircrafts a ON f.aircraft = a.aircraft_name +WHERE c.customer_status = 'Gold' +GROUP BY a.aircraft_name +ORDER BY total_bookings DESC +LIMIT 1; +-- Boeing 777 2 + + + + + + + + + + + + + +