Datenbanken Loesungen: Unterschied zwischen den Versionen

Aus CCWiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Sandro (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
 
(4 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt)
Zeile 1: Zeile 1:
Die meisten Übungsbeispiele gelöst, hauptsächlich mit '''JOIN''' nicht mit '''WHERE'''. Fügt die '''WHERE''' Queries, wenn möglich hinzu. Gibts bessere Lösung, dann rein damit!!!
Die meisten Übungsbeispiele gelöst, hauptsächlich mit '''JOIN''' nicht mit '''WHERE'''. Fügt die '''WHERE''' Queries, wenn möglich hinzu. Gibts bessere Lösung, dann rein damit!!!


Wie viele Einwohner hat Österreich?
 
 
-- Wie viele Einwohner hat Österreich?
{{SQLML|code=
{{SQLML|code=
SELECT Population FROM Country WHERE NAME = 'Austria';
select population
from country
where name = "austria";
}}
}}


Wie viele Einwohner hat der Kontinent Europa?
 
-- Wie viele Einwohner hat der Kontinent Europa?
{{SQLML|code=
{{SQLML|code=
SELECT SUM(Country.Population) AS EuropePopulation FROM Continent, Encompasses, Country
select sum(population)
WHERE Continent.Name = Encompasses.Continent
from country c
AND Encompasses.Country = Country.Code
join encompasses e on c.Code = e.Country
AND Continent.Name = 'Europe';
where e.Continent ='Europe';  
}}
}}




Welche Flüsse fließen durch Österreich?
 
-- Welche Flüsse fließen durch Österreich?
{{SQLML|code=
{{SQLML|code=
SELECT Country.Name, River.Name FROM Country, Geo_river, River
select gr.River from geo_river gr
WHERE Country.Code = Geo_river.Country
where Country = "A";
AND Geo_river.River = River.Name
 
AND Country.Name = 'Austria';
select * from geo_river
where country = "A";
}}
}}


Welche Flüsse fließen durch Europa (nur den Flussnamen)?


{{SQLML|code=
-- Welche Flüsse fließen durch Europa (nur den Flussnamen)?
SELECT river.Name from continent, encompasses, country, province, geo_river, river
{{SQLML|code=
WHERE
select river
continent.Name = encompasses.Continent
from geo_river gr
AND encompasses.Country = country.Code
join country c on c.Code = gr.Country
AND country.Code = province.Country
join encompasses e on c.Code = e.Country
AND province.Name = geo_river.Province
where e.Continent ='Europe'
AND river.Name = geo_river.River
group by river;
AND continent.Name like 'Europe'
GROUP BY river.Name
ORDER by river.Name
}}
}}


-- Wie viel Prozent der Menschen leben im Kontinent Europa?
{{SQLML|code=
{{SQLML|code=
SELECT river.Name FROM continent
select 100*sum(population) / (select sum(Population) from country) as populationInProzent
INNER JOIN encompasses ON continent.Name = encompasses.Continent
from country c
INNER JOIN country ON country.Code = encompasses.Country
join encompasses e on c.Code = e.Country
INNER JOIN province ON province.Country = country.Code
where e.Continent ='Europe' and e.Percentage > 99;
INNER JOIN geo_river ON geo_river.Province = province.Name
INNER JOIN river ON river.Name = geo_river.River
WHERE continent.Name like 'Europe'
GROUP BY river.Name
ORDER BY river.Name
}}
}}




Wie viel Prozent der Menschen leben im Kontinent Europa?


-- Alle Länder die mit einem A anfangen
{{SQLML|code=
{{SQLML|code=
WITH Worldpercentage AS(
select name from country
SELECT SUM(Population) AS Worldpopulation FROM Country)
where name like "a%";
SELECT SUM(Country.Population), Worldpercentage.Worldpopulation,
100/ Worldpercentage.Worldpopulation * SUM(Country.Population) AS Percentage
FROM Worldpercentage, Continent
INNER JOIN Encompasses ON Continent.Name = Encompasses.Continent
INNER JOIN Country ON Encompasses.Country = Country.Code
WHERE Continent.Name = 'Europe'
}}
}}


Alle Länder die mit einem A anfangen


-- Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl
{{SQLML|code=
{{SQLML|code=
SELECT * FROM country WHERE name like 'A%';
select name, province, population from country
where name like "a%"
order by population desc;
}}
}}




Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl


-- Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge
{{SQLML|code=
{{SQLML|code=
SELECT country.name, province.name as provincename, province.population FROM country
select r.name, r.length, c.Name 
INNER JOIN province ON country.code = province.country
from geo_river gr
ORDER BY country.name, province.Population desc;
join river r on gr.River = r.name
join country c on gr.Country = c.Code
join encompasses e on c.Code = e.Country
where e.Continent = 'Europe' -- and e.Percentage > 50
group by name
order by length desc;
}}
}}




Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge


-- Alle Inseln im Pazifik mit >50% islamischem Bekenntnis
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT river.name, river.length FROM river
select i.Island,r.Name,r.Percentage
INNER JOIN geo_river ON geo_river.River = river.name
from islandin i
INNER JOIN encompasses ON encompasses.Country = geo_river.Country
join geo_island gi on i.island = gi.island
WHERE encompasses.Continent like 'Europe'
join religion r on gi.Country = r.Country  
ORDER BY river.Length DESC;
where i.sea = 'Pacific Ocean' and r.Name ='Muslim' and Percentage > 50;
}}
}}




