Skip to content
Advertisement

Using IF in PostgreSQL

I need to check (here for an example) if the average earnings of DE workers are higher than IT workers. I tried using

but it doesn’t seem to work. Is there any way to do this in PostgresSQL (I am using version 14)

first_name last_name country earnings
Andrea Pfeiffer DE 800
Eufrosina Marchesi IT 2975
Elisa Sabbatini IT 2450
Marco Grunewald DE 3000
Doreen Kalb DE 5000
Isidoro Bruno IT 1100
Lucas Mueller DE 3000
Ausonio Loggia IT 1300

Advertisement

Answer

Postgres knows a Boolean type and Boolean expressions will evaluate to a value of that type. So you could simply SELECT the expression.

Or, if you explicitly need strings, use a CASE expression.

And you could even use Postgres’ FILTER to get rid of the sub selects. (That might be a bit faster.)

For a Boolean:

For a string:

Instead of FILTER you could also use a CASE expression as argument to avg() with the same effect.

would become

(and analog for 'IT'). That would also work in many other DBMS, not just Postgres.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement