Showing posts with label Basic Select Queries. Show all posts
Showing posts with label Basic Select Queries. 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.

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;