Before continuing: This entry is part of the aliases guide. If you are new to the topic, start with Alias.
Query
SELECT status,
COUNT(*) AS total_orders,
ROUND(SUM(total), 2) AS total_amount
FROM orders
WHERE status <> 'cancelled'
GROUP BY status
ORDER BY total_amount DESC;total_orders explains the count and total_amount identifies the rounded sum. The second alias can also be used in ORDER BY on the major engines, which saves repeating the whole expression.
How aliases work with COUNT and SUM
Use names that describe the value and, where needed, its unit or scope: average_price_gbp, cancelled_orders or units_sold. Avoid names such as x, result or total when they could be ambiguous.
Common mix-ups with aliases on COUNT and SUM
Trying to use the alias too soon
The alias is created in the list of SELECT. Do not rely on it in WHERE, because the filter is applied before the output columns are produced. Write the original column or expression there instead.
The rules for reusing aliases in GROUP BY and HAVING differ between engines. MySQL allows several uses that SQL Server does not. For portable queries, repeat the expression when in doubt and keep the alias for the output and for ORDER BY.
Naming a metric means defining what it counts and what it adds up
A useful alias works as a contract with whoever receives the report. total_orders should count orders, not order lines. If you join each order to its products before grouping, an order with three lines can appear three times: the column name does not fix that multiplication. Check what each row represents before choosing COUNT(*) or a sum.
Explain the scope as well. The query excludes cancelled orders before calculating the metrics, so its figures do not cover every stored order. If you export the result, a name such as non_cancelled_amount_gbp can convey the rule and the unit better. When there are several currencies, adding them up under a single amount alias produces a meaningless number until you define a conversion or group by currency.
In the small case below there are two paid orders of 30 and 20 and one cancelled order of 99. The paid result is two orders and 50, not three and 149. Rounding is applied to the final sum; rounding each row first is a different operation. Check the output names as well as the numbers if a spreadsheet or an application depends on them.
Self-contained case to check in SQLite
This data set lets you check the filter's scope and the metric names at the same time.
WITH orders(status, total) AS (
VALUES ('paid', 30), ('paid', 20), ('cancelled', 99)
)
SELECT status, COUNT(*) AS total_orders,
ROUND(SUM(total), 2) AS total_amount
FROM orders
WHERE status <> 'cancelled'
GROUP BY status
ORDER BY status;| status | total_orders | total_amount |
|---|---|---|
| paid | 2 | 50 |
The cancelled row is removed before grouping. Changing only the alias does not alter any of the three values.
Check your interpretation: If you add another paid order of 10, which two metrics should change?
Show answer
order_total would go from 2 to 3 and total_amount from 50 to 60. The aliases do not change; only the values do.
Technical questions: PostgreSQL: aggregate functions (opens in a new tab).