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, 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 = where s.status=1 group by st.theme ) cnt_sites on cnt_sites.theme = inner join tag_groups tg on t.groupx = where in () order by t.theme