diff --git a/C01_blog_normalization.sql b/C01_blog_normalization.sql new file mode 100644 index 0000000..662c3f4 --- /dev/null +++ b/C01_blog_normalization.sql @@ -0,0 +1,45 @@ +-- ============================================================ +-- Exercise 1: Normalize a Blog Database +-- ============================================================ + +-- STEP 1: Analysis +-- Raw table has redundancy: author name repeated across multiple posts +-- Normalization to 3NF requires separating authors from posts + +-- 1NF: each cell has one value, each row is unique ✓ +-- 2NF: add surrogate keys to eliminate partial dependencies +-- 3NF: separate author data into its own table (author info depends on author, not on post) + +-- STEP 2: DDL + +-- Authors table +CREATE TABLE authors ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL +); + +-- Posts table (references author) +CREATE TABLE posts ( + id INT PRIMARY KEY AUTO_INCREMENT, + author_id INT NOT NULL, + title VARCHAR(255) NOT NULL, + word_count INT, + views INT, + FOREIGN KEY (author_id) REFERENCES authors(id) +); + +-- STEP 3 (Optional): Insert sample data + +INSERT INTO authors (name) VALUES +('Maria Charlotte'), +('Juan Perez'), +('Gemma Alcocer'); + +INSERT INTO posts (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); diff --git a/C02_airline_normalization.sql b/C02_airline_normalization.sql new file mode 100644 index 0000000..56450be --- /dev/null +++ b/C02_airline_normalization.sql @@ -0,0 +1,86 @@ +-- ============================================================ +-- Exercise 2: Normalize an Airline Database +-- ============================================================ + +-- STEP 1 & 2: Analysis and functional dependencies +-- +-- Raw table has three independent entities mixed together: +-- - Customer (name, status, total_mileage) → depends only on customer +-- - Aircraft (name, total_seats) → depends only on aircraft +-- - Flight (flight_number, mileage, aircraft) → depends only on flight +-- - Booking → junction between customer and flight +-- +-- Decomposition into 4 tables (3NF): + +-- STEP 3: DDL with FOREIGN KEY constraints + +-- Aircrafts table +CREATE TABLE aircrafts ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(100) NOT NULL, + total_seats INT NOT NULL +); + +-- Customers table +CREATE TABLE customers ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + status VARCHAR(50), + total_mileage INT +); + +-- Flights table (references aircraft) +CREATE TABLE flights ( + flight_number VARCHAR(10) PRIMARY KEY, + aircraft_id INT NOT NULL, + mileage INT NOT NULL, + FOREIGN KEY (aircraft_id) REFERENCES aircrafts(id) +); + +-- Bookings table (junction: customer ↔ flight) +CREATE TABLE bookings ( + id INT PRIMARY KEY AUTO_INCREMENT, + customer_id INT NOT NULL, + flight_number VARCHAR(10) NOT NULL, + FOREIGN KEY (customer_id) REFERENCES customers(id), + FOREIGN KEY (flight_number) REFERENCES flights(flight_number) +); + +-- STEP 4: Insert sample data + +INSERT INTO aircrafts (name, total_seats) VALUES +('Boeing 747', 400), +('Airbus A330', 236), +('Boeing 777', 264); + +INSERT INTO customers (name, status, total_mileage) VALUES +('Agustine Riviera', 'Silver', 115235), +('Alaina Sepulvida', NULL, 6008), +('Tom Jones', 'Gold', 205767), +('Sam Rio', NULL, 2653), +('Jessica James', 'Silver', 127656), +('Ana Janco', 'Silver', 136773), +('Jennifer Cortez', 'Gold', 300582), +('Christian Janco', 'Silver', 14642); + +INSERT INTO flights (flight_number, aircraft_id, mileage) VALUES +('DL143', 1, 135), +('DL122', 2, 4370), +('DL53', 3, 2078), +('DL222', 3, 1765), +('DL37', 1, 531); + +INSERT INTO bookings (customer_id, flight_number) VALUES +(1, 'DL143'), +(1, 'DL122'), +(2, 'DL122'), +(3, 'DL122'), +(3, 'DL53'), +(3, 'DL222'), +(4, 'DL143'), +(4, 'DL37'), +(5, 'DL143'), +(5, 'DL122'), +(6, 'DL222'), +(7, 'DL222'), +(8, 'DL222'); diff --git a/C03_airline_queries.sql b/C03_airline_queries.sql new file mode 100644 index 0000000..136e1b2 --- /dev/null +++ b/C03_airline_queries.sql @@ -0,0 +1,53 @@ +-- ============================================================ +-- Exercise 3: SQL Queries on the Airline Database +-- ============================================================ + +-- 1. Total number of flights +SELECT COUNT(DISTINCT flight_number) AS total_flights +FROM flights; + +-- 2. Average flight distance +SELECT AVG(mileage) AS avg_flight_distance +FROM flights; + +-- 3. Average number of seats per aircraft +SELECT AVG(total_seats) AS avg_seats_per_aircraft +FROM aircrafts; + +-- 4. Average miles flown by customers, grouped by status +SELECT status, AVG(total_mileage) AS avg_mileage +FROM customers +GROUP BY status; + +-- 5. Max miles flown by customers, grouped by status +SELECT status, MAX(total_mileage) AS max_mileage +FROM customers +GROUP BY status; + +-- 6. Number of aircrafts with "Boeing" in their name +SELECT COUNT(*) AS boeing_count +FROM aircrafts +WHERE name LIKE '%Boeing%'; + +-- 7. Flights with distance between 300 and 2000 miles +SELECT * +FROM flights +WHERE mileage BETWEEN 300 AND 2000; + +-- 8. Average flight distance booked, grouped by customer status +SELECT c.status, AVG(f.mileage) AS avg_booked_distance +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 +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;