SQLite Forum

Confusion of some subquery results
Login
For query:

```SQL
CREATE TABLE v0 ( v2 CHAR(30), v1 CHAR(30) );
INSERT INTO v0 ( v1 ) VALUES ( 10 ),( 0 );
CREATE INDEX v19 ON v0 ( v2, v1 );
INSERT INTO v0 VALUES ( 10, 10 );
INSERT INTO v0 VALUES ( 0, 10 );
SELECT * FROM v0;
SELECT '----------------------------------------';
SELECT * FROM v0 WHERE ( ( v1 IN ( SELECT v2 FROM v0 ORDER BY v2 ) ));
SELECT '----------------------------------------';
SELECT * FROM v0 WHERE ( v2 = 0 );
SELECT '----------------------------------------';
SELECT * FROM v0 WHERE ( ( v1 IN ( SELECT v2 FROM v0 ORDER BY v2 ) ) AND v2 = 0);
```

We got the result:

```
|10
|0
10|10
0|10
------------
|10
|0
10|10
0|10
------------
0|10
------------
(EMPTY RESULT)
```


Since the second and the third SELECT statements return some results, we expect some results being outputted from the last SELECT statement too. However, there is no outputs from the last SELECT statement. 

A few more interesting behaviors that we observe is that:

    -If we delete the CREATE INDEX statement, the inconsistency is gone and the last SELECT successfully output 0|10 just like the third SELECT statement.

    -If we change the data type of v1, v2 to be INT, the inconsistency is also gone and the last SELECT successfully output 0|10 just like the third SELECT statement.

We are not sure whether this is a bug or this is just an affinity behavior that is known and expected. We are really appreciated for any explanation provided. Thank you.