Wednesday, January 22, 2014

JOIN

JOIN : Sql Join are used to combine rows from two or more tables.
Different SQL JOINs
  1. Inner Join : Return rows when there is at least one match in both tables.
  2. Left Outer Join : Return all rows from the left table, even if there are no matches in the right table.
  3. Right Outer Join : Return all rows from the right table, even if there are on matches in the left table.
  4. Full Outer Join : Return rows when there is a match in the one of the table.

1st Table –
SELECT * FROM Student
clip_image002
2nd Table –
SELECT * FROM Course
clip_image004
 
1) Inner Join : Return rows when there is at least one match in both tables.
INPUT—

SELECT  Student.Id, Student.Name, Student.City, Student.Mobile,
                Student.Email, Course.Code, Course.Name
FROM Student
INNER JOIN Course ON Student.CourseId=Course.Id;

OUTPUT –
clip_image006

2) Left Outer Join : Return all rows from the left table, even if there are no matches in the right table.
INPUT—

SELECT Student.Id, Student.Name, Student.City, Student.Mobile,
               Student.Email, Course.Code, Course.Name
FROM Student
LEFT OUTER JOIN Course ON Student.CourseId=Course.Id;

OUTPUT—
clip_image008

3) Right Outer Join : Return all rows from the right table, even if there are on matches in the left table.

INPUT –
SELECT Student.Id, Student.Name, Student.City, Student.Mobile,
               Student.Email, Course.Code, Course.Name
FROM Student
RIGHT OUTER JOIN Course ON Student.CourseId=Course.Id;

OUTPUT—
clip_image010

4) Full Outer Join : Return rows when there is a match in the one of the table.
INPUT—

SELECT Student.Id, Student.Name, Student.City, Student.Mobile,
               Student.Email, Course.Code, Course.Name
FROM Student
FULL OUTER JOIN Course ON Student.CourseId=Course.Id;

OUTPUT—
clip_image012



No comments:

Post a Comment