The Wayback Machine - https://web.archive.org/web/20230512161252/https://www.geeksforgeeks.org/inner-join-vs-outer-join/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Inner Join vs Outer Join

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

An SQL Join is used to combine data from two or more tables based on a common field between them. For example, consider the following two tables. 

Student Table

EnrollNoStudentNameAddress
1001geek1geeksquiz1
1002geek2geeksquiz2
1003geek3geeksquiz3
1004geek4geeksquiz4

StudentCourse Table

CourseIDEnrollNo
11001
21001
31001
11002
21003

Inner Join / Simple join:

In an INNER join, it allows retrieving data from two tables with the same ID.

Syntax:

SELECT COLUMN1, COLUMN2 FROM

 [TABLE 1] INNER JOIN [TABLE 2] 

ON Condition;

The following is a join query that shows the names of students enrolled in different courseIDs.

SELECT StudentCourse.CourseID,Student.StudentName
FROM Student
INNER JOIN StudentCourse 
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;

Note: INNER is optional above.  Simple JOIN is also considered as INNER JOIN The above query would produce following result.

CourseIDStudentName
1geek1
1geek2
2geek1
2geek3
3geek1

What is the difference between inner join and outer join? 

Outer Join is of three types:

  1. Left outer join 
  2. Right outer join 
  3. Full Join

1. Left outer join returns all rows of a table on the left side of the join. For the rows for which there is no matching row on the right side, the result contains NULL on the right side.

Syntax:

SELECT  T1.C1, T2.C2

 FROM TABLE T1 

LEFT JOIN TABLE T2 

ON T1.C1= T2.C1;

SELECT Student.StudentName,StudentCourse.CourseID
FROM Student
LEFT OUTER JOIN StudentCourse 
ON StudentCourse.EnrollNo = Student.EnrollNo
ORDER BY StudentCourse.CourseID;

Note: OUTER is optional above. Simple LEFT JOIN is also considered as LEFT OUTER JOIN

StudentNameCourseID
geek4NULL
geek21
geek11
geek12
geek32
geek13

2. Right Outer Join is similar to Left Outer Join (Right replaces Left everywhere).

Syntax:

SELECT T1.C1, T2.C2

 FROM TABLE T1 

RIGHT JOIN TABLE T2 

ON T1.C1= T2.C1;

Example:

SELECT Student.StudentName, StudentCourse.CourseID 

FROM Student 

RIGHT OUTER JOIN StudentCourse 

ON StudentCourse.EnrollNo = Student.EnrollNo 

ORDER BY StudentCourse.CourseID;

3. Full Outer Join contains the results of both the Left and Right outer joins. It is also known as cross join. It will provide a mixture of two tables.

Syntax:

SELECT * FROM T1 

CROSS JOIN T2;

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above

In a relational database management system (RDBMS), there are different types of joins that can be used to combine data from two or more tables in a database. The two most common types of joins are Inner Join and Outer Join.

Inner Join:
An Inner Join returns only the matching rows between the two tables based on a specified condition. It combines data from two tables based on a common column between them, which is specified using the ON keyword in SQL. Only the rows that meet the join condition from both tables are returned. If a row in one table does not have a matching row in the other table, that row will not be included in the result set.

For example, let’s say we have two tables, Table1 and Table2, with the following data:

Table1
ID | Name
1 | John
2 | Sarah
3 | David

Table2
ID | Address
1 | 123 Main St.
2 | 456 Elm St.
4 | 789 Oak St.

If we perform an Inner Join on these tables using the ID column, the result set would only include the matching rows from both tables, which are the rows with ID values of 1 and 2:

SELECT Table1.ID, Table1.Name, Table2.Address
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID

Result:
ID | Name | Address
1 | John | 123 Main St.
2 | Sarah | 456 Elm St.

Outer Join:
An Outer Join returns all the rows from one table and matching rows from the other table based on a specified condition. It combines data from two tables based on a common column between them, which is also specified using the ON keyword in SQL. In addition to the matching rows, it also includes rows from one table that do not have matching rows in the other table.

There are three types of Outer Join: Left Outer Join, Right Outer Join, and Full Outer Join.

Left Outer Join returns all the rows from the left table and matching rows from the right table. If a row in the left table does not have a matching row in the right table, the result set will include NULL values for the columns in the right table.
Right Outer Join returns all the rows from the right table and matching rows from the left table. If a row in the right table does not have a matching row in the left table, the result set will include NULL values for the columns in the left table.
Full Outer Join returns all the rows from both tables, including matching and non-matching rows. If a row in one table does not have a matching row in the other table, the result set will include NULL values for the columns in the table that does not have a match.
For example, let’s say we have the same two tables as before:

Table1
ID | Name
1 | John
2 | Sarah
3 | David

Table2
ID | Address
1 | 123 Main St.
2 | 456 Elm St.
4 | 789 Oak St.

If we perform a Left Outer Join on these tables using the ID column, the result set would include all the rows from Table1 and only the matching rows from Table2:

SELECT Table1.ID, Table1.Name, Table2.Address
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID = Table2.ID

Result:
ID | Name | Address
1 | John | 123 Main St.
2 | Sarah | 456 Elm St.
3 | David | NULL

Advantages of Inner Joins:

Reduced Data Duplication: Inner joins only return rows that have matching values in both tables being joined, which can reduce the amount of duplicate data returned in the result set.

Efficient Query Execution: Since inner joins only involve rows that match in both tables, they can be more efficient in terms of query execution time compared to outer joins.

Data Accuracy: Inner joins only return rows that have matching values in both tables, which can improve data accuracy by excluding irrelevant or mismatched data.

Disadvantages of Inner Joins:

Data Loss: Since inner joins only return rows that have matching values in both tables, some data may be lost if there are no matching values.

Query Complexity: Inner joins can become complex and difficult to write and understand when working with multiple tables.

Overlapping Data: In some cases, inner joins may return overlapping data that needs to be deduplicated in post-processing.

Advantages of Outer Joins:

Increased Data Retrieval: Outer joins can retrieve more data than inner joins since they include non-matching rows in the result set.

Data Integrity: Outer joins can help maintain data integrity by ensuring that all records in the primary table are returned, even if there are no corresponding records in the secondary table.

Data Analysis: Outer joins can be useful for data analysis, especially when exploring relationships between data sets and identifying trends and patterns.

Disadvantages of Outer Joins:

Slow Query Execution: Outer joins can be slower to execute than inner joins, especially when dealing with large data sets.

Data Duplication: Outer joins can return duplicate data when the secondary table has multiple matching records.

Data Quality: Outer joins may return inaccurate or incomplete data if the tables being joined have incomplete or inconsistent data.


My Personal Notes arrow_drop_up
Last Updated : 08 May, 2023
Like Article
Save Article
Similar Reads