tags:

views:

295

answers:

9

In

SELECT a.NAME, a.NUMBER, a.STRING, a.RDB$DB_KEY FROM ADMIN a

what does a stand for?

Thanks.

+23  A: 

a is an alias for the table ADMIN

SQL Alias

jitter
+1 for both speed typing AND getting a link in there too :)
Binary Worrier
so why not simplySELECT NAME, NUMBER, STRING, RDB$DB_KEY FROM AMDIN?
Jack
@Jack that is a perfectly valid query
Matthew Jones
@Jack: If you're referencing two tables (e.g., ADMIN_1, ADMIN_2) and they have columns in common (e.g. NAME), then your SELECT has to differentiate between the tables so it knows which one to pull from. So using a.NAME is shorter than ADMIN_1.NAME.
Adam V
A: 

A is a shorthand notation (term: alias) for the table ADMIN

Matthew Jones
+1  A: 

a is what is called a table alias. In the part of the query that says:

FROM ADMIN a

By placing "a" after the table name, you have created an alias that can now be used in place of the table name. Without the alias, you would need to use the table's full name in order to fully-qualify the column name(s) that you are referring to in the query.

Without the table alias, your query would look like this:

SELECT ADMIN.NAME, ADMIN.NUMBER, ADMIN.STRING, ADMIN.RDB$DB_KEY FROM ADMIN

Although since you are only selecting columns from a single table, the table name (or alias) actually isn't needed at all in this example.

bcwood
+1  A: 

A is an alias for the table.

You can change a to any valid identifier, it isn't dependant on the underlying schema. Usually it's used to differentiate fields from different tables, saves you typeing the full table name each time (makes the SQL easier to read with a short alias).

It isn't actually required in the sample you gave,

SELECT NAME, NUMBER, STRING, RDB$DB_KEY FROM AMDIN

should work just as well

Binary Worrier
+1  A: 

The query is using a like that just so that you don't have to write ADMIN.NAME, ADMIN.NUMBER, etc etc. If you have fifteen fields on your table and your table has a name like VPCPDEEE it gets very tiresome to type the same table name over and over.

Terry Donaghe
Also tiresome to read - and we read code much more often than we write it. Aliases enhance readability.
Carl Manaster
A: 

a = ADMIN

Equivalent:

SELECT ADMIN.NAME, ADMIN.NUMBER, ADMIN.STRING, ADMIN.RDB$DB_KEY FROM ADMIN
NinethSense
+2  A: 

An alias for the table ADMIN. It's not necessary here, because you only have one table in your query.

When you have more than one table, and some of the columns are the same, then you need to distinguish between them. One way is to write the table name in front of the column name. E.g.,

Select ADMIN.Name, person.name from ADMIN, person where person.id = admin.id

To make this shorter, add aliases for the table names.

select a.Name, p.Name from ADMIN a, person p where person.id = admin.id
UncleO
A: 

The a is used as an alias for the ADMIN table.

When it is correct to use an alias, and of what form that alias should take can raise some strong opinions from writers of sql.

Heres a couple of stackoverflow questions on the subject.

http://stackoverflow.com/questions/198196/when-to-use-sql-table-alias

http://stackoverflow.com/questions/11043/sql-table-aliases-good-or-bad

Paul Rowland
A: 

In the ISO/ANSI SQL-92 Standard this is called a 'table correlation name', though the colloquial 'alias' seems to be more commonly used.

onedaywhen