×

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement.

The syntax for the INTERSECT operation:

SELECT COLUMN_NAME1, COLUMN_NAME2, COLUMN_NAME3 FROM TABLE_NAME1 INTERSECT SELECT COLUMN_NAME1, COLUMN_NAME2, COLUMN_NAME3 FROM TABLE_NAME2;

Rules to be followed using INTERSECT operator are as follows:

  1. The number of Columns and Order of Columns must be same.
  2. Data Type must be Compatible.

Let's understand the concept of SQL INTERSECT with the help of examples.

Consider the following tables along with the given records.

Table 1: Student

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
1NEHA8588100273
2VISHAL709082242
3SAMKEET758896259
4NIKHIL607580215
5YOGESH566578199
6ANKITA958596276
7SONAM9889100287
8VINEET8590100275
9SANKET867865229
10PRACHI908075245
101SNEHA8588100273
103VISHAL758896259

Table 2: Stud

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
4NIKHIL607580215
6ANKITA958596276
7SONAM9889100287
9SANKET867865229
101SNEHA8588100273
102SAMKEET709082242
103VISHAL758896259
105YOGESHWARI566578199
106VINAY8590100275
107PRASHAKA908075245
8VINEET8590100275

Example 1: Execute a query to perform INTERSECT operation between Student table and Stud table.

SELECT * FROM STUDENT INTERSECT SELECT * FROM STUD;

In the above query, we have used two SELECT queries. The First SELECT query fetches the data from the Student table. It performs INTERSECT operation with the data fetched by the Second SELECT query that retrieves the data from the Stud table. Only similar records between these two tables are selected.

The output from the above query is:

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
4NIKHIL607580215
6ANKITA958596276
7SONAM9889100287
8VINEET8590100275
9SANKET867865229
101SNEHA8588100273
103VISHAL758896259
SQL INTERSECT

Only common records between the Student Table and the Stud tables are displayed.

Example 2: Execute a query to perform INTERSECT operation between Student table and Stud table but display only those Student records from the Stud table where maths marks are equal to 100.

SELECT * FROM STUDENT INTERSECT SELECT * FROM STUD WHERE MATHS_MARKS = 100;

The output from the above query is:

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
7SONAM9889100287
8VINEET8590100275
101SNEHA8588100273
SQL INTERSECT

Only those students' records are displayed whose math marks are equal to 100 from the Stud table and are common between both the Student and the Stud table.

Example 3: Execute a query to perform INTERSECT operation between Student table and Stud table but display only those Student records from the Student table where chemistry marks are greater than 80.

SELECT * FROM STUDENT WHERE CHEMISTRY_MARKS > 80 INTERSECT SELECT * FROM STUD;

The output from the above query is:

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
6ANKITA958596276
7SONAM9889100287
8VINEET8590100275
101SNEHA8588100273
103VISHAL758896259
SQL INTERSECT

Only those students' records are displayed whose chemistry marks are greater than 80 from the Student Table and are common between both the Student and the Stud table.

Example 4: Execute a query to perform INTERSECT operation between Student table and Stud table and display only those Student records where physics marks are greater than 75 from the Student Table, and second select queries that display only those Student records where maths marks are greater than 90 from the Stud.

SELECT * FROM STUDENT WHERE PHYSICS_MARKS > 75 INTERSECT SELECT * FROM STUD WHERE MATHS_MARKS > 90;

The output from the above query is:

STUDENT_IDSTUDENT_NAMEPHYSICS_MARKSCHEMISTRY_MARKSMATHS_MARKSTOTAL_MARKS
6ANKITA958596276
7SONAM9889100287
8VINEET8590100275
101SNEHA8588100273
SQL INTERSECT

Only those students' records are displayed whose physics marks are greater than 75 from the Student Table, and math marks are greater than 90 from the Stud Table and are common between both the Student and the Stud table.


Related Topics

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.