Question: I have a mysql table with 2 columns.

```
+---------+-----------+
| Barcode | StationID |
+---------+-----------+
|   89411 |         1 |
|   89411 |         2 |
|   89411 |         3 |
|   89412 |         1 |
|   89413 |         1 |
+---------+-----------+

```

I would like to select all valus from Barcode column which have StationID = 1 and do NOT have a StationID different than 1.

As shown in the picture Barcode 89411 appears three times with different StationID and should be excluded from the result.

Can you help me make a query?

Answer: 
Another approach is to use an `EXISTS` query:

```
SELECT t1.*
FROM yourTable t1
WHERE
    t1.StationID = 1 AND
    NOT EXISTS (SELECT 1 FROM yourTable t2
                WHERE t1.Barcode = t2.Barcode AND t2.StationID <> 1);

```

[Demo
----](https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4e544ed735f65c3f616345820069010a)
Try this: <https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=641d334c5f9e57bbdde07e4f24365f88>

```
select barcode from tablename
group by barcode
having sum(case when sanctionid=1 then 0 else 1 end)=0

```

output:

```
barcode
89412
89413

```