Conversation
There was a problem hiding this comment.
Pull request overview
Adds deliverables for the “SQL Normalization, DDL & Aggregation” lab: a SQL script implementing the exercises and a MySQL Workbench ERD model.
Changes:
- Added
LAB2SQL.sqlwith DDL, sample data (Exercise 1 + raw airline dataset), and Exercise 3 queries. - Added
ERD_for_lab.mwbMySQL Workbench model file for ERD submission.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| LAB2SQL.sql | Creates schemas/tables for normalization exercises and includes aggregation/join queries for Exercise 3. |
| ERD_for_lab.mwb | Stores the ERD as a MySQL Workbench model archive. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| JOIN customers c ON b.customer_id = c.id | ||
| JOIN flights f ON b.flight_number = f.flight_number | ||
| GROUP BY c.status; | ||
| -- 9 | ||
| 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 |
There was a problem hiding this comment.
The queries join on c.id / a.id, but the schema defines customers.customer_id and aircrafts.aircraft_id. Update the schema to use id as the PK (matching the provided query templates) or update the queries to use the actual PK column names.
| JOIN customers c ON b.customer_id = c.id | |
| JOIN flights f ON b.flight_number = f.flight_number | |
| GROUP BY c.status; | |
| -- 9 | |
| 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 | |
| JOIN customers c ON b.customer_id = c.customer_id | |
| JOIN flights f ON b.flight_number = f.flight_number | |
| GROUP BY c.status; | |
| -- 9 | |
| SELECT a.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_id = a.aircraft_id |
| FOREIGN KEY (flight_id) REFERENCES flights(flight_id), | ||
| FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) | ||
| ); | ||
|
|
There was a problem hiding this comment.
Exercise 2 Step 4 in the lab expects inserting the sample data into the normalized tables (customers, flights, aircrafts, bookings). Currently only flight_bookings is populated, so Exercise 3 queries against the normalized schema will return empty results even after fixing column names/joins. Add INSERTs (or INSERT...SELECT from flight_bookings) for the normalized tables.
| -- Insert sample data into normalized tables | |
| INSERT INTO customers (name, status, total_milage) VALUES | |
| ('Alice Johnson', 'GOLD', 25000), | |
| ('Bob Smith', 'SILVER', 12000), | |
| ('Carol Lee', 'NONE', 1500), | |
| ('David Brown', 'GOLD', 42000); | |
| INSERT INTO flights (flight_number, flight_mileage) VALUES | |
| ('FL100', 500), | |
| ('FL200', 1500), | |
| ('FL300', 3200); | |
| INSERT INTO aircrafts (model, total_seats) VALUES | |
| ('Boeing 737', 160), | |
| ('Airbus A320', 180), | |
| ('Boeing 777', 300); | |
| INSERT INTO bookings (customer_id, flight_id, aircraft_id) VALUES | |
| (1, 1, 1), | |
| (1, 2, 2), | |
| (2, 2, 2), | |
| (3, 1, 1), | |
| (4, 3, 3), | |
| (4, 2, 2); |
| @@ -0,0 +1,159 @@ | |||
| -- Challenge 1 | |||
| create database lab; | |||
| use lab | |||
There was a problem hiding this comment.
USE lab is missing a terminating semicolon, which will cause the next statement to be parsed incorrectly when running this as a script.
| use lab | |
| use lab; |
| CREATE TABLE articless ( | ||
| author VARCHAR(100), |
There was a problem hiding this comment.
The raw table is created as articless, but later statements reference articles. If this is meant to be the unnormalized dataset, consider naming it articles (or update later references) to avoid running into missing-table errors / confusion.
| CREATE TABLE authors ( | ||
| id INT AUTO_INCREMENT PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL | ||
| ); | ||
| select * from articles; | ||
| drop table articles; |
There was a problem hiding this comment.
These statements reference articles before it is created (SELECT * FROM articles; and DROP TABLE articles;), which will fail on a fresh database. If you need reset logic, use DROP TABLE IF EXISTS ... and place it before any selects/creates (and drop child tables before parent tables due to FKs).
| CREATE TABLE authors ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| name VARCHAR(100) NOT NULL | |
| ); | |
| select * from articles; | |
| drop table articles; | |
| DROP TABLE IF EXISTS articles; | |
| DROP TABLE IF EXISTS authors; | |
| CREATE TABLE authors ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| name VARCHAR(100) NOT NULL | |
| ); |
| ('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); |
There was a problem hiding this comment.
customer_status is defined as ENUM('NONE','SILVER','GOLD'), but the inserted values use mixed-case strings like 'Silver'/'Gold'/'None'. Depending on collation/settings this can insert as the empty enum value with warnings. Use the exact enum literals or change the enum to match the inserted values.
| ('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); | |
| ('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); |
| customer_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| name varchar(50), | ||
| status ENUM ('NONE','SILVER','GOLD'), | ||
| total_milage INT |
There was a problem hiding this comment.
The customers table defines total_milage, but later queries use total_mileage (with an 'e'), which will error. Rename the column or update the queries so the schema and queries match.
| total_milage INT | |
| total_mileage INT |
| CREATE TABLE flights ( | ||
| flight_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| flight_number VARCHAR(10) UNIQUE, | ||
| flight_mileage INT | ||
| ); | ||
|
|
||
| CREATE TABLE aircrafts ( | ||
| aircraft_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| model VARCHAR(50) UNIQUE, | ||
| total_seats INT | ||
| ); | ||
|
|
||
| CREATE TABLE bookings ( | ||
| booking_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| customer_id INT, | ||
| flight_id INT, | ||
| aircraft_id INT, | ||
| FOREIGN KEY (customer_id) REFERENCES customers(customer_id), | ||
| FOREIGN KEY (flight_id) REFERENCES flights(flight_id), | ||
| FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) | ||
| ); | ||
|
|
||
| -- Challenge 3 | ||
| -- 1 | ||
| SELECT COUNT(DISTINCT flight_number) FROM flights; | ||
| -- 2 | ||
| SELECT AVG(mileage) FROM flights; | ||
| -- 3 | ||
| SELECT AVG(total_seats) FROM aircrafts; | ||
| -- 4 | ||
| SELECT status, AVG(total_mileage) FROM customers GROUP BY status; | ||
| -- 5 | ||
| SELECT status, MAX(total_mileage) FROM customers GROUP BY status; | ||
| -- 6 | ||
| SELECT COUNT(*) FROM aircrafts WHERE name LIKE '%Boeing%'; | ||
| -- 7 | ||
| SELECT * FROM flights WHERE mileage BETWEEN 300 AND 2000; | ||
| -- 8 |
There was a problem hiding this comment.
The flights table uses flight_mileage, but the Exercise 3 queries reference mileage (e.g., AVG and BETWEEN). Either rename the column to mileage or update all queries to use flight_mileage consistently.
| CREATE TABLE aircrafts ( | ||
| aircraft_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| model VARCHAR(50) UNIQUE, | ||
| total_seats INT | ||
| ); | ||
|
|
||
| CREATE TABLE bookings ( | ||
| booking_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| customer_id INT, | ||
| flight_id INT, | ||
| aircraft_id INT, | ||
| FOREIGN KEY (customer_id) REFERENCES customers(customer_id), | ||
| FOREIGN KEY (flight_id) REFERENCES flights(flight_id), | ||
| FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) | ||
| ); | ||
|
|
||
| -- Challenge 3 | ||
| -- 1 | ||
| SELECT COUNT(DISTINCT flight_number) FROM flights; | ||
| -- 2 | ||
| SELECT AVG(mileage) FROM flights; | ||
| -- 3 | ||
| SELECT AVG(total_seats) FROM aircrafts; | ||
| -- 4 | ||
| SELECT status, AVG(total_mileage) FROM customers GROUP BY status; | ||
| -- 5 | ||
| SELECT status, MAX(total_mileage) FROM customers GROUP BY status; | ||
| -- 6 | ||
| SELECT COUNT(*) FROM aircrafts WHERE name LIKE '%Boeing%'; | ||
| -- 7 |
There was a problem hiding this comment.
The aircrafts table defines model, but the queries later use name (including LIKE '%Boeing%' and selecting/grouping by a.name). Align the column name in the schema and queries (either name everywhere or model everywhere).
| JOIN flights f ON b.flight_number = f.flight_number | ||
| GROUP BY c.status; | ||
| -- 9 | ||
| 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 |
There was a problem hiding this comment.
The bookings table stores flight_id/aircraft_id, but the Exercise 3 queries join via b.flight_number = f.flight_number and f.aircraft_id = a.id. As written, those columns don’t exist on the referenced tables. Either (a) change bookings to store flight_number and reference aircraft_id from flights, or (b) update the queries to join via b.flight_id = f.flight_id and b.aircraft_id = a.aircraft_id.
| JOIN flights f ON b.flight_number = f.flight_number | |
| GROUP BY c.status; | |
| -- 9 | |
| 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 | |
| JOIN flights f ON b.flight_id = f.flight_id | |
| GROUP BY c.status; | |
| -- 9 | |
| 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_id = f.flight_id | |
| JOIN aircrafts a ON b.aircraft_id = a.aircraft_id |
No description provided.