Skip to main content

Command Palette

Search for a command to run...

Comparison Operators in WHERE (PostgreSQL)

Published
2 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

We’ve already seen that the WHERE keyword allows us to filter rows returned by a query.

In simple words:

WHERE checks each row one by one and decides whether it should appear in the final result.

Let’s now look at different comparison operators we can use inside a WHERE clause.


Recap: Filtering with WHERE

Consider this query:

select
  name,
  area
from cities
where area > 4000;

What happens here is very simple:

  • PostgreSQL looks at each row

  • It checks the area value

  • Only rows with area > 4000 are returned

That’s why we only see cities like Tokyo and Shanghai.


Equality Check (=)

We can also check if a column value is exactly equal to something.

For example, let’s find the city with an area equal to 8223:

select
  name,
  area
from cities
where area = 8223;

Important Note

In SQL:

  • = is not assignment

  • it is a comparison operator

We are not changing anything — we are just checking a condition.


Result

This query returns:

  • Tokyo

Because Tokyo is the only city with an area of 8223.


Not Equal (!=)

We can also check for values that are not equal.

select
  name,
  area
from cities
where area != 8223;

This returns:

  • all cities except Tokyo

Alternative Not Equal Operator (<>)

PostgreSQL also supports another operator for “not equal”:

<>

So this query:

select
  name,
  area
from cities
where area <> 8223;

Produces the same result as !=.

Both are valid — you may see either in real projects.


How These Comparisons Work

One very important thing to remember:

The comparison in the WHERE clause is evaluated for every single row.

PostgreSQL does this internally:

  1. Take one row

  2. Apply the condition

  3. Keep or discard the row

  4. Move to the next row

This happens until all rows are checked.


Common Comparison Operators

Here are some comparison operators we’ve seen so far:

  • = → equal to

  • != → not equal to

  • <> → not equal to

  • > → greater than

  • < → less than

  • >= → greater than or equal to

  • <= → less than or equal to

These operators form the foundation of filtering in SQL.