SlideShare a Scribd company logo
Important SQL
Interview Questions
Find the employees having salary more than thier manager salary
Select employee.emp_id, employee.emp_name, M.emp_name as manager_name,
employee.salary, M.salary as Manager_salary
from employee
Inner Join employee M On employee.manager_id = M.emp_id
Where employee.salary > M.salary;
Find the employee with second highest salary from each department
Select * from (select emp.*, dense_rank() Over(Partition By department_id Order By salary Desc ) as rn from
emp) a
where rn= 2;
find employees who are not present in the depratment table
Select emp.*, dept.dept_id, dept.dept_name
from emp
Left Join dept On emp.department_id = dept.dept_id
where dept.dept_id is Null;
How to find the mode
--Method 1
With Freq_cte as(
Select id, count(*) as freq from mode group By id)
Select * from freq_cte
where freq = (select Max(freq) from Freq_cte);
--Method 2
With Freq_cte as(
Select id, count(*) as freq from mode group By id),
rnk_cte as(
Select *, rank() over(order By freq desc) as rn
from Freq_cte)
Select * from rnk_cte where rn = 1
SELECT e20.*, e21.*,
CASE
WHEN e20.designation IS DISTINCT FROM e21.designation THEN 'Promoted'
WHEN e21.designation IS NULL THEN 'Resigned'
ELSE 'New'
END AS comment
FROM emp_2020 e20
FULL OUTER JOIN emp_2021 e21 ON e20.emp_id = e21.emp_id
WHERE COALESCE(e20.designation, 'XXX') != COALESCE(e21.designation, 'YYY');
find the change in employee status from 2020 to 2021 -> Example of full outer join
With cte_dup as(
Select id from list group by id Having count(1)>1),
cte_rank as(
select id, rank() Over(order by id asc) as rn from cte_dup)
select l.id, 'DUP' || cast(cr.rn as varchar(2)) as output
from list l
Left Join cte_rank cr On l.id = cr.id
Rank the duplicate records
Select *,
sum(cost) Over(order by cost asc, product_id) as running_cost
from product;
find the running sum
--method1
create table empl_backUp as Select * from empl;
Delete from empl;
Insert Into empl Select distinct * from empl_backUP;
Select * from empl
Identify and delete excat duplicate record
--method2
Insert Into empl
select id,name, salary, joining_date from
(Select *, row_number() Over(partition by id order by
salary) as rn from empl_backUP ) A
where rn =1;
with cte as(
Select Customer_Name,region, sum(sales) as total_Sales
from Order_A
group by Customer_Name, region
Order By region, total_sales desc
Limit 18)
Select * from (Select * ,
NTILE(4) Over (Partition By region Order by total_Sales) as cust_groups
from cte) a
where cust_groups IN(1)
NTILE function
Select gold as player_name, count(1) as no_of_medals
from events
where gold not in (Select silver from events Union All Select bronze from events)
group by gold;
Find no of goldmedal per swimmer for swimmers who only won gold medals.
Select gold as player_name, count(1) as no_of_medals
from events
where gold not in (Select silver from events Union All Select bronze from events)
group by gold;
Find no of goldmedal per swimmer for swimmers who only won gold medals.
--using CTE
With cte as (Select gold as player_name, 'gold' as medal_type from events
Union All Select silver, 'silver' as medal_type from events
Union All Select bronze, 'bronze' as medal_type from events)
Select player_name, count(1) as no_of_gold_medals
from cte
group by player_name
having count (distinct medal_type) = 1 and max(medal_type) = 'gold';
SELECT
t.ticket_id,
t.create_date,
t.resolved_date,
COUNT(*) AS business_days
FROM
tickets t
JOIN
generate_series(t.create_date, t.resolved_date, interval '1 day') AS dates
ON EXTRACT(ISODOW FROM dates) < 6 -- Exclude Saturday (6) and Sunday (7)
LEFT JOIN
holidays h
ON dates = h.holiday_date
WHERE
h.holiday_date IS NULL
GROUP BY
t.ticket_id, t.create_date, t.resolved_date;
find business days between create days and resolved days excluding weekends and public holidays
--method1
with cte as(
select emp_id,
max(case when action='in' then time end) as intime,
max(case when action = 'out' then time end) as outtime
from hospital
group by emp_id)
Select * from cte
where intime > outtime or outtime is null
find the total no of people present inside the hospital
--method 2
with latest_time as(
select emp_id, max(time) as max_latest_time from hospital group by emp_id),
latest_in_time as (select emp_id, max(time) as max_in_time from hospital
where action = 'in' group by emp_id)
select * from latest_time lt
inner Join latest_in_time lit on
lt.emp_id=lit.emp_id and max_latest_time = max_in_time;
with sal_dep as (
select dept_id, salary
from emp_salary
group by dept_id, salary
having count(1)>1)
Select es.*
from emp_salary es
inner join sal_dep sd ON es.dept_id= sd.dept_id and es.salary = sd.salary
return all employees whose salary same in same department
-- left join method 2
with sal_dep as (
select dept_id, salary
from emp_salary
group by dept_id, salary
having count(1)=1)
Select es.*
from emp_salary es
left join sal_dep sd ON es.dept_id= sd.dept_id and es.salary = sd.salary
where sd.dept_id is null
SELECT word, COUNT(*) AS cnt_of_words
FROM (
SELECT regexp_split_to_table(content, ' ') AS word
FROM namaste_python
) AS split_words
GROUP BY word
HAVING COUNT(*) > 1
ORDER BY cnt_of_words desc;
find the words whicch are repeating more than once considering all the rows of content column
with cte as(
select m.genre, m.title, round(avg(r.rating),0) as avg_rating,
row_number() over(partition by m.genre order by avg(r.rating) desc) as rn
from movies m
inner join reviews r On m.id = r.movie_id
group by m.genre, m.title
order by m.genre, avg_rating desc)
select genre, title, avg_rating
, Repeat('*',avg_rating::int) as stars
from cte
where rn = 1
find, within each genre, the movie which have highest average rating and also print the star *****
--approach1
with cte as(
select dep_id, max(salary) as max_salary, min(salary) as min_salary
from employee1
group by dep_id)
select e.dep_id,
max(case when salary = max_salary then emp_name else null end )as max_sal_emp,
min(case when salary = min_salary then emp_name else null end )as min_sal_emp
from employee1 e
inner join cte On e.dep_id = cte.dep_id
group by e.dep_id;
write the query to find the highest and lowest salary employee in each department
--approach2
with cte as(
select *,
rank() over(partition by dep_id order by salary desc) as rank_desc,
rank() over(partition by dep_id order by salary asc) as rank_asc
from employee1)
select dep_id,
max(case when rank_desc = 1 then emp_name end) as max_sal_emp
,min(case when rank_asc = 1 then emp_name end )as min_sal_emp
from cte
group by dep_id
--method1
Select coalesce(s.id,t.id) as id, -- s.name,t.name
case when t.name is null then 'new in source' when s.name is null then 'new in target'
else 'mismatched' end as comment
from source s
full outer join target t On s.id = t.id
where s.name != t.name or s.name is null or t.name is null
find the names which are not common in both source and target table.
--method2
with cte as(
select *, 'source' as table_name from source
union all
select *, 'target' as table_name from target
)
select id,
case when min(name) != max(name) then 'mismatched'
when min(table_name)= 'source' then 'new in source'
when max(table_name) = 'target' then 'new in target' end as comment
from cte
group by id
having count(*) =1 or( count(*) = 2 and min(name) != max(name))
select o.cid, o.origin, o.destination as final_destination
from flights o
inner join flights d On o.destination = d.origin
find the original and final destination for each flight
Select order_date, count(distinct customer) as count_new_cust from(
select *,
rank() over(partition by customer order by order_date) as rn
from sales) a
where rn = 1
group by order_date
find the count of new customer in each month
-- approach 1
select r.c_id, p.name as child_name, max(m.name) as mother_name, max(f.name) as father_name
from relations r
left join people m on r.p_id = m.id and m.gender = 'F'
left join people f on r.p_id = f.id and f.gender = 'M'
inner join people p On p.id = r.c_id
group by r.c_id, p.name
find the father's name and mother's name of every child
--approach 2
select r.c_id as child_id, p.name as child_name,
max(case when p.gender = 'F' then name end) as mother_name,
max(case when p.gender = 'M' then name end) as father_name
from relations r
inner join people p on r.p_id=p.id
group by 1, p.name
--appraoch2
with cte as(
select *,
revenue - lag(revenue,1,0) over(partition by company order by year) as rev_diff
from company_revenue
)
select company
from cte
where company not in (select company from cte where rev_diff < 0)
group by company
find the compnies which revenue is icreasing (no dip)
--approach1
with cte as(
select *,
revenue - lag(revenue,1,0) over(partition by company order by year) as rev_diff,
count(1) over(partition by company) as cnt
from company_revenue
)
select company, cnt, count(1) as sales_inc_year
from cte
where rev_diff > 0
group by company, cnt
having cnt = count(1)
all the children will go on ride with adults only. adults can go alone if no more children is left for pairing. and
eldest person will pair with youngest child.
with cte_adult as(
select *, row_number() over(order by age desc) as rn from family
where type = 'Adult'
),
cte_child as(
select *, row_number() over(order by age asc) as rn from family
where type = 'Child')
select a.person, c.person, a.age as adult_age, c.age as child_age
from cte_adult a
left join cte_child c On c.rn = a.rn
determine the team which qualifies the below criteria
1.Both Criteria1 and Criteria2 are marked as 'Y' for a particular team.
2.The count of such occurrences of both 'Y' criteria within each team is at least 2.
--approach1
with qualified_team as(
select teamID, count(1) as no_of_eligible_members
from Ameriprise_LLC
where Criteria1 = 'Y' and Criteria2 = 'Y'
group by teamID
having count(1) >= 2)
select al.*,qt.*
, case when Criteria1 = 'Y' and Criteria2 = 'Y' and qt.no_of_eligible_members is not null then 'Y' else 'N' end as qualified_flag
from Ameriprise_LLC al
left join qualified_team qt On al.teamID = qt.teamID
--approach 2
select al.*,
case when Criteria1 = 'Y' and Criteria2 = 'Y' and
sum(case when Criteria1 = 'Y' and Criteria2 = 'Y' then 1 else 0 end) over (partition by teamID) >=2 then 'Y' else 'N' end as qualified_flag
from Ameriprise_LLC al
determine the team which qualifies the below criteria
1.Both Criteria1 and Criteria2 are marked as 'Y' for a particular team.
2.The count of such occurrences of both 'Y' criteria within each team is at least 2.
Top 2 highest salried employees in each dept
select * from(
Select *,
row_number() over(partition by department_id order by salary desc) as rn,
dense_rank() over(partition by department_id order by salary desc) as dense_rn
from emp)a
--where rn <= 2
where dense_rn <= 2
Top 5 products by category and sales
with cte as (
select category, product_id, sum(sales) as sales
from order_a
group by category, product_id
)
select * from(
select *,
row_number() over(partition by category order by sales desc) as rn
from cte)
where rn <= 5
YOY grwoth/category wise products with sales more than pervious yesr sales(lead / lag function use case)
with cte as(
SELECT category, EXTRACT(YEAR FROM order_date) AS year_order, SUM(sales) AS sales
FROM order_a
GROUP BY category, EXTRACT(YEAR FROM order_date)
order BY category, EXTRACT(YEAR FROM order_date)
),
cte2 as(
select *,
lag(sales,1,sales) over(partition by category order by year_order) as prev_year_sales
from cte
)
select *,
Round((sales - prev_year_sales)*100/ prev_year_sales,0) as YOY_sales
from cte2
YOY grwoth/category wise products with sales more than pervious yesr sales(lead / lag function use case)
runnuing/cummulative sales year wise/ rolling n months sales
with cte as(
SELECT category, EXTRACT(YEAR FROM order_date) AS year_order,
SUM(sales) AS sales
FROM order_a
GROUP BY category, EXTRACT(YEAR FROM order_date)
order by category, EXTRACT(YEAR FROM order_date)
)
select *,
sum(sales) over(partition by category order by year_order )
as cummulative_sales
from cte
pivoting -> converting rows into column ->year wise sales for each category
Select Extract(Year from order_date) as year_order,
Sum(case when category = 'Furniture' then sales else 0 end ) AS furniture_sales,
Sum(case when category = 'Office Supplies' then sales else 0 end ) AS OS_sales,
Sum(case when category = 'Technology' then sales else 0 end ) AS technology_sales
Into order_a_b
FROM order_a
Group by Extract(year from order_date)
find the category of games
1. No social interaction
2.one sided interaction
3.Both sided intraction without custome_typed messages
4.both side interaction with custome_typed message from at least one player
SELECT game_id,
CASE
WHEN count(interaction_type) = 0 THEN 'No social interaction'
WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 1
THEN 'One sided interaction'
WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 2 AND
count(DISTINCT CASE WHEN interaction_type = 'custom_typed' THEN user_id END) = 0
THEN 'Both sided interaction without custom_typed messages'
WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 2 AND
count(DISTINCT CASE WHEN interaction_type = 'custom_typed' THEN user_id END) >= 1
THEN 'Both sided interaction with custom_typed message from at least one player'
END AS game_type
FROM user_interactions
GROUP BY game_id;
find the category of games
1. No social interaction
2.one sided interaction
3.Both sided intraction without custome_typed messages
4.both side interaction with custome_typed message from at least one player
Extract First name, middle name, last name
With cte as (
select *,
length(customer_name) - length(replace(customer_name,' ','')) as no_of_space,
Position(' 'IN customer_name) as first_space_position,
Position(' ' IN Substring(customer_name From Position(' ' IN customer_name) + 1)) + Position(' ' IN customer_name)
AS second_space_position
from customers
)
select *,
case when no_of_space = 0 then customer_name
else left(customer_name, first_space_position-1) end as First_name,
case when no_of_space <= 1 then null
else substring(customer_name, first_space_position +1, second_space_position - (first_space_position )) end as middle_name,
case when no_of_space =0 then null
when no_of_space = 1 then substring(customer_name, first_space_position +1, length(customer_name)- first_space_position)
when no_of_space = 2 then substring(customer_name, second_space_position+1, length(customer_name)- second_space_position)
end as last_name
from cte
Extract First name, middle name, last name
Return the record which have 3 or more consecutive rows where no. of people is more than 100(inclusive)
with cte as (
select *, row_number() over(order by visit_date) as rn,
id - row_number() over(order by visit_date) as grp
from stadium
where no_of_people >= 100)
select id,visit_date,no_of_people from cte
where grp IN(
select grp
from cte
group by grp
having count(1) >=3)
Find the employees whose salary is median of salary of all employees company wise
select company,avg(salary) from(
select *,
row_number() over(partition by company) as rn,
count(1) over(partition by company) as total_count
from median_salary) a
where rn between (total_count*1.0)/2 and (total_count*1.0 )/2 +1
group by company
list of players according to city -> pivoting
select
max(case when city = 'Bangalore' then name end) as Bangalore,
max(case when city = 'Mumbai' then name end) as Mumbai,
max(case when city = 'Delhi' then name end) as Delhi
from
(select *,
row_number() over(partition by city) as players_location
from players_location) a
group by players_location
order by players_location
return the second most recent activity, if there is only one then return first
with cte as (
select *,
count(1) over(partition by username) as total_activities,
row_number() over(partition by username order by startDate) as rn
from UserActivity
) select * from cte
where total_activities = 1 or rn = 2
Total sales by year
with recursive r_cte as(
select min(period_start)::date as dates,
max(period_end)::date as max_date from recursive_sales
Union All
select (dates + INTERVAL '1 day')::date, max_date::date from r_cte
where dates < max_date)
SELECT product_id, Extract(year from dates) as report_year,
sum(average_daily_sales) as total_amount from r_cte
inner join recursive_sales On dates between period_start and period_end
group by product_id, report_year
order by product_id, report_year
with cte as (
select spend_date, user_id, max(platform) as platform, sum(amount) as amount
from spending
group by user_id, spend_date having count(distinct platform) =1
Union All
select spend_date, user_id, 'Both' as platform, sum(amount) as amount
from spending
group by user_id, spend_date having count(distinct platform) =2)
select spend_date, platform, sum(amount) as total_amount, count(distinct user_id) as total_users
from cte
group by spend_date, platform
order by spend_date, platform
Find the total number of users and total amount spent using mobile only, desktop only and mobile and desktop both for
each date
select * from(
select *,
rank() over(partition by player_id order by event_date) as rn
from activity)a
where rn = 1
Write a SQL query that reports the device that is first logged in for each player
select *,
sum(games_played) over(partition by player_id order by event_date) as total_palyed
from activity
Write an SQL query that reports for each player and date, how many games played so far by the player.
That is, the total number of games played by the player until that date.
with rnk_orders as(
select *,
rank() over(partition by seller_id order by order_date asc) as rn
from orderss)
select u.user_id as seller_id,
case when i.item_brand = u.favorite_brand then 'Yes' else 'No' end as item_fav_brand
from users U
Left join rnk_orders ro on ro.seller_id = u.user_id and rn = 2
Left join items i on i.item_id = ro.item_id
Write an SQL query that reports the fraction of playFind for each seller, whether the brand of the second item (by date) is
his favourite brand. if a seller has sold leass than 2 item then report as No for him in the answerers that logged in again
on the day after the day they first logged in, rounded to 2 decimal places
with player_scores as(
select first_player as player_id, first_score as score from matches
union all
select second_player as player_id, second_score as score from matches)
, final_scores as(
select p.group_id, ps.player_id, sum(score) as score
from player_scores ps
inner join players p on p.player_id = ps.player_id
group by ps.player_id, p.group_id
),
final_ranking as(
select *,
rank() over(partition by group_id order by score desc, player_id asc) as rn
from final_scores)
select * from final_ranking where rn = 1
Find the winner in each group. the winner is one who scored maximum total points in that group .in case of a tie the lowest
player_id wins
THANK YOU!

More Related Content

Similar to IMPORTAnt sql ques for data analyst interview.pdf (20)

DOCX
Useful date number and string sql queries
Feras Ahmad
 
PDF
Sql queries
Paritosh Gupta
 
PPTX
NumPy_Aggregations - Python for Data Science.pptx
JohnWilliam111370
 
PPT
Sub queries
VARSHAKUMARI49
 
PDF
Data Exploration with Apache Drill: Day 2
Charles Givre
 
DOCX
Database Query Using SQL_ip.docx
VandanaGoyal21
 
PPT
Module03
Sridhar P
 
DOCX
Complex queries in sql
Charan Reddy
 
PDF
Simran kaur,BCA Final Year 2015
dezyneecole
 
DOCX
DECLARE example
Christopher Smith
 
DOC
Sql task answers
Nawaz Sk
 
PDF
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
dezyneecole
 
PDF
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SudhanshuPandey222889
 
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
PPTX
Comparing 30 Elastic Search operations with Oracle SQL statements
Lucas Jellema
 
DOCX
Oracle SQL
Anar Godjaev
 
PDF
Nikhil Khandelwal BCA 3rd Year
dezyneecole
 
PPTX
Lecture 8 DML3 aggregate functions in DB.pptx
imranahmadrana28
 
Useful date number and string sql queries
Feras Ahmad
 
