SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack (5) Thread Tools Search this Thread
  5 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-15-2006, 06:24 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 92
ben has disabled reputation
Default ANY dialect question:

Find duplicate entries in a table?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 04-17-2006, 01:38 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 92
ben has disabled reputation
Default SQL 92 answer. Re: Find duplicate entries in a table?

Let's assume we want to find duplicate emails in a 'users' table:

SELECT id, email, count(email) FROM users GROUP BY email HAVING count(email) > 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2006, 01:41 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 92
ben has disabled reputation
Default MySQL answer. Re: Find duplicate entries in a table?

Another approach is to list all the duplicate entries. We assume the field 'id' is the primary key of the table 'users'

SELECT DISTINCT t1.id, t1.email FROM users t1, users t2 WHERE t1.email = t2.email AND t1.id <> t2.id
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-07-2006, 11:06 PM
Rob137
 
Posts: n/a
Default MS SQL answer. Re: Find duplicate entries in a table?

SELECT *
FROM Something AS zd
WHERE (SELECT count(*) FROM Something AS t1
     WHERE = zd.liefschnum AND t1.pos = zd.pos) > 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-18-2007, 01:26 AM
Rock
 
Posts: n/a
Default MS SQL answer. Re: Find duplicate entries in a table?

SELECT empno,count(*) AS counter FROM emp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-01-2008, 07:30 AM
Unregistered
 
Posts: n/a
Lightbulb ANY answer. Re: Find duplicate entries in a table?

SELECT t1.field_name FROM tablename t1, tablename t2 WHERE t1.field_name=t2.field_name AND t1.rowid <> t2.rowid;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-16-2008, 12:11 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: Find duplicate entries in a table?

SELECT e.column_name1
FROM table_name e, table_name f
WHERE e.column_name1 =f.column_name1 AND e.column_name2=f.column_name2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-16-2008, 08:45 AM
dandv dandv is offline
Junior Member
 
Join Date: Nov 2006
Posts: 2
dandv is on a distinguished road
Angry ANY answer. Re: Find duplicate entries in a table?

Quote:
Originally Posted by ben View Post
Let's assume we want to find duplicate emails in a 'users' table:

SELECT id, email, count(email) FROM users GROUP BY email HAVING count(email) > 1
That doesn't work in PostgreSQL:
ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-16-2008, 05:43 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: Find duplicate entries in a table?

Quote:
Originally Posted by dandv View Post
That doesn't work in PostgreSQL:
ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
Yes, the statement is not valid according to SQL standards. It a documented behaviour of MySQL.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.sqlrecipes.com/sql_questions_answers/find_duplicate_entries_table-5/
Posted By For Type Date
PHP script resource - [MSSQL] leidige theme Dubletten This thread Refback 03-06-2008 01:29 PM
Discover From Your Favorite Topic or Web Page: www.w3schools.com/sql/sql_join.asp This thread Refback 02-29-2008 05:31 AM
PHP script resource [MSSQL] leidige theme Dubletten This thread Refback 08-19-2007 09:51 PM
PHP script resource - [MSSQL] leidige theme Dubletten This thread Refback 07-11-2007 10:45 AM
PHP script resource - [MSSQL] leidige theme Dubletten This thread Refback 05-31-2007 02:25 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Duplicate Entries in a table? Simon SQL - Questions and Answers 6 06-09-2008 11:15 PM


All times are GMT. The time now is 02:20 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8