Showing posts with label Top SQL Queries for Practice (With Short Answers). Show all posts
Showing posts with label Top SQL Queries for Practice (With Short Answers). Show all posts

Sunday, May 31, 2026

Top SQL Queries for Practice (With Short Answers) - Real-Time Scenario Queries (81–100)

 

  1. Find duplicate transactions
    Use GROUP BY transaction_id HAVING COUNT(*) > 1.
  2. Find inactive customers
    Find customers with no orders in last 6 months.
  3. Find repeat customers
    Group by customer and filter COUNT(order_id) > 1.
  4. Find one-time customers
    Group by customer and filter COUNT(order_id) = 1.
  5. Find top-selling products
    Group by product and order by total quantity sold desc.
  6. Find least-selling products
    Group by product and order by total quantity sold asc.
  7. Find average order value
    SUM(order_amount) / COUNT(order_id).
  8. Find monthly revenue
    Group by month and sum order amount.
  9. Find YoY sales growth
    Compare current year sales with previous year sales.
  10. Find MoM sales growth
    Use LAG() on monthly sales.
  11. Find customer lifetime value
    Sum total purchase amount by customer.
  12. Find customer churn
    Find customers with no recent activity.
  13. Find best performing region
    Group by region and sort by sales desc.
  14. Find worst performing region
    Group by region and sort by sales asc.
  15. Find null-heavy columns
    Use COUNT(*) - COUNT(column).
  16. Find bad quality records
    Check NULLs, duplicates, invalid formats.
  17. Find orphan records
    Use LEFT JOIN and filter unmatched child records.
  18. Validate source vs target count
    Compare COUNT(*) from both tables.
  19. Find changed records in ETL
    Compare source and target using hash/timestamp.
  20. Find load failures in ETL
    Check audit table, rejected rows, and error logs.

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;


Monday, May 25, 2026

Top SQL Queries for Practice (With Short Answers) - Basic Select Queries (1–15)

 

  1. Fetch all records from Employee table
    SELECT * FROM Employee;
  1. Fetch only employee names
    SELECT emp_name FROM Employee;
  1. Fetch unique department names
    SELECT DISTINCT department FROM Employee;
  1. Fetch employees with salary > 50000
    SELECT * FROM Employee WHERE salary > 50000;
  1. Fetch employees from HR department
    SELECT * FROM Employee WHERE department = 'HR';
  1. Fetch employees with salary between 30000 and 60000
    SELECT * FROM Employee WHERE salary BETWEEN 30000 AND 60000;
  1. Fetch employees in HR or IT
    SELECT * FROM Employee WHERE department IN ('HR','IT');
  1. Fetch employees whose name starts with A
    SELECT * FROM Employee WHERE emp_name LIKE 'A%';
  1. Fetch employees whose name ends with n
    SELECT * FROM Employee WHERE emp_name LIKE '%n';
  1. Fetch employees whose name contains 'ar'
    SELECT * FROM Employee WHERE emp_name LIKE '%ar%';
  1. Fetch employees with NULL manager_id
    SELECT * FROM Employee WHERE manager_id IS NULL;
  1. Fetch employees sorted by salary ascending
    SELECT * FROM Employee ORDER BY salary ASC;
  1. Fetch employees sorted by salary descending
    SELECT * FROM Employee ORDER BY salary DESC;
  1. Fetch top 5 highest paid employees
    SELECT TOP 5 * FROM Employee ORDER BY salary DESC;
  1. Fetch first 5 rows (MySQL)
    SELECT * FROM Employee LIMIT 5;