Sunday, September 14, 2014

SQL JOINS

SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. SQL Join is used for combining column from two or more tables by using values common to both tables.
Join Keyword is used in SQL queries for joining two or more tables.

Types of Join
  • Inner
  • Outer
  • Left
  • Right
 

Student

Reg_id name address sub_id
2008/SP/67 Thenuzan Kurumankadu CSC
2008/SP/20 Nilashan Poonthodam CSC
2008/SP/41 Tharopan Chunaakam PHY
2008/SP/34 Pancha Vadukkoodai PHY

Subject
sub_id sub_name
CSC Computer Science
PHY Physics
CHE Chemistry

CROSS JOIN /Cartesian Product
This type of JOIN returns the cartesian product of rows of from the tables in Join. It will return a table which consists of records which combines each row from the first table with each row of the second table.
SELECT column-name-list
from tableA
CROSS JOIN
tableB;

SELECT *
from Student
CROSS JOIN
Subject

Reg_id name address sub_id sub_id sub_name
2008/SP/67 Thenuzan Kurumankadu CSC CSC Computer Science
2008/SP/67 Thenuzan Kurumankadu CSC PHY Physics
2008/SP/67 Thenuzan Kurumankadu CSC CHE Chemistry
2008/SP/20 Nilashan Poonthodam CSC CSC Computer Science
2008/SP/20 Nilashan Poonthodam CSC PHY Physics
2008/SP/20 Nilashan Poonthodam CSC CHE Chemistry


INNER JOIN
The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables. 
SELECT * 
FROM tableA INNER JOIN tableB 
ON tableA.column_name = tableB.column_name; 


1 comment:

  1. I think, There will be six rows additionally in the result table of cross join. Three for tharopan and other three for pancha. Because 4*3=12

    ReplyDelete