On this page
Largest countries of a continent
A first Script to learn from: a required text parameter, an optional integer with a default, on the WORLD sample of every installation.
Verification status. The output below was captured with this query on the WORLD sample database, as shown in Arguments and parameters. The header lines added here (description, tags, status, access, dialect) do not change the result.
When to use it
- Learn how
-- @paramchecks and converts arguments before anything runs. - Start from a working Script and adapt it to your own tables.
The Script
Download world-largest-countries.sql
-- @description: Largest countries of a continent
-- @tags: world, sample, geo
-- @status: stable
-- @access: read
-- @dialect: H2
-- @param continent TEXT "Continent, as in COUNTRY.CONTINENT"
-- @param min_population INTEGER = 50000000 "Smallest population listed"
SELECT CODE, NAME, POPULATION
FROM COUNTRY
WHERE CONTINENT = ${continent} AND POPULATION >= ${min_population}
ORDER BY POPULATION DESC;
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
continent | TEXT | Required | Continent, as in COUNTRY.CONTINENT |
min_population | INTEGER | 50000000 | Smallest population listed |
Install and run it
- Save the file in your Scripts Library folder (
scriptsin the installation folder by default), for example asgallery/world-largest-countries.sql. - Open the sample database:
CONNECT WORLD;. - Check what it declares:
LIB SHOW gallery/world-largest-countries.sql; - Run it:
@gallery/world-largest-countries.sql continent='Europe';
To keep the result in a file: DUMP LIB gallery/world-largest-countries.sql continent='Asia' TO asia_largest AS XLSX; (see Exporting Script results).
What you get
The countries of the continent with at least min_population inhabitants, largest first, with the values used shown under the statement.
@countries/largest.sql continent='Europe';
-- continent = 'Europe'
-- min_population = 50000000
|----|----------------------------------------------------|-----------|
|CODE|NAME |POPULATION |
|----|----------------------------------------------------|-----------|
|RUS |Russian Federation |146934000 |
|DEU |Germany |82164700 |
|GBR |United Kingdom |59623400 |
|FRA |France |59225700 |
|ITA |Italy |57680000 |
|UKR |Ukraine |50456000 |
|----|----------------------------------------------------|-----------|
6 rows fetched in 12 ms.
countries/largest.sql: SUCCESS (1 statement, 0 failed) [run BUZ2SI4X]
Limits
continentis compared exactly:'Europe', not'europe'.- Without
continent, the call is refused before anything runs, unless a session variablecontinentexists.
Related
Found something wrong or unclear on this page? Tell us, with the page address.