SQL AS: rename a column and compute values
A query does more than copy the columns of a table: it can compute new values (a price including tax, a stock value) and join text together (a full name). The AS keyword then gives these computed columns a clear name.
Syntax
SELECT expression AS new_name
FROM table_name;How it works
Calculations
In the SELECT list, you can use the +, -, * and / operators between columns and numbers. The calculation is done for each row.
⚠️ In SQLite, dividing two integers gives an integer: 7 / 2 equals 3. To get 3.5, write 7 / 2.0 or multiply by 1.0 first.
Joining text
In SQLite (as in PostgreSQL and Oracle), the || operator glues two pieces of text together. MySQL uses the CONCAT() function instead, which recent SQLite versions also accept.
Aliases with AS
Without an alias, a computed column is named after its full expression, for example prix * stock. With AS valeur_stock, the result becomes readable. The AS keyword is optional (prix * stock valeur_stock also works), but writing it avoids a lot of confusion. Aliases are also used to shorten table names in joins.
Diagram
| nom | prix | stock |
|---|---|---|
| Clavier | 49 | 10 |
| Plaid | 25 | 4 |
| nom | valeur_stock |
|---|---|
| Clavier | 490 |
| Plaid | 100 |
Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).
Glossary: the sample database uses French names
clients- customers
commandes- orders
lignes_commande- order lines
produits- products
fournisseurs- suppliers
employes- employees
prenom / nom- first name / last name (or name)
ville- city
pays- country
prix- price
prix_unitaire- unit price
quantite- quantity
categorie- category
date_commande- order date
poste- job title
departement- department
salaire- salary
statut- status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled
Examples with their results
1. Compute a stock value
The price multiplied by the quantity in stock gives the value tied up in each product.
SELECT nom, prix, stock, prix * stock AS valeur_stock
FROM produits
LIMIT 6; Try it in the sandbox| nom | prix | stock | valeur_stock |
|---|---|---|---|
| Clavier mécanique | 15 | 105 | 1,575 |
| Souris sans fil | 103 | 75 | 7,725 |
| Écran 27" | 342 | 102 | 34,884 |
| Webcam HD | 101 | 21 | 2,121 |
| SSD 1To | 317 | 90 | 28,530 |
| Casque gaming | 98 | 37 | 3,626 |
6 rows in total
2. Compute a rounded price including tax
ROUND(x, 2) rounds to two decimal places.
SELECT nom, prix AS prix_ht, ROUND(prix * 1.2, 2) AS prix_ttc
FROM produits
LIMIT 6; Try it in the sandbox| nom | prix_ht | prix_ttc |
|---|---|---|
| Clavier mécanique | 15 | 18 |
| Souris sans fil | 103 | 123.6 |
| Écran 27" | 342 | 410.4 |
| Webcam HD | 101 | 121.2 |
| SSD 1To | 317 | 380.4 |
| Casque gaming | 98 | 117.6 |
6 rows in total
3. Build a full name
The first name, a space and the last name are glued together with ||.
SELECT prenom || ' ' || nom AS nom_complet, poste
FROM employes
LIMIT 6; Try it in the sandbox| nom_complet | poste |
|---|---|
| Chloé Dubois | PDG |
| Jade Lefebvre | Directeur commercial |
| Nathan Roux | Directrice technique |
| Samir Simon | Responsable RH |
| Alice Simon | Commercial |
| Nathan Martin | Commercial |
6 rows in total
Common mistakes
Integer division
Dividing two integers in SQLite truncates the result. Add a decimal to get an exact result.
SELECT 7 / 2 AS resultat;SELECT 7 / 2.0 AS resultat;Using + to join text
In SQLite, + is a math operator: it tries to convert the text to numbers and returns 0. Use ||.
SELECT prenom + ' ' + nom FROM clients;SELECT prenom || ' ' || nom FROM clients;An alias with spaces
An alias containing spaces or accents must be wrapped in double quotes. The simplest option is to use underscores.
SELECT prix * 1.2 AS prix ttc FROM produits;SELECT prix * 1.2 AS prix_ttc FROM produits;Exercises
Try writing the query yourself before opening the solution.
Challenge 1. Show the name of each product and its price increased by 10%, rounded to 2 decimals, in a column named nouveau_prix.
Show a solution
SELECT nom, ROUND(prix * 1.1, 2) AS nouveau_prix FROM produits;| nom | nouveau_prix |
|---|---|
| Clavier mécanique | 16.5 |
| Souris sans fil | 113.3 |
| Écran 27" | 376.2 |
| Webcam HD | 111.1 |
| SSD 1To | 348.7 |
| Casque gaming | 107.8 |
| Hub USB-C | 53.9 |
| Lampe LED | 56.91 |
29 rows in total · 8 shown
Try it in the sandboxChallenge 2. Show a single column “identite” in the format “Name (city)” for each customer.
Show a solution
SELECT nom || ' (' || ville || ')' AS identite FROM clients;| identite |
|---|
| Dubois (Lille) |
| Martin (Lille) |
| Laurent (Paris) |
| Lefebvre (Toulouse) |
| Laurent (Marseille) |
| Michel (Bordeaux) |
| Dubois (Casablanca) |
| Lambert (Montréal) |
120 rows in total · 8 shown
Try it in the sandboxThe interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.
Frequently asked questions
Is the AS keyword required?
No, SQL also accepts an alias written right after the expression. Writing AS makes the query clearer, though, and avoids mistaking an alias for a forgotten comma.
Can I use an alias in the WHERE clause?
Not in standard SQL: WHERE is evaluated before SELECT. SQLite tolerates it, but for portable code, repeat the expression in the WHERE clause or use a subquery.
How do I concatenate text in MySQL?
MySQL uses CONCAT(prenom, ' ', nom). SQLite, PostgreSQL and Oracle use the || operator.