Modules / Database / Query Visualization

SQL GroupBy Visualizer

Interactively build and visualize how SQL aggregates data by grouping rows based on specific columns.

Source Table

SALES_DATA

Live Query

SELECT ... FROM SalesData;

Result Table

READY

Overview

GROUP BY divides filtered rows into groups with the same value for each grouping expression. An aggregate such as COUNT or SUM then summarizes each group. If you group orders by customer_id, the result has one row per customer who has at least one qualifying order.

What a group represents

query.sqlSQLite
Result

The query first removes unpaid rows, then groups the remaining rows and calculates values. WHERE filters input rows; HAVING filters completed groups. See HAVING for a threshold example and aggregate functions for NULL behavior.

Select only values that describe the group

In standard SQL, every selected expression that is not aggregated must be part of the grouping key (or otherwise be functionally determined where the engine supports that rule). Selecting an arbitrary name next to a grouped customer_id can be invalid or ambiguous. SQLite permits some bare columns and may choose a value from an input row; do not rely on that behavior for portable results.

If a customer's name is needed, group by both columns, join the grouped result back to customers, or use a window function if rows should remain separate. Decide based on the output grain you want.

NULL and empty groups

Rows with NULL in a grouping key are collected into one NULL group in common SQL engines. A group appears only when at least one input row belongs to it. A query with GROUP BY over no qualifying rows therefore returns no groups, while an aggregate without GROUP BY still returns one summary row.

Example: count distinct customers by month

If the output should be one row per month, group by the month expression and count customer IDs. Decide whether a customer with multiple orders in one month counts once; if so, COUNT(DISTINCT customer_id) expresses that rule. Be careful when grouping on formatted date text: sorting and time-zone conversion can change which month an event belongs to. Store or convert timestamps consistently, then group using an expression appropriate for the database. State the grain in a comment when the report is reused.

Further reading

Predict, then reveal

About to run: switch Aggregate Function to AVG() - Average Value. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “What a group represents”?

  2. What does this module say about “Select only values that describe the group”?

  3. What does this module say about “NULL and empty groups”?

Cheat sheet

SQL GroupBy Visualizer

Group rows by a column and watch COUNT, SUM and AVG collapse many rows into one row per group, live against a sample table.

DATABASE · vizlearn.in/database/groupby_in_sql.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.