Tony
Tony

Reputation: 479

How to list items and count(*) in PostgreSQL

I'm not sure if it's even possible. Let's say, I have a table A having 100 records. I want to select top five records and also return a total number of elements in this table in one query. How can I do this?

Upvotes: 0

Views: 1104

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270431

You can add the count as an additional column using window functions:

select a.*, count(*) over () as records_in_table
from a
order by <whatever>  -- however you define "top five"
fetch first 5 rows only;

Upvotes: 3

Related Questions