In the previous article,
We learned about SQL Aliases. In this article, we will learn about SQL Joins and practically see the usage of Aliases in Joins.
Our data may be stored in multiple tables. In many situations, we need the data from multiple tables together at a single place. In order to retrieve data from multiple related tables and display it as a single table, we use Joins. We can apply joins on the tables having a common attribute.
There are different types of joins:
- Inner Join
- Outer Join
- Cross Join
- Equi Join
- Self Join
Syntax:
- SELECT column1, column2, .....
- FROM Table1 JOIN Table2
- ON Table1.CommonColumn Join_Operator Table2.CommonColumn
- [Where search_condition]
- where,
- Table1 and Table2 are the names of the table that are to be joined.
- Table1.CommonColumn and Table2.CommonColumn are the names of the common column on the basis of which the join is to be applied.
- Join_Operator is the comparison operator based on which the join will be applied.
INNER JOIN:
An inner join retrieves results from multiple tables having a common attribute (common column).
An inner join retrieves results from multiple tables having a common attribute (common column).
Example:
- Select o.orderid, o.orderdate, c.custid, c.contactname
- from Sales.Orders as o Inner Join Sales.Customers as c
- On o.custid=c.custid;
NOTE:
You will require TSQL2012 Sample database to execute these queries and see the difference between the various types of joins. You can download the database from here.
You will require TSQL2012 Sample database to execute these queries and see the difference between the various types of joins. You can download the database from here.
OUTER JOIN:
Outer Join displays all the records of one table and matching records from another table. There are three types of outer joins: Left outer join, Right outer join and Full outer join.
Left Outer Join:

Kumaresh RajalingamPosted Feb 14, 2016, 5:17 AM
Good one