Preamble
PostgreSQL UNION statement is used to combine the resulting sets of 2 or more SELECT operators. It removes repetitive lines between different SELECT operators.
Each SELECT statement in a UNION operator must have the same number of fields in the result sets with the same data types.
The syntax for a UNION statement in PostgreSQL
SELECT expression1_id, expression2_id,... expression_n_id
FROM tabs
[WHERE conds]
UNION
SELECT expression1_id, expression2_id,... expression_n_id
FROM tabs
[WHERE conds];
Parameters and arguments of the statement
- expression1_id, expression2_id,…_n_id – The column or the calculation you want to get.
- tabs – The tables from which you want to get the records. The FROM operator must specify at least one table.
- WHERE conds – Optional. The conditions to be met for the records to be selected.
Note:
- Both SELECT statements must have the same number of expressions.
- Since the UNION operator by default removes all repetitive strings from the result set, providing a UNION DISTINCT modifier does not affect the results.
- The column names from the first SELECT operator in UNION are used as column names for the result set.
Example with the return of a single field
Below is an example of PostgreSQL UNION operator, which returns one field from several SELECT operators (and both fields have the same data type):
SELECT category_id
FROM products
UNION
SELECT category_id
FROM categories;
In this example of the PostgreSQL UNION operator, if a category_id appears in both the products table and the categories table, it will appear in your resulting set once. The PostgreSQL UNION statement removes duplicates. If you do not want to remove duplicates, try using PostgreSQL UNION ALL operator.
Example using the ORDER BY statement
PostgreSQL UNION operator can use ORDER BY operator to organize query results.
For example:
SELECT product_id, product_name
FROM products
WHERE product_id >= 24
UNION
SELECT category_id, category_name
FROM categories
WHERE category_name <> 'Hardware'
ORDER BY 2;
In this PostgreSQL operator UNION, since column names in two SELECT operators are different, it is more advantageous to refer to columns in ORDER BY operator by their position in the resulting set. In this example, we have sorted the results by product_name / category_name in ascending order as ORDER BY 2.
The product_name / category_name fields are at position #2 in the resulting set. is used to combine the resulting sets of 2 or more SELECT operators. It removes repetitive lines between different SELECT operators.
Each SELECT statement in a UNION operator must have the same number of fields in the result sets with the same data types.
PostgreSQL Tutorial – UNION and UNION ALL
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Who Should Use Enteros for RevOps Efficiency: AI-Powered Cost Estimation and Performance Optimization
- 23 February 2026
- Database Performance Management
Introduction Revenue Operations (RevOps) has evolved from a coordination function into a strategic growth engine. Today’s organizations rely on RevOps to unify sales, marketing, customer success, finance, and IT around one common objective: predictable revenue growth. But there’s a hidden challenge most revenue leaders underestimate. Behind every CRM workflow, marketing automation campaign, subscription billing system, … Continue reading “Who Should Use Enteros for RevOps Efficiency: AI-Powered Cost Estimation and Performance Optimization”
How to Achieve Banking Cost Transparency and Database Stability with Enteros AIOps Intelligence
Introduction In today’s banking environment, CIOs are balancing three relentless pressures: regulatory compliance, cost optimization, and uninterrupted digital performance. Core banking systems must process millions of transactions per hour. Digital banking platforms must operate 24/7. Fraud detection engines and risk analytics pipelines must run in real time. And all of it must be cost-efficient, audit-ready, … Continue reading “How to Achieve Banking Cost Transparency and Database Stability with Enteros AIOps Intelligence”
What Retail CIOs Should Know About Enteros: AI SQL, SaaS Database Intelligence, and Performance-Driven Growth
- 22 February 2026
- Database Performance Management
Introduction Retail has become a real-time, data-driven industry. From omnichannel commerce and dynamic pricing to AI-powered personalization and global inventory orchestration, modern retail operates on SaaS platforms and cloud-native databases. Every product search, cart update, promotion rule, payment authorization, loyalty redemption, and supply chain update is processed through database systems. As digital commerce expands, SaaS … Continue reading “What Retail CIOs Should Know About Enteros: AI SQL, SaaS Database Intelligence, and Performance-Driven Growth”
What to Prioritize in Healthcare Database Strategy: Enteros AIOps Platform and Cloud FinOps for Sustainable Performance
Introduction Healthcare organizations are under unprecedented pressure to modernize. Electronic health records (EHRs), telemedicine platforms, AI-driven diagnostics, remote patient monitoring, digital imaging, revenue cycle systems, and compliance reporting all depend on reliable, scalable data infrastructure. At the center of this digital ecosystem lies the database layer. Every patient interaction, prescription update, lab result, imaging file … Continue reading “What to Prioritize in Healthcare Database Strategy: Enteros AIOps Platform and Cloud FinOps for Sustainable Performance”