retagged by
6,042 views
32 32 votes

Suppose a database consist of the following relations:

SUPPLIER (SCODE,SNAME,CITY).
PART (PCODE,PNAME,PDESC,CITY).
PROJECTS (PRCODE,PRNAME,PRCITY).
SPPR (SCODE,PCODE,PRCODE,QTY).

 Write SQL programs corresponding to the following queries:

  1. Print PCODE values for parts supplied to any project in DEHLI by a supplier in DELHI.
  2. Print all triples <CITY, PCODE, CITY> such that a supplier in first city supplies the specified part to a project in the second city, but do not print the triples in which the two CITY values are same.

6 Answers

Best answer
30 30 votes
  1. Print PCODE values for parts supplied to any project in DELHI by a supplier in DELHI 
Select SP.PCODE
From SPPR SP, Projects PR, Supplier SU
Where SP.PRcode = PR.PRcode
and SU.Scode = SP.Scode
and PR.PRcity = "DELHI"
and SU.city = "DELHI";
  1. Print all triples <CITTY, PCODE, CITY>
Select SU.city, SP.Pcode,PR.PRcity
from Supplier SU, Projects PR, SPPR SP
Where SU.Scode = SP.Scode
And PR.PRcode = SP.PRcode
And SU.city <> PR.PRcity;
edited by
18 18 votes
i) $\pi _{pcode}(\sigma _{city==prcity=="Delhi"\;}(sppr\Join supplier\Join project))$

ii) $\pi _{city, \;pcode,\;prcity}(\sigma _{city!=prcity}(sppr\Join supplier\Join project))$
8 8 votes
a)i) select pcode from sprrr where prcode IN (select prcode from project where city="delhi") AND
scode IN (select scode from supplier where city="delhi")

ii) select s.city,p.pcode,pr.city from supplier s, project pr, parts p, sprr sp
where s.city != pr.city AND sp.scode=s.scode AND p.pcode=sp.pcode AND sp.prcode=pr.prcode
4 4 votes

i) Print PCODE values for parts supplied to any project in DEHLI by a supplier in DELHI.

SELECT PCODE FROM SPPR, PROJECTS, SUPPLIER WHERE PROJECTS.PRCITY="DELHI" AND SUPPLIER.CITY="DELHI" AND SPPR.PRCODE = PROJECTS.PRCODE AND SPPR.SCODE = SUPPLIER.SCODE;

ii) Print all triples <CITY, PCODE, CITY> such that a supplier in first city supplies the specified part to a project in the second city, but do not print the triples in which the two CITY values are same.

SELECT SUPPLIER.CITY, SPPR.PCODE, PROJECTS.PRCITY FROM SUPPLIER, SPPR, PROJECTS WHERE SUPPLIER.CITY <> PROJECTS.PRCITY AND SUPPLIER.SCODE = SPPR.SCODE AND PROJECTS.PRCODE = SPPR.PRCODE;

0 0 votes

I) More efficient query

Select A.pcode

from SPPR as A JOIN Projects as PR ON A.PRcode=PR.PRcode JOIN Supplier as SU ON SP.Scode=SU.Scode

where SP.city="Delhi" and PR.city="Delhi" ;

0 0 votes

Using Natural join 

(i)    

select PCODE
from SUPPLIER S natural join SPPR SP join PROJECTS P using(PRCODE)
where P.PRCITY = "Delhi" and S.CITY = "Dehi"

(ii) 

select S.CITY, SP.PCODE, P.PRCITY
from SUPPLIER S natural join SPPR SP natural join PROJECTS P 
where P.PRCITY <> S.CITY 

 

edited by
Position:
Show:

Related questions

50 50 votes
4 answers 4 answers
9.8k
9.8k views
Kathleen asked Sep 12, 2014
9,828 views
Match the pairs in the following questions by writing the corresponding letters only.$$\begin{array}{|c|l|c|l|} \hline A. & \text{The number of distinct binary tree} & P....
16 16 votes
3 3 answers
5.2k
5.2k views
go_editor asked Apr 18, 2016
5,171 views
Suppose a database consist of the following relations:SUPPLIER (SCODE,SNAME,CITY). PART (PCODE,PNAME,PDESC,CITY). PROJECTS (PRCODE,PRNAME,PRCITY). SPPR (SCODE,PCODE,PRCOD...
70 70 votes
4 answers 4 answers
13.3k
13.3k views
Kathleen asked Sep 12, 2014
13,296 views
Find the number of binary strings $w$ of length $2n$ with an equal number of $1's$ and $0's$ and the property that every prefix of $w$ has at least as many $0's$ as $1's....
15 15 votes
3 3 answers
6.5k
6.5k views
Kathleen asked Sep 12, 2014
6,503 views
Consider the following scheme for implementing a critical section in a situation with three processes $P_i, P_j$ and $P_k$.Pi; repeat flag[i] := true; while flag [j] or f...