title: Improving the query performance
body: <p>I have the following table for document storage. The document could be either a book with multiple pages, page numbering starting from 0 or a singlepage document.</p>
  
  <pre><code>Category
  Title
  PageNo - integer
  LineNo integer
  Key
  Value
  </code></pre>
  
  <p>I need a query that pulls all the keys from the table that belong to one-page documents and Category1.</p>
  
  <p>I have the following query:</p>
  
  <pre><code>SELECT DISTINCT Key 
  FROM Materials
  WHERE Category='Category1' and Key NOT IN 
  (SELECT DISTINCT Key 
  from Materials 
  WHERE Category='Category1'
  and PageNo&gt;0)
  </code></pre>
  
  <p>The query works fine excluding all the materials that have more than 1 page. I wanted to rewrite the query to improve it in terms of performance and also to avoid any repeating code such as keyword DISTINCT and WHERE clause, if possible. I'd appreciate any pointers in the right direction.</p>