Using Subqueries with IN Operator in MySQL
📖 Scenario: You work for a small bookstore that keeps track of books and their authors in two tables. You want to find all books written by authors who have published more than one book.
🎯 Goal: Create a MySQL query using a subquery with the IN operator to list all books by authors who have more than one book in the database.
📋 What You'll Learn
Create a table called
authors with columns author_id (integer) and author_name (varchar).Create a table called
books with columns book_id (integer), title (varchar), and author_id (integer).Insert exactly 5 authors into the
authors table with these author_id values: 1, 2, 3, 4, 5.Insert exactly 7 books into the
books table with these exact titles and author_ids:'The Lost World', 1, 'Dinosaur Tales', 1, 'Ocean Depths', 2, 'Mountain High', 3, 'City Lights', 3, 'Desert Winds', 4, 'Forest Whisper', 5.Write a subquery that finds
author_id values for authors with more than one book.Use the
IN operator with the subquery to select all books by those authors.💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to find books by authors with multiple works to create special collections or promotions.
💼 Career
Understanding subqueries and the IN operator is essential for database querying roles, data analysis, and backend development.
Progress0 / 4 steps