[NB. This is a derivative of a question posed on MS Sql Svr forum.]
I have a classic self-referring table
USER(USER_ID,PARENT)
I want to list all users in Bob's "family" i.e. Bob, children, parent, siblings.
Is the following query OK? Is it possible to avoid repeating the sub-select?
SELECT USER_ID FROM USER
WHERE
USER_ID = 'Bob' OR
PARENT = 'Bob' OR
USER_ID = (SELECT PARENT FROM USER WHERE USER_ID = 'Bob') OR
PARENT = (SELECT PARENT FROM USER WHERE USER_ID = 'Bob')
I have a classic self-referring table
USER(USER_ID,PARENT)
I want to list all users in Bob's "family" i.e. Bob, children, parent, siblings.
Is the following query OK? Is it possible to avoid repeating the sub-select?
SELECT USER_ID FROM USER
WHERE
USER_ID = 'Bob' OR
PARENT = 'Bob' OR
USER_ID = (SELECT PARENT FROM USER WHERE USER_ID = 'Bob') OR
PARENT = (SELECT PARENT FROM USER WHERE USER_ID = 'Bob')