0
0
PostgreSQLquery~5 mins

Standard comparison operators in PostgreSQL

Choose your learning style9 modes available
Introduction

We use comparison operators to check if values are equal, bigger, smaller, or different. This helps us find or filter data in a database.

Finding all customers older than 30 years.
Checking if a product price is less than 100 dollars.
Selecting orders that were made on a specific date.
Filtering employees who are not in a certain department.
Comparing two columns to find matching records.
Syntax
PostgreSQL
value1 = value2
value1 <> value2
value1 != value2
value1 > value2
value1 < value2
value1 >= value2
value1 <= value2

= means equal to.

<> and != both mean not equal to.

Examples
Selects employees who are exactly 25 years old.
PostgreSQL
SELECT * FROM employees WHERE age = 25;
Finds products with a price greater than 50.
PostgreSQL
SELECT * FROM products WHERE price > 50;
Gets orders that are not shipped yet.
PostgreSQL
SELECT * FROM orders WHERE status <> 'shipped';
Finds users who signed up on or before January 1, 2023.
PostgreSQL
SELECT * FROM users WHERE signup_date <= '2023-01-01';
Sample Program

This creates a table of employees, adds three people, then selects those who are 30 or older.

PostgreSQL
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, age INT);
INSERT INTO employees (name, age) VALUES ('Alice', 30), ('Bob', 25), ('Carol', 35);
SELECT name FROM employees WHERE age >= 30;
OutputSuccess
Important Notes

Use = for exact matches, and <, >, <=, >= for range checks.

Both <> and != mean 'not equal' and can be used interchangeably.

Comparison operators work with numbers, text, dates, and more.

Summary

Comparison operators help filter data by comparing values.

= means equal, <> or != means not equal.

<, >, <=, >= check if values are smaller, bigger, or equal.