Using the EXISTS Operator in SQL: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-18 09:42:46   6128  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

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



Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.