Join in SQL
JOIN in SQL
JOIN is used to combine rows from two or more tables based on a related column between them. It allows you to retrieve data that is spread across multiple tables.
Types of JOINs:
INNER JOIN
Returns rows when there is a match in both tables.LEFT (OUTER) JOIN
Returns all rows from the left table, and matched rows from the right table. If no match, returns NULL on right table columns.RIGHT (OUTER) JOIN
Returns all rows from the right table, and matched rows from the left table. If no match, returns NULL on left table columns.FULL (OUTER) JOIN
Returns rows when there is a match in one of the tables. Rows without match in either table appear with NULLs.CROSS JOIN
Returns the Cartesian product of both tables (all possible combinations).
Basic JOIN Syntax:
SELECT columnsFROM table1JOIN_TYPE table2ON table1.common_column = table2.common_column;JOIN_TYPEcan beINNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN, orCROSS JOIN.
Example: INNER JOIN
SELECT employees.name, departments.department_nameFROM employeesINNER JOIN departments ON employees.department_id = departments.department_id;This retrieves employees along with their department names where department matches.
If you want examples for specific JOIN types or complex JOIN queries, just let me know!