Check Repeated Records in SQL statements

Source: Internet
Author: User
-- Create a test environment
Create Table ttt
Name varchar (20 ),
ADDR varchar (20)
Insert ttt
Select 'zhang san', 'beijing' Union all
Select 'Li si', 'shanghai China' Union all
Select 'wang 5', 'tianjin, China' Union all
Select 'zhang san', 'sichuan province, China'

Method 1:
Select * From TTT t
Where exists (select 1 from TTT where name = T. Name and ADDR <> T. ADDR)
Method 2:
Declare @ TB table (ID int identity, name varchar (20), ADDR varchar (20 ))
Insert @ Tb (name, ADDR) Select * From ttt
Select name, ADDR from @ TB t
Where exists (select 1 from @ TB where name = T. Name and ID <> T. ID)
Method 3:
Select * From TTT t
(Select count (1) From TTT where name = T. Name)> 1
Order by name
Method 4:
Select * From TTT t
Where exists (select top B. name from (select Top 2 from TTT T1
Where name = order by B
Where B. Name = T. Name)
Order by name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.