What Is SQL Injection?
SQL injection (SQLi) is a vulnerability where an attacker inserts malicious SQL code into an application's input field, tricking the underlying database into executing commands the developer never intended. Instead of the input being treated as plain data — a username, a search term — part of it gets interpreted as executable database code.
Most web applications build database queries by combining fixed SQL logic with user-supplied input — a login form takes a username and password and slots them into a query that checks the database for a match. SQL injection happens when that input isn't properly separated from the query's actual code, so a user can submit input containing SQL syntax that the database executes as part of the command itself, rather than treating it as a plain value to search for.
This is what makes SQL injection so consequential: it doesn't just break one feature, it can expose the application's entire underlying data — user credentials, financial records, personal information — or, depending on database permissions, let an attacker modify or delete data outright. It's consistently ranked among the OWASP Top 10 vulnerabilities and has been behind some of the largest data breaches in the industry's history, precisely because a single unvalidated input field can expose an entire database.
It's also a good illustration that this class of vulnerability isn't limited to web forms — in 2018, a security researcher registered a physical license plate containing SQL injection syntax specifically to test whether parking enforcement systems that scan and log plates were vulnerable to it. Any system that takes external input and eventually inserts it into a database query is a potential target, not just login boxes.
Use Cases (Where SQL Injection Occurs)
- Login forms — manipulating the username or password field to bypass authentication entirely, without knowing valid credentials
- Search boxes — injecting SQL into a search query to extract data far beyond what the search feature was meant to return
- URL parameters — modifying a query string parameter (like a product ID) that gets passed directly into a database lookup
- Any external input reaching a query — including unconventional sources like license plates, HTTP headers, or file uploads with metadata that eventually reaches a database call
Best Practices
- Use parameterized queries (prepared statements) as the default way of building any query that includes user input — this is the single most effective defense, since it keeps input strictly separated from executable SQL
- Use an ORM (object-relational mapping) library correctly, which handles parameterization automatically in most standard use cases
- Apply the principle of least privilege to database accounts used by applications, limiting the damage possible even if an injection succeeds
- Validate and sanitize input as a secondary layer of defense, not a replacement for parameterized queries
- Avoid constructing SQL queries through direct string concatenation of user input under any circumstances
How It Works: Step-by-Step
- Identify an input point that reaches a database query. The attacker finds a form field, URL parameter, or other input that eventually gets used in a SQL query.
- Test for injection. The attacker submits SQL syntax (a single quote, a comment sequence, boolean logic) to see if it breaks or alters the query's expected behavior — a common sign the input isn't properly parameterized.
- Craft a payload. Once confirmed vulnerable, the attacker builds input that manipulates the query's logic — bypassing a login check, appending a second query to extract additional data, or using conditional logic to extract data one bit at a time (blind SQL injection).
- Execute and extract or manipulate data. The database executes the attacker's injected logic as part of the original query, returning unauthorized data or performing unauthorized changes.
Types of SQL Injection
- In-band (classic) SQLi. The attacker uses the same communication channel to both launch the attack and see the results — the most straightforward and common form.
- Blind SQLi. The application doesn't return query results or error messages directly, so the attacker infers information indirectly — through true/false response differences (boolean-based) or response timing (time-based).
- Out-of-band SQLi. The attacker retrieves data through a different channel than the one used to inject the payload, such as triggering a DNS or HTTP request to a server they control — used when in-band and blind techniques aren't feasible.
Conclusion
SQL injection persists not because it's hard to prevent, but because prevention requires a consistent habit — parameterized queries used everywhere input reaches a database, with no exceptions carved out for "just this one form." A single unparameterized query is enough to expose an entire database, which is exactly why this vulnerability class has remained a top security concern for over two decades despite the fix being well understood.
FAQ
What's the difference between SQL injection and XSS?
SQL injection targets the application's database, injecting malicious SQL that the backend executes. Cross-site scripting (XSS) targets other users of the application, injecting malicious script that runs in their browser. SQLi compromises data; XSS compromises other users' sessions or browsers. They can sometimes appear in the same vulnerable application, but they exploit entirely different trust boundaries.
How is SQL injection actually prevented?
The primary defense is parameterized queries (prepared statements), which keep user input structurally separate from the SQL command itself, so injected syntax can never be interpreted as code. Input validation and least-privilege database accounts add additional layers, but they're not substitutes for parameterization — an application relying only on input filtering is still commonly bypassable.
Is SQL injection still a relevant threat today?
Yes. Despite being a well-understood, well-documented vulnerability class for over two decades, it remains one of the most commonly found issues in security assessments and bug bounty programs — usually in legacy code, third-party integrations, or newer code written without parameterized queries as the default habit.
What can an attacker actually do with a successful SQL injection?
Depending on database permissions, an attacker can read data they shouldn't have access to (including other users' credentials), modify or delete records, and in some database configurations, execute operating system commands on the underlying server — turning a data vulnerability into full system compromise.