RMRM Full Stack & AI Engineer · All guides · Roadmaps
Security · guide

SQL Injection Explained

SQL Injection (SQLi) is one of the most prevalent and dangerous web security vulnerabilities, allowing attackers to manipulate database queries by inserting malicious SQL code into user-supplied input. Understanding how it works is essential for every developer building data-driven applications.

What Is SQL Injection?

SQL Injection occurs when an application incorporates untrusted user input directly into a SQL query without proper sanitization or parameterization. An attacker can craft input that alters the query's logic, potentially exposing, modifying, or deleting data. For example, entering ' OR '1'='1 into a login field can bypass authentication entirely. It consistently ranks in the OWASP Top 10 list of critical web application security risks.

Why It Matters

A successful SQLi attack can give an attacker full read and write access to your database, exposing sensitive user data, credentials, and business records. In severe cases, attackers can execute operating system commands through the database engine, escalating to full server compromise. Data breaches caused by SQLi have cost companies millions of dollars in fines, lawsuits, and reputational damage. Regulations like GDPR and PCI-DSS hold organizations legally accountable for such vulnerabilities.

How It Works

Consider this vulnerable query: SELECT * FROM users WHERE username = '[input]' AND password = '[input]'. If an attacker submits admin' -- as the username, the double dash comments out the rest of the query, bypassing the password check entirely. The database then executes SELECT * FROM users WHERE username = 'admin' --, which returns the admin record without a valid password. The root cause is the database being unable to distinguish between the developer's intended query structure and injected attacker-controlled data.

Types of SQL Injection

In-band SQLi is the most common type, where results are returned directly in the application's HTTP response, including Error-based and Union-based techniques. Blind SQLi occurs when the application gives no direct output, so attackers infer data by asking true/false questions (Boolean-based) or measuring server response delays (Time-based). Out-of-band SQLi exfiltrates data via separate channels like DNS or HTTP requests and is less common but highly dangerous. Each variant requires different detection and exploitation techniques.

Prevention: Parameterized Queries and Prepared Statements

The primary defense is using parameterized queries (also called prepared statements), which separate SQL code from data so user input is never interpreted as part of the query. In Python with psycopg2, use cursor.execute('SELECT * FROM users WHERE username = %s', (username,)) instead of string concatenation. ORMs like SQLAlchemy or Hibernate also enforce parameterization by default. Always combine this with input validation, least-privilege database accounts, and a Web Application Firewall (WAF) as a defense-in-depth strategy.

Key Gotcha: Escaping Alone Is Not Enough

A common misconception is that manually escaping special characters like single quotes is a sufficient defense — it is not. Escaping can be bypassed through encoding tricks, second-order injection (where sanitized data is stored then later re-used unsafely), or developer mistakes. Parameterized queries are the only reliable primary defense because they are enforced at the database driver level, not in application code. Always treat parameterization as non-negotiable and escaping as a last-resort supplement, never a standalone solution.

Go deeper with an AI tutor that teaches this in context — and quizzes you on it.
Open the app — free to start

© RM Full Stack & AI Engineer · All guides · Roadmaps · Open the app