<div dir="ltr">Hi,<div><br></div><div>I wrote an SQL query wherein the result set differs with or without "order by" clause. I think it should not be so.</div><div><br></div><div>The correct result set, without order by:</div><div><br></div><div><span style="font-family:monospace"><font color="#000000" style="background-color:rgb(255,255,255)">+------+-----+-----+-----+
<br>| vker | t4  | t8  | o4  |
<br>+------+-----+-----+-----+
<br>|    3 |   4 |  11 |   7 |
<br>|    5 |  23 |  23 |   0 |
<br>|    6 |  20 |  15 |  -5 |
<br>|   13 |  13 |  14 |   1 |
<br>|   24 |  -3 | -10 |  -7 |
<br>|   25 |  21 |  23 |   2 |
<br>|   26 |  24 |  16 |  -8 |
<br>|   28 |   7 |  -2 |  -9 |
<br>|   42 |   3 |  -2 |  -5 |
<br>|   43 |   7 |  -6 | -13 |
<br>|   46 |  -5 |  -7 |  -2 |
<br>|   48 |   0 | -14 | -14 |
<br>|   49 |  -1 |  -2 |  -1 |
<br>|   53 |  -5 | -13 |  -8 |
<br>|   67 |  10 |   8 |  -2 |
<br>|   71 |  -3 | -11 |  -8 |
<br>|   77 |   8 |   5 |  -3 |
<br>|   88 |  23 |  16 |  -7 |
<br>|   89 |   9 |   1 |  -8 |
<br>|   91 | -10 | -23 | -13 |
<br>|   94 |  13 |  -7 | -20 |
<br>|   97 |   4 |  -2 |  -6 |
<br>|  105 |   0 | -13 | -13 |
<br>+------+-----+-----+-----+
<br>23 <span style="font-weight:bold">rows</span> <span style="font-weight:bold">in</span> <span style="font-weight:bold">set</span> (0.000 <span style="font-weight:bold">sec</span>)</font><br></span></div><div><span style="font-family:monospace"><font color="#000000" style="background-color:rgb(255,255,255)"><br></font></span></div><div><span style="font-family:monospace"><font color="#000000" style="background-color:rgb(255,255,255)">The wrong one with "order by o4" clause:</font></span></div><div><span style="font-family:monospace"><font color="#000000" style="background-color:rgb(255,255,255)"><br></font></span></div><div><span style="font-family:monospace"><span style="background-color:rgb(255,255,255)"><font color="#000000">+------+----+----+----+
<br>| vker | t4 | t8 | o4 |
<br>+------+----+----+----+
<br>|   26 | 24 | 16 |  0 |
<br>|  105 |  0 |  0 |  0 |
<br>|   28 |  7 |  0 |  0 |
<br>|   42 |  3 |  0 |  0 |
<br>|   43 |  7 |  0 |  0 |
<br>|   46 |  0 |  0 |  0 |
<br>|   48 |  0 |  0 |  0 |
<br>|   49 |  0 |  0 |  0 |
<br>|   53 |  0 |  0 |  0 |
<br>|   67 | 10 |  8 |  0 |
<br>|   71 |  0 |  0 |  0 |
<br>|   77 |  8 |  5 |  0 |
<br>|    5 | 23 | 23 |  0 |
<br>|   88 | 23 | 16 |  0 |
<br>|    6 | 20 | 15 |  0 |
<br>|   89 |  9 |  1 |  0 |
<br>|   91 |  0 |  0 |  0 |
<br>|   24 |  0 |  0 |  0 |
<br>|   94 | 13 |  0 |  0 |
<br>|   97 |  4 |  0 |  0 |
<br>|   13 | 13 | 14 |  1 |
<br>|   25 | 21 | 23 |  2 |
<br>|    3 |  4 | 11 |  7 |
<br>+------+----+----+----+
<br>23 <span style="font-weight:bold">rows</span> <span style="font-weight:bold">in</span> <span style="font-weight:bold">set</span> (0.001 <span style="font-weight:bold">sec</span>) </font></span><br></span></div><div><span style="font-family:monospace"><span style="background-color:rgb(255,255,255)"><font color="#000000"><br></font></span></span></div><div><span style="font-family:monospace"><span style="background-color:rgb(255,255,255)"><font color="#000000">The SQL:</font></span></span></div><div><span style="font-family:monospace"><span style="background-color:rgb(255,255,255)"><font color="#000000"><br></font></span></span></div><div><span style="font-family:monospace"><font color="#000000">select 'Without order';
<br>select t4.vker, (t4.ossz-t4.fid) as t4, (t8.ossz-t8.fid) as t8, ((t8.ossz-t8.fid) - (t4.ossz-t4.fid)) as o4
<br>from ogyval as t4, ogyval as t8
<br>where t4.val = '2014' and t8.val = '2018' and t4.vker = t8.vker
<br>;</font></span></div><div><span style="font-family:monospace"><font color="#000000"><br>select 'With order';
<br>select t4.vker, (t4.ossz-t4.fid) as t4, (t8.ossz-t8.fid) as t8, ((t8.ossz-t8.fid) - (t4.ossz-t4.fid)) as o4
<br>from ogyval as t4, ogyval as t8
<br>where t4.val = '2014' and t8.val = '2018' and t4.vker = t8.vker
<br>order by o4;</font><br></span></div><div><span style="font-family:monospace"><font color="#000000"><br></font></span></div>The data are from parliamentary elections in Hungary in 2014 and 2018 in some election districts ("vker" column, 23 districts of 106). Ell, fid and ossz are percents. I want to compare district results between 2014 and 2018. Ell is max result of maverick parties, Ossz is sum result of maverick parties, fid is the only governing party (Fidesz).<div><br></div><div>I attached the mysqldump of table ogyval. It contains version numbers. My system is 10.9 debian, mariadb packages are from standard binary debian repository:</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">deb <a href="http://deb.debian.org/debian">http://deb.debian.org/debian</a> buster main contrib non-free
</span><br>deb <a href="http://deb.debian.org/debian">http://deb.debian.org/debian</a> buster-updates main contrib non-free
<br>deb <a href="http://security.debian.org/">http://security.debian.org/</a> buster/updates main contrib non-free<br></span></div><div><br></div><div>Thank you,</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>Laszlo</div></blockquote><div><br></div></div>