0
0
SQLquery~5 mins

Subquery with EXISTS operator in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the EXISTS operator do in SQL?
EXISTS checks if a subquery returns any rows. It returns TRUE if the subquery has at least one row, otherwise FALSE.
Click to reveal answer
beginner
Write a simple example of a query using EXISTS.
SELECT * FROM Customers WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.CustomerID = Customers.ID);<br>This returns customers who have at least one order.
Click to reveal answer
intermediate
How is EXISTS different from IN in SQL?
EXISTS checks for the presence of rows and stops at the first match, making it efficient for some cases. IN compares values and may scan more rows.
Click to reveal answer
intermediate
Can EXISTS subquery refer to the outer query tables?
Yes, EXISTS subqueries often use columns from the outer query to filter results, making them correlated subqueries.
Click to reveal answer
intermediate
Why might you use EXISTS instead of JOIN?
EXISTS is useful when you only want to check if related rows exist, not to retrieve them. It can be faster and clearer for existence checks.
Click to reveal answer
What does the EXISTS operator return if the subquery finds no rows?
AFALSE
BAn error
CNULL
DTRUE
Which of the following is a valid use of EXISTS?
ASELECT * FROM Employees WHERE EXISTS Employees.DeptID = Departments.ID;
BSELECT * FROM Employees WHERE EXISTS (Departments.ID);
CSELECT * FROM Employees WHERE EXISTS Departments;
DSELECT * FROM Employees WHERE EXISTS (SELECT * FROM Departments WHERE Departments.ID = Employees.DeptID);
EXISTS subqueries are usually:
AUncorrelated
BAlways return multiple rows
CCorrelated
DUsed only with aggregate functions
Which is true about EXISTS performance?
AEXISTS always scans the entire subquery result
BEXISTS stops checking after finding the first matching row
CEXISTS is slower than IN in all cases
DEXISTS returns all rows from the subquery
What does this query do? SELECT * FROM Products WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.ProductID = Products.ID);
AReturns products that have at least one order
BReturns all products
CReturns products with no orders
DReturns orders with matching products
Explain how the EXISTS operator works in SQL and give a simple example.
Think about checking if something exists before selecting.
You got /3 concepts.
    Describe the difference between EXISTS and IN operators and when you might prefer EXISTS.
    Consider performance and purpose of each operator.
    You got /4 concepts.