Sobes.tech
Junior — Middle

Can you explain how the INNER JOIN operation works in SQL and how it differs from other types of joins?

sobes.tech AI

Answer from AI

The INNER JOIN operation in SQL is used to combine rows from two tables based on matching values in related columns. It returns only those records where there is a match in both tables.

Differences from other types of joins:

  • LEFT JOIN returns all rows from the left table and the matching rows from the right table; if there is no match, NULLs are shown in the right table's columns.
  • RIGHT JOIN is the opposite — all from the right and the matching from the left.
  • FULL 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 an existing department.