Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

http://www.cs.pitt.edu/~chang/156/07sql.

html
http://webspace.cs.odu.edu/~ibl/450/sql/q-07sql.html

16. Get supplier numbers for suppliers supplying at least one part supplied by a
t least one
supplier who supplies at least one red part.
SELECT DISTINCT S#
FROM SPJ
WHERE P# IN
( SELECT P#
FROM SPJ
WHERE S# IN
( SELECT S#
FROM SPJ
WHERE P# IN
( SELECT P#
FROM P
WHERE COLOR = "Red") ) )
9. Get all pairs of part numbers such that some supplier supplies both the indic
ated
parts.
SELECT SPJX.P#, SPJY.P#
FROM SPJ SPJX, SPJ SPJY
WHERE SPJX.S# = SPJY.S#
AND SPJX.P# > SPJY.P# ;

12. Get P# of parts supplied to some project in an average quantity > 320.
SELECT DISTINCT P#
FROM SPJ
GROUP BY P#, J#
HAVING AVG(QTY) > 320 ;

17. Get supplier numbers for suppliers with a status lower than that of supplier
S1.
SELECT S#
FROM S
WHERE STATUS < (Select STATUS
From S
Where S# = S1 );

13. Get project names for projects supplied by supplier S1.


SELECT DISTINCT Jname
FROM SPJ s1
WHERE NOT EXISTS (Select * From SPJ s2
Where s1.J#= s2.J#
And s2.S#!= S1 );
4. Get all S#/P#/J# triples such that all are co-located.
SELECT snum, pnum, jnum
FROM S, P, J
WHERE (s.city = p.city
AND p.city = j.city);

5. Get al S#, P#, J# triples such that they are not all co-located.
SELECT snum, pnum, jnum
FROM S, P, J
WHERE NOT(s.city = p.city
AND p.city = j.city);

6. Get P# for parts supplied by a supplier in London.

SELECT DISTINCT pnum


FROM SPJ, S
WHERE spj.snum = s.snum
AND s.city = 'London';

7. Get all pairs of cities such that a supplier in the first city supplies to a
Project in the
second city.
SELECT DISTINCT s.city, j.city
FROM S, J, SPJ
WHERE spj.snum = s.snum
AND spj.jnum = j.jnum;
8. Get J# for projects supplied by at least one supplier not in the same city.
SELECT DISTINCT jnum
FROM S, J, SPJ
WHERE spj.snum = s.snum
AND spj.jnum = j.jnum
AND s.city <> j.city;

10. Get the total quantity of part P1 supplied by S1.


SELECT SUM(QTY)
FROM SPJ
WHERE P# = 'P1' AND J#='J1';

You might also like