SQLZOO:SELECT basics

From SQLZOO
Revision as of 10:59, 27 March 2014 by Varma9876543210 (Talk | contribs)

Jump to: navigation, search
/tr>
area population
11123
12 123
12234
13345
14234

Contents

Introducing the world table of countries

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

Summary

The example shows the population of 'France'. Strings should be in 'single quotes';

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 population density population/area for each country where the area is over 5,000,000 km2.
Show the 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

Where to find some very small, very rich countries.
We use AND to ensure that two or more conditions hold true.

The example shows the countries where the population is small and the gdp is high.
Show the name and continent where the area is less then 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 - % is the wildcard. The examples shows countries beginning with D

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? Show the country and the area for countries with an area between 207600 and 244820. BETWEEN allows range checking - note that it is inclusive.

Show the area in 1000 square km. Show area/1000 instead of area
SELECT name, area FROM world
  WHERE area BETWEEN 207600 AND 244820...
SELECT name, area/1000 FROM world
  WHERE area BETWEEN 207600 AND 244820
Clear your results

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


Language: English
Personal tools

Variants
Actions
Reference
Toolbox
Google AdSense