SQL

595. Big Countries and Difference between Where and Having

There is a table World

+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.

Write a SQL solution to output big countries’ name, population and area.

For example, according to the above table, we should output:

+--------------+-------------+--------------+
| name | population | area |
+--------------+-------------+--------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+

Logic: Pretty straight-forward, use “where” clause

Solution:

Addition information: The difference between “Where” and “Having”:

Where is used in filtering records from a result, used before any groupings; while “Having” is used after grouping. See the syntax as below:

SELECT
FROM
WHERE
GROUP BY
HAVING

--

--

--

My homepage to record my thought processes for solving SQL and Algorithm questions

Recommended from Medium

Data Cleaning: Hidden Aspect of Good Data Scientist

Research Paper On A Analysis Pdf

Building a State-of-the-Art Data Science Platform

Making Sense of NoSQL from a Relational Database Perspective

Which languages should you learn for data analytics?

Canada Election 2019 Forecast

Let's Model! Using Random Forest to Predict UCL Reconstruction

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jen-Li Chen

Jen-Li Chen

In love with telling stories with data

More from Medium

SQL Server Indexes: Interview Questions

MOST IMPORTANT SQL QUERIES TO KNOW

Core SQL Commands