SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed
SQL Playground
TutorialsPlayground
SQL FormatterSQL MinifierSyntax ValidatorJSON to SQLCSV to SQLSQL to JSONER Diagram Generator
Daily ChallengeInterviewsCheat SheetBlog
Back to Blog
2026-01-21
6 min read

SQL Conditional Aggregation: Beyond Basic GROUP BY

sqlaggregationreportingcase-whenanalytics

You've mastered COUNT(), SUM(), and GROUP BY. But what if you need to count only certain rows, or create a pivot table in a single query?

Conditional aggregation combines CASE WHEN with aggregate functions to answer complex questions without multiple queries or subqueries.

Conditional aggregation turning one table into KPI cards
Conditional aggregation turning one table into KPI cards

The Basic Pattern

Instead of:

-- Two separate queries
SELECT COUNT(*) FROM orders WHERE status = 'completed';
SELECT COUNT(*) FROM orders WHERE status = 'pending';

Use one query:

SELECT 
  COUNT(CASE WHEN status = 'completed' THEN 1 END) as completed,
  COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending
FROM orders;

Interactive Example: Order Status Report

Interactive SQL
Loading...

One query, one result row, all the answers!

SUM with Conditions

Calculate revenue by status:

Interactive SQL
Loading...

Note: Use ELSE 0 with SUM() to avoid NULL issues.

Creating Pivot Tables

Transform rows into columns - a classic pivot table:

Interactive SQL
Loading...

This turns vertical month data into horizontal columns - perfect for reports!

Calculating Percentages

Combine conditional count with total count:

Interactive SQL
Loading...

Conditional AVG and MIN/MAX

Works with any aggregate:

Interactive SQL
Loading...

Combining with GROUP BY

Create detailed breakdowns per group:

Interactive SQL
Loading...

Boolean Flags with CASE

Create flags for each row, then aggregate:

-- Flag orders, then summarize
SELECT 
  SUM(is_high_value) as high_value_count,
  SUM(is_new_customer) as new_customer_orders
FROM (
  SELECT 
    *,
    CASE WHEN amount > 500 THEN 1 ELSE 0 END as is_high_value,
    CASE WHEN customer_age_days < 30 THEN 1 ELSE 0 END as is_new_customer
  FROM orders
);

Quick Reference

GoalPattern
Count if conditionCOUNT(CASE WHEN cond THEN 1 END)
Sum if conditionSUM(CASE WHEN cond THEN value ELSE 0 END)
Average if conditionAVG(CASE WHEN cond THEN value END)
PercentageCOUNT(CASE...) * 100.0 / COUNT(*)
Pivot tableSUM(CASE WHEN col = 'X' ...) per column

Common Mistakes

  1. Forgetting ELSE in SUM: Without ELSE 0, you get NULL which propagates incorrectly.

  2. Using ELSE with COUNT: Don't need it - COUNT(NULL) returns 0 automatically.

  3. Complex conditions: Use AND/OR inside CASE:

    COUNT(CASE WHEN status = 'completed' AND amount > 100 THEN 1 END)
    

PostgreSQL FILTER Clause

PostgreSQL offers a cleaner syntax:

-- PostgreSQL only
SELECT 
  COUNT(*) FILTER (WHERE status = 'completed') as completed,
  SUM(amount) FILTER (WHERE status = 'pending') as pending_total
FROM orders;

This is equivalent to the CASE WHEN approach but more readable.

Conclusion

Conditional aggregation is a report-building superpower:

  • Turn multiple queries into one
  • Create pivot tables without complex joins
  • Calculate percentages and ratios
  • Build dashboards with a single SQL statement

The pattern is simple: put CASE WHEN inside any aggregate function. Master this technique and you'll write cleaner, faster SQL for any analytical reporting task.

Share this article:

Related Articles

sqlanalytics

Calculating Weighted Averages in SQL

Standard averages can be misleading. Learn how to calculate weighted averages in SQL to get more accurate insights from your data.

Read more
sqlanalytics

Mastering ROLLUP, CUBE, and GROUPING SETS in SQL

Stop running multiple queries for subtotals. Learn how to use advanced GROUP BY extensions to generate powerful reports in a single pass.

Read more
sqlanalytics

Time Series Analysis with SQL: Trends, Growth, and Moving Averages

Turn raw timestamps into business insights. Learn how to calculate Month-over-Month growth and smooth out noisy data with 7-day moving averages.

Read more
Previous

Calculating Percentiles and Median in SQL

Next

Mastering Temporary Tables in SQL

Comments

© 2026 SQL Playground. Built for developers.

PrivacyTermsAboutRSS Feed