Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
SELECT
t.id
FROM <tablename> t
WHERE EXISTS(SELECT 1 FROM <tablename> t2 WHERE t2.number = t.number AND t2.country = t.country)
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 questionAsk a Question
731 491 924 answers to any question