DEV Community

Cover image for PostgreSql(Aggregative Functions)
s mathavi
s mathavi

Posted on

PostgreSql(Aggregative Functions)

Aggregative Function:

Aggregate functions perform calculations on multiple rows and return a single result.

Theyโ€™re essential for:

  • Summarizing data (e.g., total salary, average marks)
  • Analyzing trends (e.g., highest sales, number of employees per department)
  • Filtering grouped results using HAVING

#Common SQL Aggregate Functions:

SELECT SUM(salary) FROM Employee;
SELECT COUNT(*) FROM Employee;
SELECT MAX(salary) FROM Employee;
SELECT MIN(salary) FROM Employee;
Enter fullscreen mode Exit fullscreen mode

Using Aggregate Functions with GROUP BY:

SELECT dept_id, AVG(salary)
FROM Employee
GROUP BY dept_id;
Enter fullscreen mode Exit fullscreen mode

Filtering Groups with HAVING

SELECT dept_id, COUNT(*) AS emp_count
FROM Employee
GROUP BY dept_id
HAVING COUNT(*) > 5;
Enter fullscreen mode Exit fullscreen mode

Shows only departments with more than 5 employees.

Bye.. bye..

Top comments (0)