How to include surplus value in the result of a LIMIT SQL query?

Asked

Viewed 35 times

1

Hello,

I have a global HDI table and would like to consult the 10 countries with the highest HDI and to include Brazil (which is not among the 10 largest ones) in this consultation. As a result, there would be 11 countries. How can I do this query? It is possible to add the ranking position (for example: 1, Niger; 2, Central African Republic; 3, Eritrea...)

My initial consultation is as follows (not including Brazil):

SELECT pais, ano, valor
  FROM  idh
  ORDER BY valor
  LIMIT 10;

Utilizo Postgresql 13.1.

  • 1

    There is question of this already on the site, I will see if I locate the link. just do order by pais != "Brasil", valor or a 2 selects UNION, one with WHERE pais="BRASIL" and the other with the current query (picking 9, for example)

  • I didn’t remember to use the UNION and it worked right. Thank you.

1 answer

0

One simple way to do this is by using Union.

SELECT pais, ano, valor
    FROM  idh
   ORDER BY valor
   LIMIT 10
 union
SELECT pais, ano, valor
    FROM  idh
 where pais = 'BRASIL'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.