Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL: LEAST() returns multiple rows...?

A co-worker just came to me with a puzzling SQL query:

(essentially)

SELECT LEAST(id) FROM tableA A, tableB B WHERE a.name = b.name(+)

The result set returned lists three numbers however:

LEAST(id)
--------------
621
644
689

(all being IDs that meet the query as if it lacked the LEAST function all together)

Why? =)

like image 998
zaczap Avatar asked Dec 09 '22 21:12

zaczap


1 Answers

LEAST(x,y,...) is not an aggregate function. It works only on its parameters. The function you want is MIN(x).

For each record, you're running LEAST(id), which will always return id. If you were passing LEAST more parameters, you would see different results. For example, LEAST(5,6,7) = 5. LEAST always returns the smallest of its parameters, whereas MIN returns the smallest of every record.

like image 113
Welbog Avatar answered Dec 13 '22 14:12

Welbog