diff --git a/SQL_exercise_01/1_build_schema.sql b/SQL_exercise_01/1_build_schema.sql index 680d9ca..a96ceed 100644 --- a/SQL_exercise_01/1_build_schema.sql +++ b/SQL_exercise_01/1_build_schema.sql @@ -1,4 +1,6 @@ -- LINK: https://en.wikibooks.org/wiki/SQL_Exercises/The_computer_store +CREATE DATABASE sql_exercises_1; +USE sql_exercises_2; CREATE TABLE Manufacturers ( Code INTEGER, diff --git a/SQL_exercise_01/1_questions_and_solutions.sql b/SQL_exercise_01/1_questions_and_solutions.sql index d676e41..e4a4b84 100644 --- a/SQL_exercise_01/1_questions_and_solutions.sql +++ b/SQL_exercise_01/1_questions_and_solutions.sql @@ -82,7 +82,7 @@ SELECT name,price ORDER BY price ASC LIMIT 1; ---SQL SERVER SOLUTION (T-SQL) +-- SQL SERVER SOLUTION (T-SQL) SELECT TOP 1 name ,price FROM Products diff --git a/SQL_exercise_02/2_build_schema.sql b/SQL_exercise_02/2_build_schema.sql index 8164c8c..6807c13 100644 --- a/SQL_exercise_02/2_build_schema.sql +++ b/SQL_exercise_02/2_build_schema.sql @@ -1,7 +1,10 @@ -- LINK : https://en.wikibooks.org/wiki/SQL_Exercises/Employee_management ------------------------------------------ +-- ----------------------------------------- -- Build the Schema ------------------------------------------ +-- ----------------------------------------- + +CREATE DATABASE sql_exercises_2; +USE sql_exercises_2; CREATE TABLE Departments ( Code INTEGER PRIMARY KEY, diff --git a/SQL_exercise_03/3_build_schema.sql b/SQL_exercise_03/3_build_schema.sql index 3a7db30..f140601 100644 --- a/SQL_exercise_03/3_build_schema.sql +++ b/SQL_exercise_03/3_build_schema.sql @@ -1,6 +1,9 @@ -- The Warehouse -- lINK: https://en.wikibooks.org/wiki/SQL_Exercises/The_warehouse +CREATE DATABASE sql_exercises_3; +USE sql_exercises_3; + CREATE TABLE Warehouses ( Code INTEGER NOT NULL, Location VARCHAR(255) NOT NULL , diff --git a/SQL_exercise_03/3_questions.sql b/SQL_exercise_03/3_questions.sql index ae22310..de8f02c 100644 --- a/SQL_exercise_03/3_questions.sql +++ b/SQL_exercise_03/3_questions.sql @@ -1,20 +1,20 @@ -- The Warehouse -- lINK: https://en.wikibooks.org/wiki/SQL_Exercises/The_warehouse ---3.1 Select all warehouses. ---3.2 Select all boxes with a value larger than $150. ---3.3 Select all distinct contents in all the boxes. ---3.4 Select the average value of all the boxes. ---3.5 Select the warehouse code and the average value of the boxes in each warehouse. ---3.6 Same as previous exercise, but select only those warehouses where the average value of the boxes is greater than 150. ---3.7 Select the code of each box, along with the name of the city the box is located in. ---3.8 Select the warehouse codes, along with the number of boxes in each warehouse. +-- 3.1 Select all warehouses. +-- 3.2 Select all boxes with a value larger than $150. +-- 3.3 Select all distinct contents in all the boxes. +-- 3.4 Select the average value of all the boxes. +-- 3.5 Select the warehouse code and the average value of the boxes in each warehouse. +-- 3.6 Same as previous exercise, but select only those warehouses where the average value of the boxes is greater than 150. +-- 3.7 Select the code of each box, along with the name of the city the box is located in. +-- 3.8 Select the warehouse codes, along with the number of boxes in each warehouse. -- Optionally, take into account that some warehouses are empty (i.e., the box count should show up as zero, instead of omitting the warehouse from the result). ---3.9 Select the codes of all warehouses that are saturated (a warehouse is saturated if the number of boxes in it is larger than the warehouse's capacity). ---3.10 Select the codes of all the boxes located in Chicago. ---3.11 Create a new warehouse in New York with a capacity for 3 boxes. ---3.12 Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in warehouse 2. ---3.13 Reduce the value of all boxes by 15%. ---3.14 Remove all boxes with a value lower than $100. +-- 3.9 Select the codes of all warehouses that are saturated (a warehouse is saturated if the number of boxes in it is larger than the warehouse's capacity). +-- 3.10 Select the codes of all the boxes located in Chicago. +-- 3.11 Create a new warehouse in New York with a capacity for 3 boxes. +-- 3.12 Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in warehouse 2. +-- 3.13 Reduce the value of all boxes by 15%. +-- 3.14 Remove all boxes with a value lower than $100. -- 3.15 Remove all boxes from saturated warehouses. -- 3.16 Add Index for column "Warehouse" in table "boxes" -- !!!NOTE!!!: index should NOT be used on small tables in practice diff --git a/SQL_exercise_03/3_questions_and_solutions.sql b/SQL_exercise_03/3_questions_and_solutions.sql index 0890ad0..df5ae07 100644 --- a/SQL_exercise_03/3_questions_and_solutions.sql +++ b/SQL_exercise_03/3_questions_and_solutions.sql @@ -1,23 +1,23 @@ -- The Warehouse -- lINK: https://en.wikibooks.org/wiki/SQL_Exercises/The_warehouse ---3.1 +-- 3.1 -- Select all warehouses. select * from warehouses; ---3.2 +-- 3.2 -- Select all boxes with a value larger than $150. select * from boxes where Value>150; ---3.3 +-- 3.3 -- Select all distinct contents in all the boxes. select distinct contents from boxes; ---3.4 +-- 3.4 -- Select the average value of all the boxes. select avg(value) from boxes; ---3.5 +-- 3.5 -- Select the warehouse code and the average value of the boxes in each warehouse. select warehouse, avg(value) from boxes group by warehouse; @@ -25,7 +25,7 @@ SELECT Warehouse, AVG(Value) FROM Boxes GROUP BY Warehouse; ---3.6 +-- 3.6 -- Same as previous exercise, but select only those warehouses where the average value of the boxes is greater than 150. select warehouse, avg(value) from boxes @@ -33,7 +33,7 @@ group by warehouse having avg(value)> 150; ---3.7 +-- 3.7 -- Select the code of each box, along with the name of the city the box is located in. select boxes.code, warehouses.location from boxes join warehouses @@ -44,7 +44,7 @@ SELECT Boxes.Code, Location INNER JOIN Boxes ON Warehouses.Code = Boxes.Warehouse; ---3.8 +-- 3.8 -- Select the warehouse codes, along with the number of boxes in each warehouse. -- Optionally, take into account that some warehouses are empty (i.e., the box count should show up as zero, instead of omitting the warehouse from the result). select Warehouse, count(*) @@ -52,7 +52,7 @@ from boxes group by warehouse; ---3.9 +-- 3.9 -- Select the codes of all warehouses that are saturated (a warehouse is saturated if the number of boxes in it is larger than the warehouse's capacity). select Code from warehouses join (select warehouse temp_a, count(*) temp_b from boxes group by warehouse) temp @@ -71,7 +71,7 @@ SELECT Code ---3.10 +-- 3.10 -- Select the codes of all the boxes located in Chicago. select Boxes.code @@ -96,23 +96,23 @@ where warehouses.location = 'Chicago'; ); ---3.11 +-- 3.11 -- Create a new warehouse in New York with a capacity for 3 boxes. INSERT INTO Warehouses VALUES (6, 'New York', 3); ---3.12 +-- 3.12 -- Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in warehouse 2. INSERT INTO Boxes VALUES('H5RT', 'Papers', 200, 2); ---3.13 +-- 3.13 -- Reduce the value of all boxes by 15%. update boxes set value = value * 0.85; ---3.14 +-- 3.14 -- Remove all boxes with a value lower than $100. delete from boxes where value < 100; diff --git a/SQL_exercise_04/4_build_schema.sql b/SQL_exercise_04/4_build_schema.sql index e32bfe7..dd0a7c2 100644 --- a/SQL_exercise_04/4_build_schema.sql +++ b/SQL_exercise_04/4_build_schema.sql @@ -1,4 +1,6 @@ -- https://en.wikibooks.org/wiki/SQL_Exercises/Movie_theatres +CREATE DATABASE sql_exercises_4; +USE sql_exercises_4; CREATE TABLE Movies ( Code INTEGER PRIMARY KEY, diff --git a/SQL_exercise_05/5_build_schema.sql b/SQL_exercise_05/5_build_schema.sql index 677314a..1e9ce7f 100644 --- a/SQL_exercise_05/5_build_schema.sql +++ b/SQL_exercise_05/5_build_schema.sql @@ -1,5 +1,8 @@ -- https://en.wikibooks.org/wiki/SQL_Exercises/Pieces_and_providers +CREATE DATABASE sql_exercises_5; +USE sql_exercises_5; + CREATE TABLE Pieces ( Code INTEGER PRIMARY KEY NOT NULL, Name TEXT NOT NULL diff --git a/SQL_exercise_06/6_build_schema.sql b/SQL_exercise_06/6_build_schema.sql index ae63e2d..9714377 100644 --- a/SQL_exercise_06/6_build_schema.sql +++ b/SQL_exercise_06/6_build_schema.sql @@ -1,3 +1,6 @@ +CREATE DATABASE sql_exercises_6; +USE sql_exercises_6; + create table Scientists ( SSN int, Name Char(30) not null, diff --git a/SQL_exercise_07/7_build_schema.sql b/SQL_exercise_07/7_build_schema.sql index 75b2833..65156b6 100644 --- a/SQL_exercise_07/7_build_schema.sql +++ b/SQL_exercise_07/7_build_schema.sql @@ -1,5 +1,8 @@ -- https://en.wikibooks.org/wiki/SQL_Exercises/Planet_Express +CREATE DATABASE sql_exercises_7; +USE sql_exercises_7; + CREATE TABLE Employee ( EmployeeID INTEGER PRIMARY KEY, Name VARCHAR(255) NOT NULL, diff --git a/SQL_exercise_08/8_build_schema.sql b/SQL_exercise_08/8_build_schema.sql index ca660db..2343a09 100644 --- a/SQL_exercise_08/8_build_schema.sql +++ b/SQL_exercise_08/8_build_schema.sql @@ -1,5 +1,8 @@ -- https://en.wikibooks.org/wiki/SQL_Exercises/The_Hospital +CREATE DATABASE sql_exercises_8; +USE sql_exercises_8; + DROP TABLE IF EXISTS Physician; CREATE TABLE Physician ( EmployeeID INTEGER NOT NULL, diff --git a/SQL_exercise_10/10_build_schema.sql b/SQL_exercise_10/10_build_schema.sql index 2933fbf..477f56d 100644 --- a/SQL_exercise_10/10_build_schema.sql +++ b/SQL_exercise_10/10_build_schema.sql @@ -1,5 +1,8 @@ -- Used SQLite3 for this example +CREATE DATABASE sql_exercises_10; +USE sql_exercises_10; + -- table PEOPLE: containing unique ID and corresponding names. CREATE TABLE PEOPLE (id INTEGER, name CHAR);