Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

SQL CheatSheet

Uploaded by

pushpakrk15
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)
24 views

SQL CheatSheet

Uploaded by

pushpakrk15
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 RI GHT JOIN UNION ALL

Cheat Sheet
A right join keeps all of the original _
left table _
right table result after RI G HT 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

SQL online at www.DataCamp.com


the left table where the joining field
Learn 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 RI G T JOIN:
H
id val id val
1 A
artist_id

RIG T JOIN on one field


H
artist_id name b
al um_id title name
1 A 1 A
1 B
1

2
SELECT *

1 B 4 A

Primary key:
Foreign key:

1 A C/DC 1 For those who rock 1 2 A


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

each record in the table. In relational databases, primary primary key of another table. In a relational database,
RI GHT JOIN album AS alb
2 Aerosmith 3 Restless and wild 2 3 A 6 A
4 A 2
ON art.artist_id = alb.artist_id; 2 A C/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

FULL JOIN
6
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
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

id _
left val
JOIN
right_val
INT R E SECT
one table is uniquely related to exactly one record in the related to one record in the first table.
id _
left val id right_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,


Many-to-many relationship:
3 L3 null
left table _ _
right table
INTERSECT

returning null values accordingly. 3 L3 5 R3


‘ ’
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
Result after

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

TERSECT:
Sample Data
1 N1 4 R2 Result after IN
Result after FULL JOIN: 1 N1

> FULL JOIN on one field artist_id name b


al um_id title name 3 L3 5 R3
artist_id

1
C/DC For those who rock
SELECT *
1 A 1 1
4 L4 6 R4 2
Artist Table Al um b Table 1 AC/DC 4 Let there be rock 1
FROM artist as art

artist_id name b
al um_id title artist_id
JOIN album AS alb
2 Aerosmith 2 Balls to the wall 2

EXCEPT
FULL OUTER
C/DC For those who rock art.artist_id = alb.artist_id;
2 Aerosmith 3 Restless and wild 2
1 A 1 1
ON
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. FROM artist

5 Rumours 6
EXCEPT

SELECT artist_id

INN R E JOIN CROSS JOIN creates all possible combinations of two


tables. CROSS JOIN does not require a field to join ON.
SELECT

FROM artist

name, titl e
left table

id
_
val id
_
right table

val
Result after

EXCEPT
FROM album;

An inner join between two tables will _


left table _
right table CROSS JOIN album; Result after E CEPT:
X

1 N1 1 N1 id val
return only records where a joining id left val_ id right_val artist_id

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 3
both tables.
id left val _ right_val name title
id1 id2 3 L3 5 R3 4 L4
2 L2 4 R2 AC/DC For those who rock
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

SEMI JOIN
1 A
1 C AC/DC Rumours
E JOIN join ON one field
INN R 2 B
Aerosmith For those who rock
2 A
SELECT *

C
FROM artist AS art

3 Aerosmith Dream on
A semi join chooses records in the first table where a SELECT *

Result after INN R E JOIN: 2 B


Aerosmith Restless and wild
m

INNER JOIN album AS alb

condition is met in the second table. A semi join makes FROM albu
C
ON art.artist_id = alb.artist_id;
artist_id name title al bum _id 2 Aerosmith Let there be rock
use of a WHERE clause to use the second table as a filter WHERE artist_i d IN

1 AC/DC For those who rock 1 3 A


Aerosmith

Morissette
Rumours

For those who rock


for the first. (SELECT artist_id

E JOIN with USING


INN R
1 A C/DC Let there be rock 4
3 B
Alanis

Alanis Morissette
FROM artist ; )
Aerosmith Dream on Dream on Result after

SELECT *

2 2
Alanis Morissette _
left table _
right table SEMI JOIN
2 Aerosmith Restless and wild 3 3 C Restless and wild
Result after Semi join:
FROM artist AS art
Alanis Morissette Let there be rock
id col1 col2 id col1
INNER JOIN album AS al b
Alanis Morissette Rumours b
al um_id title artist_id

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

SELF JOIN Set Theory Operators in SQL 2 B C 3 C 2

4
Dream on

Let there be rock


2

1
3 C 3 Restless and wild 2

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

. d,
Self join:
alb1 artist_i N
E SECT EXCEPT
JOIN
Result after UNIO INT R

alb1.title AS alb1_titl e,
artist_id name al bum _id al b2_title UNION ALL ANTI
alb2.title AS alb2_title
1 AC/DC 1 For those who rock
FROM album AS alb1
Aerosmith 2 Dream on The anti join chooses records in the first table where a SELECT *

JOIN album AS alb2

2
UNION m

INNER 2 Aerosmith 3 Restless and wild condition is NOT met in the second table. It makes use of FROM albu

ON art1.artist_id = art2.artist_id
1 AC/DC 4 Let there be rock a WHERE clause to use exclude values from the second WHERE artist_i d N

NOT I

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 ; )
L EFT JOIN all SELECT statements must have the same number of
columns and corresponding columns must have the same
UNIO N

_
left table _
right table
Left table after

ANTI JOIN
SELECT artist_id

Result after Anti oin: j


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
b
al um_id title artist_id
records in the left table and returns id _
left val id right_val id _
left val right_val
Result after UNION Result after UNION: 1 A B 1 A 5 Rumours 6
missing values for any columns from
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 A 1
3 L3 5 R3 3 L3 null id val id val 2
3 C
1 B
4 L4 6 R4 4 L4 R2 3
1 A 1 A 4 D
2 A
6
1 B 4 A
Result after L F E T JOIN: 3 A

E T JOIN on one field


2 A 5 A
L F artist_id name b
al um_id title name
4 A

SELECT *

3 A 6 A
1 AC/DC 1 For those who rock 1 5 A

AC/DC
FROM artist AS art
4 A
1 4 Let there be rock 1
6 A
LEFT JOIN album AS alb
2 Aerosmith 2 Dream on 2
Learn Data Skills Online at www.DataCamp.com
ON art.artist_id = alb.artist_id; 2 Aerosmith 3 Restless and wild 2

3 Alanis Morissette null null null

You might also like