Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 16-11-2011, 04:02 PM   #1 (permalink)
Apprentice
 
Join Date: Mar 2008
Posts: 78
Default 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.........
subhransu123 is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 16-11-2011, 04:10 PM   #2 (permalink)
Stuck in Time...
 
Vyom's Avatar
 
Join Date: May 2009
Location: Land of Logic
Posts: 2,269
Default 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! ────
Vyom is online now   Reply With Quote
Old 16-11-2011, 04:18 PM   #3 (permalink)
Apprentice
 
Join Date: Mar 2008
Posts: 78
Default Re: Sql program

Yes it is true ........this question is given in msc question .
subhransu123 is offline   Reply With Quote
Old 20-11-2011, 07:54 PM   #4 (permalink)
Have fun till death!!
 
manaskumar's Avatar
 
Join Date: Mar 2010
Location: Pune, Maharashtra
Posts: 24
Smile Re: Sql program

Quote:
Originally Posted by subhransu123 View Post
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;
manaskumar is offline   Reply With Quote
Old 20-11-2011, 10:19 PM   #5 (permalink)
VIP
 
RazorbladeXtreme's Avatar
 
Join Date: May 2008
Location: Jaipur
Posts: 187
Default Re: Sql program

Quote:
Originally Posted by vineet369 View Post
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
RazorbladeXtreme is offline   Reply With Quote
Old 20-11-2011, 10:56 PM   #6 (permalink)
Apprentice
 
Join Date: Mar 2008
Posts: 78
Default Re: Sql program

many many thanks to manaskumar...........
subhransu123 is offline   Reply With Quote
Old 21-11-2011, 12:22 PM   #7 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Sql program

Quote:
Originally Posted by vineet369 View Post
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 View Post
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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 21-11-2011, 04:19 PM   #8 (permalink)
I am the night! I am....
 
vickybat's Avatar
 
Join Date: Aug 2009
Location: Gotham City
Posts: 4,209
Default 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
vickybat is offline   Reply With Quote
Old 21-11-2011, 05:48 PM   #9 (permalink)
Fullbring
 
Zangetsu's Avatar
 
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
Default 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...
Zangetsu is offline   Reply With Quote
Old 23-11-2011, 08:22 PM   #10 (permalink)
VIP
 
RazorbladeXtreme's Avatar
 
Join Date: May 2008
Location: Jaipur
Posts: 187
Default 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.
RazorbladeXtreme is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2