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, surnameFROM PupilWe 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:
| fn | sn |
| Zainab | Singh |
| David | Boland |
| Amy | Jones |
| James | McNamee |
| Pawel | McTavish |
| Antonia | Gallgher |
| Amy | McNamee |
| Aysha | Matazinadze |
| Beatrice | Panner |
| Thomas | Glacney |
| fn | Zainab |
|---|---|
| sn | Singh |
| fn | David |
|---|---|
| sn | Boland |
| fn | Amy |
|---|---|
| sn | Jones |
| fn | James |
|---|---|
| sn | McNamee |
| fn | Pawel |
|---|---|
| sn | McTavish |
| fn | Antonia |
|---|---|
| sn | Gallgher |
| fn | Amy |
|---|---|
| sn | McNamee |
| fn | Aysha |
|---|---|
| sn | Matazinadze |
| fn | Beatrice |
|---|---|
| sn | Panner |
| fn | Thomas |
|---|---|
| sn | Glacney |
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 - filmBudgetFROM Films| filmName | filmBudget | filmBoxOffice | filmBoxOffice-filmBudget |
| Star Battles 101 | 10,000 | 200,000 | 190,000 |
| Triassic Park | 250,000 | 2,000,000 | 1,750,000 |
| The Tiger Prawn | 500,000 | 400,000 | -100,000 |
| filmName | Star Battles 101 |
|---|---|
| filmBudget | 10,000 |
| filmBoxOffice | 200,000 |
| filmBoxOffice-filmBudget | 190,000 |
| filmName | Triassic Park |
|---|---|
| filmBudget | 250,000 |
| filmBoxOffice | 2,000,000 |
| filmBoxOffice-filmBudget | 1,750,000 |
| filmName | The Tiger Prawn |
|---|---|
| filmBudget | 500,000 |
| filmBoxOffice | 400,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 FilmsThe query will return our four fields again, but with a new alias, like so:
| filmName | filmBudget | filmBoxOffice | filmProfit |
| Star Battles 101 | 10,000 | 200,000 | 190,000 |
| Triassic Park | 250,000 | 2,000,000 | 1,750,000 |
| The Tiger Prawn | 500,000 | 400,000 | -100,000 |
| filmName | Star Battles 101 |
|---|---|
| filmBudget | 10,000 |
| filmBoxOffice | 200,000 |
| filmProfit | 190,000 |
| filmName | Triassic Park |
|---|---|
| filmBudget | 250,000 |
| filmBoxOffice | 2,000,000 |
| filmProfit | 1,750,000 |
| filmName | The Tiger Prawn |
|---|---|
| filmBudget | 500,000 |
| filmBoxOffice | 400,000 |
| filmProfit | -100,000 |