Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql developer help

Author  Topic 

mikkel
Starting Member

1 Post

Posted - 2011-03-01 : 11:23:23
i have task in sql, but i am not sure that is correct...

can you help me with seeing about this correct

A very simple model for social networking like for instance Facebook could be modeled by these two relations:

person(name, age, gender)
relto(name1, name2, kind)

where we assume that a name uniquely identify persons and persons can be related to each other in different kinds of relationships, i.e. friends, married, colleagues, schoolmates, etc.

qeustion 1

Find the name and age of all John’s cousins. Order the list by age with the oldest first.

my answer

SELECT name, age
FROM person
WHERE kind = cousins;

I have som more qeustions you could help me with

Find each friend of John that is not a colleague.

Find the number of relations for each kind of relationship for John, Joe, and Nelly.

What is the average number, taken over all persons, of relations in each kind of relationship?

mikkel

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-03-01 : 11:44:10
come up with sample data of your table, identify the primary key and foreign key relationship/column of both and the desired output you are looking for in light of your sample info.

Cheers
MIK
Go to Top of Page

chriscairns
Starting Member

2 Posts

Posted - 2012-05-30 : 03:50:03
Facebook is a great way to stay in touch with those you care about, but it is also a great tool you can use to market your business.

unspammed
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-30 : 16:05:29
quote:
Originally posted by mikkel

i have task in sql, but i am not sure that is correct...

can you help me with seeing about this correct

A very simple model for social networking like for instance Facebook could be modeled by these two relations:

person(name, age, gender)
relto(name1, name2, kind)

where we assume that a name uniquely identify persons and persons can be related to each other in different kinds of relationships, i.e. friends, married, colleagues, schoolmates, etc.

qeustion 1

Find the name and age of all John’s cousins. Order the list by age with the oldest first.

my answer

SELECT name, age
FROM person
WHERE kind = cousins;

I have som more qeustions you could help me with

Find each friend of John that is not a colleague.

Find the number of relations for each kind of relationship for John, Joe, and Nelly.

What is the average number, taken over all persons, of relations in each kind of relationship?

mikkel


your query is not correct

it should be

SELECT p1.name, p1.age
FROM person p
inner join relto r
on r.name1 = p.name
inner join person p1
on p1.name = r.name2
WHERE r.kind = 'cousins'

union all

SELECT p1.name, p1.age
FROM person p
inner join relto r
on r.name2 = p.name
inner join person p1
on p1.name = r.name1
WHERE r.kind = 'cousins'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-30 : 16:05:58
try similarly for other queries

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -