SQL Interview Questions Bootcamp

For Data Analyst interviews, SQL is a crucial skill, and LeetCode has many SQL practice questions that are commonly asked. Here are some of the most frequently asked SQL questions in interviews:

Basic SQL Questions:
[SQL 175] Combine Two Tables – Use LEFT JOIN to merge two tables.
👉 Link

[SQL 176] Second Highest Salary – Use LIMIT, OFFSET, DISTINCT, or DENSE_RANK().
👉 Link

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
Select Distinct Salary
from ( select salary, DENSE_RANK() OVER (ORDER BY Salary DESC ) As Rnk
From Employee) ranked
WHERE Rnk = N
);
END

select Distinct salary
from employee
ORDER BY Salary DESC
LIMIT 1 OFFSET N-1

Select score,
DENSE_RANK() OVER (ORDER BY SCORE DESC) as 'rank'
FROM SCORES
ORDER BY SCORE DESC;

[SQL 180] Consecutive Numbers – Use HAVING COUNT(*), GROUP BY, or LAG().
👉 Link

Intermediate SQL Questions:
[SQL 181] Employees Earning More Than Their Manager – Use SELF JOIN.
👉 Link

[SQL 182] Duplicate Emails – Use GROUP BY and HAVING COUNT(*) > 1.
👉 Link

[SQL 183] Customers Who Never Order – Use LEFT JOIN and IS NULL.
👉 Link

[SQL 184] Department Top Three Salaries – Use DENSE_RANK() in a subquery.
👉 Link

[SQL 185] Department Highest Salary – Use MAX() with GROUP BY and JOIN.
👉 Link

Advanced SQL Questions:
[SQL 570] Managers with at Least 5 Direct Reports – Use GROUP BY and HAVING COUNT(*) >= 5.
👉 Link

[SQL 586] Customer Placing the Largest Number of Orders – Use COUNT(), GROUP BY, and ORDER BY.
👉 Link

[SQL 601] Human Traffic of Stadium – Use LEAD() & LAG() window functions.
👉 Link

[SQL 608] Tree Node – Use CASE WHEN for hierarchical structures.
👉 Link

[SQL 627] Swap Salary – Use UPDATE with CASE WHEN.
👉 Link

Common Concepts Tested in Interviews:
JOIN operations: Inner Join, Left Join, Self Join
GROUP BY & HAVING: Aggregations like COUNT(), SUM(), AVG()
ORDER BY & LIMIT: Ranking and sorting
WINDOW FUNCTIONS: RANK(), DENSE_RANK(), ROW_NUMBER()
SUBQUERIES & CTEs: Used for data transformation
CASE WHEN: Conditional transformations
Second Highest Salary :

 SELECT distinct(Salary) as SecondHighestSalary FROM Employee 
 UNION SELECT NULL 
 ORDER BY SecondHighestSalary DESC LIMIT 1,1;
  1. Group Sold Products By The Date
    Select sell_date, Count(DISTINCT(Product)) as num_sold,
    GROUP_CONCAT(DISTINCT Product ORDER BY product) as Products
    from activities
    Group by Sell_date
    Order by sell_date

Code
1683. Invalid Tweets
select tweet_id
from tweets
WHERE LENGTH(content) > 15;

Code
1495. Friendly Movies Streamed Last Month
Select DISTINCT title
from Content C
Join TVProgram T
On T.content_id = C.content_id
where Left(Program_date, 7) ='2020-06'
AND C.Kids_content = 'Y'
AND C.content_type = 'Movies'
group by title

  1. NPV Queries
    Select Q.id, Q.year, Coalesce(N.npv, 0) as npv
    from Queries Q
    LEFT JOIN NPV N
    ON N.id = Q.id and
    N.year = Q.year;

Code
1378. Replace Employee ID With The Unique Identifier
Select Unique_id, name
FROM Employees E
Left join
EmployeeUNI U
ON E.id = U.id
Order by Name ASC

1407.Top Travellers
Select name, Coalesce(SUM(distance), 0) as travelled_distance
from Users U
LEFT JOIN Rides R
on U.id = R.user_id
Group by name
Order by travelled_distance DESC, Name

group by u_id........

Select name, Coalesce(SUM(distance), 0) as travelled_distance
from Users U
LEFT JOIN Rides R
on U.id = R.user_id
Group by u.id
Order by travelled_distance DESC, Name

Code
1241. Number of Comments per Post

select
s1.sub_id as post_id ,
count(distinct s2.sub_id) as number_of_comments
from
Submissions s1
left join
Submissions s2
on s1.sub_id=s2.parent_id
where s1.parent_id IS NULL
group by 1

  1. Weather Type in Each Country
Select Distinct C.Country_name as country_name,
Case When Avg(W.weather_state) <= 15 then 'Cold'
     When Avg(W.weather_state) >= 25 then 'Hot'
     Else 'Warm' End as weather_type
     from countries C
     Left Join Weather W
     ON C.country_id = W.country_id
where W.day between '2019-11-01' and '2019-11-30'
Group by C.country_ID;
  1. Customer Order Frequency
WITH CTE AS 
(
        SELECT
        C.CUSTOMER_ID, 
        C.NAME, 
        SUM(O.QUANTITY * P.PRICE) AS AMT,
        MONTH(O.ORDER_DATE) AS MONTH
        FROM CUSTOMERS AS C  LEFT JOIN ORDERS AS O
        ON C.CUSTOMER_ID = O.CUSTOMER_ID LEFT JOIN PRODUCT AS P
        ON O.PRODUCT_ID = P.PRODUCT_ID
        WHERE YEAR(O.ORDER_DATE) = 2020
        GROUP BY C.CUSTOMER_ID, C.NAME , MONTH(O.ORDER_DATE)
)

SELECT CUSTOMER_ID, NAME
FROM CTE
WHERE AMT >= 100 AND MONTH IN (06,07)
GROUP BY CUSTOMER_ID, NAME
HAVING COUNT(*) = 2
  1. Find Users With Valid E-Mails
    select *
    from users
    where mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode\.com$';

  1. Fix Product Name Format
    .....

Select LOWER(TRIM(product_name)) product_name,
left(sale_date,7) sale_date,
Count(sale_id) as total
from Sales
Group by 1, 2
Order By 1, 2

.......

Comments (0)