# What is a good analogy to introduce databases and SQL?

I think that it is vital to introduce each subject area with a few handy, even if limited, images or concepts that make the idea clear. Here is how I introduce SQL (in part):

SQL takes a set of data and transforms it in to a new set: a two-dimensional result. Stop reading until you have memorized that, because it is the whole point. Every outcome of an SQL statement (with a few exceptions) is a new Table. That table might be printed on the screen and disappear, or it might be stored in to a new table, or it could be used to update an existing table, or it could be used as input to a program which could do… anything. But, it is a two-dimensional table, and nothing more.

I think that this is the very first concept that needs to lodge in their awareness if they are to make any progress at all. Perhaps it is incomplete, incorrect, too literal, doesn't use the right word (the correct word for Table is 'Relation') and so on. But we have to start somewhere.

I'll compare a database table to a table in Excel, because my students are more familiar with Excel. I'll compare SQL to a command line interface, because my students already know about command line interfaces. Are there better metaphors or analogies to teach SQL and relational database concepts?

• What is the post office box model? I have never encountered that. My very first first lesson involves a stack. – Ben I. Jul 5 '17 at 14:47
• I use the "post office" model to explain a lot of CS concepts; from DNS & IPs to memory. You need a (even if imperfect) model that people can relate too. These days, students may relate better to mobile phones; each one has a unique address, you can set up linked lists (call forwarding) etc. – AlG Jul 5 '17 at 16:13
• For a growing list of analogies, try teaching-analogy. – Gypsy Spellweaver Jul 5 '17 at 18:03
• When my daughter was 6 I introduced her to Ruby and Postgresql because she wanted to know what i did at work. She soon lost interest, but the analogy I used for her was "cubbies" like she stored her stuff in at kindergarten. Cubbies come in blocks, which are assembled into larger installations. A database is just a very large wall of cubbies, and the DML side of SQL is about getting stuff into or out of the cubbies. DDL side of SQL is making the rules about what is allowed to be put into the cubbies. – pojo-guy Jul 7 '17 at 2:59
• Instead of using abstract words to describe what it is, instead describe what it solves. "You want to save information from one invocation of your program to the next". "You don't have memory enough for all your data." "You need to do extract and generate data without writing a new program for it every time". The list is endless. – Thorbjørn Ravn Andersen Jul 7 '17 at 17:49

Beware. "The One True Lecture" is an oxymoron. I have a horror story about trying to create such a lecture to teach elementary statistics. It was perfect in every way and explains sampling without ambiguity. It. Was. Perfect - for me. A generous evaluation of it would suggest I reached maybe 15% of my students.

The problem was (and often is) "Your students are not like you." Recognize that even if they are bright and hard working (like yourself), they may not learn in the same way you do. Learning Modalities is the term of art.

Instead of trying to find a straight and direct path from where your students are to some target/goal, plan a twisty path instead (and have the path make visits to the Active Learning shrine, I think).

Many instructors have had the experience of walking out of a lecture feeling terrible about it. They said the wrong thing in the wrong way. They stumbled. They struggled with student questions. They wrote stupid-stupid code on the board and had to backtrack and patch it up. Then later they bumped into one or more of the students who thanked them for the wonderful lecture. I've been there. Part of that is learning how to get up after a fall, which is an important lesson.

But you have part of the solution in your question. After your first "Statement of Principles", draw them a picture to illustrate it, even if the "picture" is a metaphor. So:

SQL takes a set of data and transforms it in to a new set: a two-dimensional result.

Show picture of a map between sets.. (imagine a picture here)

AND/OR, use the In Their Own Words Pedagogical Pattern and have one or two of the students tell you back what you just said in their own words.

Let the visual learners into the game as well as the aural learners and readers. Do this sort of thing a lot.

I think that to understand the SELECT queries, the Who is Who? / Guess Who? game could serve as a nice metaphor.

The idea of the game is: you have a group of pictures of persons with different facial features. One gamer selects one of them and the other gamer has to make questions to try to eliminate the ones that are not relevant and try to guess which one the other gamer has chosen. It is kind of like what you do in the WHERE part of the SELECT...

SELECT faces
FROM board
WHERE hair = white
AND has glasses
AND has a mustache
...


Maybe this game is now so popular currently... it sure was when I was a kid.

• Hadn't seen that game before, I just looked it up. Seems related to database, but I am not sure if my students would be familiar with it either. – user737 Jul 8 '17 at 18:45
• It was very popular game when I was a kid... The idea is: you have a group of persons with different facial features and have to make questions to try to eliminate the ones that are not relevant, like what you do in the WHERE part of the SELECT.... :) E.g. SELECT all faces WHERE hair = white AND Has glasses AND... – Nuno Gil Fonseca Jul 8 '17 at 18:56
• Yeah, it looks like a very realistic application of something like a Where clause. Might work for boolean logic in other ways also. – user737 Jul 8 '17 at 19:17
• @nocomprende, I think it has applications in teaching basic information theory to children. My (then eight-year-old) niece wanted to play this game with me the last time she visited. After beating her a couple of times I explained how I was maximising the information gain by looking for the question which would get closest to a 50/50 split. – Peter Taylor Jul 17 '17 at 15:07
• @PeterTaylor this is a good point about information theory - extracting useful info from data so as to make decisions, perhaps. If something is very likely, or very unlikely, then the info does not have as much value. Easy to decide whether to take your umbrella, if it is already raining, or you live in the Sahara. Not so easy in Florida, typically (closer to 50% chance). So then, on to deciding how to design a database, right? Try not to have columns with only a few distinct values, etc... – user737 Jul 17 '17 at 15:44

As requested by @heather, comment converted to answer:

When my daughter was 6 I introduced her to Ruby and Postgresql because she wanted to know what i did at work. She soon lost interest, but the analogy I used for her was "cubbies" like she stored her stuff in at kindergarten. Cubbies come in blocks, which are assembled into larger installations. A database is just a very large wall of cubbies, and the DML side of SQL is about getting stuff into or out of the cubbies. DDL side of SQL is making the rules about what is allowed to be put into the cubbies

• Many are the times I have thought about the cubbies in kindergarten. Adults have the Post Office Box thing. I also recall staring at the greenbar paper my friend's father brought home from work. First I learn to read - make black marks into a stream of words in my head - then this. What are these words? What the heck does FORMAT mean and why is everything shouting at me? – user737 Jul 17 '17 at 15:48