bhylu.com

Advertisements

SQL Any

ANY compares a value with at least one value produced by a subquery. Some database systems also use the name SOME. SQLite does not implement this quantified comparison syntax, so the runnable example uses an equivalent EXISTS condition.

Understand the idea

The question here is which products cost more than at least one Stationery product. A product passes when there is a Stationery price below it. The MySQL or SQL Server spelling is price_cents > ANY (SELECT price_cents FROM products WHERE category = 'Stationery'). The SQLite form makes the row-by-row comparison explicit.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

SELECT p.name
FROM products AS p
WHERE EXISTS (
  SELECT 1 FROM products AS s
  WHERE s.category = 'Stationery' AND p.price_cents > s.price_cents
)
ORDER BY p.product_id;

Read the result

Desk lamp, Canvas bag, and Pencil set pass because each costs more than the 250-cent notebook. The notebook itself does not pass.

Try one change

Change the inner category to Accessories and predict which products remain above at least one price in that group.