-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
31 lines (24 loc) · 963 Bytes
/
Copy pathqueries.sql
File metadata and controls
31 lines (24 loc) · 963 Bytes
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
-- Query 1: JOIN
-- Requirement: Retrieve booking information along with Customer name and Vehicle name.
select b.booking_id, u.name as customer_name, v.name as vehicle_name, b.start_date, b.end_date, b.status
from bookings as b
inner join users as u using(user_id)
inner join vehicles as v using(vehicle_id);
-- Query 2: EXISTS
-- Find all vehicles that have never been booked.
select *
from vehicles as v
where not exists (
select 1 from bookings as b
where b.vehicle_id = v.vehicle_id
);
-- Query 3: WHERE
-- Requirement: Retrieve all available vehicles of a specific type (e.g. cars).
select * from vehicles
where type = 'car';
-- Query 4: GROUP BY and HAVING
-- Requirement: Find the total number of bookings for each vehicle and display only those vehicles that have more than 2 bookings.
select v.name as vehicle_name, count(*) as total_bookings from bookings as b
inner join vehicles as v using(vehicle_id)
group by v.name
having count(*) > 2;