Alle Inseln im Pazifik mit >50% islamischem Bekenntnis


-- Alle 3000er, welche in einem Land sind, welches zu mindestens 60% römisch Katholisch ist
{{SQLML|code=
{{SQLML|code=
SELECT country.Name, island.name, religion.Percentage FROM island, islandin, geo_island, religion, country
select m.Name, count(*) from mountain m
WHERE
join geo_mountain gm  on m.name = gm.Mountain
island.Name = islandin.Island AND
join country c on gm.Country = c.Code
islandin.Sea like 'Pacific%' AND
join religion r on r.Country = c.Code
geo_island.Island = island.name AND
where m.Height >= 3000 and r.Name = 'Roman Catholic' and r.Percentage >= 60
geo_island.Country = religion.Country AND
group by m.name
country.code = geo_island.country AND
order by m.name;
religion.Percentage > 0 AND
 
religion.Name = 'Roman catholic';
select * from geo_mountain gm ;
select * from country c ;
}}
-- Alle Länder mit min. einem See mit min. 100 Meter Tiefe und min. einem Berg mit min. 1500 Höhenmeter
{{SQLML|code=
select c.Name, count(*)  from country c
join geo_lake gl on c.Code = gl.Country  
join geo_mountain gm on c.Code = gm.Country  
join lake l on gl.Lake = l.Name
join mountain m on gm.Mountain = m.Name
where l.`Depth` >= 100
and m.Height >= 1500
group by name
order by count(*) desc;
}}
}}




  Alle 3000er, welche in einem Land sind, welches zu mindestens 60% römisch Katholisch ist
-- Einwohnerzahl pro Religion
{{SQLML|code=
select sum(population)
from country c
join religion r on c.Code = r.Country
where r.Name = 'muslim';
 
select r.Name, round( sum(c.Population/100 * r.Percentage),2) as pop from religion r
join country c on r.Country = c.Code
where c.Code = r.Country
group by r.Name
order by pop desc;
}}
 
 
-- Alle Länder, nur die Namen und die Meere dazu (keine Null Werte)
{{SQLML|code=
select c.Name, gs.Sea from country c
join geo_sea gs on c.Code = gs.Country
order by name;
}}
 
 
-- Alle Länder, nur die Namen, und wenn sie haben, das Meer dazu
{{SQLML|code=
select distinct c.Name, gs.Sea  from country c
left join geo_sea gs on c.Code = gs.Country
order by name;
}}
 
 
-- Das Unabhängigkeitsdatum von Ländern die eine Wüste haben und die ethnische Gruppe African
{{SQLML|code=
select distinct p.Independence, c.Name ,d.Name  from politics p
join country c on p.Country = c.Code
join ethnicgroup e on e.Country = c.Code
join geo_desert gd on gd.Country = c.Code
join desert d on gd.Desert = d.Name
where e.Name = 'African';
}}
 


-- Welche Länder haben mehr als 150 Städte?
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT m.name, m.Height, c.Name FROM mountain as m
with citycount as (select co.Name, count(*) as citycount
INNER JOIN geo_mountain as ge ON ge.Mountain = m.Name
from country co
INNER JOIN country as c ON c.Code = ge.Country
join city ci on co.code = ci.Country
INNER JOIN religion as rrr ON rrr.Country = c.Code
group by co.Name
WHERE m.Height >= 3000 AND rrr.Percentage >= 60 AND rrr.Name = 'Roman Catholic'
order by co.Name
ORDER by m.height DESC;
)
select * from citycount
where citycount > 150;
}}
}}




