Result Formatting and Aliases
SQL lets you rename columns or tables in your result using aliases. This makes your queries and output easier to read and understand, especially in reports or dashboards.
Column Aliases with AS
You can rename a column in your result using the AS
keyword:
Use AS to rename
SELECT AVG(order_total) AS average_order
FROM client_orders;
This shows the result as average_order
instead of just avg
.
You can also skip AS
and write:
SELECT AVG(order_total) average_order
FROM client_orders;
Both work — but AS
makes it clearer.
Multiple Column Aliases
Label multiple results
SELECT
region AS client_region,
COUNT(*) AS total_clients,
SUM(order_total) AS region_revenue
FROM clients
JOIN client_orders ON clients.id = client_orders.client_id
GROUP BY region;
This makes each part of the output more readable and well-labeled.
Table Aliases
You can also rename tables using descriptive aliases, which is especially useful in complex queries involving multiple joins:
Table alias with descriptive names
SELECT client_data.name, order_data.order_total
FROM clients AS client_data
JOIN client_orders AS order_data ON client_data.id = order_data.client_id;
Aliases like client_data
and order_data
improve clarity, especially in longer or nested queries.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.