Query: 

```
select 
    c1.cname, c2.sumlength as totallength 
from 
    country c1 
JOIN 
    (select 
         country, sum(length) as sumlength 
     from 
         neighbour 
     group by 
         country 
     order by 
         sum(length)) c2 ON c1.cid = c2.country;

```

Output: 

```
    cname    | totallength 
-------------+-------------
 San Marino  |          39
 Albania     |         605
 Lesotho     |         909
 Afghanistan |        5529
 Algeria     |        6343

```

I want to extract the maximum of the total length and the cname.

Desired output: 

```
    cname    |     max
-------------+------------- 
 Algeria     |        6343

```

I tried executing:

```
select 
    c1.cname, max(c2.sumlength) as totallength 
from 
    country c1 
JOIN 
   (select 
        country, 
        sum(length) as sumlength 
    from 
        neighbour 
    group by 
        country 
    order by 
        sum(length)) c2 ON c1.cid = c2.country;

```

But I keep encountering this error: 

> 
> ERROR: column "c1.cname" must appear in the GROUP BY clause or be used in an aggregate function
> 
> 
> 

Any help would be greatly appreciated.