Home / Learn Secure SQL / UNION-Based Injection: the Concept
UNION-Based Injection: the Concept
How combining result sets can be misused when a query is not properly separated from input.
Breaking it down
What it is
A UNION-based approach (at the conceptual level) relies on a query being structured so that an attacker-influenced part can be combined with a second, attacker-chosen result set using a SQL set operation, when the application fails to keep code and data separate. The output columns of the two parts must line up in number and type for this to work at all, which is itself a constraint that proper query construction prevents.
Why it matters
This is one of the most direct reasons a vulnerable listing or search feature can expose data far beyond what that one query was ever meant to return — understanding the shape of the risk, without needing working payload details, is what matters for recognizing and prioritizing this class of report.
Mechanically speaking
When assessing whether a reported finding of this type is serious, check whether the affected query is user-facing and whether its underlying table could plausibly contain sensitive columns; prepared statements (covered earlier in this course) close this entire category regardless of how many columns a query returns.
Shows up in
- Web application code review
- Backend development
- Database administration
-- A safely bound query with an explicit column list:
SELECT id, name, price FROM products WHERE id = ?;
-- Because the value is bound, not concatenated, there is no way for it
-- to add a second query to this one's structure, no matter its content.This course deliberately does not walk through constructing a working example of this technique; the prepared-statement and allowlisting patterns taught earlier in this course are what actually prevents it, and are where your engineering time is best spent.
Check yourself
1. What makes a UNION-based technique possible at all?
It requires structural influence over the query, which bound parameters prevent.