MySQL Joins Interview Guide
MySQL Joins Interview Guide
Definition
In MySQL, a JOIN is used to combine rows from two or more tables based on a related column (usually a
foreign key).
2. LEFT JOIN: Returns all rows from the left table, with matching rows from the right table.
3. RIGHT JOIN: Returns all rows from the right table, with matching rows from the left table.
4. FULL JOIN: Returns all rows when there is a match in either table (not supported directly in MySQL).
1. INNER JOIN
Example:
FROM employees
ON employees.dept_id = departments.dept_id;
Returns all rows from the left table, even if there's no match in the right.
Example:
FROM employees
ON employees.dept_id = departments.dept_id;
MySQL Joins - Interview Guide
Returns all rows from the right table, even if there's no match in the left.
Example:
FROM employees
ON employees.dept_id = departments.dept_id;
Returns all records when there is a match in either left or right table.
Example:
FROM employees
ON employees.dept_id = departments.dept_id
UNION
FROM employees
ON employees.dept_id = departments.dept_id;
5. SELF JOIN
Example:
FROM employees A
JOIN employees B
MySQL Joins - Interview Guide
ON A.manager_id = B.id;
6. CROSS JOIN
Example:
FROM employees
Interview Tips
- Use LEFT JOIN when you want all records from one table regardless of matches.