How to divide two columns in SQLAlchemy?
Last Updated :
28 Feb, 2022
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 SQLAlchemy.
Database used:
So, we have the table named "players" and what we need to do is to divide the column "score" with the column "matches_played" and get the result. We can do the given task using 2 methods . Both of the methods are as follows.
Method 1: In this method, what we will do is we will, firstly, connect to the database and then create an SQL query in which we will divide both the columns, then we will execute the query and finally, we will fetch the output.
The SQL query will look like the following:
SELECT column1 / column2 FROM table_name;
Example:
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 = 'players'
column1 = 'score'
column2 = 'matches_played'
result = connection.execute(f'SELECT {column1} / {column2} FROM {table_name}')
for value in result:
print("Value : ", value)
Note: The value obtained is in a tuple and you can do value[0] (in this case) to get the value and store it directly .
Method 2: This method involves doing the division through python.
The SQL query will look like this:
SELECT column1 , column2 FROM table_name;
Example:
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 = 'players'
column1 = 'score'
column2 = 'matches_played'
result = connection.execute(f'SELECT {column1} , {column2} FROM {table_name}')
for value in result:
x = value[0] / value[1]
print("Value : ", x)
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 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 change datetime to string in SQLAlchemy query? In this article, we are going to change DateTime to string in sqlalchemy query in the python programming language. Database used: Installation Syntax to install sqlalchemy and pymysql: pip install sqlalchmey pymysql Note: pymysql is a dependency of sqlalchemy which we need to install for this post F
2 min read
SQLAlchemy - Mapping Table Columns In this article, we will see how to map table columns using SQLAlchemy in Python. You will need a database (MySQL, PostgreSQL, SQLite, etc) to work with. 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
5 min read
Column and Data Types in SQLAlchemy SQLAlchemy is an open-source library for the Python programming language that provides a set of tools for working with databases. It allows developers to interact with databases in a more Pythonic way, making it easier to write code that is both efficient and readable. Column TypesA column type in S
4 min read