Aggregation recipe
Group Excel Rows with SQL
For a quick summary that someone else can verify, a short GROUP BY query is often easier to pass around than a pivot-table setup.
The problem
A sales export is just a long list until somebody asks for totals by region, product, or month. A pivot table can do that well, but its setup is not always obvious to the next person.
The query below spells out all three measures—order count, average revenue, and total revenue—and sorts the finished summary in one pass.
Try the recipe
- 1Upload the sales demo and copy the generated table name.
- 2Replace tbl_your_session in the query.
- 3Execute the GROUP BY and compare total revenue, average revenue, and order count.
- 4Export the compact summary to Excel.
Ready-to-use query
Build a regional sales summary with three aggregate measures.
SELECT
region,
COUNT(*) AS orders,
ROUND(AVG(revenue), 2) AS average_revenue,
SUM(revenue) AS total_revenue
FROM tbl_your_session
GROUP BY region
ORDER BY total_revenue DESC;Replace tbl_your_session with the table name displayed after upload. The supplied demo is an .xlsx file; XLS-SQL also accepts XLS, CSV, TSV, ODS, JSON, HTML, and Parquet. On the AI page, the block is a prompt for Ask AI.

Why XLS-SQL fits this job
- COUNT, AVG, SUM, GROUP BY, HAVING, and ORDER BY behave as they do in PostgreSQL.
- The query itself records how every number was calculated.
- A compact summary is ready to download for a slide, email, or another workbook.
- There is no schema to maintain after the one-off job is done.
Tips & tricks
- Round averages in the query when the report does not need long decimal values.
- Use HAVING—not WHERE—when filtering on SUM, COUNT, or another aggregate.
- Group by more than one column to compare combinations such as region and product.
- Sort by the most important measure so the downloaded report is useful immediately.
FAQ
Is GROUP BY similar to an Excel pivot table?
Both summarize categories. SQL is text-based and repeatable; pivot tables provide a visual interface and richer interactive layout.
Can I group by two Excel columns?
Yes. Select both columns and list both in GROUP BY, for example region, product.
Can I filter grouped totals?
Yes. Use HAVING after GROUP BY to filter aggregate values such as SUM(revenue).