0
0
MySQLquery~5 mins

Subqueries with EXISTS in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the EXISTS keyword do in a SQL query?
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
intermediate
How does a subquery with EXISTS differ from a normal JOIN?
EXISTS only checks for the presence of rows in the subquery and returns TRUE or FALSE, while JOIN combines rows from two tables based on a condition.
Click to reveal answer
beginner
Write a simple example of a query using EXISTS to find customers who have placed orders.
SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
Click to reveal answer
intermediate
Why might you use EXISTS instead of IN in a subquery?
EXISTS can be more efficient because it stops searching as soon as it finds a matching row, while IN may scan all rows. EXISTS also handles NULLs better.
Click to reveal answer
beginner
Can EXISTS subqueries return data? Explain.
No, EXISTS subqueries do not return data themselves. They only return TRUE or FALSE to indicate if the subquery found any rows.
Click to reveal answer
What does the EXISTS keyword return in a SQL query?
AAll rows from the subquery
BTRUE if subquery returns at least one row
CThe count of rows in the subquery
DNULL if subquery returns no rows
Which of the following is a valid use of EXISTS?
ASELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE condition);
BSELECT EXISTS FROM table1;
CEXISTS SELECT * FROM table1;
DSELECT * FROM table1 WHERE EXISTS = 1;
Why might EXISTS be preferred over IN in some cases?
AIN ignores NULL values
BIN is faster than EXISTS
CEXISTS returns all matching rows
DEXISTS stops searching after finding one match
What does the subquery inside EXISTS typically return?
ANULL values only
BA specific column to display
CAny column or 1, since only existence matters
DThe total count of rows
If a subquery inside EXISTS returns no rows, what is the result?
AFALSE
BTRUE
CAn error
DNULL
Explain how the EXISTS keyword works in SQL and give a simple example.
Think about checking if something exists rather than retrieving data.
You got /3 concepts.
    Describe a scenario where using EXISTS is better than using a JOIN or IN clause.
    Consider when you only need to know if related data exists.
    You got /4 concepts.