Write an SQL query to answer each of the following questions. Recall the world
database:
-
What languages are spoken in the United States (country_code
'USA'
)? Show the language and the percentage. Order by percentage from largest to smallest.
SELECT language, percentage
FROM languages
WHERE country_code = 'USA'
ORDER BY percentage DESC;
-
List all of the official languages spoken around the world in alphabetical order.
SELECT DISTINCT language
FROM languages
WHERE official = 'T'
ORDER BY language;
-
What country/countries use English as their exclusive language? (Hint: Use the percentage.)
SELECT country_code, language
FROM languages
WHERE percentage = 100
AND language = 'English';
-
List the names of all countries in Antarctica.
SELECT name
FROM countries
WHERE continent = 'Antarctica';
-
What countries have a life expectancy of 78 years or more? List the county names and life expectancies, sorted by greatest to least life expectancy.
SELECT name, life_expectancy
FROM countries
WHERE life_expectancy >= 78
ORDER BY life_expectancy DESC;
-
List all continents and all regions within them. Make it easy to see regions for each contient.
SELECT DISTINCT continent, region
FROM countries
ORDER BY continent, region;
-
Which countries received their independence before 0 AD? Show both the name and the year. (Hint: BC years are negative values.)
SELECT name, independace_year
FROM countries
WHERE independace_year < 0;
-
Which countries have the same local name as their official name? Show them in sorted order.
SELECT name
FROM countries
WHERE name = local_name
ORDER BY name;
-
What countries have the word "Republic" as part of their name?
SELECT name
FROM countries
WHERE name LIKE '%Republic%';
-
What countries have a monarchy as their form of government?
SELECT DISTINCT name, government_form
FROM countries
WHERE government_form LIKE '%monarchy%'
ORDER BY government_form, name;
-
List all countries in Europe, Oceania, and Antarctica, sorted by continent.
SELECT continent, name
FROM countries
WHERE continent IN ('Europe', 'Oceania', 'Antarctica')
ORDER BY continent, name;
-
Which countries have a GNP between $10,000 AND $50,000?
SELECT name, gnp
FROM countries
WHERE gnp BETWEEN 10000 AND 50000;
-
List the countries whoose names start with either A or B.
SELECT name
FROM countries
WHERE name LIKE 'A%' OR name LIKE 'B%';
-
Which countries have the densest population (population divided by surface area)? Show the densest ones at the top.
SELECT name, population, surface_area,
population / surface_area as population_density
FROM countries
ORDER BY population_density DESC;
Write an SQL query to answer each of the following questions. Recall the simpsons
database:
Now let's write a page that displays the result of a query in PHP.
Start from the following files: