Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Tough query to me

Author  Topic 

youmere
Starting Member

1 Post

Posted - 2010-03-19 : 15:54:11
Being a little sql query writer finally I failed to analyze and write the following query and just going to ask to my teacher in few days but before the act I am posting here to get it known.

Entities:

Suppliers (SId, Name, Address)
Catalog (SId, PId)
Parts (PId, Name, Color)

Write a query to find the names of suppliers who supply every part.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-20 : 01:08:28
hint: use CROSS JOIN to get all possible combinations of suppilers against Parts
then use LEFT JOIN the above result with your Catalog table on ids (SId, PId) and look for ones which doesnt return NULL at all.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-03-20 : 02:38:53
I am not getting how this will give the expected output


Create table Suppliers (SId int, Name varchar(10), Address varchar(20) )
go
insert into suppliers values(1, 's1', 'add1' )
insert into suppliers values(2, 's2', 'add2' )
insert into suppliers values(3, 's3', 'add3' )
insert into suppliers values(4, 's4', 'add4' )
go
Create table Parts (PId int, Name varchar(10), Color varchar(20))
insert into parts values(1, 'p1', 'c1')
insert into parts values(2, 'p2', 'c2')
insert into parts values(3, 'p3', 'c3')
go
Create table Catalog (SId int, PId int)
go
insert into catalog values(1, 1)
insert into catalog values(2, 1)
insert into catalog values(2, 2)
insert into catalog values(2, 3)
insert into catalog values(3, 2)
insert into catalog values(3, 3)
insert into catalog values(4, 2)
go

SELECT a.suppliername, * FROM
(
SELECT SId, s.Name SupplierName, Address, PId, p.Name PartName, Color
FROM suppliers s cross join parts p
) a
left join catalog c on c.sid = a.sid and c.pid = a.pid
WHERE c.sid is not null and c.pid is not null


Vaibhav T
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-20 : 03:01:50
you're tempting me to spoon feed


SELECT a.suppliername FROM
(
SELECT SId, s.Name SupplierName, Address, PId, p.Name PartName, Color
FROM suppliers s cross join parts p
) a
left join catalog c on c.sid = a.sid and c.pid = a.pid
GROUP BY a.suppliername
HAVING COUNT(*)-COUNT(c.pid)=0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-03-20 : 05:45:06
Sometime i forgot how to hold the spoon.

Vaibhav T
Go to Top of Page
   

- Advertisement -