In this tutorial, I will be explaining how can we find the count, sum and average in PostgreSQL query.
Let's get started...
Finding Count of rows
In this section we will explore how can we find the count of rows.
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax
SELECT COUNT(column_name)
FROM table_name;
Let's understand by an example:
Suppose we want to find the number of customers from the London in our Customer
table.
SELECT COUNT(customer_id)
FROM customers
WHERE city = 'London';
The above query first find the rows which satisfies the WHERE
clause condition. Then counts the total number of rows and returns a number.
OUTPUT
Finding SUM
In this section we will explore how can we use SUM() function.
The SUM() function returns the total sum of a numeric column.
Syntax
SELECT SUM(column_name)
FROM table_name;
Let's understand by an example:
Suppose we want to find the the sum of the quantity
fields in the order_details
table:
SELECT SUM(quantity)
FROM order_details;
The above query returns the total number of items which are in ordered table.
OUTPUT
Finding AVG
In this section we will explore how can we use AVG() function.
The AVG() function returns the average value of a numeric column.
Syntax
SELECT AVG(column_name)
FROM table_name;
Let's understand by an example:
Suppose we want to find the the average price of all the products in the product
table:
SELECT AVG(price)
FROM products;
The above query returns the average price of all the products
OUTPUT
Conclusion
In this table we learnt about how we can use Count
, SUM
and AVG
in a query.
Top comments (0)