The SELECT list can hold more than column names. It can hold an expression: a calculation that the database does for each row.
Calculate a column
SELECT name, price, price * 1.13
FROM products;| Operator | Means |
|---|---|
+ - | add, subtract |
* / | multiply, divide |
( ) | do this part first |
* and / are done before + and -. Operators of the same level go from left to right: 10 - 4 - 1 is 5.
The table does not change. The new values exist only in the result.
result
| name | price | ?column? |
|---|---|---|
| Kraken Plush | 29.99 | 33.8887 |
| Pod Golem Plush | 34.50 | 38.9850 |
| Root Hoodie | 49.95 | 56.4435 |
Name a column with AS
PostgreSQL calls an expression column ?column?. Give it a better name, an alias, with AS:
SELECT name, price * 1.13 AS with_tax
FROM products;An alias with a space or upper-case letters needs double quotes, because it is a name: AS "With Tax".
You can use the alias in ORDER BY:
SELECT name, price * 1.13 AS with_tax
FROM products
ORDER BY with_tax DESC;Join text with ||
|| joins text values into one text value:
SELECT name || ' from ' || city AS label
FROM customers;The result for Ava Chen is Ava Chen from Toronto.