Python SQLAlchemy - Performing union with three queries
Last Updated :
03 Jun, 2022
In this article, we will see how to perform a union of three queries using SQLAlchemy in Python.
Since we are going to use MySQL in this post, we will also install a SQL connector for MySQL in Python. However, none of the code implementations changes with change in the database except for the SQL connectors.
pip install pymysql
We will use the sample sakila database from MySQL. In this article, we will cover 2 examples, one each for SQLAchemy Core and ORM layers. In both examples, we will extract the records from the payment table within the sakila database which have a `payment_id` of 1, 2, or 3 (as individual queries and then take the union of them). If you do not have the sakila database and want to follow along with this article without installing it then use the SQL script present in the link mentioned below to create the required schema and payment table along with the records.
Sakila Payment Table Script
For reference, the first ten records in the payment table look like this -

The SQL query which we are looking at in the below two examples is -
SELECT * FROM payment WHERE payment_id = 1
UNION
SELECT * FROM payment WHERE payment_id = 2
UNION
SELECT * FROM payment WHERE payment_id = 3;
Method 1: Using SQLAlchemy Core to perform union with three queries:
SQLAlchemy Core is a schema-centric model that means everything is treated as a part of the database i.e., rows, columns, tables, etc. In the above example, we have created the metadata object to access the database objects like the table. Using this object we get the metadata of the `payment` table. This metadata information is then used to query the table to get the union of the three queries. We first prepare individual queries with payment_id as 1, 2, and 3. Then these queries are passed as parameters in the sqlalchemy.union() method. As we can see in the output, we get the records having payment_id as 1, 2, or 3.
Syntax: sqlalchemy.sql.expression.union(*selects, **kwargs)¶
Return a UNION of multiple selectables. The returned object is an instance of CompoundSelect.
Python3
import sqlalchemy as db
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql://root:password@localhost/sakila")
# CREATE THE METADATA OBJECT TO ACCESS THE TABLE
meta_data = db.MetaData(bind=engine)
db.MetaData.reflect(meta_data)
# GET THE `payment` TABLE FROM THE METADATA OBJECT
payment = meta_data.tables['payment']
# PREPARING THE REQUIRED QUERY
query_1 = db.select(payment).filter(payment.c.payment_id == 1)
query_2 = db.select(payment).filter(payment.c.payment_id == 2)
query_3 = db.select(payment).filter(payment.c.payment_id == 3)
query = db.union(query_1, query_2, query_3)
# EXTRACTING RECORDS USING THE ENGINE AND QUERY
with engine.connect() as conn:
result = conn.execute(query).all()
# PRINT THE RESULTANT RECORDS
for r in result:
print(r.payment_id, "|", r.customer_id, "|", r.rental_id, "|", r.amount)
Output:

Method 2: Using SQLAlchemy ORM to perform union with three queries:
SQLAlchemy ORM uses an object-centric view that encapsulates the schema with business objects. It is a more pythonic implementation as we can see the tables represented in the class format. We have used this class object to query the payment table using the SQLAlchemy syntax mentioned above. We created individual queries to extract record(s) from the table having payment_id as 1, 2, or 3. Then the first query which is of `sqlalchemy.orm.Query` type is chained using the union() method. The other two queries are passed as parameters in the union() method. One or more `sqlalachemy.orm.Query` type objects can be passed as parameters to this method.
Syntax: sqlalchemy.orm.Query.union(*q)¶
Produce a UNION of this Query against one or more queries.
Python3
from sqlalchemy.orm import sessionmaker
import sqlalchemy as db
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# DEFINE THE ENGINE (CONNECTIO OBJECT)
engine = db.create_engine("mysql+pymysql://root:password@localhost/sakila")
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Payment(Base):
__table__ = db.Table("payment", Base.metadata, autoload_with=engine)
# CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE
Session = sessionmaker(bind=engine)
session = Session()
# PREPARING QUERY USING SQLALCHEMY
query_1 = session.query(Payment).filter(Payment.payment_id == 1)
query_2 = session.query(Payment).filter(Payment.payment_id == 2)
query_3 = session.query(Payment).filter(Payment.payment_id == 3)
# EXTRACT ALL THE RECORDS BY PERFORMING UNION OF THREE QUERIES
result = query_1.union(query_2, query_3).all()
# PRINT THE RESULTANT RECORDS
for r in result:
print(r.payment_id, "|", r.customer_id, "|", r.rental_id, "|", r.amount)
Output:

Similar Reads
SQLAlchemy Tutorial in Python
This SQLAlchemy Tutorial is very well suited for beginners and also for experienced programmers. This specially designed free SQLAlchemy tutorial will help you learn SQLAlchemy most efficiently, with all topics from basics to advanced. What is SQLAlchemy?SQLAlchemy is referred to as the toolkit of P
4 min read
Serialize Python SqlAlchemy result to JSON
Serialization is converting the object into a format that can be easily stored or transmitted, and later reconstructed. In web applications, it can often involve converting the database query results into JSON format. It is a popular format for the data interchange between clients and servers. SQLAl
4 min read
Python SQLAlchemy - Write a query where a column contains a substring
In this article, we discussed how to extract the column values containing substring in SQLAlchemy against a PostgreSQL database in python. In SQLAlchemy, generic functions like SUM, MIN, MAX, are invoked like conventional SQL functions using the func attribute. Some common functions used in SQLAlch
2 min read
PostgreSQL Query To View with SQLAlchemy
As a software developer, it is a common task to query a PostgreSQL view. Using views which is a virtual table representing the output of a SQL query, is considered to be an efficient way when dealing with a relational database. This article covers how to query a PostgreSQL view using SQLAlchemy in P
9 min read
Python SQLAlchemy - func.count with filter
In this article, we are going to see how to perform filter operation with count function in SQLAlchemy against a PostgreSQL database in python Count with filter operations is performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In Po
3 min read
How to use the IN operator in SQLAlchemy in Python?
In this article, we will see how to use the IN operator using SQLAlchemy in Python. We will cover 2 examples, one each for SQLAchemy Core and ORM layers. In both examples, we will count the number of records present in the category table within the sakila database. The sample data from the table loo
4 min read
How to get all rows with keys provided in a list using SQLalchemy?
In this article, we are going to get all the rows with keys provided in a list using SQLAlchemy. Database used: Note: For this post, we are going to get "name", "class" and "dob" keys from the student table. Installation: Syntax to install SQLAlchemy: pip install sqlalchemy pymysql Note: pymysql is
2 min read
SQLalchemy Bulk insert with one to one relation
When working with databases in Python, SQLAlchemy is a popular and powerful library that provides a high-level interface for interacting with relational databases. It offers an Object-Relational Mapping (ORM) layer that allows developers to work with database tables as Python objects. In this articl
5 min read
How to update existing table rows in SQLAlchemy in Python?
In this article, we are going to see how to use the UPDATE statement in SQLAlchemy against a PostgreSQL database in Python. Creating table for demonstration:Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as sho
3 min read
Sqlalchemy Core With Text SQL For Date Range
SQLAlchemy Core is a low-level SQL abstraction layer of SQLAlchemy, a popular Python Object Oriented Mapping(ORM) library. It provides a way to interact with relational databases wing python code, allowing developers to write SQL Queries in a more object-oriented manner. SQLAlchemy is a python libra
2 min read