Skip to content
Spring Boot

Spring

Spring Boot

Spring

JOIN’S

admin, March 10, 2025March 10, 2025

1. INNER JOIN

  • Returns records that have matching values in both tables.
  • Only common data between both tables.

Example:

sqlCopyEditSELECT employees.name, employees.salary, department.dept_name
FROM employees
INNER JOIN department ON employees.dept_id = department.dept_id;

Output:

namesalarydept_name
John50000IT
Sarah60000HR

💡 2. LEFT JOIN (LEFT OUTER JOIN)

  • Returns all records from the left table, and the matched records from the right table.
  • If no match, NULL values will appear from the right table.

Example:

sqlCopyEditSELECT employees.name, employees.salary, department.dept_name
FROM employees
LEFT JOIN department ON employees.dept_id = department.dept_id;

Output:

namesalarydept_name
John50000IT
Sarah60000HR
Mike40000NULL

💡 3. RIGHT JOIN (RIGHT OUTER JOIN)

  • Returns all records from the right table, and the matched records from the left table.
  • If no match, NULL values will appear from the left table.

Example:

sqlCopyEditSELECT employees.name, employees.salary, department.dept_name
FROM employees
RIGHT JOIN department ON employees.dept_id = department.dept_id;

Output:

namesalarydept_name
John50000IT
Sarah60000HR
NULLNULLSales

💡 4. FULL OUTER JOIN

  • Returns all records when there is a match in either left table or right table.
  • If no match, NULL values will appear.

Example:

sqlCopyEditSELECT employees.name, employees.salary, department.dept_name
FROM employees
FULL OUTER JOIN department ON employees.dept_id = department.dept_id;

Output:

namesalarydept_name
John50000IT
Sarah60000HR
Mike40000NULL
NULLNULLSales

💡 5. CROSS JOIN

  • Returns the Cartesian product of both tables.
  • This means every row of the first table joins with every row of the second table.

Example:

sqlCopyEditSELECT employees.name, department.dept_name
FROM employees
CROSS JOIN department;

Output:

namedept_name
JohnIT
JohnHR
SarahIT
SarahHR

💡 6. SELF JOIN

  • Joins a table with itself.
  • Useful for comparing values in the same table.

Example:

sqlCopyEditSELECT A.name AS Employee, B.name AS Manager
FROM employees A, employees B
WHERE A.manager_id = B.emp_id;

Output:

EmployeeManager
JohnSarah
MikeJohn

✅ Summary

JOIN TypeDescription
INNER JOINReturns matching records from both tables
LEFT JOINReturns all records from the left table + matched from the right table
RIGHT JOINReturns all records from the right table + matched from the left table
FULL JOINReturns all records from both tables
CROSS JOINReturns cartesian product of both tables
SELF JOINJoins table with itself
Spring Boot

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2025 Spring Boot | WordPress Theme by SuperbThemes