AI Hints
Homeโ€บSQL Tutorialโ€บFiltering Rows
SQL ยท Lesson 9 of 126

Comparing Values

Equal, not equal, more, less.

โฑ 2 min readโ–ถ Video at 12:25๐ŸŽ“ Free
Watch this lesson in the SQL courseWatch on YouTube โ†—Full SQL course

So far we've used equals, less than, and greater than or equal. SQL has six of these comparison signs in total. Once you know them, you can ask almost any simple question.

Comparing Values
From the video: Comparing Values
  • Six comparison signs
  • You know three already
  • Learn them once, use forever

The six comparisons

Here they are. Equals checks if two things are the same. The two signs together, less than and greater than, mean not equal. Then less than, greater than, and the two with an equals sign, which include the value itself. So price less than or equal to ten includes a book that costs exactly ten.

SignMeansExample
=equal togenre = 'Romance'
<>not equal tostatus <> 'delivered'
<less thanprice < 10
>greater thanstock > 50
<=less than or equalprice <= 10
>=greater than or equalpublished >= 2024

The newest books

Let's find the newest books. Published greater than or equal to twenty twenty four. Run it. Six recent books. Helen could feature these in a new arrivals section.

query.sqlsql
SELECT title, published
FROM books
WHERE published >= 2024;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚          title           โ”‚ published โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Stars Over Lagos         โ”‚ 2024      โ”‚
โ”‚ Numbers Don't Lie        โ”‚ 2025      โ”‚
โ”‚ The Map Maker's Daughter โ”‚ 2025      โ”‚
โ”‚ Deep Water               โ”‚ 2025      โ”‚
โ”‚ One Pan Dinners          โ”‚ 2025      โ”‚
โ”‚ The Painted Door         โ”‚ 2025      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Not equal

Now, how many orders were not delivered? We use not equal, and a new trick. Count star counts the rows, instead of showing them. We'll learn counting properly later. Run it. Three hundred and thirty three orders were cancelled, returned, or are still on the way.

query.sqlsql
SELECT COUNT(*)
FROM orders
WHERE status <> 'delivered';
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ COUNT(*) โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 333      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Books by one author

Your turn. Find the title and author of every book written by Mira Hale. Pause and try it. Here's a solution. Where author equals Mira Hale, in single quotes. Three books.

โœ๏ธ Exercise: Books by one author

Show the title and author of every book by Mira Hale

Show hint

Text goes in single quotes

Show solution
solution.sqlsql
SELECT title, author
FROM books
WHERE author = 'Mira Hale';
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        title        โ”‚  author   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ The Silent Orchard  โ”‚ Mira Hale โ”‚
โ”‚ The Last Lighthouse โ”‚ Mira Hale โ”‚
โ”‚ Murder at Mill Lane โ”‚ Mira Hale โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

You can now filter with any of the six comparisons, on numbers or text. Next, combining tests, so you can ask questions like, cheap fantasy books.

Key takeaways
  • = <> < > <= >=
  • <> means not equal
  • COUNT(*) counts rows
Next up: Combining Tests with AND โ€” both must be true