Understanding Basic SQL Join Types in MySQL: A Beginner's Guide

This story explains the basic understanding on the SQL join types in MYSQL. It is providing insights on inner, left, right and outer joins.

Database
March 09, 2024

Share:

SQL is the structured query language which is the programming language to interact (communicate and manage) with the relational database.

To retrieve the rows of multiple tables of data we need to use the join clauses in the SQL query. There are these major join types in MYSQL, These are as followings:

  1. INNER JOIN
  2. LEFT JOIN
  3. RIGHT JOIN

To provide the practical examples on the join types let's create following tables.

store

manager_staff_id store_id address_id last_update
1 1 1 2006-02-15 04:57:12
2 2 2 2006-02-15 04:57:12

address

address_id address address2 district city_id postal_code phone location last_update
1 47 MySakila Drive NULL Alberta 300 NULL NULL [GEOMETRY - 25 B] 2014-09-25 22:30:27
2 28 MySQL Boulevard NULL QLD 576 NULL NULL [GEOMETRY - 25 B] 2014-09-25 22:30:09
3 23 Workhaven Lane NULL Alberta 300 14033335568 NULL [GEOMETRY - 25 B] 2014-09-25 22:30:27
4 1411 Lillydale Drive NULL QLD 576 6172235589 NULL [GEOMETRY - 25 B] 2014-09-25 22:30:09
5 1913 Hanoi Way Nagasaki 463 35200 28303384290 [GEOMETRY - 25 B] 2014-09-25 22:31:53
6 1121 Loja Avenue California 449 17886 838635286649 [GEOMETRY - 25 B] 2014-09-25 22:34:01
7 692 Joliet Street Attika 38 83579 448477190408 [GEOMETRY - 25 B] 2014-09-25 22:31:07
8 1566 Inegöl Manor Mandalay 349 53561 705814003527 [GEOMETRY - 25 B] 2014-09-25 22:32:18
9 53 Idfu Parkway Nantou 361 42399 10655648674 [GEOMETRY - 25 B] 2014-09-25 22:33:16
10 1795 Santiago de Compostela Way Texas 295 18743 860452626434 [GEOMETRY - 25 B] 2014-09-25 22:33:55
11 900 Santiago de Compostela Parkway Central Serbia 280 93896 716571220373 [GEOMETRY - 25 B] 2014-09-25 22:34:11
12 478 Joliet Way Hamilton 200 77948 657282285970 [GEOMETRY - 25 B] 2014-09-25 22:32:22
13 613 Korolev Drive Masqat 329 45844 380657522649 [GEOMETRY - 25 B] 2014-09-25 22:32:29
14 1531 Salé Drive Esfahan 162 53628 648856936185 [GEOMETRY - 25 B] 2014-09-25 22:31:36
15 1542 Tarlac Parkway Kanagawa 440 1027 635297277345 [GEOMETRY - 25 B] 2014-09-25 22:31:53
16 808 Bhopal Manor Haryana 582 10672 465887807014 [GEOMETRY - 25 B] 2014-09-25 22:31:30
17 270 Amroha Parkway Osmaniye 384 29610 695479687538 [GEOMETRY - 25 B] 2014-09-25 22:33:27
18 770 Bydgoszcz Avenue California 120 16266 517338314235 [GEOMETRY - 25 B] 2014-09-25 22:33:47
19 419 Iligan Lane Madhya Pradesh 76 72878 990911107354 [GEOMETRY - 25 B] 2014-09-25 22:31:13
20 360 Toulouse Parkway England 495 54308 949312333307 [GEOMETRY - 25 B] 2014-09-25 22:33:40
21 270 Toulon Boulevard Kalmykia 156 81766 407752414682 [GEOMETRY - 25 B] 2014-09-25 22:32:48
22 320 Brest Avenue Kaduna 252 43331 747791594069 [GEOMETRY - 25 B] 2014-09-25 22:32:25
23 1417 Lancaster Avenue Northern Cape 267 72192 272572357893 [GEOMETRY - 25 B] 2014-09-25 22:33:03
24 1688 Okara Way Nothwest Border Prov 327 21954 144453869132 [GEOMETRY - 25 B] 2014-09-25 22:32:30
25 262 A Coruña (La Coruña) Parkway Dhaka 525 34418 892775750063 [GEOMETRY - 25 B] 2014-09-25 22:30:12
  1. INNER JOIN

JOIN and INNER JOIN are identical in MYSQL. While joining the table if both table contains the match records from the both tables then only those information it returns from the query.

For e.g.

SELECT * 
FROM store
JOIN address ON store.address_id=address.address_id;

This will return the following:

manager_staff_id address_id last_update address_id address address2 district city_id postal_code phone location store_id last_update
1 1 2006-02-15 04:57:12 1 47 MySakila Drive NULL Alberta 300 NULL NULL POINT(-112.8185647 49.6999986) 1 2014-09-25 22:30:27
2 2 2006-02-15 04:57:12 2 28 MySQL Boulevard NULL QLD 576 NULL NULL POINT(153.1408538 -27.6333361) 2 2014-09-25 22:30:09
  1. LEFT JOIN

LEFT JOIN and LEFT OUTER JOIN are identical in MYSQL. This join gives the match result and if there is not match then those records will be available from the left table and related null values from the right table.

For e.g.

SELECT * 
FROM store
LEFT JOIN address ON store.address_id=address.address_id;

This will return the following:

