0% found this document useful (0 votes)
40 views

SQL Scenario-Based Interview Questions & Answers: Nitya Cloudtech PVT LTD

Uploaded by

Richard Smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

SQL Scenario-Based Interview Questions & Answers: Nitya Cloudtech PVT LTD

Uploaded by

Richard Smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Nitya CloudTech Pvt Ltd.

SQL Scenario-Based Interview


Questions & Answers

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:

WITH RECURSIVE reporting_hierarchy AS (


SELECT employee_id, manager_id
FROM employees
WHERE employee_id = <target_employee_id>

UNION ALL

SELECT e.employee_id, e.manager_id


FROM employees e
INNER JOIN reporting_hierarchy rh ON e.employee_id =
rh.manager_id
)
SELECT * FROM reporting_hierarchy;

3. Question: Write a query to calculate a moving average of sales over a


7-day window.
Answer:

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:

DELETE FROM employees AS e1


USING employees AS e2
WHERE e1.employee_id < e2.employee_id
AND similarity(e1.name, e2.name) > 0.8
AND e1.department = e2.department;

5. Question: Write a query to find the longest consecutive sequence of


login dates for each user.
Answer:

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;

7. Question: Write a query to pivot a table and get each department's


average salary by gender.
Answer:

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;

8. Question: How would you rank employees within their department,


without gaps, based on their performance score?
Answer:

SELECT employee_id, name, department, performance_score,


DENSE_RANK() OVER (PARTITION BY department ORDER BY
performance_score DESC) AS department_rank
FROM employees;

9. Question: Write a query to find overlapping time intervals between


employee shifts.
Answer:

SELECT e1.employee_id AS emp1_id, e2.employee_id AS emp2_id,


e1.start_time, e1.end_time, e2.start_time, e2.end_time
FROM shifts e1
JOIN shifts e2 ON e1.employee_id != e2.employee_id
AND e1.start_time < e2.end_time

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:

SELECT employee_id, name, salary,


(ROW_NUMBER() OVER (ORDER BY salary) - 1) / 5 + 1 AS
rank_group
FROM employees;

12. Question: How would you delete every nth row in a table?

Answer:

DELETE FROM employees


WHERE MOD(ROW_NUMBER() OVER (ORDER BY employee_id), n) = 0;

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:

SELECT month, value


FROM monthly_values
WHERE value > LAG(value) OVER (ORDER BY month);

15. Question: Write a query to count the number of distinct continuous


sequences in a binary column.
Answer:

SELECT COUNT(*) AS distinct_sequences


FROM (
SELECT binary_col,
ROW_NUMBER() OVER(ORDER BY id) -
ROW_NUMBER() OVER(PARTITION BY binary_col ORDER BY id) AS
grp
FROM binary_table
) AS sequence_groups
GROUP BY binary_col, grp;

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;

18. Question: Write a query to find users who logged in on at least 5


consecutive days.
Answer:

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;

19. Question: How would you write a query to identify cyclic


dependencies in a parent-child relationship table?
Answer:

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

WITH RECURSIVE dependency_path AS (


SELECT parent_id, child_id, ARRAY[parent_id] AS path
FROM relationships
UNION ALL
SELECT r.parent_id, r.child_id, dp.path || r.child_id
FROM relationships r
JOIN dependency_path dp ON dp.child_id = r.parent_id
WHERE NOT r.child_id = ANY(dp.path)
)
SELECT *
FROM dependency_path
WHERE ARRAY_LENGTH(path, 1) != ARRAY_LENGTH(UNIQUE(path), 1);

20. Question: Write a query to select rows where there’s a 20%


difference between a column value and the previous row’s value in a
sorted order.
Answer:

SELECT current_val, previous_val


FROM (
SELECT value AS current_val,
LAG(value) OVER (ORDER BY some_column) AS previous_val
FROM table_name
) AS diff
WHERE ABS(current_val - previous_val) / NULLIF(previous_val, 0) >
0.2;

http://www.nityacloudtech.com/ @nityacloudtech

You might also like