-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
180 lines (162 loc) · 3.05 KB
/
queries.sql
File metadata and controls
180 lines (162 loc) · 3.05 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
-- create users table
create type
user_role as enum('Admin', 'Customer');
create table
users (
user_id serial primary key,
name varchar(100) not null,
email varchar(100) unique not null,
password varchar(30) not null,
phone varchar(30) not null,
role user_role not null default 'Customer'
);
-- insert
insert into
users (name, email, password, phone, role)
values
(
'Alice',
'alice@gmail.com',
'123asd',
'+8801307495864',
'Admin'
),
(
'Bob',
'bob@gmail.com',
'123asd',
'+8801307495864',
'Customer'
),
(
'Charlie',
'charlie@gmail.com',
'123asd',
'+8801307495864',
'Customer'
);
-- create vehicle table
create type
vehicle_type as enum('car', 'bike', 'truck');
create type
vehicle_status as enum('available', 'rented', 'maintenance');
create table
vehicles (
vehicle_id serial primary key,
name varchar(150) not null,
type vehicle_type not null,
model varchar(20) not null,
registration_number varchar(50) unique not null,
rental_price int not null,
status vehicle_status not null
);
-- insert
insert into
vehicles (
name,
type,
model,
registration_number,
rental_price,
status
)
values
(
'Toyota Corolla',
'car',
'2022',
'ABC-123',
'50',
'available'
),
(
'Honda Civic',
'car',
'2021',
'DEF-456',
'60',
'rented'
),
(
'Yamaha R15',
'bike',
'2023',
'GHI-789',
'30',
'available'
),
(
'Ford F-150',
'truck',
'2020',
'JKL-012',
'100',
'maintenance'
);
-- create booking table
create type
booking_status as enum('pending', 'confirmed', 'completed', 'cancelled');
create table
bookings (
booking_id serial primary key,
user_id int not null references users (user_id),
vehicle_id int not null references vehicles (vehicle_id),
start_date date not null default now(),
end_date date not null,
status booking_status not null default 'pending',
total_cost int not null
);
-- insert
insert into
bookings (
user_id,
vehicle_id,
start_date,
end_date,
status,
total_cost
)
values
(
1,
2,
'2023-10-01',
'2023-10-05',
'completed',
240
),
(
1,
2,
'2023-11-01',
'2023-11-03',
'completed',
120
),
(3, 2, '2023-12-01', '2023-12-02', 'confirmed', 60),
(1, 1, '2023-12-10', '2023-12-12', 'pending', 100);
-- query 1: JOIN
select booking_id, u.name as customer_name, v.name as vehicle_name, start_date, end_date, b.status from bookings as b
join users as u using(user_id) join vehicles as v using (vehicle_id);
-- Query 2: EXISTS
select *
from vehicles as v
where not exists (
select *
from bookings as b
where b.vehicle_id = v.vehicle_id
);
-- Query 3: WHERE
select * from vehicles
where type = 'car' and status = 'available';
-- Query 4: GROUP BY and HAVING
select
v.name vehicle_name,
count(*) total_bookings
from
bookings b
join vehicles v using (vehicle_id)
group by
v.vehicle_id, v.name
having
count(*) > 2;