MySQL EXISTS Operator

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.