Implementing floor division in SQLAlchemy

Now import func.div() function from sqlalchemy and perform floor division as shown below

Python3




# import necessary packages
from sqlalchemy import func
 
# pass the sql query statement to
# the execute function
result = engine.execute('SELECT div(book_price,3)\
AS minimum FROM books')
 
# use fetchall() to return all result
result.fetchall()


Output:



Floor division in SQLAlchemy

In this article, we will see how to perform floor division in SQLAlchemy against a PostgreSQL database in python.

Floor division is performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In PostgreSQL, floor division is performed using a function called div(). In SQLAlchemy, generic functions like SUM, MIN, MAX are invoked like conventional SQL functions using the func attribute.

Some common functions used in SQLAlchemy are count, cube, current_date, current_time, max, min, mode etc.

Usage: func.count(). func.cube(), func.max()

Similar Reads

Creating table for demonstration:

Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below Create a table called books with columns book_id and book_price....

Implementing floor division in SQLAlchemy

...