RadhaPublication
HomeInterviewLearning PathsRole TracksMockBooksPricing
Login / Sign UpLog in

© 2026 Radha Publication. All rights reserved.

Shop on AmazonDonateContactPrivacyTerms
SQL questions
TrickySQL#1057 min readJul 11, 2026

ANY

practice
learning
Practice modeTest yourself instead of reading straight through

Hook: Interviewers love ANY because it looks tiny, but it tests whether you understand existence instead of just exact matches.

Question: What does ANY do in SQL?

Answer: ANY compares one value against a set returned by a subquery and becomes true if the comparison is true for at least one row. For example, x > ANY (...) means x is greater than at least one value in that set. In standard SQL, SOME is a synonym, and = ANY is usually the same idea as IN.

Interview-Ready Answer: I use ANY when I want a condition to pass if at least one row from a subquery matches my comparison. So price > ANY (subquery) means the price beats one or more returned values, not all of them. A useful detail is that = ANY is basically the same as IN, and SOME is the standard synonym.

🧠 Memory Map
Memory map — visual summary of this topic

What ANY really means

ANY is SQL’s way of saying at least one. A subquery is a query inside another query, and the inner query produces the candidate values. Then SQL compares the outer value to each candidate using the operator you wrote, such as =, >, or <.

How it works step by step

  1. Run the subquery and get a one-column set of values.
  2. Take the outer row’s value, for example price.
  3. Compare that value against each inner value with the chosen operator.
  4. If one or more comparisons are true, the whole expression is true.
  5. If none are true, the result is false; if SQL encounters only unknown comparisons because of NULL, the result can be unknown.
  6. If the subquery returns no rows, ANY is false because there is no matching value to prove the condition.

This is why people describe ANY as a logical OR over a set. If you imagine x > ANY (20, 50, 80), SQL is effectively asking: is x > 20 OR x > 50 OR x > 80?

ANY vs IN vs ALL

FormMeaningClosest ideaCommon gotcha
= ANYEqual to at least one valueINNULL can make the result unknown
> ANYGreater than at least one valueExistence checkNot the same as > MIN(...) in every edge case
ALLComparison must be true for every valueUniversal checkEmpty set behaves differently from ANY

IN is just the equality version of ANY. That means col IN (subquery) and col = ANY (subquery) usually express the same idea. But ANY becomes more useful when you want comparisons other than equality, like > ANY or < ANY.

Under the hood

Many SQL optimizers try to rewrite ANY into a form they can execute efficiently, often a semi-join. A semi-join is a join that only asks whether a match exists; it does not need to return columns from the inner query. If the engine can build a small hash set or use an index, the query may be very fast.

For example, if you have 1,000,000 orders and a subquery with 20 threshold rows, a good plan may scan the orders once and check each order against a hashed 20-row set, which is close to O(N + M). A naive nested-loop plan would compare every outer row to every inner row, which is closer to O(N × M); with 1,000,000 rows and 20 values, that is about 20 million comparisons. Correlated subqueries, where the inner query depends on the current outer row, are the most likely to get expensive if the database cannot decorrelate them.

When to use it

  • Use = ANY when you want membership and prefer the ANY style.
  • Use > ANY or < ANY when you want to compare against a set rather than a single aggregate like MIN or MAX.
  • Use it with subqueries when the candidate values come from another table or from a CTE, which is a named temporary result set.

Important edge cases

  1. Empty subquery: always false for ANY. No row can prove the condition.
  2. NULL values: comparisons with NULL are unknown, so the final result may be unknown if no true comparison exists.
  3. Duplicates: duplicates do not change the truth value, because one true row is enough.
  4. Negative forms: <> ANY is not the same as NOT IN; it only needs one value to be different, which is a much weaker condition.
  5. Vendor differences: some databases, like PostgreSQL, also allow ANY with arrays; interview questions here usually mean the subquery form.

Memory trick: think of ANY as a crowd check: if one person in the crowd says yes, the answer is yes. If nobody can prove it, the answer is false or unknown.

Real-world story

Imagine a checkout service for an online marketplace. The team keeps a table of fraud thresholds by region and card type, and the query uses amount > ANY (SELECT threshold ...) to flag transactions that exceed at least one risky threshold. That means a cart can enter review if it looks suspicious compared with any relevant rule, not only if it beats every rule.

What goes wrong when someone misunderstands it? A developer swaps ANY for IN because both look similar, thinking they are interchangeable. Now the system only flags transactions whose amount exactly matches one of the threshold numbers, so the fraud queue suddenly drops from thousands of alerts per day to almost none. The symptom is a sharp fall in manual-review volume, chargebacks rise a few days later, and logs show messages like reviewed=124 when the normal baseline is 8,000+. That kind of mistake is expensive because the query still runs and looks reasonable, but the business rule is silently wrong.

SQL
-- Demonstration of ANY with standard SQL-style CTEs and VALUES.
-- The comments explain WHY each query matters, not just the syntax.

-- 1) Basic use: price is greater than at least one benchmark price.
WITH products(product_name, price) AS (
  VALUES ('Keyboard', 50),
         ('Mouse', 25),
         ('Monitor', 220),
         ('Cable', 10)
),
benchmarks(price_limit) AS (
  VALUES (20),
         (100)
)
SELECT product_name, price
FROM products
WHERE price > ANY (SELECT price_limit FROM benchmarks)
ORDER BY price;

