EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MySQL EXISTS

By Roja MetlaRoja Metla

Home » Data Science » Data Science Tutorials » MySQL Tutorial » MySQL EXISTS

MySQL EXISTS

Introduction to MySQL Exists

MySQL EXITS is used to find out whether a particular row is existing in the table or not. MySQL Exists is used with the subquery and returns the rows that are equal or matches to the result returned by the subquery. The statement returns true if the row exists in the table else false. The True is represented by 1 and false is represented by 0. It is very inefficient to use the EXISTS in the MySQL since EXISTS re-run for every row in the query table. So, it is significant to not us the EXISTS condition.

In this session let us see the usage of EXISTS along with the example: –

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of MySQL EXISTS

Now let us see the example : –

<select statement> WHERE EXISTS <sub query statement>;

In the above syntax, the statement returns the values from the main select statement if it is existing in the subquery statement.

To check the existence of the value we use the below syntax: –

SELECT EXISTS (<query statement>);

How does EXISTS work in MySQL?

Now let us create a table and insert data into it and perform the EXISTS condition on the table.

create table EXISTS_Demo
(
id int,
name varchar(20),
location varchar(20),
pincode int
);

Insert the below rows into the table as below: –

insert into EXISTS_DEMO values (1, 'Sam', 'MP', 564321);
insert into EXISTS_DEMO values (2, 'Sohan', 'Bangalore', 523321);
insert into EXISTS_DEMO values (3, 'Will', 'Tamilnadu', 523021);
insert into EXISTS_DEMO values (4, 'Ben', 'UP', 564000);
insert into EXISTS_DEMO values (5, 'Hamington', 'UP', 564000);
insert into EXISTS_DEMO values (6, 'Ji eun', 'Bangalore', 523321);
insert into EXISTS_DEMO values (7, 'Jimin', 'UP', 564000);
insert into EXISTS_DEMO values (8, 'Jk', 'Bangalore', 523321);
insert into EXISTS_DEMO values (9, 'V', 'AP', 590001);
insert into EXISTS_DEMO values (10, 'Jhope', 'Bangalore', 523321);

Now let us select the columns from the table: –

Select * from EXISTS_Demo;

Output:

MySQL EXISTS output 1

Now let us check the existence of the “ID” where the location is equal to “UP”.

SELECT EXISTS (SELECT * FROM EXISTS_Demo where location='UP');

/* – – – To check the existence of the row from the table where value of location = ‘UP’ –*/

Output:

MySQL EXISTS output 2

Here we have the rows in the table where the location is “UP”. Because of which the output is “1”.

Now let us select the row where the location is “Bihar”. Here we can see that there is no column with location= ‘Bihar’ in the table. This gives the output as “0”.

SELECT EXISTS (SELECT * FROM EXISTS_Demo where location='Bihar');

/* – – – To check the existence of the row from the table where value of location = ‘Bihar’ –*/

Output:

MySQL EXISTS output 3

Example of MySQL EXISTS

Now let us consider the EXISTS condition applying between multiple tables. Let us see the example for the same: –

Let us create the below tables for the same and perform “EXISTS”. Earlier created table and order_details tables as below: –

Order_details table: –

create table order_details
(
o_id int,
id int,
product varchar(20),
quantity int
);

Let us insert data into the order_details table: –

insert into order_details values (12, 1, 'Soap', 50);
insert into order_details values (13, 2, 'shampoo', 50);
insert into order_details values (12, 4, 'Soap', 50);
insert into order_details values (11, 2, 'Tooth Paste', 50);
insert into order_details values (10, 7, 'Detergent', 50);
insert into order_details values (12, 8, 'Soap', 50);
insert into order_details values (12, 9, 'Soap', 50);
insert into order_details values (9, 4, 'Vim', 50);
insert into order_details values (13, 5, 'shampoo', 50);
insert into order_details values (13, 4, 'shampoo', 50);
insert into order_details values (13, 3, 'shampoo', 50);

Popular Course in this category
MySQL Training Program (11 Courses, 10 Projects)11 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,022 ratings)
Course Price

View Course

Related Courses
MS SQL Training (13 Courses, 11+ Projects)Oracle Training (14 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)

Select the table values as below: –

Select * from order_details;

Output:

output 4

Now let us search the existing rows from the “EXISTS_Demo” table are present in the “order_details”. Let us query the table as below: –

select * from EXISTS_Demo D where EXISTS (select * from order_details O WHERE O.ID=D.ID);

 Here if we check the table of “EXISTS_DEMO” and “order_details” table. We could see that the id=6 is not existing in the “Order_details” table. So we get the output for all the columns except the “id=6”.

Output:

output 5

Conclusion

MySQL EXITS is used to find out whether a particular row is existing in the table or not. MySQL Exists is used with the subquery and returns the rows that are equal to the result returned by the subquery. The statement returns true if the row exists in the table else false. The True is represented by 1 and false is represented by 0. It is very inefficient to use the EXISTS in the MySQL since EXISTS re-run for every query in the table. So, it is significant to not us the EXISTS condition.

Recommended Articles

This is a guide to MySQL EXISTS. Here we discuss how does EXISTS works in MySQL along with the appropriate syntax and respective query examples. You may also have a look at the following articles to learn more –

  1. MySQL Index
  2. MYSQL Database
  3. SQL Minus
  4. PostgreSQL REINDEX

MySQL Training Program (11 Courses, 10 Projects)

11 Online Courses

10 Hands-on Projects

92+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL count()
    • MIN() in MySQL
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • TRUNCATE TABLE MySQL
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL 
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT 
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL COALESCE
    • MySQL Cluster
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • Introduction to MySQL
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • How To Install MySQL
    • MySQL OpenSource
    • MySQL Commands
    • Views in MySQL
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL UUID
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL Transaction
    • MySQL sort_buffer_size
    • MySQL Sync
    • MySQL Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MySQL Training Program (11 Courses, 10 Projects) Learn More