Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);Suppose an application builds SELECT ... WHERE name = ' + input + '. With the input x' OR '1'='1, the final SQL is:
sql
SELECT id, name FROM customers WHERE name = 'x' OR '1'='1' ORDER BY id;| id | name |
|---|---|
| 1 | Ana |
| 2 | Bilal |
| 3 | Chloe |
| 4 | Dev |
The quote in the input closed the string early, and the OR '1'='1' that followed is always true, so every customer is returned. Worse inputs can read other tables with UNION or, where the driver allows several statements, modify data.
With a parameter, the same input is just a strange name that matches nobody:
text
cursor.execute("SELECT id, name FROM customers WHERE name = ?", (user_input,))Escaping quotes by hand is fragile (encodings, different quote rules, numeric contexts). Bound parameters are the primary defence; least-privilege database accounts limit the damage if something slips through.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
SELECT id, name FROM customers WHERE name = 'x' OR '1'='1' ORDER BY id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Injection: input becomes part of the SQL text
- Quotes in input can end strings early
- Bound parameters keep data separate from code
- Least privilege limits damage
Common mistakes
- Escaping quotes by hand instead of using parameters
- Thinking only login forms are at risk
Follow-ups
- Can you bind a table or column name as a parameter?
- How does an ORM help or hurt here?
Tips
- Show the final SQL string the attacker produces