SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

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.