-- 2) Equality form: = ANY is the same idea as IN for a single-column subquery.
WITH colors(color) AS (
  VALUES ('red'),
         ('blue'),
         ('green')
),
wanted(color) AS (
  VALUES ('blue'),
         ('orange')
)
SELECT color
FROM colors
WHERE color = ANY (SELECT color FROM wanted)
ORDER BY color;

-- 3) Edge case: an empty subquery makes ANY false.
--    There is no row that can prove the comparison, so nothing passes the filter.
WITH products(product_name, price) AS (
  VALUES ('Keyboard', 50),
         ('Mouse', 25)
),
empty_limits(price_limit) AS (
  SELECT 1 AS price_limit
  WHERE 1 = 0
)
SELECT product_name, price
FROM products
WHERE price > ANY (SELECT price_limit FROM empty_limits);

-- 4) Bonus gotcha: a NULL in the subquery can make the result unknown.
--    Different engines may display that as NULL in the output, which is why
--    NULL handling is one of the most common interview traps.
WITH sample_limits(price_limit) AS (
  VALUES (CAST(NULL AS INTEGER)),
         (100)
)
SELECT 50 = ANY (SELECT price_limit FROM sample_limits) AS eq_any_result;

Follow-up & Tricky Questions

  • What is the difference between ANY and ALL? ANY needs one true comparison, while ALL needs every comparison to be true. In plain English: ANY asks for one witness, ALL asks for unanimous agreement.
  • Is = ANY the same as IN? Yes, for a one-column subquery and equality comparison, they express the same idea. Interviewers like this question because it checks whether you know the shorthand and the broader operator family.
  • What happens if the subquery returns no rows? ANY is false, so the outer row does not match. This surprises people because there is nothing to compare against, and SQL treats that as a failed existence test.
  • How do NULL values affect ANY? A comparison with NULL is unknown, not true or false. If no comparison is true and at least one is unknown, the overall result can be unknown, which usually filters the row out in a WHERE clause.
  • Can the optimizer make ANY fast? Yes, many engines rewrite it into a semi-join, hash lookup, or indexed search. The exact plan depends on the database, indexes, and whether the subquery is correlated.
  • Is SOME different from ANY? No, they are synonyms in standard SQL. If you know one, you know the other.
  • Is x <> ANY (subquery) the same as NOT IN? No. <> ANY means x is different from at least one value, which is much weaker than being different from every value; NOT IN is the opposite of membership and has its own NULL trap.
  • Does x > ANY (subquery) mean x > MIN(subquery)? It is often similar when the subquery is non-empty and free of NULLs, but do not say they are always identical. Empty sets and NULL behavior can change the result, so interviewers want the logic, not a shortcut guess.

Common Mistakes

  • Thinking ANY means every row. Correction: that is ALL; ANY means at least one row.
  • Using IN for non-equality comparisons. Correction: IN is basically equality only; use > ANY, < ANY, or ALL when the operator matters.
  • Ignoring NULL results. Correction: comparisons can become unknown, so a row may disappear from a WHERE clause even when you expect a simple true/false answer.
  • Confusing <> ANY with NOT IN. Correction: they are very different, and NOT IN is usually the one with the dangerous NULL behavior.

Memory Hook

ANY = one yes is enough. Picture a bouncer checking a guest list: if the guest matches any one approved name, they get in.

Cheat Sheet

  • ANY means at least one comparison is true.
  • SOME is the same as ANY.
  • = ANY is basically IN.
  • > ANY and < ANY compare against a set, not a single value.
  • Empty subquery with ANY is false.
  • NULL can make the result unknown.

Practice Tasks

  • Rewrite a simple IN query using = ANY.
  • Create a tiny table with three numbers and predict the result of 10 > ANY (...) and 10 > ALL (...).
  • Add a NULL to the subquery and observe how the result changes.
Previous
Back to Questions←→to navigate
Next

Recommended Resources

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

Recommended Book

Cracking the Coding Interview

189 Programming Questions and Solutions

-- Demonstration of ANY with standard SQL-style CTEs and VALUES. -- The comments explain WHY each query matters, not just the syntax. -- 1) Basic use: price is greater than at least one benchmark price. WITH products(product_name, price) AS ( VALUES ('Keyboard', 50), ('Mouse', 25), ('Monitor', 220), ('Cable', 10) ), benchmarks(price_limit) AS ( VALUES (20), (100) ) SELECT product_name, price FROM products WHERE price > ANY (SELECT price_limit FROM benchmarks) ORDER BY price; -- 2) Equality form: = ANY is the same idea as IN for a single-column subquery. WITH colors(color) AS ( VALUES ('red'), ('blue'), ('green') ), wanted(color) AS ( VALUES ('blue'), ('orange') ) SELECT color FROM colors WHERE color = ANY (SELECT color FROM wanted) ORDER BY color; -- 3) Edge case: an empty subquery makes ANY false. -- There is no row that can prove the comparison, so nothing passes the filter. WITH products(product_name, price) AS ( VALUES ('Keyboard', 50), ('Mouse', 25) ), empty_limits(price_limit) AS ( SELECT 1 AS price_limit WHERE 1 = 0 ) SELECT product_name, price FROM products WHERE price > ANY (SELECT price_limit FROM empty_limits); -- 4) Bonus gotcha: a NULL in the subquery can make the result unknown. -- Different engines may display that as NULL in the output, which is why -- NULL handling is one of the most common interview traps. WITH sample_limits(price_limit) AS ( VALUES (CAST(NULL AS INTEGER)), (100) ) SELECT 50 = ANY (SELECT price_limit FROM sample_limits) AS eq_any_result;