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 problem

Author  Topic 

darius12
Starting Member

16 Posts

Posted - 2012-01-05 : 16:42:33
If i have following code:

CREATE TABLE person (
id NUMBER(2) PRIMARY KEY,
first_name VARCHAR2(10) NOT NULL,
last_name VARCHAR2(13) NOT NULL,
father NUMBER(3) REFERENCES person
mother NUMBER(3) REFERENCES person,
born DATE NOT NULL,
died DATE,
height NUMBER(3)
);

how do i give first name of the father for all people?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-05 : 17:27:47
You could do a join to the same table like this:
SELECT
p1.*,
p2.first_name AS FathersName
FROM
person p1
LEFT JOIN person p2 ON p1.father = p2.id;
Go to Top of Page

darius12
Starting Member

16 Posts

Posted - 2012-01-05 : 18:04:18
SELECT s.first_name AS "Child",
r.first_name AS "Father"

FROM person AS s LEFT OUTER JOIN person AS r
ON s.father =r.id ORDER BY r.first_name

This works, tested
Go to Top of Page
   

- Advertisement -