Postgresql – Postgres, return 0 rows if any row matches condition, otherwise return all rows

postgresql

I have a table with ten rows. One of the columns is a boolean, let's just say for argument's sake:

email text
is_activated boolean

I'd like to write a query that returns zero rows if is_activated is true for any of the rows. If none of the rows have is_activated set to true, I'd like to return all of the rows in the table.

How can I do that?

Best Answer

SELECT * FROM table_name 
WHERE NOT EXISTS 
(
  SELECT 1 FROM table_name WHERE is_activated IS TRUE
);