- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - IN vs EXISTS
In general, to make a query easy or avoid the use of multiple OR conditions in the query, we used IN. The IN operator in SQL allows you to match an expression against a pst of values. where EXISTS is the operator to return the Boolean value that is true or false. Generally, if EXISTS checks that one or more rows are available in the sub query, it will return true otherwise false.
SQL - IN
The IN operator is used to check whether a specific value matches any other value in the set of values. These set of values can either be specified separately or be returned by any sub query.
IN is used with the WHERE clause, and it allows us to use multiple values. It reduces the use of multiple OR conditional statements in the SELECT, UPDATE, INSERT, and DELETE queries.
Let us say that we have a table named Customers and we want to see the details of the customers based on their IDs. Here, we can use the IN operator to fetch the details of these IDs with the help of the WHERE clause.
Syntax
Following is the syntax of the IN operator −
SELECT Columns_name FROM table_name WHERE column_name IN (val1, val2, …, valN);
In the above syntax, the column_name matches every value (val1, val2, ... valN). If the matches occur, IN is obtained to be true; otherwise, IN is obtained to be false.
Example
In the following example, we are creating a SQL query that will fetch the customer s name and salary from the customers table whose ID is 1, 2, or 3.
Following is the customers table −
+------+----------+------+-----------+--------+ | ID | NAME | AGE | ADDRESS | SALARY | +------+----------+------+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 2000 | | 3 | kaushik | 23 | Kota | 2000 | | 4 | Chaitap | 25 | Mumbai | 6500 | | 2 | Aman | 23 | Ranchi | 40000 | +------+----------+------+-----------+--------+
Following is the SQL query to fetch the details −
SELECT Name, Salary FROM customers WHERE ID IN(1, 2, 3);
Output
When we execute the above SQL select query, we get the name and salary of the customer whose ID is 1, 2, or 3.
+---------+--------+ | Name | Salary | +---------+--------+ | Ramesh | 2000 | | kaushik | 2000 | | Aman | 40000 | +---------+--------+
SQL - EXISTS
The EXISTS operator is used to look for the existence of a row in a given table that satisfies a set of criteria. It is a Boolean operator that compares the result of the subquery to an existing record and returns true or false.
The retuned value is true, if the subquery fetches single or multiple records and false if no record is matched. It is used in the combination of the subquery and checks whether a row is returned through this subquery or not.
EXISTS operators follow the query’s efficiency features. When the first true event is detected, it will automatically stop further processing.
We can use the EXISTS operator with the SELECT, UPDATE, INSERT and DELETE queries.
syntax
SELECT column_name FROM table_name WHERE EXISTS( SELECT column_name FROM table_name WHERE condition );
Example
In the following SQL query, we are fetching the name and age of the customers whose age matches from the employees table.
Following is the customers table −
+------+----------+------+-----------+--------+ | ID | NAME | AGE | ADDRESS | SALARY | +------+----------+------+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 2000 | | 3 | kaushik | 23 | Kota | 2000 | | 4 | Chaitap | 25 | Mumbai | 6500 | | 2 | Aman | 23 | Ranchi | 40000 | +------+----------+------+-----------+--------+
Following is the employees table −
+-----+-------+-----+--------+------------+ | EID | NAME | AGE | CITY | C_Phone | +-----+-------+-----+--------+------------+ | 1 | Aman | 23 | Ranchi | 1122567800 | | 2 | Vivek | 25 | Ranchi | 9304567890 | +-----+-------+-----+--------+------------+
Following is the SQL query to display the records −
SELECT NAME, AGE FROM customers WHERE EXISTS( SELECT * FROM employeeS WHERE customers.AGE = employees.AGE);
Output
When the above SQL query is executed, we get the name and age of the customers using the exists operator if the age columns of both tables match.
+----------+------+ | NAME | AGE | +----------+------+ | kaushik | 23 | | Chaitap | 25 | | Aman | 23 | +----------+------+
IN vs EXISTS
Following is the differences between IN and EXISTS −
S.No. | IN | EXISTS |
---|---|---|
1 | It is appped to the SQL query to remove the multiple OR conditions. |
It is used to find whether the data in the subquery truly exist. |
2 | It executes all values contained within the IN block. |
If the value is matched, display the details of the given value. It will terminate the further process if the condition is met. |
3 | It can be used for the comparison of a null value because it returns true, false, and a null value. |
It cannot be used for the comparison of a null value because it returns only true and false values. |
4 | It can be used with subqueries as well as with values. |
It can be used only with subqueries. |
5 | It executes faster when the subquery is smaller. |
It executes faster when the subquery is larger. because it is more efficient than IN and returns only a Boolean value. |