0

I have a datatable with a column "year" and a column "country". Each country can appear more than one year, ranging from 1900 to 2021. How to select only countries that appear in all the year (1900 to 2021)?

This is my code in selecting which is the earliest and latest year:

SELECT
    MIN(year) AS EarliestYear,  
    MAX(year) AS LatestYear
FROM 
    owid_energy_data;
2
  • Please include table columns and sample data. Commented Nov 1, 2022 at 4:07
  • What DBMS you are using? Commented Nov 1, 2022 at 4:19

2 Answers 2

2

Generalizing off of Extreme_Tough's answer:

SELECT sub.country
  FROM owid_energy_data
 GROUP BY country
HAVING COUNT(DISTINCT year) = (SELECT MAX(year) - MIN(year) + 1
                                 FROM owid_energy_data) range

The purpose of the DISTINCT is to prevent selecting countries where for some years they appear multiple times, but are missing for other years.

Sign up to request clarification or add additional context in comments.

Comments

1

Group by country and take the count of rows. Find out if the count of rows match the number of years in the range (2021 - 1900 + 1)

SELECT country, count(year) from owid_energy_data
GROUP BY country HAVING count(year)= 2021 - 1900 + 1 ;

1 Comment

You don't need the count(year) in your SELECT if it's not going to provide anything new/useful~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.