Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7715395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:22:26+00:00 2026-06-01T02:22:26+00:00

i am trying to create sql procedure or function that need to find duplicate

  • 0

i am trying to create sql procedure or function that need to find duplicate users in my users table (duplicate in case the users have the same email). I want to save the users in a new table like this:

id | user_id | duplicate_users

The duplicate_users will contain array of user id that have the same email like user_id
This is my main query but it’s really bad because i get to many results.

SELECT  a.id user_id,
        a.email,
        b.id,
        dup_user_id
FROM    users a, 
        users b
WHERE   a.email = b.email
  AND   a.id != b.id

Thanks in advanced.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:22:27+00:00Added an answer on June 1, 2026 at 2:22 am

    What do you mean by “array of users?” Do you mean storing a collection data type? A CSV list? The term “Array” does not really fit within the Oracle sphere.

    Also, by nature of what you are asking you want the key to be the email, not one of the IDs otherwise you get each combination.

    For example users a and b each have email “bob@inter.net”. You query would have

    a, bob@inter.net, b
    b, bob@inter.net, a

    And I think what you want is

    bob@inter.net, (a,b)

    Now, to make the field a CSV list of IDs you could use :

    SELECT  email,         
            listagg(id,',') WITHIN GROUP (ORDER BY ID) as list_of_ids        
    FROM    users
    GROUP BY email
    

    If you want to store the ids in an oracle collection, I could steer you in that direction too.

    EDIT: based on your comment.

    OK, if you want the full results, then ammend to

    SELECT  a.id as id
            a.email as email,         
            listagg(b.id,',') WITHIN GROUP (ORDER BY ID) as list_of_ids        
    FROM    users a, users b
    where   a.email = b.email
    and     a.id != b.id
    GROUP BY a.id, a.email
    

    So if users a, b, and c all share email bob@inter.net you will get:

    a, bob@inter.net, “b,c”

    b, bob@inter.net, “a,c”

    c, bob@inter.net, “a,b”

    If you want to drop the email from the query, then:

    SELECT  a.id as id
            listagg(b.id,',') WITHIN GROUP (ORDER BY ID) as list_of_ids        
    FROM    users a, users b
    where   a.email = b.email
    and     a.id != b.id
    GROUP BY a.id
    

    Incidentally, if you are on an older version of Oracle that does not support the listagg string aggregation function, then you will be able to find an alternate solution here: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php

    I would suggest using the wm_concat() equivalent for Oracle 11.1 or 10, or one of the others for Oracle 9.

    So, for Oracle 11.1 or 10, use:

    SELECT  a.id as id
            a.email as email,         
            wm_concat(b.id) as list_of_ids        
    FROM    users a, users b
    where   a.email = b.email
    and     a.id != b.id
    GROUP BY a.id, a.email
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a view or a table valued function that returns one
I have an existing stored procedure in SQL Server that I need to call
I'm trying to create an SQL query in PHP to update a table. Is
I'm trying to create an sql table called 'cartable'. I'm currently using phpmyadmin via
I'm trying to create an SQL statement that joins two tables and returns a
I'm trying to create a table type in sql server 2005. Here is what
Is it possible to create a stored procedure that can call a function/generate C#
I am trying to create a stored procedure that when given a month value,
I am trying to create a simple stored procedure in SQL Server 2005, and
Mysql Version 14.14 Distrib 5.1.41 I have a stored procedure that I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.