Why do we use aggregation functions like SUM(), COUNT(), and AVG() in SQL queries?
Think about how you find totals or averages from many numbers.
Aggregation functions summarize many rows into one value, like total sales or average score, which helps in analyzing data.
Given the table Sales with columns Product and Amount, what is the output of this query?
SELECT Product, SUM(Amount) FROM Sales GROUP BY Product;
CREATE TABLE Sales (Product VARCHAR(10), Amount INT); INSERT INTO Sales VALUES ('Pen', 10), ('Pen', 15), ('Book', 20), ('Book', 30), ('Pencil', 5);
SUM adds all amounts for each product.
The query groups rows by product and sums the amounts for each group.
Which option contains a syntax error in this aggregation query?
SELECT Department, COUNT(Employee) FROM Employees GROUP BY Department;
Check the GROUP BY clause syntax carefully.
The correct syntax is GROUP BY, not GROUP alone.
You want to find the average salary per department from a large Employees table. Which query is more efficient?
Think about grouping and calculating averages directly.
Option A uses GROUP BY with AVG directly, which is efficient and clear.
Consider this query:
SELECT Department, COUNT(Employee), Salary FROM Employees GROUP BY Department;
Why does this query cause an error or unexpected result?
Check which columns must be grouped or aggregated.
Columns in SELECT must be either grouped or aggregated. Salary is neither, causing error.