BASIC SELECT (world.db) Flashcards
The basics of using SELECT to pull data out of a database and filter it
SPECIAL ANNOUNCEMENT
Don’t feel like you are being tricked - you are NOT. These flash cards contain SQL queries that are very legitimate, so that any person learning SQL would be expected to answer these. Some people can do this faster, some people need a cup of coffee and more time - but please do not feel like you are being tricked. Practice, practice, practice!
Please use these flash cards in good health and good happiness!
What are the columns in the table?
If you are using SQLite, the command that let’s you see the column names is
.schema
How many rows are there in this table?
SELECT COUNT(*) FROM world_cities;
Print a list of the cities are in countries with the same name as the city e.g. name=Singapore, country=Singapore?
SELECT city FROM world_cities WHERE city = country;
Print the list of cities that start with the letter “C”
SELECT city FROM world_cities WHERE city LIKE “C%”
How many cities start with the letter “C”?
SELECT COUNT(city) FROM world_cities WHERE city LIKE “C%”
Print a list of unique country names e.g. each country name only appears once.
SELECT DISTINCT country FROM world_cities;
TRIVIA
Not a lot of people know this, but neutron stars are stars where the gravity is so high, all the atoms have been squeezed down to pure neutrons. One teaspoon of neutron star stuff weighs as much as 900 Giza pyramids in Egypt.
How many unique country names are there?
SELECT COUNT ( DISTINCT country ) FROM world_cities;
Print a list of unique country names (e.g. each country only appears once) but printed in reverse alphabetical order
SELECT DISTINCT country FROM world_cities ORDER BY country DESC;
Print a unique list of all countries that have a space “ “ in their name
SELECT DISTINCT country FROM world_cities WHERE country LIKE “% %”; It is good to keep in mind that a space is a character just like any other characters. The wildcard %k% will return all values with a “k” in them; in the same way, % % will return all values with a space in them!
How many countries have whitespace “ “ in their name?
SELECT COUNT( DISTINCT country) FROM world_cities WHERE country LIKE “% %”;
How many cities are there in the United States?
SELECT COUNT ( city ) FROM world_cities WHERE country = “United States”;
How many different city names are there in the United States?
Special note: SELECT COUNT( city ) FROM . . . will not work, because a city like Springfield occurs in many different states. Therefore you need
SELECT COUNT( DISTINCT city ) FROM world_cities WHERE country = “United States”;
How many cities in the U.S. have a whitespace “ “ in their name?
SELECT city FROM world_cities WHERE country = “United States” AND city LIKE “% %”;