0% found this document useful (0 votes)
7 views4 pages

SQL Interview Queries

The document contains a series of SQL queries designed to solve various common database problems. These include finding the second highest salary, identifying customers who purchased every month in 2024, and calculating a 3-day moving average on sales. Additional queries address employee salaries compared to their managers, deleting duplicate rows, and analyzing customer retention and order ID gaps.

Uploaded by

twegrow9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

SQL Interview Queries

The document contains a series of SQL queries designed to solve various common database problems. These include finding the second highest salary, identifying customers who purchased every month in 2024, and calculating a 3-day moving average on sales. Additional queries address employee salaries compared to their managers, deleting duplicate rows, and analyzing customer retention and order ID gaps.

Uploaded by

twegrow9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Interview Queries

1. Find the Second Highest Salary

SELECT MAX(salary) AS second_highest_salary

FROM employees

WHERE salary < (

SELECT MAX(salary)

FROM employees

);

2. Customers Who Purchased Every Month in 2024

SELECT customer_id

FROM orders

WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'

GROUP BY customer_id

HAVING COUNT(DISTINCT MONTH(order_date)) = 12;

3. Pivot Sales Data Into Months as Columns

SELECT

product_id,

SUM(CASE WHEN MONTH(order_date) = 1 THEN sales ELSE 0 END) AS Jan,

SUM(CASE WHEN MONTH(order_date) = 2 THEN sales ELSE 0 END) AS Feb,

...

SUM(CASE WHEN MONTH(order_date) = 12 THEN sales ELSE 0 END) AS Dec

FROM sales

GROUP BY product_id;
4. Employees Earning More Than Their Managers

SELECT e.employee_id, e.name, e.salary

FROM employees e

JOIN employees m ON e.manager_id = m.employee_id

WHERE e.salary > m.salary;

5. 3-Day Moving Average on Sales

SELECT

sales_date,

product_id,

AVG(sales_amount) OVER (

PARTITION BY product_id

ORDER BY sales_date

ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

) AS moving_avg

FROM sales;

6. Top 3 Highest-Paid Employees in Each Department

WITH ranked_employees AS (

SELECT *,

ROW_NUMBER() OVER (

PARTITION BY department_id

ORDER BY salary DESC

) AS rn

FROM employees

)
SELECT *

FROM ranked_employees

WHERE rn <= 3;

7. Delete Duplicate Rows But Keep One

WITH duplicates AS (

SELECT *,

ROW_NUMBER() OVER (

PARTITION BY name, email, department

ORDER BY id

) AS rn

FROM employees

DELETE FROM employees

WHERE id IN (

SELECT id FROM duplicates WHERE rn > 1

);

8. Identify Consecutive Login Days for Each User

SELECT user_id, login_date,

login_date - INTERVAL (ROW_NUMBER() OVER (

PARTITION BY user_id ORDER BY login_date

)) DAY AS group_id

FROM logins;

9. Calculate Month-over-Month Customer Retention

WITH monthly_logins AS (
SELECT customer_id, DATE_FORMAT(login_date, '%Y-%m') AS login_month

FROM logins

GROUP BY customer_id, DATE_FORMAT(login_date, '%Y-%m')

),

joined AS (

SELECT

curr.customer_id,

curr.login_month AS current_month,

prev.login_month AS previous_month

FROM monthly_logins curr

LEFT JOIN monthly_logins prev

ON curr.customer_id = prev.customer_id

AND curr.login_month = DATE_FORMAT(DATE_ADD(STR_TO_DATE(prev.login_month,

'%Y-%m'), INTERVAL 1 MONTH), '%Y-%m')

SELECT current_month, COUNT(DISTINCT customer_id) AS retained_customers

FROM joined

WHERE previous_month IS NOT NULL

GROUP BY current_month;

10. Find Gaps in Order ID Sequence

SELECT order_id,

LAG(order_id) OVER (ORDER BY order_id) AS prev_order_id,

order_id - LAG(order_id) OVER (ORDER BY order_id) AS gap

FROM orders

HAVING gap > 1;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy