Datenbanken Loesungen: Unterschied zwischen den Versionen

Aus CCWiki
Zur Navigation springen Zur Suche springen
Eray (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
Sandro (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
 
(10 dazwischenliegende Versionen von 2 Benutzern 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?
SELECT Population FROM Country WHERE NAME = 'Austria';


Wie viele Einwohner hat der Kontinent Europa?


Welche Flüsse fließen durch Österreich?
-- Wie viele Einwohner hat Österreich?
{{SQLML|code=
select population
from country
where name = "austria";
}}
 
 
-- Wie viele Einwohner hat der Kontinent Europa?
{{SQLML|code=
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?
{{SQLML|code=
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)?
{{SQLML|code=
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?
{{SQLML|code=
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
{{SQLML|code=
select name from country
where name like "a%";
}}


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


{{SQLML|code=
 
SELECT river.Name from continent, encompasses, country, province, geo_river, river
-- Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl
WHERE
{{SQLML|code=
continent.Name = encompasses.Continent
select name, province, population from country
AND encompasses.Country = country.Code
where name like "a%"
AND country.Code = province.Country
order by population desc;
AND province.Name = geo_river.Province
}}
AND river.Name = geo_river.River
 
AND continent.Name like 'Europe'
 
GROUP BY river.Name
 
ORDER by river.Name
-- Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge
{{SQLML|code=
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
{{SQLML|code=
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
{{SQLML|code=
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
{{SQLML|code=
{{SQLML|code=
SELECT river.Name FROM continent
select c.Name, count(*)  from country c
INNER JOIN encompasses ON continent.Name = encompasses.Continent
join geo_lake gl on c.Code = gl.Country
INNER JOIN country ON country.Code = encompasses.Country
join geo_mountain gm on c.Code = gm.Country  
INNER JOIN province ON province.Country = country.Code
join lake l on gl.Lake = l.Name
INNER JOIN geo_river ON geo_river.Province = province.Name
join mountain m on gm.Mountain = m.Name  
INNER JOIN river ON river.Name = geo_river.River
where l.`Depth` >= 100
WHERE continent.Name like 'Europe'
and m.Height >= 1500
GROUP BY river.Name
group by name
ORDER BY river.Name
order by count(*) desc;
}}
}}




Wie viel Prozent der Menschen leben im Kontinent Europa?
-- Einwohnerzahl pro Religion
{{SQLML|code=
select sum(population)
from country c
join religion r on c.Code = r.Country
where r.Name = 'muslim';


Alle Länder die mit einem A anfangen
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=
{{SQLML|code=
SELECT * FROM country WHERE name like 'A%';
select c.Name, gs.Sea  from country c
join geo_sea gs on c.Code = gs.Country
order by name;  
}}
}}




Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl
-- 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=
{{SQLML|code=
SELECT country.name, province.name as provincename, province.population FROM country
select distinct p.Independence, c.Name ,d.Name  from politics p
INNER JOIN province ON country.code = province.country
join country c on p.Country = c.Code
ORDER BY country.name, province.Population desc;
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';
}}
}}




Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge
-- Welche Länder haben mehr als 150 Städte?
{{SQLML|code=
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?
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT river.name, river.length FROM river
-- Teil 1
INNER JOIN geo_river ON geo_river.River = river.name
select co.Name, count(*) as citycount from country co
INNER JOIN encompasses ON encompasses.Country = geo_river.Country
join city ci on co.Code = ci.Country
WHERE encompasses.Continent like 'Europe'
group by co.Name;
ORDER BY river.Length DESC;
-- 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;
}}
}}




Alle Inseln im Pazifik mit >50% islamischem Bekenntnis
-- population biggest 3 in america
{{SQLML|code=
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
{{SQLML|code=
{{SQLML|code=
SELECT country.Name, island.name, religion.Percentage FROM island, islandin, geo_island, religion, country
select m.Name ,m.Height from mountain m
WHERE
join geo_mountain gm on gm.Mountain = m.Name
island.Name = islandin.Island AND
join country c on c.Code = gm.Country
islandin.Sea like 'Pacific%' AND
where c.Name = 'austria'
geo_island.Island = island.name AND
order by m.Height desc
geo_island.Country = religion.Country AND
limit 1;
country.code = geo_island.country AND
religion.Percentage > 0 AND
religion.Name = 'Roman catholic';
}}
}}




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


-- bigest mountain in europe limit 1
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT m.name, m.Height, c.Name FROM mountain as m
select c.Name , m.Name ,m.Height from mountain m  
INNER JOIN geo_mountain as ge ON ge.Mountain = m.Name
join geo_mountain gm on gm.Mountain = m.Name  
INNER JOIN country as c ON c.Code = ge.Country
join country c on c.Code = gm.Country
INNER JOIN religion as rrr ON rrr.Country = c.Code
join encompasses e on e.Country = c.Code  
WHERE m.Height >= 3000 AND rrr.Percentage >= 60 AND rrr.Name = 'Roman Catholic'
where e.Continent  = 'Europe' and e.Percentage > 50
ORDER by m.height DESC;
order by m.Height desc
limit 1;
}}
}}




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


-- concat for group for one biggest mountain in 2 countrys
{{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
with mont (Name, Country, height) as (
INNER JOIN geo_lake ON geo_lake.Lake = lake.Name
select m.name, gm.Country, m.height
INNER JOIN geo_mountain ON geo_mountain.Country = geo_lake.Country
from mountain m
INNER JOIN country ON country.Code = geo_lake.Country
join geo_mountain gm on gm.Mountain = m.Name  
INNER JOIN mountain ON mountain.Name = geo_mountain.Mountain
join encompasses e on e.Country = gm.Country
WHERE mountain.Height >= 1500 and lake.Depth >= 100;
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;
}}
}}




Einwohnerzahl pro Religion


-- mountains in continents with ranks
{{SQLML|code=
{{SQLML|code=
SELECT religion.name, sum(country.population * religion.Percentage/100)/1000000 as people FROM religion
WITH mount as (SELECT continent.Name as continent, mountain.Name, mountain.Height,
INNER JOIN country ON country.code = religion.country
RANK() OVER (PARTITION BY continent.Name ORDER BY mountain.Height DESC) AS areaRank from continent, encompasses, country, geo_mountain, mountain
GROUP BY religion.Name order by people desc;
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;
}}
}}


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


-- mountains, continents , subqueries
{{SQLML|code=
{{SQLML|code=
SELECT distinct country.Name, sea.Name FROM country
SELECT DISTINCT big.Continent, m.Name, m.Height
INNER JOIN province ON country.Code = province.Country
FROM
INNER JOIN geo_sea ON province.Name = geo_sea.Province
(
INNER JOIN sea ON geo_sea.Sea = sea.Name
SELECT Continent, MAX(mo.Height) maxH
ORDER BY country.Name;
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;
}}
}}


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


-- mountains in continents with subqueries
{{SQLML|code=
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
{{SQLML|code=
{{SQLML|code=
SELECT country.name, geo_sea.Sea FROM country
WITH mount as
LEFT JOIN geo_sea ON country.code = geo_sea.country;
(
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
{{SQLML|code=
{{SQLML|code=
SELECT distinct country.Name, sea.Name "Sea Name" FROM country
-- Teil 1
LEFT OUTER JOIN geo_sea ON geo_sea.Country = country.Code
select c.Name, count(*) as anzahl  from religion r
LEFT OUTER JOIN sea ON sea.Name = geo_sea.Sea
join country c on c.Code = r.Country
ORDER BY country.Name
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);
}}
}}




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


-- organization count
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT country.name, politics.Independence, ethnicgroup.name, religion.Name, religion.Percentage FROM country
with corps as (select o.Abbreviation, o.Country as Founder 
INNER JOIN geo_desert ON geo_desert.Country = country.code
from organization o
INNER JOIN politics ON politics.Country = country.code
where country = 'A'
INNER JOIN ethnicgroup ON ethnicgroup.country = country.code
)
INNER JOIN religion ON religion.Country = country.Code
select Abbreviation, count(*) from corps
WHERE politics.Independence IS NOT NULL AND ethnicgroup.name like 'African';
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);
}}
}}

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);