Sobes.tech
Junior

How to select data from two tables with a single query?

sobes.tech AI

Answer from AI

You can use JOIN in SQL. It combines rows from two or more tables based on a related column between them.

Types of JOINs:

  • INNER JOIN: Selects rows that have matching values in both tables.
  • LEFT (OUTER) JOIN: Selects all rows from the left table and the matching rows from the right table. If there is no match, the result will contain NULLs in the columns of the right table.
  • RIGHT (OUTER) JOIN: Selects all rows from the right table and the matching rows from the left table. If there is no match, the result will contain NULLs in the columns of the left table.
  • FULL (OUTER) JOIN: Selects all rows when there is a match in either of the tables.

Example of INNER JOIN:

SELECT
    table1.column1,
    table2.column2
FROM
    table1
INNER JOIN
    table2
ON
    table1.common_column = table2.common_column;