SELECT *
from (
         SELECT fefe.id, fefe.fire_equipment_id,
                (SELECT wes.code from wl_equipment_specific wes where wes.id = fefe.fire_equipment_id) as deviceCode,
                fefe.equipment_id,
                (SELECT fe.`code` from f_equipment fe where fefe.equipment_id = fe.id)                 as mainDeviceCode
         from f_equipment_fire_equipment fefe
         ) temp
where temp.deviceCode is not null
  and temp.mainDeviceCode is not null