ADVANCED SELECT (world.db) Flashcards
SPECIAL ANNOUNCEMENT!
These flash cards are NOT designed to trick you. They contain VERY TYPICAL queries that any person knowing SQL can be expected to answer with very little difficulty. So if you find them difficult - which is normal in the beginning - DO NOT GIVE UP!
Please use these flash cards in good health and good happiness!
Do Ukraine and Russia have any cities in common?
SELECT city FROM world_cities WHERE country = "Ukraine" INTERSECT SELECT city FROM world_cities WHERE country = "Russia"
INTERSECT is a powerful command. If you use A INTERSECT B you will get all the rows that A and B share in common
The United States has many cities that have the same names. Print out a table for the United States that looks like (city name, no. of occurances of city)
SELECT city, COUNT ( city ) FROM world_cities WHERE country = "United States" GROUP BY city ORDER BY city ASC;
This is a trick “pattern” you must memorize because it will occur many times when you work on databases!
Many cities in the U.S. have the same name. Which city in the U.S. occurs the most frequently?
The easiest approach is to just read it out of the table. The SQL that prints the names of the cities and the number of times they occur:
SELECT city, COUNT ( city ) FROM world_cities WHERE country = "United States" GROUP BY city ORDER BY city ASC;
When we ORDER BY city ASC this means the very last entries in the table will occur the most often.
Print out a table of the countries and how many cities each country has?
SELECT country, COUNT ( country ) FROM world_cities GROUP BY country ORDER BY country ASC;
Which countries have the most cities?
You can do this most easily by just printing out a sorted table of (countries, no. cities), like this:
SELECT country, COUNT ( country )
FROM world_cities
GROUP BY country
ORDER BY COUNT ( country ) ASC;
Print out a list of all the states in the United States
Note: This is wrong:
SELECT subcountry FROM world_cities WHERE country = “United States”
because it prints out duplicates. Try this instead:
SELECT DISTINCT subcountry FROM world_cities WHERE country = “United States”
INTERESTING FACT
Did you know that smoke detectors in your house are actually radioactive? Nobody likes the publicize this fact because it scares people, but in fact they contain a radioactive Ameicium-241 source that emits alpha particles.
What’s worse is that even today the soft cloth “mantles” that burn inside of lanterns are radioactive - and if you handle them incorrectly, they can be a health hazard.
So be careful!
The US was settled by many immigrants from the UK who wanted the same city name as they had in the UK. So, how many city names are in both the United States and the United Kingdom.
This is quite tricky. Recommend first displaying the list of cities using INTERSECT, the embedding this query into a simple query using COUNT.
To display the list of cities, you can use an INTERSECT, like this:
SELECT city FROM world_cities WHERE country = "United States" INTERSECT SELECT city FROM world_cities WHERE country = "United Kingdom";
Because the result of this query is simply a table, you can embed this query in a very simple SELECT COUNT query, like this:
SELECT COUNT ( city ) FROM ( SELECT city FROM world_cities WHERE country = "United States" INTERSECT SELECT city FROM world_cities WHERE country = "United Kingdom" );
Be sure to delete the unneeded “;”
Germans love their baths. Nobody really knows if they are clean, but they love baths. How many cities in Germany start with “Bad”?
SELECT COUNT ( city ) FROM world_cities
WHERE Country = “Germany”
AND
city like “bad%”;
We saw that Germans love their baths and have many cities named “Bad . . . “ - but there are also other cities in the world like this. Print out a list of all the world cities starting with “Bad . . . “ EXCEPT the German cities; exclude those from the list.
SELECT city, country FROM world_cities
WHERE
city LIKE “bad%” AND
country <> “Germany”;
Print out a table with all the Swiss cities and how many letters are in their name.
Hint: use the function LENGTH()
SELECT city, LENGTH ( city )
FROM world_cities
WHERE country = “Switzerland”
GROUP BY city;
Print out a table with all the Swiss cities and how many letters are in the city name, but this time order the table in ascending order of length, so that the cities with the shortest names are on the top, and the cities with the longest names are on the bottom.
Hint: use the function LENGTH ()
SELECT city, LENGTH ( city ) FROM world_cities WHERE country = "Switzerland" GROUP BY city ORDER BY LENGTH ( city ) ;
How many characters are in city with the longest name in the world?
Be careful: it is easy if you do not do more than you are asked for. Because it is only asking for the number of characters, this is easy to find using:
SELECT LENGTH ( city ) FROM world_cities ORDER BY LENGTH ( city );
so that, down at the very bottom, you’ll see the number of characters, which is the requested answer.
What world city has the longest name?
If we just wanted a table containing the lengths, this would be easy:
SELECT LENGTH ( city ) FROM world_cities ORDER BY LENGTH ( city );
But because we want both the value and the LENGTH of the value, we need to use the GROUP BY paradigm, like this:
SELECT city, LENGTH ( city ), country FROM world_cities
GROUP BY city
ORDER BY LENGTH ( city );