title: SQL select in n to m relationship
body: <p>I have an <em>n-to-m</em> relationship between <code>Author</code> and <code>Book</code>.</p>
  
  <p><strong>Table Author</strong></p>
  
  <pre><code>ID       Name
  1        Follett  
  2        Rowling
  3        Martin
  </code></pre>
  
  <p><strong>Table Book</strong></p>
  
  <pre><code>ID     Title                       Category 
  1        A Dance with Dragons      Fantasy
  2        Harry Potter              Fantasy
  3        The Key to Rebecca        Thriller
  4        World without end         Drama
  </code></pre>
  
  <p><strong>Table book_author</strong></p>
  
  <pre><code>authorId       bookId
  1        3  
  2        2
  3        1
  1        4
  </code></pre>
  
  <p>There are a lot more authors and books in the system. Now I want to select all authors that have a book in genre "<em>Fantasy</em>".</p>
  
  <p>This is what I came up so far with:</p>
  
  <pre><code>   select distinct a.id 
     from author a, book b, written w 
     where w.authorId = a.id and w.bookId = b.id and b.category = "Fantasy";
  </code></pre>
  
  <p>I am wondering how to optimize this query since especially table book is really large.</p>