Using SUM, Count, MAX, DISTINCT and ORDER BY
From SQLZoo
Language: | English • 日本語 |
---|
Aggregates
Aggregate functions like SUM and COUNT take many rows and summarise them into a single row.
Distinct
The result of a SELECT
statement may contain identical rows.
We can remove these duplicates using the DISTINCT
key word.
Order by
ORDER BY allows us to see the result of a SELECT in a particular order. We may indicate ASC or DESC for ascending (smallest first, largest last) or descending order.
If you don't use ORDER BY the results given might be in any order.
The total population
and GDP
of Europe.
SELECT SUM(population), SUM(gdp)
FROM world
WHERE continent = 'Europe'
SELECT SUM(population), SUM(gdp)
FROM world
WHERE continent = 'Europe'
What are the regions?
SELECT DISTINCT continent FROM world
SELECT DISTINCT continent FROM world
Show the name
and population
for each country with
a population of more than 100000000. Show countries in descending order of
population.
SELECT name, population
FROM world
WHERE population > 100000000
ORDER BY population DESC
SELECT name, population
FROM world
WHERE population > 100000000
ORDER BY population DESC