-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathsolution.sql
More file actions
137 lines (116 loc) · 3.93 KB
/
solution.sql
File metadata and controls
137 lines (116 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
-- 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(100) not null,
word_count int not null,
views int not null,
author_id int not null,
constraint fk_posts_author foreign key (author_id) references authors(id)
);
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,
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 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,
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, status, total_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(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(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 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.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 status;
-- 9.Most booked aircraft among Gold status members:
select a.aircraft_name, count(*) as total_booking
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.aircraft_name
order by total_booking desc
limit 1;