Grouping Data in SQL
Master SQL aggregation functions with GROUP BY clauses
SQL Aggregation Approaches
Single Aggregation
Returns one value for the entire dataset. Perfect when you need company-wide totals or overall statistics.
GROUP BY Aggregation
Returns multiple values, one per group. Essential for department-wise analysis or category breakdowns.
Multi-Column Grouping
Groups by multiple columns for granular analysis. Enables state-by-zipcode or department-by-role insights.
GROUP BY doesn't replace regular aggregation functions - it enhances them by allowing aggregation across multiple groups simultaneously.
Single vs Group Aggregation
| Feature | Single Aggregation | GROUP BY Aggregation |
|---|---|---|
| Output Rows | 1 row | Multiple rows |
| Use Case | Company-wide average | Per-department average |
| SQL Structure | SELECT AVG(salary) | SELECT dept, AVG(salary) GROUP BY dept |
| Result Type | Single summary value | One value per group |
GROUP BY Query Structure
Choose Grouping Column
Identify the column that defines your groups (e.g., department_id, state, category). This determines how data will be segmented.
Add to SELECT
Include the grouping column in your SELECT statement. You must show the groups you're creating to users.
Apply Aggregation
Add your aggregation function (COUNT, AVG, SUM, MIN, MAX) to calculate metrics for each group.
Write GROUP BY Clause
Include the same column(s) from SELECT in your GROUP BY clause. This is the mandatory syntax pattern.
Whatever you're grouping by, you're going to choose a column and you're going to show people that column. You're always going to have it in both places.
PostgreSQL allows you to use column aliases in GROUP BY clauses, making queries more readable and maintainable compared to SQL Server.
Query Results by State (Example)
Multi-Column Grouping
You can only SELECT columns that are in your GROUP BY clause plus aggregation functions. Adding other columns will cause errors because they don't make logical sense in grouped results.
String Functions in GROUP BY
LEFT Function
Extracts leftmost characters from strings. Use LEFT(zip_code, 5) to get only the first 5 digits of zip codes.
Data Simplification
String functions help reduce group granularity by trimming unnecessary detail from grouping columns.
GROUP BY Best Practices
Every non-aggregate column in SELECT must appear in GROUP BY
Sort by count DESC to see highest-performing groups first
Use LEFT, SUBSTRING to reduce unwanted granularity
Add complexity gradually as you understand the data patterns
Ensure COUNT, SUM, AVG calculations align with analysis goals
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways