Your solution may cause performance problems if there will be larger number of records as there will be not index used in this JOIN statement: LEFT join (SELECT DISTINCT id_task FROM PROJECT_HAS_TASK)b on (a.id_task = b.id_task); Probably you should try something using the EXISTS statement like this SELECT `PROJECT`.`id_project`, CASE WHEN EXISTS (SELECT * FROM `PROJECT_HAS_TASK` WHERE `PROJECT`.`id_project` = `PROJECT_HAS_TASK`.`id_project`) THEN 1 ELSE 0 END as HasProject FROM `TASK`;