Aggregate Functions & Grouping (51–65)
-
What is an aggregate function?
It performs calculation on multiple rows and returns one result.
-
Common aggregate functions?
COUNT, SUM, AVG, MIN, MAX.
-
What does COUNT(*) do?
Counts all rows including NULLs.
-
Difference between COUNT(*) and COUNT(column)?
COUNT(column) ignores NULLs.
-
What is GROUP BY?
Groups rows with same values for aggregation.
-
What is HAVING?
Filters grouped data.
-
Can we use WHERE with aggregate?
No, use HAVING.
-
Find total salary by department.
SELECT dept, SUM(salary) FROM emp GROUP BY dept;
-
Find departments with more than 5 employees.
Use GROUP BY dept HAVING COUNT(*) > 5.
-
What is AVG?
Returns average value.
-
What is MAX?
Returns highest value.
-
What is MIN?
Returns lowest value.
-
Can GROUP BY be used with multiple columns?
Yes.
-
What is the order of execution?
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
-
Can we use alias in GROUP BY?
Usually no (depends on DB).
No comments:
Post a Comment