Exists Operator in SQL

Rumman Ansari   Software Engineer   2023-03-25   5876 Share
☰ Table of Contents

Table of Content:


The EXISTS operator is used to test for the existence of any record in a subquery.

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

Syntax:


SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

EmpId

EmpName

EmpAddress

EmpDept

1

Rambo

Kolkata

IT

2

Rohit

Kolkata

IT

3

Rohon

Kolkata

ITIS

4

Ronok

Kolkata

ITIS

5

Rubin

Kolkata

ITIS

6

Sorif

Kolkata

ADMIN

7

Soriful

Kolkata

ADMIN

8

Sofik

Kolkata

ADMIN

Example:

Code:


SELECT EmpName, EmpAddress
FROM Employee
WHERE EXISTS
(SELECT EmpId FROM Employee WHERE EmpName = 'Rambo')

Output:

The above code will produce the following result-

EmpName

EmpAddress

Rambo

Kolkata

Rohit

Kolkata

Rohon

Kolkata

Ronok

Kolkata

Rubin

Kolkata

Sorif

Kolkata

Soriful

Kolkata

Sofik

Kolkata