Alle Länder mit mindestens einem See mit mindestens 100 Meter Tiefe und mindestens einem Berg mit mindestens 1500 Höhenmeter
-- Welche Länder haben genau 3 Städte? Wie heißen diese Städte?
{{SQLML|code=
-- Teil 1
select co.Name, count(*) as citycount from country co
join city ci on co.Code = ci.Country
group by co.Name;
-- Teil2
select Name from(
select co.Name, count(*) as citycount from country co
join city ci on co.Code = ci.Country
group by co.Name
)as subquerry
where citycount = 3;
-- Teil3
select Name, citycount from
(select co.Name, count(*) citycount
from country co
join city ci on co.code = ci.Country
group by co.Name
) as tempTable
where citycount = 3
order by Name;
 
with threecity (coName, citycount) as (
select co.Name, count(*) citycount
from country co
join city ci on co.code = ci.Country
group by co.Name
)
select threecity.coName, threecity.citycount from threecity
where threecity.citycount = 3;
}}
 


-- population biggest 3 in america
{{SQLML|code=
{{SQLML|code=
SELECT country.name, lake.name as lake, lake.depth, mountain.name, mountain.height, geo_lake.Province, geo_mountain.Province FROM lake
select * from city ci
INNER JOIN geo_lake ON geo_lake.Lake = lake.Name
join country co on ci.Country =co.Code
INNER JOIN geo_mountain ON geo_mountain.Country = geo_lake.Country
join encompasses e on e.Country = co.Code
INNER JOIN country ON country.Code = geo_lake.Country
join continent cont on cont.Name = e.Continent
INNER JOIN mountain ON mountain.Name = geo_mountain.Mountain
where cont.Name = 'America'
WHERE mountain.Height >= 1500 and lake.Depth >= 100;
order by ci.Population desc
limit 3;
}}
}}




Einwohnerzahl pro Religion


-- biggest mountain in austria
{{SQLML|code=
{{SQLML|code=
SELECT religion.name, sum(country.population * religion.Percentage/100)/1000000 as people FROM religion
select m.Name ,m.Height from mountain m
INNER JOIN country ON country.code = religion.country
join geo_mountain gm on gm.Mountain = m.Name
GROUP BY religion.Name order by people desc;
join country c on c.Code = gm.Country
where c.Name = 'austria'
order by m.Height desc  
limit 1;
}}
}}


Alle Länder, nur die Namen und die Meere dazu (keine Null Werte)


-- bigest mountain in europe limit 1
{{SQLML|code=
{{SQLML|code=
SELECT distinct country.Name, sea.Name FROM country
select c.Name , m.Name ,m.Height from mountain m
INNER JOIN province ON country.Code = province.Country
join geo_mountain gm on gm.Mountain = m.Name  
INNER JOIN geo_sea ON province.Name = geo_sea.Province
join country c on c.Code = gm.Country
INNER JOIN sea ON geo_sea.Sea = sea.Name
join encompasses e on e.Country = c.Code
ORDER BY country.Name;
where e.Continent  = 'Europe' and e.Percentage > 50
order by m.Height desc
limit 1;
}}
}}


Alle Länder, nur die Namen, und wenn sie haben, das Meer dazu


-- concat for group for one biggest mountain in 2 countrys
{{SQLML|code=
{{SQLML|code=
SELECT country.name, geo_sea.Sea FROM country
with mont (Name, Country, height) as (
LEFT JOIN geo_sea ON country.code = geo_sea.country;
select m.name, gm.Country, m.height
from mountain m
join geo_mountain gm on gm.Mountain = m.Name
join encompasses e on e.Country = gm.Country 
where e.Continent  = 'Europe' and e.Percentage > 50
)
select mont.name, mont.height, group_concat(Country.Name SEPARATOR ' / ') countries
from mont
join country on mont.country = country.code
where mont.height = (select max(Height) from mont)
group by mont.name, mont.height;
}}
}}


