Sobes.tech
Junior

Tell about joins in databases.

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.

There are four main types of JOIN:

  • INNER JOIN (or just JOIN): Returns all rows where there is a match in both tables.

  • LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and the matching rows from the right table. If there is no match in the right table, the result will contain NULL for columns from the right table.

  • RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and the matching rows from the left table. If there is no match in the left table, the result will contain NULL for columns from the left table.

  • FULL JOIN (or FULL OUTER JOIN): Returns all rows when there is a match in one of the tables. If there is no match, the result will contain NULL for columns from the table without a match.

Syntax examples:

-- INNER JOIN
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;
-- LEFT JOIN
SELECT *
FROM Table1
LEFT JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;
-- RIGHT JOIN
SELECT *
FROM Table1
RIGHT JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;
-- FULL JOIN
SELECT *
FROM Table1
FULL JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;