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 SELECT column_name(s)
even if the join condition is not met. In a LEFT JOIN ,
FROM table1
every row in the left table is returned in the result set,
and if the join condition is not met, then NULL LEFT JOIN table2
values are used to fill in the columns from the right ON table1.column_name =
table.
table2.column_name;
WITH Clause
The WITH clause stores the result of a query in a WITH temporary_movies AS (
temporary table ( temporary_movies ) using an alias.
SELECT *
Multiple temporary tables can be defined with one
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 SELECT name
appear from multiple SELECT statements and filter
FROM first_names
duplicates.
For example, given a first_names table with a column UNION
name containing rows of data “James” and SELECT name
“Hermione”, and a last_names table with a column FROM last_names
name containing rows of data “James”, “Hermione”
and “Cassidy”, the 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 SELECT *
more than one table by joining them together with
FROM books
other results based on common column values
specified using an ON clause. INNER JOIN is the JOIN authors
default JOIN and it will only return results matching ON books.author_id = authors.id;
the condition specified by ON .
Print Share