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

SQL Joins Cheat Sheet

This document provides definitions and examples for joining data in SQL using RIGHT JOIN and UNION ALL operators. It shows how RIGHT JOIN keeps all records from the right table and returns missing values for columns from the left table with no match. UNION ALL works like UNION but returns duplicate values, with the same restrictions. Examples demonstrate joining two tables using RIGHT JOIN and combining the results of two SELECT queries on different tables using UNION ALL.

Uploaded by

said
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)
89 views

SQL Joins Cheat Sheet

This document provides definitions and examples for joining data in SQL using RIGHT JOIN and UNION ALL operators. It shows how RIGHT JOIN keeps all records from the right table and returns missing values for columns from the left table with no match. UNION ALL works like UNION but returns duplicate values, with the same restrictions. Examples demonstrate joining two tables using RIGHT JOIN and combining the results of two SELECT queries on different tables using UNION ALL.

Uploaded by

said
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/ 1

Joining Data in SQL RIGHT JOIN UNION ALL

Cheat Sheet
A right join keeps all of the original left table
_ right table
_ result after RI T OIN
GH J
The UNION ALL operator works just like UNION, but it SELECT artist_id

records in the right table and returns id left val


_ id left val _ id left val left val
_ _
returns duplicate values. The same restrictions of UNION FROM artist

missing values for any columns from 1 L1 1 R1 1 L1 R1 hold true for UNION ALL UNION ALL

Learn SQL online at www.DataCamp.com the left table where the joining field
did not find a match. Right joins are
far less common than left joins,
2
3
L2
L3
4
5
R2
R3
4
5 null
L2 R2
R3 Result after

SELECT artist_id

FROM album;
UNION ALL
because right joins can always be re- 4 L4 6 R4 6 null R4

> Definitions used throughout this cheat sheet


written as left joins. left right
id val Result after UNION ALL:
1 A
Result after RI G HT JOIN: id val id val 1 A
artist_id
RI HT JOIN on one field 1 A 1 A 1
G
artist_id name album_id title name 1 B
2
Primary key:
Foreign key:
SELECT *
1 AC/DC 1 For those who rock 1 1 B 4 A 2 A 3
A primary key is a field in a table that uniquely identifies A foreign key is a field in a table which references the FROM artist as art
1 Aerosmith 2 Dream on 2 2 A 5 A 3 A 6
each record in the table. In relational databases, primary primary key of another table. In a relational database, RI T OIN album AS al
GH J b
2 Aerosmith 3 Restless and wild 2 3 A 6 A 4 A
keys can be used as fields to join tables on. one way to join two tables is by connecting the foreign ON art artist_i alb album_i ;
. d = . d 2 AC/DC 4 Let there be rock 1 4 A 4 A
key from one table to the primary key of another. 3 null 5 Rumours 6 5 A

FULL JOIN
One-to-one relationship:
One-to-many relationship:
6 A
Database relationships describe the relationships In a one-to-many relationship, a record in one table can
between records in different tables. When a one-to-one
relationship exists between two tables, a given record in
be related to one or more records in a second table.
However, a given record in the second table will only be
A full join combines a left join and
right join. A full join will return all left table
_ right table
_
result after FULL OIN
J

id left val left val


INTERSECT
one table is uniquely related to exactly one record in the
_ _

related to one record in the first table. records from a table, irrespective of id left val
_ id left val_
1 L1 R1
other table. whether there is a match on the 1 L1 1 R1
2 L2 null
The INTERSECT operator returns only identical rows from two tables. SELECT artist_id

joining field in the other table, 2 L2 4 R2 FROM artist

Many-to-many relationship:
returning null values accordingly. 3 L3 5 R3
3 L3 null left table right table INTERSECT

Result after

_ _

In a many-to-many relationship, records in a given table ‘A’ can be related to one or more records in another table ‘B’, 4 L4 6 R4
4 L4 R2
id val id val INTERSECT SELECT artist_id

and records in table B can also be related to many records in table A. 5 null R3 FROM album;
6 null R4 1 N1 1 N1 id val

> Sample Data FULL JOIN on one field


Result after FULL JOIN:
artist_id
1
name
AC/DC
album_id
1
title
For those who rock
name
1
1 N1
3 L3
4 R2
5 R3
1 N1 Result after INT RS
artist_id
1
E CT:
E

SELECT 4 L4 6 R4 2
Artist Table Album Table 1 AC/DC 4 Let there be rock 1
*

FROM artist as art


2 Aerosmith 2 Balls to the wall 2
EXCEPT
artist_id name album_id title artist_id FULL OUTER OIN album AS al 2 Aerosmith 3 Restless and wild 2
J b

1 AC/DC 1 For those who rock 1 ON art artist_i alb album_i ; 3 Alanis Morissette null null null
. d = . d

2 Aerosmith 2 Dream on 2
3 Alanis Morissette 3 Restless and wild 2 null null 5 Rumours 6
The EXCEPT operator returns only those rows from SELECT artist_id

CROSS JOIN
4 Let there be rock 1 the left table that are not present in the right table.
5 Rumours 6
FROM artist

EXCEPT

