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.theme, description, groupx tag_group_id, tag_group_name , center_location_id center, map_zoom zoom, ( select count(*) cnt_sites from site_tags st2 inner join sites s2 on = where s2.status=1 and st2.theme = group by st2.theme ) cnt_sites , t.age from tags t inner join tag_groups tg on t.groupx = where in () order by t.theme