01 / 04
The heading comes out as the expression
Join tables together
INPUT · Slides
01 / 04
The result of an aggregation takes whatever you wrote as its heading. Look at the first line below.
A heading of SUM(quantity) makes sense to whoever wrote it, but it reads badly to someone handed the result later on.
SELECT SUM(quantity) FROM orders;Result
SUM(quantity) 27
02 / 04
ASAdd AS name behind a column or an expression and the heading changes to that name. This is called an alias.
Nothing about the calculation changes. Only the heading does.
SELECT SUM(quantity) AS total FROM orders;Result
total 27
03 / 04
A calculation between columns can be named just as well. The longer the expression, the more a name earns its keep.
Below works out "10 points for each one" and calls it points.
SELECT id, quantity * 10 AS points FROM orders WHERE id = 3;Result
id | points 3 | 40
04 / 04
ORDER BY takes an alias directly. That saves writing a long expression twice, which pays off when you put aggregating and sorting together.
Let us write some. Aliases will keep coming to your rescue once you start joining, as well.
SELECT channel, SUM(quantity) AS total FROM orders GROUP BY channel ORDER BY total DESC;Result
channel | total shop | 10 web | 9 app | 8