ImplementationAliases

Structured Query Language is used to allow database developers to generate queries and interrogate the data held in a database. SELECT, FROM, WHERE, AND and OR are key areas of SQL.

Part ofComputing ScienceDatabase design and development

Aliases

An alias can be used if we want to temporarily change the name of something in our query. This can be useful if:

  • a field name is too long to display in the query output
  • a field name would not make sense when given to a user

We use the key word As in a query to give something another name.

Aliases only exist at run-time, meaning that whenever we use them, they will disappear after we have run our SQL query.

For example, the query below will shorten the pupil table to 'p', shorten firstname to 'fn' and shorten surname to 'sn'

First we will write a query that shows all of the firstnames and surnames of all pupils

SELECT firstname, surname
FROM Pupil

We can then re-write this query using aliases, like so:

SELECT firstname As [‘fn’], surname As [‘sn’]
FROM Pupil As [‘p’]

This query would return the same results but our table would be:

fnsn
ZainabSingh
DavidBoland
AmyJones
JamesMcNamee
PawelMcTavish
AntoniaGallgher
AmyMcNamee
AyshaMatazinadze
BeatricePanner
ThomasGlacney
fnZainab
snSingh
fnDavid
snBoland
fnAmy
snJones
fnJames
snMcNamee
fnPawel
snMcTavish
fnAntonia
snGallgher
fnAmy
snMcNamee
fnAysha
snMatazinadze
fnBeatrice
snPanner
fnThomas
snGlacney

Remember, because these new headings are aliases, they are temporary. They only exist at run-time.

Computed values with Aliases

When we run a query on a field, we can also perform a calculation on fields if we wish. An example we could use is to calculate if a film made a profit or loss at the box office.

We can calculate with fields using several operators, such as +, -, * and /

In this example we will use minus (-)

Our query could look like:

SELECT filmName, filmBudget, filmBoxOffice, filmBoxOffice - filmBudget
FROM Films
filmNamefilmBudgetfilmBoxOfficefilmBoxOffice-filmBudget
Star Battles 10110,000200,000190,000
Triassic Park250,0002,000,0001,750,000
The Tiger Prawn500,000400,000-100,000
filmNameStar Battles 101
filmBudget10,000
filmBoxOffice200,000
filmBoxOffice-filmBudget190,000
filmNameTriassic Park
filmBudget250,000
filmBoxOffice2,000,000
filmBoxOffice-filmBudget1,750,000
filmNameThe Tiger Prawn
filmBudget500,000
filmBoxOffice400,000
filmBoxOffice-filmBudget-100,000

Now that we have a computed field, we can also give it an alias. This makes our table more presentable.

So if we re-write our query to look like:

SELECT filmName, filmBudget, filmBoxOffice,filmBoxOffice – filmBudget As [‘filmProfit’]
FROM Films

The query will return our four fields again, but with a new alias, like so:

filmNamefilmBudgetfilmBoxOfficefilmProfit
Star Battles 10110,000200,000190,000
Triassic Park250,0002,000,0001,750,000
The Tiger Prawn500,000400,000-100,000
filmNameStar Battles 101
filmBudget10,000
filmBoxOffice200,000
filmProfit190,000
filmNameTriassic Park
filmBudget250,000
filmBoxOffice2,000,000
filmProfit1,750,000
filmNameThe Tiger Prawn
filmBudget500,000
filmBoxOffice400,000
filmProfit-100,000