A join in SQL is a statement that combines data from one or two tables based on common field or matching condition. In this article, we will see the different type of JOINs in SQL with examples.

Types of JOINs:

  • Inner JOIN
  • Outer JOIN
    • Left Outer JOIN
    • Right Outer JOIN
    • Full Outer JOIN
  • CROSS JOIN
  • Self-JOIN, Natural JOIN, Equi JOIN

Inner JOIN:

  • Returns records that matches with the both the tables

Example:

Student Table:

StudentIDStudentName
1Raj
2Nanda
3Arjun

Course Table:

CourseIDCourseNameStudentID
C1Maths1
C2English1
C3HindiNull

SELECT Student. StudentID, Student. StudentName, Course. CourseName FROM Student INNER JOIN ON Course WHERE Student. StudentID = Course. StudentID

Result:

StudentIDStudentNameCourseName
1RajMaths
1RajEnglish

Outer JOIN:

Left Outer JOIN :

  • Returns records that matches with the right table and all records from the left table even if there is not match in the right table

Example:

SELECT Student. StudentID, Student. StudentName, Course. CourseName FROM Student LEFT OUTER JOIN ON Course WHERE Student. StudentID = Course. StudentID

StudentIDStudentNameCourseName
1RajMaths
1RajEnglish
2NandaNull
3ArjunNull

Right Outer JOIN:

  • Returns records that matches with the left table and all records from right table even though there is no match in the left table

Example:

SELECT Student. StudentID, Student. StudentName, Course. CourseName FROM Student RIGHT OUTER JOIN ON Course WHERE Student. StudentID = Course. StudentID

StudentIDStudentNameCourseName
1RajMaths
1RajEnglish
NullNullHindi

Full Outer JOIN:

  • Returns all records that matches with the left table or from right table

Example:

SELECT Student. StudentID, Student. StudentName, Course. CourseName FROM Student FULL OUTER JOIN ON Course WHERE Student. StudentID = Course. StudentID

StudentIDStudentNameCourseName
1RajMaths
1RajEnglish
NullNullHindi
2NandaNull
3ArjunNull

Cross JOIN:

  • Returns cartesian product of all rows from the tables

Example:

SELECT Student. StudentID, Student. StudentName, Course. CourseID, Course. CourseName FROM Student CROSS JOIN Course;

StudentIDStudentNameCourseIDCourseName
1RajC1Maths
2NandaC1Maths
3ArjunC1Maths
1RajC2English
2NandaC2English
3ArjunC2English
1RajC3Hindi
2NandaC3Hindi
3ArjunC3Hindi

Natural JOIN:

  • Return the records that joins the tables based on the same columns and same datatypes
  • It returns the unique columns

Example:

SELECT Student. StudentName, Course. CourseName FROM Student NATURAL JOIN  Course

StudentNameCourseName
RajMaths
RajEnglish

Self JOIN:

  • Joins the table with itself and returns the matching records

Example:

StudentIDStudentNameHobbies
1RajSwimming
2NandaSwimming
3ArjunFootball
4VidyaSwimming

SELECT  S1. StudentName as StudentName1, S2.StudentName as StudentName2, S1.Hobbies FROM Student S1, Student S2  WHERE  S1.StudentName <> S2.StudentName AND S1.Hobbies = S2.Hobbies

StudentName1StudentName2Hobbies
RajNandaSwimming
NandaRajSwimming
VidyaRajSwimming
RajVidyaSwimming
NandaVidyaSwimming
VidyaNandaSwimming

Equi JOIN:

  • Joins the table based on equality condition of the columns

Example:

SELECT Student. StudentID, Student. StudentName, Course. CourseName FROM Student, Course WHERE Student. StudentID = Course. StudentID

StudentIDStudentNameCourseIDCourseName
1RajC1Maths
1RajC2English