Joining Data in SQL Updated 0425
Joining Data in SQL Updated 0425
Cheat Sheet
A right join keeps all of the original left_table right_table result after RIGHT JOIN
The UNION ALL operator works just like UNION, but it SELECT artist_id
records in the right table and returns id left_val id right_val id left_val right_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
Result after
FROM album;
far less common than left joins, 3 L3 5 R3 5 null R3
UNION ALL
because right joins can always be re- 4 L4 6 R4 6 null R4
FULL JOIN
6 A 6
One-to-one relationship:
One-to-many relationship:
INTERSECT
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 be related to one or more records in a second table. A full join combines a left join and result after FULL JOIN
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 right_val
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 right_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
Many-to-many relationship:
3 L3 null left_table right_table INTERSECT
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 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
1 AC/DC 1 For those who rock 1 ON art.artist_id = alb.artist_id; 2 Aerosmith 3 Restless and wild 2
2 Aerosmith 2 Dream on 2 3 Alanis Morissette null null null
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
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 JOIN album; Result after EXCEPT:
return only records where a joining 1 N1 1 N1 id val artist_id
id left_val id right_val 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 3
both tables. 1 L1 1 R1
name title
id left_val right_val
3 L3 5 R3
2 L2 4 R2 id1 id2 AC/DC For those who rock 4 L4
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
INNER JOIN join ON one field 2 B Aerosmith For those who rock
2 A
SELECT *
3 Aerosmith Dream on
FROM artist AS art
Result after INNER JOIN:
C
2 B Aerosmith Restless and wild
A semi join chooses records in the first table where a SELECT *
artist_id name title album_id 2 C use of a WHERE clause to use the second table as a filter WHERE artist_id IN
SELECT *
Alanis Morissette Restless and wild SEMI JOIN
2 Aerosmith Restless and wild 3 3 C
Result after Semi join:
FROM artist AS art
Alanis Morissette Let there be rock 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
alb1.artist_id,
alb1.title AS alb1_title,
UNION ALL
INTERSECT EXCEPT
ANTI JOIN
alb2.title AS alb2_title
UNION
1 AC/DC 1 For those who rock
FROM album AS alb1
2 Aerosmith 2 Dream on The anti join chooses records in the first table where a SELECT *
ON alb1.artist_id = alb2.artist_id
1 AC/DC 4 Let there be rock a WHERE clause to use exclude values from the second WHERE artist_id NOT IN
WHERE alb1.album_id<>alb2.album_id; 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
columns and corresponding columns must have the same
UNION
SELECT artist_id
left_table right_table
Left table after
ANTI JOIN
Result after Anti join:
A left join keeps all of the original left_table right_table result after LEFT JOIN data type. UNION does not return duplicates. FROM album; id col1 col2 id col1
records in the left table and returns id left_val id right_val id left_val album_id title artist_id
missing values for any columns from
right_val
Result after UNION Result after UNION: 1 A B 1 A 5 Rumours 6
1 L1 1 R1 1 L1 R1
the right table where the joining field id val artist_id 2 B C 4 D
did not find a match.
2 L2 4 R2 2 L2 null left right 1
1 A
3 L3 5 R3 3 L3 null id val id val 2 3 C
1 B
4 L4 6 R4 4 L4 R2 1 A 1 A 3 4 D
2 A 6
1 B 4 A
Result after LEFT JOIN: 3 A
2 A 5 A
LEFT JOIN on one field artist_id name album_id title name 4 A
SELECT *
3 A 6 A
1 AC/DC 1 For those who rock 1 5 A
FROM artist AS art
1 AC/DC 4 Let there be rock 1 4 A 6 A
LEFT JOIN album AS alb
ON art.artist_id = alb.artist_id;
2
2
Aerosmith
Aerosmith
2
3
Dream on
Restless and wild
2
2 Learn Data Skills Online at www.DataCamp.com
3 Alanis Morissette null null null