SQL and Databases For Web Development - Working With Multiple SQL Tables Cheatsheet - Codecademy
SQL and Databases For Web Development - Working With Multiple SQL Tables Cheatsheet - Codecademy
WITH Clause
The WITH clause stores the result of a query in a
temporary table ( temporary_movies ) using an
WITH temporary_movies AS (
alias. SELECT *
Multiple temporary tables can be de ned with one FROM movies
instance of the WITH keyword. )
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 lter
SELECT name
duplicates. FROM first_names
For example, given a first_names table with a
UNION
SELECT name
column name containing rows of data “James” and
FROM last_names
“Hermione”, and a last_names table with a
column name containing rows of data “James”,
“Hermione” and “Cassidy”, the result of this query
would contain three name s: “Cassidy”, “James”, and
“Hermione”.
/
Foreign Key
A foreign key is a reference in one table’s records to the
primary key of another table. To maintain multiple
records for a speci c row, the use of foreign key plays a
vital role. For instance, to track all the orders of a
speci c customer, the table order (illustrated at the
bottom of the image) can contain a foreign key.
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 SELECT *
other results based on common column values FROM books
speci ed 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 speci ed by ON .