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: Print PCODE values for parts supplied to any project in DEHLI by a supplier in DELHI. 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. Databases gate1991 databases sql normal descriptive + – Kathleen 6.0k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 30 30 votes 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"; 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; Manu Thakur answered Oct 24, 2017 • edited Apr 17, 2021 by Lakshman Bhaiya Manu Thakur comment Share Follow See all 4 Comments 4 4 Comments reply txds commented Sep 16, 2019 i edited by txds Sep 17, 2019 reply Follow flag MySQL Queries - Part (1) - Delhi to Delhi select pcode from Sppr where scode in (select scode from Supplier where city="Delhi") and prcode in (select prcode from Project where prcity="Delhi"); Part (2) - Supplier and Project belong to two different cities create view required as (select scode, pcode, prcode from Sppr where (scode, prcode) not in (select scode, prcode from Supplier join Project on city=prcity)); select city, pcode, prcity from (select city, prcode, pcode from required left join Supplier on required.scode = Supplier.scode) as T1 left join Project on T1.prcode = Project.prcode; 0 0 replyShare the_bob commented Nov 24, 2020 reply Follow flag Why is the ‘Part’ table not used at all here? 0 0 replyShare Pranavpurkar commented Aug 8, 2022 reply Follow flag the_bob bcoz not needed! 0 0 replyShare ash_khola commented Jul 12, 2024 reply Follow flag Why too much of hustle when we have natural join For 1st part:Select SPPR.PCODE fromSUPPLIER naural join SPPR natural join PROECTS where SUPPLIER.city = "DELHI"and PROJECTS.city = "DELHI"For 2nd part:Select SUPPLIER.CITY, SPPR.PCODE, PROJCTS.CITYfrom SUPPLIER naural join SPPR natural join PROECTS where SUPPLER.city <> PROJECTS.city 4 4 replyShare Please log in or register to add a comment.
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))$ Sheshang answered Nov 25, 2016 Sheshang comment Share Follow 0 reply Please log in or register to add a comment.
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 Aravind answered Oct 4, 2014 Aravind comment Share Follow See all 3 Comments 3 3 Comments reply Vertika Srivastava commented Dec 29, 2015 reply Follow flag is this required to be compared ? "AND p.pcode=sp.pcode AND " i think its not needed 3 3 replyShare Piyush Nikam 5 commented Oct 26, 2017 reply Follow flag 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") [Question] - Why can't we have multiple tables with join like second query here? I'm not thinking of performance here. 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 0 0 replyShare tusharp commented Nov 11, 2018 reply Follow flag @Piyush Nikam 5 you can do that. Coming to the performance improvement we should do selection before join operation i.e first selecting all Delhi suppliers and Delhi projects from respective tables nad then do join operation. 0 0 replyShare Please log in or register to add a comment.
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; Ahwan answered Sep 24, 2017 Ahwan comment Share Follow 0 reply Please log in or register to add a comment.
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" ; Nitesh Singh 2 answered Oct 30, 2018 Nitesh Singh 2 comment Share Follow 0 reply Please log in or register to add a comment.
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 0xprateek answered Apr 7, 2021 • edited Apr 7, 2021 by 0xprateek 0xprateek comment Share Follow 0 reply Please log in or register to add a comment.