0% found this document useful (0 votes)
84 views

Country Profiles: Name Region Area Population GDP

This document provides examples of SQL SELECT statements for summarizing data from tables. It shows how to: 1. Select columns from a table and look at the output. 2. Filter records using the WHERE clause to show countries with a population over 200 million. 3. Use aggregates like COUNT, SUM, and AVG on columns to summarize data from a table.

Uploaded by

kenarny
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Country Profiles: Name Region Area Population GDP

This document provides examples of SQL SELECT statements for summarizing data from tables. It shows how to: 1. Select columns from a table and look at the output. 2. Filter records using the WHERE clause to show countries with a population over 200 million. 3. Use aggregates like COUNT, SUM, and AVG on columns to summarize data from a table.

Uploaded by

kenarny
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Country name region area population GDP

Profiles Afghanistan South 652225 26000000


Asia
This tutorial introduces Albania Europe 28728 3200000 6656000000
SQL as a query Algeria Middle 2400000 32900000 75012000000
language. We will be East
using the SELECT
Andorra Europe 468 64000
command on the table
bbc: ...

Exercises
Using the SELECT statement.

1a. Read the notes about this table. Issue the command: SELECT name, Results
region, population FROM bbc. Look at the output.

Before attempting the following questions read this! How to use WHERE to filter
records.

2a. Show the name for the countries that have a population of at least Results
200 million. (200 million is 200000000, there are eight zeros)
2b. Give the name and the per capita GDP for those countries with a Results
population of at least 200 million.
HELP:How to calculate per capita GDP

2c. Show the name and population in millions for the countries of 'Middle Results
East'
Divide the population by 1000000 to get population in millions.

2d. Show the name and population for 'France', 'Germany', 'Italy' Results
2e. Identify the countries which have names including the word 'United'

nobel Nobel Laureates


We continue practicing simple SQL queries on a single table.

This tutorial is concerned with a table of Nobel prize winners:

nobel(yr, subject, winner)

Exercises
Using the SELECT statement.

nobel
yr subject winner
1960 Chemistry Willard F. Libby
1960 Literature Saint-John Perse
1960 Medicine Sir Frank Macfarlane Burnet
1960 Medicine Peter Medawar

1a. Change the query shown so that it displays Nobel prizes for 1950. Results
1b. Show who won the 1962 prize for Literature. Results

2a. Show the year and subject that won 'Albert Einstein' his prize. Results

2b. Give the name of the 'Peace' winners since the year 2000, including Results
2000.
2c. Show all details (yr, subject, winner) of the Literature prize winners Results
for 1980 to 1989 inclusive.

2d. Show all details of the presidential winners: ('Theodore Roosevelt', Results
'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
2e. Show the winners with first name John Results

3a. In which years was the Physics prize awarded but no Chemistry prize.
(WARNING - this question is way too hard for this level, you will need to
use sub queries or joins).

SELECT within SELECT


This tutorial looks at how we can use SELECT statements within SELECT statements
to perform more complex queries.

Exercises
Using nested SELECT.

1a. List each country name where the population is larger than 'Russia'. Results

bbc(name, region, area, population, gdp)

1b. List the name and region of countries in the regions containing 'India', Results
'Iran'.

1c. Show the countries in Europe with a per capita GDP greater than Results
'United Kingdom'.
Per Capita GDP
1d. Which country has a population that is more than Canada but less than Results
Algeria?

To get a well rounded view of the important features of SQL you should move on to
the next tutorial concerning aggregates.

To gain an absurdly detailed view of one insignificant feature of the language, read
on.

We can use the word ALL to allow >= or > or < or <=to act over a list.

2a. Which countries have a GDP greater than any country in Europe? Results
[Give the name only.]
We can refer to values in the outer SELECT within the inner SELECT. We can name
the tables so that we can tell the difference between the inner and outer versions.

3a. Find the largest country in each region: Results

The following questions are very difficult:

3b. Find each country that belongs to a region where all populations are Results
less than 25000000. Show name, region and population.
3c. Some countries have populations more than three times that of any of Results
their neighbours (in the same region). Give the countries and regions.

BBC Country Profile: Aggregate functions


This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate
function takes many values and delivers just one value. For example the function
SUM would aggregate the values 2, 4 and 5 to deliver the single value 11.

Exercises
Using SUM, COUNT, MAX, AVG, DISTINCT and ORDER BY.

1a. Show the total population of the world. Results

bbc(name, region, area, population, gdp)


1b. List all the regions - just once each. Results

1c. Give the total GDP of Africa Results

1d. How many countries have an area of at least 1000000 Results


1e. What is the total population of ('France','Germany','Spain') Results

Using GROUP BY and HAVING.

2a. For each region show the region and number of countries. Results

2b. For each region show the region and number of countries with Results
populations of at least 10 million.
2c. List the regions with total populations of at least 100 million. Results

What next
The nobel table can be used to practice more SUM and COUNT functions

The next tutorial is looks at the Table Tennis database. It shows how queries may use
records from two related tables.

SELECT
Building queries

 Concatenate two or more fields.


 Make union between different tables to build one single view or request?
 Use 'like' in a sql select statement

The LIKE command allows "Wild


cards". A % may be used to match and
string, _ will match any single character.

The example shows countries begining


with Z. The country Zambia matches Submit Query Reset

because ambia matches with the %.


Specific to Oracle
none
Related links:
 SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL

The LIKE command allows "Wild


cards". A % may be used to match and
string, _ will match any single character.

The example shows countries begining


with Z. The country Zambia matches Submit Query Reset

because ambia matches with the %.


Specific to Oracle
none
Related links:
 SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?

List a number of SELECT


statements separated by the
UNION key word. Be sure that you
have the same number of columns
in each of the SELECT statements.
SELECT name FROM customer Submit Query Reset
UNION
SELECT name FROM employee
UNION
SELECT name FROM artist

Specific to Oracle
none
Related links:
 SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL
o
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL

 How to build a statement on a word with an Apostrophe such as WHERE
name='Tom's Book'

Specific to Oracle
Use two apostrophes.
Related links:
 SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL

 Full text search
 Display a column name for an aggregate function.
 How do you use Equi Join to join two tables with the same name.
 Use SELECT for a column whose name contains spaces.
 use NULL

The "brute force" method is to use


use the LIKE operator against any
of the fields to be searched. This
will be relatively expensive - but
probably good enough in many
Submit Query Reset
cases. The term to search for
should be quoted and placed
within two wild cards.
You should construct the string
literal in some scripting language -
don't forget to quote it.
Specific to Oracle
The LIKE command is case
sensitive - to do a case insensitive
search you should force the
searched field and the search term
into lower (or upper) case. Without
this we would miss "Bahamas,
The"
Related links:
 SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL

DATES
How to work with dates; adding, subtracting and formatting.

 Date and time types.


 Format a date and time.
 Specify a date (date literal).
 Match a range of dates
 Subtract dates.
 Add days to a date. INTERVALS. Also subtract, also years, months, hours
...
 Components of a date: such as the year or the month
 Group by day of the week (using date functions)
 Group by day of the week (using arithmetic)
 How can I select the oldest person in the table PERSON by birthday?
 Select a record with the latest date
 How to format a date like yyyymmdd
 How can I get the Lastest record from SQL

You might also like