I am new to Oracle's SQL and need help. How can I select only those rows that have the same 'code' for a different 'name'?
For example I have Table 1 and I want to get Table 2.
Table 1: customer
| id | name | num_id |
|---|---|---|
| 1 | Jenny | 456 |
| 2 | Jenny | 456 |
| 3 | Ben | 456 |
| 4 | Ursula | 879 |
| 5 | Mike | 145 |
| 6 | Mike | 145 |
| 7 | Nick | 954 |
Table 2: customer
| id | name | num_id |
|---|---|---|
| 1 | Jenny | 456 |
| 3 | Ben | 456 |
This is my query:
select distinct t1.name, t1.num_id
from customer t1
join (select distinct name, num_id, count(*)
from customer
group by name, num_id
having count(*) > 1) t2
on t1.num_id = t2.num_id
and t1.name <> t2.name
order by 2;
But it does not work. Can you help me?