SELECT cpc.competency_id as id FROM `ohrm_performance_competency_profile_competency` as cpc INNER JOIN( SELECT MIN(cp.id) as competency_profile_id FROM `ohrm_performance_competency_profile` as cp LEFT JOIN `ohrm_performance_competency_profile_mapping` as cpm ON cp.id = cpm.competency_profile_id WHERE cpm.job_title_id = #jobTitleId# AND cpm.subunit_id = #departmentId#) AS cpf ON cpc.competency_profile_id = cpf.competency_profile_id INNER JOIN `ohrm_performance_competency`as c ON c.id = cpc.competency_id WHERE c.deleted_at IS NULL UNION SELECT id FROM `ohrm_performance_competency`as c1 WHERE c1.appraisal_id = #appraisalId# AND c1.deleted_at IS NULL