The SQL EXISTS operator checks the existence of any record through a subquery.

The subquery returns one or more records if the EXISTS operator returns true.

SYNTAX

75.png

Where,

  • EXISTS                                             : keyword used checks the existence of any record in a table.

  • Table_name                                     : The name of the table from where you want to fetch the data

  • Column1Name, Column2Name        : The name of the columns of the table which you want to uniquely fetch.

EXAMPLE

Let’s consider two different tables: SalaryInfo and Persons. I want to fetch the salary details of all the people present in Persons table. Here is a DEMO DATABASE:

76.png
77.png
 

Figure 23.1. SELECT * FROM Persons;

 

Figure 23.2. SELECT * FROM SalaryInfo;

QUERY

78.png

Figure 23.3. EXISTS QUERY

OUTPUT

79.png

Figure 23.4. OUTPUT OF EXISTS QUERY

PRACTICE YOURSELF 

80.png