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..26509da --- /dev/null +++ b/your-code/task1.sql @@ -0,0 +1,47 @@ +-- I create the database here -- +CREATE DATABASE IF NOT EXISTS blogdb; + +-- Then I use this database -- +USE blogdb; + +-- 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 new file mode 100644 index 0000000..265c2d5 --- /dev/null +++ b/your-code/task2.sql @@ -0,0 +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 ( + 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, + 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 ( + 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 ( + 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) +); + +-- 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); + +-- 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); + +-- 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'); + +-- 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 new file mode 100644 index 0000000..fd5fed2 --- /dev/null +++ b/your-code/task3.sql @@ -0,0 +1,60 @@ +-- 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; + +-- 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; + +-- I get the average mileage of customers grouped by status here -- +SELECT + status, + AVG(total_mileage) AS avg_mileage +FROM customers +GROUP BY status; + +-- I get the max mileage of customers grouped by status here -- +SELECT + status, + MAX(total_mileage) AS max_mileage +FROM customers +GROUP BY status; + +-- I count the aircraft that have Boeing in their name here -- +SELECT COUNT(*) AS boeing_aircraft_count +FROM aircrafts +WHERE name LIKE '%Boeing%'; + +-- I get all flights with mileage between 300 and 2000 here -- +SELECT * +FROM flights +WHERE mileage BETWEEN 300 AND 2000; + +-- 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.id + JOIN flights f ON b.flight_number = f.flight_number +GROUP BY c.status; + +-- 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.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; \ No newline at end of file