Skip to main content

Schema Diagram Lab

Original interactive lab for this course — built on the Reddit-style users / posts schema from the SQL lesson, extended here with a comments table for the exercise below.

Query-writing is only half of the "SQL & DB design" interview question — the other half is reading (or drawing) an entity-relationship diagram and reasoning about foreign keys. This lab is a small draggable canvas: cards represent tables, and you connect them the way you'd sketch a schema on a whiteboard.

Explore: the shipped schema​

This is the exact two-table schema the SQL lesson queries against. Drag the cards around, or rewire the connection — nothing here is graded, it's just the same diagram rendered as a canvas instead of a markdown table.

Notice the wire attaches to the specific user_id row on each card, not just "somewhere on the box" — table cards connect field-to-field, the way an ER tool would.

Exercise: wire the extended schema, and spot the missing column​

Reddit posts also have comments. Below are three tables — users, posts, and a new comments table — with one bug: comments is missing the foreign key back to users (right now it only knows which post it's under, not who wrote it). You'll need to:

  1. Add the missing field. Click the dashed + field name row inside comments, type user_id, give it the badge FK, and press Enter (or click +).
  2. Wire the foreign keys, field to field: drag from posts.user_id to users.user_id, from comments.post_id to posts.post_id, and from the comments.user_id you just added to users.user_id. Direction matters (drag from the table that has the FK to the table it references); which side of the card you start from doesn't.

Click Check once you think it's done — after two wrong checks, whatever's still missing is revealed.

comments ends up with two foreign keys — one to posts, one to users — which is exactly the kind of "wait, doesn't this table need another FK" detail interviewers probe for when they hand you a schema with a subtly missing column.