Questions tagged [sql-in]

`SQL IN` operator allows us to specify multiple values in a WHERE clause.

SQL IN operator allows us to specify multiple values in a WHERE clause.

simple syntax:

SELECT * 
FROM PERSON
WHERE ID IN (1, 5, 10)

Reference

222 questions
503
votes
22 answers

Difference between EXISTS and IN in SQL?

What is the difference between the EXISTS and IN clause in SQL? When should we use EXISTS, and when should we use IN?
Krantz
  • 6,143
  • 3
  • 25
  • 18
271
votes
3 answers

IN vs ANY operator in PostgreSQL

What is the difference between IN and ANY operator in PostgreSQL? The working mechanism of both seems to be the same. Can anyone explain this with an example?
mohangraj
  • 9,842
  • 19
  • 59
  • 94
239
votes
17 answers

ORDER BY the IN value list

I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of comments. I provide a sorted list of values to the IN construct in the WHERE clause: SELECT * FROM comments WHERE (comments.id IN (1,3,2,4)); This returns comments in an arbitrary…
nutcracker
  • 2,944
  • 2
  • 17
  • 16
138
votes
10 answers

SQL Server IN vs. EXISTS Performance

I'm curious which of the following below would be more efficient? I've always been a bit cautious about using IN because I believe SQL Server turns the result set into a big IF statement. For a large result set, this could result in poor…
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
88
votes
3 answers

Room - Select query with IN condition?

Is it possible to use SQLite's IN condition with Room? I'm trying to select a list of items from my database where the value of a certain column (in this case a TEXT column) matches any one of a set of filter values. That's pretty easily done in SQL…
VerumCH
  • 2,995
  • 2
  • 15
  • 22
78
votes
29 answers

Passing a varchar full of comma delimited values to a SQL Server IN function

Duplicate of Dynamic SQL Comma Delimited Value Query Parameterized Queries with Like and In I have a SQL Server Stored Procedure where I would like to pass a varchar full of comma delimited values to an IN function. For example: DECLARE @Ids…
BeYourOwnGod
  • 2,345
  • 7
  • 30
  • 35
76
votes
8 answers

PDO binding values for MySQL IN statement

I have an issue with PDO that I'd really like to get an answer for after being plagued by it for quite some time. Take this example: I am binding an array of ID's to a PDO statement for use in a MySQL IN statement. The array would be say: $values =…
hd82
  • 763
  • 1
  • 6
  • 4
42
votes
3 answers

PostgreSQL - IN vs ANY

I have tried both: smthng = ANY (select id from exmplTable) smthng IN (select id from exmplTable) and I am getting the same results for my data. Is there any difference for the two expressions?
PROvlima
  • 541
  • 1
  • 4
  • 9
14
votes
2 answers

Using IN clause in a native sql query

We are trying to dynamically generate an IN clause for a native sql query to return a JPA entity. Hibernate is our JPA provider. Our code looks something like this. @NamedQuery( name="fooQuery", queryString="select f from Foo f where…
VerrDon Mason
14
votes
1 answer

Using IN clause in a native sql query with Hibernate 3.2.2

In a fashion similar to the question found here: Using IN clause in a native sql query; I am attempting to make use of an IN() clause by way of a native SQL query in Hibernate. While the author in the other question was able to use JPA, I am not. In…
carlsz
  • 2,224
  • 3
  • 18
  • 16
13
votes
3 answers

How do you use IN clauses with mysqli prepared statements

I’m moving some old code over to the new msqli interface using prepared statements, I’m having trouble with SQL statements containing the IN clause. I would just normally do this: $ids = '123,535,345,567,878' $sql = "SELECT * FROM table WHERE id IN…
user93836
12
votes
4 answers

SQL IN condtion

select transferTypes from TransferData transferTypes -------------- TTH, TT TRANSIT, TTH ST, TRANSIT TRANSIT, TTH ST, TT Is there is any way or inbuilt function to achieve below results? Tried with below IN condition but unable to get required…
Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55
12
votes
2 answers

Node.js sqlite3 IN operator

So I am currently trying to make a query in Node.js: // friends is an array object db.all('SELECT email ' + 'FROM users' + 'WHERE email in ?', friends, function(err, rows) { if (!err) { I know that you can pass in an array…
user1927638
  • 1,133
  • 20
  • 42
8
votes
5 answers

SQL IN clause which is guaranteed to evaluate to false

Is there any value which I can place in a SQL IN clause which will guarantee that the clause will evaluate to false? SELECT * FROM Products WHERE ProductID IN (???) Is there anything I could replace ??? with to guarantee no rows will be returned?
Ian Newson
  • 7,679
  • 2
  • 47
  • 80
7
votes
3 answers

Using 'IN' with a sub-query in SQL Statements

Are there any performance issues of using "IN" keyword in SQL statements in places where we can use JOIN? SELECT xxx FROM xxx WHERE ID IN (SELECT Id FROM xxx)
user776248
1
2 3
14 15