JOIN
is unnecessary here, as it seems that you are just looking to check that the "country" has atleast one corresponding row in the statistic_detail
table. This query can be written in a more performant way using EXISTS()
on a Correlated Subquery.
Try the following query; it should be able to use the country_idx
index in the statistic_detail
table):
SELECT cd.country_name
FROM country_detail AS cd
WHERE EXISTS (SELECT 1 FROM statistic_detail AS sd
WHERE sd.country = cd.country_code)
If country_name
values are not UNIQUE in the country_detail
table, you can additionally utilize DISTINCT
clause to get rid of duplicates:
SELECT DISTINCT cd.country_name
FROM country_detail AS cd
WHERE EXISTS (SELECT 1 FROM statistic_detail AS sd
WHERE sd.country = cd.country_code)