What is the method for performing a join in SQL between two tables?

Join clauses are typically used in SQL to achieve a join query between two tables. Here are several common methods for join queries.

  1. INNER JOIN: an inner join returns the intersection of rows that match between two tables.
SELECT *
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
  1. LEFT JOIN, also known as LEFT OUTER JOIN, returns all rows from the left table and the matched rows from the right table.
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
  1. RIGHT JOIN (or RIGHT OUTER JOIN): Right join, which returns all rows from the right table and the matched rows from the left table.
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
  1. FULL JOIN, also known as FULL OUTER JOIN, returns all rows from both the left and right tables, filling in with NULL where there is no match.
SELECT *
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;

Apart from the JOIN clause mentioned above, you can also use the WHERE clause to perform table joins, but it is generally recommended to use the JOIN clause for table joins because it is clearer and easier to understand.

广告
Closing in 10 seconds
bannerAds