# Difference between revisions of "SELECT basics"

namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
....

## Introducing the `world` table of countries

This tutorial introduces SQL. We will be using the SELECT command on the table world:

Summary

The example uses a WHERE clause to show the population of 'France'. Note that strings (pieces of text that are data) should be in 'single quotes';

Modify it to show the population of Germany

```SELECT population FROM world
WHERE name = 'France'```
```SELECT population FROM world
WHERE name = 'Germany'```

## Per Capita GDP

The query shows the name and population density

for each country where the area is over 5,000,000 km2. Population density is not a column in the World table, but we

can calculate it as `population/area`.
Modify it to show the name and per capita gdp: `gdp/population` for each country where the area is over 5,000,000 km2
.
```SELECT name, population/area FROM world
WHERE area > 5000000```
```SELECT name, gdp/population FROM world
WHERE area > 5000000```

## Small and wealthy

The example shows the countries where the population is small and the gdp is high.

We use `AND` to ensure that two or more conditions hold true. Here, the `WHERE` clause has two parts that both must be true for a row to be retrieved.

Modify it to show the name and continent where the area is less than 2000 and the gdp is more than 5000000000
```SELECT name , continent
FROM world
WHERE population < 2000000
AND gdp > 5000000000```
```SELECT name , continent
FROM world
WHERE area < 2000
AND gdp > 5000000000```

## Scandinavia

Checking a list The word IN allows us to check if an item is in a list. The example shows the name and population for the countries 'Ireland', 'Iceland' and 'Denmark'

Show the name and the population for 'Denmark', 'Finland', 'Norway', 'Sweden'
```SELECT name, population FROM world
WHERE name IN ('Ireland', 'Iceland',
'Denmark')```
```SELECT name, population FROM world
WHERE name IN ('Denmark', 'Finland',
'Norway', 'Sweden')```

## Starts with G

What are the countries beginning with G? The word `LIKE` permits pattern matching. The examples shows countries beginning with D. % is the wildcard that means "any text". D% means "It must start with a D but then can have any text".

Modify it to show each country that begins with G
```SELECT name FROM world
WHERE name LIKE 'D%'```
```SELECT name FROM world
WHERE name LIKE 'G%'```

## Just the right size

Which countries are not too small and not too big? `BETWEEN` allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to show the country and the area for countries with an area between 200000 and 250000.

```SELECT name, area FROM world
WHERE area BETWEEN 250000 AND 300000```
```SELECT name, area FROM world
WHERE area BETWEEN 200000 AND 250000```

You are ready for tutorial one:SELECT statements with WHERE.

 Language: English  • Deutsch