title: How to create a SQL search query with varying arguments
body: <p>I'm implementing search functionality into my application, and I need to write the SQL query that will search the database for records based on a wide range of possible parameters. I'm including a sample of what I have so far:</p>
  <pre><code>SELECT m.*, a.*, p.*, e.*
  FROM members m
    LEFT JOIN addresses a ON m.member_id = a.address_member_id AND a.preferred_address = TRUE
    LEFT JOIN phones p ON m.member_id = p.phone_member_id AND p.preferred_phone = TRUE
    LEFT JOIN emails e ON m.member_id = e.email_member_id AND e.preferred_email = TRUE
  WHERE m.member_id IN (
      (SELECT address_member_id 
       FROM addresses 
       WHERE address = '123 Hart Ct' AND unit = NULL AND city = 'Hometown' AND state = 'NJ' AND zip_code = '08895'),
      (SELECT phone_member_id 
       FROM phones 
       WHERE area_code = '732' AND prefix = '619' AND line_number = '7826'),
      (SELECT email_member_id
       FROM emails
       WHERE email_address = 'craigmiller160@gmail.com')
  )
  AND m.first_name = 'Jane' AND m.middle_name = 'Deborah' AND m.last_name = 'Foster'
  ORDER BY member_id ASC;
  </code></pre>
  <p>As you can see, I'm searching for records in the &quot;members&quot; table based on the values in that table, as well as the values of the &quot;preferred&quot; address, phone, email, etc, that are associated with it.</p>
  <p>The problem is that the query above requires all of those values to be provided. In most cases, the user will only provide a handful of the actual values to perform the search.</p>
  <p>Now, I know that one option is to build the query dynamically in my application layer, concatenating Strings together and whatnot. That way the WHERE clauses would only have the values that are needed for that specific query. But before I do that, I'm wondering if there's any way to accomplish this in a purely-SQL way?</p>