'SQL Select Rows that have same column value
I Have following table, I Would like to Select rows that have same value in Zone field for the same Line number.
| Id | Line | Name | Zone |
|---|---|---|---|
| 1 | 10 | A | 2 |
| 2 | 10 | B | 2 |
| 3 | 15 | C | 3 |
| 4 | 15 | D | 4 |
| 5 | 17 | E | 5 |
| 6 | 17 | F | 5 |
| 7 | 17 | G | 6 |
| 8 | 20 | H | 7 |
| 9 | 20 | I | 7 |
Result should be:
| Id | Line | Name | Zone |
|---|---|---|---|
| 1 | 10 | A | 2 |
| 2 | 10 | B | 2 |
| 8 | 20 | H | 7 |
| 9 | 20 | I | 7 |
What's the correct way to query this?
Solution 1:[1]
One way is using EXISTS
Select Id, Line, Name, Zone
from tbl t
where not exists (
select 1
from tbl t2
where t2.Line = t1.Line and t2.Zone <> t1.Zone)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Serg |
