SQL Scenario-Based Interview Questions & Answers: Nitya Cloudtech PVT LTD
SQL Scenario-Based Interview Questions & Answers: Nitya Cloudtech PVT LTD
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
1. Question: Write a query to get the last non-null value for each
employee's salary from a time series of salary updates.
Answer:
SELECT employee_id,
FIRST_VALUE(salary) OVER (PARTITION BY employee_id ORDER BY
update_date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING) AS last_non_null_salary
FROM salary_updates
WHERE salary IS NOT NULL;
2. Question: How would you write a recursive query to find all the
reporting lines of a particular employee in a hierarchical organization?
Answer:
UNION ALL
SELECT sale_date,
amount,
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
AVG(amount) OVER (ORDER BY sale_date ROWS BETWEEN 6 PRECEDING
AND CURRENT ROW) AS moving_avg_7_days
FROM sales;
4. Question: How would you detect and delete rows that have almost
identical values (fuzzy duplicates) based on a similarity threshold?
Answer: doesn't natively support fuzzy matching, so you'd typically use a
Levenshtein or similarity function (depending on DB support). Here's an
example for Postgre:
WITH date_diffs AS (
SELECT user_id, login_date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY
login_date) -
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY
login_date) AS date_diff_group
FROM logins
)
SELECT user_id, MIN(login_date) AS start_date, MAX(login_date) AS
end_date, COUNT(*) AS consecutive_days
FROM date_diffs
GROUP BY user_id, date_diff_group
ORDER BY user_id, consecutive_days DESC;
6. Question: How would you find rows that appear in one table but not in
another, given a composite key?
Answer:
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2 ON t1.key1 = t2.key1 AND t1.key2 = t2.key2
WHERE t2.key1 IS NULL;
SELECT department,
AVG(CASE WHEN gender = 'Male' THEN salary END) AS
avg_male_salary,
AVG(CASE WHEN gender = 'Female' THEN salary END) AS
avg_female_salary
FROM employees
GROUP BY department;
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
AND e2.start_time < e1.end_time;
10. Question: How would you perform a full outer join if your dialect
does not support it?
Answer:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.id = table2.id
UNION
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.id = table2.id;
11. Question: How do you create a rank that restarts for every 5 rows?
Answer:
12. Question: How would you delete every nth row in a table?
Answer:
13. Question: Write a query to find the average monthly revenue growth
rate.
Answer:
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
WITH monthly_revenue AS (
SELECT DATE_TRUNC('month', sale_date) AS month, SUM(amount) AS
revenue
FROM sales
GROUP BY month
)
SELECT month,
(revenue - LAG(revenue) OVER (ORDER BY month)) /
NULLIF(LAG(revenue) OVER (ORDER BY month), 0) AS growth_rate
FROM monthly_revenue;
14. Question: How do you identify rows where the value has increased in
the current month compared to the previous month?
Answer:
16. Question: How would you generate all possible pairs from a table of
values?
Answer:
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
SELECT a.value AS value1, b.value AS value2
FROM values_table a
CROSS JOIN values_table b
WHERE a.value < b.value;
17. Question: How would you select the top N percentile of rows by score
in each category?
Answer:
SELECT *
FROM (
SELECT category, score,
NTILE(100) OVER (PARTITION BY category ORDER BY score
DESC) AS percentile_rank
FROM scores_table
) AS ranked
WHERE percentile_rank <= n;
WITH login_streaks AS (
SELECT user_id, login_date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY
login_date) -
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY
login_date) AS streak
FROM logins
)
SELECT user_id, MIN(login_date) AS start_date, MAX(login_date) AS
end_date
FROM login_streaks
GROUP BY user_id, streak
HAVING COUNT(login_date) >= 5;
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
http://www.nityacloudtech.com/ @nityacloudtech