SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. EXISTS operator

Text copied!

« Previous
Next »

EXISTS operator

May 26, 2023, 2:24 p.m. under SQL

  • Hi! You should check IS NOT NULL operator post first.

    In SQL, Operators are special character, symbol or a keyword that is used to perform some specific operations. For example : comparing data

    SQL operators are commonly used with SQL statements to retrieve, filter, and manipulate data.

    EXISTS operator :

    EXISTS operator is used to check whether rows exists in a subquery or not. It retrieves only those rows that exists in a subquery. When a query is placed inside the 'parentheses ( )' of another query, it is referred as 'subquery' or 'nested query'.

    The syntax of the EXISTS operator generally looks like this :
    SELECT column_name(s)
    FROM table_name
    WHERE EXISTS (subquery);
            

    • Specify the column(s) name after the 'SELECT' keyword.

    • Specify Asterisk (*) symbol to selects all columns from the table after the 'SELECT' keyword.

    • Specify the table name after the 'FROM' keyword.

    • Specify the conditions after the 'WHERE' keyword.

    • Specify the subquery after 'EXISTS' keyword.

    Here's an example of how you might use the EXISTS operator :

    Let's suppose we have two tables :

    [a] Employees Table :

    → It contains columns [Employee_Id], [Employee_Name], [Gender] and [Salary].

    [b] Employees_2 Table :

    → It contains all columns same as '[Employees] table' with same data types and order. Except '[Age]' column.

    EXISTS operator

    Let's assume you want to check and retrieve employee data from [Employees] table if there are any rows that have a matching Employee_Id with the [Employees_2] table.

    Run below SQL statement :

    SELECT *
    FROM [Employees]
    WHERE EXISTS ( SELECT * 
                                   FROM [Employees_2] 
                                   WHERE Employees].Employee_Id = [Employees_2].Employee_Id );
            

    Above statement will retrieve employee data from [Employees] table where Employee_Id is 1, as it is the only matching Employee_Id in both the tables.

    EXISTS operator
    To enhance your understanding of 'SQL operators', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What is the use of exists operator in SQL?

    The EXISTS operator in SQL is used to check for the existence of rows in a subquery result.

    How to use if exists condition in SQL?

    To use the IF EXISTS condition in SQL, you typically incorporate it within a conditional statement to perform actions based on whether certain data exists or not.

    What is exists and not exists in SQL?

    EXISTS and NOT EXISTS in SQL are logical operators used in conjunction with a subquery to check for the existence or non-existence of matching rows.

    WHERE to use in and exist in SQL?

    In SQL, the IN operator is used to compare a value against a list of values, while the EXISTS operator is used to check for the existence of rows returned by a subquery within the main query's result set.
    Thank You! You should check NOT EXISTS operator post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy