You are on page 1of 1

Joining Data in SQL RIGHT JOIN UNION ALL

A right join keeps all of the original left_table right_table result after RIGHT JOIN

Cheat Sheet
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

the left table where the joining field


Learn SQL online at www.DataCamp.com did not find a match. Right joins are
2 L2 4 R2 4 L2 R2

Result after

SELECT artist_id

3 L3 5 R3 5 null R3 FROM album;


far less common than left joins,
UNION ALL
because right joins can always be re- 4 L4 6 R4 6 null R4
id val
written as left joins. Result after UNION ALL:
left right
1 A

> Definitions used throughout this cheat sheet Result after RIG HT JOIN: id val id val
1 A
artist_id

RIG HT JOIN on one field artist_id name album_id title name


1 A 1 A
1 B
1

1 B 4 A 2
SELECT*
1 AC/DC 1 For those who rock 1 2 A
Primary key:
Foreign key:
3
2 A 5 A
FROM artist as art
1 Aerosmith 2 Dream on 2 3 A
A primary key is a field in a table that uniquely identifies A foreign key is a field in a table which references the
1
RIGHT JOIN album AS alb
2 Aerosmith 3 Restless and wild 2 3 A 6 A
each record in the table. In relational databases, primary primary key of another table. In a relational database, 4 A
2
ON art.artist_id = alb.album_id; 2 AC/DC 4 Let there be rock 1 4 A
keys can be used as fields to join tables on. one way to join two tables is by connecting the foreign 4 A
2
key from one table to the primary key of another. 3 null 5 Rumours 6
5 A 1

6 A 6
One-to-one relationship:
One-to-many relationship:

FULL JOIN
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. A full join combines a left join and
left_table right_table
result after FULL JOIN
INTERSECT
However, a given record in the second table will only be right join. A full join will return all id left_val left_val

one table is uniquely related to exactly one record in the related to one record in the first table.
id left_val id left_val
records from a table, irrespective of 1 L1 R1
other table. 1 L1 1 R1 The INTERSECT operator returns only identical rows from two tables. SELECT artist_id

whether there is a match on the 2 L2 null


2 L2 4 R2 FROM artist

joining field in the other table,


T

3 L3 null
Many-to-many relationship:
left_table right_table
INTERSEC
returning null values accordingly. 3 L3 5 R3
Result after

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

4 L4 6 R4 id val id val INTERSECT


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

1 N1 4 R2 1 N1 Result after INTERSECT:


Result after FULL JOIN:
> Sample Data
FULL JOIN on one field
artist_id name album_id title name 3 L3 5 R3
artist_id

1
1 AC/DC 1 For those who rock 1
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


artist_id name album_id title artist_id
FULL OUTER JOIN album AS alb

EXCEPT
2 Aerosmith 3 Restless and wild 2
1 AC/DC 1 For those who rock 1
ON art.artist_id = alb.album_id;
3 Alanis Morissette null null null
2 Aerosmith 2 Dream on 2
null null 5 Rumours 6
3 Alanis Morissette 3 Restless and wild 2
X P
The E CE T operator returns only those rows from SELECT artist_id

4 Let there be rock 1


the left table that are not present in the right table. FROM artist

5 Rumours 6 CROSS JOIN X PT

E CE

SELECT artist_id

name, e
left_table right_table
INNER JOIN CROSS JOIN creates all possible combinations of two SELECT titl
Result after

FROM album;
tables. CROSS JOIN does not require a field to join ON. FROM artist
id val id val X P
E CE T

An inner join between two tables will left_table right_table CROSS JOIN album; Result after E XCEPT:
1 N1 1 N1 id val
return only records where a joining id left_val id left_val artist_id
result after
Result after CROSS JOIN:
field, such as a key, finds a match in result after INNER JOIN
CROSS JOIN
1 N1 4 R2 3 L3 1
1 L1 1 R1
both tables. name title 2
id left_val left_val
2 L2 4 R2 id1 id2 3 L3 5 R3 4 L4
AC/DC For those who rock 3
1 L1 R1 table 1 table 2
1 A AC/DC Dream on
3 L3 5 R3 4 L4 6 R4
4 L4 R2 id1 id AC/DC Restless and wild
4 L4 6 R4 1 B
2 AC/DC Let there be rock
1 A

