#Set up your own playground
AddedEvery query on this site was run on a real database, and the results under each one are copied from what it printed. You can get the same databases on your own computer, with the same data already loaded, and type the queries yourself: reading ORDER BY department_id, salary DESC is one thing; typing it, getting Jonas first and working out why is what makes it stick.
The playground is four databases running in Docker: MySQL 9.7 (most chapters), PostgreSQL 18 (the comparisons), MongoDB 9.0 and Redis 8.10. One file, compose.yaml, describes them; one command starts them; on the first start each database loads the site's sample shop by itself. Nothing is installed on your computer except Docker, and every port listens on 127.0.0.1 only, so nothing outside your machine can reach them.
Think of it as a practice kitchen with the ingredients already on the counter. You can make a mess (delete every order, drop a table), and one command puts everything back exactly as the page expects, so your results keep matching the ones printed here.
What you need: Docker with Compose (Docker Desktop on a Mac or Windows; on a Mac, OrbStack or Colima work too; on Linux, Docker Engine with the compose plugin) and about 3.7 GB of disk for the four images, which the first start downloads. docker compose version should print a version.
# 1. Get the playground's files (once): seven small text files in a new folder
mkdir db-playground && cd db-playground
for f in compose.yaml mysql/load.sh mysql/shop.sql mysql/practice.sql mysql/client.cnf postgres/shop.sql mongo/shop.js; do
curl -fsSL --create-dirs -o "$f" "https://rohit-databases.pages.dev/playground/$f"
done
# 2. Start all four databases and wait until they're ready
docker compose up -d --wait
# 3. Open a MySQL prompt, already in the shop database, and type
docker compose exec mysql mysql[+] up 5/5
✔ Network db-notes_default Created 0.0s
✔ Container db-notes-redis-1 Healthy 3.3s
✔ Container db-notes-postgres-1 Healthy 3.8s
✔ Container db-notes-mongo-1 Healthy 3.8s
✔ Container db-notes-mysql-1 Healthy 6.3s
Welcome to the MySQL monitor. Commands end with ; or \g.
Server version: 9.7.2 MySQL Community Server - GPL
mysql> SELECT name, department_id, salary
-> FROM employees
-> ORDER BY department_id, salary DESC;
+-------+---------------+-----------+
| name | department_id | salary |
+-------+---------------+-----------+
| Jonas | NULL | 91000.00 |
| Asha | 1 | 185000.00 |
| Ben | 1 | 142000.00 |
| Chloe | 1 | 142000.00 |
| Dev | 1 | 118000.00 |
| Elena | 2 | 130000.00 |
| Grace | 2 | 99000.00 |
| Farid | 2 | 95000.00 |
| Hiro | 3 | 88000.00 |
| Isla | 3 | 72000.00 |
+-------+---------------+-----------+
10 rows in set (0.000 sec)
mysql> exit
Bye- The loop downloads the seven files from this site into
db-playground/, creating themysql/,postgres/andmongo/folders as it goes (--create-dirs).compose.yamldescribes the four containers; the others are the data, the script that loads it into MySQL, and one client setting. You can also download them one by one from the list at the end of this topic. docker compose up -dcreates and starts the four containers in the background (-d);--waitdoesn't return until each one passes its health check. On the very first run it downloads the images first, which takes a few minutes; the times above (3–6 seconds) are a start with the images already there.- While MySQL starts for the first time,
load.shrunsshop.sql(the 7 tables of the sample shop) into a database calledshop, and the shop pluspractice.sql(the practice tables of Classic SQL Interview Problems) into a second one calledpractice, so chapters that create their own tables never collide with the practice ones. PostgreSQL and MongoDB load their own copy of the shop the same way. The health checks only pass once that's finished, so when--waitreturns, the data is there. docker compose exec mysql mysqlruns themysqlclient inside the MySQL container. It doesn't ask for a password and opensshopstraight away, becausemysql/client.cnfis mounted as the container's client settings (userroot, passwordstudy, databaseshop). Its welcome is longer than shown above: a note that it's reading table names for completion, the connection id, the copyright notice and a help line are left out. Themysql>prompt is yours: a statement can span lines (->means "carry on") and runs at the;.- The query is the one from Sorting with ORDER BY, typed by hand, and the table is exactly the one printed there:
NULLsorts first in MySQL, then each department by salary, highest first.exit(or Ctrl-D) leaves the prompt; the databases keep running.
Each has its own prompt, opened the same way. PostgreSQL's psql opens the shop's PostgreSQL copy:
docker compose exec postgres psql -U postgres shoppsql (18.6 (Debian 18.6-1.pgdg13+2))
Type "help" for help.
shop=# SELECT name, department_id, salary FROM employees ORDER BY department_id, salary DESC;
name | department_id | salary
-------+---------------+-----------
Asha | 1 | 185000.00
Ben | 1 | 142000.00
Chloe | 1 | 142000.00
Dev | 1 | 118000.00
Elena | 2 | 130000.00
Grace | 2 | 99000.00
Farid | 2 | 95000.00
Hiro | 3 | 88000.00
Isla | 3 | 72000.00
Jonas | | 91000.00
(10 rows)
shop=# \q-U postgreslogs in as thepostgressuperuser (no password is needed from inside the container), andshopis the database.\qquits.- The same query on the same rows puts Jonas last: PostgreSQL sorts
NULLafter every value in ascending order, MySQL before. It's the first of the SQL differences you'll hit first, and now you've seen it rather than read it. psqlpages long results (pressqto leave the pager), and printsNULLas an empty cell;\pset null '∅'makes it visible.
MongoDB's shell, mongosh, opens the shop database, which has customers and orders (each order with its items inside it):
docker compose exec mongo mongosh shoprs0 [direct: primary] shop> db.customers.find({ city: "Toronto" }, { _id: 0, name: 1, city: 1 }).sort({ name: 1 })
[
{ name: 'Noah', city: 'Toronto' },
{ name: 'Rohit', city: 'Toronto' }
]
rs0 [direct: primary] shop> exit- Before the prompt,
mongoshprints its version, a note about usage data and a few startup warnings (no access control, memory allocator advice). They're normal for a local playground and were left out above. - The prompt
rs0 [direct: primary]says the server is the primary of a replica set calledrs0. It's a one-member set, which is what transactions and change streams need;mongo/shop.jsstarts it on the first run. - The query is the one side by side with its SQL version in SQL to MongoDB: the vocabulary, and the answer matches the MySQL one: Noah and Rohit.
Redis starts empty. redis-cli gives you its prompt; with a command after it, it runs just that command:
docker compose exec redis redis-cli SET greeting hello
docker compose exec redis redis-cli GET greetingOK
"hello"SET greeting hellostores the stringhellounder the keygreeting, and Redis repliesOK.GET greetingreads it back. The quotes areredis-climarking a string when it prints to a terminal; with-Tor a pipe it prints the barehello. Plaindocker compose exec redis redis-cliopens the interactive prompt (127.0.0.1:6379>) for the examples in Redis and Caching.
The same databases are reachable from programs on your computer, on these ports. They're the ports the site's examples were run on, so the Node examples connect as written:
| Database | Host and port | User / password | Database | Connection string |
|---|---|---|---|---|
| MySQL 9.7 | 127.0.0.1:3307 | root / study | shop | mysql://root:study@127.0.0.1:3307/shop |
| PostgreSQL 18 | 127.0.0.1:5433 | postgres / study | shop | postgres://postgres:study@127.0.0.1:5433/shop |
| MongoDB 9.0 | 127.0.0.1:27018 | none | shop | mongodb://127.0.0.1:27018/shop?directConnection=true |
| Redis 8.10 | 127.0.0.1:6380 | none | — | redis://127.0.0.1:6380 |
- The ports are the usual ones plus one (3307 instead of 3306, and so on), so the playground doesn't clash with a database you may already run on the standard port.
- Any GUI that speaks MySQL (MySQL Workbench, TablePlus, DBeaver, DataGrip, VS Code extensions) connects with the first row's settings; MongoDB Compass takes the MongoDB connection string.
directConnection=truematters for MongoDB: inside its container the replica-set member calls itselflocalhost:27017, and without the option a driver would try to reach that address on your Mac and fail.- The Node examples in later chapters name a database after the chapter they were run in (
database: 'ch25'). In your playground, useshop. A connection with each of the four strings above was tested from Node on the Mac (mysql2, mongodb with a transaction, redis, pg).
The mysql and psql prompts already complete names when you press Tab (type empl, press Tab, get employees; that's what mysql's startup line "Reading table information for completion" is for). mycli (MySQL) and pgcli (PostgreSQL) go further: as you type, a small menu of matching names pops up without pressing anything, and they colour the SQL. Both run on your Mac and connect through the playground's ports from the table above. With Homebrew:
brew install mycli pgcli
mycli mysql://root:study@127.0.0.1:3307/shopThen type SELECT name FROM empl, press Tab, and finish the line:
MySQL root@127.0.0.1:shop> SELECT name FROM employees LIMIT 2;
┌──────┐
│ name │
├──────┤
│ Asha │
│ Ben │
└──────┘
2 rows in set
Time: 0.004sbrew installadds both programs (andfzf, which mycli uses for searching your history with Ctrl-R). The URL aftermycliholds the user, password, host, port and database: the same values as the MySQL row of the connection table.emplplus Tab becameemployees. When several names fit, sayord(ordersandorder_items), the menu lists them all: press Tab again or use the arrow keys to pick one, then keep typing.- The rows are the same as the page's; only the borders differ (mycli draws box lines).
- For PostgreSQL it's
pgcli postgres://postgres:study@127.0.0.1:5433/shop, with the same Tab behaviour; it printsSELECT 2where MySQL prints2 rows in set.exitleaves mycli,\qleaves pgcli. - MongoDB's own shell already completes with Tab: in
docker compose exec mongo mongosh shop,db.custplus Tab becomesdb.customers.
All commands run in the db-playground folder:
| Command | What happens | Your data |
|---|---|---|
docker compose stop | Stops the four containers (frees memory) | Kept |
docker compose up -d --wait | Starts them again | Still there |
docker compose down && docker compose up -d --wait | Throws the containers away and makes new ones, which load the sample data again | Reset to the page's starting point |
docker compose exec -T mysql mysql shop < mysql/shop.sql | Reloads only the 7 sample tables in MySQL's shop (it ends by printing a header line and one analyze status OK line per table: fresh statistics) | Those tables reset |
docker compose exec -T mysql mysql practice < mysql/practice.sql | Reloads only the practice tables (in practice) | Those tables reset |
docker compose down | Removes the containers (the images stay, so the next start is quick) | Gone |
- The containers have no volumes on purpose: the data lives inside each container, so
downdeletes it and the nextupstarts fresh. That makes "reset" one command, which is what you want for practice, and it's why a stop/start keeps your changes but down/up doesn't. - The reload commands feed a file into the client with
-T(no terminal, just input), and the word before<is the database to load it into. Each script drops its tables first (DROP TABLE IF EXISTS), so it can be run again at any time. Use them after the chapters that change data (Changing Data, Transactions and Concurrency) or the duplicate clean-ups in the practice questions. - To delete the images too (3.7 GB), run
docker image rm mysql:9.7 postgres:18 mongo:9.0 redis:8.10afterdown.
Every chapter was checked by typing its examples in page order into a freshly started playground and comparing each result with the page. Almost all of them print exactly what's shown; the exceptions are predictable:
- Error messages name the database. The examples were run in a database per chapter, so an error on the page says
'ch04.employees.name'where yours says'shop.employees.name'. - A query without
ORDER BYcan list rows in another order. The order isn't promised (see Sorting with ORDER BY), so two rows may come back swapped; one practice answer,JoeandAna, did exactly that. - Plans, costs and timings vary.
EXPLAINshows the same plan shape, but itscost=androws=estimates depend on table statistics, andEXPLAIN ANALYZEtimes depend on your machine. The text explains the shape, not the exact numbers. - Some examples need data first: open the "Starting data" box. When an example needs data the shop doesn't have, or the data put back after an earlier example changed it, a folded Starting data box right above it holds the exact statements that were run. Run them, then the example. Tables a topic creates on the page itself (
CREATE TABLE…INSERT) you type as part of the example. - Some chapters work in a database of their own, and their "Try it yourself" tip says which:
practicefor the interview problems,ch11for chapters 11–12,ch14(MySQL and PostgreSQL) for chapter 14,ch16…ch22in MongoDB (andch15for the review questions),ch25for the caching chapters. The rest useshop. - Status lines are left out. Most printed results skip
Query OK, 1 row affectedand3 rows in set, which your prompt prints after each statement. - A few examples needed more than one server: replication and sharding in Scaling, Replication and Operations, encryption at rest in Views, Stored Programs and Security, MongoDB replica sets of three, sharding, authentication and TLS in chapters 22–23. Those topics say so where they start; the playground has one server of each kind. One example can't be rebuilt at all, and its page says so: the MongoDB profiler output in chapter 23 ran on a collection whose data wasn't kept.
- "The sandbox" in the text is the machine the examples were run on: the same four databases, ports and password as your playground.
Any MySQL 8.4 or 9.x server works for the SQL chapters. Create the two databases and load the scripts with the mysql client (it asks for your root password each time):
mysql -u root -p -e "CREATE DATABASE shop; CREATE DATABASE practice"
mysql -u root -p shop < mysql/shop.sql
mysql -u root -p practice < mysql/shop.sql
mysql -u root -p practice < mysql/practice.sql- The first command creates the two empty databases; the others run the scripts into them, exactly as
load.shdoes in the playground. - Then open it with
mysql -u root -p shopand type away. MySQL 8.4 has nearly everything this site uses; the few 9.x-only features are labelled with their version where they appear.
| File | What it is |
|---|---|
| compose.yaml | The four containers, their ports, health checks and the files each one loads |
| mysql/load.sh | Runs on MySQL's first start: loads the shop into shop, and the shop plus the practice tables into practice |
| mysql/shop.sql | The sample shop for MySQL (the script in The sample schema used on this site) |
| mysql/practice.sql | The practice tables of Classic SQL Interview Problems, loaded into practice |
| mysql/client.cnf | Lets the mysql prompt in the container log in and open shop without a password |
| postgres/shop.sql | The sample shop for PostgreSQL: the same rows, with an ENUM type and identity columns |
| mongo/shop.js | The customers and orders for MongoDB, each order with its items embedded |