SQL Exercise: Country ID and number of cities in country has
SQL SORTING and FILTERING on HR Database: Exercise-25 with Solution
25. From the following table, write a SQL query to count the number of cities in each country. Return country ID and number of cities.
Sample table : locations
Sample Solution:
SELECT country_id, COUNT(*)
FROM locations
GROUP BY country_id;
Sample Output:
country_id | count ------------+------- CH | 2 MX | 1 US | 4 AU | 1 IT | 2 Ox | 1 JP | 2 CA | 2 DE | 1 NL | 1 SG | 1 CN | 1 UK | 2 IN | 1 BR | 1 (15 rows)
Code Explanation:
The said query in SQL which aggregates data from the 'locations' table, grouping by the "country_id" column. It returns the values for each group of locations in the same country as below:
country_id: the country id of the locations
COUNT(*): the number of locations in the group/country.
Therefore the final result will be a list of country_id values and the count of locations for each country.
Relational Algebra Expression:
Relational Algebra Tree:
Practice Online

Query Visualization:
Duration:

Rows:

Cost:

Contribute your code and comments through Disqus.
Previous SQL Exercise: Jobs done by two or more for more than 300 days.
Next SQL Exercise: Find employees managed by the manager.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
Difference between natural join and inner join
One significant difference between INNER JOIN and NATURAL JOIN is the number of columns returned-
Consider:
TableA TableB +------------+----------+ +--------------------+ |Column1 | Column2 | |Column1 | Column3 | +-----------------------+ +--------------------+ | 1 | 2 | | 1 | 3 | +------------+----------+ +---------+----------+
The INNER JOIN of TableA and TableB on Column1 will return
SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1); SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1;
+------------+-----------+---------------------+ | a.Column1 | a.Column2 | b.Column1| b.Column3| +------------------------+---------------------+ | 1 | 2 | 1 | 3 | +------------+-----------+----------+----------+
The NATURAL JOIN of TableA and TableB on Column1 will return:
SELECT * FROM TableA NATURAL JOIN TableB +------------+----------+----------+ |Column1 | Column2 | Column3 | +-----------------------+----------+ | 1 | 2 | 3 | +------------+----------+----------+
Ref: https://bit.ly/3AG5CId
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook