Skip to content
AgileLogics
5 min read

Choosing Columns, Aliases, and Calculations

Prerequisites: Your first query: SELECT

SELECT * is handy for a quick look, but real queries list the columns they need.

Choosing columns

SELECT name, price
FROM products;
+-----------------------------+--------+
|            name             | price  |
+-----------------------------+--------+
| Mechanical Keyboard         |     89 |
| USB-C Hub                   |   35.5 |
| 27-inch Monitor             | 249.99 |
| Laptop Stand                |     42 |
| Noise-Cancelling Headphones |    199 |
| Webcam                      |   59.9 |
+-----------------------------+--------+

Columns appear in the order you list them. Naming columns explicitly makes queries clearer, returns less data, and keeps working if someone adds columns to the table later.

Calculations

A SELECT can compute new values from columns:

SELECT name, price, price * 1.2 AS price_with_tax
FROM products;
+-----------------------------+--------+--------------------+
|            name             | price  |   price_with_tax   |
+-----------------------------+--------+--------------------+
| Mechanical Keyboard         |     89 |              106.8 |
| USB-C Hub                   |   35.5 |               42.6 |
| 27-inch Monitor             | 249.99 |            299.988 |
| Laptop Stand                |     42 |               50.4 |
| Noise-Cancelling Headphones |    199 | 238.79999999999998 |
| Webcam                      |   59.9 |              71.88 |
+-----------------------------+--------+--------------------+
  • price * 1.2 calculates a value for every row. The table itself isn't changed — this only affects the query's result.
  • AS gives the result column a name, called an alias. Without it, the column would be named after the expression.

Floating-point arithmetic can produce tiny rounding errors, like 238.79999999999998 for the headphones; the Numbers lesson shows how to round results.

Joining text

SELECT name || ' (' || category || ')' AS label
FROM products;
+-------------------------------------+
|                label                |
+-------------------------------------+
| Mechanical Keyboard (Accessories)   |
| USB-C Hub (Accessories)             |
| 27-inch Monitor (Displays)          |
| Laptop Stand (Accessories)          |
| Noise-Cancelling Headphones (Audio) |
| Webcam (Video)                      |
+-------------------------------------+

|| joins (concatenates) text in standard SQL, SQLite, and PostgreSQL. MySQL uses CONCAT(name, ' (', category, ')'), which also works in PostgreSQL and SQL Server.

Aliases with spaces

Put aliases containing spaces or capital letters in double quotes: AS "Price With Tax". Most teams prefer simple snake_case aliases instead.

Practice

Show each customer's name and email, with the columns renamed to customer and contact.

Show solution
SELECT name AS customer, email AS contact
FROM customers;
+-------------------+----------------------+
|     customer      |       contact        |
+-------------------+----------------------+
| Ada Lovelace      | ada@example.com      |
| Linus Torvalds    | linus@example.com    |
| Grace Hopper      | grace@example.com    |
| Alan Turing       | alan@example.com     |
| Margaret Hamilton | margaret@example.com |
| Tim Berners-Lee   | tim@example.com      |
+-------------------+----------------------+