Learn SQL - Multiple Tables Cheatsheet - Codecademy
Learn SQL - Multiple Tables Cheatsheet - Codecademy
Multiple Tables
Outer Join
An outer join will combine rows from different tables even
if the join condition is not met. In a LEFT JOIN , every row SELECT column_name(s)
in the left table is returned in the result set, and if the join FROM table1
condition is not met, then NULL values are used to fill in LEFT JOIN table2
the columns from the right table. ON table1.column_name
= table2.column_name;
WITH Clause
The WITH clause stores the result of a query in a
temporary table ( temporary_movies ) using an alias. WITH temporary_movies AS (
Multiple temporary tables can be defined with one SELECT *
instance of the WITH keyword. FROM movies
)
SELECT *
FROM temporary_movies
WHERE year BETWEEN 2000 AND 2020;
UNION Clause
The UNION clause is used to combine results that appear
from multiple SELECT statements and filter duplicates. SELECT name
For example, given a first_names table with a column FROM first_names
name containing rows of data “James” and “Hermione”, UNION
and a last_names table with a column name containing SELECT name
rows of data “James”, “Hermione” and “Cassidy”, the
FROM last_names
result of this query would contain three name s:
“Cassidy”, “James”, and “Hermione”.
Primary Key
A primary key column in a SQL table is used to uniquely
identify each record in that table. A primary key cannot
be NULL . In the example, customer_id is the primary key.
The same value cannot re-occur in a primary key column.
Primary keys are often used in JOIN operations.
Inner Join
The JOIN clause allows for the return of results from
more than one table by joining them together with other SELECT *
JOIN authors
only return results matching the condition specified by