clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2591607 fiddles created (45736 in the last week).

CREATE TABLE `student_points` ( `student_no` int(9) NOT NULL, `level_id` int(11) NOT NULL, `points` int(3) NOT NULL, `timestamp` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() ); INSERT INTO `student_points` (`student_no`, `level_id`, `points`, `timestamp`) VALUES (12345678, 1, 80, '2021-01-15 16:07:43'), (12345678, 2, 25, '2021-01-13 17:15:10'), (12345678, 3, 90, '2021-01-17 22:41:55'), (12345678, 4, 90, '2021-01-17 22:41:55'), (40145489, 1, 85, '2021-01-14 21:50:58'), (40204123, 1, 80, '2021-01-12 15:37:11'), (40204123, 2, 75, '2021-01-12 15:38:06'), (40204123, 3, 30, '2021-01-13 22:13:13'), (40213894, 1, 90, '2021-01-14 21:52:00'), (40213894, 2, 95, '2021-01-17 22:42:50'), (40213894, 4, 100, '2021-01-17 22:42:50'), (40283947, 1, 57, '2021-01-14 21:50:14'), (40334891, 1, 95, '2021-01-14 21:54:25'), (40829379, 1, 70, '2021-01-14 21:50:38'), (45216227, 1, 97, '2021-01-16 19:05:16');
 hidden batch(es)


SELECT student_no, SUM(points) FROM ( (SELECT student_no, SUM(points), 1 orderby FROM student_points a HAVING SUM(points) > (SELECT SUM(points) FROM student_points WHERE student_no = 40204123) ORDER BY SUM(points) ASC LIMIT 3) UNION ALL (SELECT student_no, SUM(points), 2 orderby FROM student_points a WHERE student_no = 40204123) UNION ALL (SELECT student_no, SUM(points), 3 orderby FROM student_points a HAVING SUM(points) <= (SELECT SUM(points) FROM student_points WHERE student_no = 40204123) AND student_no <> 40204123 ORDER BY SUM(points) DESC LIMIT 3) ) t1 ORDER BY orderby ASC , SUM(points) DESC
Unknown column 'points' in 'field list'
 hidden batch(es)