Sobes.tech
Junior

What is an SQL Join?

sobes.tech AI

Answer from AI

SQL JOIN is used to combine rows from one or more tables based on a related column between them. It allows creating more informative data sets by merging data from different tables.

Main types of JOIN:

  1. INNER JOIN: Returns only the rows where there are matches in both tables.
  2. LEFT (OUTER) JOIN: Returns all rows from the left table and matching rows from the right. If there are no matches in the right table, NULLs are returned for columns from the right table.
  3. RIGHT (OUTER) JOIN: Returns all rows from the right table and matching rows from the left. If there are no matches in the left table, NULLs are returned for columns from the left table.
  4. FULL (OUTER) JOIN: Returns all rows where there is a match in one of the tables. NULLs are returned for columns from the table without a match.
  5. CROSS JOIN: Returns the Cartesian product of rows from both tables. Each row from the first table is combined with every row from the second.

Example syntax for INNER JOIN:

SELECT
  table1.column1,
  table2.column2
FROM
  table1
INNER JOIN
  table2
ON
  table1.common_column = table2.common_column; -- Join condition, usually on a foreign key