HTLWienWest/Group By Having 01

From SQLZoo
Jump to navigation Jump to search

Einleitung

Schalten Sie in den Einstellungen (Zahnrad oben rechts ) auf die SQL Engine : MySQL um!
In dieser Übung werden Sie auf einer Tabelle mit GROUP BY Informationen zusammenfassen.
Unter der Fragestellung sehen Sie, wie Ihr Ergebnis aussehen soll.

Die Tabelle world


+-------------------+---------+-------+----------+------------+----------------+
|name               |continent|area   |population|gdp         |capital         |
+-------------------+---------+-------+----------+------------+----------------+
|Afghanistan        |Asia     | 652230|  25500100| 20364000000|Kabul           |
|Albania            |Europe   |  28748|   2821977| 12044000000|Tirana          |
|Algeria            |Africa   |2381741|  38700000|207021000000|Algiers         |
|Andorra            |Europe   |    468|     76098|  3222000000|Andorra la Vella|
|Angola             |Africa   |1246700|  19183590|116308000000|Luanda          |
|Antigua and Barbuda|Caribbean|    442|     86295|  1176000000|St. John's      |
|...                |...      |...    |...       |...         |...             |                                                 |

Abfragen zur Tabelle world

Geben Sie die Gesamtfläche der Länder aus, die ein 'c' an der 4ten Stelle im namen haben.

Select sum(area) as Gesamtfläche from world where name like '___c%';
Gesamtfläche|
------------+
       79477|

Wieviele Kontinente gibt es laut den Daten in der Tabelle world?

select count(distinct (continent)) 'Kontinente' from world;
Kontinente|
----------+
         8|

Wieviele Länder gibt es pro Kontinent?
Sortieren Sie nach der Anzahl absteigend und geben Sie nur die Kontinente aus, die an der letzten Stelle ein a haben.

select count(name) as 'Anzahl Länder', continent from world where continent like '%a' group by continent order by 1 desc;


Anzahl Länder|continent    |
-------------+-------------+
           53|Africa       |
           47|Asia         |
           14|Oceania      |
           13|South America|
           11|North America|
            2|Eurasia      |


Die Fläche eines Landes ist in km² gespeichert. Geben Sie pro Kontinent die Bevölkerungsdichte (=Menschen pro km²) aus. Sortieren Sie nach Kontinent alphabetisch aufsteigend und beschränken Sie die Abfrage auf die Kontinente Amerika (Nord und Süd), Europa, Eurasia.
Runden Sie die Zahlen auf 4 Stellen.

select round(sum(population)/sum(area),4) as 'Menschen/km²', continent from world where continent like '%eur%' or continent like '%america%' group by continent order by continent;
Menschen/km²|continent    |
------------+-------------+
      8.6865|Eurasia      |
     70.0297|Europe       |
     23.2644|North America|
     22.9799|South America|

Manchmal gibt es Hauptstädte (capital) die gleich heißen wie das Land. Geben Sie Anzahl der Länder pro Kontinent aus, bei denen die Länder gleich heißen wie die Hauptstadt.

select count(name) 'Gleicher Name', continent
from world
where capital = name
group by continent;


Gleicher Name|continent|
-------------+---------+
            1|Africa   |
            1|Asia     |
            2|Europe   |

Geben Sie nur die Namen jener Kontinente aus, deren Fläche größer als 15 Millionen aber kleiner als 25 Millionen ist.

select continent 
from world 
group by continent
having sum(area) between 15e6 and 25e6
order by 1;
continent    |
-------------+
Eurasia      |
North America|
South America|
DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects