V
V
vasenka2020-09-07 09:51:04
Oracle
vasenka, 2020-09-07 09:51:04

How to write a query to select from the database by the same values ​​of two columns?

Good day! There is a task - to select rows from the table that have the same values. Sample table below ↓

|id|number|country|
|123|789-456-46|Mexico|Helen
|654|65-741-697|England|Victor
|789|54-485-664|England|Michael
|334|55-895-231|Mexico|Helen


You need to select rows that repeat values ​​from number and country, such as Mexico and Helen. Tell me in which direction to think? What operators to use?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman, 2020-09-07
@vasenka

SELECT
 t.id
FROM <tablename> t
WHERE EXISTS(SELECT 1 FROM <tablename> t2 WHERE t2.number = t.number  AND t2.country = t.country)

P
PavelMos, 2020-09-07
@PavelMos

groupby having count
there must be no space after count
add column "c" with count optional
see also sql aggregate functions multiple clause / sql aggregating multiple clauses
upd without group by (id) the query will not run normally, but I don't know why.

SELECT *, COUNT(*) as C 
FROM table1 
GROUP BY (id)
HAVING COUNT(country)>1 AND COUNT(name)>1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question