Skip to content

Fixed incorrect comments and added database creation to all build_sch… #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions SQL_exercise_01/1_build_schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- LINK: https://en.wikibooks.org/wiki/SQL_Exercises/The_computer_store
CREATE DATABASE sql_exercises_1;
USE sql_exercises_2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cberry216 , seems this line is inconsistent with the line 2. I believe it's a typo?


CREATE TABLE Manufacturers (
Code INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion SQL_exercise_01/1_questions_and_solutions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions SQL_exercise_02/2_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_03/3_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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 ,
Expand Down
28 changes: 14 additions & 14 deletions SQL_exercise_03/3_questions.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 14 additions & 14 deletions SQL_exercise_03/3_questions_and_solutions.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
-- 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;

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
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
Expand All @@ -44,15 +44,15 @@ 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(*)
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
Expand All @@ -71,7 +71,7 @@ SELECT Code



--3.10
-- 3.10
-- Select the codes of all the boxes located in Chicago.

select Boxes.code
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions SQL_exercise_04/4_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_05/5_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_06/6_build_schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CREATE DATABASE sql_exercises_6;
USE sql_exercises_6;

create table Scientists (
SSN int,
Name Char(30) not null,
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_07/7_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_08/8_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions SQL_exercise_10/10_build_schema.sql
Original file line number Diff line number Diff line change
@@ -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);

Expand Down