-
The first step is to create the database we built in the lectures. The easiest way to do this would be to download this Python script and run it. It will create the database and everything in it. (Another option is to go through the lecture by hand, and write out all the queries yourself, this would take more time but be a useful learning practice.)
-
WARNING: Python will only be able to edit a database file if the file and/or the folder it is in have general write permissions. If you are on a Mac/Linux computer, all you have to do is "chmod 777". If you are on a Windows computer, simply right-click the file and navigate through the menus to add write permissions.
Make sure you have a database server up and running and you have permissions to work in it.
- Use
INSERT
to add a "License to Kill" for agentIDs 4, 5, 6, 7, 8, and 9 to thelicense
table. - Use
SELECT
to create a list of all agents (by agentID) who have a "License to Kill" in thelicense
table. - Use
SELECT
to create a list of all agentIDs from theagents
table. - Using the list generated in parts
2
and3
above, find a list of all agents (by agentID) who do NOT have a License to Kill. - Use
SELECT
and the list generated in part4
above to create a list of actual agent names for all the agents who don't have a License to Kill. - NEW MATERIAL: Repeat all of the logic in parts
1
through5
above using a single query. (HINT: You can usein
andnot in
to compare the results of two queries, and you can compare queries by putting them in paranthesis.)
CREATE
aTABLE
namedmissions
, with four columsns: id (an integer primary key), agentID (an integer), completed (a boolean), and mission_name (a text field).- Read the CSV file [top_secret_missions](top_secret_missions.csv] into a list.
- Loop through each line (except the header) in your CSV list, and use
INSERT
to add a row to themissions
table for each line. - Use a
SELECT
query with aWHERE
conditional statement to select all the mission names that are not yet completed.
Our secret agents are notoriously competitive. They always arguing about who's the best secret agent in the world. How macho. We'll decide by counting who has completed the most missions.
SELECT
just the agentIDs from themissions
table.- Use the list method
.count()
to count how many missions each of the 8 agents have been on. SELECT
the agent names and IDs from theagents
table.- Decide which agent wins.
- NEW MATERIAL:
GROUP BY
is a SQL keyword that allows you to do the same type of counting you did in part2
. (Look here to learn more about theGROUP BY
keyword. UseGROUP BY
andJOIN
to perform parts1
through4
with a single query.
- Solutoins - MySQL
- Solutions - PostgreSQL
- Solutions - SQLite
Back to Lecture - MySQL - PostgreSQL - SQLite