Joins In Sql Server 2005
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.
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.