PostgreSQL - ARRAY_AGG() Function
Last Updated :
11 Oct, 2024
The PostgreSQL ARRAY_AGG
function is a aggregate function that allows users to combine values from multiple rows into an array. This function is particularly useful when managing grouped data and returning multiple values in a single row.
In this article, We will learn about the What is PostgreSQL ARRAY_AGG
by understanding various examples and so on.
What is PostgreSQL ARRAY_AGG
?
- The
ARRAY_AGG
function in PostgreSQL is an aggregate function that combines values from multiple rows into an array.
- It is especially useful when we need to return multiple values in a single row or group values from related records.
- This function can be applied to a wide range of data types such as integers, strings, dates, and more.
Syntax of ARRAY_AGG:
The basic syntax of the ARRAY_AGG
functions is as follows:
ARRAY_AGG(expression [ORDER BY sorting_expression])
Explanation:
- expression: This is the column or value that you want to aggregate into an array.
- ORDER BY (optional): Specifies the order in which values will be aggregated into the array.
Examples of PostgreSQL ARRAY_AGG() Function
Example 1: Basic Use of ARRAY_AGG
Suppose we have a table called employees with the following structure:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50)
);
INSERT INTO employees (name, department) VALUES
('Alice', 'HR'),
('Bob', 'IT'),
('Charlie', 'HR'),
('David', 'Finance'),
('Eve', 'IT');
Now, we want to group the employees by their department and return an array of employee names for each department.
SELECT department, ARRAY_AGG(name) AS employees
FROM employees
GROUP BY department;
Output:
department | employees |
---|
HR | {Alice, Charlie} |
IT | {Bob, Eve} |
Finance | {David} |
Explanation:
- In this query, the
ARRAY_AGG
function aggregates the employee names for each department into an array. - The
GROUP BY
clause ensures that names are grouped by department.
Example 2: Using ORDER BY
with ARRAY_AGG
We can also specify the order in which values should appear in the array by using the ORDER BY
clause.
SELECT department, ARRAY_AGG(name ORDER BY name DESC) AS employees
FROM employees
GROUP BY department;
Output:
department | employees |
---|
HR | {Charlie, Alice} |
IT | {Eve, Bob} |
Finance | {David} |
Explanation:
- The
ORDER BY name DESC
orders the employee names in descending order within the array.
Example 3: Aggregating Integer Values
Let’s consider a sales table where we want to aggregate sales amounts into an array for each product.
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
product_name VARCHAR(100),
amount INTEGER
);
INSERT INTO sales (product_name, amount) VALUES
('Product A', 200),
('Product A', 150),
('Product B', 300),
('Product B', 250),
('Product C', 100);
Now, aggregate the sales amounts for each product using ARRAY_AGG
.
SELECT product_name, ARRAY_AGG(amount) AS sales_amounts
FROM sales
GROUP BY product_name;
Output:
product_name | sales_amounts |
---|
Product A | {200, 150} |
Product B | {300, 250} |
Product C | {100} |
Explanation:
- This query collects all sales amounts into an array for each product.
Example 4: Using DISTINCT
with ARRAY_AGG
SELECT product_name, ARRAY_AGG(DISTINCT amount) AS unique_sales
FROM sales
GROUP BY product_name;
Output:
product_name | unique_sales |
---|
Product A | {200, 150} |
Product B | {300, 250} |
Product C | {100} |
Explanation:
- The
DISTINCT
keyword ensures that duplicate sales amounts are excluded from the array.
Advantages of Using ARRAY_AGG
- Data Aggregation:
ARRAY_AGG
simplifies the task of aggregating multiple values from rows into a single array, which is ideal for reporting and analytics.
- Flexible Data Type Support: It works with various data types, including text, numbers, and dates.
- Efficient Querying: You can use
ARRAY_AGG
to efficiently manage and retrieve grouped data with minimal effort.
- Maintains Data Order: The optional
ORDER BY
clause allows for controlling the order of elements in the resulting array.
Important Points About ARRAY_AGG() Function in PostgreSQL
ARRAY_AGG
()
in PostgreSQL is an aggregate function that collects a set of values into an array and returns that array as a result.
ARRAY_AGG
()
effectively handles NULL values in aggregated columns, ensuring comprehensive data aggregation across rows.
- While primarily used in PostgreSQL, similar array aggregation functions are available in other SQL databases, each with its own syntax and capabilities.
- The
ARRAY_AGG
()
function aggregates values specified by the expression
into an array. Optional ORDER BY
clause allows sorting of elements within the array based on specified criteria.
Conclusion
Overall, the PostgreSQL ARRAY_AGG
function is an essential tool for aggregating data into arrays, especially when dealing with grouped datasets. Its ability to aggregate values efficiently, work with different data types, and allow for ordered data using the ORDER BY
clause makes it a powerful asset for anyone working with PostgreSQL.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read