SQL JOIN. • SQL tidak hanya menyediakan mekanisme query dan operasi
modifikasi database saja, tetapi SQL juga menyediakan mekanisme untuk ...
SQL OPERASI JOIN
Teori Himpunan Inner Join; Outer Join
Basdat 2012_Mrs Imana's Materi versi 2
1
SQL JOIN •
SQL tidak hanya menyediakan mekanisme query dan operasi modifikasi database saja, tetapi SQL juga menyediakan mekanisme untuk menggabungkan(join) dan relasi‐relasi:
•
Saat data yang dibutuhkan berasal lebih dari satu table, maka kondisi join di butuhkan.
•
Umumnya dalam men‐jointable berdasarkan pada kolom yang bersesuaian Primary Keydari table‐1 dengan Foreign Key dari table‐2, atau yang disebut dengan join atau equi‐join.
•
Kondisi Join meliputi: ‐Equijoin(Inner Join atauSimple Join)‐Non‐Equijoin‐Outer Join (Left Outer Join & Right Outer Join)‐Self Join Basdat 2012_Mrs Imana's Materi versi 2
2
Syntax Join SQL JOIN / EQUI‐JOIN: Syntax Join SQL JOIN / EQUI JOIN: • SELECTtable1 SELECTtable1.column, column table2.columnFROMtable1, table2WHEREtable1 column1 table2WHEREtable1.column1 =table2.column2;SELECTtable1.column, table2 columnFROMtable1 table2.columnFROMtable1, table2WHEREtable1.column1 =table2.colum
Basdat 2012_Mrs Imana's Materi versi 2
3
Contoh Contoh Pemakaian JOIN Pemakaian JOIN •
•
•
SELECT Orders.OrderID Orders.OrderID, , Orders.CustomerID, Orders.CustomerID, Customers.ContactName, Customers.ContactName , Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID SELECT Orders.OrderID Orders.OrderID, , Orders.CustomerID Orders.CustomerID, , Customers.ContactName, Customers.ContactName , Orders.OrderDate FROM Orders LEFT JOIN Customers ON Orders.CustomerID O d C t ID = Customers.CustomerID C t C t ID SELECT Orders.OrderID Orders.OrderID, , Orders.CustomerID, Orders.CustomerID, Customers.ContactName, Customers.ContactName , Orders.OrderDate FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID
Basdat 2012_Mrs Imana's Materi versi 2
4
Contoh Pemakaian JOIN (Lanjutan) Contoh Pemakaian JOIN (Lanjutan) •
SELECT Orders.OrderID Orders.OrderID, , Orders.OrderDate, Orders.OrderDate, Customers.ContactName, Customers.ContactName , Products.ProductName, Products.ProductName, [Order Detail].UnitPrice Detail]. UnitPrice, , [Order Detail].Quantity FROM Customers INNER JOIN Orders O d ON Customers.CustomerID = Orders.CustomerID INNER JOIN [Order Detail] ON Orders.OrderID O d O d ID = [Order [O d D Detail]. Detail].OrderID t il] OrderID O d ID INNER JOIN Products ON [Order Detail].ProductID Detail].ProductID = Products.ProductID
Basdat 2012_Mrs Imana's Materi versi 2
5
Contoh Pemakaian JOIN (Lanjutan) Contoh Pemakaian JOIN (Lanjutan) •
SELECT Orders.OrderID Orders.OrderID, , Customers.ContactName, Customers.ContactName, Sum([Order Detail].UnitPrice Detail]. UnitPrice), ), Sum([Order Detail].Quantity) FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN [Order Detail] ON Orders.OrderID = [Order Detail].OrderID Detail].OrderID GROUP BY Orders.OrderID Orders.OrderID, , Customers.ContactName
Basdat 2012_Mrs Imana's Materi versi 2
6
Syntax UNION Syntax UNION < select statement > UNION [ ALL ] < select statement > [ UNION [ ALL ] < select statement > [ … ] ]
Basdat 2012_Mrs Imana's Materi versi 2
7
Contoh UNION Contoh UNION • •
SELECT * FROM Customers WHERE Country = ‘Indonesia’ OR Country = ‘Malaysia’ SELECT * FROM Customers WHERE Country = ‘Indonesia’ UNION SELECT * FROM Customers WHERE Country = ‘Malaysia’
Basdat 2012_Mrs Imana's Materi versi 2
8
Syntax VIEW Syntax VIEW •
CREATE VIEW view_name AS < select statement >
•
ALTER VIEW view_name AS < select statement >
•
DROP VIEW view_name
Basdat 2012_Mrs Imana's Materi versi 2
9
Contoh Contoh CREATE VIEW CREATE VIEW •
•
CREATE VIEW Tabel1 AS SELECT Orders.OrderID Orders.OrderID, , Orders.CustomerID Orders.CustomerID, , Customers.ContactName, Customers.ContactName , Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID Orders CustomerID = Customers.CustomerID Customers CustomerID CREATE VIEW Tabel2 AS SELECT Orders.OrderID Orders.OrderID, , Customers.ContactName, Customers.ContactName, Products.ProductName, Products.ProductName , [Order Detail].Quantity FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN [Order Detail] ON Orders.OrderID = [Order Detail].OrderID Detail]. i OrderID INNER JOIN Products ON [Order Detail].ProductID Detail].ProductID = Products.ProductID Basdat 2012_Mrs Imana's Materi versi 2
10
Contoh CREATE VIEW (Lanjutan) Contoh CREATE VIEW (Lanjutan) •
CREATE VIEW Tabel Tabel3 3 AS SELECT Orders.OrderID, Customers.ContactName, Sum([Order Detail].Quantity) AS [Jlh Barang], Sum([Order Detail].UnitPrice * [Order Detail].Quantity * (1 [Order Detail] Detail].Discount)) Discount)) AS [Hrg Total] FROM Customers INNER JOIN Orders ON Customers.CustomerID Customers CustomerID = Orders Orders.CustomerID CustomerID INNER JOIN [Order Detail] ON Orders.OrderID = [Order Detail].OrderID GROUP BY Orders.OrderID, Orders OrderID Customers Customers.ContactName ContactName
Basdat 2012_Mrs Imana's Materi versi 2
11
Tugas Proyek Tugas Proyek Buat sejumlah query dari proyek anda,, kemudian jelaskan tujuan & anda manfaat f t dari d i query yang anda d buat b buat. t.
Basdat 2012_Mrs Imana's Materi versi 2
12