MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL INTERSECT Operator

The MySQL does not support INTERSECT operator. However you can still achive using join, depending on the complexity of the INTERSECT query. Basically, the INTERSECT operator takes the results of two queries and returns only rows that appear in both result sets.

MySQL INTERSECT OPERATOR Syntax

To fetch the results of two queries and returns only rows that appear in both result sets, use the following syntax:

Syntax

SELECT col1, col2 FROM tablename1
INTERSECT
SELECT col1, col2 FROM tablename2;

Parameters:

tablename1 & 2: The table name from which you want to achive INTERSECT operator.

.

MySQL INTERSECT OPERATOR example using JOIN

The following MySQL, is using JOIN to perform INTERSECT operator in MySQL:

Example

SELECT A.firstName, A.lastName
FROM tblcustomer A 
JOIN tblcustomerloan B
ON A.firstName = B.firstName AND A.lastName = B.lastName

Note:

In the above example, the JOIN is used to achive INTERSECT with MySQL.

You can use MySQL Command Line Client to achive INTERSECT in MySQL using join. It will look like this: