{"id":98914,"date":"2021-01-12T11:00:00","date_gmt":"2021-01-12T09:00:00","guid":{"rendered":"https:\/\/examples.javacodegeeks.com\/?p=98914"},"modified":"2021-02-19T15:42:42","modified_gmt":"2021-02-19T13:42:42","slug":"sql-intersect-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/","title":{"rendered":"SQL Intersect Example"},"content":{"rendered":"<h2 class=\"wp-block-heading\" id=\"h-1-introduction\">1. Introduction<\/h2>\n<p>In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL Server, and PostgreSQL support the Intersect operator. However, MySQL does not. In this article, we will understand what the Intersect operator is and how to emulate it in MySQL.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-2-what-does-set-theory-mean\">2. What does Set Theory mean?<\/h2>\n<p>In SQL, an entity that can hold a collection of objects is called a \u201cset\u201d. Hence in SQL all \u201cRelations\u201d i.e. tables are sets. Set Theory is the set of operations in which objects from multiple sets merge to form a single result. In other words, the set operators combine multiple select queries to give a single result.<\/p>\n<p>The SQL Set operators are UNION, INTERSECT, and EXCEPT. Out of these operators, MySQL supports only UNION. It does not support the Intersect and Except keywords. However, MySQL emulates these operations using Joins and\/or subqueries.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-3-what-is-intersect-operator\">3. What is Intersect Operator?<\/h2>\n<p>The Intersect operator is the intersection of 2 or more datasets. In Relational Algebra, the intersection is shown with \ua4f5 symbol. The Venn diagram representation of the intersect operator is as follows<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/intersect_venn_diagram.jpg\"><img decoding=\"async\" width=\"699\" height=\"401\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/intersect_venn_diagram.jpg\" alt=\"sql Intersect operator Venn Diagram\" class=\"wp-image-98915\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/intersect_venn_diagram.jpg 699w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/intersect_venn_diagram-300x172.jpg 300w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/a><figcaption>Intersect operator Venn Diagram<\/figcaption><\/figure>\n<\/div>\n<p>In SQL, the basic syntax of the Intersect operator is as follows:<\/p>\n<pre class=\"brush:sql\">SELECT [column_names or *]\nFROM tables\n[WHERE conditions]\n\nINTERSECT\n\nSELECT [column_names or *]\nFROM tables\n[WHERE conditions];\n<\/pre>\n<p>As per MySQL Theory<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The intersect, except and Minus statements are not elementary since they can be composed with other commands. Thus, they are not implemented.<\/p>\n<p><cite>Unknown Source<\/cite><\/p><\/blockquote>\n<h2 class=\"wp-block-heading\" id=\"h-4-how-to-emulate-intersect-in-mysql\">4. How to Emulate Intersect in MySQL<\/h2>\n<p>To get the effect of the Intersect operator, we can use the following methods in MySQL RDBMS.<\/p>\n<ul class=\"wp-block-list\">\n<li>INNER JOIN: MySQL supports the INNER JOIN which gets the exact same result as the Intersect operator. Inner join can be implemented on two or more tables. The Basic syntax of the Inner join query is as follows<\/li>\n<\/ul>\n<pre class=\"brush:sql\">SELECT [column_names]\n FROM [table1]\nINNER JOIN table2 USING([column_name_common_to_both]);<\/pre>\n<p> <span> OR <\/span><\/p>\n<pre class=\"brush:sql\">SELECT [column_names]\n FROM [table1]\nINNER JOIN table2 ON [table1.column_name] = [table2.column_name];\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>IN and Sub Query: We can also use the In Operator and subqueries to get the same result as Intersect. The basic syntax of a IN operator is as follows<\/li>\n<\/ul>\n<pre class=\"brush:sql\">SELECT column_name [, column_name ]\nFROM   table1 [, table2 ]\nWHERE  column_name IN\n   (SELECT column_name [, column_name ]\n   FROM table1 [, table2 ]\n   [WHERE] conditions);\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>EXISTS: In Some cases we can also use the EXISTS operator to get the effect of an Intersect operator. The basic syntax of a EXISTS query is as follows:<\/li>\n<\/ul>\n<pre class=\"brush:sql\">SELECT column_name [, column_name ]\nFROM   table1 [, table2 ]\nWHERE EXISTS\n(SELECT column_name\n   FROM table1 \n   [WHERE condition])\n<\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-5-setup\">5. Setup<\/h2>\n<p>Forgoing through the examples related to the Intersect operator, we will consider the database called \u201cSakila\u201d. This is an example database given by MySQL. The schema structure is available on the <a href=\"https:\/\/dev.mysql.com\/doc\/sakila\/en\/sakila-structure.html\" target=\"_blank\" rel=\"noreferrer noopener\">documentation<\/a> page.<\/p>\n<p>For running the queries, we will use the MySQL Workbench. The documentation for MySQL Workbench is available <a href=\"https:\/\/dev.mysql.com\/doc\/workbench\/en\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-6-examples\">6. Examples<\/h2>\n<p>We will look at all examples of all the 3 ways in which we can emulate the Intersect operator in MySQL.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h3 class=\"wp-block-heading\" id=\"h-6-1-inner-join\">6.1 INNER JOIN<\/h3>\n<p>INNER JOIN is the simplest join supported by MySQL. It simply matches each record of each of the tables to each other and returns only the records that are present in all tables. It performs the exact same operation that the INTERSECT operator does. This is the JOIN that is used by Default and hence the INNER Keyword is optional.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-6-1-1-inner-join-2-tables-using-the-on-keyword\">6.1.1 INNER Join 2 tables using the ON Keyword<\/h4>\n<p>When we join two tables using a column name, we are using the inner join. Inner joins can be done using the ON keyword. Below given is an example of Inner Join<\/p>\n<pre class=\"brush:sql\">SELECT C.CATEGORY_ID,C.NAME,FC.FILM_ID,FC.LAST_UPDATE\nFROM CATEGORY C\nINNER JOIN FILM_CATEGORY FC\nON C.CATEGORY_ID = FC.CATEGORY_ID;\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_ON.jpg\"><img decoding=\"async\" width=\"700\" height=\"691\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_ON.jpg\" alt=\"SQL Intersect - Inner Join with ON keyword\" class=\"wp-image-98916\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_ON.jpg 700w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_ON-300x296.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_ON-70x70.jpg 70w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><figcaption>Inner Join with ON keyword<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-1-2-inner-join-2-tables-with-the-using-keyword\">6.1.2 INNER JOIN 2 tables with the USING Keyword<\/h4>\n<p>If the column names from the tables in the Join are the same, then we can use the USING keyword instead. For example<\/p>\n<pre class=\"brush:sql\">SELECT C.CATEGORY_ID,C.NAME,FC.FILM_ID,FC.LAST_UPDATE\nFROM CATEGORY C\nINNER JOIN FILM_CATEGORY FC\nUSING (CATEGORY_ID);\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_USING.jpg\"><img decoding=\"async\" width=\"651\" height=\"707\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_USING.jpg\" alt=\"SQL Intersect - Inner Join with USING keyword\" class=\"wp-image-98917\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_USING.jpg 651w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_2_tables_USING-276x300.jpg 276w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption>Inner Join with USING keyword<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-1-3-inner-join-3-tables-using-the-on-keyword\">6.1.3 INNER JOIN 3 tables using the ON Keyword<\/h4>\n<p>The inner join can intersect 3 tables also. The column names in each of the intersects can be different. We can do it using both the ON and the USING keyword. For example:<\/p>\n<pre class=\"brush:sql\">SELECT C.CATEGORY_ID,C.NAME,FC.FILM_ID,F.TITLE,\nF.DESCRIPTION,F.RATING,F.SPECIAL_FEATURES\nFROM CATEGORY C\nINNER JOIN FILM_CATEGORY FC\nON C.CATEGORY_ID = FC.CATEGORY_ID\nINNER JOIN FILM F\nON F.FILM_ID = FC.FILM_ID;\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_ON.jpg\"><img decoding=\"async\" width=\"810\" height=\"470\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_ON.jpg\" alt=\"Inner Join 3 tables with ON keyword\" class=\"wp-image-98918\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_ON.jpg 810w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_ON-300x174.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_ON-768x446.jpg 768w\" sizes=\"(max-width: 810px) 100vw, 810px\" \/><\/a><figcaption>Inner Join 3 tables with ON keyword<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-1-4-inner-join-3-tables-with-the-using-keyword\">6.1.4 INNER JOIN 3 tables with the USING Keyword.<\/h4>\n<p>We can join 3 tables with the USING keyword if the columns to be matched have the same column name in the joined tables.<\/p>\n<pre class=\"brush:sql\">SELECT C.CATEGORY_ID,C.NAME,FC.FILM_ID,F.TITLE,\nF.DESCRIPTION,F.RATING,F.SPECIAL_FEATURES\nFROM CATEGORY C\nINNER JOIN FILM_CATEGORY FC\nUSING (CATEGORY_ID)\nINNER JOIN FILM F\nUSING (FILM_ID);\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_USING.jpg\"><img decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_USING.jpg\" alt=\"Inner Join 3 tables with USING keyword\" class=\"wp-image-98919\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_USING.jpg 800w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_USING-300x188.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_3_tables_USING-768x480.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><figcaption>Inner Join 3 tables with USING keyword<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-1-5-inner-join-using-multiple-tables\">6.1.5 INNER JOIN using Multiple tables<\/h4>\n<p>We can also use Inner joins to join more than 3 tables. Since Mysql by default does Inner Join, the following queries are equivalent to each other.<\/p>\n<pre class=\"brush:sql\">SELECT F.FILM_ID,F.TITLE,F.DESCRIPTION,C.NAME,F.RENTAL_RATE,F.LENGTH,F.RATING,\nGROUP_CONCAT(CONCAT(A.FIRST_NAME,\" \",A.LAST_NAME) SEPARATOR ', ') AS \"ACTORS\"\nFROM CATEGORY C\nINNER JOIN FILM_CATEGORY FC ON C.CATEGORY_ID = FC.CATEGORY_ID\nINNER JOIN FILM F ON FC.FILM_ID = F.FILM_ID\nINNER JOIN FILM_ACTOR FA ON F.FILM_ID = FA.FILM_ID\nINNER JOIN ACTOR A ON FA.ACTOR_ID = A.ACTOR_ID\nGROUP BY F.TITLE\nORDER BY C.NAME,F.LENGTH DESC,F.TITLE ASC;\n<\/pre>\n<p><span>#Equivalent to above Query<\/span><\/p>\n<pre class=\"brush:sql\">SELECT F.FILM_ID,F.TITLE,F.DESCRIPTION,C.NAME,F.RENTAL_RATE,F.LENGTH,F.RATING,\nGROUP_CONCAT(CONCAT(A.FIRST_NAME,\" \",A.LAST_NAME) SEPARATOR ', ') AS \"ACTORS\"\nFROM CATEGORY C, FILM_CATEGORY FC,FILM F, FILM_ACTOR FA,ACTOR A\nWHERE C.CATEGORY_ID = FC.CATEGORY_ID\nAND FC.FILM_ID = F.FILM_ID\nAND F.FILM_ID = FA.FILM_ID\nAND FA.ACTOR_ID = A.ACTOR_ID\nGROUP BY F.TITLE\nORDER BY C.NAME,F.LENGTH DESC,F.TITLE ASC; <\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_multiple_tables.jpg\"><img decoding=\"async\" width=\"800\" height=\"382\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_multiple_tables.jpg\" alt=\"Inner Join Multiple tables\" class=\"wp-image-98920\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_multiple_tables.jpg 800w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_multiple_tables-300x143.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/inner_join_multiple_tables-768x367.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><figcaption>Inner Join Multiple tables<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"h-6-2-in-with-a-subquery\">6.2 In with a Subquery<\/h3>\n<p>The In operator also does the job of the intersect operator. The Subquery used with the IN, returns one or more records. The Query using the IN then matches the column with every record returned and the result contains only the records present in both.<\/p>\n<h4 class=\"wp-block-heading\" id=\"h-6-2-1-simple-in-with-a-subquery\">6.2.1 Simple IN with a subquery<\/h4>\n<p>The In query can be used with a simple subquery as in the below example<\/p>\n<pre class=\"brush:sql\">SELECT *\nFROM FILM_CATEGORY \nWHERE CATEGORY_ID IN (\nSELECT CATEGORY_ID FROM CATEGORY);\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Simple_IN.jpg\"><img decoding=\"async\" width=\"580\" height=\"690\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Simple_IN.jpg\" alt=\"Simple IN Query\" class=\"wp-image-98921\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Simple_IN.jpg 580w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Simple_IN-252x300.jpg 252w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><figcaption>Simple IN Query<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-2-2-using-in-with-inner-join\">6.2.2 Using IN with INNER JOIN<\/h4>\n<p>Inner joins and IN can be combined with each other and it gives the same effect as multiple Intersect operator queries.<\/p>\n<pre class=\"brush:sql\">SELECT FILM_ID,TITLE,DESCRIPTION,RATING \nFROM FILM\nINNER JOIN FILM_CATEGORY USING (FILM_ID) \nWHERE CATEGORY_ID IN (\nSELECT CATEGORY_ID FROM CATEGORY);\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/IN_AND_INNER_JOIN.jpg\"><img decoding=\"async\" width=\"797\" height=\"695\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/IN_AND_INNER_JOIN.jpg\" alt=\"In and INNER JOIN combination\" class=\"wp-image-98922\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/IN_AND_INNER_JOIN.jpg 797w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/IN_AND_INNER_JOIN-300x262.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/IN_AND_INNER_JOIN-768x670.jpg 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/a><figcaption>In and INNER JOIN combination<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\" id=\"h-6-2-3-nested-in\">6.2.3 Nested IN<\/h4>\n<p>Subqueries with the IN operator can be nested as well. Writing queries using this method is discouraged since there are much easier and more cost-effective ways of achieving the same effect. However, the below given query is valid. This is equivalent to writing multiple Intersect operators.<\/p>\n<pre class=\"brush:sql\">SELECT SUM(AMOUNT) AS \"TOTAL SALES\"\nFROM PAYMENT\nWHERE RENTAL_ID IN (SELECT RENTAL_ID FROM RENTAL\n       WHERE INVENTORY_ID IN (SELECT INVENTORY_ID FROM INVENTORY\n       WHERE FILM_ID IN (SELECT FILM_ID FROM FILM\n       WHERE FILM_ID IN (SELECT FILM_ID FROM FILM_CATEGORY\n       WHERE CATEGORY_ID IN (SELECT CATEGORY_ID FROM CATEGORY)))));\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Nested_INs.png\"><img decoding=\"async\" width=\"814\" height=\"445\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Nested_INs.png\" alt=\"Nested In Queries\" class=\"wp-image-98946\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Nested_INs.png 814w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Nested_INs-300x164.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/Nested_INs-768x420.png 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/a><figcaption>Nested In Queries<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"h-6-3-exists\">6.3 Exists<\/h3>\n<p>In cases where a table needs to be joined across multiple tables, we can use the Exists operator to achieve the effect of an intersect operator.<\/p>\n<pre class=\"brush:sql\">SELECT C.FIRST_NAME,C.LAST_NAME,A.ADDRESS,A.POSTAL_CODE\nFROM CUSTOMER C,ADDRESS A\nWHERE C.ACTIVE =1 \nAND C.ADDRESS_ID = A.ADDRESS_ID\nAND EXISTS (SELECT * \n            FROM CITY CT\n            WHERE  CT.COUNTRY_ID IN (44,91,103,8,20) \n            AND CT.CITY_ID = A.CITY_ID);\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/exists.jpg\"><img decoding=\"async\" width=\"708\" height=\"678\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/exists.jpg\" alt=\"Intersect operator with Exists\" class=\"wp-image-98947\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/exists.jpg 708w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/exists-300x287.jpg 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/a><figcaption>Intersect operator with Exists<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-7-summary\">7. Summary<\/h2>\n<p>Even though My SQL does not directly support the Intersect operator, we can still emulate the Intersect operator in multiple ways. There have been several requests made already to My SQL to introduce support for all the set operators to provide better dynamic SQL query building.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-8-download-the-source-code\">8. Download the Source Code<\/h2>\n<p>This was an example of the Intersect operation using MySQL RDBMS.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/01\/SQL_INTERSECT_Queries.zip\"><strong> SQL Intersect Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL Server, and PostgreSQL support the Intersect operator. However, MySQL does not. In this article, we will understand what the Intersect operator is and how to emulate it in MySQL. 2. &hellip;<\/p>\n","protected":false},"author":232,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[647],"class_list":["post-98914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Intersect Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Intersect Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-12T09:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-19T13:42:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Reshma Sathe\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reshma Sathe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\"},\"author\":{\"name\":\"Reshma Sathe\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/079aa9a12c7b8ebea3391ebeb6036a74\"},\"headline\":\"SQL Intersect Example\",\"datePublished\":\"2021-01-12T09:00:00+00:00\",\"dateModified\":\"2021-02-19T13:42:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\"},\"wordCount\":951,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"mysql\"],\"articleSection\":[\"sql\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\",\"name\":\"SQL Intersect Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2021-01-12T09:00:00+00:00\",\"dateModified\":\"2021-02-19T13:42:42+00:00\",\"description\":\"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"sql\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"SQL Intersect Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/079aa9a12c7b8ebea3391ebeb6036a74\",\"name\":\"Reshma Sathe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/08\/reshma_sathe-96x96.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/08\/reshma_sathe-96x96.png\",\"caption\":\"Reshma Sathe\"},\"description\":\"I am a recent Master of Computer Science degree graduate from the University Of Illinois at Urbana-Champaign.I have previously worked as a Software Engineer with projects ranging from production support to programming and software engineering.I am currently working on self-driven projects in Java, Python and Angular and also exploring other frontend and backend technologies.\",\"sameAs\":[\"www.linkedin.com\/in\/reshma-sathe\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/reshma-sathe\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Intersect Example - Java Code Geeks","description":"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/","og_locale":"en_US","og_type":"article","og_title":"SQL Intersect Example - Java Code Geeks","og_description":"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL","og_url":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-01-12T09:00:00+00:00","article_modified_time":"2021-02-19T13:42:42+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Reshma Sathe","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Reshma Sathe","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/"},"author":{"name":"Reshma Sathe","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/079aa9a12c7b8ebea3391ebeb6036a74"},"headline":"SQL Intersect Example","datePublished":"2021-01-12T09:00:00+00:00","dateModified":"2021-02-19T13:42:42+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/"},"wordCount":951,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["mysql"],"articleSection":["sql"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/","url":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/","name":"SQL Intersect Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2021-01-12T09:00:00+00:00","dateModified":"2021-02-19T13:42:42+00:00","description":"1. Introduction In this article, we will look at the Intersect operation. SQL Set Theory contains the Intersect. Most of the RDBMS like Oracle, SQL","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/sql-intersect-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"sql","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/"},{"@type":"ListItem","position":5,"name":"SQL Intersect Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/079aa9a12c7b8ebea3391ebeb6036a74","name":"Reshma Sathe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/08\/reshma_sathe-96x96.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/08\/reshma_sathe-96x96.png","caption":"Reshma Sathe"},"description":"I am a recent Master of Computer Science degree graduate from the University Of Illinois at Urbana-Champaign.I have previously worked as a Software Engineer with projects ranging from production support to programming and software engineering.I am currently working on self-driven projects in Java, Python and Angular and also exploring other frontend and backend technologies.","sameAs":["www.linkedin.com\/in\/reshma-sathe"],"url":"https:\/\/examples.javacodegeeks.com\/author\/reshma-sathe\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/232"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=98914"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98914\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=98914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=98914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=98914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}