Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Subquery with two selects from different table is giving random selection

Tags:

sql

sql-server

SQL Subquery with two selects from different table is giving random selection

select top(1) Questions.Ques_ID, Question 
from Questions 
where   Questions.Ques_ID in(
                   select   top(4)  Answers.Que_ID,Answer 
                   from Answers 
                   where  Questions.Ques_ID = Answers.Que_ID   
                   order by newid())

enter image description here

like image 459
Mustafe Sacad Avatar asked Oct 18 '22 07:10

Mustafe Sacad


1 Answers

When using IN() , same amount(and type) of elements should appear on both sides!

select top(1) Questions.Ques_ID, Question 
from Questions 
where   Questions.Ques_ID in(
                             select   top(4)  Answers.Que_ID 
                               from Answers 
                               where  Questions.Ques_ID = Answers.Que_ID   
                               order by newid())
like image 170
sagi Avatar answered Oct 21 '22 01:10

sagi