Module 5 Numeric Comparisons (>, <, >=, <=)

Module 5

Numeric Comparisons (>, <, >=, <=)

A step-by-step, hands-on SQL practice lesson — with a live query playground

SELECT * FROM products WHERE price > 50;

In Module 4, you learned to filter rows with exact matches using = and !=. But most real questions aren't about exact matches — they're about ranges: "products over $50," "employees under 5 years tenure," "orders with fewer than 10 items left in stock." This lesson covers the four numeric comparison operators — >, <, >=, and <= — and includes a live, in-browser SQL playground so you can run every example for real.

Introduction

What You'll Learn — and Why It Matters

🎯 Objective

By the end of this lesson, you will be able to:

  • Filter numeric columns using > (greater than) and < (less than).
  • Use >= and <= to include the boundary value itself.
  • Understand the difference between price > 50 and price >= 50 in practice.
  • Combine numeric comparisons with column selection, aliases, and sorting.

⚠️ What to Avoid

Common mistakes
  • Wrapping numbers in quotes, like WHERE price > '50' — this can cause the comparison to behave like a text comparison instead of a numeric one.
  • Mixing up > and >= and accidentally excluding the exact boundary value you wanted included.
  • Trying to use a comparison operator on a text column, which can produce confusing, unexpected results.

💡 Important to Know

Before you start

Comparison operators work on numbers, dates, and even some text (alphabetically), but they behave most predictably on numeric columns — which is the focus of this lesson. Also remember: rows with a NULL value in the compared column are never included in the result, no matter which operator you use.

Requirements

What You'll Need Before Starting

Operating SystemWindows, macOS, or Linux — this lesson works identically on all of them.
Database ServerMySQL installed and running. See How to Install MySQL if you haven't set it up yet.
Optional but recommendedXAMPP with phpMyAdmin, for a visual way to run queries. See How to Install XAMPP.
Prior knowledgeComfortable with SELECT, aliases, and WHERE with = / != from Module 1.

No installation needed for the live playground further down this page — it runs entirely in your browser.

Practice

Step-by-Step Practice

Follow along in order — each step builds on the one before it.

1 Open MySQL (CLI or phpMyAdmin)

Open a terminal and connect with the MySQL command-line client, or open phpMyAdmin at http://localhost/phpmyadmin/.

mysql -u root -p

The basic syntax pattern for this lesson looks like this:

SELECT column1, column2 FROM table_name WHERE numeric_column > value;

2 Create a Database and Verify It

CREATE DATABASE module5_practice;
SHOW DATABASES;
USE module5_practice;

Now create a table and verify it was created:

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  category VARCHAR(50),
  price DECIMAL(8,2),
  stock INT
);
SHOW TABLES;
DESCRIBE products;

3 Insert Sample Data

INSERT INTO products (id, name, category, price, stock) VALUES
(1, 'Wireless Mouse', 'Electronics', 15.99, 40),
(2, 'Mechanical Keyboard', 'Electronics', 79.99, 12),
(3, '27-inch Monitor', 'Electronics', 249.00, 5),
(4, 'Desk Lamp', 'Furniture', 22.50, 30),
(5, 'Office Chair', 'Furniture', 149.00, 8),
(6, 'Notebook Pack', 'Stationery', 4.99, 100);

4 Greater Than (>)

Find every product priced above $50.

SELECT name, price FROM products WHERE price > 50;

5 Less Than (<)

Find products running low — fewer than 10 left in stock.

SELECT name, stock FROM products WHERE stock < 10;

6 Greater Than or Equal (>=)

Include the boundary value itself — everything $149 and up.

SELECT name, price FROM products WHERE price >= 149;

7 Less Than or Equal (<=)

Include the boundary here too — 8 units or fewer.

SELECT name, stock FROM products WHERE stock <= 8;

8 Combine with Column Selection and an Alias

Bring in what you learned in Module 2.

SELECT name AS product_name, price AS unit_price
FROM products
WHERE price > 20;

9 Combine with Sorting

Filter first, then sort the filtered results from highest price to lowest.

SELECT name, price
FROM products
WHERE price >= 20
ORDER BY price DESC;

Troubleshooting

Common Problems and Solutions

Problem 1: Comparison gives unexpected results with quoted numbers

You wrote WHERE price > '50' and the results looked off.

SolutionNumbers don't need quotes. Write WHERE price > 50 — quoting a number can cause MySQL to compare it as text in some situations, which sorts differently than you'd expect.

