SQL INJECTION Get Certified
Learn what SQL Injection is, why it is dangerous, and how parameterized queries protect applications from malicious input.
What is SQL Injection?
SQL Injection is a security vulnerability that occurs when an application places untrusted user input directly into an SQL query. Instead of being treated only as data, the input may be interpreted as part of the SQL statement itself.
A successful injection can allow attackers to bypass application logic, read unauthorized data, modify records, or perform other unintended database operations.
A Normal Safe Query
What this code does: Creates a customers table and runs a simple, fixed-value search. This is the safe pattern we want applications to follow.
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL,
city TEXT NOT NULL
);
INSERT INTO customers (customer_name, city) VALUES
('Aarav', 'Delhi'),
('Meera', 'Pune'),
('Kabir', 'Jaipur'),
('Riya', 'Mumbai');
SELECT *
FROM customers
WHERE customer_name = 'Meera';
How SQL Injection Happens
The danger appears when an application builds SQL by concatenating user input with the query text. Special characters supplied by a user can then change the meaning of the statement.
What this code does: Shows a normal users table and the kind of query an application would produce for a legitimate username.
DROP TABLE IF EXISTS users;
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
user_role TEXT NOT NULL
);
INSERT INTO users (username, user_role) VALUES
('aman', 'user'),
('neha', 'editor'),
('rohan', 'admin');
-- Example of the SQL produced for a normal username:
SELECT *
FROM users
WHERE username = 'aman';
Why String Concatenation is Dangerous
Directly joining untrusted input into an SQL string allows the input to alter the query structure. This is why applications must never build SQL by simple string concatenation of user data.
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
category TEXT NOT NULL,
price REAL NOT NULL
);
INSERT INTO products (product_name, category, price) VALUES
('Wireless Mouse', 'Electronics', 850),
('Notebook', 'Stationery', 180),
('Desk Lamp', 'Office', 1200),
('USB Cable', 'Electronics', 450);
-- Normal application query:
SELECT *
FROM products
WHERE category = 'Electronics';
Parameterized Queries – The Correct Approach
The recommended defense is a parameterized query (also called a prepared statement). The SQL structure and the user-provided values are handled separately by the database API. The values are never able to change the meaning of the SQL.
What this code does: Demonstrates the safe structure using a fixed department value so the example can run in the editor.
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT NOT NULL,
department TEXT NOT NULL,
salary INTEGER NOT NULL
);
INSERT INTO employees (employee_name, department, salary) VALUES
('Aditi', 'Engineering', 72000),
('Vikram', 'Design', 58000),
('Sana', 'Engineering', 68000),
('Karan', 'Support', 52000);
-- Safe SQL structure (value would be bound by the application API):
SELECT employee_name, department, salary
FROM employees
WHERE department = 'Engineering';
Parameterized INSERT Concept
INSERT statements that accept user data must also use parameters. The SQL command stays fixed; only the values change.
DROP TABLE IF EXISTS messages;
CREATE TABLE messages (
message_id INTEGER PRIMARY KEY AUTOINCREMENT,
author TEXT NOT NULL,
message TEXT NOT NULL
);
INSERT INTO messages (author, message) VALUES
('Maya', 'Welcome to SQL security!'),
('Arjun', 'Parameterized queries help prevent SQL Injection.');
SELECT *
FROM messages
ORDER BY message_id;
Parameterized UPDATE Concept
UPDATE statements that receive values from users should bind those values rather than embed them in the SQL string.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
account_name TEXT NOT NULL,
status TEXT NOT NULL
);
INSERT INTO accounts (account_name, status) VALUES
('North Branch', 'Active'),
('Central Branch', 'Active'),
('South Branch', 'Inactive');
UPDATE accounts
SET status = 'Inactive'
WHERE account_id = 2;
SELECT *
FROM accounts
ORDER BY account_id;
Parameterized DELETE Concept
DELETE operations must also protect any values that originate from users or external applications.
DROP TABLE IF EXISTS tasks;
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY,
task_name TEXT NOT NULL,
task_status TEXT NOT NULL
);
INSERT INTO tasks (task_name, task_status) VALUES
('Backup database', 'Pending'),
('Update documentation', 'Completed'),
('Check server logs', 'Pending'),
('Review reports', 'Completed');
DELETE FROM tasks
WHERE task_id = 2;
SELECT *
FROM tasks
ORDER BY task_id;
Input Validation
Input validation provides an extra layer of protection. An application expecting an age, for example, can verify that the value is numeric and within an acceptable range before it ever reaches the database.
DROP TABLE IF EXISTS students;
CREATE TABLE students (
student_id INTEGER PRIMARY KEY,
student_name TEXT NOT NULL,
age INTEGER NOT NULL
);
INSERT INTO students (student_name, age) VALUES
('Riya', 19),
('Dev', 21),
('Tara', 20),
('Kabir', 23);
SELECT *
FROM students
WHERE age >= 20
ORDER BY age;
Login Queries Need Special Care
Authentication queries are high-risk because they use values supplied through forms. If those values are inserted directly into SQL, the login system can become vulnerable.
DROP TABLE IF EXISTS login_users;
CREATE TABLE login_users (
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
password_hash TEXT NOT NULL
);
INSERT INTO login_users (username, password_hash) VALUES
('riya', 'hash_101'),
('dev', 'hash_202'),
('tara', 'hash_303');
-- Safe query structure demonstrated with a fixed value:
SELECT user_id, username
FROM login_users
WHERE username = 'riya';
Using Parameters in Real Applications
SQLite supports parameter binding through every major programming-language API. The application prepares the SQL statement once and then binds values separately. The exact syntax depends on the language, but the principle is always the same: never concatenate user input into the SQL string.
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
price REAL NOT NULL
);
INSERT INTO products (product_name, price) VALUES
('Mechanical Keyboard', 3200),
('USB-C Adapter', 750),
('Monitor Stand', 2100);
-- SQL statement used by an application (value would be bound):
SELECT product_name, price
FROM products
WHERE product_name = 'USB-C Adapter';
SQL Injection Prevention Methods
Common Mistakes
- Directly concatenating user input into SQL statements.
- Building SQL queries with string interpolation.
- Relying only on client-side validation.
- Giving an application database account excessive privileges.
- Exposing detailed database errors to end users.
- Failing to use prepared statements or parameterized queries.
Best Practices
- Always use parameterized queries for values supplied by users.
- Use prepared statements whenever the database API supports them.
- Validate input according to the application’s requirements.
- Apply the principle of least privilege to database accounts.
- Never expose sensitive database errors to end users.
- Regularly review any application code that builds SQL queries.
SQL Injection occurs when untrusted input can influence the structure of an SQL statement. The most important defense is to use parameterized queries or prepared statements so that user input is always treated as data, never as SQL code. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.