Sams Teach Yourself SQL in 24 Hours, Third Edition By Ronald R. Plew, Ryan K. Stephens
Table of Contents
Hour 10. Sorting and Grouping Data
Why Group Data?
Grouping data is the process of combining columns with duplicate values in a logical order. For example, a database may contain information about employees; many employees live in different cities, while some employees live in the same city. You may want to execute a query that shows employee information for each particular city. You are grouping employee information by city, and a summarized report is created.
Suppose that you wanted to figure the average salary paid to employees according to each city. You would do this by using the aggregate function AVG on the SALARY column, as you learned last hour, and by using the GROUP BY clause to group the output by city.
Grouping data is accomplished through the use of the GROUP BY clause of a SELECT statement (query). Last hour, you learned how to use aggregate functions. During this lesson, you will see how aggregate functions are used in conjunction with the GROUP BY clause for the database to display results more effectively.