Sobes.tech
Junior — Middle

Can you explain the different types of table joins in SQL?

sobes.tech AI

Answer from AI

In SQL, there are several main types of table joins:

  • INNER JOIN — returns only the rows that match the join condition in both tables.
  • LEFT JOIN (LEFT OUTER JOIN) — returns all rows from the left table and the matching rows from the right; if there is no match, NULLs are used for the right table.
  • RIGHT JOIN (RIGHT OUTER JOIN) — similar to LEFT JOIN, but returns all rows from the right table.
  • FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, filling in NULLs where there are no matches.

Example of INNER JOIN:

SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

This query will return the names of employees and their departments only for those employees who have a corresponding department.