SQL Practice Logo

SQLPractice Online

String Aggregation Functions: Functions

Module: Aggregate Functions & Grouping

-- PostgreSQL

SELECT

department,

STRING_AGG(employee_name, ', ') AS employees

FROM employees

GROUP BY department;

-- With ordering

SELECT

department,

STRING_AGG(employee_name, ', ' ORDER BY employee_name) AS employees

FROM employees

GROUP BY department;

-- MySQL

SELECT

department,

GROUP_CONCAT(employee_name SEPARATOR ', ') AS employees

FROM employees

GROUP BY department;

-- With DISTINCT

SELECT

customer_id,

STRING_AGG(DISTINCT product_name, ', ') AS products_ordered

FROM orders

GROUP BY customer_id;

STRING_AGG(column, delimiter) for PostgreSQL/SQL Server

GROUP_CONCAT(column SEPARATOR delimiter) for MySQL

ORDER BY inside function controls concatenation order

DISTINCT removes duplicate values before aggregation

NULL values typically ignored

Result is single string per group

STRING_AGG(column, delimiter ORDER BY col)

GROUP_CONCAT(column ORDER BY col SEPARATOR delimiter)

STRING_AGG(column, delimiter) WITHIN GROUP (ORDER BY col)

Core references in this topic include =, IS NULL / IS NOT NULL, ANY / ALL. Learn what each one does, when to use it, and the execution or engine rules that matter.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

IS NULL / IS NOT NULL

Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.