0
0
MySQLquery~30 mins

Subqueries with EXISTS in MySQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Subqueries with EXISTS in MySQL
📖 Scenario: You are managing a small online bookstore database. You want to find authors who have written books that are currently in stock.
🎯 Goal: Build a MySQL query using a subquery with EXISTS to find all authors who have at least one book available in stock.
📋 What You'll Learn
Create a table called authors with columns author_id and author_name.
Create a table called books with columns book_id, title, author_id, and in_stock.
Write a query using EXISTS to select authors who have books with in_stock greater than 0.
💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to find authors who have available books for sale or loan. Using EXISTS helps efficiently check related data.
💼 Career
Database developers and analysts use subqueries with EXISTS to filter data based on related tables, a common task in real-world SQL querying.
Progress0 / 4 steps
1
Create the authors table and insert data
Create a table called authors with columns author_id (integer) and author_name (varchar 50). Insert these exact rows: (1, 'Alice Walker'), (2, 'Mark Twain'), (3, 'Jane Austen').
MySQL
Need a hint?

Use CREATE TABLE to define the table and INSERT INTO to add rows.

2
Create the books table and insert data
Create a table called books with columns book_id (integer), title (varchar 100), author_id (integer), and in_stock (integer). Insert these exact rows: (101, 'The Color Purple', 1, 5), (102, 'Adventures of Huckleberry Finn', 2, 0), (103, 'Pride and Prejudice', 3, 3).
MySQL
Need a hint?

Define the books table with the specified columns and insert the given rows.

3
Write a query using EXISTS to find authors with books in stock
Write a SELECT query to get author_name from authors where there exists at least one book in books with the same author_id and in_stock greater than 0. Use a subquery with EXISTS.
MySQL
Need a hint?

Use EXISTS with a subquery that checks for books with matching author_id and in_stock > 0.

4
Complete the query with ordering
Add an ORDER BY clause to the query to sort the results by author_name in ascending order.
MySQL
Need a hint?

Use ORDER BY author_name ASC at the end of the query to sort alphabetically.