Domesticated Brain

 

SQL JOINs

SQL joins are used to join database tables based on a common field

There are different types of joins in sql

  • INNER JOIN
  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • INNER JOIN

INNER JOIN

In following example we are working with the student table and course table

 SQL Tutorials INNER JOIN Student Table

.

SQL Tutorials INNER JOIN Course Table

What is common in both tables are course_id and couseID

As an example now we need to create a new table to find out what is the course followed by each student

But our required information is in two tables

Using inner join we can create the required table

Here is the sql query with the inner join

 

SELECT student.FirstName, student.LastName, course.CourseTitle, course.Duration
FROM student INNER JOIN
course
ON course.courseID = student.course_id
ORDER BY student.FirstName

 

SQL Tutorials INNER JOIN Result

 

OUTER JOIN

LEFT OUTER JOIN

Following table called course has null values for some student name

That is Students who are not following any courses

If we are going to use inner join for joining those two tables we will get a result excluding students with null values on their course id

SQL Tutorials LEFT OUTER JOIN Student Table

To eliminate this we can use left outer join

SELECT student.FirstName,course.CourseTitle
FROM student LEFT OUTER JOIN
course
ON course.courseID = student.course_id
ORDER BY student.FirstName

 SQL Tutorials LEFT OUTER JOIN Result

 

 Right outer join is the opposite of the left outer join

If you have found  what you expected please comment and like on our facebook page