Bird
0
0

You have tables:

hard📝 Application Q8 of 15
SQL - Table Relationships
You have tables:
Authors(id, name)
Books(id, title, author_id)
How would you write a query to list all authors and their books, including authors with no books?
ASELECT Authors.name, Books.title FROM Authors LEFT JOIN Books ON Authors.id = Books.author_id;
BSELECT Authors.name, Books.title FROM Authors INNER JOIN Books ON Authors.id = Books.author_id;
CSELECT Authors.name, Books.title FROM Books LEFT JOIN Authors ON Books.author_id = Authors.id;
DSELECT Authors.name, Books.title FROM Authors RIGHT JOIN Books ON Authors.id = Books.author_id;
Step-by-Step Solution
Solution:
  1. Step 1: Identify requirement

    List all authors, even those without books, so include unmatched authors.
  2. Step 2: Choose correct JOIN type

    LEFT JOIN from Authors to Books keeps all authors.
  3. Final Answer:

    SELECT Authors.name, Books.title FROM Authors LEFT JOIN Books ON Authors.id = Books.author_id; -> Option A
  4. Quick Check:

    LEFT JOIN includes unmatched left table rows [OK]
Quick Trick: Use LEFT JOIN to include all from left table [OK]
Common Mistakes:
MISTAKES
  • Using INNER JOIN excludes authors without books
  • Confusing LEFT and RIGHT JOIN

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes