Skip to content

Commit 4eefdc0

Browse files
authored
Update sqlalchemy-aggregation.md
1 parent f0db474 commit 4eefdc0

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

contrib/database/sqlalchemy-aggregation.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SQLAlchemy
2-
SQLAlchemy is a powerful and flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python. It is a versatile library that bridges the gap between Python applications and relational databases.<br>
2+
SQLAlchemy is a powerful and flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python. It is a versatile library that bridges the gap between Python applications and relational databases.
33

44
SQLAlchemy allows the user to write database-agnostic code that can work with a variety of relational databases such as SQLite, MySQL, PostgreSQL, Oracle, and Microsoft SQL Server. The ORM layer in SQLAlchemy allows developers to map Python classes to database tables. This means you can interact with your database using Python objects instead of writing raw SQL queries.
55

@@ -10,9 +10,11 @@ SQLAlchemy allows the user to write database-agnostic code that can work with a
1010
```bash
1111
pip install sqlalchemy mysql-connector-python
1212
```
13+
1314
* If not installed, you can install them using the above command in terminal,
1415

1516
## Establishing Connection with Database
17+
1618
* Create a connection with the database using the following code snippet:
1719
```python
1820
from sqlalchemy import create_engine
@@ -27,11 +29,13 @@ session = Session()
2729

2830
Base = declarative_base()
2931
```
32+
3033
* The connection string **DATABASE_URL** is passed as an argument to **create_engine** function which is used to create a connection to the database. This connection string contains the database credentials such as the database type, username, password, and database name.
3134
* The **sessionmaker** function is used to create a session object which is used to interact with the database
3235
* The **declarative_base** function is used to create a base class for all the database models. This base class is used to define the structure of the database tables.
3336

3437
## Creating Tables
38+
3539
* The following code snippet creates a table named **"products"** in the database:
3640
```python
3741
from sqlalchemy import Column, Integer, String, Float
@@ -46,10 +50,12 @@ class Product(Base):
4650

4751
Base.metadata.create_all(engine)
4852
```
53+
4954
* The **Product class** inherits from **Base**, which is a base class for all the database models.
5055
* The **Base.metadata.create_all(engine)** statement is used to create the table in the database. The engine object is a connection to the database that was created earlier.
5156

5257
## Inserting Data for Aggregation Functions
58+
5359
* The following code snippet inserts data into the **"products"** table:
5460
```python
5561
products = [
@@ -63,39 +69,51 @@ products = [
6369
session.add_all(products)
6470
session.commit()
6571
```
72+
6673
* A list of **Product** objects is created. Each Product object represents a row in the **products table** in the database.
6774
* The **add_all** method of the session object is used to add all the Product objects to the session. This method takes a **list of objects as an argument** and adds them to the session.
6875
* The **commit** method of the session object is used to commit the changes made to the database.
6976

7077
## Aggregation Functions
78+
7179
SQLAlchemy provides functions that correspond to SQL aggregation functions and are available in the **sqlalchemy.func module**.
80+
7281
### COUNT
82+
7383
The **COUNT** function returns the number of rows in a result set. It can be demonstrated using the following code snippet:
7484
```python
7585
from sqlalchemy import func
7686

7787
total_products = session.query(func.count(Product.id)).scalar()
7888
print(f'Total products: {total_products}')
7989
```
90+
8091
### SUM
92+
8193
The **SUM** function returns the sum of all values in a column. It can be demonstrated using the following code snippet:
8294
```python
8395
total_price = session.query(func.sum(Product.price)).scalar()
8496
print(f'Total price of all products: {total_price}')
8597
```
98+
8699
### AVG
100+
87101
The **AVG** function returns the average of all values in a column. It can be demonstrated by the following code snippet:
88102
```python
89103
average_price = session.query(func.avg(Product.price)).scalar()
90104
print(f'Average price of products: {average_price}')
91105
```
106+
92107
### MAX
108+
93109
The **MAX** function returns the maximum value in a column. It can be demonstrated using the following code snippet :
94110
```python
95111
max_price = session.query(func.max(Product.price)).scalar()
96112
print(f'Maximum price of products: {max_price}')
97113
```
114+
98115
### MIN
116+
99117
The **MIN** function returns the minimum value in a column. It can be demonstrated using the following code snippet:
100118
```python
101119
min_price = session.query(func.min(Product.price)).scalar()

0 commit comments

Comments
 (0)