Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been asked a quetion in Interivew

FIleID FileName IsValid
1 A 0
2 B 0
3 C 1
4 D 0

So question is that in above table column isValid can contain any numeric value ranging from 0 to infinity.Means it can have 100,200 likewise

so if that column don't have same value throughout then query should return false else true

without using join or where condition

What I have tried:

I am trying to create a function but this also not required
Posted
Updated 5-Sep-17 21:23pm
Comments
CPallini 2-Sep-17 9:04am    
Could you please elaborate?
vivvicks 2-Sep-17 9:13am    
means if value in isValid columns are same then it shd return true else false
it shd like
0
0
0
0
is true
0
1
0
0
is false
but we dont know the what value will be there

Try this:
IF (SELECT COUNT(*) FROM tablename) = (SELECT COUNT(DISTINCT column_of_interest) FROM tablename)
   SELECT 'FALSE'
ELSE
   SELECT 'TRUE'
 
Share this answer
 
Comments
CPallini 2-Sep-17 9:15am    
5.Possibly GROUP BY could be an alternative.
vivvicks 2-Sep-17 9:22am    
hum..but it shd have only select.and above is not working as expected
Peter Leow 2-Sep-17 9:50am    
It's the "thought" that counts.
vivvicks 2-Sep-17 10:33am    
yes Sir..Thanks for reply
Peter Leow 2-Sep-17 9:49am    
Thank you, CPallini.
Not sure if I understand the question correctly, but try the following:
SQL
select
   case (select count(*) 
         from (select distinct isValid 
		       from TableName) a)
      when 1 then 'True'
      else 'False'
   end
 
Share this answer
 
We do not do your homework: and interviews are homework! They are there so the company can get an idea of your skills and assess if you fit into their team, and can do the job - not if I do and can. If we do it for you, that isn't fair on anybody: the company because interviewing is a very expensive business, the other applicants who could do the task and presumably the job, and ultimately you - because once the company finds out you lied on the interview you won't last long in the job either.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
vivvicks 2-Sep-17 8:56am    
Interview is done.Some forums reply with answer that is why i put it here i guess i have paste in wrong forum.
Dave Kreskowiak 2-Sep-17 11:35am    
Sure, you're going to get queries that will show you the answer to the question, but you're not going to know WHY it works and that's the entire point of the question in the interview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900