SQL ET POSTGRE 5.27 : logical operators

So far we have used the WHERE clause with an equality test against a value. PostgreSQL also supports the comparison operators >, <, >= and <=, so we can filter on values that are greater or smaller than a threshold. Let's start with a simple selection: SELECT titre, duree FROM film; displays every film and its running time. We can then keep only the films lasting more than two hours with WHERE duree > 120; adding the = sign gives us duree >= 120, which also includes films of exactly 120 minutes such as Spirited Away and The Wizard of Oz.

From integers to dates and strings

We can flip the comparison to get films shorter than two hours with duree < 120, or duree <= 120 to include the two 120-minute films we just saw. Comparison operators are also very useful with dates: SELECT * FROM film WHERE date_de_sortie > '1999-12-31'; returns all films released in the 21st century (note the English date format in this table). Using < we get the films from the 20th century instead, and again we can add = to include an exact match if needed.
SELECT titre, duree FROM film WHERE duree > 120;
SELECT * FROM film WHERE date_de_sortie > '1999-12-31';
SELECT * FROM film WHERE pays > 'Etats-Unis';   -- compares alphabetically
  • Works for integers (durations, ages...) and dates.
  • Works for strings too — using alphabetical ordering.
  • Use >= / <= to include the boundary.
These operators can even be used on strings — though it is not very meaningful. SELECT * FROM film WHERE pays > 'Etats-Unis'; returns countries whose first letter sits after E in the alphabet. Using < gives the opposite, with countries before E. You can add = to include United States itself in the result. It is rarely useful to compare strings this way, but now you know it is possible. Next up: a challenge to put SELECT, WHERE and logical operators into practice.

Summary

This lesson introduces SQL logical operators (>, <, >=, <=) used in WHERE clauses to filter data beyond simple equality checks. Through practical examples on a film database, the instructor demonstrates how these operators work on numeric columns (duration), date columns (release date), and even text columns (country), comparing values alphabetically. The key takeaway is that logical operators provide flexible data filtering across different data types, with the option to include boundary values using >= and <=.

Key points

  • Logical operators (>, <, >=, <=) are essential for filtering data in WHERE clauses beyond simple equality
  • These operators work on numeric columns (e.g., duration > 120 minutes) to compare integer or decimal values
  • Date columns can be filtered using logical operators (e.g., release_date > '1999-12-31' for 21st century films)
  • Adding an equal sign (>=, <=) includes the boundary value in results, unlike > and < which exclude it
  • String columns can use logical operators, comparing values alphabetically—though less common in practice
  • PostgreSQL interprets string comparisons alphabetically, allowing queries like country > 'United States'

FAQ

How do I select all films with a duration greater than or equal to 120 minutes?

Use the query: SELECT title, duration FROM film WHERE duration >= 120. The >= operator includes films that are exactly 120 minutes as well as longer ones.

Can I use logical operators on date columns, and how do I format the date?

Yes, logical operators work on date columns. In PostgreSQL, use the format 'YYYY-MM-DD' in your WHERE clause, for example: WHERE release_date > '1999-12-31' to select films from the 21st century.

What's the practical difference between > and >=?

The > operator excludes the exact boundary value and returns only greater values, while >= includes the boundary value. For example, duration > 120 excludes 120-minute films, but duration >= 120 includes them.