Fix SQL logic operation