Running with an alias

Aliases are used to temporarily rename a table or a field heading in SQL. We use the keyword AS when creating an alias. Aliases are often used to make a field name more readable and a table name shorter.

Field name alias

Field names often use the _ underscore to separate logical words. This often does not look appealing to the end user. Rather than seeing last_name in the heading of the query results we can have it display “Last Name”.

Below is an example of using the alias keyword AS with the first_name and last_name fields.

SELECT first_name AS "First Name", last_name AS "Last Name"
FROM student;

This would display “First Name” and “Last Name” in the heading of the results rather than seeing “first_name” and “last_name”.

Table name alias

The alias also works on table names. Often it is used when we select fields from multiple tables. Typically programmers will shorten the table table to the first letter(s) to shorten the query.

Below is an example of using the alias keyword AS with two table names. The shortened tables names are used to make the query easier to follow.

SELECT s.last_name, s.first_name, p.name, p.year
FROM student AS s, program AS p
WHERE s.program_id = p.program_id;

Lesson Activities

  1. Create a list of the students and their program name using aliases for the field names.
    Click to see code
    SELECT student.first_name AS "First Name", student.last_name AS "Last Name", program.program_name AS "Program Name"
    FROM student, program
    WHERE student.program_id = program.program_id;
  2. Create a list of students and their faculty adviser names using aliases for the field names.
    Click to see code
    SELECT student.first_name AS "Student First", student.last_name AS "Student Last", faculty.first_name AS "Adviser First", faculty.last_name AS "Adviser Last"
    FROM student, faculty
    WHERE student.faculty_id = faculty.faculty_id;

Questions