Using Subqueries in WHERE with IN in PostgreSQL
📖 Scenario: You work at a bookstore that keeps track of books and their authors in two tables. You want to find all books written by authors from a specific country.
🎯 Goal: Build a SQL query using a subquery with IN in the WHERE clause to list all books by authors from 'Canada'.
📋 What You'll Learn
Create a table called
authors with columns author_id, name, and country.Create a table called
books with columns book_id, title, and author_id.Insert the specified data into both tables.
Write a SQL query that selects
title from books where author_id is in a subquery selecting author_id from authors where country is 'Canada'.💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to find books by authors from specific countries or categories. Using subqueries with IN helps filter data efficiently.
💼 Career
Database developers and analysts use subqueries to write complex queries that combine data from multiple tables based on conditions.
Progress0 / 4 steps