Finding duplicate values in a SQL table
SELECT
name, email, COUNT(*)
FROM
users
GROUP BY
name, email
HAVING
COUNT(*) > 1
Simply group on both of the columns.
Note: the older ANSI standard is to have all non-aggregated columns in the GROUP BY but this has changed with the idea of "functional dependency":
In relational database theory, a functional dependency is a constraint between two sets of attributes in a relation from a database. In other words, functional dependency is a constraint that describes the relationship between attributes in a relation.
Support is not consistent:
- Recent PostgreSQL supports it.
- SQL Server (as at SQL Server 2017) still requires all non-aggregated columns in the GROUP BY.
- MySQL is unpredictable and you need
sql_mode=only_full_group_by
:- GROUP BY lname ORDER BY showing wrong results;
- Which is the least expensive aggregate function in the absence of ANY() (see comments in accepted answer).
- Oracle isn't mainstream enough (warning: humour, I don't know about Oracle).
SQL - How to get duplicate records from one table with join on another table
Without much information as to why 1000 - Value1000
should be the result instead of 1000 - Value9999
, we can just get the first record from the top:
select top 1 * from (
-- your original query
SELECT a.AccessNumber, b.Value from Table1 a
JOIN Table2 b on b.AccessNumber = a.AccessNumber
Group by a.AccessNumber, b.Value
HAVING COUNT(1) > 1
-- your original query
) as x;
If we are just looking for the AccessNumber
that has duplicate records, you can remove the b.Value
from the selected columns and remove that as well in the Group By
clause.
SELECT a.AccessNumber from Table1 a
JOIN Table2 b on b.AccessNumber = a.AccessNumber
Group by a.AccessNumber
HAVING COUNT(1) > 1;
http://www.sqlfiddle.com/#!18/3f114/3
Related Topics
How to Find the Size of a Table in SQL
How Different Is Postgresql to MySQL
How to Output Oracle SQL Result into a File in Windows
How to Return Second Newest Record in SQL
Boolean 'Not' in T-SQL Not Working on 'Bit' Datatype
Do Indexes Work with "In" Clause
Django: Using Custom Raw SQL Inserts with Executemany and MySQL
Use Row Values as Columns in Postgresql
How to Bulk Insert a File into a *Temporary* Table Where the Filename Is a Variable
Inline Blob/Binary Data Types in SQL/Jdbc
Get Month and Year from a Datetime in SQL Server 2005
When to Use an Auto-Incremented Primary Key and When Not To
What's the Escape Sequence for Hyphen (-) in Postgresql
How to Add Time to Datetime in SQL
Finding Free Slots in a Booking System
Entity Framework: How to Properly Handle Exceptions That Occur Due to SQL Constraints