How to get specific columns in SQLAlchemy with filter? Last Updated : 04 Jan, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to query and select specific columns using SQLAlchemy in Python. For our examples, we have already created a Students table which we will be using: Students TableSelecting specific column in SQLAlchemy based on filter:To select specific column in SQLAlchemySyntax: sqlalchemy.select(*entities) entities: Entities to SELECT from. This is typically a series of ColumnElement for Core usage and ORM-mapped classes for ORM usage. To filter records in SQLAlchemy Syntax: sqlalchemy.query.filter(*criterion) criterion: Criterion is one or more criteria for selecting the records. Example 1: Selecting specific column based on a single filter Python import sqlalchemy as db from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() # DEFINE THE ENGINE (CONNECTION OBJECT) engine = db.create_engine("mysql+pymysql://\ root:password@localhost/Geeks4Geeks") # CREATE THE TABLE MODEL TO USE IT FOR QUERYING class Students(Base): __tablename__ = 'students' first_name = db.Column(db.String(50), primary_key=True) last_name = db.Column(db.String(50), primary_key=True) course = db.Column(db.String(50)) score = db.Column(db.Float) # CREATE THE SESSION OBJECT Session = sessionmaker(bind=engine) session = Session() # SELECTING COLUMN `first_name`, `last_name` WHERE `score > 80` result = session.query(Students) \ .with_entities(Students.first_name, Students.last_name) \ .filter(Students.score > 80).all() for r in result: print(r.first_name, r.last_name) Output: Output - Example 1Example 2: Selecting a specific column based on multiple filters Python3 import sqlalchemy as db from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() # DEFINE THE ENGINE (CONNECTION OBJECT) engine = db.create_engine("mysql+pymysql://\ root:password@localhost/Geeks4Geeks") # CREATE THE TABLE MODEL TO USE IT FOR QUERYING class Students(Base): __tablename__ = 'students' first_name = db.Column(db.String(50), primary_key=True) last_name = db.Column(db.String(50), primary_key=True) course = db.Column(db.String(50)) score = db.Column(db.Float) # CREATE THE SESSION OBJECT Session = sessionmaker(bind=engine) session = Session() # SELECTING COLUMN `first_name`, `score` # WHERE `score > 80` AND `course` is STATISTICS result = session.query(Students) \ .with_entities(Students.first_name, Students.score) \ .filter(Students.score > 80, Students.course.like('Statistics')).all() for r in result: print(r.first_name, r.score) Output: Output - Example 2 Comment More infoAdvertise with us Next Article How to get specific columns in SQLAlchemy with filter? apathak092 Follow Improve Article Tags : Python Python-SQLAlchemy Practice Tags : python Similar Reads How to get column names from SQLAlchemy? In this article, we will discuss how to get column names using SQLAlchemy in Python. SQLAlchemy is an open-source SQL toolkit and object-relational mapper for the Python programming language released under the MIT License. It gives full power and flexibility of SQL to an application. To follow along 3 min read How to divide two columns in SQLAlchemy? In this article, we are going to divide two columns using the SQLAlchemy module of python. Installing SQLAlchemy To install SQLAlchemy, run the following command in the terminal. pip install sqlalchemy pymysql So, what we have to do in this post is to divide two columns and get output using SQLAlche 2 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 Querying and selecting specific column in SQLAlchemy In this article, we will see how to query and select specific columns using SQLAlchemy in and For our examples, we have already created a Students table which we will be using: Selecting specific column in SQLAlchemy:Syntax: sqlalchemy.select(*entities) Where: Entities to SELECT from. This is typica 4 min read How to count rows with SELECT COUNT(*) with SQLAlchemy? In this article, we will see how to select the count of rows 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 3 min read How To Select Only One Column Using SQLAlchemy? In this article, we are going to see how to select only one column using SQLAlchemy in Python. SQLAlchemy is a large SQL toolkit with lots of different components. The two largest components are SQLAlchemy Core and SQLAlchemy ORM. The major difference between them is SQLAlchemy Core is a schema-cent 3 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 How to Execute Raw SQL in SQLAlchemy In this article, we will see how to write a Conventional SQL query in SQLAlchemy using text() 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() func 3 min read How to find tables that contain a specific column in SQL using Python? MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a w 3 min read Like