SELECT artist_id

INNER JOIN CROSS OIN creates all possible combinations of two


J

tables. CROSS OIN does not require a field to join ON.


J
SELECT name, title

FROM artist

left table
id
_

val
right table
id
_

val
Result after

EXCEPT
FROM album;
An inner join between two tables will left table right table CROSS OIN album; Result after XCEPT:
1 N1 1 N1
E

id val
_ _ J

return only records where a joining id left val id left val artist_id
field, such as a key, finds a match in
_ _

result after INNER OIN result after


Result after CROSS JOIN: 1 N1 4 R2 3 L3 1
1 L1 1 R1
J
CROSS OIN
both tables. id left val left val
J

name title 2
2 L2 4 R2
_ _
id1 id2 AC/DC For those who rock 3 L3 5 R3 4 L4 3
1 L1 R1 table 1 table 2
3 L3 5 R3 1 A AC/DC Dream on 4 L4 6 R4
4 L4 R2 id1 id AC/DC Restless and wild
4 L4 6 R4 1 B
A2
SEMI JOIN
1 AC/DC Let there be rock
1 C AC/DC Rumours
INN R JOIN oin ON one field 2 B Aerosmith For those who rock
2 A
E j

SELECT 3 Aerosmith Dream on


C
A semi join chooses records in the first table where a SELECT
*

FROM artist AS art Result after INN R JOIN: 2 B Aerosmith Restless and wild
*

condition is met in the second table. A semi join makes FROM album

INNER OIN album AS al


E

2 Aerosmith Let there be rock use of a W ERE clause to use the second table as a filter
album_id name artist_id C
W ERE artist_i IN

J b

ON art artist_i alb artist_i ; Aerosmith Rumours for the first.


H H d

1 AC/DC 1 3 A SELECT artist_id

. d = . d

Alanis Morissette For those who rock


(

INN R JOIN with USIN 1 AC/DC 4


3 B Alanis Morissette Dream on
FROM artist ;
Result after

)
E G
2 Aerosmith 2
SELECT *

2 Aerosmith 3 3 C Alanis Morissette Restless and wild left table


_ right table
_ SEMI OIN
Result after Semi oin:
J

FROM artist AS art


Alanis Morissette Let there be rock id col1 col2 id col1
j

INNER OIN album AS al


J b
Alanis Morissette Rumours album_id title artist_id
USIN artist_i ; 1 A B 2 B 1 For those who rock 1
Set Theory Operators in SQL
G ( d)

SELF JOIN 2 B C 3 C 2 Dream on 2


3 Restless and wild 2
3 C
Self-joins are used to compare values in a table to other values of the same table by joining different parts
of a table together. 4 D
SELECT

alb1 artist_i ,

. d

alb1 title AS alb1_title,

Result after Self oin:


artist_id
j

alb1_title alb2_title
UNION

UNION ALL
INTERSECT EXCEPT
ANTI JOIN
1 For those who rock For those who rock
UNION
.

alb2 title AS alb2_title


The anti join chooses records in the first table where a SELECT
2 Dream on Dream on
. *

FROM album AS alb1


condition is NOT met in the second table. It makes use of FROM album

INNER OIN album AS alb2


2 Restless and wild Dream on a W ERE clause to use exclude values from the second W ERE artist_i NOT IN

1 Let there be rock For those who rock


J H H d

ON alb1 artist_i alb2 album_i ;


. d = . d The UNION operator is used to vertically combine the results SELECT artist_id
table. (SELECT artist_id

of two SELECT statements. For UNION to work without errors, FROM artist
FROM artist ;
LEFT JOIN
)

all SELECT statements must have the same number of UNION


Left table after

columns and corresponding columns must have the same SELECT artist_id
left table right table ANTI OIN
Result after Anti oin:
_ _ J

A left join keeps all of the original left table right table result after LEFT OIN data type. UNION does not return duplicates. FROM album; id col1 col2 id col1
j

album_id title artist_id


_ _ J

records in the left table and returns id left val id left val id left val left val
1 A B 1 A
missing values for any columns from
_ _ _ _

Result after UNION: 5 Rumours 6


1 L1 1 R1 1 L1 R1 Result after UNION
the right table where the joining field artist_id 2 B C 4 D
did not find a match.
2 L2 4 R2 2 L2 null id val 1
3 L3 5 R3 3 L3 null left right 1 A 2 3 C
id val id val 3
4 L4 6 R4 4 L4 R2 1 B 4 D
1 A 1 A 2 A 1
Result after L FT JOIN:
E 1 B 4 A 2
3 A
L FT JOIN on one field
E
artist_id name album_id title name 2 A 5 A 2
SELECT 4 A 1
*
1 AC/DC 1 For those who rock 1 3 A 6 A
FROM artist AS art
1 AC/DC 4 Let there be rock 1 5 A 6
LEFT OIN album AS al 4 A
J

ON art artist_i alb album_i ;


. d =
b

. d
2
2
Aerosmith
Aerosmith
2
3
Dream on
Restless and wild
2
2
6 A
Learn Data Skills Online at www.DataCamp.com
3 Alanis Morissette null null null

You might also like