manager_staff_id address_id last_update address_id address address2 district city_id postal_code phone location store_id last_update
1 1 2006-02-15 04:57:12 1 47 MySakila Drive NULL Alberta 300 NULL NULL POINT(-112.8185647 49.6999986) 1 2014-09-25 22:30:27
2 2 2006-02-15 04:57:12 2 28 MySQL Boulevard NULL QLD 576 NULL NULL POINT(153.1408538 -27.6333361) 2 2014-09-25 22:30:09
  1. RIGHT JOIN

RIGHT JOIN and RIGHT OUTER JOIN are identical in MYSQL. While using this RIGHT JOIN this will return the rows from the table which are matched rows and for not matched values it return the right table rows by providing null values to left tables rows values.

For e.g:

SELECT * 
FROM store
RIGHT JOIN address ON store.address_id=address.address_id;

This will return the following results:

manager_staff_id address_id last_update address_id address address2 district city_id postal_code phone location store_id last_update
1 1 2006-02-15 04:57:12 1 47 MySakila Drive NULL Alberta 300 POINT(-112.8185647 49.6999986) 1 2014-09-25 22:30:27
2 2 2006-02-15 04:57:12 2 28 MySQL Boulevard NULL QLD 576 POINT(153.1408538 -27.6333361) 2 2014-09-25 22:30:09
NULL NULL NULL 3 23 Workhaven Lane NULL Alberta 300 14033335568 POINT(-112.8185673 49.6999951) NULL 2014-09-25 22:30:27
NULL NULL NULL 4 1411 Lillydale Drive NULL QLD 576 6172235589 POINT(153.1913094 -27.6333373) NULL 2014-09-25 22:30:09
NULL NULL NULL 5 1913 Hanoi Way Nagasaki 463 35200 28303384290 POINT(129.7227851 33.1591726) NULL 2014-09-25 22:31:53
NULL NULL NULL 6 1121 Loja Avenue California 449 17886 838635286649 POINT(-117.2897714 34.1083428) NULL 2014-09-25 22:34:01
NULL NULL NULL 7 692 Joliet Street Attika 38 83579 448477190408 POINT(23.7162294 37.9794508) NULL 2014-09-25 22:31:07
NULL NULL NULL 8 1566 Inegöl Manor Mandalay 349 53561 705814003527 POINT(95.3884368 21.4600272) NULL 2014-09-25 22:32:18
NULL NULL NULL 9 53 Idfu Parkway Nantou 361 42399 10655648674 POINT(120.6638785 23.9156685) NULL 2014-09-25 22:33:16
NULL NULL NULL 10 1795 Santiago de Compostela Way Texas 295 18743 860452626434 POINT(-99.5075448 27.5064117) NULL 2014-09-25 22:33:55
NULL NULL NULL 11 900 Santiago de Compostela Parkway Central Serbia 280 93896 716571220373 POINT(0 0) NULL 2014-09-25 22:34:11
NULL NULL NULL 12 478 Joliet Way Hamilton 200 77948 657282285970 POINT(175.2833399 -37.7833313) NULL 2014-09-25 22:32:22
NULL NULL NULL 13 613 Korolev Drive Masqat 329 45844 380657522649 POINT(58.5922971 23.6138798) NULL 2014-09-25 22:32:29
NULL NULL NULL 14 1531 Salé Drive Esfahan 162 53628 648856936185 POINT(51.6746246 32.6524674) NULL 2014-09-25 22:31:36
NULL NULL NULL 15 1542 Tarlac Parkway Kanagawa 440 1027 635297277345 POINT(139.3589644 35.5557889) NULL 2014-09-25 22:31:53
NULL NULL NULL 16 808 Bhopal Manor Haryana 582 10672 465887807014 POINT(0 0) NULL 2014-09-25 22:31:30
NULL NULL NULL 17 270 Amroha Parkway Osmaniye 384 29610 695479687538 POINT(36.2477824 37.0741756) NULL 2014-09-25 22:33:27
NULL NULL NULL 18 770 Bydgoszcz Avenue California 120 16266 517338314235 POINT(-121.2810649 38.7071296) NULL 2014-09-25 22:33:47
NULL NULL NULL 19 419 Iligan Lane Madhya Pradesh 76 72878 990911107354 POINT(77.4028938 23.2546953) NULL 2014-09-25 22:31:13
NULL NULL NULL 20 360 Toulouse Parkway England 495 54308 949312333307 POINT(0.7143363 51.5378203) NULL 2014-09-25 22:33:40
NULL NULL NULL 21 270 Toulon Boulevard Kalmykia 156 81766 407752414682 POINT(44.2558374 46.3077803) NULL 2014-09-25 22:32:48
NULL NULL NULL 22 320 Brest Avenue Kaduna 252 43331 747791594069 POINT(7.4382858 10.5222412) NULL 2014-09-25 22:32:25
NULL NULL NULL 23 1417 Lancaster Avenue Northern Cape 267 72192 272572357893 POINT(24.7623254 -28.7322674) NULL 2014-09-25 22:33:03
NULL NULL NULL 24 1688 Okara Way Nothwest Border Prov 327 21954 144453869132 POINT(0 0) NULL 2014-09-25 22:32:30
NULL NULL NULL 25 262 A Coruña (La Coruña) Parkway Dhaka 525 34418 892775750063 POINT(89.9165584 24.2498482) NULL 2014-09-25 22:30:12

So the above are some of the basic join types in mysql.

2024 © Madhu Sudhan Subedi.