Yatra.com Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Above Query is wrong.It would also return students studying more than 2 subjects.We need to return students studying either DS or Networking and not any other subjects, else the query is pretty easy.
So we can do concatenation of all the subjects and check that it is either DSNetworking or NetworkingDS. This would do the job.
select studentName from student where (subject1='DS' OR subject1='Networking') OR (subject2='DS' OR subject2='Networking') OR (subject3='DS' OR subject3='Networking') OR (subject4='DS' OR subject4='Networking') OR (subject5='DS' OR subject5='Networking')
any other better approach than this. I also replied with this solution but the interviewer was keen to know any better approach, as the number of subjects may increase it is not much efficient.
Well, even with possible 5 different subjects the subjects should go into another table. And then join the student table with the subjects table for the query.
select distinct studentName from students, subjects where students.id = subjects.id and (subjects.title = 'DS' or subject.title = 'Networking')
select studentName from student where 'DS' IN(subject1,subject2,subject3,subject4,subject5)
- Sumanth January 25, 2012OR 'Networking' IN (subject1,subject2,subject3,subject4,subject5)