DEV Community

Cover image for Aggregate function in PostgreSQL
G Gokul
G Gokul

Posted on

Aggregate function in PostgreSQL

Aggregate function:

  • An aggregate function in PostgreSQL computes a single result from a set of input values.
  • They process multiple rows of data and return a summarized output, often used alongside the GROUP BY clause.

Common Aggregate Functions

table ii
COUNT(): Counts rows from above table

select count(salary) from employees;
Enter fullscreen mode Exit fullscreen mode

count
SUM(): Adds numeric values

select sum(salary) from employees;
Enter fullscreen mode Exit fullscreen mode

sum
MAX(): Finds highest value.

select max(salary) from employees;
Enter fullscreen mode Exit fullscreen mode

max
MIN(): Finds lowest value.

select min(salary) from employees;
Enter fullscreen mode Exit fullscreen mode

min
AVG(): to find average

select avg(salary) from employees;
Enter fullscreen mode Exit fullscreen mode

avg
ROUND(): rounds the value

select round(avg(salary),2) from employees;
Enter fullscreen mode Exit fullscreen mode

round

Sub queries:

queries inside another queries

select name from employees where salary = (select min(salary) from employees);
Enter fullscreen mode Exit fullscreen mode

sub query

select name from employees where salary = (select max(salary) from employees) and name like 'A%';
Enter fullscreen mode Exit fullscreen mode

sub query
to find second largest salary in the above table

select max(salary) from employees where salary not in (select max(salary) from employees);
Enter fullscreen mode Exit fullscreen mode

second max
to find second smallest salary in the above table

select min(salary) from employees where salary not in (select min(salary) from employees);
Enter fullscreen mode Exit fullscreen mode

second min
Group by clause

select count(name), designation from employees group by designation;
Enter fullscreen mode Exit fullscreen mode

group by
Group By and Order By clause

select count(name), designation from employees group by designation order by count(name);
Enter fullscreen mode Exit fullscreen mode

clause
Having clause:
The HAVING clause in PostgreSQL filters group rows created by the GROUP BY clause based on a specified condition.

select count(name), designation from employees 
group by designation 
having count(name)>=2;
Enter fullscreen mode Exit fullscreen mode

having

SQL Topics:

1. DDL:
Data Definition Language (DDL) in PostgreSQL consists of SQL commands used to create, modify, and delete database structures (such as tables, indexes, views, and schemas).

Core DDL Commands:(TBD)
CREATE: Builds new database objects.
ALTER: Modifies existing database structures.
DROP: Deletes database objects permanently.
TRUNCATE: Removes all rows from a table quickly.

2. DML:
Data Manipulation Language (DML) in PostgreSQL consists of the SQL commands used to add, modify, delete, and retrieve data within database tables.

Core DML Commands:(TBD)
SELECT: Retrieves records from one or more tables.
INSERT: Adds new rows of data into a table.
UPDATE: Modifies existing data within a table.
DELETE: Removes existing rows from a table.
MERGE: Conditionally inserts, updates, or deletes rows (available in PostgreSQL 15 and newer).

3. TCL:
TCL (Transaction Control Language) in PostgreSQL consists of commands that manage transactions to ensure data integrity and rollback changes if errors occur.

Core TCL Commands:(TBD)
BEGIN (or START TRANSACTION)
COMMIT
ROLLBACK
SAVEPOINT

4. DQL:
Data Query Language (DQL) in PostgreSQL refers to the subset of SQL commands used to retrieve data from a database, which primarily consists of the SELECT statement.

Core Components of a PostgreSQL DQL Query:
SELECT: Specifies the columns to retrieve.
FROM: Identifies the table or tables to query.
WHERE: Filters rows based on specific conditions.
GROUP BY: Groups rows sharing identical data for aggregate functions.
HAVING: Filters groups created by the GROUP BY clause.
ORDER BY: Sorts the final result set (ascending or descending).
LIMIT / OFFSET: Restricts the number of returned rows.

Top comments (0)