Question

yes this is a homework question, but I'm giving it honest effort and I'm not asking you to do it for me, just point me in the right direction! :)

So I have a set of questions concerning the SQL IN subquery, and while I understand what it does I'm having trouble applying it in these situations.

The first question:

1) Use the In subquery technique to list the customer number, customer name and phone for customers who have paid either by visa or mc. Sort by customer name.

This one I believe I got:

SELECT DISTINCT
   tblCust.CustomerNum, CustomerName, Phone
FROM
   tblCust
INNER JOIN tblOrd ON
   tblCust.CustomerNum=tblOrd.CustomerNum
WHERE
   tblOrd.PayMethod IN ("visa", "mc")
ORDER BY CustomerName;

But the next one just makes no sense to me:

2) Use the In subquery technique to list from tblCust the customer number, customer name and state for customers who have a state description that begins with “co”.

Shouldn't I be using the LIKE operator for this instead of IN? So far I have:

SELECT
   CustomerNum, CustomerName, StateProv
FROM
   tblCust
INNER JOIN tblState ON
   tblCust.StateProv = tblState.StateProv
WHERE 
   tblState.StateProvDesc IN 

And I don't know what to put after the IN, if I were using LIKE I would say

...StateProvDesc LIKE "co%"

and be done with it, but I don't think that's what my teacher wants. Finally, the third question:

3) Use the In subquery technique to list from tblCust the customer name, country for customers who have at least one order in the range of $90,000-$120,000. Sort by country then customer name.

Shouldn't between be used for a range of numbers?

Thanks for anything you guys can think of!

using microsoft access by the way

Was it helpful?

Solution

I think what the prof wants you to do is write a subquery within the IN clause. You can actually put a select statement within IN() like this:

SELECT
   CustomerNum, CustomerName, StateProv
FROM
   tblCust
WHERE StateProv IN(SELECT StateProv FROM tblState WHERE StateProvDesc LIKE 'co%')

OTHER TIPS

I think the instructor wants you to change the query to use ... IN (SELECT ... FROM ...) style instead of the inner join.

You can work on these queries from the back of the description, like this:

...a state description that begins with “co”.

-- Inner query
SELECT StateProv
FROM tblState
WHERE StateProvDesc LIKE `co%`

Now that you have this, you can compose the outer query:

list from tblCust the customer number, customer name and state for customers who have...

SELECT
   CustomerNum, CustomerName, StateProv
FROM
   tblCust
WHERE tblCust.StateProv IN (
-- Your inner query goes here
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top