-- mountains in continents with ranks
{{SQLML|code=
{{SQLML|code=
SELECT distinct country.Name, sea.Name "Sea Name" FROM country
WITH mount as (SELECT continent.Name as continent, mountain.Name, mountain.Height,
LEFT OUTER JOIN geo_sea ON geo_sea.Country = country.Code
RANK() OVER (PARTITION BY continent.Name ORDER BY mountain.Height DESC) AS areaRank from continent, encompasses, country, geo_mountain, mountain
LEFT OUTER JOIN sea ON sea.Name = geo_sea.Sea
WHERE continent.Name = encompasses.Continent
ORDER BY country.Name
AND encompasses.Country = country.Code
AND geo_mountain.country = country.Code
AND mountain.Name = geo_mountain.mountain
ORDER BY mountain.Height desc)
select distinct continent, name, height from mount WHERE mount.areaRank = 1;
}}
}}




Das Unabhängigkeitsdatum von Ländern die eine Wüste haben und die ethnische Gruppe '''African'''


-- mountains, continents , subqueries
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT country.name, politics.Independence, ethnicgroup.name, religion.Name, religion.Percentage FROM country
SELECT DISTINCT big.Continent, m.Name, m.Height
INNER JOIN geo_desert ON geo_desert.Country = country.code
FROM
INNER JOIN politics ON politics.Country = country.code
(
INNER JOIN ethnicgroup ON ethnicgroup.country = country.code
SELECT Continent, MAX(mo.Height) maxH
INNER JOIN religion ON religion.Country = country.Code
FROM mountain mo
WHERE politics.Independence IS NOT NULL AND ethnicgroup.name like 'African';
JOIN geo_mountain gm on mo.Name = gm.Mountain
join encompasses e on gm.Country = e.Country
WHERE e.Percentage >= 50
group by Continent
) big
join encompasses e on big.Continent = e.Continent
JOIN geo_mountain gm2 on e.Country = gm2.Country
JOIN mountain m ON gm2.Mountain = m.Name
WHERE e.Percentage >= 50 AND m.Height = big.maxH
ORDER BY m.Height desc;
}}
}}


Welche Länder haben genau 3 Städte? Welche Länder sind dies?


-- mountains in continents with subqueries
{{SQLML|code=
{{SQLML|code=
WITH sub as (SELECT count(*) AS anzahl, country.Name from country, city WHERE
WITH mont (Continent, Name, Height) AS
country.Code = city.Country
(
GROUP BY country.Code)
SELECT e.Continent, mo.Name, mo.Height
SELECT Name FROM sub WHERE anzahl = 3
FROM mountain mo
JOIN geo_mountain gm on mo.Name = gm.Mountain
join encompasses e on gm.Country = e.Country
WHERE e.Percentage >= 50
)
SELECT DISTINCT big.Continent, mont.Name, mont.Height
FROM
(
SELECT Continent, MAX(mont.Height) maxH
FROM mont
GROUP BY mont.Continent
) AS big
JOIN mont ON mont.Continent = big.Continent AND mont.Height = big.maxH
ORDER BY Height desc;
}}
}}
Welches sind die 3 größten Städte von Amerika (Kontinent)?


