Biruwa
Replies to this thread:

More by Biruwa
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 sql question
[VIEWED 7002 TIMES]
SAVE! for ease of future access.
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?

 
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';


 
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.................


 
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.................


 
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)
                 )
                         
  
 

 
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.


 
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 ?


 
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


 
Posted on 04-21-08 2:35 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

jeffali,

We can definitely have foreign key reference as in the following web page (http://www.sqlteam.com/article/implementing-table-inheritance-in-sql-serveryou) you can see that Students, Teachers, and Parents are all "People" and Students, Teachers id have reference to the people's id.

techGuy's diagram looks skewed. U mean to say that u'r sql works as is?


 
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")

 

 

 

 

 

 


 
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'


 
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.

 


 
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.


 
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?

 


 
Posted on 04-22-08 5:39 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

that's for normalization.
 
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')



 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
ChatSansar.com Naya Nepal Chat
Toilet paper or water?
TPS EAD auto extended to June 2025 or just TPS?
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
I hope all the fake Nepali refugee get deported
From Trump “I will revoke TPS, and deport them back to their country.”
Tourist Visa - Seeking Suggestions and Guidance
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
advanced parole
TPS Renewal Reregistration
Sajha Poll: Who is your favorite Nepali actress?
Biden said he will issue new Employment visa for someone with college degree and job offers
Why Americans reverse park?
Nepali Passport Renew
Driver license help ASAP sathiharu
They are openly permitting undocumented immigrants to participate in federal elections in Arizona now.
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters