Empowering you to understand your world

PostgreSQL Tutorial: Count Rows In A PostgreSQL Table

If you need to count the number of rows in a table in your PostgreSQL database, you can use the ‘COUNT’ function as shown below:

SELECT COUNT(*) FROM tablename

To count only rows that meet certain criteria in your table, you can add ‘WHERE’ to your statement. Let us pretend for this example that the table is populated with batteries, and we want to count how many of the batteries in the table have a capacity greater than 11:

SELECT COUNT(*) FROM batteries WHERE capacity > 11;

The result should look something like this if there are 2 batteries with a capacity greater than 11:

count
——-
2

Subscribe to our newsletter
Get notified when new content is published