In this PostgreSQL post explores all of the comparison operators used to test for equality and inequality, as well as the more advanced operators.
Comparison operators are used in the WHERE clause to determine which records to select. Here is a list of the comparison operators that you can use in PostgreSQL:
Comparison Operator | Description |
---|---|
= | Equal |
> | Not Equal |
!= | Not Equal |
> | Greater Than |
>= | Greater Than or Equal |
Less Than | |
= | Less Than or Equal |
IN ( ) | Matches a value in a list |
NOT | Negates a condition |
BETWEEN | Within a range (inclusive) |
IS NULL | NULL value |
IS NOT NULL | Non-NULL value |
LIKE | Pattern matching with % and _ |
EXISTS | Condition is met if subquery returns at least one row |
Let's explore the comparison operators that you can use in PostgreSQL.
In PostgreSQL, you can use the = operator to test for equality in a query.
For example:
In this example, the SELECT statement above would return all rows from the employees table where the first_name is equal to Julie.
In PostgreSQL, there are two ways to test for inequality in a query. You can use either the > or != operator.
For example, we could test for inequality using the > operator, as follows:
In this example, the SELECT statement would return all rows from the employees table where the first_name is not equal to Julie.
Or you could also write this query using the != operator, as follows:
Both of these queries would return the same results.
You can use the > operator in PostgreSQL to test for an expression greater than.
In this example, the SELECT statement would return all rows from the products table where the product_id is greater than 50. A product_id equal to 50 would not be included in the result set.
In PostgreSQL, you can use the >= operator to test for an expression greater than or equal to.
In this example, the SELECT statement would return all rows from the products table where the product_id is greater than or equal to 50. In this case, product_id equal to 50 would be included in the result set.
You can use the operator in PostgreSQL to test for an expression less than.
In this example, the SELECT statement would return all rows from the inventory table where the inventory_id is less than 25. An inventory_id equal to 25 would not be included in the result set.
In PostgreSQL, you can use the = operator to test for an expression less than or equal to.
In this example, the SELECT statement would return all rows from the inventory table where the inventory_id is less than or equal to 25. In this case, n inventory_id equal to 25 would be included in the result set.