"if you encounter any errors in SAP, send me a screenshot at pramod@learntosap.com, and I will help you resolve the issue."

SQL EXISTS Operator

SQL TUTORIALS-

SQL EXISTS Operator

Introduction-


Trulli Trulli

βœ…In a Simple Word The SQL EXISTS operator is used to check whether a subquery returns any rows. It returns TRUE if the subquery returns one or more rows, and FALSE if the subquery returns no rows...


πŸ’‘ Note: The SQL EXISTS operator It is faster than IN in many cases, especially with large subqueries.

βœ… Syntax Example -



SELECT column1, column2, ...
FROM table_name
WHERE EXISTS (SELECT 1 FROM another_table WHERE condition);

πŸ’‘ Note: The SQL EXISTS operator Example Of Two Tables 1)-Employee. And 2)-Department
Emoji Examples in HTML

πŸ”Έ Example -)Table: Employee


EmpID Name DepID
101 ANNI 1
102 RAJ 2
103 POOJA 3
104 SUJAN 4
105 RUPESH 5
108 SIMA 6

Emoji Examples in HTML

πŸ”Έ Example -)Table: Department


DeptID DeptName
1 IT
2 HR
3 HR
4 SALES
5 FINANCE
7 IT

βœ… Example 1:-(Show) Check if Employees Exist in a Department-



SELECT DeptName
FROM Department D
WHERE EXISTS (
  SELECT 1
  FROM Employee E
  WHERE E.DeptID = D.DeptID
);

βœ…Result Show :-

Result β†’

Emoji Examples in HTML

EmpID Name DeptName
101 ANNI IT
102 RAJ HR

βœ… Example 2: (SEARCH) Find employees who are not in any department-



SELECT EmpName
FROM Employee E
WHERE EXISTS (
  SELECT 1 FROM Orders O
  WHERE O.EmpID = E.EmpID
);

βœ…Result Show :-

Result β†’

Emoji Examples in HTML

DepID NAME
108 SIMA

βœ… Example 3: List employees who are in the β€˜HR’ department-



SELECT EmpName
FROM EMPLOYEE E
WHERE EXISTS (
  SELECT 1
  FROM DEPARTMENT D
  WHERE D.DeptID = E.DeptID AND D.DeptName = 'HR'
);

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


DeptID DeptNAME NAME
102 HR RAJ
103 HR POOJA

Practice - Yes/No Quiz

1.EXISTS faster than IN in all situations?

2.an EXISTS be used to check if a department has employees?

3.Does NOT EXISTS return true if the subquery returns rows?


May Be Like Important Link-

-Credit Memo and Return PO

-Bill Of Material (BOM)

-How To Create Quotation

-How To Create Debit Memo Request