[VIEWED 6945
TIMES]
|
SAVE! for ease of future access.
|
|
|
Biruwa
Please log in to subscribe to Biruwa's postings.
Posted on 04-20-08 4:59
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
I was recently asked the following SQL question There are 3 tables. cat dog animal id, name id, name id, color
what's the sql for listing the name of the animals with color = brown?
Can u help me?
|
|
|
|
M$Hacks
Please log in to subscribe to M$Hacks's postings.
Posted on 04-20-08 5:15
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
One way would be"
select name from (cat Union dog) join (animal) on id where color='brown';
|
|
|
leader
Please log in to subscribe to leader's postings.
Posted on 04-20-08 5:36
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
select a.name from cat a, animal b where b.color='brown' and a.id=b.id;
this will definitely do.................
|
|
|
leader
Please log in to subscribe to leader's postings.
Posted on 04-20-08 5:36
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
select a.name from cat a, animal b where b.color='brown' and a.id=b.id;
this will definitely do.................
|
|
|
techGuy
Please log in to subscribe to techGuy's postings.
Posted on 04-20-08 7:23
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
well,
Leader might be wrong , since it will only list out name of cats.
Another way (just to simplify m$hacks query)
select name from (
-- will give you all the cat names with color=brown
(select name from cat c, animal a where a.color='brown' and a.id=c.id)
union
-- will give you all the dog names with color=brown
( select name from dog d, animal a where a.color='brown' and a.id=d.id)
)
|
|
|
Biruwa
Please log in to subscribe to Biruwa's postings.
Posted on 04-20-08 8:12
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
thanx,
I think techGuy's soln will work. But is there a way to make it cleaner, concise?
m$hacks soln was definitely a starter, but I don't know whether you can do
select field from (table1 union table2)
leader's won't work precisely because it only returns for 1 type - 'cat' where as the Q is asking for both cats and dogs.
|
|
|
jeffali
Please log in to subscribe to jeffali's postings.
Posted on 04-20-08 8:33
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
experts, what assumption are you making about the foreign keys ? How can a single field id on animal table have foreign key to id's of two different tables ?
|
|
|
techGuy
Please log in to subscribe to techGuy's postings.
Posted on 04-20-08 10:22
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
why is it not possible? cat
animal |------------------- id
id --------------------------| name
color | dog
|------------------ id
name
|
|
|
Biruwa
Please log in to subscribe to Biruwa's postings.
Posted on 04-21-08 2:35
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|
|
|
hurray
Please log in to subscribe to hurray's postings.
Posted on 04-21-08 2:48
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Select name from Cat
UNION ALL //(or just UNION)
Select name from Dog
where id = (Select id from Animal where color = "brown")
|
|
|
yak_yak_yak
Please log in to subscribe to yak_yak_yak's postings.
Posted on 04-21-08 3:19
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
here is answer,
select * from cat a
inner join dog b
on a.id =b.id
inner join animal c
on b.id =c.id
where c.color = 'Brown'
|
|
|
yak_yak_yak
Please log in to subscribe to yak_yak_yak's postings.
Posted on 04-21-08 3:23
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
select a.name, b.name from cat a
inner join dog b
on a.id =b.id
inner join animal c
on b.id =c.id
where c.color = 'Brown'
Both from table A and B.
or USE * which will give you all.
|
|
|
Biruwa
Please log in to subscribe to Biruwa's postings.
Posted on 04-21-08 4:56
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
yak yak yak,
we just need a single column with the names of cats and dogs with color brown.
Your 2<sup>nd</sup> sql if it works will produce 2 columns with names of cats and separate column with names of dogs.
|
|
|
krishna
Please log in to subscribe to krishna's postings.
Posted on 04-21-08 5:09
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
select a.name from cat a, animal b where a.id = b.id and b.color='Brown'
union all
select a.name from dog a, animal b where a.id = b.id and b.color='Brown'
btw, I don't like the schema, why the need for dog and cat table seperately?
|
|
|
Biruwa
Please log in to subscribe to Biruwa's postings.
Posted on 04-22-08 5:39
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
that's for normalization.
|
|
|
sujanks
Please log in to subscribe to sujanks's postings.
Posted on 04-22-08 8:00
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
i ran this and works assuming that id in cats and/or dogs are foreign keys from animals. let me know other wise select name from cats where id in (select id from animals where color = 'brown') union
select name from dogs where id in (select id from animals where color = 'brown')
|
|