BASIC SELECT (world.db) Flashcards

The basics of using SELECT to pull data out of a database and filter it

1
Q

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!

A

Please use these flash cards in good health and good happiness!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the columns in the table?

A

If you are using SQLite, the command that let’s you see the column names is

.schema

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many rows are there in this table?

A

SELECT COUNT(*) FROM world_cities;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Print a list of the cities are in countries with the same name as the city e.g. name=Singapore, country=Singapore?

A

SELECT city FROM world_cities WHERE city = country;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Print the list of cities that start with the letter “C”

A

SELECT city FROM world_cities WHERE city LIKE “C%”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many cities start with the letter “C”?

A

SELECT COUNT(city) FROM world_cities WHERE city LIKE “C%”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Print a list of unique country names e.g. each country name only appears once.

A

SELECT DISTINCT country FROM world_cities;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TRIVIA

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many unique country names are there?

A

SELECT COUNT ( DISTINCT country ) FROM world_cities;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Print a list of unique country names (e.g. each country only appears once) but printed in reverse alphabetical order

A

SELECT DISTINCT country FROM world_cities ORDER BY country DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Print a unique list of all countries that have a space “ “ in their name

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many countries have whitespace “ “ in their name?

A

SELECT COUNT( DISTINCT country) FROM world_cities WHERE country LIKE “% %”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many cities are there in the United States?

A

SELECT COUNT ( city ) FROM world_cities WHERE country = “United States”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many different city names are there in the United States?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many cities in the U.S. have a whitespace “ “ in their name?

A

SELECT city FROM world_cities WHERE country = “United States” AND city LIKE “% %”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many cities are named after Capt. Kirk’s starship?

A
SELECT city FROM world_cities WHERE city = "Enterprise"; 
-or-
SELECT COUNT( city ) FROM world_cities WHERE city = "Enterprise";
17
Q

INTERESTING FACT

A

Did you know that William Shakespeare invented the word “vomit”?

18
Q

How many cities are in Switzerland?

A

SELECT COUNT ( city ) FROM world_cities WHERE country = “Switzerland”;

19
Q

Which cities are in the Swiss canton of Bern?

A

SELECT * FROM world_cities WHERE subcountry = “Bern”;

20
Q

How many states are there in Germany?

A

You can see the states with

SELECT subcountry FROM world_cities WHERE country = “Germany”;

but they are not unique. In order to see the list of unique states,

SELECT DISTINCT subcountry FROM world_cities WHERE country = “Germany”;

And if you want just the count, then

SELECT COUNT( DISTINCT subcountry ) FROM world_cities WHERE country = “Germany”;

21
Q

How many times does the word “Zürich” appear in the list of Swiss cities?

A

You can filter for this using

SELECT city FROM world_cities WHERE country = “Switzerland” AND city LIKE “%Zür%”;

and if you want the count, then

SELECT COUNT ( city ) FROM world_cities WHERE country = “Switzerland” AND city LIKE “%Zür%”;

22
Q

Homer Simpson and his family live in Springfield. How many Springfields are there in the U.S.?

A

SELECT city FROM world_cities WHERE city = “Springfield”;

-or-

SELECT COUNT( city ) FROM world_cities WHERE city = “Springfield”;

23
Q

INTERESTING FACT

A

Temperatures in Fahrenheit are more human-friendly than temperatures in Celsius. Celsius tells you when a glass of water will freeze - that’s probably important if you are a glass of water.

But you are a human! Fahrenheit makes more sense. Above 100 F you die without support. Below 0 the insides of your nose freeze.

24
Q

How many cities have 6 spaces in their name e.g. 6 occurances of “ “?

A

SELECT * FROM world_cities WHERE city LIKE “% % % % % % %”;

-or-

SELECT COUNT ( * ) FROM world_cities WHERE city LIKE “% % % % % % %”;

(Look closely: between the % you will see 6 whitespaces.)

25
Q

How many cities have 5 whitespaces in their name? e.g. 5 separate occurances of “ “?

A

SELECT * FROM world_cities WHERE city LIKE “% % % % % %”;

-or-

SELECT COUNT ( * ) FROM world_cities WHERE city LIKE “% % % % % %”;

(Look closely: between the % you will see 5 whitespaces.)

26
Q

How many cities are there in the list with the same name for the subcountry?

A

SELECT COUNT ( * ) FROM world_cities WHERE city = subcountry;

27
Q

In what country will you find the city “Lake Zurich”?

A

SELECT city, country FROM world_cities WHERE city = “Lake Zurich”;

28
Q

In the movie “The Omen,” the number 666 was considered evil. How many locations have 666 that appear in their location id?

A

To see them,

SELECT * FROM world_cities WHERE geonameid LIKE “%666%;

To count them,

SELECT COUNT ( * ) FROM world_cities WHERE geonameid LIKE “%666%;

29
Q

William Shakespeare is thought to have authored the King James version of the Bible. The supposed “proof” of this is that he finished the book on his 46th birthday. In Psalm 46, the 46th word from the beginning is “Shake” and the 46th world from the end is “Spear.” How many locations have the property that they start with 46 and end with 46?

A

To see them,

SELECT * FROM world_cities WHERE geonameid = “46%46”;

To count them,

SELECT COUNT ( * ) FROM world_cities WHERE geonameid = “46%46”;