Yamah Karim
Yamah Karim

Reputation: 7

SQL Query multiple WITH statements

I am fairly new to querying with SQL. I have a large data file containing customer data. I am looking to generate lead by querying all the data for customers that fulfill a few conditions:

60k annual household income

Homeowner of 200k + home

680+ credit

Under 64 years old

States AZ, CA, CO, NH, FL, NJ, MA, RI, NM, NV, IL, TX, UT.

I have written a preliminary query but I am sure my syntax is off. Can someone please help me getting this query to run?

What am I missing in the syntax to connect these conditionals? Here's the query:

SELECT * FROM `tablename` 
where state in 'AZ' or 
  state like 'CO' or 
  state like 'CA' or
  state like 'NH' or
  state like 'FL' or
  state like 'NJ' or
  state like 'MA' or
  state like 'RI' or
  state like 'NM' or
  state like 'NV' or
  state like 'IL' or
  state like 'TX' or
  state like 'UT' and 
    where estimatedcurrenthomevaluecode  like 'I' or
    estimatedcurrenthomevaluecode like 'J' or
    estimatedcurrenthomevaluecode like 'K' or
    estimatedcurrenthomevaluecode like 'L' or
    estimatedcurrenthomevaluecode like 'M' or
    estimatedcurrenthomevaluecode like 'N' or
    estimatedcurrenthomevaluecode like 'O' or
    estimatedcurrenthomevaluecode like 'P' or
    estimatedcurrenthomevaluecode like 'Q' or
    estimatedcurrenthomevaluecode like 'R' or
    estimatedcurrenthomevaluecode like 'S' and 
      personexactage    < 65 and 
       where estimatedincomecode like 'M' or 
             estimatedincomecode like 'N' or
             estimatedincomecode like 'O' or
             estimatedincomecode like 'P' or
             estimatedincomecode like 'Q' or
             estimatedincomecode like 'R' or 
             estimatedincomecode like 'S' and 
              where CreditRating like 'D' or 
                    CreditRating like 'C' or
                    CreditRating like 'B' or
                    CreditRating like 'A';

Upvotes: 0

Views: 205

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

You seem to want:

where state in ('AZ', 'CO', . . .) and
      estimatedcurrenthomevaluecode in ('I', 'J', . . .) and
      personexactage  < 65 and 
      estimatedincomecode in ('M', . . . ) and
      . . . 
  

There is only one where clause.

Upvotes: 1

Related Questions