You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') order by t.theme' at line 11 SELECT t.id, t.theme, description, groupx tag_group_id, tg.group tag_group_name , center_location_id center, map_zoom zoom, ( select count(*) cnt_sites from site_tags st2 inner join sites s2 on st2.site = s2.id where s2.status=1 and st2.theme = t.id group by st2.theme ) cnt_sites , t.age from tags t inner join tag_groups tg on t.groupx = tg.id where t.id in () order by t.theme