Monday, November 2, 2015

Different Types of SQL Joins

Sql joins are used to fetch/retrieve data from two or more data tables, based on a join condition. A join condition is a relationship among some columns in the data tables that take part in Sql join. Basically data tables are related to each other with keys. We use these keys relationship in sql joins.

Types of Joins

In Sql Server we have only three types of joins. Using these joins we fetch the data from multiple tables based on condition.
  1. Inner Join

    Inner join returns only those records/rows that match/exists in both the tables. Syntax for Inner Join is as
    1. Select * from table_1 as t1
    2. inner join table_2 as t2
    3. on t1.IDcol=t2.IDcol
  2. Outer Join

    We have three types of Outer Join.
    1. Left Outer Join

      Left outer join returns all records/rows from left table and from right table returns only matched records. If there are no columns matching in the right table, it returns NULL values. Syntax for Left outer Join is as :
      1. Select * from table_1 as t1
      2. left outer join table_2 as t2
      3. on t1.IDcol=t2.IDcol
    2. Right Outer Join

      Right outer join returns all records/rows from right table and from left table returns only matched records. If there are no columns matching in the left table, it returns NULL values. Syntax for right outer Join is as :
      1. Select * from table_1 as t1
      2. right outer join table_2 as t2
      3. on t1.IDcol=t2.IDcol
    3. Full Outer Join

      Full outer join combines left outer join and right outer join. This join returns all records/rows from both the tables.If there are no columns matching in the both tables, it returns NULL values. Syntax for full outer Join is as :
      1. Select * from table_1 as t1
      2. full outer join table_2 as t2
      3. on t1.IDcol=t2.IDcol
  3. Cross Join

    Cross join is a cartesian join means cartesian product of both the tables. This join does not need any condition to join two tables. This join returns records/rows that are multiplication of record number from both the tables means each row on left table will related to each row of right table. Syntax for right outer Join is as :
    1. Select * from table_1
    2. cross join table_2
  4. Self Join

    Self join is used to join a database table to itself, particularly when the table has a Foreign key that references its own Primary Key. Basically we have only three types of joins : Inner join, Outer join and Cross join. We use any of these three JOINS to join a table to itself. Hence Self join is not a type of Sql join.

Join Examples

Suppose we following three tables and data in these three tables is shown in figure. You can download the SQL script used in this article by using link.

Inner Join

  1. SELECT t1.OrderID, t0.ProductID, t0.Name, t0.UnitPrice, t1.Quantity, t1.Price
  2. FROM tblProduct AS t0
  3. INNER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. ORDER BY t1.OrderID

Inner Join among more than two tables

  1. SELECT t1.OrderID, t0.ProductID, t0.Name, t0.UnitPrice, t1.Quantity, t1.Price, t2.Name AS Customer
  2. FROM tblProduct AS t0
  3. INNER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. INNER JOIN tblCustomer AS t2 ON t1.CustomerID = t2.CustID
  5. ORDER BY t1.OrderID

Inner Join on multiple conditions

  1. SELECT t1.OrderID, t0.ProductID, t0.Name, t0.UnitPrice, t1.Quantity, t1.Price, t2.Name AS Customer
  2. FROM tblProduct AS t0
  3. INNER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. INNER JOIN tblCustomer AS t2 ON t1.CustomerID = t2.CustID AND t1.ContactNo = t2.ContactNo
  5. ORDER BY t1.OrderID

Left Outer Join

  1. SELECT t1.OrderID AS OrderID , t0.ProductID , t0.Name , t0.UnitPrice , t1.Quantity AS Quantity , t1.Price AS Price
  2. FROM tblProduct AS t0
  3. LEFT OUTER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. ORDER BY t0.ProductID

Right Outer Join

  1. SELECT t1.OrderID AS OrderID , t0.ProductID , t0.Name , t0.UnitPrice , t1.Quantity AS Quantity , t1.Price AS Price
  2. FROM tblProduct AS t0
  3. RIGHT OUTER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. ORDER BY t0.ProductID

Full Outer Join

  1. SELECT t1.OrderID AS OrderID , t0.ProductID , t0.Name , t0.UnitPrice , t1.Quantity AS Quantity , t1.Price AS Price
  2. FROM tblProduct AS t0
  3. FULL OUTER JOIN tblOrder AS t1 ON t0.ProductID = t1.ProductID
  4. ORDER BY t0.ProductID

Cross Join

  1. SELECT t1.OrderID, t0.ProductID, t0.Name, t0.UnitPrice, t1.Quantity, t1.Price
  2. FROM tblProduct AS t0, tblOrder AS t1
  3. ORDER BY t0.ProductID

Self Join

To understand Self Join, suppose we following two tables and data in these two tables is shown in figure.
  1. CREATE TABLE emp
  2. (
  3. id int NOT NULL primary key,
  4. name varchar(100) NULL,
  5. designation varchar(50) NULL,
  6. supid int foreign key references emp(id) ) -- In this table we have a Foreign key supid that references its own Primary Key id. We use it for Self Join
  7. INSERT INTO emp(id,name,designation) VALUES(1,'mohan','Manger')
  8. INSERT INTO emp(id,name,designation,supid) VALUES(2,'raj kumar','SE',1)
  9. INSERT INTO emp(id,name,designation) VALUES(3,'bipul kumar','Manager')
  10. INSERT INTO emp(id,name,designation,supid) VALUES(4,'mrinal kumar','SE',2)
  11. INSERT INTO emp(id,name,designation,supid) VALUES(5,'jitendra kumar','SE',2)
  1. CREATE TABLE empinfo
  2. (
  3. id int primary key,
  4. address varchar(50) NULL
  5. )
  6. INSERT INTO empinfo(id,address) VALUES(1,'Delhi')
  7. INSERT INTO empinfo(id,address) VALUES(2,'Noida')
  8. INSERT INTO empinfo(id,address) VALUES(4,'Gurgaon')
  9. INSERT INTO empinfo(id,address) VALUES(6,'Delhi')
  10. INSERT INTO empinfo(id,address) VALUES(7,'Noida')
  1. select e.id,e.name,e.supid as managerid, ei.name as managername from emp e left join emp ei on e.supid=ei.id;
  2. -- outer keyword is optional

 Note: This article is taken from : http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html


No comments:

Post a Comment

Please Comment Here!

How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...