Creating Calculated Fields and Aliases
Data stored in a database table is not always in the exact format required by your reporting applications. You may need to retrieve a value that is mathematically derived from multiple columns, such as multiplying a price by a quantity to find a total. Rather than retrieving the raw data and performing the calculations in the client application, you can instruct the database to perform these operations on the fly using calculated fields.
A calculated field does not physically exist in the database table. It is created dynamically by the database engine within the SELECT statement during query execution.
Performing Mathematical Calculations
SQL supports standard mathematical operators to create calculated fields. You can use these operators to add, subtract, multiply, or divide numerical columns directly within your query.
- Addition uses the
+operator. - Subtraction uses the
-operator. - Multiplication uses the
*operator. - Division uses the
/operator.
To calculate the total value of an inventory item, you multiply the unit price by the quantity on hand.
SELECT product_name, price, quantity, price * quantity FROM products;The database engine will compute the equation for every row returned by the query and display the result in a new, temporary column.
Assigning Column Aliases
When you create a calculated field, the database engine does not have a predefined column name to assign to the result. By default, it may leave the column name blank or use the literal calculation formula as the header, which is difficult for client applications to reference. To solve this problem, you assign a temporary name to the calculated column using an alias. The AS keyword is used immediately after the calculation to specify this alias .SELECT product_name, price * quantity AS total_value
FROM products;In this example, the client application can reference the newly computed column using the clean identifier total_value.
Concatenating Text Fields
Mathematical operations apply to numeric data, but you can also create calculated fields for text data. Concatenation is the process of joining two or more text columns into a single string. The exact syntax for concatenation varies across database management systems, utilizing functions or specific operators to bind the strings.SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;This operation merges the first name, a literal space character, and the last name into a single column, which is then aliased for clean reporting.
Aliases for Standard Columns and Tables
Aliases are not restricted to calculated fields. A common use for aliases is to rename standard table columns in the retrieved results to match specific reporting or client needs. This makes your output much more readable, especially if the underlying database schema uses obscure naming conventions.SELECT prd_nm AS product_name, prd_prc AS price
FROM inventory;`
Table names can also receive aliases within the FROM clause. Assigning a short alias to a table name significantly reduces the amount of typing required when constructing complex queries. This concept becomes absolutely essential when querying multiple tables to avoid ambiguity errors.
Aliases only exist for the duration of the query. They do not alter the physical table structure or permanently change the original column names in the database system.
Order of Evaluation
When combining multiple mathematical operations in a single calculated field, standard mathematical order of operations applies. Multiplication and division are evaluated before addition and subtraction. To enforce a specific evaluation order, enclose the operations within parentheses.SELECT product_name, (price - discount) * quantity AS net_value FROM products;The database will first subtract the discount from the price, and then multiply that result by the quantity.
Apply these calculation and aliasing techniques to format and compute your data efficiently within the database.