0% found this document useful (0 votes)
8 views3 pages

Learn SQL_ Multiple Tables Cheatsheet _ Codecademy

This document is a cheatsheet for SQL operations involving multiple tables, covering key concepts such as outer joins, the WITH clause, UNION, CROSS JOIN, foreign keys, primary keys, and inner joins. It provides example SQL queries to illustrate how these operations work. The cheatsheet is designed to help users understand how to combine and manipulate data from different tables in SQL.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Learn SQL_ Multiple Tables Cheatsheet _ Codecademy

This document is a cheatsheet for SQL operations involving multiple tables, covering key concepts such as outer joins, the WITH clause, UNION, CROSS JOIN, foreign keys, primary keys, and inner joins. It provides example SQL queries to illustrate how these operations work. The cheatsheet is designed to help users understand how to combine and manipulate data from different tables in SQL.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

06/11/24, 10:57 Learn SQL: Multiple Tables Cheatsheet | Codecademy

Cheatsheets / Learn SQL

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

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-multiple-tables/cheatsheet 1/3
06/11/24, 10:57 Learn SQL: Multiple Tables Cheatsheet | Codecademy

CROSS JOIN Clause

The CROSS JOIN clause is used to combine each SELECT shirts.shirt_color,


row from one table with each row from another in the
pants.pants_color
result set. This JOIN is helpful for creating all possible
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.

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-multiple-tables/cheatsheet 2/3
06/11/24, 10:57 Learn SQL: Multiple Tables Cheatsheet | Codecademy

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

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-multiple-tables/cheatsheet 3/3

You might also like