Joins using table aliases in SQL Server 2005

by top54u.com 26 Feb, 2008

In the previous article you learnt the use of simple Join in SQL queries.

 

There you learnt the use of tableName.columnName to retrieve some particular fields from the sql server database tables. If you want to join more than two tables then the SQL query becomes complicated by using tableNames.columnName approach to build a SQL Join query.

 

In the last article Joins in Sql Server 2005 you can see that using table names to retrieve the specified columns make the SQL Join statements very lengthy and difficult to understand. So in this case you can use table name aliases to instead of full table names.

 

Example:

SELECT C.CATEGORYNAME, P.PRODUCTNAME, P.UNITPRICE FROM
PRODUCTS AS P JOIN CATEGORIES AS C ON C.CATEGORYID = P.CATEGORYID
ORDER BY C.CATEGORYNAME

 

Above SQL Join Query example shows the use of table aliases instead of full table name. This method increases the readability as well as reduces the length of SQL Query.

 

You can also use conditions to filter the records retrieved through SQL Join queries.

 

Example:

SELECT C.CATEGORYNAME, P.PRODUCTNAME, P.UNITPRICE FROM
PRODUCTS AS P JOIN CATEGORIES AS C ON C.CATEGORYID = P.CATEGORYID
WHERE P.PRODUCTNAME LIKE 'C%'
ORDER BY C.CATEGORYNAME

 

Above SQL Join Query example will return the product name starting with letter ‘C’ along with its unit price and category name.

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:32:07 AM




related videos.....
recent posts.....
top54u ezines.....