-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_example.sql
executable file
·38 lines (32 loc) · 965 Bytes
/
sql_example.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
DROP VIEW IF EXISTS joined;
DROP VIEW IF EXISTS toyotas;
DROP TABLE IF EXISTS "public"."make";
DROP TABLE IF EXISTS "public"."cars";
CREATE TABLE "public"."cars" (
id SERIAL PRIMARY KEY,
type TEXT,
model TEXT,
cost INT,
make_id INT
);
INSERT INTO "public"."cars" ("type", "model", "cost", "make_id") VALUES
('sedan', 'roadster', '33', '2'),
('sedan', 'prius', '22', '1'),
('sedan', 'focus', '18', '3'),
('suv', 'highlander', '40', '1');
CREATE TABLE "public"."make" (
id SERIAL PRIMARY KEY,
name TEXT
);
INSERT INTO "public"."make" ("name") VALUES ('toyota'), ('tesla'), ('ford');
CREATE VIEW joined AS
SELECT cars.type, cars.cost, cars.model, make.name
FROM cars
INNER JOIN make ON (cars.make_id = make.id)
ORDER BY cost DESC LIMIT 30;
CREATE VIEW toyotas AS
SELECT cars.type, cars.cost, cars.model, make.name
FROM cars
INNER JOIN make ON (cars.make_id = make.id)
WHERE make.name = 'toyota'
ORDER BY cost DESC LIMIT 30;