Professional Documents
Culture Documents
_ _ GHT JOIN
Cheat Sheet
A right join keeps all of the original left table right table result after RI
The UNION ALL operator works just like UNION, but it SELECT artist_id
Result after
SELECT artist_id
FROM album;
3 L3 5 R3 5 null R3
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 RI GHT JOIN: id val id val
1 A
artist_id
2
SELECT*
1 B 4 A
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 6
each record in the table. In relational databases, primary primary key of another table. In a relational database,
RIGHT JOIN album AS alb
2 Aerosmith 3 Restless and wild 2 3 A 6 A
4 A
key from one table to the primary key of another. 3 null 5 Rumours 6
5 A
FULL JOIN
6 A
One-to-one relationship:
One-to-many relationship:
Database relationships describe the relationships In a one-to-many relationship, a record in one table can
JOIN INTERSECT
between records in different tables. When a one-to-one be related to one or more records in a second table. result after FULL
A full join combines a left join and
relationship exists between two tables, a given record in However, a given record in the second table will only be right join. A full join will return all
_
left table _
right table
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
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 R2
id val id val SELECT artist_id
4 L4 6 R4 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
AC/DC For those who rock
SELECT *
1 1 1
4 L4 6 R4 2
Artist Table Album Table 1 AC/DC 4 Let there be rock 1
FROM artist as art
3
Aerosmith
Alanis Morissette
3
null
Restless and wild
null
2
null
EXCEPT
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
CROSS JOIN
the left table that are not present in the right table. FROM artist
5 Rumours 6
X PT
E CE
SELECT artist_id
tables. CROSS JOIN does not require a field to join ON. FROM artist
id val id val X P
E CE T
field, such as a key, finds a match in result after INNER JOIN result after
Result after CROSS JOIN:
1 L1 1 R1
CROSS JOIN 1 N1 4 R2 3 L3 1
both tables.
id _
left val _
left val name title 2
id1 id2 3 L3 5 R3 4 L4
2 L2 4 R2 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
3 Aerosmith Dream on
A semi join chooses records in the first table where a SELECT *
Morissette
Rumours
2 Aerosmith 2
SELECT Alanis Morissette Restless and wild _
left table _
right table SEMI JOIN
C
Semi join:
2 Aerosmith 3 3
FROM artist AS art
Alanis Morissette Let there be rock
Result after
id col1 col2 id col1
INNER JOIN album AS al b
Alanis Morissette Rumours album_id title artist_id
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.
T
Self join: N
INTERSECT EXCEPT
JOIN
SELEC UNIO
Result after
art1.artist_id,
UNION ALL ANTI
1 2
art1.title AS art1_title,
art2.title AS art2_title
2 Dream on Dream on UNION condition is NOT met in the second table. It makes use of FROM albu m
NOT I
ON art1.artist_id = art2.album_id;
For those who rock
1 Let there be rock
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 N
all SELECT statements must have the same number of UNIO Left table after
E
3 A
L FT JOIN on one field artist_id name album_id title name
2
2 A 5 A
SELECT *
1 AC/DC 1 For those who rock 1
3 A 6 A
4 A 1
5 A 6
FROM artist AS art
1 AC/DC 4 Let there be rock 1
JOIN album AS b