Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Aliases...

Status
Not open for further replies.

e1k4l3cs

MIS
Nov 11, 2002
56
0
0
SG
Can anyone elaborate "SQL Aliases" to me please ???

A million thanks in advance...

Kelvin
 
Are you asking about table aliases? Table aliases are a way to make your query more readable and easier to type. For instance:

No aliases:
SELECT employee.firstname, employee.lastName, company.officeNumber, payroll.grosspay
FROM employee
JOIN company ON employee.employeeID = company.employeeID
JOIN payroll ON payroll.employeeID = employee.employeeID
ORDER BY employee.lastname, employee.firstname

Made simple with aliases....
SELECT emp.firstname, emp.lastName, comp.officeNumber, pay.grosspay
FROM employee emp
JOIN company comp ON emp.employeeID = comp.employeeID
JOIN payroll pay ON pay.employeeID = emp.employeeID
ORDER BY emp.lastname, emp.firstname

This is a simple example, but with larger queries (especially self-joins) you can really see the difference.
-- Just trying to help... LOL [ponder]
 
Hmm... Can I say that alias is for extracting data to another column of different name or extracting data to another table with different name N column contents ???

Thanks...

Kelvin
 
No I dont think that's quite right

The alias allows you to make a vritual change to a column or table including using the abberviation shown above to make coding simpler. I think it is most important when you need to make multiple joins to the same table (e.g in the example below joining to a table of staff names twice to return the names of both the owenr and a salesperson (tables aliases shown in bold)

e.g.

select
sq.name,
s2.name
from
maintable m
innerjoin staff s1 on
m ownerid = s1.name

innerjoin staff s2 on
m.salespersonid = s2.name
 
I didn't mention column aliases...

SELECT fName + ' '+ lName AS 'Full Name'
FROM myTable -- Just trying to help... LOL [ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top