0
select * from tablename 
     where id in(select id from tablename2 where condition UNION select -1)

Is it ok to use select -1 as if the inner query does not result anything it will give error. It is feasible or not?

3
  • 3
    If the inner query doesn't return anything, there just won't be a row returned in the outer query. What error are you getting? Commented Aug 9, 2011 at 11:39
  • I don't think it will result in error if inner query does not return any value. Commented Aug 9, 2011 at 11:40
  • got it.. if we kept in() blank then only it gives error. if inner query returns empty query will returns empty result.Thanks anyways Commented Aug 9, 2011 at 12:12

3 Answers 3

1

imho, inner-select is far from ideal (slow)

based on your posted SQL, an inner join will do the trick

select * 
from tablename as t1
inner join tablename2 as t2
on t1.id=t2.id
where condition; --- your condition
Sign up to request clarification or add additional context in comments.

Comments

1

If you have to get it done with a subquery then the correct way to do it would probably be:

SELECT *
FROM tablename AS t1
WHERE EXISTS
    (SELECT id 
     FROM tablename2 AS t2
     WHERE conditions)

Comments

0

It won't give an error if the query returns nothing. It just returns an empty resultset.

1 Comment

got it.. if we kept in() blank then only it gives error. if inner query returns empty query will returns empty result.Thanks anyways

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.