SQL ET POSTGRE 5.26 : Conditions WHERE

In this video we learn how to select data and filter the result with the WHERE clause. We already encountered WHERE when modifying tables; now we use it to retrieve only the rows we are interested in. The syntax is: SELECT * FROM <table> WHERE <column> = <value>. Note that in PostgreSQL the value must be enclosed in single quotes for strings. We work on the film table from our film database.

Single and multiple conditions

Let's start with one condition: SELECT * FROM film WHERE pays = 'Japon';. The result shows only the films from Japan. We can also combine two conditions with AND: SELECT * FROM film WHERE pays = 'Japon' AND realisateur_id = 21; — note that integer values do not need quotes. The result is films from Japan whose director has id 21.
SELECT * FROM film WHERE pays = 'Japon';

SELECT * FROM film
WHERE pays = 'Japon' AND realisateur_id = 21;

SELECT * FROM film
WHERE pays = 'Etats-Unis' AND realisateur_id = 10 AND duree = 150;
  • = compares a column to a value (strings between quotes).
  • AND — both conditions must be true.
  • OR — at least one condition must be true.
  • You can stack as many conditions as you need.
If we replace AND with OR we get all films from Japan plus all films whose director id is 21, even if they are not Japanese. AND requires both conditions to be true; OR requires at least one. We can also chain three or more conditions: SELECT * FROM film WHERE pays = 'Etats-Unis' AND realisateur_id = 10 AND duree = 150; returns the film made in the United States by director 10 whose running time is 150 minutes. In the next video we will look at logical operators such as greater than and less than.

Summary

This lesson teaches how to filter and select specific data from a PostgreSQL table using the WHERE condition. The video covers the basic syntax `SELECT * FROM table WHERE column = value`, with emphasis on proper quote formatting for text values in PostgreSQL. Multiple practical examples demonstrate filtering by country (Japan), combining conditions with AND/OR operators, and stacking multiple filters to narrow results progressively.

Key points

  • WHERE clause syntax and usage in SELECT statements for filtering table data
  • Text values in PostgreSQL require single quotes around the value
  • AND operator combines multiple conditions (all must be true); OR operator allows either condition to be true
  • Multiple filters can be stacked together to create complex queries that match specific criteria
  • Difference between AND and OR behavior: AND narrows results while OR expands results to include both conditions

FAQ

What is the basic syntax for filtering data with WHERE in PostgreSQL?

The basic syntax is `SELECT * FROM table_name WHERE column_name = 'value'`. Text values must be enclosed in single quotes in PostgreSQL.

How do I filter by multiple conditions at the same time?

Use the AND operator to combine conditions (all must be true) or the OR operator to include rows matching either condition. For example: `SELECT * FROM film WHERE country = 'japan' AND director_id = 21`.

What is the difference between using AND versus OR in a WHERE clause?

AND requires both conditions to be true to include a row in the results, narrowing the output. OR includes rows where either condition is true, expanding the output to match more rows.