PostgreSQL COUNT

COUNT

  • COUNT function returns the number of input rows that match a specific condition of a query
  • can apply COUNT on a specific column or just pass COUNT(*)
  • must have parenthesis because this is a function
  • COUNT is much more useful when combined with other commands such as DISTINCT
  • COUNT is returning the number of rows, so whether its the whole table or a column the rows should be the same

Examples:

SELECT COUNT(DISTINCT (name)) FROM table;

SELECT COUNT(*) FROM payment;

you can put in a column name example below

SELECT COUNT(amount) FROM payment;

  • if this helps you remember what you are doing or why you wrote it in this format – either way should give you the same result.