Skip to content

Commit

Permalink
Add the completed Computer Store Database project to the SQL with Pyt…
Browse files Browse the repository at this point in the history
…hon track.
  • Loading branch information
dsfb committed Sep 11, 2023
1 parent bf2fb1a commit 9a4d804
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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)
);
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;

0 comments on commit 9a4d804

Please sign in to comment.