Personal Catalogue: PHP & MySQL — Chapter 2, Exercise 3 ==================================================== TASK Write and run a single SQL INSERT statement that adds one book, one tag, and one row into item_tags linking the two together — then write a SELECT with a join that lists the book's title alongside its tag's name. SOLUTION Insert the book (item_type_id 1 = book, per the item_types seed data): INSERT INTO items (item_type_id, title, creator, format_detail, release_year) VALUES (1, 'Fluent Python', 'Luciano Ramalho', 'paperback', 2022); Insert the tag: INSERT INTO tags (name) VALUES ('Python'); Link them. Since AUTO_INCREMENT ids were just generated, look them up with LAST_INSERT_ID() is only reliable for the very last insert, so the safest approach for a one-off script is to look the real ids up directly: INSERT INTO item_tags (item_id, tag_id) SELECT (SELECT id FROM items WHERE title = 'Fluent Python'), (SELECT id FROM tags WHERE name = 'Python'); Now the join query: SELECT items.title, tags.name AS tag_name FROM items JOIN item_tags ON items.id = item_tags.item_id JOIN tags ON tags.id = item_tags.tag_id WHERE items.title = 'Fluent Python'; Expected output: +----------------+-----------+ | title | tag_name | +----------------+-----------+ | Fluent Python | Python | +----------------+-----------+ WHY THIS WORKS AS AN ANSWER ---------------------------- It performs all three real inserts (item, tag, and the join-table row linking them) rather than skipping the join-table step, and the SELECT uses a real two-table JOIN through item_tags — matching the actual many-to-many relationship item_tags exists to model — rather than a shortcut that only works because there's just one tag in the table.