SQL Aliases and Readable Results
What you will learn
- How to rename a column in the result with
AS - How to write a name that contains spaces (double quotes)
- How to give a table a short alias
- How to add comments and lay a query out so it is easy to read
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. Rename a column with AS
The title of a result column is normally the column name, or the text of the calculation. That is not always pretty. AS gives it a name of your choice:
SELECT name AS product, price - cost AS margin FROM products ORDER BY product_id LIMIT 5;
product margin
---------------- ------------------
Steel pan 2.6399999999999997
Ceramic mug 13.89
Wooden spoon set 16.2
Glass jar 2.95
Chopping board 11.769999999999998
(5 rows)
SELECT account_type AS type, balance_cents / 100.0 AS balance FROM accounts ORDER BY account_id LIMIT 5;
type balance
-------- -------
checking 1924.0
savings 520.78
savings 627.8
checking 999.0
checking 1531.0
(5 rows)
SELECT title AS assignment, max_points * 0.4 AS pass_mark FROM assignments ORDER BY assignment_id LIMIT 5;
assignment pass_mark
---------- ---------
Midterm 40.0
Project 40.0
Quiz 1 8.0
Homework 2 20.0
Homework 1 4.0
(5 rows)
Without AS, the second column would be titled with the whole calculation. An alias changes only the title in the result; the table is not touched. AS is optional (writing the name straight after the column also works) but writing it makes the query easier to read.
2. Names with spaces
A name with spaces or special characters goes in double quotes:
SELECT price AS "list price" FROM products ORDER BY product_id LIMIT 3;
list price
----------
6.6
44.1
29.5
(3 rows)
SELECT balance_cents AS "balance in cents" FROM accounts ORDER BY account_id LIMIT 3;
balance in cents
----------------
192400
52078
62780
(3 rows)
SELECT max_points AS "maximum points" FROM assignments ORDER BY assignment_id LIMIT 3;
maximum points
--------------
100
100
20
(3 rows)
Single quotes are for text values and double quotes are for names. Names without spaces, such as margin or list_price, need no quotes at all, so prefer them: they are easier to type and to use later.
3. Aliases and sorting
You can use an alias to sort. The ORDER BY clause is covered in chapter 2; here it shows that the alias is a real name you can use:
SELECT name AS product, price - cost AS margin FROM products ORDER BY margin DESC LIMIT 5;
product margin
------------ ------------------
Foam roller 34.21
Tea kettle 26.310000000000002
Highlighters 23.75
Water bottle 23.430000000000007
Drawing kit 20.659999999999997
(5 rows)
SELECT account_type AS type, balance_cents / 100.0 AS balance FROM accounts ORDER BY balance DESC LIMIT 5;
type balance
-------- -------
savings 5020.8
checking 4328.0
savings 4280.04
checking 4016.0
checking 3799.0
(5 rows)
SELECT title AS assignment, max_points * 0.4 AS pass_mark FROM assignments ORDER BY pass_mark DESC LIMIT 5;
assignment pass_mark
---------- ---------
Midterm 40.0
Project 40.0
Midterm 40.0
Project 40.0
Midterm 40.0
(5 rows)
4. Table aliases
A table can have an alias too. It shortens long names and becomes important when a query uses several tables (the chapter on joins):
SELECT x.name, x.price FROM products AS x ORDER BY x.product_id LIMIT 5;
name price
---------------- -----
Steel pan 6.6
Ceramic mug 44.1
Wooden spoon set 29.5
Glass jar 5.4
Chopping board 26.4
(5 rows)
SELECT x.account_type, x.balance_cents FROM accounts AS x ORDER BY x.account_id LIMIT 5;
account_type balance_cents
------------ -------------
checking 192400
savings 52078
savings 62780
checking 99900
checking 153100
(5 rows)
SELECT x.title, x.max_points FROM assignments AS x ORDER BY x.assignment_id LIMIT 5;
title max_points
---------- ----------
Midterm 100
Project 100
Quiz 1 20
Homework 2 50
Homework 1 10
(5 rows)
Once a table has an alias, use the alias for its columns (x.name in the example), as above.
5. Comments
A comment is text the database ignores. It explains the query to people. Two styles:
-- A comment runs from the two dashes to the end of the line.
SELECT name, -- the name
price /* a comment in the middle can use slash and star */
FROM products
ORDER BY product_id
LIMIT 3;
name price
---------------- -----
Steel pan 6.6
Ceramic mug 44.1
Wooden spoon set 29.5
(3 rows)
-- A comment runs from the two dashes to the end of the line.
SELECT account_type, -- the name
balance_cents /* a comment in the middle can use slash and star */
FROM accounts
ORDER BY account_id
LIMIT 3;
account_type balance_cents
------------ -------------
checking 192400
savings 52078
savings 62780
(3 rows)
-- A comment runs from the two dashes to the end of the line.
SELECT title, -- the name
max_points /* a comment in the middle can use slash and star */
FROM assignments
ORDER BY assignment_id
LIMIT 3;
title max_points
------- ----------
Midterm 100
Project 100
Quiz 1 20
(3 rows)
Write comments to say why, not what: "only active rows, finance asked for it" helps; "select the columns" does not.
6. Lay the query out
SQL does not care about line breaks or capital letters, but people do. A layout used in this course:
- keywords in capitals (
SELECT,FROM,AS); - each major part (
SELECT,FROM,WHERE,ORDER BY) on its own line, or the query on one line when it is short; - one column per line in a long list, with the commas at the end;
- a semicolon at the end of every statement.
Compare these two versions of the same query. Both give the same result, only one is pleasant to read:
SELECT name,price,price - cost AS margin FROM products ORDER BY product_id LIMIT 3;
name price margin
---------------- ----- ------------------
Steel pan 6.6 2.6399999999999997
Ceramic mug 44.1 13.89
Wooden spoon set 29.5 16.2
(3 rows)
SELECT name,
price,
price - cost AS margin
FROM products
ORDER BY product_id
LIMIT 3;
name price margin
---------------- ----- ------------------
Steel pan 6.6 2.6399999999999997
Ceramic mug 44.1 13.89
Wooden spoon set 29.5 16.2
(3 rows)
SELECT account_type,balance_cents,balance_cents / 100.0 AS balance FROM accounts ORDER BY account_id LIMIT 3;
account_type balance_cents balance
------------ ------------- -------
checking 192400 1924.0
savings 52078 520.78
savings 62780 627.8
(3 rows)
SELECT account_type,
balance_cents,
balance_cents / 100.0 AS balance
FROM accounts
ORDER BY account_id
LIMIT 3;
account_type balance_cents balance
------------ ------------- -------
checking 192400 1924.0
savings 52078 520.78
savings 62780 627.8
(3 rows)
SELECT title,max_points,max_points * 0.4 AS pass_mark FROM assignments ORDER BY assignment_id LIMIT 3;
title max_points pass_mark
------- ---------- ---------
Midterm 100 40.0
Project 100 40.0
Quiz 1 20 8.0
(3 rows)
SELECT title,
max_points,
max_points * 0.4 AS pass_mark
FROM assignments
ORDER BY assignment_id
LIMIT 3;
title max_points pass_mark
------- ---------- ---------
Midterm 100 40.0
Project 100 40.0
Quiz 1 20 8.0
(3 rows)
Worked example: make a result readable for a colleague
A colleague will paste a result into a report. You:
- choose the columns they need (lesson 1.2);
- give each calculated column a clear one-word name with
AS(section 1); - quote a name only if it must contain spaces (section 2);
- lay the query out and add one comment saying what it is for (sections 5 and 6).
Nothing in the data changed; only the way it is presented. The data is made up.
Common mistakes
- Using single quotes for a name (
AS 'margin'). Single quotes are for text; it may work in SQLite but is not portable. Use double quotes for names with spaces and nothing for simple names. - Using an alias for the table, then the old name for its columns. After
FROM products AS p, writep.name. - Aliases with spaces everywhere. They need quotes each time. Prefer
list_price. - Comments that repeat the code. Say why.
Key takeaways
ASnames a result column (or a table); the stored data does not change.- Double quotes are for names with spaces; single quotes are for text.
--and/* */start comments, which the database ignores.- A tidy layout costs nothing and makes a query easy to read.
Practice
1. Which query renames price to list_price in the result?
Show answer
SELECT price AS list_price FROM products;
2. How do you write an alias that contains a space?
Show answer
In double quotes: SELECT price AS "list price" FROM products;
3. Does AS change the table?
Show answer
No. It only names the column in the result.
4. After FROM customers AS c, how do you refer to the column city?
Show answer
As c.city.
5. Write a comment that runs to the end of the line, and one that sits inside a line.
Show answer
-- like this runs to the end of the line. /* like this */ can sit inside a line.
6. Why write the keywords in capitals?
Show answer
SQL does not need it, but it makes the structure of the query stand out so people read it faster.