Apa itu Intersect?Query Intersect adalah membandingkan hasil dua query antara query A dan query B dan hanya menampilkan record yang sama hasil dari kedua query tersebut. Show Query Intersect digambarkan sebagai berikut: Sintak DasarDi bawah ini adalah sintak dasar SQL Query menggunakan Intersect clause pada Oracle SQL: SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition] INTERSECT SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition] ContohAsumsikan Anda telah mempunyai dua tabel sebagai contoh pada tutorial kali ini, yaitu tabel Customer dan tabel Orders dengan data-data sebagai berikut: Tabel Customer SQL> SELECT * FROM customer; CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS ----------- ----------------- ----------------- CS001 Nursalim Bandung CS002 Nani Indriyani Jakarta CS003 Naura Krasiva Surabaya CS004 Ahmad Fatoni Semarang CS005 Sri Rahayu Yogyakarta Tabel Orders SQL> SELECT * FROM orders; ORDER_ID ORDER_DATE CUSTOMER_ID QTY AMOUNT -------- ---------- ----------- ----- ------- 00001 01-04-2015 CS001 2 10000 00002 08-04-2015 CS001 3 7000 00003 14-04-2015 CS004 1 15000 Di bawah ini adalah contoh Query Intersect menggunakan kedua tabel diatas: SQL> SELECT customer.customer_id, customer.customer_name, orders.order_id, orders.order_date, orders.qty, orders.amount 2 FROM customer 3 LEFT JOIN orders 4 ON customer.customer_id = orders.customer_id 5 INTERSECT 6 SELECT customer.customer_id, customer.customer_name, orders.order_id, orders.order_date, orders.qty, orders.amount 7 FROM customer 8 RIGHT JOIN orders 9 ON customer.customer_id = orders.customer_id; CUSTOMER_ID CUSTOMER_NAME ORDER_ID ORDER_DATE QTY AMOUNT ----------- ----------------- -------- ---------- ----- -------- CS001 Nursalim 00001 01-04-2015 2 10000 CS001 Nursalim 00002 08-04-2015 3 7000 CS004 Ahmad Fatoni 00003 14-04-2015 1 15000 About UnknownThis is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel. Summary: in this tutorial, we will introduce you to the Note that MySQL does not support the Introduction to the INTERSECT operatorThe The following illustrates the syntax of the
The To use the
The following diagram
illustrates the The left query produces a result set of (1,2,3). The right query returns a result set of (2,3,4). The Unlike the Note that the SQL standard has
three set operators that include Emulating INTERSECT in MySQLUnfortunately, MySQL does not support the Setting up sample tablesThe following statements create tables
The following query returns rows from the
id ---- 1 2 3 The following query returns the rows from the
id --- 2 3 4 1) Emulate INTERSECT using DISTINCT and INNER JOIN clauseThe following statement uses
id ---- 2 3 How it works.
2) Emulate INTERSECT using IN and subqueryThe following statement uses the
id ---- 2 3 How it works.
In this tutorial, you have learned a couple of ways to simulate the
Was this tutorial helpful? |