DataDriven
LearnPracticeInterviewDiscussDaily
HelpContactPrivacyTermsSecurityiOS App

© 2026 DataDriven

Loading lesson...

  1. Home
  2. Learn
  3. Aggregating: Beginner

Aggregating: Beginner

A million rows walk into a SUM...

A million rows walk into a SUM...

Category
SQL
Difficulty
beginner
Duration
38 minutes
Challenges
0 hands-on challenges

Topics covered: GROUP BY for categorizing data, COUNT variations (*,col,DISTINCT), SUM and AVG calculations, MIN and MAX for extremes, HAVING for filtered groups

Lesson Sections

  1. GROUP BY for categorizing data

    Understanding GROUP BY How GROUP BY Works Consider this sales data: Watch how rows with the same region value are gathered into groups, then collapsed into a single summary row per group. The result has one row per region. Each row summarizes all the sales in that region. The original 6 rows have been collapsed into 3 summary rows. Rules and Patterns GROUP BY Golden Rule This rule exists because SQL needs to know how to handle columns that have multiple values per group. If you group by region,

  2. COUNT variations (*,col,DISTINCT)

    COUNT Variations COUNT(*) - Count All Rows COUNT(column) Consider a table where some customers have not provided an email: COUNT(DISTINCT) 1,847 orders came from 892 unique customers. This tells you that some customers placed multiple orders. Practical Applications Comparing COUNT Variants Database Execution

  3. SUM and AVG calculations

    SUM Functions Basic Example SUM with GROUP BY Each group's amounts are added together into a single total. North's three sales (150 + 175 + 190) become 515, while East's lone sale stays at 160. Now you can instantly see which region generates the most revenue and sells the most units. SUM with Expressions You can sum calculated values, not just raw columns. This is common for computing totals from unit prices and quantities. The expression price * quantity is calculated for each row, then those

  4. MIN and MAX for extremes

    Basic MIN/MAX Usage Finding Extremes Within each category, the smallest price is selected. Electronics keeps 149 (ignoring 299 and 599), while Clothing keeps 29. This tells you the price range within each category. Electronics spans $450 while Clothing only spans $60. Advanced Applications MIN/MAX with Dates Now you can see each customer's entire order history at a glance: when they first ordered, their most recent order, and how many orders they have placed. MIN/MAX with Text Combining Aggregat

  5. HAVING for filtered groups

    Understanding HAVING Why We Need HAVING Consider this question: "Which regions have more than 100 orders?" Groups are formed first, then the count threshold is applied. North (4 orders) and South (3 orders) pass the filter, while East (1 order) is eliminated entirely. HAVING Examples This finds highly-rated products with enough reviews to be statistically meaningful. Both conditions must be true: at least 10 reviews AND average rating of 4.0 or higher. Practical Patterns WHERE vs HAVING Executio

Related

  • All Lessons
  • Practice Problems
  • Mock Interview Practice
  • Daily Challenges