Problem 2: A row you expected is missing from the results

You used price > 149 but expected the $149 chair to show up too.

Solution> excludes the exact boundary value — only >= includes it. If you want "149 and above," use >= instead of >.

Problem 3: Comparing a text column with > or < gives odd results

You tried WHERE name > 5 or something similar and got confusing output.

SolutionComparison operators do work on text, but alphabetically — not numerically. Only use >, <, >=, and <= on text columns when you actually mean alphabetical order; otherwise, stick to numeric or date columns.

Problem 4: An alias from SELECT doesn't work inside WHERE

You aliased price AS unit_price and then tried WHERE unit_price > 50, and got an "unknown column" error.

SolutionSame rule as previous modules — WHERE can't see aliases created in the same SELECT. Use the real column name, price, inside WHERE.

Problem 5: Rows with missing (NULL) values silently disappear

A product with no stock value entered doesn't show up in either stock < 10 or stock >= 10.

SolutionThis is expected — NULL can never satisfy a comparison operator, since it represents an unknown value. If you need to include those rows too, add OR stock IS NULL to your condition.

Conclusion

What You Should Take Away

You should now be able to filter numeric columns by range — pulling everything above, below, or at a specific threshold — and you understand the subtle but important difference between >/< and their "or equal to" counterparts.

In Module 6, we'll combine everything so far by chaining multiple conditions together with AND and OR — letting you ask more precise, real-world questions in a single query.

Homework

Homework & Quiz

📝 Homework Task

Using the products table from this lesson, write your own query that:

  • Selects name and stock only.
  • Filters for products where stock is less than or equal to 10.
  • Sorts the results from lowest stock to highest.

Try it yourself first — then check it against the live playground below.

🧪 Quiz: Test Your Understanding (10 Questions)

Pick one answer per question, then click Submit to see your score instantly.

1. Which operator means "greater than"?

2. WHERE price >= 100 includes rows where price equals exactly 100?

3. Do you need quotes around a number in a WHERE comparison?

4. Which operator would you use to find items with 5 or fewer in stock?

5. What happens to rows with a NULL value in the compared column?

6. SELECT * FROM products WHERE price < 50; returns:

7. Comparison operators can be used on:

8. Can WHERE reference an alias created in the same SELECT statement?

9. Which clause would you add to sort filtered results by price, highest first?

10. WHERE stock <= 8 — does a product with exactly 8 in stock appear in the results?

0 / 10

Try It Live

🧪 Live SQL Playground

A real SQLite database running entirely in your browser (via WebAssembly) — nothing is sent to a server. Practice numeric comparisons on the products table below, or pick an example query to get started.

products id INTEGER PK
name TEXT
category TEXT
price REAL
stock INTEGER
Loading SQL engine…
Run a query to see results here.

FAQ

Frequently Asked Questions

1. What's the difference between > and >=?

> excludes the exact boundary value; >= includes it. price > 50 skips a $50 item, while price >= 50 includes it.

2. Can I use these operators on dates?

Yes. Date columns compare chronologically, so WHERE order_date > '2026-01-01' returns every order placed after January 1, 2026.

3. Do I need quotes around numbers in a comparison?

No. Quotes are for text values only. Write WHERE price > 50, not WHERE price > '50'.

4. What happens with NULL values in a comparison?

They're always excluded. NULL represents an unknown value, so it can never be judged greater than, less than, or equal to anything.

5. Can I combine two comparisons on the same column?

Yes, using AND — for example, WHERE price > 20 AND price < 100 — which we'll cover in detail in Module 6.

6. Can I use > and < on text columns?

Yes, but they compare alphabetically rather than numerically — useful in some cases, but easy to misuse by accident.

7. Does ORDER BY work together with WHERE and comparisons?

Yes. WHERE filters the rows first, and ORDER BY then sorts whatever's left — you can combine them freely in the same query.

8. Is there a shorthand for "between two values"?

Yes — the BETWEEN operator, for example WHERE price BETWEEN 20 AND 100, is a cleaner way to write a range than two separate comparisons.

9. Why did my comparison return zero rows even though matching data exists?

Double-check you're not accidentally quoting a number, comparing the wrong column, or excluding the boundary value with the wrong operator — these are the most common causes.

10. Is the live playground on this page a real database?

Yes — it's a genuine SQLite database compiled to WebAssembly, running entirely inside your browser tab. No data leaves your device, and nothing is saved once you close the page.

Post a Comment

Previous Post Next Post