-
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
INSERTto add a "License to Kill" for agentIDs 4, 5, 6, 7, 8, and 9 to thelicensetable. - Use
SELECTto create a list of all agents (by agentID) who have a "License to Kill" in thelicensetable. - Use
SELECTto create a list of all agentIDs from theagentstable. - Using the list generated in parts
2and3above, find a list of all agents (by agentID) who do NOT have a License to Kill. - Use
SELECTand the list generated in part4above 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
1through5above using a single query. (HINT: You can useinandnot into compare the results of two queries, and you can compare queries by putting them in paranthesis.)
CREATEaTABLEnamedmissions, 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
INSERTto add a row to themissionstable for each line. - Use a
SELECTquery with aWHEREconditional 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.
SELECTjust the agentIDs from themissionstable.- Use the list method
.count()to count how many missions each of the 8 agents have been on. SELECTthe agent names and IDs from theagentstable.- Decide which agent wins.
- NEW MATERIAL:
GROUP BYis a SQL keyword that allows you to do the same type of counting you did in part2. (Look here to learn more about theGROUP BYkeyword. UseGROUP BYandJOINto perform parts1through4with a single query.
- Solutoins - MySQL
- Solutions - PostgreSQL
- Solutions - SQLite
Back to Lecture - MySQL - PostgreSQL - SQLite