Saturday, May 16, 2026

Aggregate Functions & Grouping (51–65)

 

Aggregate Functions & Grouping (51–65)

  1. What is an aggregate function?
    It performs calculation on multiple rows and returns one result.
  2. Common aggregate functions?
    COUNT, SUM, AVG, MIN, MAX.
  3. What does COUNT(*) do?
    Counts all rows including NULLs.
  4. Difference between COUNT(*) and COUNT(column)?
    COUNT(column) ignores NULLs.
  5. What is GROUP BY?
    Groups rows with same values for aggregation.
  6. What is HAVING?
    Filters grouped data.
  7. Can we use WHERE with aggregate?
    No, use HAVING.
  8. Find total salary by department.
    SELECT dept, SUM(salary) FROM emp GROUP BY dept;
  9. Find departments with more than 5 employees.
    Use GROUP BY dept HAVING COUNT(*) > 5.
  10. What is AVG?
    Returns average value.
  11. What is MAX?
    Returns highest value.
  12. What is MIN?
    Returns lowest value.
  13. Can GROUP BY be used with multiple columns?
    Yes.
  14. What is the order of execution?
    FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
  15. Can we use alias in GROUP BY?
    Usually no (depends on DB).

No comments:

Post a Comment