-- mountain, continent, heighest
{{SQLML|code=
{{SQLML|code=
SELECT city.Name, city.Population FROM continent, encompasses, country, city
WITH mount as
(
SELECT continent.Name as continent, mountain.Name, mountain.Height,
RANK() OVER (PARTITION BY continent.Name ORDER BY mountain.Height DESC) AS areaRank from continent, encompasses, country, geo_mountain, mountain
WHERE continent.Name = encompasses.Continent
WHERE continent.Name = encompasses.Continent
AND encompasses.Country = country.Code
AND encompasses.Country = country.Code
AND city.Country = country.Code
AND geo_mountain.country = country.Code
AND continent.Name = "America"
AND mountain.Name = geo_mountain.mountain
ORDER BY city.population DESC
ORDER BY mountain.Height desc
LIMIT 3
)
select distinct continent, name, height,areaRank from mount
WHERE mount.areaRank = 1;
}}
 
 
-- count religion in countrys
{{SQLML|code=
-- Teil 1
select c.Name, count(*) as anzahl  from religion r
join country c on c.Code = r.Country
group by c.Name
order by anzahl desc;
 
-- Teil 2
select Name, anzahl from (
select c.Name, count(*) as anzahl  from religion r
join country c on c.Code = r.Country
group by c.Name
order by anzahl desc)
as test
where anzahl = 5;
 
-- Teil 3
WITH religion_count AS (
  SELECT c.Name, COUNT(*) AS anzahl
  FROM religion r
  JOIN country c ON c.Code = r.Country
  GROUP BY c.Name
)
SELECT Name, anzahl
FROM religion_count
WHERE anzahl = (SELECT MAX(anzahl) FROM religion_count);
}}
 
 
 
-- organization count
{{SQLML|code=
with corps as (select o.Abbreviation, o.Country as Founder 
from organization o
where country = 'A'
)
select Abbreviation, count(*) from corps
join ismember i on corps.Abbreviation = i.Organization
group by Abbreviation
;
 
-- organizations
select o.Abbreviation, count(*) anzahlMG
from organization o
join ismember i on o.Abbreviation = i.Organization
where o.Country = 'A'
group by o.Abbreviation
}}
 
-- top 10 languages
{{SQLML|code=
select l.name, round(sum(c.population/100 * l.Percentage), -6) as lang
from `language` l
join country c on l.country = c.code
where c.code = l.country
group by l.name
order by lang desc
limit 10
;
 
select * from `language` l ;
-- languages with subqueries
with lang2 as
(
with languages as (
select
l.name,
round(sum(c.population/100 * l.Percentage)) as lang
from `language` l
join country c on l.country = c.code
group by l.name)
select *, rank () over (order by lang desc) as languageRank
from languages
)
select Name, lang
from lang2
where languageRank in (2,3,5);  -- between 1 and 15; -- languageRank in (2,4,6,8);
}}
}}
Was ist der größte Berg von Österreich? Wie hoch ist er?
Was ist der größte Berg von Europa? Wie hoch ist er?
Was ist der größte Berg pro Kontinent? Wie hoch sind diese?
Welches Land hat am meisten anerkannte Religionen? Wie viel sind es?
Welche Organisationen haben deren Hauptsitz in Österreich? Wie viele Mitglieder haben diese Organisationen?
Was sind die Top 10 Sprachen? Wie viele native Speaker sprechen diese Sprache?

Aktuelle Version vom 28. November 2024, 13:30 Uhr

Die meisten Übungsbeispiele gelöst, hauptsächlich mit JOIN nicht mit WHERE. Fügt die WHERE Queries, wenn möglich hinzu. Gibts bessere Lösung, dann rein damit!!!


-- Wie viele Einwohner hat Österreich?

select population
from country 
where name = "austria";


-- Wie viele Einwohner hat der Kontinent Europa?

select sum(population)
from country c 
join encompasses e on c.Code = e.Country
where e.Continent ='Europe';


-- Welche Flüsse fließen durch Österreich?

select gr.River from geo_river gr
where Country = "A";

select * from geo_river
where country = "A";


-- Welche Flüsse fließen durch Europa (nur den Flussnamen)?

select river
from geo_river gr 
join country c on c.Code = gr.Country
join encompasses e on c.Code = e.Country
where e.Continent ='Europe'
group by river;


-- Wie viel Prozent der Menschen leben im Kontinent Europa?

select 100*sum(population) / (select sum(Population) from country) as populationInProzent
from country c 
join encompasses e on c.Code = e.Country
where e.Continent ='Europe' and e.Percentage > 99;


-- Alle Länder die mit einem A anfangen

select name from country
where name like "a%";


-- Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl

select name, province, population from country
where name like "a%"
order by population desc;


-- Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge

select r.name, r.length, c.Name  
from geo_river gr 
join river r on gr.River = r.name 
join country c on gr.Country = c.Code 
join encompasses e on c.Code = e.Country
where e.Continent = 'Europe' -- and e.Percentage > 50
group by name
order by length desc;


-- Alle Inseln im Pazifik mit >50% islamischem Bekenntnis

select i.Island,r.Name,r.Percentage 
from islandin i
join geo_island gi on i.island = gi.island
join religion r on gi.Country = r.Country 
where i.sea = 'Pacific Ocean' and r.Name ='Muslim' and Percentage > 50;


-- Alle 3000er, welche in einem Land sind, welches zu mindestens 60% römisch Katholisch ist

