 <p>I may be wrong but I do not see any condition in your query that checks if "latitude IS NOT NULL" or "longitude IS NOT NULL" or "coordinates = '1'". This means that your query should return all records that have coordinates or don't have coordinates. For the latter, of course the SELECT output will not have any values in 'latitude', 'longitude' and 'distance' fields.</p> <p>I think you do not need to bypass the distance calculations for 'coordinates = 0' because that will anyways return NULL, but if you want to you can do it using CASE function, like below:</p> <pre><code>SELECT price, address, state, thumbnail, name, provider, category, latitude, longitude, ( CASE WHEN coordinates = '1' THEN ( 6371 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) ELSE 0 ) AS distance FROM records ... ... </code></pre>