# Raw SQL Queries

**Retrieving Data**

```python
from myapp.models import MyModel
from django.db import connection

# Execute a raw SQL query to retrieve data
with connection.cursor() as cursor:
 cursor.execute("SELECT * FROM myapp_mymodel WHERE price < %s", [100])
 results = cursor.fetchall()
```

**Updating Data**

```python
from myapp.models import Product
from django.db import connection

# Execute a raw SQL query to update data
with connection.cursor() as cursor:
 cursor.execute("UPDATE myapp_product SET price = price * 1.10 WHERE category = %s", ["Electronics"])
```

**Using Parameterized Queries**

```python
from myapp.models import Product
from django.db import connection

# Execute a parameterized raw SQL query
category = "Electronics"
with connection.cursor() as cursor:
 cursor.execute("SELECT * FROM myapp_product WHERE category = %s", [category])
 results = cursor.fetchall()
```

**Mapping Raw SQL to Model Objects**

আমাদের ডেটাবেজ এ myapp\_person নামে টেবিল আছে আমরা ওই টেবিল থেকে ডেটা আমাদের app এ Person নামে মডেল এ ম্যাপিং করবো

```python
from myapp.models import Person

# Execute a raw SQL query to retrieve data and map it to the Person model
for p in Person.objects.raw("SELECT * FROM myapp_person"):
 print(p)
```

