๐ŸŽ“ A1Learner
๐Ÿ”

SQL Comparison Operators

A comparison operator compares two values and tests how they stand. Comparisons test numbers by size and text by alphabet order.

Smaller than

The < sign tests that a price is smaller than 10. Four products pass.

SELECT name, price FROM products WHERE price < 10 ORDER BY price;

At least a value

The >= sign keeps values that reach the named value. A stock of exactly 150 passes.

SELECT name, stock FROM products WHERE stock >= 150 ORDER BY name;

Text in alphabet order

Text compares by alphabet order. Names that come before the letter D pass here.

SELECT name FROM customers WHERE name < 'D' ORDER BY name;

Example

SELECT name, category, price FROM products WHERE category <> 'Stationery' AND price <= 12.25 ORDER BY price;

Key points

  • =: tests that two values are equal
  • <>: tests that two values are not equal
  • != is the other spelling of <>.
  • <: tests that the left value is smaller or earlier
  • >: tests that the left value is larger or later
  • <=: tests that a value is smaller than or equal to another value
  • >=: tests that a value is larger than or equal to another value

In this example, the <> test drops every Stationery product. The <= test keeps prices up to and including 12.25. Three products pass both tests. The rows come back in price order.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท DB Fiddle

Exercise

Which sign tests that two values are not equal?