#SQL notes
SELECT
withinSELECT
- select each country in Europe and show their population in percentage of population of Germany
SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE NAME = 'Germany')*100), '%') FROM world WHERE continent = 'Europe'
ALL
to run through a database list made withSELECT
- select countries with gdp higher than every country in europe ( gdp > 0 to avoid comparing NULL values )
SELECT name FROM world WHERE gdp > ALL( SELECT gdp FROM world WHERE continent = 'Europe' AND gdp > 0 )
- Scoping in SQL
- Without using
GROUP BY
(need the details for rows involved)- Finding the largest country (by area) for each continent
SELECT continent, name, area FROM world x WHERE area >= ALL( SELECT area FROM world y WHERE x.continent = y.continent AND area > 0)
- With
GROUP BY
(only suitable for aggregate functions)- For each continent show the number of countries
SELECT continent, COUNT(name) FROM world GROUP BY continent
- Without using
- The
HAVING
clause allows use to filter the groups which are displayed. TheWHERE
clause filters rows before the aggregation, theHAVING
clause filters after the aggregation.WHERE
SELECT continent, COUNT(name) FROM world WHERE population>200000000 GROUP BY continent
HAVING
SELECT continent, SUM(population) FROM world GROUP BY continent HAVING SUM(population)>500000000
JOIN
-> left table will duplicate for right table