0

I'm a SQL beginner and solving a hacker rank problem and had to find the median of a column "lat_n" from table "station"

Here is the solution I came up with:

SET @counter := (SELECT COUNT(lat_n) FROM station);
SELECT CASE
WHEN (@counter%2 !=0) THEN (SELECT ROUND(lat_n,4) FROM station order by lat_n LIMIT floor(@counter/2),1)
ELSE (SELECT ROUND(AVG(lat_n),4) FROM station order by lat_n LIMIT floor(@counter/2),2)
END
FROM station;

This is the error message I am receiving: ERROR 1064 (42000) at line 6: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(@counter/2),1)

ELSE (SELECT ROUND(AVG(lat_n),4) FROM station order by lat_n LIM' at line 2

Ideas on how to fix the code and feedback on the code would be much appreciated :)

0 Answers0