HTML Dropdown

Friday, 8 May 2026

Understanding SQL Joins in a Simple Way (INNER JOIN, LEFT JOIN, RIGHT JOIN)

 When working with databases, data is often stored in multiple tables instead of one big table. To get meaningful information, we need a way to combine data from two tables. This is where SQL Joins are used.



What Are SQL Joins?

SQL Joins are used to combine rows from two tables based on a related column between them.

Think of joins as a way to connect tables and get data that makes sense together.




INNER JOIN – Common Data from Both Tables

What is INNER JOIN?

An INNER JOIN returns only the matching records from both tables.

If there is no match, the data is not shown.

Simple Explanation:

  • Takes data that exists in both Table A and Table B
  • Ignores non‑matching rows

Simple Diagram (Idea):

Table A  ∩  Table B  →  Result

(Only the overlapping part is returned)

Example:

SELECT *
FROM Students
INNER JOIN Marks
ON Students.id = Marks.student_id;

Exam tip:

INNER JOIN = Common data from both tables


LEFT JOIN – All Data from Left Table

What is LEFT JOIN?

A LEFT JOIN returns:

  • All records from the left table
  • Plus matching records from the right table

If there is no match in the right table, the result will show NULL values.

Simple Explanation:

  • Left table is more important
  • Nothing from the left table is lost

Simple Diagram (Idea):

[ Table A ] ←→ (Table B)
Result = Full Table A + Matching from Table B

Example:

SELECT *
FROM Students
LEFT JOIN Marks

ON Students.id = Marks.student_id

Exam tip:
LEFT JOIN = Everything from left table


RIGHT JOIN – All Data from Right Table

What is RIGHT JOIN?

A RIGHT JOIN returns:

  • All records from the right table
  • Plus matching records from the left table

If there is no match in the left table, NULL values are shown.

Simple Explanation:

  • Right table is more important
  • Nothing from the right table is lost

Simple Diagram (Idea):

(Table A) ←→ [ Table B ]
Result = Full Table B + Matching from Table A

Examples:

SELECT *
FROM Students
RIGHT JOIN Marks
ON Students.id = Marks.student_id;

Exam tip:
RIGHT JOIN = Everything from right table


Quick Comparison (Easy to Remember)

  • INNER JOIN → Only matching data from both tables
  • LEFT JOIN → All data from left table + matching right
  • RIGHT JOIN → All data from right table + matching left

FULL OUTER JOIN = LEFT JOIN + RIGHT JOIN


Simple Explanation (Easy to Remember)

  • INNER JOIN → only matching data
  • LEFT JOIN → all from left table
  • RIGHT JOIN → all from right table
    FULL OUTER JOIN → everything from both tables

Nothing is lost.


Simple Table Diagram (In Words)

Imagine two tables:

Table A → Students
Table B → Marks

A FULL OUTER JOIN will show:

  • Students who have marks
  • Students who don’t have marks
  • Marks that don’t belong to any student
Table A        Table B
   ○────────○
 Result = complete union of both tables

(Think of both circles fully included)


Example of FULL OUTER JOIN

SELECT *
FROM Students
FULL OUTER JOIN Marks
ON Students.id = Marks.student_id;

What this query does:

  • Shows all students
  • Shows all marks
  • Matches student IDs where possible
  • Shows NULL when data is missing on either side

Why FULL OUTER JOIN is Useful

  • When you want complete data
  • When you don’t want to lose any records
  • Helpful in data analysis and reports

No comments:

Post a Comment