↓Skip to main content
  1. LeetCode/

LeetCode 595: Big Countries

·1 min· ·
LeetCode Easy Database
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2021-10-10

  • Submissions: Accepted ×3

Approach
#

A country is big if either condition holds, so join them with OR (SQL Template 1). The problem says at least, so use >=.

My Attempts
#

All three passed: OR, OR in the other order, and a UNION of two queries. They used > although the problem says “at least”; the boundary should be >=, which the tests happened not to probe.

Solution
#

SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;

Complexity
#

One table scan, O(n).

Mistakes to Avoid
#

  • “At least” means >=.
  • Two queries with UNION can be faster with indexes, but OR is usually fine.

Interview Explanation
#

I filter with WHERE for an area of at least three million or a population of at least twenty-five million and return the name, population and area.

Related

LeetCode 197: Rising Temperature
·1 min
LeetCode Easy Database
LeetCode 197 note: self-join Weather on a one-day date difference with today warmer; compare dates with DATEDIFF, never ids
LeetCode 596: Classes With at Least 5 Students
·1 min
LeetCode Easy Database
LeetCode 596 note: group by class with HAVING COUNT(DISTINCT student) >= 5; older test data had duplicate rows, so count distinct students
LeetCode 620: Not Boring Movies
·1 min
LeetCode Easy Database
LeetCode 620 note: odd id and description not ‘boring’, ordered by rating from high to low
LeetCode 175: Combine Two Tables
·1 min
LeetCode Easy Database
LeetCode 175 note: people without an address must still appear, so LEFT JOIN, leaving city and state NULL when unmatched
LeetCode 181: Employees Earning More Than Their Managers
·1 min
LeetCode Easy Database
LeetCode 181 note: self-join Employee on managerId = id to pair employees with managers, then compare salaries
LeetCode 182: Duplicate Emails
·1 min
LeetCode Easy Database
LeetCode 182 note: group by email and keep groups with HAVING COUNT(*) > 1