Python Data Science Jobs & Interviews
20.7K subscribers
195 photos
4 videos
26 files
338 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Question 31 (Intermediate - Django ORM):
When using Django ORM's select_related() and prefetch_related() for query optimization, which statement is correct?

A) select_related uses JOINs (1 SQL query) while prefetch_related uses 2+ queries
B) Both methods generate exactly one SQL query
C) prefetch_related works only with ForeignKey relationships
D) select_related is better for many-to-many relationships

#Python #Django #ORM #Database

By: https://t.iss.one/DataScienceQ
1🔥1
Q: How can you implement a thread-safe, connection-pooling mechanism using Python's sqlite3 with concurrent.futures.ThreadPoolExecutor, while ensuring atomic transactions and handling database schema migrations dynamically? Provide a complete example with error handling and logging.

A:
import sqlite3
import threading
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import contextmanager
import os
import time

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Database path
DB_PATH = "example.db"

# Schema definition
SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
"""

# Connection pool with threading
class DatabaseConnectionPool:
def __init__(self, db_path, max_connections=5):
self.db_path = db_path
self.max_connections = max_connections
self._connections = []
self._lock = threading.Lock()

def get_connection(self):
with self._lock:
if self._connections:
return self._connections.pop()
else:
return sqlite3.connect(self.db_path)

def release_connection(self, conn):
with self._lock:
if len(self._connections) < self.max_connections:
self._connections.append(conn)
else:
conn.close()

def close_all(self):
with self._lock:
for conn in self._connections:
conn.close()
self._connections.clear()

@contextmanager
def get_db_connection(pool):
conn = pool.get_connection()
try:
yield conn
except Exception as e:
conn.rollback()
logger.error(f"Database error: {e}")
raise
finally:
pool.release_connection(conn)

def execute_transaction(pool, query, params=None):
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.execute(query, params or ())
conn.commit()

def create_user(pool, name, email):
query = "INSERT INTO users (name, email) VALUES (?, ?)"
try:
execute_transaction(pool, query, (name, email))
logger.info(f"User {name} created.")
except sqlite3.IntegrityError:
logger.warning(f"Email {email} already exists.")

def fetch_users(pool):
query = "SELECT id, name, email FROM users"
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.execute(query)
return cursor.fetchall()

def schema_migration(pool, new_schema):
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.executescript(new_schema)
conn.commit()
logger.info("Schema migration applied.")

# Example usage
if __name__ == "__main__":
# Initialize pool
pool = DatabaseConnectionPool(DB_PATH)

# Apply schema
schema_migration(pool, SCHEMA)

# Simulate concurrent user creation
names_emails = [("Alice", "[email protected]"), ("Bob", "[email protected]")]

with ThreadPoolExecutor(max_workers=4) as executor:
futures = [
executor.submit(create_user, pool, name, email)
for name, email in names_emails
]
for future in as_completed(futures):
try:
future.result()
except Exception as e:
logger.error(f"Task failed: {e}")

# Fetch results
users = fetch_users(pool)
logger.info(f"Users: {users}")

# Cleanup
pool.close_all()

#Python #SQLite #Database #Multithreading #ThreadSafety #ConnectionPooling #AtomicTransactions #SchemaMigration #Concurrency #Programming #AdvancedPython

By: @DataScienceQ 🚀
Django ORM Tip: F() Expressions for Database-Level Operations

F() expressions allow you to reference model field values directly within database operations. This avoids fetching data into Python memory, making queries more efficient for updates or comparisons directly on the database.

from django.db.models import F
from your_app.models import Product # Assuming a Product model with 'stock' and 'price' fields

Increment the stock of all products by 5 directly in the database

Product.objects.all().update(stock=F('stock') + 5)

Update the price to be 10% higher than the current price

Product.objects.all().update(price=F('price') 1.1)

Filter for products where the stock is less than 10 times its price

low_ratio_products = Product.objects.filter(stock__lt=F('price') 10)


#Django #DjangoORM #Python #Database #Optimization #Fexpressions #CodingTip

---
By: @DataScienceQ
Top 100 SQL Interview Questions & Answers

#SQL #InterviewQuestions #DataAnalysis #Database #SQLQueries

👇👇👇👇👇
Please open Telegram to view this post
VIEW IN TELEGRAM
Top 100 SQL Interview Questions & Answers

#SQL #InterviewQuestions #DataAnalysis #Database #SQLQueries

Part 1: Basic Queries & DML/DDL (Q1-20)

#1. Select all columns and rows from a table named products.
A: Use SELECT * to retrieve all columns.

SELECT *
FROM products;

Output: All data from the 'products' table.


#2. Select only the product_name and price columns from the products table.
A: List the desired column names.

SELECT product_name, price
FROM products;

Output: A table with two columns: 'product_name' and 'price'.


#3. Find all products with a price greater than 50.
A: Use the WHERE clause with a comparison operator.

SELECT product_name, price
FROM products
WHERE price > 50;

Output: Products whose price is more than 50.


#4. Find all products that are red or have a price less than 20.
A: Use WHERE with OR to combine conditions.

SELECT product_name, color, price
FROM products
WHERE color = 'Red' OR price < 20;

Output: Products that are red OR cheaper than 20.


#5. Find all products that are red AND have a price greater than 100.
A: Use WHERE with AND to combine conditions.

SELECT product_name, color, price
FROM products
WHERE color = 'Red' AND price > 100;

Output: Products that are red AND more expensive than 100.


#6. Select all unique category values from the products table.
A: Use the DISTINCT keyword.

SELECT DISTINCT category
FROM products;

Output: A list of unique categories (e.g., 'Electronics', 'Books').


#7. Count the total number of products in the products table.
A: Use the COUNT(*) aggregate function.

SELECT COUNT(*) AS total_products
FROM products;

Output: A single number representing the total count.


#8. Find the average price of all products.
A: Use the AVG() aggregate function.

SELECT AVG(price) AS average_price
FROM products;

Output: A single number representing the average price.


#9. Find the highest and lowest price among all products.
A: Use the MAX() and MIN() aggregate functions.

SELECT MAX(price) AS highest_price, MIN(price) AS lowest_price
FROM products;

Output: Two numbers: the maximum and minimum price.


#10. Sort products by price in ascending order.
A: Use ORDER BY with ASC (or omit ASC as it's the default).

SELECT product_name, price
FROM products
ORDER BY price ASC;

Output: Products listed from cheapest to most expensive.


#11. Sort products by price in descending order.
A: Use ORDER BY with DESC.

SELECT product_name, price
FROM products
ORDER BY price DESC;

Output: Products listed from most expensive to cheapest.


#12. Sort products by category (ascending), then by price (descending).
A: Specify multiple columns in ORDER BY.

SELECT product_name, category, price
FROM products
ORDER BY category ASC, price DESC;

Output: Products grouped by category, then sorted by price within each category.