Mastering SQL Joins: Visual Explanation

SQL Joins Overview

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

       Table A                     Table B
   +----+----------+           +----+----------+
   | id | name     |           | id | order_val|
   +----+----------+           +----+----------+
   | 1  | Alice    |           | 1  | $150     |
   | 2  | Bob      |           | 3  | $400     |
   +----+----------+           +----+----------+
  1. INNER JOIN: Returns records that have matching values in both tables. sql SELECT A.name, B.order_val FROM TableA A INNER JOIN TableB B ON A.id = B.id; -- Result: Alice | $150
  2. LEFT JOIN (OUTER): Returns all records from the left table, and the matched records from the right table. sql SELECT A.name, B.order_val FROM TableA A LEFT JOIN TableB B ON A.id = B.id; -- Result: -- Alice | $150 -- Bob | NULL
  3. RIGHT JOIN (OUTER): Returns all records from the right table, and the matched records from the left table.
  4. FULL OUTER JOIN: Returns all records when there is a match in either left or right table.

Related Articles