Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lab.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
CREATE DATABASE lab_SQL;

USE lab_SQL;
DROP TABLE authors;
DROP TABLE posts;
create TABLE authors(
author_id int primary key AUTO_INCREMENT,
name varchar(20) UNIQUE
);
INSERT INTO authors VALUES (1 ,'Maria Charlotte');
INSERT INTO authors VALUES (2 ,'Juan Perez');
INSERT INTO authors VALUES (3 ,'Gemma Alcocer');

SELECT *FROM authors;
CREATE TABLE posts(
post_id int primary key AUTO_INCREMENT,
title varchar(50) UNIQUE ,
word_count int,
views int,
author_id int,
FOREIGN KEY (author_id)REFERENCES authors(author_id)
);
INSERT INTO posts VALUES(1,'Best Paint Colors',814,14,1);
INSERT INTO posts VALUES(2,'Small Space Decorating Tips', 1146 , 221,2);
INSERT INTO posts VALUES (3,'Hot Accessories',986 , 105,1);
INSERT INTO posts VALUES (4,'Mixing Textures' , 765 , 22,1);
INSERT INTO posts VALUES (5,'Kitchen Refresh' , 1242 , 307,2);
INSERT INTO posts VALUES (6,'Homemade Art Hacks' , 1002 , 193,1);
INSERT INTO posts VALUES (7,'Refinishing Wood Floors' , 1571 , 7542,3);

SELECT *FROM posts;

CREATE TABLE customers(
customer_id int primary key AUTO_INCREMENT,
name varchar(40),
status varchar(20)
);
INSERT INTO customers (name, status) VALUES('Agustine Riviera', 'Silver');
INSERT INTO customers (name, status) VALUES ('Alaina Sepulvida' , 'None');
INSERT INTO customers (name, status) VALUES('Tom Jones', 'Gold');
INSERT INTO customers (name, status) VALUES('Sam Rio', 'None');
INSERT INTO customers (name, status) VALUES('Jessica James', 'Silver');
INSERT INTO customers (name, status) VALUES('Jennifer Cortez', 'Gold');
INSERT INTO customers (name, status) VALUES('Ana Janco', 'None');
INSERT INTO customers (name, status) VALUES('Christian Janco', 'Silver');

ALTER TABLE customers
ADD total_customer_mileage INT;
UPDATE customers SET total_customer_mileage = 115235 WHERE customer_id =1;
UPDATE customers SET total_customer_mileage = 6008 WHERE customer_id= 2;
UPDATE customers SET total_customer_mileage = 205767 WHERE customer_id = 3;
UPDATE customers SET total_customer_mileage = 2653 WHERE customer_id = 4;
UPDATE customers SET total_customer_mileage = 127656 WHERE customer_id = 5;
UPDATE customers SET total_customer_mileage = 300582 WHERE customer_id= 6;
UPDATE customers SET total_customer_mileage = 136773 WHERE customer_id= 7;
UPDATE customers SET total_customer_mileage = 14642 WHERE customer_id= 8;

;


CREATE TABLE flights(
flight_id int primary key AUTO_INCREMENT,
flight_number varchar(10),
flight_mileage int
);
INSERT INTO flights (flight_number,flight_mileage) VALUES('DL143',135);
INSERT INTO flights (flight_number,flight_mileage) VALUES('DL122',4370 );
INSERT INTO flights (flight_number,flight_mileage) VALUES('DL53',2078 );
INSERT INTO flights (flight_number,flight_mileage) VALUES('DL222',1765);
INSERT INTO flights (flight_number,flight_mileage) VALUES('DL37',531);

CREATE TABLE aircrafts(
aircraft_id int primary key AUTO_INCREMENT,
aircraft_name varchar(30),
total_aircraft_seats int
);

INSERT INTO aircrafts (aircraft_name, total_aircraft_seats) VALUES ('Boeing 747', 400);
INSERT INTO aircrafts (aircraft_name, total_aircraft_seats) VALUES ('Airbus A330', 236);
INSERT INTO aircrafts (aircraft_name, total_aircraft_seats) VALUES ('Boeing 777', 264);

DROP TABLE aircrafts;

CREATE TABLE bookings(
bookings_id int primary key AUTO_INCREMENT,
customer_id int,
flight_id int,
aircraft_id int,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (flight_id) REFERENCES flights(flight_id),
FOREIGN KEY (aircraft_id)REFERENCES aircrafts(aircraft_id)
);

INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(1,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(1,2,2);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(2,2,2);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(1,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(3,2,2);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(3,3,3);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(1,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(4,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(1,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(3,4,3);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(5,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(4,1,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(6,4,3);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(7,4,3);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(5,2,2);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(4,5,1);
INSERT INTO bookings(customer_id, flight_id, aircraft_id) VALUES(8,4,3);

SELECT *FROM bookings;

SELECT COUNT(DISTINCT flight_number) FROM flights;

SELECT AVG(flights.flight_mileage) FROM flights;

SELECT AVG(total_aircraft_seats) FROM aircrafts;

SELECT status, AVG(customers.total_customer_mileage) FROM customers GROUP BY status;

SELECT status, MAX(customers.total_customer_mileage) FROM customers GROUP BY status;

SELECT COUNT(*) FROM aircrafts WHERE aircrafts.aircraft_name LIKE '%Boeing%';

SELECT * FROM flights WHERE flights.flight_mileage BETWEEN 300 AND 2000;

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;


SELECT a.aircraft_name, 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 b.aircraft_id = a.aircraft_id
WHERE c.status = 'Gold'
GROUP BY a.aircraft_name
ORDER BY total_bookings DESC
LIMIT 1;