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.
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.
Be the first to rate this post
Tags: sql, sql joins, sql join example, table aliases sql
10/11/2008 3:32:07 AM