select m.Name, count(*) from mountain m 
join geo_mountain gm  on m.name = gm.Mountain
join country c on gm.Country = c.Code 
join religion r on r.Country = c.Code
where m.Height >= 3000 and r.Name  = 'Roman Catholic' and r.Percentage >= 60
group by m.name
order by m.name;

select * from geo_mountain gm ;
select * from country c ;

-- Alle Länder mit min. einem See mit min. 100 Meter Tiefe und min. einem Berg mit min. 1500 Höhenmeter

select c.Name, count(*)  from country c
join geo_lake gl on c.Code = gl.Country 
join geo_mountain gm on c.Code = gm.Country 
join lake l on gl.Lake = l.Name 
join mountain m on gm.Mountain = m.Name 
where l.`Depth` >= 100
and m.Height >= 1500
group by name
order by count(*) desc;


-- Einwohnerzahl pro Religion

select sum(population)
from country c 
join religion r on c.Code = r.Country
where r.Name = 'muslim'; 

select r.Name, round( sum(c.Population/100 * r.Percentage),2) as pop from religion r 
join country c on r.Country = c.Code
where c.Code = r.Country
group by r.Name
order by pop desc;


-- Alle Länder, nur die Namen und die Meere dazu (keine Null Werte)

select c.Name, gs.Sea  from country c 
join geo_sea gs on c.Code = gs.Country
order by name;


-- Alle Länder, nur die Namen, und wenn sie haben, das Meer dazu

select distinct c.Name, gs.Sea  from country c 
left join geo_sea gs on c.Code = gs.Country
order by name;


-- Das Unabhängigkeitsdatum von Ländern die eine Wüste haben und die ethnische Gruppe African

select distinct p.Independence, c.Name ,d.Name  from politics p 
join country c on p.Country = c.Code 
join ethnicgroup e on e.Country = c.Code 
join geo_desert gd on gd.Country = c.Code 
join desert d on gd.Desert = d.Name
where e.Name = 'African';


-- Welche Länder haben mehr als 150 Städte?

with citycount as (select co.Name, count(*) as citycount
	from country co
	join city ci on co.code = ci.Country
	group by co.Name 
	order by co.Name
)
select * from citycount
where citycount > 150;


-- Welche Länder haben genau 3 Städte? Wie heißen diese Städte?

-- Teil 1
select co.Name, count(*) as citycount from country co
join city ci on co.Code = ci.Country 
group by co.Name; 
-- Teil2
select Name from(
select co.Name, count(*) as citycount from country co
join city ci on co.Code = ci.Country 
group by co.Name 
)as subquerry
where citycount = 3;
-- Teil3
select Name, citycount from 
	(select co.Name, count(*) citycount
	from country co
	join city ci on co.code = ci.Country
	group by co.Name 	
) as tempTable
where citycount = 3
order by Name;

with threecity (coName, citycount) as (
select co.Name, count(*) citycount
from country co
join city ci on co.code = ci.Country
group by co.Name 
)
select threecity.coName, threecity.citycount from threecity
where threecity.citycount = 3;


-- population biggest 3 in america

select * from city ci 
join country co on ci.Country =co.Code
join encompasses e on e.Country = co.Code
join continent cont on cont.Name = e.Continent
where cont.Name = 'America'
order by ci.Population desc
limit 3;


-- biggest mountain in austria

select m.Name ,m.Height from mountain m 
join geo_mountain gm on gm.Mountain = m.Name 
join country c on c.Code = gm.Country
where c.Name = 'austria'
order by m.Height desc 
limit 1;


-- bigest mountain in europe limit 1

select c.Name , m.Name ,m.Height from mountain m 
join geo_mountain gm on gm.Mountain = m.Name 
join country c on c.Code = gm.Country
join encompasses e on e.Country = c.Code 
where e.Continent  = 'Europe' and e.Percentage > 50
order by m.Height desc 
limit 1;


-- concat for group for one biggest mountain in 2 countrys

with mont (Name, Country, height) as (
select m.name, gm.Country, m.height
from mountain m 
join geo_mountain gm on gm.Mountain = m.Name 
join encompasses e on e.Country = gm.Country  
where e.Continent  = 'Europe' and e.Percentage > 50
)
select mont.name, mont.height, group_concat(Country.Name SEPARATOR ' / ') countries 
from mont 
join country on mont.country = country.code
where mont.height = (select max(Height) from mont)
group by mont.name, mont.height;



-- mountains in continents with ranks

WITH mount as (SELECT continent.Name as continent, mountain.Name, mountain.Height,
RANK() OVER (PARTITION BY continent.Name ORDER BY mountain.Height DESC) AS areaRank from continent, encompasses, country, geo_mountain, mountain
WHERE continent.Name = encompasses.Continent
AND encompasses.Country = country.Code
AND geo_mountain.country = country.Code
AND mountain.Name = geo_mountain.mountain
ORDER BY mountain.Height desc)
select distinct continent, name, height from mount WHERE mount.areaRank = 1;


-- mountains, continents , subqueries

SELECT DISTINCT big.Continent, m.Name, m.Height
FROM
	(
	SELECT Continent, MAX(mo.Height) maxH
	FROM mountain mo
	JOIN geo_mountain gm on mo.Name = gm.Mountain
	join encompasses e on gm.Country = e.Country
	WHERE e.Percentage >= 50
	group by Continent
	) big
join encompasses e on big.Continent = e.Continent
JOIN geo_mountain gm2 on e.Country = gm2.Country
JOIN mountain m ON gm2.Mountain = m.Name
WHERE e.Percentage >= 50 AND m.Height = big.maxH
ORDER BY m.Height desc;


-- mountains in continents with subqueries

WITH mont (Continent, Name, Height) AS
(
	SELECT e.Continent, mo.Name, mo.Height
	FROM mountain mo
	JOIN geo_mountain gm on mo.Name = gm.Mountain
	join encompasses e on gm.Country = e.Country
	WHERE e.Percentage >= 50	
)
SELECT DISTINCT big.Continent, mont.Name, mont.Height
FROM
(
	SELECT Continent, MAX(mont.Height) maxH
	FROM mont
	GROUP BY mont.Continent
) AS big
JOIN mont ON mont.Continent = big.Continent AND mont.Height = big.maxH
ORDER BY Height desc;


-- mountain, continent, heighest

WITH mount as 
(
SELECT continent.Name as continent, mountain.Name, mountain.Height,
RANK() OVER (PARTITION BY continent.Name ORDER BY mountain.Height DESC) AS areaRank from continent, encompasses, country, geo_mountain, mountain
WHERE continent.Name = encompasses.Continent
AND encompasses.Country = country.Code
AND geo_mountain.country = country.Code
AND mountain.Name = geo_mountain.mountain
ORDER BY mountain.Height desc
)
select distinct continent, name, height,areaRank from mount 
WHERE mount.areaRank = 1;


-- count religion in countrys

-- Teil 1
select c.Name, count(*) as anzahl  from religion r
join country c on c.Code = r.Country 
group by c.Name 
order by anzahl desc;

-- Teil 2
select Name, anzahl from (
select c.Name, count(*) as anzahl  from religion r
join country c on c.Code = r.Country 
group by c.Name 
order by anzahl desc)
as test
where anzahl = 5;

-- Teil 3
WITH religion_count AS (
  SELECT c.Name, COUNT(*) AS anzahl
  FROM religion r
  JOIN country c ON c.Code = r.Country
  GROUP BY c.Name
)
SELECT Name, anzahl
FROM religion_count
WHERE anzahl = (SELECT MAX(anzahl) FROM religion_count);


-- organization count

with corps as (select o.Abbreviation, o.Country as Founder  
from organization o
where country = 'A'
)
select Abbreviation, count(*) from corps
join ismember i on corps.Abbreviation = i.Organization
group by Abbreviation
;

-- organizations
select o.Abbreviation, count(*) anzahlMG 
from organization o 
join ismember i on o.Abbreviation = i.Organization 
where o.Country = 'A'
group by o.Abbreviation

-- top 10 languages

select l.name, round(sum(c.population/100 * l.Percentage), -6) as lang 
from `language` l 
join country c on l.country = c.code
where c.code = l.country
group by l.name
order by lang desc
limit 10
;

select * from `language` l ;
-- languages with subqueries
with lang2 as 
(
	with languages as (
		select 
			l.name, 
			round(sum(c.population/100 * l.Percentage)) as lang		
		from `language` l 
		join country c on l.country = c.code
		group by l.name)
	select *, rank () over (order by lang desc) as languageRank 
	from languages
)
select Name, lang
from lang2 
 where languageRank in (2,3,5);   -- between 1 and 15; -- languageRank in (2,4,6,8);