So, we already know how to use the SELECT command. Well, at least we’ve learned how to pull data from a database, pick the columns we want, and write queries that start turning raw data into useful info. Now it’s time to add some “filters” to our magic palette. Yup, today we’re talking about filtering data using comparison operators like =, >, <, and <>.
When you’re working with big tables, you probably don’t want to look at every single row (unless you’re into chaos). To pick out just the rows you need, SQL uses the WHERE keyword. It lets you set up conditions that each row has to meet to make it into your results. And for those conditions, you use comparison operators.
Filtering Syntax with WHERE
SELECT column1, column2
FROM table
WHERE condition;
Simple structure: we add WHERE after the FROM table and write a specific condition that the rows have to match.
For example, let’s pull data from the students table:
SELECT name, age
FROM students
WHERE age > 18;
This query grabs only those students (name and age) who are older than 18. Easy, right? Now let’s dive into the world of comparison operators.
Main Comparison Operators
SQL has a few comparison operators that help you filter data the way you want. You probably know them from math, but let’s clarify what’s what. Here’s a list of the most common ones:
=— Equals.<>— Not equal (alternative:!=in some DBMS, but it’s better to use<>since it’s the SQL standard).>— Greater than.<— Less than.>=— Greater than or equal to.<=— Less than or equal to.
Comparison Operators Table
| Operator | Meaning | Example | Result |
|---|---|---|---|
= |
Equals | age = 20 |
Returns rows where age is 20 |
<> |
Not equal | age <> 20 |
Returns rows where age is not 20 |
> |
Greater than | age > 18 |
Returns rows where age is greater than 18 |
< |
Less than | age < 18 |
Returns rows where age is less than 18 |
>= |
Greater than or equal to | age >= 18 |
Returns rows where age is 18 or more |
<= |
Less than or equal to | age <= 18 |
Returns rows where age is 18 or less |
Examples of Using Comparison Operators
Example 1: Getting students older than 20
Here’s our table:
| id | name | age |
|---|---|---|
| 1 | Alex Lin | 22 |
| 2 | Anna Song | 19 |
| 3 | Otto Art | 21 |
SELECT name, age
FROM students
WHERE age > 20;
Here we set the condition age > 20 to pick only those students whose age is over 20. If there’s a student who’s 19, sorry, they won’t show up in the results.
In our case, we’ll get this result:
| name | age |
|---|---|
| Alex Lin | 22 |
| Otto Art | 21 |
Student Anna Song didn’t make the cut because she’s only 19.
Example 2: Getting employees with a salary of at least 50,000
Here’s our employees table:
| first_name | last_name | salary |
|---|---|---|
| Julia | Kim | 48000 |
| Marcus | Lee | 52000 |
| Anya | Patel | 50000 |
| Ethan | Zhou | 61000 |
And here’s our SQL query:
SELECT first_name, last_name, salary
FROM employees
WHERE salary >= 50000;
This query will return all employees whose salary is equal to 50,000 or more than that amount. Equality counts too!
| first_name | last_name | salary |
|---|---|---|
| Marcus | Lee | 52000 |
| Anya | Patel | 50000 |
| Ethan | Zhou | 61000 |
Example 3: Getting products with a price less than 100
| product_name | price |
|---|---|
| USB Cable | 85 |
| Wireless Mouse | 150 |
| Notebook | 60 |
| Mechanical Pencil | 45 |
SELECT product_name, price
FROM products
WHERE price < 100;
If you run an online store, this query lets you pick out all products that cost less than 100 (like for a promo “Everything for 99!”).
| product_name | price |
|---|---|
| USB Cable | 85 |
| Notebook | 60 |
| Mechanical Pencil | 45 |
Example 4: Excluding users with a certain id
SELECT id, username
FROM users
WHERE id <> 1;
This query picks all users except the one whose id is 1. The <> operator says “give me everyone who’s not this value.”
Table users:
| id | username |
|---|---|
| 1 | admin |
| 2 | elena.gray |
| 3 | tom.bishop |
| 4 | aria.noble |
Result of the query:
| id | username |
|---|---|
| 2 | elena.gray |
| 3 | tom.bishop |
| 4 | aria.noble |
Common Mistakes When Filtering with Comparison Operators
When you’re just starting out writing queries with filtering, mistakes are super common. Here are a couple of classic cases to watch out for:
Problem 1: Typo in column names
If you mess up a column name in your query, PostgreSQL will throw an error. For example:
SELECT name, age
FROM students
WHERE ages > 18; -- Error: column "ages" does not exist
Always double-check the exact column names before you write your query.
Problem 2: Wrong comparison operator
Sometimes newbies mix up the operators by accident. For example:
SELECT name, age
FROM students
WHERE age => 18; -- Error: invalid operator
The right syntax is >=, not =>.
Problem 3: Filtering by values that don’t exist
If you try to filter rows by a value that’s not in the table, the query will run but return nothing. For example:
SELECT name
FROM students
WHERE age = 999; -- Result: 0 rows
This isn’t a syntax error, but it can be confusing if you expected to see data.
Practical Use
Filtering data with comparison operators is used in all sorts of scenarios:
- Data analysis: like picking all customers who spent more than 10,000 rupees.
- Reports: picking employees with above-average salaries.
- Monitoring: tracking users who haven’t been active in the last month.
SQL lets you quickly pull the data you need from huge tables, and comparison operators are your first step on that journey.
GO TO FULL VERSION