Interview Question SQL
Interview Question SQL
Interview Question
SQL
b1 2022-03-23 u1 Flight
b2 2022-03-27 u2 Flight
b3 2022-03-28 u1 Hotel
b4 2022-03-31 u4 Flight
b5 2022-04-02 u1 Hotel
b6 2022-04-02 u2 Flight
b7 2022-04-06 u5 Flight
b8 2022-04-06 u6 Hotel
b9 2022-04-06 u2 Flight
u1 s1
u2 s1
u3 s1
u4 s2
u5 s2
u6 s3
u7 s3
u8 s3
u9 s3
u10 s3
1.Write a query to find the total number of users for
each segment and total number of users who
booked flight in April 2022.
QUERY
QUERY EXPLANATION
total_flights_booke
segment total_users
d
s1 3 2
s2 2 2
s3 5 1
2.Write a query to identify users whose first
booking was a hotel booking.
QUERY
QUERY EXPLANATION
1.We are using rnk_cte to get the required details
and rank on the basis of Order date for each user.
This will help us to get the first booking done by that
particular user.
OUTPUT
user_id
u6
3.Write a query to calculate the days between first
and last booking of each user.
QUERY
QUERY EXAPLANATION
1.We are using DATEDIFF to calculate the day difference
between first booking date and last booking date for
each user_id.
user_id diff
u1 44
u2 32
u4 34
u5 14
u6 16
4.Write a query to count the number of flight and
hotel bookings in each user segments for the year
2022
QUERY
QUERY EXPLANATION
1.By using CASE WHEN statement we are flagging with
1 if booking was for 'Flight' or 'Hotel' and then by using
SUM function we are adding these 1s to count them.
To filter for year 2022 only we have used DATEPART
Function.
OUTPUT
s1 8 4
s2 3 3
s3 1 1
THANK YOU
Don't judge each day by the harvest you
reap but by the seeds that you plant.
Robert Louis Stevenson