Question: I have two tables T1 and T2. I want to select all rows from T2 table where t1\_id not equal to any id in T1 table. This is not working:

```
SELECT t2.id, t2.t1_id, t2.data FROM T2.t2, T1.t1 WHERE t2.t1_id != t1.id

```

Answer: 
You could use subquery with NOT IN clause as below:

```
SELECT t2.id, t2.t1_id, t2.data 
FROM T2 t2
WHERE t2.t1_id NOT IN (SELECT DISTINCT id
                       FROM T1)

```
```
SELECT t2.id, t2.t1_id, t2.data 
FROM T2.t2
LEFT JOIN T1.t1 ON t2.t1_id = t1.id
GROUP BY t2.id, t2.t1_id, t2.data
HAVING sum(t1.id is not null) = 0

```