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
Binary file added ERD_for_lab.mwb
Binary file not shown.
159 changes: 159 additions & 0 deletions LAB2SQL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
-- Challenge 1
create database lab;
use lab
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USE lab is missing a terminating semicolon, which will cause the next statement to be parsed incorrectly when running this as a script.

Suggested change
use lab
use lab;

Copilot uses AI. Check for mistakes.

CREATE TABLE articless (
author VARCHAR(100),
Comment on lines +5 to +6
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
title VARCHAR(255),
word_count INT,
views INT
);

INSERT INTO articless (author, title, word_count, views) VALUES
('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);


-- Solution 1
-- i made 2 (authors and Articles) table for 3NF
CREATE TABLE authors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
select * from articles;
drop table articles;
Comment on lines +24 to +29
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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
);

Copilot uses AI. Check for mistakes.
INSERT INTO authors (name) VALUES
('Maria Charlotte'),
('Juan Perez'),
('Gemma Alcocer');

CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
author_id INT,
title VARCHAR(255),
word_count INT,
views INT,
FOREIGN KEY (author_id) REFERENCES authors(id)
);

INSERT INTO articles (author_id, title, word_count, views) VALUES
(1, 'Best Paint Colors', 814, 14),
(2, 'Small Space Decorating Tips', 1146, 221),
(1, 'Hot Accessories', 986, 105),
(1, 'Mixing Textures', 765, 22),
(2, 'Kitchen Refresh', 1242, 307),
(1, 'Homemade Art Hacks', 1002, 193),
(3, 'Refinishing Wood Floors', 1571, 7542);

-- it is for testing
SELECT
a.name AS author,
art.title,
art.word_count,
art.views
FROM articles art
INNER JOIN authors a ON art.author_id = a.id;

-- Challenge 2

CREATE TABLE flight_bookings (
customer_name VARCHAR(50),
customer_status ENUM('NONE', 'SILVER', 'GOLD'),
flight_number VARCHAR(10),
aircraft VARCHAR(50) ,
total_aircraft_seats INT,
flight_mileage INT,
total_customer_mileage INT
);
INSERT INTO flight_bookings (
customer_name,
customer_status,
flight_number,
aircraft,
total_aircraft_seats,
flight_mileage,
total_customer_mileage
) 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);
Comment on lines +82 to +98
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
('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);

Copilot uses AI. Check for mistakes.

CREATE TABLE customers(
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name varchar(50),
status ENUM ('NONE','SILVER','GOLD'),
total_milage INT
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
total_milage INT
total_mileage INT

Copilot uses AI. Check for mistakes.
);

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)
);

Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
-- 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);

Copilot uses AI. Check for mistakes.
-- 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
Comment on lines +113 to +142
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
SELECT * FROM flights WHERE mileage BETWEEN 300 AND 2000;
-- 8
Comment on lines +107 to +144
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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
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
Comment on lines +147 to +155
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +148 to +155
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
WHERE c.status = 'Gold'
GROUP BY a.name
ORDER BY total_bookings DESC
LIMIT 1;
Loading