Interview Questions

1) Write a query to find the second highest salary from the employees table.


Table: employees

id name salary
1 Alice 5000
2 Bob 7000
3 Charlie 6000

 

Solution:

SELECT MAX(salary) AS second_highest_salary
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

 

2) Write a query to find employees who earn more than the average salary in their department.


Table: employees

id name salary department_id
1 Alice 5000 1
2 Bob 7000 1
3 Charlie 6000 2

 

Solution:

SELECT e.name, e.salary, e.department_id
FROM employees e
JOIN (
  SELECT department_id, AVG(salary) AS avg_salary
  FROM employees
  GROUP BY department_id
) d ON e.department_id = d.department_id
WHERE e.salary > d.avg_salary;

 

3) Write a query to find duplicate rows based on a specific column.


Table: employees

id name salary
1 Alice 5000
2 Bob 7000
3 Alice 5000

 

Solution:

SELECT name, COUNT(*) AS duplicate_count
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;

 

4) Write a query to delete duplicate rows while keeping one.


WITH CTE AS (
  SELECT name, salary, ROW_NUMBER() OVER (PARTITION BY name, salary ORDER BY id) AS row_num
  FROM employees
)
DELETE FROM employees
WHERE id IN (
  SELECT id
  FROM CTE
  WHERE row_num > 1
);

 

5) Write a query to find the Nth highest salary (e.g., 3rd highest).


SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2; -- Replace 2 with (N-1) for the Nth highest

 

6) Write a query to find employees who joined in the last 6 months.


Table: employees

id name hire_date
1 Alice 2024-06-01
2 Bob 2024-10-15
3 Charlie 2024-12-20

 

Solution:

SELECT name, hire_date
FROM employees
WHERE hire_date >= DATE_ADD(CURRENT_DATE, INTERVAL -6 MONTH);

 

7) Write a query to find the top 3 salaries in each department.


SELECT department_id, name, salary
FROM (
  SELECT department_id, name, salary,
         RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
  FROM employees
) ranked
WHERE rank <= 3;

 

8) Write a query to count how many employees are in each department.


SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;

 

9) Write a query to find managers who have at least three employees reporting to them.


Table: employees

id name manager_id
1 Alice NULL
2 Bob 1
3 Charlie 1
4 David 2

 

Solution:

SELECT manager_id, COUNT(*) AS employee_count
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING COUNT(*) >= 3;

 

10) Write a query to find days with no transactions from a transactions table.


Table: transactions

id transaction_date
1 2024-10-01
2 2024-10-02
3 2024-10-04

 

Solution:

WITH all_dates AS (
  SELECT DATE_ADD('2024-10-01', INTERVAL n DAY) AS transaction_date
  FROM generate_series(0, DATEDIFF(CURRENT_DATE, '2024-10-01')) n
)
SELECT d.transaction_date
FROM all_dates d
LEFT JOIN transactions t ON d.transaction_date = t.transaction_date
WHERE t.transaction_date IS NULL;

 

11) Write a query to find each customer and the date of their last purchase.


Table: purchases

customer_id purchase_date
1 2024-11-01
2 2024-11-05
1 2024-12-20

 

Solution:

SELECT customer_id, MAX(purchase_date) AS last_purchase_date
FROM purchases
GROUP BY customer_id;

 

12) Write a query to find products that were never sold.


Tables:

  • products
    | id | name |
    |----|---------|
    | 1 | Laptop |
    | 2 | Phone |

  • sales
    | product_id | quantity |
    |------------|----------|
    | 1 | 5 |

 

Solution:

SELECT p.name
FROM products p
LEFT JOIN sales s ON p.id = s.product_id
WHERE s.product_id IS NULL;

 

13) Write a query to calculate the cumulative sales for each transaction.


Table: sales

id amount
1 100
2 200
3 300

 

Solution:

SELECT id, amount,
       SUM(amount) OVER (ORDER BY id) AS running_total
FROM sales;

 

14) Write a query to find employees who work in all departments.


Tables:

  • employees
    | id | name |
    |----|------|
    | 1 | Bob |

  • employee_departments
    | employee_id | department_id |
    |-------------|---------------|
    | 1 | 1 |
    | 1 | 2 |

  • departments
    | id | name |
    |----|------|
    | 1 | HR |
    | 2 | IT |

 

Solution:

SELECT e.name
FROM employees e
JOIN employee_departments ed ON e.id = ed.employee_id
GROUP BY e.id, e.name
HAVING COUNT(DISTINCT ed.department_id) = (SELECT COUNT(*) FROM departments);

 

15) Write a query to find common records between two tables.


Tables:

  • table_a
    | id | name |
    |----|---------|
    | 1 | Alice |
    | 2 | Bob |

  • table_b
    | id | name |
    |----|---------|
    | 2 | Bob |
    | 3 | Charlie |

 

Solution:

SELECT a.id, a.name
FROM table_a a
INNER JOIN table_b b ON a.id = b.id AND a.name = b.name;

 

16) Write a query to find records that are in table_a but not in table_b.


SELECT a.id, a.name
FROM table_a a
LEFT JOIN table_b b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL;

 

17) Write a query to retrieve the first and last purchase date for each customer.


Table: purchases

customer_id purchase_date
1 2024-01-15
1 2024-02-20
2 2024-03-10

 

Solution:

SELECT customer_id,
       MIN(purchase_date) AS first_purchase,
       MAX(purchase_date) AS last_purchase
FROM purchases
GROUP BY customer_id;

 

18) Write a query to find the longest sequence of consecutive dates in a transactions table.


Table: transactions

id transaction_date
1 2024-01-01
2 2024-01-02
3 2024-01-04

 

Solution:

WITH ranked_dates AS (
  SELECT transaction_date,
         ROW_NUMBER() OVER (ORDER BY transaction_date) - DATEDIFF(transaction_date, '2024-01-01') AS grp
  FROM transactions
)
SELECT MIN(transaction_date) AS start_date, MAX(transaction_date) AS end_date
FROM ranked_dates
GROUP BY grp
ORDER BY (DATEDIFF(MAX(transaction_date), MIN(transaction_date))) DESC
LIMIT 1;

 

19) Write a query to find the top 3 products based on sales quantity.


Table: sales

product_id quantity
1 10
2 20
3 15

 

Solution:

SELECT product_id, quantity
FROM sales
ORDER BY quantity DESC
LIMIT 3;

 

20) Write a query to rank products by their sales quantity.


SELECT product_id, quantity,
       RANK() OVER (ORDER BY quantity DESC) AS rank
FROM sales;