Datenbanken Loesungen: Unterschied zwischen den Versionen

Aus CCWiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Sandro (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
 
(16 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?


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 viel Prozent der Menschen leben im Kontinent Europa?
-- 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";
}}


Alle Länder die mit einem A anfangen


-- Welche Flüsse fließen durch Europa (nur den Flussnamen)?
{{SQLML|code=
{{SQLML|code=
SELECT * FROM country WHERE name like 'A%';
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;
}}
}}




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


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




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


-- Alle Länder die mit einem A anfangen
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT river.name, river.length FROM river
select name from country
INNER JOIN geo_river ON geo_river.River = river.name
where name like "a%";
INNER JOIN encompasses ON encompasses.Country = geo_river.Country
WHERE encompasses.Continent like 'Europe'
ORDER BY river.Length DESC;
}}
}}




Alle Inseln im Pazifik mit >50% islamischem Bekenntnis


-- Alle Länder die mit einem A anfangen und deren Bundesländer mit Einwohnerzahl, sortiert nach Einwohnerzahl
{{SQLML|code=
{{SQLML|code=
SELECT country.Name, island.name, religion.Percentage FROM island, islandin, geo_island, religion, country
select name, province, population from country
WHERE
where name like "a%"
island.Name = islandin.Island AND
order by population desc;
islandin.Sea like 'Pacific%' AND
geo_island.Island = island.name AND
geo_island.Country = religion.Country AND
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


-- Alle Flüsse die durch Europa fließen (nur Flüsse und deren Länge), sortiert nach Länge
{{SQLML|code=
{{SQLML|code=
SELECT DISTINCT m.name, m.Height, c.Name FROM mountain as m
select r.name, r.length, c.Name
INNER JOIN geo_mountain as ge ON ge.Mountain = m.Name
from geo_river gr
INNER JOIN country as c ON c.Code = ge.Country
join river r on gr.River = r.name
INNER JOIN religion as rrr ON rrr.Country = c.Code
join country c on gr.Country = c.Code  
WHERE m.Height >= 3000 AND rrr.Percentage >= 60 AND rrr.Name = 'Roman Catholic'
join encompasses e on c.Code = e.Country
ORDER by m.height DESC;
where e.Continent = 'Europe' -- and e.Percentage > 50
group by name
order by length desc;
}}
}}




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


-- Alle Inseln im Pazifik mit >50% islamischem Bekenntnis
{{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 i.Island,r.Name,r.Percentage
INNER JOIN geo_lake ON geo_lake.Lake = lake.Name
from islandin i
INNER JOIN geo_mountain ON geo_mountain.Country = geo_lake.Country
join geo_island gi on i.island = gi.island
INNER JOIN country ON country.Code = geo_lake.Country
join religion r on gi.Country = r.Country  
INNER JOIN mountain ON mountain.Name = geo_mountain.Mountain
where i.sea = 'Pacific Ocean' and r.Name ='Muslim' and Percentage > 50;
WHERE mountain.Height >= 1500 and lake.Depth >= 100;
}}
}}




Einwohnerzahl pro Religion


-- 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 religion.name, sum(country.population * religion.Percentage/100)/1000000 as people FROM religion
select c.Name, count(*)  from country c
INNER JOIN country ON country.code = religion.country
join geo_lake gl on c.Code = gl.Country
GROUP BY religion.Name order by people desc;
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 Länder, nur die Namen, und wenn sie haben, das Meer dazu
-- 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=
{{SQLML|code=
SELECT country.name, geo_sea.Sea FROM country
select c.Name, gs.Sea from country c
LEFT JOIN geo_sea ON country.code = geo_sea.country;
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'''
-- 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 DISTINCT country.name, politics.Independence, ethnicgroup.name, religion.Name, religion.Percentage FROM country
select distinct p.Independence, c.Name ,d.Name  from politics p
INNER JOIN geo_desert ON geo_desert.Country = country.code
join country c on p.Country = c.Code
INNER JOIN politics ON politics.Country = country.code
join ethnicgroup e on e.Country = c.Code
INNER JOIN ethnicgroup ON ethnicgroup.country = country.code
join geo_desert gd on gd.Country = c.Code
INNER JOIN religion ON religion.Country = country.Code
join desert d on gd.Desert = d.Name
WHERE politics.Independence IS NOT NULL AND ethnicgroup.name like 'African';
where e.Name = 'African';
}}
 
 
-- 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=
-- 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=
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=
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
{{SQLML|code=
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
{{SQLML|code=
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
{{SQLML|code=
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
{{SQLML|code=
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
{{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=
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
{{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);
}}
}}

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