Skip to main content
Practice

Result Formatting and Aliases

SQL lets you rename columns or tables in your results using aliases. This makes your queries and output easier to read and understand, especially in reports or dashboards.


Column Aliases with AS

Use the AS keyword to rename columns in your query results:

Use AS to rename
SELECT AVG(order_total) AS average_order
FROM client_orders;

This displays the column as average_order instead of the default system label (like avg).

You can also skip AS and write:

SELECT AVG(order_total) average_order
FROM client_orders;

Both versions work, but using AS improves readability.


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;

Clear aliases make each part of the result easier to understand and visually organized.


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;

Using descriptive table aliases like client_data and order_data makes long or nested queries easier to read and maintain.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.