/plushcap/analysis/cockroach-labs/sql-join

SQL JOINs and how to use them, with examples

What's this blog post about?

The different types of SQL joins are used for combining rows from two or more tables, based on a related column between them. Here are the four most common join types: 1. INNER JOIN: Returns records that have matching values in both tables. 2. LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table. The result is NULL from the right side, if there is no match. 3. RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table. The result is NULL from the left side, if there is no match. 4. FULL (OUTER) JOIN: Return all records when there is a match in either left or right table. Additionally, SQL tables can be joined with themselves using a self-join command. For example, imagine we have a school directory table listing teachers and their students. We could use a self-join to output the names of students along with their assigned teacher's name next to them. Lastly, a CROSS JOIN returns the Cartesian product of the columns you opt to join, giving all possible combinations of the values in both tables.

Company
Cockroach Labs

Date published
Feb. 5, 2024

Author(s)
Charlie Custer

Word count
2198

Hacker News points
None found.

Language
English


By Matt Makai. 2021-2024.