Sub topic 3
Choosing Columns and Naming Them with Aliases
Assume you already understand relational tables, rows, columns, and basic Oracle data types. The examples use the employees table from Oracle's HR sample schema. A SELECT statement chooses data to return. The FROM clause identifies the table that supplies that data.
Selecting specific columns
The simplest useful query names the columns you want after SELECT:
SELECT employee_id, first_name, last_name, salary
FROM employees;This query returns four columns for every row in employees. The order of the column names controls the order of the result columns. It does not change the order of rows.
You can select every column with an asterisk:
SELECT *
FROM employees;This is convenient for exploration, but explicit column names are usually better in application code. They make the result's shape clear and prevent unexpected new columns from appearing if the table changes.
A query can select the same column more than once, although this is rarely useful:
SELECT employee_id, employee_id
FROM employees;Oracle returns two result columns. Because both have the same default heading, aliases are useful when a result contains repeated or calculated values.
Naming result columns with aliases
A column alias is a temporary name for a column or expression in the query result. It does not rename the column in the table.
Use AS for a clear column alias:
SELECT employee_id AS id,
first_name AS given_name,
salary AS monthly_salary
FROM employees;The result headings are ID, GIVEN_NAME, and MONTHLY_SALARY in many Oracle client tools. Oracle normally stores unquoted identifiers in uppercase, so the alias's displayed case can depend on the client.
Oracle also permits an alias without AS:
SELECT first_name given_name,
salary monthly_salary
FROM employees;Although this is valid, AS makes the boundary between an expression and its alias easier to read.
Aliases are especially useful for expressions:
SELECT first_name || ' ' || last_name AS full_name,
salary * 12 AS annual_salary
FROM employees;The || operator joins character values. The expression salary * 12 calculates an annual amount when salary stores monthly pay. These calculated values exist only in the result.
Alias rules and quoted names
Use simple, unquoted aliases whenever possible. An unquoted alias can contain letters, numbers, and underscores, but it cannot contain spaces unless you quote it.
SELECT first_name AS "Employee first name",
salary AS "Monthly salary"
FROM employees;A quoted alias preserves its exact spelling and spaces. It must be referenced with matching double quotes if another part of a statement needs that alias. Quoted names can make reports easier to read, but they are less convenient for later SQL and application code.
An alias cannot normally be used in the same SELECT list where it is created:
SELECT salary * 12 AS annual_salary,
annual_salary * 1.1 AS adjusted_salary
FROM employees;This raises an invalid identifier error because Oracle does not evaluate one select-list alias for use by another expression at that level. Repeat the expression or use an inline view:
SELECT annual_salary,
annual_salary * 1.1 AS adjusted_salary
FROM (
SELECT salary * 12 AS annual_salary
FROM employees
);How a query supplies its result
The table provides source columns. Expressions transform values. Aliases label the output.
Practice
Exercise
Write a query that returns first_name, last_name, and salary * 12 from employees, using the aliases given_name, family_name, and annual_salary.
Show answer
SELECT firstname AS givenname, lastname AS familyname, salary * 12 AS annual_salary FROM employees;