From dbf811f30365b3018bf33840c95afeaefc2ac5e5 Mon Sep 17 00:00:00 2001 From: elcin Date: Mon, 30 Mar 2026 19:55:00 +0400 Subject: [PATCH 1/3] all task done --- solution.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solution.sql diff --git a/solution.sql b/solution.sql new file mode 100644 index 0000000..e69de29 From e6c3c06cc4dc41427c62a9c1684582334bf2b727 Mon Sep 17 00:00:00 2001 From: elcin Date: Mon, 30 Mar 2026 19:58:32 +0400 Subject: [PATCH 2/3] all task done --- solution.sql | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/solution.sql b/solution.sql index e69de29..2d5a901 100644 --- a/solution.sql +++ b/solution.sql @@ -0,0 +1,136 @@ +-- Exercise 1: Normalize a Blog Database + +create table if not exists authors( +id int auto_increment primary key, +name varchar(50) not null +); +create table if not exists posts( +id int auto_increment primary key, +title varchar(50) not null, +word_count int not null, +views int not null, +author_id int , +constraint fk_posts_author foreign key (author_id) references authors(id) +); +INSERT INTO authors (name) VALUES +('Ali Hasan'), +('Leyla Mammadova'); +INSERT INTO posts (title, word_count, views, author_id) VALUES +('Java Basics', 500, 120, 1), +('SQL Fundamentals', 650, 200, 2), +('Spring Boot Intro', 700, 150, 1); +select * from authors; + + +-- Exercise 2: Normalize an Airline Database + +create table if not exists customers( +id int auto_increment primary key, +customer_name varchar(50) not null, +status varchar(50) not null, +mileage int not null +); + +create table if not exists aircrafts( +id int auto_increment primary key, +aircraft_name varchar(50) not null, +seats int not null +); + +create table if not exists flights( +id int auto_increment primary key, +flight_number varchar(10) not null unique, +mileage int not null, +aircraft_id int not null, +constraint fk_flights_aircraft foreign key(aircraft_id) references aircrafts(id) +); + + +create table if not exists bookings ( +id int auto_increment primary key, +customer_id int not null, +constraint fk_bookings_customers foreign key(customer_id) references customers(id), +flight_id int not null, +constraint fk_bookings_flights foreign key(flight_number) references flights(flight_number) +); + + +INSERT INTO customers (customer_name, customer_status, mileage) VALUES +('Agustine Riviera', 'Silver', 115235), +('Alaina Sepulvida', 'None', 6008), +('Tom Jones', 'Gold', 205767), +('Sam Rio', 'None', 2653), +('Jessica James', 'Silver', 127656), +('Ana Janco', 'Silver', 136773), +('Jennifer Cortez', 'Gold', 300582), +('Christian Janco', 'Silver', 14642); + +INSERT INTO aircrafts (aircraft_name, seats) VALUES +('Boeing 747', 400), +('Airbus A330', 236), +('Boeing 777', 264); + + +INSERT INTO flights (flight_number, mileage, aircraft_id) VALUES +('DL143', 135, 1), +('DL122', 4370, 2), +('DL53', 2078, 3), +('DL222', 1765, 3), +('DL37', 531, 1); + +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'); + +-- Exercise 3: Write SQL Queries on the Airline Database +-- 1.Total number of flights: +select count(distinct flight_number) from flights; + +-- 2.Average flight distance: +select avg(milage) from flights; + +-- 3.Average number of seats per aircraft: +select avg(seats) from aircrafts; + +-- 4.Average miles flown by customers, grouped by status: +select status, avg(milage) from customers group by status; + +-- 5.Max miles flown by customers, grouped by status: +select status, max(milage) from customers group by status; + +-- 6.Number of aircrafts with "Boeing" in their name: +select count(*) from aircrafts where aircraft_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.milage) +from bookings b +join costumers c on b.customer_id = c.id +join flights f ON b.flight_number = f.flight_number +group by status; + +-- 9.Most booked aircraft among Gold status members: +select a.aircraft_name, count(*) as total_booking +from bookings b +join costumers c on b.cucustomer_id=c.id +join flights f ON b.flight_number = f.flight_number +join aircrafts a ON a.aircraft_id= a.id +where c.status = 'GOLD' +group by a.name +order by total_booking desc +limit 1; + + From 2479dfa1e35a6f9b0627a7be1ab6ad94bb3b50b3 Mon Sep 17 00:00:00 2001 From: elcin Date: Mon, 30 Mar 2026 20:28:13 +0400 Subject: [PATCH 3/3] all task fix and done --- solution.sql | 107 ++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/solution.sql b/solution.sql index 2d5a901..91db26c 100644 --- a/solution.sql +++ b/solution.sql @@ -6,56 +6,59 @@ name varchar(50) not null ); create table if not exists posts( id int auto_increment primary key, -title varchar(50) not null, +title varchar(100) not null, word_count int not null, views int not null, -author_id int , +author_id int not null, constraint fk_posts_author foreign key (author_id) references authors(id) ); -INSERT INTO authors (name) VALUES -('Ali Hasan'), -('Leyla Mammadova'); -INSERT INTO posts (title, word_count, views, author_id) VALUES -('Java Basics', 500, 120, 1), -('SQL Fundamentals', 650, 200, 2), -('Spring Boot Intro', 700, 150, 1); -select * from authors; +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); --- Exercise 2: Normalize an Airline Database -create table if not exists customers( -id int auto_increment primary key, -customer_name varchar(50) not null, -status varchar(50) not null, -mileage int not null +-- Exercise 2: Normalize an Airline Database +create table if not exists customers ( + id int auto_increment primary key, + customer_name varchar(50) not null, + status varchar(50) not null, + total_mileage int not null ); -create table if not exists aircrafts( -id int auto_increment primary key, -aircraft_name varchar(50) not null, -seats int not null +create table if not exists aircrafts ( + id int auto_increment primary key, + aircraft_name varchar(50) not null, + seats int not null ); -create table if not exists flights( -id int auto_increment primary key, -flight_number varchar(10) not null unique, -mileage int not null, -aircraft_id int not null, -constraint fk_flights_aircraft foreign key(aircraft_id) references aircrafts(id) +create table if not exists flights ( + id int auto_increment primary key, + flight_number varchar(10) not null unique, + mileage int not null, + aircraft_id int not null, + constraint fk_flights_aircraft + foreign key (aircraft_id) references aircrafts(id) ); - create table if not exists bookings ( -id int auto_increment primary key, -customer_id int not null, -constraint fk_bookings_customers foreign key(customer_id) references customers(id), -flight_id int not null, -constraint fk_bookings_flights foreign key(flight_number) references flights(flight_number) + id int auto_increment primary key, + customer_id int not null, + flight_number varchar(10) not null, + constraint fk_bookings_customers + foreign key (customer_id) references customers(id), + constraint fk_bookings_flights + foreign key (flight_number) references flights(flight_number) ); - -INSERT INTO customers (customer_name, customer_status, mileage) VALUES +insert into customers (customer_name, status, total_mileage) values ('Agustine Riviera', 'Silver', 115235), ('Alaina Sepulvida', 'None', 6008), ('Tom Jones', 'Gold', 205767), @@ -65,20 +68,19 @@ INSERT INTO customers (customer_name, customer_status, mileage) VALUES ('Jennifer Cortez', 'Gold', 300582), ('Christian Janco', 'Silver', 14642); -INSERT INTO aircrafts (aircraft_name, seats) VALUES +insert into aircrafts (aircraft_name, seats) values ('Boeing 747', 400), ('Airbus A330', 236), ('Boeing 777', 264); +insert into flights (flight_number, mileage, aircraft_id) values +('DL143', 135, 1), +('DL122', 4370, 2), +('DL53', 2078, 3), +('DL222', 1765, 3), +('DL37', 531, 1); -INSERT INTO flights (flight_number, mileage, aircraft_id) VALUES -('DL143', 135, 1), -('DL122', 4370, 2), -('DL53', 2078, 3), -('DL222', 1765, 3), -('DL37', 531, 1); - -INSERT INTO bookings (customer_id, flight_number) VALUES +insert into bookings (customer_id, flight_number) values (1, 'DL143'), (1, 'DL122'), (2, 'DL122'), @@ -93,21 +95,22 @@ INSERT INTO bookings (customer_id, flight_number) VALUES (7, 'DL222'), (8, 'DL222'); + -- Exercise 3: Write SQL Queries on the Airline Database -- 1.Total number of flights: select count(distinct flight_number) from flights; -- 2.Average flight distance: -select avg(milage) from flights; +select avg(mileage) from flights; -- 3.Average number of seats per aircraft: select avg(seats) from aircrafts; -- 4.Average miles flown by customers, grouped by status: -select status, avg(milage) from customers group by status; +select status, avg(total_mileage) from customers group by status; -- 5.Max miles flown by customers, grouped by status: -select status, max(milage) from customers group 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 aircraft_name like '%Boeing%'; @@ -116,20 +119,20 @@ select count(*) from aircrafts where aircraft_name like '%Boeing%'; select * from flights where mileage between 300 and 2000; -- 8.Average flight distance booked, grouped by customer status: -select c.status, avg(f.milage) +select c.status, avg(f.mileage) from bookings b -join costumers c on b.customer_id = c.id +join customers c on b.customer_id = c.id join flights f ON b.flight_number = f.flight_number group by status; -- 9.Most booked aircraft among Gold status members: select a.aircraft_name, count(*) as total_booking from bookings b -join costumers c on b.cucustomer_id=c.id +join customers c on b.customer_id =c.id join flights f ON b.flight_number = f.flight_number -join aircrafts a ON a.aircraft_id= a.id -where c.status = 'GOLD' -group by a.name +join aircrafts a ON f.aircraft_id= a.id +where c.status = 'Gold' +group by a.aircraft_name order by total_booking desc limit 1;