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
[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;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
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
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;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(*) = 2Select 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
.......