What is the purpose of the GROUP BY clause in SQL?
a) To group rows that have the same values into summary rows
b) To sort the result set in ascending order
c) To limit the number of rows returned by the query
d) To delete duplicate rows from a result set
Answer:
a) To group rows that have the same values into summary rows
Explanation:
The GROUP BY clause in SQL is used to group rows that have the same values in specified columns into summary rows, such as totals or averages. It is typically used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), or MIN().
For example, SELECT department, COUNT(*) FROM employees GROUP BY department will group employees by their department and return the count of employees in each department.
GROUP BY helps in summarizing large datasets and is often used in reporting and data analysis.