0

I tried this, and it works,

SELECT name    
FROM    
  (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
  )    
WHERE LENGTH >= ALL    
  (SELECT LENGTH FROM    
    (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
    )    
  ) 

but my final code would be like this:

SELECT a.name,    
  a.length    
FROM    
  (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
  ) a,    
  geo_river b,    
  encompasses c    
WHERE a.length >= ALL    
  (SELECT a2.LENGTH    
  FROM    
    (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
    ) a2    
  )    
AND a.name      = b.river    
AND b.country   = c.country   
AND c.continent = 'America'

this is really complicated. Is there an easy way to let

(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)

be river, so I don't need to use this

(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)

two times.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
user92322
  • 29
  • 5

3 Answers3

0

If you want to simplify your code writing, you can use WITH:

with viewA as (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL )
SELECT a.name,    
  a.length    
FROM    
  viewA a,    
  geo_river b,    
  encompasses c    
WHERE a.length >= ALL    
  (SELECT a2.LENGTH    
  FROM    
    viewA a2    
  )    
AND a.name      = b.river    
AND b.country   = c.country   
AND c.continent = 'America'
Community
  • 1
  • 1
Aleksej
  • 22,443
  • 5
  • 33
  • 38
0

Using a single table scan:

SELECT name,
       length
FROM   (
  SELECT name,
         length,
         RANK() OVER ( ORDER BY length DESC ) AS rnk
  FROM   river
)
WHERE rnk = 1;

So your code would then be:

SELECT a.name,    
       a.length    
FROM   (
         SELECT name,
                length
         FROM   (
           SELECT name,
                  length,
                  RANK() OVER ( ORDER BY length DESC ) AS rnk
           FROM   river
         )
         WHERE rnk = 1
       ) a
       INNER JOIN
       geo_river b
       ON ( a.name    = b.river )
       INNER JOIN
       encompasses c
       ON ( b.country = c.country )   
WHERE  c.continent = 'America';
MT0
  • 143,790
  • 11
  • 59
  • 117
0

I don't think you need to filter out null lengths as they won't show up if you subset on length (i.e. when comparing NULL values without using the NVL function the comparison will always evaluate to false and not show the row). So something simple such as:

Select a.name, a.length 
from river a, geo_river b,  encompasses c   
WHERE a.length > 0 
AND a.name      = b.river  
AND b.country   = c.country 
AND c.continent = 'America' 
;

Will do the trick

Roger Cornejo
  • 1,507
  • 1
  • 8
  • 7