SELECT with Aliases
In this page:
What is an Alias?
An alias is a temporary, query-scoped name you assign to a column or table using AS, which makes result sets and complex queries far easier to read and reference. Aliases exist only for the duration of that single query and have no effect on the actual table or column names in the schema.
Example: What is an Alias?
SELECT name AS full_name FROM users;
Aliases with Calculations
Aliases become especially valuable on calculated columns — without one, a computed expression shows up in results under an unreadable auto-generated name instead of something meaningful like total_price.
Example: Aliases with Calculations
SELECT price * quantity AS total FROM orders;
Multi-Word Aliases
If an alias contains a space or other special character, it must be wrapped in quotes so MySQL parses it as one single name rather than as multiple separate tokens. Forgetting the quotes around a multi-word alias is a common syntax error that produces a confusing 'unknown column' message instead.
Example: Multi-Word Aliases
SELECT price * quantity AS 'Order Total' FROM orders;
Table Aliases
Giving a table a short alias lets you reference it with far less typing throughout a long query, which matters a lot once you're joining several tables together in one statement. This becomes essential once a query joins the same table to itself, since MySQL needs a way to distinguish the two references.
Example: Table Aliases
SELECT u.name FROM users AS u;
Omitting the AS Keyword
The AS keyword is technically optional in MySQL — writing just a space between the expression and its alias works identically, though many style guides recommend keeping AS for clarity.
Example: Omitting the AS Keyword
SELECT name full_name FROM users;
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: