Question

I'm trying to select duplicates from this table:

snr zip
01 83
02 82
03 43
04 28

Expected result is just empty table. Cuz it got no duplicates.

I've tried with this query:

SELECT snr, zip
FROM student
GRUOP BY snr
HAVING  (COUNT(zip) > 1)

But it says that syntax is error, and that I'm trying to query an aggregate functions, etc..

Was it helpful?

Solution

It looks like you need to either remove zip from the SELECT columns, or else wrap it in an aggregate function, such as COUNT(zip):

SELECT   snr, COUNT(zip)
FROM     student
GROUP BY snr
HAVING   (COUNT(zip) > 1)

Also check out @OMG Ponies's answer for further suggestions.

OTHER TIPS

Use:

  SELECT snr, zip
    FROM student
GROUP BY snr, zip
  HAVING COUNT(DISTINCT zip) > 1

Standard SQL requires that columns in the SELECT clause that are not wrapped in aggregate functions (COUNT, MIN, MAX, etc) need to be defined in the GROUP BY. However, MySQL and SQLite allow for columns to be omitted.

Additionally, use COUNT(DISTINCT or you'll risk false positives.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top