MySQL | EXISTS Operator: Learn about the MySQL EXISTS Operator, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 14, 2022

EXISTS Operator

As the name suggests, the EXISTS operator checks whether the particular condition record exists in the table or not. It is a purely Boolean type operator, which returns TRUE or FALSE according to the condition applied. If the condition returns any record then the value will be TRUE, else FALSE.

EXISTS Operator Syntax

SELECT column_name(s) 
FROM table_name 
WHERE EXISTS (sub query);

Let us see some examples;

EXISTS Operator Examples

Suppose we have a table named "student_details" and inside this table, we have the following columns,

EXISTS Operator Example 1

And the table is named "attendance" and inside this table, we have the following columns:

EXISTS Operator Example 2

Suppose we want to find the student names of all the IDs present in the table named as attendance, in such case we will write,

SELECT student_name, ID 
FROM student_details 
WHERE EXISTS (SELECT * FROM attendance WHERE student_details.ID=attendance.ID);
EXISTS Operator Example 3

In the same way, if we want to find the names of the student, those who are present in the "student_details" table but are not there in the attendance table,

In such case, we will use NOT EXISTS,

EXISTS Operator Example 4

This will give all the names and IDs which are there in the "student_details" table but do not exist in the attendance table.


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.