Sorting and Grouping in QUERY
Order results and aggregate them with ORDER BY and GROUP BY.
Sorting and Grouping in QUERY is a free Excel Formulas Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Excel Formulas Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Filtering
Filtering shows you the right rows, but real reports need order and totals. QUERY handles both with two more SQL-style clauses: ORDER BY and GROUP BY.
With these you can answer questions like which region sells the most or list deals from biggest to smallest, all in one formula.
Sorting With ORDER BY
The ORDER BY clause sorts your results by one or more columns. It goes after WHERE (if present).
By default it sorts ascending (smallest first, A to Z). This formula lists every row sorted by sales from low to high.
=QUERY(A1:D7, "SELECT A, B, D ORDER BY D", 1)Descending Order
Add DESC after the column to sort from highest to lowest. Use ASC to be explicit about ascending.
This shows your biggest sales first, perfect for a top performers list. Pair it with LIMIT to get a clean top 3.
=QUERY(A1:D7, "SELECT B, D ORDER BY D DESC LIMIT 3", 1)Sorting by Multiple Columns
List several columns in ORDER BY, separated by commas, to break ties. Sheets sorts by the first column, then uses the next to order rows that match.
Here results are grouped by region alphabetically, and within each region the highest sales come first.
=QUERY(A1:D7, "SELECT A, B, D ORDER BY A ASC, D DESC", 1)Introducing GROUP BY
GROUP BY collapses rows that share a value into a single summary row. It is how you build totals per category.
To use it, your SELECT combines the grouping column with an aggregate function like SUM, COUNT, or AVG applied to another column.
Summing per Group
This formula totals sales for each region. SUM(D) adds the Sales column, and GROUP BY A produces one row per region.
The result is a mini pivot table: East with its total, West with its total, all from a single function.
=QUERY(A1:D7, "SELECT A, SUM(D) GROUP BY A", 1)Counting per Group
Swap in COUNT to count rows instead of summing. This tells you how many deals each region closed.
You can use COUNT(D) to count non-empty sales cells, giving a quick row tally per group.
=QUERY(A1:D7, "SELECT A, COUNT(D) GROUP BY A", 1)Averaging per Group
Use AVG for the mean value in each group. Here we get the average deal size per region.
You can even mix aggregates: select both SUM(D) and AVG(D) in the same query to show total and average side by side.
=QUERY(A1:D7, "SELECT A, SUM(D), AVG(D) GROUP BY A", 1)Every Non-Aggregate Must Be Grouped
A common error: each column in SELECT that is not wrapped in an aggregate must appear in GROUP BY.
SELECT A, B, SUM(D) GROUP BY A fails because B is neither aggregated nor grouped. Either group by both, or drop B from the select list.
=QUERY(A1:D7, "SELECT A, B, SUM(D) GROUP BY A, B", 1)Sorting a Grouped Result
Combine the clauses to rank your summary. After grouping, sort by the aggregate to put the biggest group on top.
This lists each region with its total sales, ordered from highest total to lowest, a ready-made leaderboard.
=QUERY(A1:D7, "SELECT A, SUM(D) GROUP BY A ORDER BY SUM(D) DESC", 1)Labeling Aggregate Columns
Grouped columns get ugly headers like sum Sales. Add a LABEL clause to rename them for a polished report.
This renames the total column to Total Sales. The label text uses single quotes, just like filter values.
=QUERY(A1:D7, "SELECT A, SUM(D) GROUP BY A LABEL SUM(D) 'Total Sales'", 1)Quick Check
Check your grasp of sorting and grouping.
Recap
You can now shape QUERY results:
ORDER BY col [ASC|DESC]sorts, with extra columns to break tiesGROUP BYplusSUM,COUNT, orAVGbuilds per-category summaries- Non-aggregated selected columns must be in
GROUP BY LABELrenames aggregate headers
One formula now produces a sorted, grouped, labeled report.
=QUERY(A1:D7, "SELECT A, SUM(D) GROUP BY A ORDER BY SUM(D) DESC LABEL SUM(D) 'Total Sales'", 1)Frequently asked questions
Is the “Sorting and Grouping in QUERY” lesson free?
Yes — the full text of “Sorting and Grouping in QUERY” is free to read here on the web, and the Excel Formulas Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Excel Formulas Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sorting and Grouping in QUERY”?
Order results and aggregate them with ORDER BY and GROUP BY. You practise Excel Formulas Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Excel Formulas Academy?
No prior experience is required. Excel Formulas Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sorting and Grouping in QUERY” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Excel Formulas Academy lesson?
Yes. Every Excel Formulas Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Querying Data With QUERY
- Sorting and Grouping in QUERY
- Applying Formulas to Columns With ARRAYFORMULA
- Pulling Data With IMPORTRANGE