How efficient is this query. It’s selecting from the same table twice from within a sub-query.
SELECT DISTINCT country, rowCount FROM
(
SELECT *, (
SELECT COUNT(id) FROM tbl WHERE hide != 1 AND country = y.country
) AS rowCount FROM tbl y
) AS x
HAVING rowCount >= 1
ORDER BY x.country ASC
I have indexed the table where required and the query executes in “0.000 sec” (says HeidiSQL) so it must be good, however is there anything that would improve the structure of the query?
Seems like it can be rewritten as a single query:
Playground: http://sqlfiddle.com/#!2/28978/1