SQL Joins
When working with relational databases, data is often stored in multiple tables. To combine this data meaningfully, we use SQL Joins.
In this article, we will understand:
-
✅ INNER JOIN
-
✅ LEFT JOIN
-
✅ RIGHT JOIN
-
✅ CROSS JOIN
Using simple and clear examples.
📘 Example Tables
Notice:
-
Student Devi (dept_id 40) has no matching department.
-
Department MECH (dept_id 50) has no students.
Student Devi (dept_id 40) has no matching department.
Department MECH (dept_id 50) has no students.
🔹 1. INNER JOIN
📌 Definition
Returns only the matching records from both tables.
Query:
SELECT s.student_id, s.name, d.dept_name
FROM Student s
INNER JOIN Department d
ON s.dept_id = d.dept_id;
EXACT OUTPUT:
Explanation
Only students whose dept_id matches a department are shown.
Devi is excluded
MECH is excluded
Only students whose dept_id matches a department are shown.
Devi is excludedMECH is excluded
2. LEFT JOIN
Definition
Returns all records from the left table and matching records from the right table. If no match, NULL is returned.
Returns all records from the left table and matching records from the right table. If no match, NULL is returned.
Query
Explanation
3. RIGHT JOIN
Definition
Explanation
. CROSS JOIN
Definition
Query
Comments
Post a Comment