Download - SQL Bag 3 Join Tabel
![Page 1: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/1.jpg)
SQL (Structure SQL (Structure Query Language) – Query Language) –
MySQL MySQL (Bagian 3: Table Joins)(Bagian 3: Table Joins)
Oleh: Euis MarlinaOleh: Euis Marlina0817942431908179424319
http://euismarlina.edublogs.orghttp://euismarlina.edublogs.org
![Page 2: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/2.jpg)
Perintah SQL join tabel digunakan untuk menampilkan data yang diambil dari beberapa tabel yang saling berhubungan.
Kita dapat menampilkan beberapa kolom dari tabel 1, beberapa kolom dari tabel 2, dan beberapa kolom dari tabel n.
Hampir semua perintah pada materi DML di bagian 2 dapat digabungkan penggunaannya dalam join table ini.
![Page 3: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/3.jpg)
JOIN digunakan untuk menampilkan semua record dari sebuah tabel yang cocok maupun tidak cocok dengan record dari tabel lainnya.
JOIN digunakan ketika beberapa tabel diakses melalui sebuah statemen SQL yaitu SELECT.
JOIN membandingkan semua kolom pada kedua tabel yang memiliki kesamaan nama kolom. Hasilnya adalah hanya menampilkan satu kolom untuk setiap pasang kolom yang namanya sama.
![Page 4: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/4.jpg)
table_reference JOIN table_factor WHERE | ON [join_condition]
table_reference [INNER | CROSS] JOIN table_factor WHERE [join_condition]
table_reference STRAIGHT_JOIN table_factor
![Page 5: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/5.jpg)
table_reference STRAIGHT_JOIN table_factor ON conditional_expr
table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference ON join_condition
table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor WHERE join_condition
![Page 6: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/6.jpg)
This type of join is the simplest join. The cross join result in cartesian product of all the records from two tables.
Jika pada tabel pertama ada 4 record, dan pada tabel kedua juga ada 4 record, maka jumlah record hasil query akan berjumlah 16 (hasil dari 4 x 4 =16)
Syntax:table_reference CROSS JOIN table_factor | WHERE [join_condition]
![Page 7: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/7.jpg)
This is the type of join where tables are combined based on a common column.
INNER JOIN memerlukan kriteria kolom tertentu sebagai penghubung kedua tabel.
Syntax:table_reference INNER JOIN table_factor WHERE [join_condition]
In MySQL, CROSS JOIN is a syntactic equivalent to INNER JOIN (they can replace each other).
![Page 8: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/8.jpg)
Menampilkan record dari tabel sebelah kiri yang cocok maupun yang tidak cocok dengan tabel sebelah kanan.
Record yang tidak cocok, akan ditampilkan dengan nilai NULL.
Syntax:table_reference LEFT [OUTER] JOIN table_reference ON join_condition
![Page 9: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/9.jpg)
Menampilkan record yang cocok dengan tabel sebelah kanan.
Syntax:table_reference RIGHT [OUTER] JOIN table_reference ON join_condition
![Page 10: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/10.jpg)
Table JoinsSelain beberapa jenis join di atas, anda juga bisa menggunakan konsep join tabel yang sangat sederhana yaitu dengan cara:
Jika ingin menampilkan semua field SELECT * from table_1, table2;
Jika ingin menampilkan field tertentu SELECT table_1.field_1, table_2.field_2, table_2.field_3 from table_1, table2;
![Page 11: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/11.jpg)
ContohContoh pada database mysql:SELECT * from help_relation,
help_keyword; atau
SELECT * from help_relation JOIN help_keyword;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword,help_relation WHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;
![Page 12: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/12.jpg)
INNER/CROSS JOINSELECT * FROM help_keyword INNER JOIN help_relation;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword INNER JOIN help_relationWHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;
SELECT * FROM help_keyword CROSS JOIN help_relation;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword CROSS JOIN help_relationWHERE help_keyword.help_keyword_id=help_relation.help_keyword_id;
![Page 13: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/13.jpg)
STRAIGHT_JOIN
SELECT * FROM help_keyword STRAIGHT_JOIN help_relation;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword STRAIGHT_JOIN help_relation ON
help_keyword.help_keyword_id=help_relation.help_keyword_id;
![Page 14: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/14.jpg)
LEFT/RIGHT [OUTER] JOINSELECT help_relation.help_keyword_id,
help_keyword.name from help_keyword LEFT JOIN help_relation ON
help_keyword.help_keyword_id=help_relation.help_keyword_id;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword RIGHT OUTER JOIN help_relation ON
help_keyword.help_keyword_id=help_relation.help_keyword_id;
![Page 15: SQL Bag 3 Join Tabel](https://reader033.vdocuments.pub/reader033/viewer/2022061118/5469f766b4af9fd27e8b4db5/html5/thumbnails/15.jpg)
NATURAL LEFT/RIGHT [OUTER] JOIN
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword NATURAL RIGHT JOIN help_relation WHERE
help_keyword.help_keyword_id=help_relation.help_keyword_id;
SELECT help_relation.help_keyword_id, help_keyword.name from help_keyword NATURAL LEFT OUTER JOIN help_relation WHERE
help_keyword.help_keyword_id=help_relation.help_keyword_id;