cswork / libraryOpen practice workspace

Coding questions · Capital One

Identify country with highest sunny-day probability

This question evaluates data manipulation competencies including SQL aggregation, NULL handling, case-insensitive condition matching, date-level deduplication, probability calculation, and multi-criteria ordering.

Write SQL to find the country with the highest probability that a day is sunny. Use the schema and sample data below. Rules: consider a day sunny for a country if any observation for that country on that date has condition = 'Sunny' (case-insensitive); exclude dates where all observations for that country are NULL; compute probability = sunny_days / total_observed_days; only include countries with at least 5 observed days; tie-break by (1) higher probability, (2) higher total_observed_days, then (3) country_name ascending; return one row with columns (country_name, sunny_days, total_days, sunny_day_prob rounded to 3 decimals). Schema:

  • countries(country_id INT PRIMARY KEY, country_name VARCHAR)
  • weather_obs(obs_id INT PRIMARY KEY, country_id INT, obs_date DATE, condition VARCHAR, temp_c INT) Sample data (ASCII):
Table: countries
+------------+--------------+
| country_id | country_name |
+------------+--------------+
| 1 | USA |
| 2 | Canada |
| 3 | Japan |
+------------+--------------+
Table: weather_obs
+--------+------------+------------+-----------+--------+
| obs_id | country_id | obs_date | condition | temp_c |
+--------+------------+------------+-----------+--------+
| 1 | 1 | 2025-06-01 | Sunny | 30 |
| 2 | 1 | 2025-06-02 | Cloudy | 24 |
| 3 | 1 | 2025-06-03 | sunny | 28 |
| 4 | 1 | 2025-06-03 | Rain | 22 |
| 5 | 2 | 2025-06-01 | Rain | 18 |
| 6 | 2 | 2025-06-02 | Sunny | 21 |
| 7 | 2 | 2025-06-02 | Cloudy | 20 |
| 8 | 2 | 2025-06-03 | NULL | 19 |
| 9 | 2 | 2025-06-04 | Sunny | 23 |
| 10 | 3 | 2025-06-01 | Cloudy | 25 |
| 11 | 3 | 2025-06-02 | Sunny | 27 |
| 12 | 3 | 2025-06-03 | Sunny | 29 |
| 13 | 3 | 2025-06-04 | NULL | 26 |
| 14 | 1 | 2025-06-04 | Sunny | 31 |
| 15 | 2 | 2025-06-05 | Sunny | 24 |
| 16 | 3 | 2025-06-05 | Rain | 22 |
+--------+------------+------------+-----------+--------+

Write a single SQL query (standard SQL) that implements the rules above and returns the required row.

Identify the Country with the Highest Sunny-Day Probability

Using PostgreSQL, treat a country-date as sunny when any observation on that date has condition = 'Sunny' case-insensitively. Exclude country-dates whose observations all have NULL condition. For each country with at least five observed dates, compute sunny_days / total_observed_days. Return exactly one row for the best country, ordered by higher probability, then higher total observed days, then country_name ascending. Return country_name, sunny_days, total_days, and sunny_day_prob rounded to three decimals.

Table countries

columntypeconstraints
country_idINTEGERPRIMARY KEY
country_nameTEXTNOT NULL

Sample data:

country_idcountry_name
1USA
2Canada
3Japan
4Norway
5France

Table weather_obs

columntypeconstraints
obs_idINTEGERPRIMARY KEY
country_idINTEGERNOT NULL
obs_dateDATENOT NULL
conditionTEXT
temp_cINTEGER

Sample data:

conditioncountry_idobs_dateobs_idtemp_c
Sunny12025-06-01130
Cloudy12025-06-02224
sunny12025-06-03328
Rain12025-06-03422
Sunny12025-06-04531
Rain12025-06-05621
NULL12025-06-06720
Rain22025-06-01818
Sunny22025-06-02921
Cloudy22025-06-021020
NULL22025-06-031119
Sunny22025-06-041223
Sunny22025-06-051324
Rain22025-06-061417
Cloudy22025-06-071519
Cloudy32025-06-011625
Sunny32025-06-021727
Sunny32025-06-031829
NULL32025-06-041926
Rain32025-06-052022
Sunny32025-06-062128
Sunny32025-06-072230
Sunny42025-06-012314
Sunny42025-06-022415
Sunny42025-06-032516
Sunny42025-06-042617
Sunny52025-06-012723
SUNNY52025-06-022824
Rain52025-06-032920
Sunny52025-06-043025
Cloudy52025-06-053122
Sunny52025-06-063226

Expected output

country_namesunny_daystotal_dayssunny_day_prob
France460.667

Hints

  1. First collapse multiple weather observations to one row per country and date.
  2. COUNT(condition) distinguishes an all-NULL day from an observed non-sunny day.
  3. Apply the minimum-day rule before the final deterministic LIMIT 1.