0

I am new to SQL and I have this question on a homework assignment: "List the ids of customer representatives whose id is under 100 along with the total number of customers that each of these representatives serves in zip codes starting with 9 or 0 (combined)"

SO far I have this code:

SELECT cust_rep, count(cust_nbr) as "CustomerCount"
FROM customer
WHERE cust_rep < 100
GROUP BY cust_rep
ORDER BY cust_rep;

But I do not know how to add in the zip code restriction. Thanks for your help!

3
  • what do you mean by combined? Commented Apr 19, 2018 at 17:28
  • You have tagged the question with "sql-like" - do you intend that the solution actually uses LIKE? Commented Apr 19, 2018 at 17:31
  • What database are you suing? Commented Apr 19, 2018 at 17:41

2 Answers 2

1

This will do what your query is doing in addition to makeusre sure the first chararacter of the zipcode is either a 0 or a 9

SELECT cust_rep, count(cust_nbr) as "CustomerCount"
FROM customer
WHERE cust_rep < 100
AND (
    // adding quotes to avoid implicit conversion
    LEFT(ZipCode, 1) = '0'
    OR LEFT(ZipCode, 1) = '9'
)

GROUP BY cust_rep
ORDER BY cust_rep;
Sign up to request clarification or add additional context in comments.

4 Comments

While the database is likely to convert the 0 to '0', it would be better to explicitly put a string there instead of hoping that the conversion will do what you want. Or it might be that it converts LEFT(ZipCode, 1) to a number - I can't remember which will take precedence: can you?
If the zipcode is a number already, I think it will keep it as a number so it will compare 0 = 0 , it wont do implicit conversion. If it is a varcahr, i think it will do an implicit conversion to convert the LEFT() value of '0' to a int of 0 on the right and then match that to the 0. Either way it would work as long as the LEFT is guaranteed to return a number it just may do implicit conversions.
LEFT is a string function, so it cannot keep it as a number. You should provide at least a link to documentation which gives the conversion precedence rules to explain why you have not put quotes around 0 and 9. Or just put the quotes in. This is the second hint to avoid a downvote ;)
I updated to add single quotes and a note about implicit conversion. I tested the version out without the quotes on one of my tables in my DB before posting to make sure. :)
0

Try this.

SELECT CUST_REP, COUNT(*) as "CustomerCount"
FROM CUSTOMER 
WHERE cust_rep < 100 
   AND (zipCode like '0%' OR zipCode like '9%')
GROUP BY cust_rep
ORDER BY cust_rep;

3 Comments

Also, without knowing the type of the zip column, this could be incorrect (yes, I saw a real DB with numeric zips-don't ask me why).
numeric zips can't started with 0 ;)

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.