we want to have the second biggest element. We first use ANY to exclude the biggest one. Then we use all to select the biggest. However when we run this query, it shows the biggest and not the second one. Why?
SELECT *
FROM bestelling
WHERE totaalprijs > ALL
(
SELECT totaalprijs
FROM bestelling
WHERE totaalprijs < ANY
(
SELECT totaalprijs
FROM bestelling
)
)
elements in the table:
157.00
5.00
82.80
15.00
20.00
20.00
The problem is the “>” in your outermost query. If you break this down in language, from the inside out, you’re saying:
If you just want the second biggest value, you can replace that outer clause with a MAX statement:
Or, if you want all records that bear that “second biggest” value, you can use: