-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the completed Computer Store Database project to the SQL with Pyt…
…hon track.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
JetBrainsAcademy/SQL with Python Track/Easy/Computer Store Database/Computer_Store.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- Create the Product table | ||
CREATE TABLE Product ( | ||
maker VARCHAR(50) NOT NULL, | ||
model INT NOT NULL, | ||
type VARCHAR(50) NOT NULL, | ||
PRIMARY KEY (model) | ||
); | ||
|
||
-- Create the PC table | ||
CREATE TABLE PC ( | ||
code INT NOT NULL, | ||
model INT NOT NULL, | ||
speed INT NOT NULL, | ||
ram INT NOT NULL, | ||
hd INT NOT NULL, | ||
cd VARCHAR(50) NOT NULL, | ||
price DECIMAL(10, 2) NOT NULL, | ||
PRIMARY KEY (code), | ||
FOREIGN KEY (model) REFERENCES Product(model) | ||
); | ||
|
||
-- Create the Laptop table | ||
CREATE TABLE Laptop ( | ||
code INT NOT NULL, | ||
model INT NOT NULL, | ||
speed INT NOT NULL, | ||
ram INT NOT NULL, | ||
hd INT NOT NULL, | ||
screen INT NOT NULL, | ||
price DECIMAL(10, 2) NOT NULL, | ||
PRIMARY KEY (code), | ||
FOREIGN KEY (model) REFERENCES Product(model) | ||
); | ||
|
||
-- Create the Printer table | ||
CREATE TABLE Printer ( | ||
code INT NOT NULL, | ||
model INT NOT NULL, | ||
color CHAR(1) NOT NULL, | ||
type VARCHAR(50) NOT NULL, | ||
price DECIMAL(10, 2) NOT NULL, | ||
PRIMARY KEY (code), | ||
FOREIGN KEY (model) REFERENCES Product(model) | ||
); |
38 changes: 38 additions & 0 deletions
38
...ademy/SQL with Python Track/Easy/Computer Store Database/computer_store_full_solution.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-- stage 1 completed | ||
SELECT model, | ||
type, | ||
price | ||
FROM Printer | ||
WHERE price > 200; | ||
|
||
-- stage 2 completed | ||
SELECT p.maker, p.model, l.hd, l.speed, l.price | ||
FROM Product AS p | ||
INNER JOIN Laptop AS l | ||
ON p.model = l.model | ||
WHERE l.hd >= 1000 | ||
ORDER BY l.hd ASC, l.speed DESC, l.price ASC; | ||
|
||
-- stage 3 completed | ||
SELECT COUNT(p1.maker) as number_of_unique_makers | ||
FROM Product AS p1 | ||
WHERE (SELECT COUNT(p2.model) FROM Product AS p2 | ||
WHERE p2.maker = p1.maker) = 1; | ||
|
||
-- stage 4 completed | ||
WITH all_prices AS ( | ||
SELECT a.maker, a.model, b.speed, b.price | ||
FROM Product a INNER JOIN PC b | ||
ON a.model = b.model | ||
|
||
UNION | ||
|
||
SELECT a.maker, a.model, b.speed, b.price | ||
FROM Product a INNER JOIN Laptop b | ||
ON a.model = b.model | ||
), | ||
lowest_prices AS ( | ||
SELECT r.maker AS maker, r.model AS model, r.speed AS speed, r.price AS price | ||
FROM all_prices r HAVING speed = ( SELECT MIN(speed) FROM all_prices ) | ||
) | ||
SELECT * FROM lowest_prices; |