30 Days SQL
30 Days SQL
DAY 1
Problem Statement:
- For pairs of brands in the same year (e.g. apple/samsung/2020 and samsung/apple/2020)
- if custom1 = custom3 and custom2 = custom4 : then keep only one pair
- For brands that do not have pairs in the same year : keep those rows as well
Expected output :
case
when
else concat(Brand2,brand1,year)
DAY 2
Problem statement:
A ski resort company is planning to construct a new ski slope using a pre-existing network of mountain huts
and trails between them. A new slope has to begin at one of the mountain huts, have a middle station at
another hut connected with the first one by a direct trail, and end at the third mountain hut which is also
connected by a direct trail to the second hut. The altitude of the three huts chosen for constructing the ski
slope has to be strictly decreasing.
You are given two SQL tables, mountain_huts and trails, with the following structure:
id name altitude
1 Dakonat 1900
2 Natisa 2100
3 Gajantut 1600
4 Rifat 782
5 Tupur 1370
hut1 hut2
1 3
3 2
3 5
4 5
1 5
Assume that:
Expected result:
Query:
with cte as (
from mountain_huts h1
cte2 as (
from cte c1
cte_final as
(select
from cte2)
c1.end_hut_name as middlePt,
DAY 3
PROBLEM STATEMENT: Write a sql query to return the footer values from input table, meaning all
the last non null values from each field as shown in expected output.
Expected result:
Solution 1:
with cte as (select top 1 car from footer where car is not null order by id desc),
cte2 as (select top 1 length from footer where length is not null order by id desc),
cte3 as (select top 1 width from footer where width is not null order by id desc),
cte4 as (select top 1 height from footer where height is not null order by id desc)
Solution 2:
select * from
(select top 1 car from footer where car is not null order by id desc) car
cross join (select top 1 length from footer where length is not null order by id desc) length
cross join (select top 1 width from footer where width is not null order by id desc) width
cross join (select top 1 height from footer where height is not null order by id desc) height
30 Days SQL Challenge
Solution 3:
with cte as
(select
id,car,length,width,height,
sum(case when car is not null then 1 else 0 end) over (order by id) as car_segment,
sum(case when length is not null then 1 else 0 end) over (order by id) as length_segment,
sum(case when width is not null then 1 else 0 end) over (order by id) as width_segment,
sum(case when height is not null then 1 else 0 end) over (order by id) as height_segment
from footer),
cte1 as (select top 1 car,FIRST_VALUE(car) over (Partition by car_segment order by id) as new_car
from cte order by id desc),
DAY 4
Problem Statement: Derive expected output
Given table
id name location
1 NULL NULL
2 David NULL
3 NULL London
4 NULL NULL
5 David NULL
Expected result:
id name location
1 David London
id name location
5 David London
Solution:
union all
DAY 5
PROBLEM STATEMENT: Using the given Salary, Income and Deduction tables, first write an sql query to
populate the Emp_Transaction table as shown below and then generate a salary report as shown.
Solution:
Step1
with cte as
union
cte2 as
into emp_transaction
from cte2
30 Days SQL Challenge
Step2
select name,Allowance,basic,others,
(Allowance+basic+others) as Gross,
Insurance,Health,House,
(Insurance+Health+House) as Total_Deductions,
((Allowance+basic+others)-(Insurance+Health+House)) as net_pay
from
) bq
pivot
sum(amount)
)p
30 Days SQL Challenge
DAY 6
PROBLEM STATEMENT:
You are given a table having the marks of one student in every test.
You have to output the tests in which the student has improved his performance.
For a student to improve his performance he has to score more than the previous test.
Provide 2 solutions, one including the first test score and second excluding it.
Solution 1:
from student_tests),
cte2 as (select *,
else 0
where Isgreater=1
Solution 2:
from student_tests)
DAY 7
PROBLEM STATEMENT:
In the given input table DAY_INDICATOR field indicates the day of the week with the first character being
Monday, followed by Tuesday and so on.
Write a query to filter the dates column to showcase only those days where day_indicator character for that
day of the week is 1.
Expected output:
Product_Id Day_Indicator Dates
AP755 1010101 2024-03-04
AP755 1010101 2024-03-06
AP755 1010101 2024-03-08
AP755 1010101 2024-03-10
XQ802 1000110 2024-03-04
XQ802 1000110 2024-03-08
XQ802 1000110 2024-03-09
30 Days SQL Challenge
Solution:
with cte as (
select *,
case when (Cast((datepart(dw,dates)) as int)-1)!=0 then (Cast((datepart(dw,dates)) as int)-1)
else (Cast((datepart(dw,dates)) as int)+6)
end as dow
from Day_Indicator),
cte2 as
(select *,
case when SUBSTRING(Day_Indicator,dow,1)=1 then 1
else 0
end as flag
from cte)
select Product_Id,Day_Indicator,Dates from cte2 where flag=1
30 Days SQL Challenge
DAY 8
PROBLEM STATEMENT:
In the given input table, there are rows with missing JOB_ROLE values. Write a query to fill in those blank
fields with appropriate values.
Assume row_id is always in sequence and job_role field is populated only for the first skill.
Provide two different solutions to the problem.
Solution1:
Solution 2:
with cte as
(select row_id, job_role, skills from job_skills where row_id=1
union all
select e.row_id, case when e.job_role is null then cte.job_role else e.job_role end as job_role
, e.skills from job_skills e
join cte on e.row_id = cte.row_id + 1)
select * from cte;
30 Days SQL Challenge
DAY 9
PROBLEM STATEMENT:
Write an sql query to merge products per customer for each day as shown in expected output.
Solution:
select Dates,cast(product_id as varchar) as products from orders
union
select dates,STRING_AGG(cast(product_id as varchar),',') as products from orders
group by customer_id,dates order by dates
30 Days SQL Challenge
DAY 10
Solution:
select * from
pivot(
count(level)
)p
30 Days SQL Challenge
DAY 11
PROBLEM STATEMENT: In the given input table, there are hotel ratings which are either too high or too low
compared to the standard ratings the hotel receives each year. Write a query to identify and exclude these
outlier records as shown in expected output below.
Your output should follow the same order of records as shown.
Query 1:
from cte2 ),
cte4 as (select *, rank() over (partition by hotel order by difference desc) as rank
from cte3)
select Hotel,Year,Rating from cte4 where rank !=1 order by hotel desc,year
Query2:
from hotel_ratings),
cte3 as (select *, rank() over (partition by hotel order by diff desc) as rank
from cte2 )
select Hotel,Year,Rating from cte3 where rank !=1 order by hotel desc, year
30 Days SQL Challenge
DAY 12
PROBLEM STATEMENT:
Write an SQL query to split the hierarchy and show the employees corresponding to their team.
EXPECTED OUTPUT:
TEAMS MEMBERS
Team 1 Elon, Bret, Mark, Phil, Jon
Team 2 Elon, Earl, Omid
Team 3 Elon, Ira, James, Drew
Query:
with cte as
rec_cte as
union all
DAY 13
PROBLEM STATEMENT:
Find out the no. of employees managed by each manager.
INPUT OUTPUT
1 Sundar Sundar 5
2 Kent 1 Alison 3
3 Ruth 1 Larry 3
4 Alison 1 Kent 2
5 Clay 2 Ruth 1
6 Ana 2
7 Philipp 3
8 Prabhakar 4
9 Hiroshi 4
10 Jeff 4
11 Thomas 1
12 John 15
13 Susan 15
14 Lorraine 15
15 Larry 1
Query:
DAY 14
PROBLEM STATEMENT: In the given input table, some of the invoice are missing, write a sql query
to identify the missing serial no.
As an assumption, consider the serial no with the lowest value to be the first generated invoice and
the highest serial no value to be the last generated invoice
INPUT OUTPUT
330124
Query:
WITH MinMax AS (
),
Numbers AS (
UNION ALL
SELECT s_no + 1 FROM Numbers WHERE s_no < (SELECT max_s_no FROM MinMax)
DAY 15
PROBLEM STATEMENT:
For the given friends, find the no of mutual friends.
INPUT OUTPUT
Solution 1:
union all
Solution 2:
union all
from friends f
DAY 16
PROBLEM STATEMENT: Given table contains reported covid cases in 2020.
Calculate the percentage increase in covid cases each month versus cumulative cases as of the prior month.
Return the month number, and the percentage increase rounded to one decimal. Order the result by the
month.
INPUT OUTPUT
20124 1/10/2020 1 -
50099 4/13/2020
87045 4/22/2020
101101 4/30/2020
40015 5/1/2020
54035 5/9/2020
71099 5/14/2020
82045 5/21/2020
90103 5/25/2020
99103 5/31/2020
11015 6/3/2020
28035 6/10/2020
38099 6/14/2020
45045 6/20/2020
36033 7/9/2020
40011 7/23/2020
30 Days SQL Challenge
25001 8/12/2020
29990 8/26/2020
20112 9/4/2020
43991 9/18/2020
51002 9/29/2020
26587 10/25/2020
11000 11/7/2020
35002 11/16/2020
56010 11/28/2020
15099 12/2/2020
38042 12/11/2020
73030 12/26/2020
Solution:
with cte as (
),
cte2 as
from cte)
select months,case
when
DAY 17
Problem Statement:
User login table shows the date when each user logged in to the system. Identify the users who logged in for 5 or more
consecutive days. Return the user id, start date, end date and no of consecutive days.
Please remember a user can login multiple times during a day but only consider users whose consecutive logins spanned
5 days or more.
1 2024-03-30
Expected output:
Query:
from user_login )
order by user_id
30 Days SQL Challenge
DAY 18
PROBLEM STATEMENT:
Find out the employees who attended all the company events.
Conference 2 3/3/2024
Conference 3 3/2/2024
Conference 4 3/2/2024
Training 3 3/4/2024
Training 2 3/4/2024
Training 4 3/4/2024
Training 4 3/5/2024
Solution:
with cte as (
from employees e
DAY 19
Given table showcases details of pizza delivery order for the year of 2023. If an order is delayed then the
whole order is given for free. Any order that takes more than 30 minutes from the Ordered time is considered
as a delayed order. Identify the percentage of delayed order for each month and also display the total no of
free pizzas given each month.
Sample Data:
Expected output:
Jan-2023 9.23% 31
Feb-2023 12.22% 49
Mar-2023 15.79% 61
Apr-2023 13.41% 77
May-2023 14.29% 65
Jun-2023 10.96% 48
Jul-2023 15.71% 43
Aug-2023 11.24% 63
Sep-2023 18.92% 89
Oct-2023 15.91% 60
Dec-2023 15.15% 58
30 Days SQL Challenge
Query:
with cte as
datediff(MINUTE,order_time,actual_delivery) as delay_minutes
from pizza_delivery ) ,
cte2 as
select *, case
from cte),
cte3 as
sum(free_pizzas) as free_pizzas
group by mm,yyyy
select
DAY 20
PROBLEM STATEMENT: Find the median ages of countries
INPUT OUTPUT
1 10 6
Poland Germany
2 5 54
Poland Germany
3 34 33
Poland India
4 56 38
Poland India
5 45 58
Poland Japan
6 60 44
Poland Malaysia
7 18 34
India Poland
8 15 45
India Poland
9 33 32
India USA
10 38
India
11 40
India
12 50
India
13 20
USA
14 23
USA
15 32
USA
16 54
USA
17 55
USA
18 65
Japan
19 6
Japan
20 58
Japan
21 54
Germany
22 6
Germany
23 44
Malaysia
Query:
(select *,
range between unbounded preceding and unbounded following) as float) as count_Per_Country from people) x
DAY 21
PROBLEM STATEMENT: The column 'perc_viewed' in the table 'post_views' denotes the percentage of the session
duration time the user spent viewing a post. Using it, calculate the total time that each post was viewed by users. Output
post ID and the total viewing time in seconds, but only for posts with a total viewing time of over 5 seconds.
Sample data:
Expected output:
post_id timeviewed
4 5.1
2 24
Query:
with cte as
(select p.*,s.session_starttime,s.session_endtime,
DATEDIFF(SECOND,session_starttime,session_endtime) as sessiontime
from user_sessions s
from cte)
select post_id, sum(totaltime) as timeviewed from cte2 group by post_id having sum(totaltime) > 5 order by 2
30 Days SQL Challenge
DAY 22
Problem Statement: IPL Winning Streak
Given table has details of every IPL 2023 matches. Identify the maximum winning streak for each team.
1) Update the dataset such that when Chennai Super Kings win match no 17, your query shows the updated streak.
2) Update the dataset such that Royal Challengers Bangalore loose all match and your query should populate the winning streak as 0.
Sample Data:
1 1 2023-03-31 Narendra Modi Stadium, Ahmedabad Gujarat Titans Chennai Super Kings Gujarat Titans
Punjab Cricket Association IS Bindra Kolkata Knight
2 1 2023-04-01 Stadium, Moha Punjab Kings Riders Punjab Kings
Bharat Ratna Shri Atal Bihari Vajpayee
3 1 2023-04-01 Ekana Crick Lucknow Super Giants Delhi Capitals Lucknow Super Giants
Rajiv Gandhi International Stadium,
4 1 2023-04-02 Hyderabad Sunrisers Hyderabad Rajasthan Royals Rajasthan Royals
Royal Challengers Royal Challengers
5 1 2023-04-02 M Chinnaswamy Stadium, Bengaluru Bangalore Mumbai Indians Bangalore
Expected output:
teams max_winning_streak
Gujarat Titans 3
Mumbai Indians 3
Rajasthan Royals 3
Sunrisers Hyderabad 2
Punjab Kings 2
Delhi Capitals 2
30 Days SQL Challenge
Query:
union
cte as (
select Dates,home_team,away_team,teams,result,
from cte_teams t
cte_3 as (
select *,
cte_final as
(select teams,
from cte_3)
select t.teams,
case when max(streak) is not null then max(streak) else 0 end as max_winning_streak
from cte_teams t
DAY 23
2153. The Number of Passengers in Each Bus II
Table: Buses
+--------------+------+
+--------------+------+
| bus_id | int |
| arrival_time | int |
| capacity | int |
+--------------+------+
Each row of this table contains information about the arrival time of a bus at the LeetCode station and its
capacity (the number of empty seats it has).
No two buses will arrive at the same time and all bus capacities will be positive integers.
Table: Passengers
+--------------+------+
+--------------+------+
| passenger_id | int |
| arrival_time | int |
+--------------+------+
Each row of this table contains information about the arrival time of a passenger at the LeetCode station.
Buses and passengers arrive at the LeetCode station. If a bus arrives at the station at a time tbus and a
passenger arrived at a time tpassenger where tpassenger <= tbus and the passenger did not catch any
bus, the passenger will use that bus. In addition, each bus has a capacity. If at the moment the bus arrives at
the station there are more passengers waiting than its capacity capacity, only capacity passengers will
use the bus.
30 Days SQL Challenge
Write a solution to report the number of users that used each bus.
Example 1:
Input:
Buses table:
+--------+--------------+----------+
+--------+--------------+----------+
| 1 | 2 | 1 |
| 2 | 4 | 10 |
| 3 | 7 | 2 |
+--------+--------------+----------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11 | 1 |
| 12 | 1 |
| 13 | 5 |
| 14 | 6 |
| 15 | 7 |
+--------------+--------------+
Output:
30 Days SQL Challenge
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+--------+----------------+
Explanation:
- Bus 1 arrives at time 2 and collects passenger 11 as it has one empty seat.
- Bus 2 arrives at time 4 and collects passenger 12 as it has ten empty seats.
- Bus 3 arrives at time 7 and collects passengers 12 and 13 as it has two empty seats.
SOLUTION:
with cte_data as (
cte as
select rn,bus_id,capacity,total_passengers,
case when capacity < total_passengers then capacity else total_passengers end as onboarded_passengers,
case when capacity < total_passengers then capacity else total_passengers end as Total_onboarded_passengers
union all
select d.rn,d.bus_id,d.capacity,d.total_passengers,
30 Days SQL Challenge
end as onboarded_passengers,
end as onboarded_passengers
from cte c
DAY 24
Problem Statement: Find valid email id's
A consumer electronics store in Warsaw stores all the customer feedback in the feedback table. The email ids mentioned by customers
are then used by the store to contact customers to promote any upcoming sales. However, some of the customers, while sharing
feedback, enter invalid email addresses. Write an SQL query to identify and return all the valid email addresses from the feedback table.
Part 1 is the username. A username can contain upper or lower-case letters, numbers and special characters like underscore character
"_", dot ".", hyphen "-". Username should always start with a letter.
Part 3 is the domain which needs to have 2 sub parts. The first part contains upper or lower-case letters followed by a dot symbol and
then followed by 2 or 3 letters.
Sample data:
3 Zayn ZAYN...@gmail 3 ok
4 Emir emir-#1@outlook.com 4 ok
Expected output:
Query:
30 Days SQL Challenge
DAY 25
PROBLEM STATEMENT:
Analyse the given input table and come up with output as shown.
Sample data:
store_id product_1 product_2
Expected output:
1 2 1
2 1 3
3 0 0
Query:
select store_id,
sum(case when product_2 like 'Apple%' or product_2 like ' Apple%' or product_2 like ' Apple%' then 1 else 0 end) as
product_2
DAY 26
PROBLEM STATEMENT: Given table contains tokens taken by different customers in a tax office.
Write a SQL query to return the lowest token number which is unique to a customer (meaning token should be allocated
to just a single customer).
Sample data:
token_num customer
1 Maryam
2 Rocky
3 John
3 John
2 Arya
1 Pascal
9 Kate
9 Ibrahim
8 Lilly
8 Lilly
5 Shane
Expected output:
minimum_Token_num
Solution:
with cte as
cte2 as
DAY 27
PROBLEM STATEMENT:
Given vacation_plans tables shows the vacations applied by each employee during the year 2024. Leave_balance table
has the available leaves for each employee. Write an SQL query to determine if the vacations applied by each employee
can be approved or not based on the available leave balance. If an employee has enough available leaves then mention
the status as "Approved" else mention "Insufficient Leave Balance". Assume there are no public holidays during 2024.
weekends (sat & sun) should be excluded while calculating vacation days.
Sample data:
1 1 2024-02-12 2024-02-16 1 12
2 2 2024-02-20 2024-02-29 2 10
3 3 2024-03-01 2024-03-31 3 26
4 1 2024-04-11 2024-04-23 4 20
5 4 2024-06-01 2024-06-30 5 14
6 3 2024-07-05 2024-07-15
7 3 2024-08-28 2024-09-15
Expected output:
1 1 5 Approved
2 2 8 Approved
3 3 21 Approved
5 4 20 Approved
4 1 9 Insufficient balance
6 3 7 Insufficient balance
7 3 13 Insufficient balance
30 Days SQL Challenge
Query:
from vacation_plans
union all
select id,emp_id,DATEADD(day, 1, DateValue), to_dt from DateSeries where DateValue < to_dt),
cte as
cte2 as
(select *,case when DateName(WEEKDAY,DateValue) in ('Saturday','Sunday') then 0 else 1 end as weekday from cte),
cte3 as (select
from cte2 c2
cte4 as (
union all
select c.id,c.emp_id,c.applied_Vac_days,c.balance,c.rn,(cte4.remaining_balance-c.applied_vac_days) as
remaining_balance
from cte4
select id,emp_id,applied_Vac_days,
case when remaining_balance>=0 then 'Approved' else 'Insufficient balance' end as Status
DAY 28
Find length of comma seperated values in items field.
id items id lengths
1 221,221,022 1 2,3,4
2 ,6,0,9999 2 0,1,1,4
3 100,2000,2 3 3,4,1
4 4,44,444,4444 4 1,2,3,4
Query:
DAY 29
PROBLEM STATEMENT: Given table provides login and logoff details of one user.
Generate a report to represent the different periods (in mins) when user was logged in.
OUTPUT
INPUT
10:04:00 on
10:05:00 on
10:06:00 off
10:07:00 off
10:08:00 off
10:09:00 on
10:10:00 on
10:11:00 on
10:12:00 on
10:13:00 off
10:14:00 off
10:15:00 on
10:16:00 off
10:17:00 off
Query:
with cte as
from
cte2 as
from
cte3 as (
cte4 as
from
DAY 30
PROBLEM STATEMENT: Given tables represent the marks scored by engineering students.
Create a report to display the following results for each student.
- Student_id, Student name, Total Percentage of all marks
- Failed subjects (must be comma separated values in case of multiple failed subjects)
- Result (if percentage >= 70% then 'First Class', if >= 50% & <=70% then 'Second class', if <=50% then 'Third class' else 'Fail'. The
result should be Fail if a students fails in any subject irrespective of the percentage marks)
*** The sequence of subjects in student_marks table match with the sequential id from subjects table.
*** Students have the option to choose either 4 or 5 subjects only.
Sample data:
id name pass_marks
S1 Mathematics 40
S2 Algorithms 35
S3 Computer Networks 35
S4 Data Structure 40
S5 Artificial Intelligence 30
S6 Object Oriented Programming 35
Expected output:
Solution:
UNPIVOT (
) AS unpivoted_data),
student as
on y.rn=x.rn),
percentage as (
cte_final as (
select Student_id,name,percentage_marks,
from cte_final