Before continuing: This entry is part of the aliases guide. If you are new to the topic, start with Alias.
Query
SELECT name AS product,
ROUND(price * stock, 2) AS inventory_value
FROM products
WHERE stock > 0
ORDER BY inventory_value DESC, id
LIMIT 3;product replaces the technical heading name in the output. inventory_value describes the calculation and can be used in ORDER BY to sort the result once it has been built.
How aliases work in SELECT
AS makes the intent to name the result visible. Many engines let you leave it out, but writing it reduces the risk of mistaking an alias for a separate column when a comma is missing.
Cases that change the result
An output alias is defined in the list SELECT. So do not assume it is available in clauses evaluated earlier. Standard SQL does not let you use it in WHERE; to filter on the calculation, repeat the expression or move the query into a subquery or CTE.
-- Not portable; invalid in many database engines
SELECT price * stock AS inventory_value
FROM products
WHERE inventory_value > 500;SELECT price * stock AS inventory_value
FROM products
WHERE price * stock > 500;ORDER BY usually does accept the output alias. The rules for GROUP BY and HAVING differ between dialects, so a portable query should not depend on extensions without checking them.
Common mix-ups with aliases in SELECT
Using spaces without understanding the quotes
An alias such as "Valor de inventory" needs identifier delimiters, and you have to repeat them whenever you refer to it. For queries that feed code, spreadsheets or APIs, it is usually more robust to use inventory_value.
And do not mistake the alias for a permanent change: the next query will still find the original column name, not a new column called product.