0% found this document useful (0 votes)
203 views

Learn SQL - Multiple Tables Cheatsheet - Codecademy

The document provides explanations and examples of different SQL clauses including outer join, WITH clause, UNION clause, CROSS JOIN clause, foreign key, primary key, and inner join. It explains that an outer join returns all rows from the left table even if the join condition is not met, the WITH clause stores query results in a temporary table, the UNION clause combines results from multiple queries and removes duplicates, a CROSS JOIN returns all possible combinations of rows from two tables, a foreign key references the primary key of another table, a primary key uniquely identifies each record in a table, and an inner join returns results that match the ON clause condition.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

Learn SQL - Multiple Tables Cheatsheet - Codecademy

The document provides explanations and examples of different SQL clauses including outer join, WITH clause, UNION clause, CROSS JOIN clause, foreign key, primary key, and inner join. It explains that an outer join returns all rows from the left table even if the join condition is not met, the WITH clause stores query results in a temporary table, the UNION clause combines results from multiple queries and removes duplicates, a CROSS JOIN returns all possible combinations of rows from two tables, a foreign key references the primary key of another table, a primary key uniquely identifies each record in a table, and an inner join returns results that match the ON clause condition.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn SQL

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”.

CROSS JOIN Clause


The CROSS JOIN clause is used to combine each row from
one table with each row from another in the result set. SELECT shirts.shirt_color,
This JOIN is helpful for creating all possible    pants.pants_color
combinations for the records (rows) in two tables. FROM shirts
The given query will select the shirt_color and CROSS JOIN pants;
pants_color columns from the result set, which will
contain all combinations of combining the rows in the
shirts and pants tables. If there are 3 different shirt
colors in the shirts table and 5 different pants colors in
the pants table then the result set will contain 3 x 5 = 15
rows.
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 specific row, the use of foreign key plays a
vital role. For instance, to track all the orders of a specific
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 other SELECT * 

results based on common column values specified using FROM books


an ON clause. INNER JOIN is the default JOIN and it will

JOIN authors
only return results matching the condition specified by

  ON books.author_id = authors.id;


ON .

You might also like