How to get all rows with keys provided in a list using SQLalchemy? Last Updated : 28 Feb, 2022 Comments Improve Suggest changes Like Article Like Report 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 a dependency that we need to install for this post Stepwise ImplementationStep 1 : The first step includes importing the module and connecting to the database. You can do that by the following code: Python3 from sqlalchemy import create_engine user , password , host , database = 'root' , '123' , 'localhost' , 'geeksforgeeks' engine = create_engine(url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8') connection = engine.connect() Step 2 : In the second step, we will define the table name and all the keys inside their respective variables. Python3 table_name = 'student' keys = ['name','class','dob'] Step 3 : Now, firstly in SQL to get particular keys, you need to specify them in the query. The syntax of the SQL query is: SELECT key1 , key2 , ..... keyN FROM table_name; So, we will form a query in python. Firstly we will use the "join" method of python to join all the keys (separated by a comma) and then using "f-string" we will form a query following the above syntax. So our python code for that will be: Python3 keys_joined = ",".join(keys) query = f'SELECT {keys_joined} FROM {table_name}' Step 4 : Now, finally, we will execute the query and print out all the elements in the fetched result using a for loop. Python3 result = connection.execute(query) for elem in result: print(elem) Example: In this example, we are combining all the above-mentioned step into a single code and then displaying all rows with keys provided in a list. Python3 from sqlalchemy import create_engine user , password , host , database = 'root' , '123' , 'localhost' , 'geeksforgeeks' engine = create_engine(url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8') connection = engine.connect() table_name = 'student' keys = ['name','class','dob'] keys_joined = ",".join(keys) query = f'SELECT {keys_joined} FROM {table_name}' result = connection.execute(query) for elem in result: print(elem) Output: Comment More infoAdvertise with us Next Article How to get all rows with keys provided in a list using SQLalchemy? mycodenotein Follow Improve Article Tags : Python Geeks Premier League Geeks-Premier-League-2022 Python-SQLAlchemy Practice Tags : python Similar Reads How to get specific columns in SQLAlchemy with filter? 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: sq 2 min read How to Show All Tables in MySQL using Python? A connector is employed when we have to use mysql with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. In order to make python interact with 1 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 Join with sum and count of grouped rows in SQLAlchemy SQLAlchemy is a popular Python ORM (Object-Relational Mapping) library that provides a convenient way to interact with databases. One of the common tasks when working with databases is to perform joins between tables and calculate aggregate values based on grouped rows. In this article, we will expl 4 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 Returning distinct rows in SQLAlchemy with SQLite In this article, we are going to see how to return distinct rows in SQLAlchemy with SQLite in Python. Installation SQLAlchemy is available via pip install package. pip install sqlalchemy However, if you are using flask you can make use of its own implementation of SQLAlchemy. It can be installed usi 3 min read Python SQLAlchemy - Performing union with three queries 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 co 4 min read How to Install SQLAlchemy in Python in Windows? SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that is used as flexible database access using SQL. In this article, we will look into the process of installing SQLAlchemy on a windows machine. Pre-requisites: The only thing that you need for installing Numpy on Windows are: Python 2 min read How to use avg and sum in SQLAlchemy Query? In this article, we are going to see how to use avg and sum in SQLAlchemy query using Python. Installing SQLAlchemy SQLAlchemy is available via the pip install package. pip install sqlalchemy However, if you are using flask you can make use of its own implementation of SQLAlchemy. It can be installe 2 min read Like