Postgres Intersect Command

PostgreSQL intersects feature combines the resultant values of two or more SELECT statements. This option is used to fetch the common entities in two or more tables. Then a single resultant value is obtained. A set of rows is obtained that is present in both tables. This feature is very helpful in fetching all common records in a single query by using more.


Select Column_name1, Column_name2 from table1


Select Column_name1, Column_name2 from table2

This is a basic syntax of the intersection feature. This can be changed by adding an asterisk for all the data by not specifying the column names. The command works in a way that both the SELECT statements will work and fetch the record and then the INTERSECT feature will choose the common data and remove the uncommon one.

Working of INTERSECT Feature in PostgreSQL

  • This feature is only used when we have common data or duplicate data in both the specified columns.
  • It helps to deal with more than one table fetching the same entities from the tables.

To implement the intersect command, we need to create three tables here. We can use any table for example but to understand the concept of intersection, we need to inherit some attributes and create a joint. The first one will contain the whole information regarding the student. Whereas the second and third tables will contain some portion of the information added in the first one. So we will create the first table named student.

>> CREATE TABLE student(

student_id serial PRIMARY KEY,

student_name VARCHAR (255) NOT NULL


The table contains two columns, student id, and the student’s name. Now, we will further create two tables named subject and practical. Both tables are originated from the parent table student. So to connect them, both the tables will contain the primary key of the student table as a foreign key constraint to create a joint between all the tables. Both the below mentioned tables have one attribute of their own and one attribute as a primary key exported from the student table.


student_id INT PRIMARY KEY,

join_date DATE NOT NULL,

  FOREIGN KEY (student_id) REFERENCES student (student_id)


CREATE TABLE practical(

student_id INT PRIMARY KEY,

join_date DATE NOT NULL,

  FOREIGN KEY (student_id) REFERENCES student (student_id)


After the creation of all the tables, we will use an insert statement to enter data in it. Each table will get data after the execution of the command.

>> INSERT INTO student(student_name) VALUES ('Thomas'), ('Bay hikel'), ('Jason sily'), ('Robert Hunt'),('Arjit suba'), ('Micky Angel'), ('Shweta singh'), ('Tom ridle'), ('Sabana'), ('Timmy honad');

You can see that the data is inserted only in the name column; the id column is filled automatically like the serial number. And the values in the id column of the subject and practical tables will be filled from the subject table. But the insertion in the subject and the practical table will be in such a way that some ids in both the tables should be similar to cope with the situation we had applied.

Similarly the subject and the practical tables are inserted by data.

INSERT INTO subject VALUES (1, '2000-02-01'), (2, '2001-06-01'), (5, '2002-01-01'), (7, '2005-06-01');

>> INSERT INTO practical VALUES (9, '2000-01-01'), (2, '2002-06-01'), (5, '2006-06-01'), (10, '2005-06-01');

Subject table:

Practical table:

Now, we will use a SELECT statement to see the inserted data in the student table.

>> select * from student;

Example 1

This example will show the record of those students who have joined both the subjects and the practical from the student table. This will be done by using an INTERSECT statement applied on both subject and the practical table. This command will work in such a way that it will consider the student ids in both the subject and the practical class. Those ids that are common in both tables will be fetched separately.

>> SELECT student_id FROM subject INTERSECT SELECT student_id FROM practical;

This will fetch two rows as only two students have enrolled in both the subjects and the practical. Both ids have separate joining dates, that’s why we have not fetched that column.

Now consider a scenario in which we want to fetch the record from the id column and the join_date column from one table that intersects the data in the id column of the practical table, and then guess what will happen?

>> select student_id, join_date from subject INTERSECT select student_id from practical;

An error will occur because for the intersect feature to be applied, we must have mentioned the same number of columns in both tables. Otherwise, the query will not work.

So to overcome this error, now we will take both columns for both tables to apply to intersect, but unfortunately there is not a single row that matches with the join_date. If any date of joining in both the tables is the same, then the resultant value must have two columns with a common row of both affected tables.

>> select student_id, join_date from subject INTERSECT select student_id, join_date from practical;

Example 2

This example includes the same intersect command, but now some additional features are added in the command that is ORDER BY clause. This feature is used to arrange the fetched result. But as we mention ‘DESC’ with this clause, then the result is arranged in descending order.

>> SELECT student_id FROM Subject INTERSECT SELECT student_id FROM Practical ORDER BY student_id DESC;

From the output, you can see that the values were first arranged in ascending order but DESC has converted them reversely.

Example 3

In this example, we will apply the intersect command on the student_id column of the subject and the student table. Then those ids that we have entered only in the subject table will be fetched.

>> select student_id from subject INTERSECT select student_id from student;

Example 4

If you apply an intersect feature on the same table that you have selected then it will fetch all the records of the table just like the resultant value obtained from the SELECT statement. You can see that we have applied the intersect command on the practical table without specifying the column, so all the data along with the join date is fetched.

>> select * from practical INTERSECT select * from practical;

Postgres Intersect Command via pgAdmin

Now, we will check the intersect command in the pgAdmin dashboard just like psql. After opening the interface, go to the tables in the Postgres database and sequence options. Keep on expanding until you get the tables option now select the table student and then proceed to the query tool to use a command.

>> select student_id from practical INTERSECT select student_id from student;

We have altered the above query in which the intersect feature is applied on the practical and the student table.

After applying the query, execute it by selecting the option. You will see that all the common ids are fetched from both the specified tables in the command.


The article ‘Postgres intersect command’ describes the working of this operator in the PostgreSQL database system. Some elementary examples are used here to describe its working in detail. Each common data can be fetched easily by using this INTERSECT feature in the commands to avoid any redundancy and coupling of data in a database.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.