SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Thursday, April 13, 2006

Literal value with "IN" clause

SQL Apprentice Question
Is it okay to use a literal value with the IN clause. E.g.


SELECT somefield, anotherfield
....
WHERE ...etc.
AND 1234 IN (SELECT userid FROM tblUsers)


I was told it wasn't valid, but I'm pretty sure it worked for me. Just
seeking clarification.


Celko Answers
It is valid, Standard SQL and can be a useful trick to avoid OR-ed
predicates. The IN() list just has to be expressions that will cast
to the proper data type.

No comments: