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, cnt_sites.cnt_sites, t.age from tags t left join ( select st.theme, count(*) cnt_sites from site_tags st inner join sites s on st.site = s.id where s.status=1 group by st.theme ) cnt_sites on cnt_sites.theme = t.id inner join tag_groups tg on t.groupx = tg.id where t.id in () order by t.theme