johnlemon
johnlemon

Reputation: 21509

Postgresql Special Order BY

I need to use an ORDER BY on a table but keep some special rows on top. The rows that have name='developer' for example.

Upvotes: 2

Views: 227

Answers (3)

Frank Heikens
Frank Heikens

Reputation: 127556

You could also do the ORDER BY without the usage of a CASE:

ORDER BY 
  name = 'developer' DESC,
  name ASC;

Upvotes: 3

kevpie
kevpie

Reputation: 26118

Something like this might work for you.

SELECT * FROM table ORDER BY CASE WHEN name = 'developer' THEN 0 ELSE 1 END, name

Upvotes: 1

mechanical_meat
mechanical_meat

Reputation: 169524

ORDER BY CASE WHEN name = 'developer' THEN '0' ELSE name END

Upvotes: 5

Related Questions