Joins In Sql Server 2005

Updated on 09 Jan 2012,
Published on 26 Feb 2008

In RDBMS (Relational Database Management Systems) we have to normalize the database structure according to different form of normalization. Generally we create different tables having login information and address and other related information in other table. To display the data from both tables we can use SQL Joins or sub queries

Using joins you can retrieve data from two or more tables. To join two or more tables you have to set some conditions in the join queries such as:

  • Relating the two keys of the joining tables using foreign key and its relational key in other table.
  • Set the condition logical operators e.g. =, >, < to compare the values and drilling down the rows to display only required data. 

SQL Join Query Syntax

SELECT [columnNames | *] FROM [tableName 1st] joinType [tableName 2nd] [ON join condition]

To retrieve the specific columns you have to use tableName.columnName in the SQL query if both the tables have similar columnNames. But it is recommended to use tableName.columnName for escaping from errors.

Example
USE NORTHWIND

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

Above SQL Join query shows the syntax usage of simple join that retrieves the category names, product names related to the categories and unit price of products.

Continue to next tutorial: Joins using table aliases in SQL Server 2005 to learn how to apply joins using alias name of tables.

0 Responses to "Joins In Sql Server 2005"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email