Before you can exploit SQLi you need to think in SQL. The same statements that run legitimately against a database are the ones you’ll hijack to pivot queries toward attacker goals.
Core Statements
SELECT — the query you’re hijacking. Returns rows.
SELECT * FROM logins;
SELECT username, password FROM logins WHERE id > 1;INSERT — adds records. Matters when injection lands in a signup or profile update query. A stacked query (; INSERT ...) can create a privileged account.
INSERT INTO users (username, password) VALUES ('admin','P@ssword123');
INSERT INTO users (username, password) VALUES ('a','1'), ('b','2');UPDATE — modifies records. Same risk surface as INSERT.
UPDATE users SET password='new' WHERE id=0;DROP / ALTER — destructive. Rare to hit during exploitation but possible with stacked queries in admin interfaces.
Know the table structure before injecting
DESCRIBE table_nameshows column names and types. If you can read it, your UNION-based payloads become trivial.
Result-Shaping Clauses
Two clauses show up constantly in SQLi payloads: ORDER BY for column counting and LIMIT for row targeting.
ORDER BY — sorts by column number or name. When you inject ORDER BY 10, an “Unknown column ‘10’” error tells you the original query returns fewer than 10 columns. Increment until the error appears.
LIMIT — restricts result count and offset. Useful when the app only displays one row and you need to walk through records.
SELECT * FROM users LIMIT 1; -- first row
SELECT * FROM users LIMIT 1, 1; -- second row (offset 1, count 1)
SELECT * FROM users LIMIT 2, 3; -- rows 3, 4, 5WHERE — the filter clause. Where authentication-bypass payloads live: inject into a WHERE predicate and make it always true.
LIKE — pattern matching with % (any chars) and _ (one char). Useful for boolean-blind extraction (WHERE password LIKE 'a%').
Operators You’ll Use in Payloads
- Comparison:
=,!=,<,>,<=,>=,LIKE - Logical:
AND(&&),OR(||),NOT(!) - Arithmetic:
+,-,*,/,%
Watch out: || means OR in MySQL by default, but means string concatenation in Oracle and PostgreSQL. Fingerprint first.
Comments
Injected payloads usually need to terminate the original query. Comments are how you do that.
--or#— single-line comments/* ... */— multi-line / inline
Gotcha 1: -- on its own isn’t a comment. MySQL requires whitespace or a non-identifier character after the two dashes. In a URL, spaces get encoded to +, so the safest form is -- - (dash space dash).
Gotcha 2: # in a URL is a fragment marker — the browser never sends it to the server. URL-encode it as %23.
Data Types and Quoting
String and date values need quotes (' or "), numbers don’t:
SELECT * FROM users WHERE id = 1; -- numeric
SELECT * FROM users WHERE name = 'admin'; -- stringThis matters for SQLi because the injection context (quoted vs unquoted) determines what you need to break out of. If the query is WHERE id=$input, you can inject 1 OR 1=1. If it’s WHERE name='$input', you need admin' OR '1'='1.
