Design and best practice · Designing databases

Best practice for SQL aliases

Choose aliases that explain each table's role and what each calculation means. Keep the same convention throughout the query.

Before continuing: This entry is part of the aliases guide. If you are new to the topic, start with Alias.

Example

Hard-to-maintain version
SELECT a.name AS n,
       SUM(b.quantity) AS t,
       SUM(b.quantity * b.unit_price) AS x
FROM products a
JOIN order_items b ON b.product_id = a.id
GROUP BY a.id, a.name;
Version with a visible intent
SELECT cli.name AS customer,
       COUNT(ped.id) AS total_orders,
       ROUND(SUM(ped.total), 2) AS cumulative_amount
FROM customers AS cli
INNER JOIN orders AS ped
  ON ped.customer_id = cli.id
GROUP BY cli.id, cli.name
ORDER BY cumulative_amount DESC;

cli and ped point back to the source tables. The names total_orders e cumulative_amount document the metrics that the consuming person or application receives.

Differences and trade-offs

  1. Link the alias to the entity. Usa cli, ped or product if a single letter is ambiguous.
  2. Keep one convention. Do not switch from c to cl for the same table within the project without a reason.
  3. Name the meaning, not the function. revenue usually says more than total_sum.
  4. Avoid reserved words and spaces. total_amount is more portable than an identifier that requires quotes.
  5. Do not rename columns that are already clear. p.id AS id adds noise; p.id AS order_id can make the source clear.

Avoid this confusion

Abbreviating until the source disappears

Aliases such as a, b, c1 and c2 force you to keep going back to FROM constantly. In a SELF JOIN, functional names such as employee and owner are more useful than two arbitrary letters.

Another mistake is putting presentation details in the alias, such as spaces, symbols or units that the consumer should format. Prefer simple identifiers and leave presentation to the interface.

Names that survive a larger query

A convention works if it stays understandable as you add relationships. c and p work in a short query on customers and orders; in a report with payments, products and suppliers, three aliases starting with p are easy to mix up. Use role-based names when the same entity appears several times: buyer and seller say more than two numbers tacked onto customer.

In calculated columns, include whatever is needed to interpret the value: unit, period or population. amount_gbp avoids confusing pounds with cents and paid_orders separates a subset from the overall number of orders. The example gives explicit names to both keys because whoever consumes the result must be able to tell which identifier they are getting without checking the column order.

Aliases do not change the physical name of the tables and need no migration, but they can change the contract of an export or an application. Renaming an output column can break code that looks it up by name. If a label has to contain spaces, quoting identifiers depends on the engine; for a query that has to move between engines, simple names with underscores reduce that dependency. Avoid abbreviations that only make sense in the memory of whoever wrote the query.

Self-contained case to check in SQLite

Two keys that are both called id in their sources are told apart without depending on their position in the output.

Data and query · SQLite
WITH customers(id, name) AS (VALUES (7, 'Amy')),
     orders(id, customer_id, amount_cents) AS (VALUES (101, 7, 1250))
SELECT buyer.id AS customer_id, order_record.id AS order_id,
       order_record.amount_cents / 100.0 AS amount_gbp
FROM customers buyer JOIN orders order_record
  ON order_record.customer_id = buyer.id;
Verified result of the self-contained case
customer_idorder_idamount_gbp
710112.5

customer_id and order_id have different meanings. amount_gbp declares the unit after converting cents.

Check your interpretation: What would stop being clear if the three columns were called id, id and total?

Show answer

You would not know which id belongs to the customer and which to the order, and total would not say whether the amount is in cents or pounds. Also, two columns with the same name can overwrite each other when an application turns the row into an object.

Technical questions: PostgreSQL: SELECT, columns and aliases (opens in a new tab).