Skip to content

Instantly share code, notes, and snippets.

@jhannah
Last active March 15, 2018 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhannah/30e54bbf9963a9ca4bb0af8edfd4a04b to your computer and use it in GitHub Desktop.
Save jhannah/30e54bbf9963a9ca4bb0af8edfd4a04b to your computer and use it in GitHub Desktop.
PostgreSQL 10.3 coalesce vs. greatest
drop table t;
create table t(c1 int, c2 int);
insert into t values (1, null), (2, null), (null, 7), (null, 4), (3, 20);
# select * from t;
c1 | c2
----+----
1 |
2 |
| 7
| 4
3 | 20
(5 rows)
# select max(coalesce(c1,c2)) from t;
max
-----
7
# select max(greatest(c1, c2)) from t;
max
-----
20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment