Understanding SQL Joins

A SQL Join statement is used to combine data or rows from two or more tables based on a common field between them. There are mainly 4 types of SQL joins.
 
Pernyataan SQL Join digunakan untuk menggabungkan data atau baris dari dua atau lebih tabel berdasarkan bidang umum di antara mereka. Ada 4 jenis SQL joins.
 
The database tables which i'm using for this post are;

Tabel database yang saya gunakan untuk postingan ini adalah : 
 

INNER JOIN :

This type of join returns those records which have matching values in both tables. This keyword will create the result-set by combining all rows from both the tables where the condition satisfies i.e value of the common field will be same.
 
Jenis gabungan ini mengembalikan catatan yang memiliki nilai yang cocok di kedua tabel. Kata kunci ini akan membuat kumpulan hasil dengan menggabungkan semua baris dari kedua tabel di mana kondisinya memenuhi yaitu nilai bidang umum akan sama.
 
Syntax :

matching_column is the Column common to both the tables (Foreign Key).
 
matching_column adalah kolom umum untuk kedua tabel (Foreign Key)

Example for INNER JOIN :

SQL query to show the names and age of students enrolled in different courses.
 
SQL query untuk menampilkan nama dan usia siswa yang terdaftar di kursus yang berbeda.
 

Output :


 

LEFT JOIN :

The LEFT JOIN or the LEFT OUTER JOIN returns all the records from the left table and also those records which satisfy a condition from the right table. Also, for the records having no matching values in the right table, the output or the result-set will contain the NULL values.
 
LEFT JOIN atau LEFT OUTER JOIN mengembalikan semua record dari tabel kiri dan juga record yang memenuhi kondisi dari tabel kanan. Juga, untuk catatan yang tidak memiliki nilai yang cocok di tabel kanan, output atau kumpulan hasil akan berisi nilai NULL.
 
Syntax :
 
Example for LEFT JOIN:
 


RIGHT JOIN :

The RIGHT JOIN or the RIGHT OUTER JOIN returns all the records from the right table and also those records which satisfy a condition from the left table. Also, for the records having no matching values in the left table, the output or the result-set will contain the NULL values.
 
The RIGHT JOIN atau RIGHT OUTER JOIN mengembalikan semua record dari tabel kanan dan juga record yang memenuhi kondisi dari tabel kiri. Juga, untuk catatan yang tidak memiliki nilai yang cocok di tabel kiri, output atau kumpulan hasil akan berisi nilai NULL. 
 
Syntax :

Example for RIGHT JOIN:
 

Output :


 

FULL JOIN :

FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows for which these is no matching, the result-set will contain NULL values.
 
FULL JOIN membuat kumpulan hasil dengan menggabungkan hasil dari LEFT JOIN dan RIGHT JOIN. Hasil set akan berisi semua baris dari kedua tabel. Baris yang tidak cocok dengan ini, kumpulan hasil akan berisi nilai NULL.
 
Syntax :
 
Note: IN Mysql FULL JOIN is not applicable so to the full join operation we can UNION between Left and Right join.
 
CATATAN : di Mysql FULL JOIN tidak berlaku sehingga untuk operasi full join kita bisa pakai UNION antara Left dan Right join.
 
Example for FULL JOIN:
 

Output :