INNER JOIN join ON one field 2 B


1 C AC/DC Rumours
SEMI JOIN
Aerosmith For those who rock
2 A
SELECT *
Aerosmith Dream on
3 C
2 B A semi join chooses records in the first table where a SELECT *

FROM artist AS art


Result after INNER JOIN: Aerosmith Restless and wild

INNER JOIN album AS alb

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

Aerosmith Let there be rock

ON art.artist_id = alb.artist_id;
album_id name artist_id
2 C
use of a WHERE clause to use the second table as a filter WHERE artist_id I N

Aerosmith Rumours
1 AC/DC 1 3 A for the first. (SELECT artist_id

Alanis Morissette For those who rock


1 AC/DC 4 FROM artist);
INNER JOIN with USING 3 B Alanis Morissette Dream on Result after

2 Aerosmith 2
SELECT *
Alanis Morissette Restless and wild left_table right_table SEMI JOIN
2 Aerosmith 3 3 C
FROM artist AS art
Alanis Morissette Let there be rock
Result after Semi join:
id col1 col2 id col1
INNER JOIN album AS alb
Alanis Morissette Rumours album_id title artist_id

USING (artist_id); 1 A B 2 B 1 For those who rock 1

SELF JOIN Set Theory Operators in S QL 2 B C 3 C


2

3
Dream on

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
4 D
of a table together.

SELEC T
Result after Self join: N

UNIO INTERSECT EXCEPT

art1 .artist_id ,
UNION ALL ANTI JOIN
art1 .title 1 e,

AS art _titl
artist_id 1
art _title 2
art _title

art2 AS art2_title

1 For those who rock For those who rock


.title The anti join chooses records in the first table where a SELECT *

FROM artist as art1

2 Dream on Dream on UNION condition is NOT met in the second table. It makes use of FROM albu m

OIN artist as art2

2 Restless and wild Dream on


INNER J a WHERE clause to use exclude values from the second WHERE artist_id NOT I N

ON art1 artist_i art2 album_i


1 Let there be rock For those who rock
. 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);
N
Left table after

LEFT JOIN all SELECT statements must have the same number of UNIO
left_table right_table ANTI JOIN
columns and corresponding columns must have the same SELECT artist_id

Result after Anti join:


left_table right_table result after LEFT JOIN
data type. UNION does not return duplicates. FROM album; id col1 col2 id col1
A left join keeps all of the original
album_id title artist_id
records in the left table and returns id left_val id left_val id left_val left_val
Result after UNION: 1 A B 1 A 5 Rumours 6
missing values for any columns from
1 L1 1 R1 1 L1 R1 Result after UNION
the right table where the joining field 2 B C 4 D
artist_id
did not find a match.
2 L2 4 R2 2 L2 null id val
1
left right 3 C
3 L3 5 R3 3 L3 null 1 A
2
id val id val
4 L4 6 R4 4 L4 R2 1 B 3 4 D
1 A 1 A
6
2 A
Result after LEFT JOIN: 1 B 4 A
3 A
LEFT JOIN on one field artist_id name album_id title name 2 A 5 A
4 A
SELECT *
1 AC/DC 1 For those who rock 1
3 A 6 A
FROM artist AS art
5 A

Learn Data Skills Online at www.DataCamp.com


1 AC/DC 4 Let there be rock 1
4 A
LEFT JOIN album AS alb
2 Aerosmith 2 Dream on 2 6 A

ON art.artist_id = alb.album_id; 2 Aerosmith 3 Restless and wild 2

3 Alanis Morissette null null null

You might also like