The tables we have
SHOW TABLES
Tables_in_test | |
---|---|
0 | nobel |
1 | world |
GROUP BY is used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
-- format
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Number of countries in each continent
SELECT continent,
COUNT(name)
FROM world
GROUP BY continent
continent | COUNT(name) | |
---|---|---|
0 | Asia | 47 |
1 | Europe | 42 |
2 | Africa | 53 |
3 | Caribbean | 11 |
4 | South America | 13 |
5 | Eurasia | 2 |
6 | Oceania | 14 |
7 | North America | 11 |
List each continent and the name of the country that comes first alphabetically.
Using GROUP BY
and MIN
SELECT continent,
MIN(name)
FROM world
GROUP BY continent
ORDER BY continent;
continent | MIN(name) | |
---|---|---|
0 | Africa | Algeria |
1 | Asia | Afghanistan |
2 | Caribbean | Antigua and Barbuda |
3 | Eurasia | Armenia |
4 | Europe | Albania |
5 | North America | Belize |
6 | Oceania | Australia |
7 | South America | Argentina |
Top ten nobel prize winner countries
SELECT birth_country,
COUNT(birth_country) AS prize_count
FROM nobel
WHERE birth_country IS NOT NULL
GROUP BY birth_country
ORDER BY prize_count DESC
LIMIT 10
birth_country | nobel_count | |
---|---|---|
0 | United States of America | 276 |
1 | United Kingdom | 88 |
2 | Germany | 70 |
3 | France | 53 |
4 | Sweden | 30 |
5 | Japan | 29 |
6 | Russia | 20 |
7 | Netherlands | 19 |
8 | Italy | 18 |
9 | Canada | 18 |
Number of nobel laureates born in particular month
SELECT Month(birth_date) AS 'month',
COUNT(*)
FROM nobel
GROUP BY month
ORDER BY month
Month | COUNT(*) | |
---|---|---|
0 | NaN | 29 |
1 | 0.0 | 1 |
2 | 1.0 | 72 |
3 | 2.0 | 65 |
4 | 3.0 | 77 |
5 | 4.0 | 72 |
6 | 5.0 | 80 |
7 | 6.0 | 87 |
8 | 7.0 | 75 |
9 | 8.0 | 84 |
10 | 9.0 | 84 |
11 | 10.0 | 89 |
12 | 11.0 | 72 |
13 | 12.0 | 82 |
Looks like the data has wrong incorrect birth date information for 30 laureates. Lets check.
SELECT year, full_name, birth_date
FROM nobel
WHERE Month(birth_date) IS NULL
OR Month(birth_date) = 0.0
year | full_name | birth_date | |
---|---|---|---|
0 | 1904 | Institut de droit international (Institute of ... | None |
1 | 1910 | Bureau international permanent de la Paix (Per... | None |
2 | 1917 | Comité international de la Croix Rouge (Intern... | None |
3 | 1938 | Office international Nansen pour les Réfugiés ... | None |
4 | 1944 | Comité international de la Croix Rouge (Intern... | None |
5 | 1947 | Friends Service Council (The Quakers) | None |
6 | 1947 | American Friends Service Committee (The Quakers) | None |
7 | 1954 | Office of the United Nations High Commissioner... | None |
8 | 1960 | Albert John Lutuli | 1898-00-00 |
9 | 1963 | Comité international de la Croix Rouge (Intern... | None |
10 | 1963 | Ligue des Sociétés de la Croix-Rouge (League o... | None |
11 | 1965 | United Nations Children's Fund (UNICEF) | None |
12 | 1969 | International Labour Organization (I.L.O.) | None |
13 | 1977 | Amnesty International | None |
14 | 1981 | Office of the United Nations High Commissioner... | None |
15 | 1985 | International Physicians for the Prevention of... | None |
16 | 1988 | United Nations Peacekeeping Forces | None |
17 | 1995 | Pugwash Conferences on Science and World Affairs | None |
18 | 1997 | International Campaign to Ban Landmines (ICBL) | None |
19 | 1999 | Médecins Sans Frontières | None |
20 | 2001 | United Nations (U.N.) | None |
21 | 2005 | International Atomic Energy Agency (IAEA) | None |
22 | 2006 | Grameen Bank | None |
23 | 2007 | Intergovernmental Panel on Climate Change (IPCC) | None |
24 | 2009 | Venkatraman Ramakrishnan | None |
25 | 2011 | Saul Perlmutter | None |
26 | 2011 | Saul Perlmutter | None |
27 | 2012 | European Union (EU) | None |
28 | 2013 | Organisation for the Prohibition of Chemical W... | None |
29 | 2015 | National Dialogue Quartet | None |
As per wikipedia, Albert John Lutuli does not have exact birth date. And the rest of the records with NULL entries are of organisations. So our Group by query has given correct output