 |
16-11-2011, 04:02 PM
|
#1 (permalink)
|
|
Apprentice
Join Date: Mar 2008
Posts: 78
|
Sql program
There r two table .....
Person(pid,name,city)
parent(parentid,childid)
both parentid and childid are foreign key refering to person.pid
1. Find the name of gradparents of all people whose city name is kolkata.
2. Find the name of all people who lived in the same city as their father.
Please help to solve it.........
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
16-11-2011, 04:10 PM
|
#2 (permalink)
|
|
Stuck in Time...
Join Date: May 2009
Location: Land of Logic
Posts: 2,269
|
Re: Sql program
How can both parentid and childid be a foreign key refering to a single attribute, person.pid?
__________________
Marty: Hey, Doc, we better back up. We don't have enough road to get up to 88.
Doc Brown: Roads? Where we're going, we don't need, "roads!" :)
──── On the Internet you can be Anything you want. It's Strange that, so many people choose to be Stupid! ────
|
|
|
16-11-2011, 04:18 PM
|
#3 (permalink)
|
|
Apprentice
Join Date: Mar 2008
Posts: 78
|
Re: Sql program
Yes it is true ........this question is given in msc question .
|
|
|
20-11-2011, 07:54 PM
|
#4 (permalink)
|
|
Have fun till death!!
Join Date: Mar 2010
Location: Pune, Maharashtra
Posts: 24
|
Re: Sql program
Quote:
Originally Posted by subhransu123
There r two table .....
Person(pid,name,city)
parent(parentid,childid)
both parentid and childid are foreign key refering to person.pid
1. Find the name of gradparents of all people whose city name is kolkata.
2. Find the name of all people who lived in the same city as their father.
Please help to solve it.........
|
Hope this works!!
Answer 1:
Code:
select p.name
from Person p, parent pr, parent pr1
where pr.childid = pr1.parentid
AND pr.parentid = p.pid
AND p.city = "kolkalta";
Answer 2:
Code:
Select ch.name
from (select pr.parentid,pr.childid, p.city
from parent pr, person p
where pr.parentid = p.pid) fa,
(select pr.parentid,p.name, pr.childid, p.city
from parent pr, person p
where pr.childid = p.pid) ch
where fa.parentid = ch. parentid AND fa.city = ch.city;
|
|
|
20-11-2011, 10:19 PM
|
#5 (permalink)
|
|
VIP
Join Date: May 2008
Location: Jaipur
Posts: 187
|
Re: Sql program
Quote:
Originally Posted by vineet369
How can both parentid and childid be a foreign key refering to a single attribute, person.pid? 
|
Yes, it is possible, the foreign key may even reference a column from same table.
Quote:
|
Originally Posted by Wikipedia
The referencing and referenced table may be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known in SQL:2003 as a self-referencing or recursive foreign key.
|
__________________
Twitter: twitter.com/SharmaTushar
Facebook: facebook.com/tushar.sharma
|
|
|
20-11-2011, 10:56 PM
|
#6 (permalink)
|
|
Apprentice
Join Date: Mar 2008
Posts: 78
|
Re: Sql program
many many thanks to manaskumar...........
|
|
|
21-11-2011, 12:22 PM
|
#7 (permalink)
|
|
Sami Hyypiä, LFC legend
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
|
Re: Sql program
Quote:
Originally Posted by vineet369
How can both parentid and childid be a foreign key refering to a single attribute, person.pid? 
|
Of course it can. It doesn't mean both of the relations has to be with the same tuple.
Quote:
Originally Posted by manaskumar
Hope this works!!
Answer 1:
Code:
select p.name
from Person p, parent pr, parent pr1
where pr.childid = pr1.parentid
AND pr.parentid = p.pid
AND p.city = "kolkalta";
Answer 2:
Code:
Select ch.name
from (select pr.parentid,pr.childid, p.city
from parent pr, person p
where pr.parentid = p.pid) fa,
(select pr.parentid,p.name, pr.childid, p.city
from parent pr, person p
where pr.childid = p.pid) ch
where fa.parentid = ch. parentid AND fa.city = ch.city;

|
Glad that you helped the OP, but the point is the OP won't learn it this way with answer offered on the plate.
|
|
|
21-11-2011, 04:19 PM
|
#8 (permalink)
|
|
I am the night! I am....
Join Date: Aug 2009
Location: Gotham City
Posts: 4,209
|
Re: Sql program
@ Op
Manaskumar gave you an example of subqueries. You should read more SQL material to learn more.
In the above example, both parentid and childid referred to the primary key i.e pid. So a relation had to be made between parentid and childid in order to pick corresponding data. Using pid, which is foreign key to both parentid and childid, a relation is made between them.
Refer your second question. In the first subquery,manas picked parentid,childid and city from both tables satisfying the join condition where parentid and pid are equal and rest are filtered.
Similarly in the 2nd subquery, the join condition was childid = pid. So rest of the data are filtered.
Now you got all the childid and parentid which are equal to pid and thus a relation between them is formed. Finally you pick name of child satisfying the above condition and an extra AND condition is given to match the city name.
__________________
core i5 750, biostar h55 A+ (X16+X4), 4gb 1333 ddr3, corsair vx450, cm elite 335,Asus EAH 5750 FORMULA , samsung 2033 sw plus, wd green 1tb , wd 1tb my book , hp dvd writer,Apc 650 va, logitech z313
|
|
|
21-11-2011, 05:48 PM
|
#9 (permalink)
|
|
Fullbring
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
|
Re: Sql program
^^oh yeah what Manaskumar posted is correct..
here he used inner join + self join methodology
I remember similar type of question in interview
"Employee Table with data of both normal employees & manager...
write the query to find list of managers..."
ans is self join...
__________________
I'm the One you've been Waiting for...
|
|
|
23-11-2011, 08:22 PM
|
#10 (permalink)
|
|
VIP
Join Date: May 2008
Location: Jaipur
Posts: 187
|
Re: Sql program
Answer 1 isn't fully correct. It won't give the names of the grandparents if they didn't live in Kolkata. If a person lives in Kolkata, and his grandparents' data exist, it should display it.
EDIT:
Code:
select p2.name
from Person p, Person p2, parent pr, parent pr1
where pr.childid = pr1.parentid
AND pr1.childid = p.pid
AND p.city = 'Kolkata'
AND p2.pid = pr.parentid;
I edited manaskumar's query and think this might be correct, but looks very unoptimised. I'll try something from scratch later (Have Microprocessor exam tomorrow  ).
Last edited by RazorbladeXtreme; 23-11-2011 at 08:31 PM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|