I want to find out the second most populous country from the partitioned table

39 Views Asked by At

i am using the following query:

select country_name from (SELECT country_name, rank() OVER (ORDER BY TOTAL_pop desc) 
as rk FROM (SELECT country_name, sum(col8) as TOTAL_pop FROM world_bank_data_partitioned GROUP BY country_name)
) SUM_EMP where rk = 2;

and getting the following error:

Error while compiling statement: FAILED: ParseException line 1:197 cannot recognize input near ')' 'SUM_EMP' 'where' in subquery source

Please help me with the same.

1

There are 1 best solutions below

0
On

You can use below SQL. You need to alias each subquery. I name the subquery as rs.

select country_name from 
(SELECT country_name, rank() OVER (ORDER BY TOTAL_pop desc)  as rk FROM 
    (SELECT country_name, sum(col8) as TOTAL_pop FROM world_bank_data_partitioned GROUP BY country_name) rs 
) SUM_EMP where rk = 2;