0

I am just a new beginner in MySQL and I wanted to write the a SELECT statement that returns customers IDs and customer names(alphabetical order) for customers who live in Indiana, Ohio,Michigan, and Illinois, and whose names begin with the letters A or B.

Here is the CUSTOMER_TBL structure using the DESCRIBE CUSTOMER_TBL;

    Name                         Null?                      Type
    -------------------------------------------------------------
   CUST_ID                      NOT NULL                   VARCHAR2(10)
   CUST_NAME                    NOT NULL                   VARCHAR2(30)
   CUST_ADDRESS                 NOT NULL                   VARCHAR2(20)
   CUST_CITY                    NOT NULL                   VARCHAR2(12)
   CUST_STATE                   NOT NULL                   CHAR(2)
   CUST_ZIP                     NOT NULL                   CHAR2(5)
   CUST_PHONE                                              NUMBER(10)
   CUST_FAX                                                NUMBER(10)

Here is my solution.I just need to know if I am correct. thanks

   SELECT CUST_ID,CUST_NAME FROM CUSTOMER_TBL
   WHERE IN('Indiana','Ohio','Ohio','Michigan','Illinois') AND WHERE LIKE(A% OR B%)
   ORDER BY CUST_ID,CUST_NAME
3
  • 2
    "Where " clause is completely wrong , the query is simple learn the syntax. i just u to learn from here "w3schools.com/sql/sql_where.asp". It will be better than giving u the answer Commented Apr 9, 2012 at 11:59
  • 1
    ... and if you need to learn something about w3schools.com itself, you need to go here: w3fools.com Commented Apr 9, 2012 at 12:04
  • first of all clear your basics, you missed to write column name after where Commented Apr 9, 2012 at 12:06

3 Answers 3

1
SELECT CUST_ID,CUST_NAME FROM CUSTOMER_TBL
WHERE CUST_STATE IN('Indiana','Ohio','Ohio','Michigan','Illinois') AND (CUST_NAME LIKE 'A%' OR CUST_NAME LIKE 'B%') 
ORDER BY CUST_ID,CUST_NAME
Sign up to request clarification or add additional context in comments.

Comments

0

Your CUST_STATE is a CHAR(2) column so I assume that it contains state codes, not state names. So instead of Indiana, Ohio etc you should use state codes:

SELECT CUST_ID, CUST_NAME
FROM CUSTOMER_TBL
WHERE CUST_STATE IN ('IN','OH','MI','IL') AND (
    CUST_NAME LIKE 'A%' OR
    CUST_NAME LIKE 'B%'
)
ORDER BY CUST_NAME

Correct the state codes in case they are wrong.

2 Comments

@deadrunk: I noticed the typo (if that is what you were referring to) and fixed it.
yeah, i was talking about the first like expression
0

You have couple of things wrong in your query:

  • You should first try it, not just asking is it correct or not.
  • When nesting conditions in the WHERE statement, don't repeat the where itslef.
  • You should mention the column you are trying to match against the condition.

So, your query should look like:

SELECT CUST_ID, CUST_NAME 
FROM CUSTOMER_TBL
WHERE CUSTOMER_City IN('Indiana','Ohio','Ohio','Michigan','Illinois') 
    AND ( CUST_NAME LIKE 'A%' OR CUST_NAME LIKE 'B%' )
ORDER BY CUST_ID,CUST_NAME

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.