← Python lessons

SQL Comparison Operators

Part of a free Python course — sign up to save your progress and get AI hints.Sign up free

SQL comparison operators such as >= compare numbers in a WHERE condition. >= means greater than or equal to. A number is written without quotes.

The examples below use this books table. Change a query and press Run to see its result.

Table: books

titleauthorpages
WonderR. J. Palacio320
MatildaRoald Dahl240
HolesLouis Sachar272
The BFGRoald Dahl208

Keep numbers at or above a value with >=

WHERE pages >= 240 keeps the books with 240 pages or more:

SELECT title, pagesFROM booksWHERE pages >= 240;

Wonder, Matilda and Holes are in the result. Matilda has exactly 240 pages, and >= keeps it. The number 240 has no quotes, because pages holds numbers, not text.

Check that >= keeps the number itself

Compare it with >, which means greater than. > keeps only the values bigger than the number:

SELECT title, pagesFROM booksWHERE pages > 240;

Now only Wonder and Holes are in the result. Matilda is left out, because 240 is not greater than 240. For "at least 240", use >=.

Note: Write >= with the = second and no space between the two signs.

See also: SQL WHERE.

Your turn

Add a WHERE line so the query shows only the books with 272 pages or more.

🎯 Goal — make your query show exactly:
title  | pages
-------+------
Wonder | 320
Holes  | 272
(2 rows)
-- add a WHERE line after FROM, and move the ; to the endSELECT title, pagesFROM books;