> makes the easy stuff easy, and the hard stuff doable
I like SQL, but I disagree. It also makes easy stuff hard.
Given many numbers (billions), how would you find the 1st, 3rd and 5th highest values?
I like SQL, but I disagree. It also makes easy stuff hard.
Given many numbers (billions), how would you find the 1st, 3rd and 5th highest values?
Does any other language implement this in a better way? While also still giving a O(n) time complexity and O(1) space? I may have to implement my own top-n heapsort then, or my own top-n insertion sort.
SELECT * FROM (
SELECT
row_number() AS n, num
FROM t
ORDER BY num DESC
LIMIT 5
) top5 WHERE n IN (1, 3, 5);
This is assuming we do not care about ties. If we care about ties it gets a bit messier but not that bad. SELECT
nth_value(num, 1) OVER ORDER BY num,
nth_value(num, 3) OVER ORDER BY num,
nth_value(num, 5) OVER ORDER BY num
FROM t