Sql joins

From wikinotes

Joins

Overview

Joins let you

  • query rows from multiple tables at once
  • filter your table based on an association to another table
SELECT *                                   # select all rows from 'users' and from 'department_users' tables
FROM users
INNER JOIN department_users                # only return subset of rows where the 'ON' condition is met
ON department_users.user_id = users.id     # what determines a match? multiple conditions can be defined

Types

There are generally at least 3x types of joins.

  • INNER JOIN - rows where match-condition is met
  • LEFT JOIN - all rows in src-table, columns in join-table without a match are all NULL
  • RIGHT JION - all rows in join-table, columns in src-tabl without a match are all NULL

Subqueries

You can also join on a nested query.
See sql queries for an example.