Top 20 SQL Injection & Blind SQLi Interview Questions
SQL Injection (SQLi) has consistently remained near the top of the OWASP Top 10 vulnerabilities, allowing malicious actors to manipulate database queries, bypass authentication filters, and exfiltrate sensitive enterprise data.
Whether you are interviewing for a senior application security engineering role or hardening database layers against boolean-based and time-based blind SQLi, mastering injection mechanics and parameterization is essential.
Core Mechanics of SQL Injection and Blind SQLi
Understanding how SQL vulnerabilities arise and how attackers infer database state without direct error feedback involves evaluating request dynamics:
- In-Band SQLi: Attackers use the same channel of communication (such as direct query responses or error output) to gather data and extract table contents.
- Boolean-Based Blind SQLi: Occurs when an application does not return database data or errors directly, forcing attackers to construct True/False logic statements and infer data character-by-character based on page response variations.
- Time-Based Blind SQLi: Used when no visual response changes occur; attackers inject database sleep or delay functions (e.g.,
PG_SLEEP()orWAITFOR DELAY) and measure HTTP response latency.
Vulnerable Dynamic SQL vs. Parameterized Queries
| Evaluation Pillar | Vulnerable Dynamic Query | Parameterized Secure Query |
|---|---|---|
| Query Construction | Concatenating raw user input directly into SQL strings | Using placeholders and parameterized database drivers |
| Input Interpretation | Interpreted by the SQL engine as executable commands and syntax | Strictly treated as literal data parameters, neutralizing injection |
| Best Suited For | Insecure legacy codebases and unvalidated input handling | Production enterprise web applications, ORMs, and secure microservices |
Python Parameterized Query SQL Security Blueprint
Here is how developers can prevent SQL injection vulnerabilities in Python by utilizing parameterized database queries with SQLite and psycopg2/PostgreSQL:
import sqlite3
def get_user_profile(username):
# 1. Connect to database (or connection pool)
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT, role TEXT)')
cursor.execute("INSERT INTO users (username, role) VALUES ('admin', 'administrator')")
conn.commit()
# 2. Use parameterized queries (?) to prevent SQL injection completely
query = "SELECT id, username, role FROM users WHERE username = ?"
try:
cursor.execute(query, (username,))
result = cursor.fetchone()
if result:
print(f"[SUCCESS] User found: ID={result[0]}, Username={result[1]}, Role={result[2]}")
return result
else:
print("[STATUS] User not found.")
return None
except Exception as e:
print(f"[ERROR] Database query failed: {e}")
return None
finally:
conn.close()
# Execution test
if __name__ == "__main__":
# Simulated user input (even malicious payloads are treated as literal strings)
user_input = "admin' OR '1'='1"
get_user_profile(user_input)
SQL Injection Prevention Best Practices
When securing enterprise applications against SQL injection, blind SQLi, and database compromise, follow these guidelines:
- Always Use Prepared Statements: Enforce parameterized queries or Object-Relational Mappers (ORMs like SQLAlchemy or Hibernate) across all database operations.
- Apply Principle of Least Privilege: Restrict database user accounts used by web applications so they only possess permissions required for their specific tasks (e.g., no
DROPorEXECprivileges). - Disable Detailed Database Errors: Ensure production web applications catch database exceptions globally and return generic error messages to prevent leaking schema details to attackers.