HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to check for mutiple rows with same data?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rowssamewithforhowcheckdatamutiple

Problem

Can anyone advise how do I check multiple rows with same data?

E.g.

I have 2 rows with following values:

Cola         Colb  Colc
TYPE_APPLE   123   abc
Colb         Colb  Colc
TYPE_APPLE   123   abc


I want to write an SQL query which will not select data of multiple rows which contain the same data as above. How should I write the query?

Solution

DISTINCT will combine identical rows. GROUP BY will combine them and allow you to know which ones had multiples using COUNT(*). To meet your "not select data of multiple rows" requirement you will need to add a HAVING clause. Your query will look something like this:

SELECT cola, colb, colc FROM T1 GROUP BY cola, colb, colc HAVING count(*) = 1;


SQL Fiddle Demonstration

Code Snippets

SELECT cola, colb, colc FROM T1 GROUP BY cola, colb, colc HAVING count(*) = 1;

Context

StackExchange Database Administrators Q#35525, answer score: 4

Revisions (0)

No revisions yet.