Showing posts with label Window Function Queries. Show all posts
Showing posts with label Window Function Queries. Show all posts

Friday, May 29, 2026

Top SQL Queries for Practice (With Short Answers) - Window Function Queries (61–80)


  1. Assign row numbers to employees
    SELECT emp_name, ROW_NUMBER() OVER (ORDER BY salary DESC) rn
    FROM Employee;
  1. Rank employees by salary
    SELECT emp_name, RANK() OVER (ORDER BY salary DESC) rnk
    FROM Employee;
  1. Dense rank employees by salary
    SELECT emp_name, DENSE_RANK() OVER (ORDER BY salary DESC) drnk
    FROM Employee;
  1. Find top 3 salaries
    SELECT *
    FROM (
SELECT *, DENSE_RANK() OVER (ORDER BY salary DESC) drnk
FROM Employee
    ) x
    WHERE drnk <= 3;
  1. Find top 3 salaries in each department
    SELECT *
    FROM (
SELECT *, DENSE_RANK() OVER (PARTITION BY dept_id
    ORDER BY salary DESC) drnk
FROM Employee
    ) x
    WHERE drnk <= 3;
  1. Find highest salary in each department
    SELECT *
    FROM (
SELECT *, RANK() OVER (PARTITION BY dept_id
    ORDER BY salary DESC) rnk
FROM Employee
    ) x
    WHERE rnk = 1;
  1. Calculate running salary total
    SELECT emp_name, salary,
    SUM(salary) OVER (ORDER BY emp_id) running_total
    FROM Employee;
  1. Calculate cumulative sales
    SELECT month, sales,
    SUM(sales) OVER (ORDER BY month) cumulative_sales
    FROM Sales;
  1. Get previous salary value
    SELECT emp_name, salary,
    LAG(salary) OVER (ORDER BY emp_id) prev_salary
    FROM Employee;
  1. Get next salary value
    SELECT emp_name, salary,
    LEAD(salary) OVER (ORDER BY emp_id) next_salary
    FROM Employee;
  1. Compare current and previous month sales
    SELECT month, sales,
    LAG(sales) OVER (ORDER BY month) prev_month_sales
    FROM Sales;
  1. Find salary difference from previous employee
    SELECT emp_name, salary,
    salary - LAG(salary) OVER (ORDER BY emp_id) diff
    FROM Employee;
  1. Find first salary in each department
    SELECT *,
    FIRST_VALUE(salary) OVER (PARTITION
    BY dept_id ORDER BY salary DESC)
    FROM Employee;
  1. Find last salary in each department
    SELECT *,
    LAST_VALUE(salary) OVER (PARTITION BY
    dept_id ORDER BY salary DESC)
    FROM Employee;
  1. Find average salary by department without grouping
    SELECT emp_name, dept_id,
    AVG(salary) OVER (PARTITION BY dept_id) avg_salary
    FROM Employee;
  1. Find percent contribution of salary
    SELECT emp_name, salary,
    salary * 100.0 / SUM(salary) OVER() pct
    FROM Employee;
  1. Find duplicate rows using row_number
    SELECT *
    FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION
    BY emp_name ORDER BY emp_id) rn
FROM Employee
    ) x
    WHERE rn > 1;
  1. Delete duplicate rows
    Use above query in CTE and delete where rn > 1.
  2. Find latest order per customer
    SELECT *
    FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY
    customer_id ORDER BY order_date DESC) rn
FROM Orders
    ) x
    WHERE rn = 1;
  1. Find first order per customer
    SELECT *
    FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY
    customer_id ORDER BY order_date ASC) rn
FROM Orders
    ) x
    WHERE rn = 1;