From 0c80db30f05f072e1cd0921c5c920a966d72b9f0 Mon Sep 17 00:00:00 2001 From: stickymittens Date: Mon, 29 Sep 2025 13:38:47 +0200 Subject: [PATCH 1/2] lab week 6 --- .idea/.gitignore | 8 ++ .idea/airline_database_commands | 140 ++++++++++++++++++++++++++++++++ .idea/lab_week_6_commands | 41 ++++++++++ 3 files changed, 189 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/airline_database_commands create mode 100644 .idea/lab_week_6_commands diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/airline_database_commands b/.idea/airline_database_commands new file mode 100644 index 0000000..8c6af55 --- /dev/null +++ b/.idea/airline_database_commands @@ -0,0 +1,140 @@ +CREATE DATABASE IF NOT EXISTS airline_database; +USE airline_database; + +CREATE DATABASE IF NOT EXISTS airline_database; +USE airline_database; + +CREATE TABLE airline_database ( + customer_name VARCHAR(100), + customer_status VARCHAR(20), + flight_number VARCHAR(10), + aircraft VARCHAR(50), + total_aircraft_seats INT, + flight_mileage INT, + total_customer_mileage BIGINT +); + +INSERT INTO airline_database ( + 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), + ('Christian Janco','Silver','DL222','Boeing 777',264,1765,14642); + + +# aircrafts - aircraft, total_aircraft_seats +CREATE TABLE aircrafts ( + aircraft VARCHAR(50), + total_aircraft_seats INT +); + +ALTER TABLE aircrafts + ADD PRIMARY KEY (aircraft); + +INSERT INTO aircrafts (aircraft, total_aircraft_seats) +SELECT DISTINCT aircraft, total_aircraft_seats +FROM airline_database; + +SELECT * FROM aircrafts; + +# flights - flight no, AIRCRAFT, flight mileage +CREATE TABLE flights ( + flight_number VARCHAR(10) UNIQUE PRIMARY KEY, + aircraft VARCHAR(50), + flight_mileage INT, + FOREIGN KEY (aircraft) REFERENCES aircrafts(aircraft) +); + +INSERT INTO flights (flight_number, aircraft, flight_mileage) +SELECT DISTINCT flight_number, aircraft, flight_mileage +FROM airline_database; + +SELECT * FROM flights; + +#customers - name, status, total mileage +CREATE TABLE customers ( + customer_name VARCHAR(100) PRIMARY KEY, + customer_status VARCHAR(20), + total_customer_mileage BIGINT +); + +INSERT INTO customers(customer_name, customer_status, total_customer_mileage) +SELECT DISTINCT customer_name, customer_status, total_customer_mileage +FROM airline_database; + +SELECT * FROM customers; + +# bookings - CUSTOMER, FLIGHT_NUMBER +CREATE TABLE bookings ( + customer_name VARCHAR(100), + flight_number VARCHAR(10), + PRIMARY KEY (customer_name, flight_number), + FOREIGN KEY (customer_name) REFERENCES customers(customer_name), + FOREIGN KEY (flight_number) REFERENCES flights(flight_number) +); + +INSERT INTO bookings (customer_name, flight_number) +SELECT DISTINCT customer_name, flight_number +FROM airline_database; + +SELECT * FROM bookings; + + +### + + +#EXERCISE 3 +SELECT COUNT(DISTINCT flight_number) FROM flights; +#4 + +SELECT AVG(flight_mileage) FROM flights; +#2087 + +SELECT AVG(total_aircraft_seats) FROM aircrafts; +#300 + +SELECT customer_status, AVG(customers.total_customer_mileage) FROM customers GROUP BY customer_status; +#Silver,85844.3333 +#None,4330.5000 +#Gold,205767.0000 + +SELECT customer_status, MAX(total_customer_mileage) FROM customers GROUP BY customer_status; +#Silver,127656 +#None,6008 +#Gold,205767 + +SELECT COUNT(*) FROM aircrafts WHERE aircraft LIKE '%Boeing%'; +#2 + +SELECT * FROM flights WHERE flight_mileage BETWEEN 300 AND 2000; +#DL222,Boeing 777,1765 + +SELECT c.customer_status, AVG(f.flight_mileage) +FROM bookings b + JOIN customers c ON b.customer_name = c.customer_name + JOIN flights f ON b.flight_number = f.flight_number +GROUP BY c.customer_status; +#Silver,1601.2500 +#None,2252.5000 +#Gold,2737.6667 + +SELECT a.aircraft, COUNT(*) AS total_bookings +FROM bookings b + JOIN customers c ON b.customer_name = c.customer_name + JOIN flights f ON b.flight_number = f.flight_number + JOIN aircrafts a ON f.aircraft = a.aircraft +WHERE c.customer_status = 'Gold' +GROUP BY a.aircraft +ORDER BY total_bookings DESC +LIMIT 1; +#Boeing 777, 2 \ No newline at end of file diff --git a/.idea/lab_week_6_commands b/.idea/lab_week_6_commands new file mode 100644 index 0000000..94a8908 --- /dev/null +++ b/.idea/lab_week_6_commands @@ -0,0 +1,41 @@ +CREATE DATABASE lab_week_6; +USE lab_week_6; + +CREATE TABLE exercise1_db ( + author VARCHAR(100), + title VARCHAR(255), + word_count INT, + views INT +); + +INSERT INTO exercise1_db (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', NULL, NULL); + +#authors +CREATE TABLE authors ( + author VARCHAR(100) PRIMARY KEY +) + +INSERT INTO authors (author) +SELECT DISTINCT author +FROM exercise1_db; + +#books +CREATE TABLE books ( + author VARCHAR(100), + title VARCHAR(255), + word_count INT, + views INT + FOREIGN KEY author REFERENCES authors(author) +) + +INSERT INTO authors (author, title, word_count, views) +SELECT DISTINCT author, title, word_count, views +FROM exercise1_db; + From b011e98b90fcb2a601ae31af985482411a9e4f6d Mon Sep 17 00:00:00 2001 From: stickymittens Date: Mon, 29 Sep 2025 13:52:24 +0200 Subject: [PATCH 2/2] lab week 6, sql --- ...commands => airline_database_commands.sql} | 0 .idea/lab_week_6_commands | 41 ----------------- .idea/lab_week_6_commands.sql | 45 +++++++++++++++++++ 3 files changed, 45 insertions(+), 41 deletions(-) rename .idea/{airline_database_commands => airline_database_commands.sql} (100%) delete mode 100644 .idea/lab_week_6_commands create mode 100644 .idea/lab_week_6_commands.sql diff --git a/.idea/airline_database_commands b/.idea/airline_database_commands.sql similarity index 100% rename from .idea/airline_database_commands rename to .idea/airline_database_commands.sql diff --git a/.idea/lab_week_6_commands b/.idea/lab_week_6_commands deleted file mode 100644 index 94a8908..0000000 --- a/.idea/lab_week_6_commands +++ /dev/null @@ -1,41 +0,0 @@ -CREATE DATABASE lab_week_6; -USE lab_week_6; - -CREATE TABLE exercise1_db ( - author VARCHAR(100), - title VARCHAR(255), - word_count INT, - views INT -); - -INSERT INTO exercise1_db (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', NULL, NULL); - -#authors -CREATE TABLE authors ( - author VARCHAR(100) PRIMARY KEY -) - -INSERT INTO authors (author) -SELECT DISTINCT author -FROM exercise1_db; - -#books -CREATE TABLE books ( - author VARCHAR(100), - title VARCHAR(255), - word_count INT, - views INT - FOREIGN KEY author REFERENCES authors(author) -) - -INSERT INTO authors (author, title, word_count, views) -SELECT DISTINCT author, title, word_count, views -FROM exercise1_db; - diff --git a/.idea/lab_week_6_commands.sql b/.idea/lab_week_6_commands.sql new file mode 100644 index 0000000..7eefd16 --- /dev/null +++ b/.idea/lab_week_6_commands.sql @@ -0,0 +1,45 @@ +CREATE DATABASE lab_week_6; +USE lab_week_6; + +CREATE TABLE exercise1_db ( + author VARCHAR(100), + title VARCHAR(255), + word_count INT, + views INT +); + +INSERT INTO exercise1_db (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', NULL, NULL); + +#authors +CREATE TABLE authors ( + author VARCHAR(100) PRIMARY KEY +); + +INSERT INTO authors (author) +SELECT DISTINCT author +FROM exercise1_db; + +SELECT * FROM authors; + +#books +CREATE TABLE books ( + author VARCHAR(100), + title VARCHAR(255), + word_count INT, + views INT, + PRIMARY KEY (author, title), + FOREIGN KEY (author) REFERENCES authors(author) +); + +INSERT INTO books (author, title, word_count, views) +SELECT DISTINCT author, title, word_count, views +FROM exercise1_db; + +SELECT * FROM books;