From e4d2ef9ca205e1dfff9cfa3dfd6fa9d98d6085dc Mon Sep 17 00:00:00 2001 From: Capumaf Date: Sat, 14 Jun 2025 02:28:41 +0200 Subject: [PATCH 1/2] Lab normalization ddl and aggregation done --- .idea/.gitignore | 10 +++++ ...lab-java-normalization-ddl-aggregation.iml | 9 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ Solutions/airline_data.sql | 20 ++++++++++ Solutions/airline_queries.sql | 38 +++++++++++++++++++ Solutions/airline_schema.sql | 27 +++++++++++++ Solutions/blog_schema.sql | 13 +++++++ 9 files changed, 137 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-normalization-ddl-aggregation.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Solutions/airline_data.sql create mode 100644 Solutions/airline_queries.sql create mode 100644 Solutions/airline_schema.sql create mode 100644 Solutions/blog_schema.sql diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/lab-java-normalization-ddl-aggregation.iml b/.idea/lab-java-normalization-ddl-aggregation.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/lab-java-normalization-ddl-aggregation.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f03c948 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..152076a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Solutions/airline_data.sql b/Solutions/airline_data.sql new file mode 100644 index 0000000..1211d91 --- /dev/null +++ b/Solutions/airline_data.sql @@ -0,0 +1,20 @@ +INSERT INTO customers (id, name, status, total_mileage) VALUES + (1, 'Agustine Riviera', 'Silver', 115235), + (2, 'Alaina Sepulvida', NULL, 6008), + (3, 'Tom Jones', 'Gold', 205767); + +INSERT INTO aircrafts (id, name, total_seats) VALUES + (1, 'Boeing 747', 400), + (2, 'Airbus A330', 236), + (3, 'Boeing 777', 264); + +INSERT INTO flights (flight_number, aircraft_id, mileage) VALUES + ('DL143', 1, 135), + ('DL122', 2, 4370), + ('DL53', 3, 2078); + +INSERT INTO bookings (id, customer_id, flight_number) VALUES + (1, 1, 'DL143'), + (2, 1, 'DL122'), + (3, 2, 'DL122'), + (4, 3, 'DL53'); diff --git a/Solutions/airline_queries.sql b/Solutions/airline_queries.sql new file mode 100644 index 0000000..3122732 --- /dev/null +++ b/Solutions/airline_queries.sql @@ -0,0 +1,38 @@ +-- Total number of flights +SELECT COUNT(DISTINCT flight_number) FROM flights; + +-- Average flight distance +SELECT AVG(mileage) FROM flights; + +-- Average number of seats per aircraft +SELECT AVG(total_seats) FROM aircrafts; + +-- Average miles flown by customers, grouped by status +SELECT status, AVG(total_mileage) FROM customers GROUP BY status; + +-- Max miles flown by customers, grouped by status +SELECT status, MAX(total_mileage) FROM customers GROUP BY status; + +-- Number of aircrafts with "Boeing" in their name +SELECT COUNT(*) FROM aircrafts WHERE name LIKE '%Boeing%'; + +-- Flights with distance between 300 and 2000 miles +SELECT * FROM flights WHERE mileage BETWEEN 300 AND 2000; + +-- Average flight distance booked, grouped by customer status +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; + +-- 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; diff --git a/Solutions/airline_schema.sql b/Solutions/airline_schema.sql new file mode 100644 index 0000000..5255000 --- /dev/null +++ b/Solutions/airline_schema.sql @@ -0,0 +1,27 @@ +CREATE TABLE customers ( + id INT PRIMARY KEY, + name VARCHAR(100), + status VARCHAR(20), + total_mileage INT +); + +CREATE TABLE aircrafts ( + id INT PRIMARY KEY, + name VARCHAR(100), + total_seats INT +); + +CREATE TABLE flights ( + flight_number VARCHAR(10) PRIMARY KEY, + aircraft_id INT, + mileage INT, + FOREIGN KEY (aircraft_id) REFERENCES aircrafts(id) +); + +CREATE TABLE bookings ( + id INT PRIMARY KEY, + customer_id INT, + flight_number VARCHAR(10), + FOREIGN KEY (customer_id) REFERENCES customers(id), + FOREIGN KEY (flight_number) REFERENCES flights(flight_number) +); diff --git a/Solutions/blog_schema.sql b/Solutions/blog_schema.sql new file mode 100644 index 0000000..d2c7905 --- /dev/null +++ b/Solutions/blog_schema.sql @@ -0,0 +1,13 @@ +CREATE TABLE authors ( + id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL +); + +CREATE TABLE blog_posts ( + id INT PRIMARY KEY, + author_id INT, + title VARCHAR(255), + word_count INT, + views INT, + FOREIGN KEY (author_id) REFERENCES authors(id) +); From c6df82d3505cab3c3d6fcb22e6380d95b876223d Mon Sep 17 00:00:00 2001 From: Capumaf Date: Sat, 14 Jun 2025 02:36:11 +0200 Subject: [PATCH 2/2] Agregando ERDs y actualizando soluciones SQL --- Solutions/blog_data.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Solutions/blog_data.sql diff --git a/Solutions/blog_data.sql b/Solutions/blog_data.sql new file mode 100644 index 0000000..377983e --- /dev/null +++ b/Solutions/blog_data.sql @@ -0,0 +1,15 @@ +-- Tabla authors +INSERT INTO authors (id, name) VALUES + (1, 'Maria Charlotte'), + (2, 'Juan Perez'), + (3, 'Gemma Alcocer'); + +-- Tabla blog_posts +INSERT INTO blog_posts (id, author_id, title, word_count, views) VALUES + (1, 1, 'Best Paint Colors', 814, 14), + (2, 2, 'Small Space Decorating Tips', 1146, 221), + (3, 1, 'Hot Accessories', 986, 105), + (4, 1, 'Mixing Textures', 765, 22), + (5, 2, 'Kitchen Refresh', 1242, 307), + (6, 1, 'Homemade Art Hacks', 1002, 193), + (7, 3, 'Refinishing Wood Floors', 1571, 7542);