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

  • Home
  • SEARCH
  • 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 7874471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:48:44+00:00 2026-06-03T02:48:44+00:00

I need to find duplicates in a table. In MySQL I simply write: SELECT

  • 0

I need to find duplicates in a table. In MySQL I simply write:

SELECT *,count(id) count FROM `MY_TABLE`
GROUP BY SOME_COLUMN ORDER BY count DESC

This query nicely:

  • Finds duplicates based on SOME_COLUMN, giving its repetition count.
  • Sorts in desc order of repetition, which is useful to quickly scan major dups.
  • Chooses a random value for all remaining columns, giving me an idea of values in those columns.

Similar query in Postgres greets me with an error:

column “MY_TABLE.SOME_COLUMN” must appear in the GROUP BY clause or be
used in an aggregate function

What is the Postgres equivalent of this query?

PS: I know that MySQL behaviour deviates from SQL standards.

  • 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-03T02:48:45+00:00Added an answer on June 3, 2026 at 2:48 am

    Back-ticks are a non-standard MySQL thing. Use the canonical double quotes to quote identifiers (possible in MySQL, too). That is, if your table in fact is named "MY_TABLE" (all upper case). If you (more wisely) named it my_table (all lower case), then you can remove the double quotes or use lower case.

    Also, I use ct instead of count as alias, because it is bad practice to use function names as identifiers.

    Simple case

    This would work with PostgreSQL 9.1:

    SELECT *, count(id) ct
    FROM   my_table
    GROUP  BY primary_key_column(s)
    ORDER  BY ct DESC;
    

    It requires primary key column(s) in the GROUP BY clause. The results are identical to a MySQL query, but ct would always be 1 (or 0 if id IS NULL) – useless to find duplicates.

    Group by other than primary key columns

    If you want to group by other column(s), things get more complicated. This query mimics the behavior of your MySQL query – and you can use *.

    SELECT DISTINCT ON (1, some_column)
           count(*) OVER (PARTITION BY some_column) AS ct
          ,*
    FROM   my_table
    ORDER  BY 1 DESC, some_column, id, col1;
    

    This works because DISTINCT ON (PostgreSQL specific), like DISTINCT (SQL-Standard), are applied after the window function count(*) OVER (...). Window functions (with the OVER clause) require PostgreSQL 8.4 or later and are not available in MySQL.

    Works with any table, regardless of primary or unique constraints.

    The 1 in DISTINCT ON and ORDER BY is just shorthand to refer to the ordinal number of the item in the SELECT list.

    SQL Fiddle to demonstrate both side by side.

    More details in this closely related answer:

    • Select first row in each GROUP BY group?

    count(*) vs. count(id)

    If you are looking for duplicates, you are better off with count(*) than with count(id). There is a subtle difference if id can be NULL, because NULL values are not counted – while count(*) counts all rows. If id is defined NOT NULL, results are the same, but count(*) is generally more appropriate (and slightly faster, too).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find and delete duplicates from within a table, yet keep a
I need to write a query that will go through a table, find duplicate
I need to find a way to spin off a thread from a static
I need to get the nid alone in this query. SELECT count(nid) as total,nid
I need to find duplicate entries in my db table. There has to be
Possible Duplicate: every derived table must have its own alias I need to find
i am trying to create sql procedure or function that need to find duplicate
Need to find the timestamp for the first minute of the first day of
I need to find a way to call a vb.net function in my aspx
I need to find/create a library that can load hdr images in many formats

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.