Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Urgent help needed!! reg: nested SQL query (using Select but tricky ) 1

Status
Not open for further replies.

sjy

Programmer
Oct 9, 2002
1
US
Customer-Stock-order Database:

Customer (C) (CNO, CNAME, CITY) CNO is th primary key
Order (O) (ONO, CNO, ORDER_DATE) ONO,CNO are primary keys
Order_detail (OD) (ONO, SNO, QTY) ONO,SNO are primary keys
Stock(S) ( SNO, SNAME, PRICE, SLEVEL) SNO is th primary key


1)List the names of the stock items that have been ordered by all customers.

The query looks simple but it took a lot of time to me..it is just using a couple of selects statements and nesting but finally I am ending up confused..
I have tried my best but was unable to come up with the SQL query for the above question. So could anyone please let me know the solution for this query.

Thanks for the support!!I shall be looking forward for responses..
sjy
 
try this:

select distinct s.sname
from stocks s,order_detail od, order o
where o.ono=od.ono
and s.sno=od.sno
group by s.sname
having count(distinct o.cno)=(select count(c.cno) from customer c)

senior rdbms specialist
 
Hello sjy,

When you say 'by all customers', this could mean just the stock names that were ever ordered, since all orders are associated to a t least one customer.

so then ...

Customer (C) (CNO, CNAME, CITY) CNO is th primary key
Order (O) (ONO, CNO, ORDER_DATE) ONO,CNO are primary keys
Order_detail (OD) (ONO, SNO, QTY) ONO,SNO are primary keys
Stock(S) ( SNO, SNAME, PRICE, SLEVEL) SNO is th primary key
Code:
select s.sno
       s.sname
from   stock s
       order o
where  s.sno = o.sno
Expanding this, you can retrieve the count of orders and sum of quantity as follows
Code:
select s.sno
       s.sname
       count(o.ono)
       sum(o.qty)
from   stock s
       order o
where  s.sno = o.sno
group by
       s.sno
       s.sname
If your dbms allows, you may also add the following to your select to collect the distinct count of customers per stock item:
Code:
count ( distinct o.cno)

Hope this helps
Cheers AA 8~)
 
Hello sjy,

When you say 'by all customers', this could mean just the stock names that were ever ordered, since all orders are associated to a t least one customer.

so then ...
Code:
select s.sno
       s.sname
from   stock s
       order o
where  s.sno = o.sno
Expanding this, you can retrieve the count of orders and sum of quantity as follows
Code:
select s.sno
       s.sname
       count(o.ono)
       sum(o.qty)
from   stock s
       order o
where  s.sno = o.sno
group by
       s.sno
       s.sname
If your dbms allows, you may also add the following to your select to collect the distinct count of customers per stock item:
Code:
count ( distinct o.cno)

Hope this helps
Cheers AA 8~)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top