O
O
Omniverse2016-04-15 01:09:40
MySQL
Omniverse, 2016-04-15 01:09:40

SQL query. How to choose the number of cities by region?

Hello.
There is a country table with fields Code (records like: RUS, BEL, KAZ) and Region (records like: Asia, Europe, Africa). There is also a city table with fields ID, Name (records like: Moscow, Minsk, Beijing) and CountryCode (records like: RUS, BEL, KAZ).
How to display the resulting table, where in one column are the names of the regions, and in the second the number of cities in each region?
Well, that is:
Region | Cities
---------+----------
Europe |... 100
Africa ..|.... 80
Asia .....|.... 300

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2016-04-15
@EreminD

Select R.Code as Region, count(C.ID) as Cities
from Region R
Inner join city C
on R.Code = C.CountryCode
group by R.Code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question