Junior — Middle
What is the purpose of the JOIN operator in SQL and how is it used to combine data from multiple tables?
sobes.tech AI
Answer from AI
The JOIN operator in SQL is used to combine rows from two or more tables based on a related column between them. This allows you to retrieve combined data from different tables.
Main types of JOIN:
- INNER JOIN — returns only matching records in both tables.
- LEFT JOIN (LEFT OUTER JOIN) — returns all records from the left table and the matching records from the right table, if any.
- RIGHT JOIN (RIGHT OUTER JOIN) — returns all records from the right table and the matching records from the left table.
- FULL JOIN (FULL OUTER JOIN) — returns all records from both tables, filling with NULL where there are no matches.
Example of joining "employees" and "departments" tables on the department_id field:
SELECT employees.name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.id;
This query will return the names of employees and the names of their departments where there are matches.