Sobes.tech
Junior

What are the types of join queries?

sobes.tech AI

Answer from AI

In SQL, there are four main types of JOIN:

  • INNER JOIN: Returns only those rows where the value in the joined columns exists 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 are no matches in the "right" table, the columns will contain NULL.
  • RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the "right" table and the matching rows from the "left" table. If there are no matches in the "left" table, the columns will contain NULL.
  • FULL JOIN (or FULL OUTER JOIN): Returns all rows from both tables. If there is no match in one of the tables for a row from the other table, the columns in that row will contain NULL.

There is also CROSS JOIN, which returns the Cartesian product of rows from two tables (each row from the first table is combined with each row from the second).

Example syntax in SQL:

-- INNER JOIN
SELECT *
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

-- LEFT JOIN
SELECT *
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;

-- RIGHT JOIN
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;

-- FULL JOIN
SELECT *
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;

-- CROSS JOIN
SELECT *
FROM table1
CROSS JOIN table2;