I am doing this HackerRank SQL question and cannot understand why this OFFSET does not work.This is for MySQL syntax:
SELECT ROUND(LAT_N, 4)
FROM STATION
ORDER BY LAT_N
LIMIT 1 OFFSET (SELECT FLOOR(COUNT(*) / 2) FROM STATION);
But if I manually calculate the number of rows to skip, get the number, and put that number after OFFSET, it works perfectly. But it’s not dynamic. Do you know why?
2
Answers
The limit and offset must be constants. From the documentation:
You can use a prepared statement:
Here I use integer arithmetic
count(*) div 2
instead of float arithmeticFLOOR(COUNT(*)/2)