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:
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).