Sql queries
Paritosh Gupta
 
NumPy_Aggregations - Python for Data Science.pptx
JohnWilliam111370
 
Sub queries
VARSHAKUMARI49
 
Data Exploration with Apache Drill: Day 2
Charles Givre
 
Database Query Using SQL_ip.docx
VandanaGoyal21
 
Module03
Sridhar P
 
Complex queries in sql
Charan Reddy
 
Simran kaur,BCA Final Year 2015
dezyneecole
 
DECLARE example
Christopher Smith
 
Sql task answers
Nawaz Sk
 
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
dezyneecole
 
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SudhanshuPandey222889
 
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
Comparing 30 Elastic Search operations with Oracle SQL statements
Lucas Jellema
 
Oracle SQL
Anar Godjaev
 
Nikhil Khandelwal BCA 3rd Year
dezyneecole
 
Lecture 8 DML3 aggregate functions in DB.pptx
imranahmadrana28
 

Recently uploaded (20)

PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Ad

IMPORTAnt sql ques for data analyst interview.pdf

  • 2. Find the employees having salary more than thier manager salary Select employee.emp_id, employee.emp_name, M.emp_name as manager_name, employee.salary, M.salary as Manager_salary from employee Inner Join employee M On employee.manager_id = M.emp_id Where employee.salary > M.salary;
  • 3. Find the employee with second highest salary from each department Select * from (select emp.*, dense_rank() Over(Partition By department_id Order By salary Desc ) as rn from emp) a where rn= 2;
  • 4. find employees who are not present in the depratment table Select emp.*, dept.dept_id, dept.dept_name from emp Left Join dept On emp.department_id = dept.dept_id where dept.dept_id is Null;
  • 5. How to find the mode --Method 1 With Freq_cte as( Select id, count(*) as freq from mode group By id) Select * from freq_cte where freq = (select Max(freq) from Freq_cte); --Method 2 With Freq_cte as( Select id, count(*) as freq from mode group By id), rnk_cte as( Select *, rank() over(order By freq desc) as rn from Freq_cte) Select * from rnk_cte where rn = 1
  • 6. SELECT e20.*, e21.*, CASE WHEN e20.designation IS DISTINCT FROM e21.designation THEN 'Promoted' WHEN e21.designation IS NULL THEN 'Resigned' ELSE 'New' END AS comment FROM emp_2020 e20 FULL OUTER JOIN emp_2021 e21 ON e20.emp_id = e21.emp_id WHERE COALESCE(e20.designation, 'XXX') != COALESCE(e21.designation, 'YYY'); find the change in employee status from 2020 to 2021 -> Example of full outer join
  • 7. With cte_dup as( Select id from list group by id Having count(1)>1), cte_rank as( select id, rank() Over(order by id asc) as rn from cte_dup) select l.id, 'DUP' || cast(cr.rn as varchar(2)) as output from list l Left Join cte_rank cr On l.id = cr.id Rank the duplicate records
  • 8. Select *, sum(cost) Over(order by cost asc, product_id) as running_cost from product; find the running sum
  • 9. --method1 create table empl_backUp as Select * from empl; Delete from empl; Insert Into empl Select distinct * from empl_backUP; Select * from empl Identify and delete excat duplicate record --method2 Insert Into empl select id,name, salary, joining_date from (Select *, row_number() Over(partition by id order by salary) as rn from empl_backUP ) A where rn =1;
  • 10. with cte as( Select Customer_Name,region, sum(sales) as total_Sales from Order_A group by Customer_Name, region Order By region, total_sales desc Limit 18) Select * from (Select * , NTILE(4) Over (Partition By region Order by total_Sales) as cust_groups from cte) a where cust_groups IN(1) NTILE function
  • 11. Select gold as player_name, count(1) as no_of_medals from events where gold not in (Select silver from events Union All Select bronze from events) group by gold; Find no of goldmedal per swimmer for swimmers who only won gold medals.
  • 12. Select gold as player_name, count(1) as no_of_medals from events where gold not in (Select silver from events Union All Select bronze from events) group by gold; Find no of goldmedal per swimmer for swimmers who only won gold medals. --using CTE With cte as (Select gold as player_name, 'gold' as medal_type from events Union All Select silver, 'silver' as medal_type from events Union All Select bronze, 'bronze' as medal_type from events) Select player_name, count(1) as no_of_gold_medals from cte group by player_name having count (distinct medal_type) = 1 and max(medal_type) = 'gold';
  • 13. SELECT t.ticket_id, t.create_date, t.resolved_date, COUNT(*) AS business_days FROM tickets t JOIN generate_series(t.create_date, t.resolved_date, interval '1 day') AS dates ON EXTRACT(ISODOW FROM dates) < 6 -- Exclude Saturday (6) and Sunday (7) LEFT JOIN holidays h ON dates = h.holiday_date WHERE h.holiday_date IS NULL GROUP BY t.ticket_id, t.create_date, t.resolved_date; find business days between create days and resolved days excluding weekends and public holidays
  • 14. --method1 with cte as( select emp_id, max(case when action='in' then time end) as intime, max(case when action = 'out' then time end) as outtime from hospital group by emp_id) Select * from cte where intime > outtime or outtime is null find the total no of people present inside the hospital --method 2 with latest_time as( select emp_id, max(time) as max_latest_time from hospital group by emp_id), latest_in_time as (select emp_id, max(time) as max_in_time from hospital where action = 'in' group by emp_id) select * from latest_time lt inner Join latest_in_time lit on lt.emp_id=lit.emp_id and max_latest_time = max_in_time;
  • 15. with sal_dep as ( select dept_id, salary from emp_salary group by dept_id, salary having count(1)>1) Select es.* from emp_salary es inner join sal_dep sd ON es.dept_id= sd.dept_id and es.salary = sd.salary return all employees whose salary same in same department -- left join method 2 with sal_dep as ( select dept_id, salary from emp_salary group by dept_id, salary having count(1)=1) Select es.* from emp_salary es left join sal_dep sd ON es.dept_id= sd.dept_id and es.salary = sd.salary where sd.dept_id is null
  • 16. SELECT word, COUNT(*) AS cnt_of_words FROM ( SELECT regexp_split_to_table(content, ' ') AS word FROM namaste_python ) AS split_words GROUP BY word HAVING COUNT(*) > 1 ORDER BY cnt_of_words desc; find the words whicch are repeating more than once considering all the rows of content column
  • 17. with cte as( select m.genre, m.title, round(avg(r.rating),0) as avg_rating, row_number() over(partition by m.genre order by avg(r.rating) desc) as rn from movies m inner join reviews r On m.id = r.movie_id group by m.genre, m.title order by m.genre, avg_rating desc) select genre, title, avg_rating , Repeat('*',avg_rating::int) as stars from cte where rn = 1 find, within each genre, the movie which have highest average rating and also print the star *****
  • 18. --approach1 with cte as( select dep_id, max(salary) as max_salary, min(salary) as min_salary from employee1 group by dep_id) select e.dep_id, max(case when salary = max_salary then emp_name else null end )as max_sal_emp, min(case when salary = min_salary then emp_name else null end )as min_sal_emp from employee1 e inner join cte On e.dep_id = cte.dep_id group by e.dep_id; write the query to find the highest and lowest salary employee in each department --approach2 with cte as( select *, rank() over(partition by dep_id order by salary desc) as rank_desc, rank() over(partition by dep_id order by salary asc) as rank_asc from employee1) select dep_id, max(case when rank_desc = 1 then emp_name end) as max_sal_emp ,min(case when rank_asc = 1 then emp_name end )as min_sal_emp from cte group by dep_id
  • 19. --method1 Select coalesce(s.id,t.id) as id, -- s.name,t.name case when t.name is null then 'new in source' when s.name is null then 'new in target' else 'mismatched' end as comment from source s full outer join target t On s.id = t.id where s.name != t.name or s.name is null or t.name is null find the names which are not common in both source and target table. --method2 with cte as( select *, 'source' as table_name from source union all select *, 'target' as table_name from target ) select id, case when min(name) != max(name) then 'mismatched' when min(table_name)= 'source' then 'new in source' when max(table_name) = 'target' then 'new in target' end as comment from cte group by id having count(*) =1 or( count(*) = 2 and min(name) != max(name))
  • 20. select o.cid, o.origin, o.destination as final_destination from flights o inner join flights d On o.destination = d.origin find the original and final destination for each flight
  • 21. Select order_date, count(distinct customer) as count_new_cust from( select *, rank() over(partition by customer order by order_date) as rn from sales) a where rn = 1 group by order_date find the count of new customer in each month
  • 22. -- approach 1 select r.c_id, p.name as child_name, max(m.name) as mother_name, max(f.name) as father_name from relations r left join people m on r.p_id = m.id and m.gender = 'F' left join people f on r.p_id = f.id and f.gender = 'M' inner join people p On p.id = r.c_id group by r.c_id, p.name find the father's name and mother's name of every child --approach 2 select r.c_id as child_id, p.name as child_name, max(case when p.gender = 'F' then name end) as mother_name, max(case when p.gender = 'M' then name end) as father_name from relations r inner join people p on r.p_id=p.id group by 1, p.name
  • 23. --appraoch2 with cte as( select *, revenue - lag(revenue,1,0) over(partition by company order by year) as rev_diff from company_revenue ) select company from cte where company not in (select company from cte where rev_diff < 0) group by company find the compnies which revenue is icreasing (no dip) --approach1 with cte as( select *, revenue - lag(revenue,1,0) over(partition by company order by year) as rev_diff, count(1) over(partition by company) as cnt from company_revenue ) select company, cnt, count(1) as sales_inc_year from cte where rev_diff > 0 group by company, cnt having cnt = count(1)
  • 24. all the children will go on ride with adults only. adults can go alone if no more children is left for pairing. and eldest person will pair with youngest child. with cte_adult as( select *, row_number() over(order by age desc) as rn from family where type = 'Adult' ), cte_child as( select *, row_number() over(order by age asc) as rn from family where type = 'Child') select a.person, c.person, a.age as adult_age, c.age as child_age from cte_adult a left join cte_child c On c.rn = a.rn
  • 25. determine the team which qualifies the below criteria 1.Both Criteria1 and Criteria2 are marked as 'Y' for a particular team. 2.The count of such occurrences of both 'Y' criteria within each team is at least 2. --approach1 with qualified_team as( select teamID, count(1) as no_of_eligible_members from Ameriprise_LLC where Criteria1 = 'Y' and Criteria2 = 'Y' group by teamID having count(1) >= 2) select al.*,qt.* , case when Criteria1 = 'Y' and Criteria2 = 'Y' and qt.no_of_eligible_members is not null then 'Y' else 'N' end as qualified_flag from Ameriprise_LLC al left join qualified_team qt On al.teamID = qt.teamID --approach 2 select al.*, case when Criteria1 = 'Y' and Criteria2 = 'Y' and sum(case when Criteria1 = 'Y' and Criteria2 = 'Y' then 1 else 0 end) over (partition by teamID) >=2 then 'Y' else 'N' end as qualified_flag from Ameriprise_LLC al
  • 26. determine the team which qualifies the below criteria 1.Both Criteria1 and Criteria2 are marked as 'Y' for a particular team. 2.The count of such occurrences of both 'Y' criteria within each team is at least 2.
  • 27. Top 2 highest salried employees in each dept select * from( Select *, row_number() over(partition by department_id order by salary desc) as rn, dense_rank() over(partition by department_id order by salary desc) as dense_rn from emp)a --where rn <= 2 where dense_rn <= 2
  • 28. Top 5 products by category and sales with cte as ( select category, product_id, sum(sales) as sales from order_a group by category, product_id ) select * from( select *, row_number() over(partition by category order by sales desc) as rn from cte) where rn <= 5
  • 29. YOY grwoth/category wise products with sales more than pervious yesr sales(lead / lag function use case) with cte as( SELECT category, EXTRACT(YEAR FROM order_date) AS year_order, SUM(sales) AS sales FROM order_a GROUP BY category, EXTRACT(YEAR FROM order_date) order BY category, EXTRACT(YEAR FROM order_date) ), cte2 as( select *, lag(sales,1,sales) over(partition by category order by year_order) as prev_year_sales from cte ) select *, Round((sales - prev_year_sales)*100/ prev_year_sales,0) as YOY_sales from cte2
  • 30. YOY grwoth/category wise products with sales more than pervious yesr sales(lead / lag function use case)
  • 31. runnuing/cummulative sales year wise/ rolling n months sales with cte as( SELECT category, EXTRACT(YEAR FROM order_date) AS year_order, SUM(sales) AS sales FROM order_a GROUP BY category, EXTRACT(YEAR FROM order_date) order by category, EXTRACT(YEAR FROM order_date) ) select *, sum(sales) over(partition by category order by year_order ) as cummulative_sales from cte
  • 32. pivoting -> converting rows into column ->year wise sales for each category Select Extract(Year from order_date) as year_order, Sum(case when category = 'Furniture' then sales else 0 end ) AS furniture_sales, Sum(case when category = 'Office Supplies' then sales else 0 end ) AS OS_sales, Sum(case when category = 'Technology' then sales else 0 end ) AS technology_sales Into order_a_b FROM order_a Group by Extract(year from order_date)
  • 33. find the category of games 1. No social interaction 2.one sided interaction 3.Both sided intraction without custome_typed messages 4.both side interaction with custome_typed message from at least one player SELECT game_id, CASE WHEN count(interaction_type) = 0 THEN 'No social interaction' WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 1 THEN 'One sided interaction' WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 2 AND count(DISTINCT CASE WHEN interaction_type = 'custom_typed' THEN user_id END) = 0 THEN 'Both sided interaction without custom_typed messages' WHEN count(DISTINCT CASE WHEN interaction_type IS NOT NULL THEN user_id END) = 2 AND count(DISTINCT CASE WHEN interaction_type = 'custom_typed' THEN user_id END) >= 1 THEN 'Both sided interaction with custom_typed message from at least one player' END AS game_type FROM user_interactions GROUP BY game_id;
  • 34. find the category of games 1. No social interaction 2.one sided interaction 3.Both sided intraction without custome_typed messages 4.both side interaction with custome_typed message from at least one player
  • 35. Extract First name, middle name, last name With cte as ( select *, length(customer_name) - length(replace(customer_name,' ','')) as no_of_space, Position(' 'IN customer_name) as first_space_position, Position(' ' IN Substring(customer_name From Position(' ' IN customer_name) + 1)) + Position(' ' IN customer_name) AS second_space_position from customers ) select *, case when no_of_space = 0 then customer_name else left(customer_name, first_space_position-1) end as First_name, case when no_of_space <= 1 then null else substring(customer_name, first_space_position +1, second_space_position - (first_space_position )) end as middle_name, case when no_of_space =0 then null when no_of_space = 1 then substring(customer_name, first_space_position +1, length(customer_name)- first_space_position) when no_of_space = 2 then substring(customer_name, second_space_position+1, length(customer_name)- second_space_position) end as last_name from cte
  • 36. Extract First name, middle name, last name
  • 37. Return the record which have 3 or more consecutive rows where no. of people is more than 100(inclusive) with cte as ( select *, row_number() over(order by visit_date) as rn, id - row_number() over(order by visit_date) as grp from stadium where no_of_people >= 100) select id,visit_date,no_of_people from cte where grp IN( select grp from cte group by grp having count(1) >=3)
  • 38. Find the employees whose salary is median of salary of all employees company wise select company,avg(salary) from( select *, row_number() over(partition by company) as rn, count(1) over(partition by company) as total_count from median_salary) a where rn between (total_count*1.0)/2 and (total_count*1.0 )/2 +1 group by company
  • 39. list of players according to city -> pivoting select max(case when city = 'Bangalore' then name end) as Bangalore, max(case when city = 'Mumbai' then name end) as Mumbai, max(case when city = 'Delhi' then name end) as Delhi from (select *, row_number() over(partition by city) as players_location from players_location) a group by players_location order by players_location
  • 40. return the second most recent activity, if there is only one then return first with cte as ( select *, count(1) over(partition by username) as total_activities, row_number() over(partition by username order by startDate) as rn from UserActivity ) select * from cte where total_activities = 1 or rn = 2
  • 41. Total sales by year with recursive r_cte as( select min(period_start)::date as dates, max(period_end)::date as max_date from recursive_sales Union All select (dates + INTERVAL '1 day')::date, max_date::date from r_cte where dates < max_date) SELECT product_id, Extract(year from dates) as report_year, sum(average_daily_sales) as total_amount from r_cte inner join recursive_sales On dates between period_start and period_end group by product_id, report_year order by product_id, report_year
  • 42. with cte as ( select spend_date, user_id, max(platform) as platform, sum(amount) as amount from spending group by user_id, spend_date having count(distinct platform) =1 Union All select spend_date, user_id, 'Both' as platform, sum(amount) as amount from spending group by user_id, spend_date having count(distinct platform) =2) select spend_date, platform, sum(amount) as total_amount, count(distinct user_id) as total_users from cte group by spend_date, platform order by spend_date, platform Find the total number of users and total amount spent using mobile only, desktop only and mobile and desktop both for each date
  • 43. select * from( select *, rank() over(partition by player_id order by event_date) as rn from activity)a where rn = 1 Write a SQL query that reports the device that is first logged in for each player
  • 44. select *, sum(games_played) over(partition by player_id order by event_date) as total_palyed from activity Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date.
  • 45. with rnk_orders as( select *, rank() over(partition by seller_id order by order_date asc) as rn from orderss) select u.user_id as seller_id, case when i.item_brand = u.favorite_brand then 'Yes' else 'No' end as item_fav_brand from users U Left join rnk_orders ro on ro.seller_id = u.user_id and rn = 2 Left join items i on i.item_id = ro.item_id Write an SQL query that reports the fraction of playFind for each seller, whether the brand of the second item (by date) is his favourite brand. if a seller has sold leass than 2 item then report as No for him in the answerers that logged in again on the day after the day they first logged in, rounded to 2 decimal places
  • 46. with player_scores as( select first_player as player_id, first_score as score from matches union all select second_player as player_id, second_score as score from matches) , final_scores as( select p.group_id, ps.player_id, sum(score) as score from player_scores ps inner join players p on p.player_id = ps.player_id group by ps.player_id, p.group_id ), final_ranking as( select *, rank() over(partition by group_id order by score desc, player_id asc) as rn from final_scores) select * from final_ranking where rn = 1 Find the winner in each group. the winner is one who scored maximum total points in that group .in case of a tie the lowest player_id wins