From 886924e3488b11be727f455ca7f16a963d438786 Mon Sep 17 00:00:00 2001 From: kenanqafarov Date: Tue, 31 Mar 2026 12:47:36 +0400 Subject: [PATCH 1/3] All done[Sorry, I have already done, but forgot to submit] --- .idea/.gitignore | 10 +++ ...lab-java-normalization-ddl-aggregation.iml | 9 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ your-code/task1.sql | 46 +++++++++++ your-code/task2.sql | 78 +++++++++++++++++++ your-code/task3.sql | 54 +++++++++++++ 8 files changed, 217 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 your-code/task1.sql create mode 100644 your-code/task2.sql create mode 100644 your-code/task3.sql diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ 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..a20905f --- /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/your-code/task1.sql b/your-code/task1.sql new file mode 100644 index 0000000..9276854 --- /dev/null +++ b/your-code/task1.sql @@ -0,0 +1,46 @@ +-- I created database here -- +CREATE DATABASE IF NOT EXISTS blogdb; + +-- Then I use this database -- +use blogdb; + + +-- I create authors table here -- +CREATE TABLE IF NOT EXISTS authors ( + author_id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL +); + +-- I create articles table heree -- +CREATE TABLE IF NOT EXISTS articles ( + article_id INT PRIMARY KEY, + author_id INT NOT NULL, + title VARCHAR(255) NOT NULL, + word_count INT NOT NULL, + views INT NOT NULL DEFAULT 0, + CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(author_id) +); + +-- Then we add some data to authors' table -- +INSERT INTO authors (author_id, name) VALUES + (1,'Maria Charlotte'), + (2,'Juan Perez'), + (3,'Kenan Gafarov'); + +-- Then we add some data to articles' table -- +INSERT INTO articles (article_id, author_id, title, word_count, views) VALUES + (1,3,'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); + + +-- We get all data to show in output from articles -- +SELECT * FROM articles; + +-- We get all data to show in output from authors -- +SELECT * FROM authors; + diff --git a/your-code/task2.sql b/your-code/task2.sql new file mode 100644 index 0000000..e31561f --- /dev/null +++ b/your-code/task2.sql @@ -0,0 +1,78 @@ +-- Aircrafts table +CREATE TABLE aircrafts ( + aircraft_id INT PRIMARY KEY, + model VARCHAR(50) NOT NULL, + total_seats INT NOT NULL +); + +-- Flights table +CREATE TABLE flights ( + flight_id INT PRIMARY KEY, + flight_number VARCHAR(10) NOT NULL UNIQUE, + aircraft_id INT NOT NULL, + flight_mileage INT NOT NULL, + CONSTRAINT fk_flight_aircraft + FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) +); + +-- Customers table +CREATE TABLE customers ( + customer_id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'None', + total_mileage INT NOT NULL DEFAULT 0 +); + +-- Bookings table +CREATE TABLE bookings ( + booking_id INT PRIMARY KEY, + customer_id INT NOT NULL, + flight_id INT NOT NULL, + CONSTRAINT fk_booking_customer + FOREIGN KEY (customer_id) REFERENCES customers(customer_id), + CONSTRAINT fk_booking_flight + FOREIGN KEY (flight_id) REFERENCES flights(flight_id), + CONSTRAINT uq_booking + UNIQUE (customer_id, flight_id) +); + + +-- Insert aircraft data +INSERT INTO aircrafts (aircraft_id, model, total_seats) VALUES + (1, 'Boeing 747', 400), + (2, 'Airbus A330', 236), + (3, 'Boeing 777', 264); + +-- Insert flight data +INSERT INTO flights (flight_id, flight_number, aircraft_id, flight_mileage) VALUES + (1, 'DL143', 1, 1351), + (2, 'DL122', 2, 4370), + (3, 'DL53', 3, 2078), + (4, 'DL222', 3, 1765), + (5, 'DL37', 1, 531); + +-- Insert customer data +INSERT INTO customers (customer_id, name, status, total_mileage) VALUES + (1, 'Agustine Riviera', 'Silver', 115235), + (2, 'Alaina Sepulvida', 'None', 6008), + (3, 'Tom Jones', 'Gold', 205767), + (4, 'Sam Rio', 'None', 2653), + (5, 'Jessica James', 'Silver', 127656), + (6, 'Ana Janco', 'Silver', 136773), + (7, 'Jennifer Cortez', 'Gold', 300582), + (8, 'Christian Janco', 'Silver', 14642); + +-- Insert booking data +INSERT INTO bookings (booking_id, customer_id, flight_id) VALUES (1, 1, 1), + (2, 1, 2), + (3, 2, 2), + (4, 3, 2), + (5, 3, 3), + (6, 3, 4), + (7, 4, 1), + (8, 4, 5), + (9, 5, 1), + (10, 5, 2), + (11, 6, 4), + (12, 7, 4), + (13, 8, 4); \ No newline at end of file diff --git a/your-code/task3.sql b/your-code/task3.sql new file mode 100644 index 0000000..b7748e3 --- /dev/null +++ b/your-code/task3.sql @@ -0,0 +1,54 @@ +-- 1. Total number of flights +SELECT COUNT(DISTINCT flight_number) FROM flights; + + +-- 2. Average flight distance +SELECT AVG(flight_mileage) FROM flights; + + +-- 3. Average number of seats per aircraft +SELECT AVG(total_seats) FROM aircrafts; + + +-- 4. Average miles flown by customers, grouped by status +SELECT status, AVG(total_mileage) +FROM customers +GROUP BY status; + + +-- 5. Max miles flown by customers, grouped by status +SELECT status, MAX(total_mileage) +FROM customers +GROUP BY status; + + +-- 6. Number of aircrafts with "Boeing" in their name +SELECT COUNT(*) +FROM aircrafts +WHERE model LIKE '%Boeing%'; + + +-- 7. Flights with distance between 300 and 2000 miles +SELECT * +FROM flights +WHERE flight_mileage BETWEEN 300 AND 2000; + + +-- 8. Average flight distance booked, grouped by customer status +SELECT c.status, AVG(f.flight_mileage) +FROM bookings b + JOIN customers c ON b.customer_id = c.customer_id + JOIN flights f ON b.flight_id = f.flight_id +GROUP BY c.status; + + +-- 9. Most booked aircraft among Gold status members +SELECT a.model, COUNT(*) AS total_bookings +FROM bookings b + JOIN customers c ON b.customer_id = c.customer_id + JOIN flights f ON b.flight_id = f.flight_id + JOIN aircrafts a ON f.aircraft_id = a.aircraft_id +WHERE c.status = 'Gold' +GROUP BY a.model +ORDER BY total_bookings DESC + LIMIT 1; \ No newline at end of file From c5c8d1b7a1e91a4652d559cfa15becd2fd926f32 Mon Sep 17 00:00:00 2001 From: kenanqafarov Date: Tue, 31 Mar 2026 12:51:44 +0400 Subject: [PATCH 2/3] All done[Sorry, I have already done, but forgot to submit] --- your-code/task1.sql | 65 ++++++++++++++++--------------------- your-code/task2.sql | 79 +++++++++++++++++++++------------------------ 2 files changed, 64 insertions(+), 80 deletions(-) diff --git a/your-code/task1.sql b/your-code/task1.sql index 9276854..99485f9 100644 --- a/your-code/task1.sql +++ b/your-code/task1.sql @@ -1,46 +1,35 @@ --- I created database here -- CREATE DATABASE IF NOT EXISTS blogdb; +USE blogdb; --- Then I use this database -- -use blogdb; - - --- I create authors table here -- CREATE TABLE IF NOT EXISTS authors ( - author_id INT PRIMARY KEY, - name VARCHAR(100) NOT NULL -); + author_id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL + ); --- I create articles table heree -- CREATE TABLE IF NOT EXISTS articles ( - article_id INT PRIMARY KEY, - author_id INT NOT NULL, - title VARCHAR(255) NOT NULL, + article_id INT AUTO_INCREMENT PRIMARY KEY, + author_id INT NOT NULL, + title VARCHAR(255) NOT NULL, word_count INT NOT NULL, - views INT NOT NULL DEFAULT 0, - CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(author_id) -); - --- Then we add some data to authors' table -- -INSERT INTO authors (author_id, name) VALUES - (1,'Maria Charlotte'), - (2,'Juan Perez'), - (3,'Kenan Gafarov'); + views INT NOT NULL DEFAULT 0, + + CONSTRAINT fk_author + FOREIGN KEY (author_id) REFERENCES authors(author_id) + ); + +INSERT INTO authors (name) VALUES + ('Maria Charlotte'), + ('Juan Perez'), + ('Kenan Gafarov'); + +INSERT INTO articles (author_id, title, word_count, views) VALUES + (3, '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); --- Then we add some data to articles' table -- -INSERT INTO articles (article_id, author_id, title, word_count, views) VALUES - (1,3,'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); - - --- We get all data to show in output from articles -- SELECT * FROM articles; - --- We get all data to show in output from authors -- -SELECT * FROM authors; - +SELECT * FROM authors; \ No newline at end of file diff --git a/your-code/task2.sql b/your-code/task2.sql index e31561f..f9bf864 100644 --- a/your-code/task2.sql +++ b/your-code/task2.sql @@ -1,57 +1,52 @@ --- Aircrafts table CREATE TABLE aircrafts ( - aircraft_id INT PRIMARY KEY, - model VARCHAR(50) NOT NULL, - total_seats INT NOT NULL + aircraft_id INT PRIMARY KEY, + model VARCHAR(50) NOT NULL, + total_seats INT NOT NULL ); --- Flights table CREATE TABLE flights ( - flight_id INT PRIMARY KEY, - flight_number VARCHAR(10) NOT NULL UNIQUE, + flight_number VARCHAR(10) PRIMARY KEY, aircraft_id INT NOT NULL, flight_mileage INT NOT NULL, + CONSTRAINT fk_flight_aircraft FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) ); --- Customers table CREATE TABLE customers ( - customer_id INT PRIMARY KEY, - name VARCHAR(100) NOT NULL, - status VARCHAR(20) NOT NULL DEFAULT 'None', - total_mileage INT NOT NULL DEFAULT 0 + customer_id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + status VARCHAR(20) NOT NULL DEFAULT 'None', + total_mileage INT NOT NULL DEFAULT 0 ); --- Bookings table CREATE TABLE bookings ( - booking_id INT PRIMARY KEY, - customer_id INT NOT NULL, - flight_id INT NOT NULL, + booking_id INT PRIMARY KEY, + customer_id INT NOT NULL, + flight_number VARCHAR(10) NOT NULL, + CONSTRAINT fk_booking_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id), + CONSTRAINT fk_booking_flight - FOREIGN KEY (flight_id) REFERENCES flights(flight_id), + FOREIGN KEY (flight_number) REFERENCES flights(flight_number), + CONSTRAINT uq_booking - UNIQUE (customer_id, flight_id) + UNIQUE (customer_id, flight_number) ); - --- Insert aircraft data INSERT INTO aircrafts (aircraft_id, model, total_seats) VALUES (1, 'Boeing 747', 400), (2, 'Airbus A330', 236), (3, 'Boeing 777', 264); --- Insert flight data -INSERT INTO flights (flight_id, flight_number, aircraft_id, flight_mileage) VALUES - (1, 'DL143', 1, 1351), - (2, 'DL122', 2, 4370), - (3, 'DL53', 3, 2078), - (4, 'DL222', 3, 1765), - (5, 'DL37', 1, 531); +INSERT INTO flights (flight_number, aircraft_id, flight_mileage) VALUES + ('DL143', 1, 1351), + ('DL122', 2, 4370), + ('DL53', 3, 2078), + ('DL222', 3, 1765), + ('DL37', 1, 531); --- Insert customer data INSERT INTO customers (customer_id, name, status, total_mileage) VALUES (1, 'Agustine Riviera', 'Silver', 115235), (2, 'Alaina Sepulvida', 'None', 6008), @@ -62,17 +57,17 @@ INSERT INTO customers (customer_id, name, status, total_mileage) VALUES (7, 'Jennifer Cortez', 'Gold', 300582), (8, 'Christian Janco', 'Silver', 14642); --- Insert booking data -INSERT INTO bookings (booking_id, customer_id, flight_id) VALUES (1, 1, 1), - (2, 1, 2), - (3, 2, 2), - (4, 3, 2), - (5, 3, 3), - (6, 3, 4), - (7, 4, 1), - (8, 4, 5), - (9, 5, 1), - (10, 5, 2), - (11, 6, 4), - (12, 7, 4), - (13, 8, 4); \ No newline at end of file +INSERT INTO bookings (booking_id, customer_id, flight_number) VALUES + (1, 1, 'DL143'), + (2, 1, 'DL122'), + (3, 2, 'DL122'), + (4, 3, 'DL122'), + (5, 3, 'DL53'), + (6, 3, 'DL222'), + (7, 4, 'DL143'), + (8, 4, 'DL37'), + (9, 5, 'DL143'), + (10,5, 'DL122'), + (11,6, 'DL222'), + (12,7, 'DL222'), + (13,8, 'DL222'); \ No newline at end of file From 4771b66e0dcee0f2c4dbc4311d3f638432c1dce3 Mon Sep 17 00:00:00 2001 From: kenanqafarov Date: Tue, 31 Mar 2026 12:59:13 +0400 Subject: [PATCH 3/3] All done[Sorry, I have already done, but forgot to submit] --- your-code/task1.sql | 74 ++++++++++++++---------- your-code/task2.sql | 135 +++++++++++++++++++++++++------------------- your-code/task3.sql | 70 ++++++++++++----------- 3 files changed, 158 insertions(+), 121 deletions(-) diff --git a/your-code/task1.sql b/your-code/task1.sql index 99485f9..26509da 100644 --- a/your-code/task1.sql +++ b/your-code/task1.sql @@ -1,35 +1,47 @@ +-- I create the database here -- CREATE DATABASE IF NOT EXISTS blogdb; + +-- Then I use this database -- USE blogdb; -CREATE TABLE IF NOT EXISTS authors ( - author_id INT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(100) NOT NULL - ); - -CREATE TABLE IF NOT EXISTS articles ( - article_id INT AUTO_INCREMENT PRIMARY KEY, - author_id INT NOT NULL, - title VARCHAR(255) NOT NULL, - word_count INT NOT NULL, - views INT NOT NULL DEFAULT 0, - - CONSTRAINT fk_author - FOREIGN KEY (author_id) REFERENCES authors(author_id) - ); - -INSERT INTO authors (name) VALUES - ('Maria Charlotte'), - ('Juan Perez'), - ('Kenan Gafarov'); - -INSERT INTO articles (author_id, title, word_count, views) VALUES - (3, '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); - -SELECT * FROM articles; +-- I drop tables first to avoid conflicts on re-run -- +DROP TABLE IF EXISTS posts; +DROP TABLE IF EXISTS authors; + +-- I create the authors table here -- +CREATE TABLE authors ( + id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL UNIQUE +); + +-- I create the posts table here -- +CREATE TABLE posts ( + id INT PRIMARY KEY, + author_id INT NOT NULL, + title VARCHAR(255) NOT NULL UNIQUE, + word_count INT NOT NULL CHECK (word_count > 0), + views INT NOT NULL DEFAULT 0 CHECK (views >= 0), + CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id) +); + +-- Then I add some data to the authors table -- +INSERT INTO authors (id, name) VALUES + (1, 'Maria Charlotte'), + (2, 'Juan Perez'), + (3, 'Gemma Alcocer'); + +-- Then I add some data to the posts table -- +INSERT INTO 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); + +-- I get all data from the posts table -- +SELECT * FROM posts; + +-- I get all data from the authors table -- SELECT * FROM authors; \ No newline at end of file diff --git a/your-code/task2.sql b/your-code/task2.sql index f9bf864..265c2d5 100644 --- a/your-code/task2.sql +++ b/your-code/task2.sql @@ -1,73 +1,92 @@ +-- I create the database here -- +CREATE DATABASE IF NOT EXISTS airlinedb; + +-- Then I use this database -- +USE airlinedb; + +-- I drop all tables first to avoid conflicts on re-run -- +DROP TABLE IF EXISTS bookings; +DROP TABLE IF EXISTS flights; +DROP TABLE IF EXISTS aircrafts; +DROP TABLE IF EXISTS customers; + +-- I create the aircrafts table here -- CREATE TABLE aircrafts ( - aircraft_id INT PRIMARY KEY, - model VARCHAR(50) NOT NULL, - total_seats INT NOT NULL + id INT PRIMARY KEY, + name VARCHAR(100) NOT NULL UNIQUE, + total_seats INT NOT NULL CHECK (total_seats > 0) ); +-- I create the flights table here -- CREATE TABLE flights ( - flight_number VARCHAR(10) PRIMARY KEY, - aircraft_id INT NOT NULL, - flight_mileage INT NOT NULL, - - CONSTRAINT fk_flight_aircraft - FOREIGN KEY (aircraft_id) REFERENCES aircrafts(aircraft_id) + flight_number VARCHAR(10) PRIMARY KEY, + aircraft_id INT NOT NULL, + mileage INT NOT NULL CHECK (mileage > 0), + CONSTRAINT fk_aircraft FOREIGN KEY (aircraft_id) REFERENCES aircrafts(id) ); +-- I create the customers table here -- CREATE TABLE customers ( - customer_id INT PRIMARY KEY, - name VARCHAR(100) NOT NULL, - status VARCHAR(20) NOT NULL DEFAULT 'None', - total_mileage INT NOT NULL DEFAULT 0 + id INT PRIMARY KEY, + name VARCHAR(150) NOT NULL UNIQUE, + status VARCHAR(20) NOT NULL DEFAULT 'None' + CHECK (status IN ('None', 'Silver', 'Gold')), + total_mileage INT NOT NULL DEFAULT 0 CHECK (total_mileage >= 0) ); +-- I create the bookings table here -- CREATE TABLE bookings ( - booking_id INT PRIMARY KEY, - customer_id INT NOT NULL, - flight_number VARCHAR(10) NOT NULL, - - CONSTRAINT fk_booking_customer - FOREIGN KEY (customer_id) REFERENCES customers(customer_id), - - CONSTRAINT fk_booking_flight - FOREIGN KEY (flight_number) REFERENCES flights(flight_number), - - CONSTRAINT uq_booking - UNIQUE (customer_id, flight_number) + id INT PRIMARY KEY, + customer_id INT NOT NULL, + flight_number VARCHAR(10) NOT NULL, + CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id), + CONSTRAINT fk_flight FOREIGN KEY (flight_number) REFERENCES flights(flight_number), + UNIQUE (customer_id, flight_number) ); -INSERT INTO aircrafts (aircraft_id, model, total_seats) VALUES - (1, 'Boeing 747', 400), - (2, 'Airbus A330', 236), - (3, 'Boeing 777', 264); +-- Then I add some data to the aircrafts table -- +INSERT INTO aircrafts (id, name, total_seats) VALUES + (1, 'Boeing 747', 400), + (2, 'Airbus A330', 236), + (3, 'Boeing 777', 264); + +-- Then I add some data to the flights table -- +INSERT INTO flights (flight_number, aircraft_id, mileage) VALUES + ('DL143', 1, 1351), + ('DL122', 2, 4370), + ('DL53', 3, 2078), + ('DL222', 3, 1765), + ('DL37', 1, 531); -INSERT INTO flights (flight_number, aircraft_id, flight_mileage) VALUES - ('DL143', 1, 1351), - ('DL122', 2, 4370), - ('DL53', 3, 2078), - ('DL222', 3, 1765), - ('DL37', 1, 531); +-- Then I add some data to the customers table -- +INSERT INTO customers (id, name, status, total_mileage) VALUES + (1, 'Agustine Riviera', 'Silver', 115235), + (2, 'Alaina Sepulvida', 'None', 6008), + (3, 'Tom Jones', 'Gold', 205767), + (4, 'Sam Rio', 'None', 2653), + (5, 'Jessica James', 'Silver', 127656), + (6, 'Ana Janco', 'Silver', 136773), + (7, 'Jennifer Cortez', 'Gold', 300582), + (8, 'Christian Janco', 'Silver', 14642); -INSERT INTO customers (customer_id, name, status, total_mileage) VALUES - (1, 'Agustine Riviera', 'Silver', 115235), - (2, 'Alaina Sepulvida', 'None', 6008), - (3, 'Tom Jones', 'Gold', 205767), - (4, 'Sam Rio', 'None', 2653), - (5, 'Jessica James', 'Silver', 127656), - (6, 'Ana Janco', 'Silver', 136773), - (7, 'Jennifer Cortez', 'Gold', 300582), - (8, 'Christian Janco', 'Silver', 14642); +-- Then I add some data to the bookings table -- +INSERT INTO bookings (id, customer_id, flight_number) VALUES + (1, 1, 'DL143'), + (2, 1, 'DL122'), + (3, 2, 'DL122'), + (4, 3, 'DL122'), + (5, 3, 'DL53'), + (6, 3, 'DL222'), + (7, 4, 'DL143'), + (8, 4, 'DL37'), + (9, 5, 'DL143'), + (10, 5, 'DL122'), + (11, 6, 'DL222'), + (12, 7, 'DL222'), + (13, 8, 'DL222'); -INSERT INTO bookings (booking_id, customer_id, flight_number) VALUES - (1, 1, 'DL143'), - (2, 1, 'DL122'), - (3, 2, 'DL122'), - (4, 3, 'DL122'), - (5, 3, 'DL53'), - (6, 3, 'DL222'), - (7, 4, 'DL143'), - (8, 4, 'DL37'), - (9, 5, 'DL143'), - (10,5, 'DL122'), - (11,6, 'DL222'), - (12,7, 'DL222'), - (13,8, 'DL222'); \ No newline at end of file +-- I get all data from each table to verify the inserts -- +SELECT * FROM aircrafts; +SELECT * FROM flights; +SELECT * FROM customers; +SELECT * FROM bookings; \ No newline at end of file diff --git a/your-code/task3.sql b/your-code/task3.sql index b7748e3..fd5fed2 100644 --- a/your-code/task3.sql +++ b/your-code/task3.sql @@ -1,54 +1,60 @@ --- 1. Total number of flights -SELECT COUNT(DISTINCT flight_number) FROM flights; +-- I use the airline database here -- +USE airlinedb; +-- I get the total number of distinct flights here -- +SELECT COUNT(DISTINCT flight_number) AS total_flights +FROM flights; --- 2. Average flight distance -SELECT AVG(flight_mileage) FROM flights; +-- I get the average flight distance here -- +SELECT AVG(mileage) AS avg_flight_distance +FROM flights; +-- I get the average number of seats per aircraft here -- +SELECT AVG(total_seats) AS avg_seats_per_aircraft +FROM aircrafts; --- 3. Average number of seats per aircraft -SELECT AVG(total_seats) FROM aircrafts; - - --- 4. Average miles flown by customers, grouped by status -SELECT status, AVG(total_mileage) +-- I get the average mileage of customers grouped by status here -- +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) +-- I get the max mileage of customers grouped by status here -- +SELECT + status, + MAX(total_mileage) AS max_mileage FROM customers GROUP BY status; - --- 6. Number of aircrafts with "Boeing" in their name -SELECT COUNT(*) +-- I count the aircraft that have Boeing in their name here -- +SELECT COUNT(*) AS boeing_aircraft_count FROM aircrafts -WHERE model LIKE '%Boeing%'; +WHERE name LIKE '%Boeing%'; - --- 7. Flights with distance between 300 and 2000 miles +-- I get all flights with mileage between 300 and 2000 here -- SELECT * FROM flights -WHERE flight_mileage BETWEEN 300 AND 2000; - +WHERE mileage BETWEEN 300 AND 2000; --- 8. Average flight distance booked, grouped by customer status -SELECT c.status, AVG(f.flight_mileage) +-- I get the average booked flight distance grouped by customer status here -- +SELECT + c.status, + AVG(f.mileage) AS avg_booked_distance FROM bookings b - JOIN customers c ON b.customer_id = c.customer_id - JOIN flights f ON b.flight_id = f.flight_id + 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.model, COUNT(*) AS total_bookings +-- I get the most booked aircraft among Gold customers here -- +SELECT + a.name AS aircraft, + COUNT(*) AS total_bookings FROM bookings b - JOIN customers c ON b.customer_id = c.customer_id - JOIN flights f ON b.flight_id = f.flight_id - JOIN aircrafts a ON f.aircraft_id = a.aircraft_id + 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.model +GROUP BY a.name ORDER BY total_bookings DESC LIMIT 1; \ No newline at end of file