MySQL
SECURITY
SQL

SQL Injection Cheat Sheet: SQLi 101

intro

SQL injection is one of the most dangerous attacks targeting applications. This SQL injection cheat sheet will walk you through what it is, how it works, and what you can do to protect against it.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

All developers are aware of security flaws targeting their applications; security engineers will also be aware of the existence of the OWASP Top 10 listing, too. OWASP says that the 2025 edition of the OWASP security flaws should be released in the first half of 2025, as they’re collecting data around security issues and flaws in web applications. We’ll see a refreshed edition of OWASP in 2025, but we’re almost certain that injection will remain a part of the list. Explore this SQL injection cheat sheet!

Injection and Its Characteristics in the OWASP List

Injection was a part of the OWASP list in 2007, 2010, 2017, and 2021. We’re certain that in 2025, injection will remain a part of the list. The reason why we’re saying that is because injection has three key characteristics:

  1. Easy to “deploy” for developers — injection flaws will remain a part of the list because as long as applications are built, there will be people who communicate with their databases by providing user input straight to the database.
  2. Easy to find for hackers or security researchers — many developers choose to not turn off the error messages displayed by their applications and by not doing so, display information of interest to a potentially nefarious party. An attacker using them can guess the state of the database and exploit the security flaw.
  3. Easy to fix — thankfully, while injection is easy to deploy and find, it is also very easy to fix. To fix injection flaws, don’t provide user input to a database by separating the query from the user input.

It’s also worth keeping in mind that injection attacks don’t exclusively refer to SQL injection — what it does refer to is any web-based attack where a user injecting malicious input to harm an application is the culprit. For web applications, it just so happens that most of such situations have something to do with SQL injection, hence the connotation.

Now, keep on reading this SQL injection cheat sheet to find out how that’s done!

How Does SQL Injection Work?

SQL injection is very easy to come across because it’s very simple to write queries that provide user input to the database without verifying the input at hand. This concept is what makes SQL injection possible — if it weren’t for such a concept, this SQL injection cheat sheet wouldn’t exist. What a weird world we live in, huh?

Injecting SQL works in the following way:

  1. A developer writes a SQL query
  2. A developer passes unsanitized input from a user straight into that SQL query (read: an unsanitized variable is passed into a query that is further passed onto a database)
  3. The database executes the query

The weak link here is the second step: an application becomes susceptible to SQL injection once user input is passed directly onto a database. That’s because a malicious payload (a malicious query crafted by an attacker) can extract data from the database or even manipulate some data.

Types of Queries and SQL Injection

Before we jump into the specifics, we’ll walk you through the types of queries working inside of your database. There are four types of queries that can interact with your database:

  1. INSERT queries insert data. As shown in the example above, a SQL query formed with an INSERT statement would insert data into columns. If such a query is susceptible to SQL injection, an attacker could insert data into the database.
  2. UPDATE queries update data. If such a query is susceptible to SQL injection, an attacker could update columns in a table in a database. Such an update is unlikely to do much damage unless an internal logic by the application dictates otherwise.
  3. SELECT queries select data. If such a query is susceptible to SQL injection, an attacker could select sensitive data (think usernames, emails, IP addresses, hashed passwords, etc.) and export it to sell it to nefarious parties for it to be used for credential stuffing later on.
  4. DELETE queries delete data. If such a query is susceptible to SQL injection, precious data may be deleted by an attacker. One can restore it by restoring a backup into a database, but if we don’t get rid of the security flaw, data will get deleted again.

SQL injection is equally dangerous regardless of what query it is based on!

Just as multiple types of queries can be susceptible to SQL injection, there are multiple different types of SQL injection as well: there is classic SQL injection, error-based SQL injection, union-based SQL injection, blind SQL injection, and out-of-band SQL injection.

  • Classic SQL injection is the default “vanilla” form of the vulnerability — an attacker comes across SQL injection by probing for error messages, and launches payloads towards the database to extract the data from it.
  • Error-based SQL injection is based on errors. In other words, if an attacker comes across an application susceptible to an error-based SQL injection attack, an attacker obtains information on the application’s behavior from these error messages. That’s why security experts advise turning error messages off in a live environment.
  • Union-based SQL injection plays on the UNION SQL operator. This operator allows a nefarious party to combine the results of two queries into a single statement the results of which would be returned and visible to an attacker.
  • Blind SQL injection is made possible by an attacker observing the responses of an application. If a payload of OR 1=1 returns a normal-looking page while a payload of OR 1=2 makes some objects on the screen disappear, we’ve got problems. Blind SQL injection also has subtypes — it can either be boolean-based or time-based.

The type of SQL injection an application will be susceptible to depends on how the queries, the application, the database, and the server work in conjunction. We won’t get into all of the nitty-gritty details in this SQL injection cheat sheet, but regardless, you can prevent SQL injection using simple methods that apply to all of the types at once.

Preventing SQL Injection

It’s a well-known fact that SQL injection can be prevented by separating user input from the query.

Prepared statements, or PDO, work by sending the query and the data to the database separately, thus preventing user input from being provided straight to the database, and avoiding SQL injection as a result.

For example, this query would not be susceptible to SQL injection:

A query not susceptible to SQL injection
A query not susceptible to SQL injection

The above example would be a SQL injection cheat sheet example of a PDO-based query.

A good SQL injection cheat sheet should also tell you that prepared statements won’t always protect you from injection either — if you use prepared statements or PDO but still provide user input straight into a database, the end result will still be the same. Here’s an example:

SQL injection example with PDO
SQL injection example with PDO

Even though the user ID is a value of NULL (NULL values increment automatically on a field that has the AUTO_INCREMENT value), the application would be susceptible to SQL injection because the username and the user email would still be provided by the user. The IP address is another discussion because the REMOTE_ADDR value can be easily spoofed and so you would need to look into the HTTP_FORWARDED_FOR value too, but we’re not getting into that here.

A common misconception is that only SELECT queries are a target for SQL injection because of attackers searching for a malicious payload (and thus gaining access to the database) — this SQL injection cheat sheet tells you that it’s not the case!

INSERT queries are susceptible to SQL injection the same way as all other types of queries dependent on user input would: an attacker could simply nullify the rest of the query using -- (a comment sign) and act on data.

Granted, a DELETE query won’t grant the attacker the ability to select usernames and passwords from the database (data would be deleted instead), but nonetheless, the defined action would be taken by the database.

Preventing Other Security Flaws

Preventing SQL injection by using parameterized statements is only half the battle — you shouldn’t rely solely on them for your security strategy. Attackers are searching for and exploiting other security flaws present in your application, too — XSS (Cross-Site Scripting), CSRF (Cross-Site Request Forgery), DoS (Denial of Service), and DDoS (Distributed Denial of Service) attacks are all you need to brace yourself for.

All this means that your application needs a proper security strategy — grab a WAF (Web Application Firewall), harden the security of your server (follow a backup plan, patch vulnerabilities, protect user accounts, and perform other actions), set up a CDN for DDoS protection (CloudFlare or Imperva distributes the traffic coming onto your application by distributing it across multiple servers to lessen the load coming onto the server that hosts your application), and follow other steps outlined in the OWASP Top 10 or other security guidelines.

Consider connecting to your database using an SSH client, make use of privileges and permissions in SQL clients to prevent potentially dangerous queries from being executed in the first place, and you will be on your way.

Permissions in DbVisualizer
Permissions in DbVisualizer

These steps should give you a good head start in securing your application from SQL injection and related attack vectors, but lastly, you shouldn’t forget that the weakest link in the chain is often a human.

Use strong passwords for every online account (password managers do a great job of doing that for you), make use of data breach search engines like BreachDirectory to scan your online accounts through hundreds of publicly available data breaches, and change your passwords if you’re found to be in danger, and use a VPN when connected to publicly accessible networks for your safety.

Follow these basic tips, and even the most sophisticated SQL injection attacks won’t be that scary anymore.

Summary

This SQL injection cheat sheet has provided you with the types of SQL injection that you may encounter and discussed ways to prevent SQL injection from harming your most precious data. Regardless, keep in mind that SQL injection is only the tip of the iceberg when it comes to security — keep yourself informed on ways to protect your application and your database from other attacks like Cross-Site Request Forgery and Cross-Site Scripting, make use of data breach search engines like BreachDirectory to protect your most precious assets on the web, and until next time.

FAQ

What is SQL injection and how does it work?

SQL injection is a type of injection attack outlined in the OWASP Top 10 and other venues that choose your database as its primary victim. An application is susceptible to an injection attack if it provides user input straight into a query and doesn’t sanitize it.

How can I protect my application from SQL injection?

Protect your application from SQL injection by separating user input from the database query. That can be easily done using prepared statements.

What else can I do to protect my application, database, and server?

To further protect your application, database, and the server they run on, consider educating yourself on the rest of the security flaws outlined in the OWASP Top 10 list as well as making use of CDNs to prevent DDoS attacks, not providing information to the users of your application through errors, and use other measures.

How would the BreachDirectory API help the security of my database or application?

Consider using the BreachDirectory API to assist with the security measures of your application and other infrastructure by implementing publicly available data breach data into the lifecycle of your security plan.

By doing so, you will be the first one that get informed once the data from a certain application gets leaked on the web and will be enabled to take preventative action to protect your clients, customer base, employees, and users of your application at the same time.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Clustered vs Non-Clustered Index: Complete SQL Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-02-13
title

Queries and Subqueries in SQL: The Definitive Guide

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-02-12
title

How to Optimize a Clustered Index Scan in SQL

author Antonello Zanini tags MySQL SQL SQL SERVER 7 min 2025-02-11
title

SQL Derived Table: Everything You Need to Know

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-02-06
title

Understanding MVCC in MySQL

author Lukas Vileikis tags DbVisualizer MySQL SQL 7 min 2025-02-05
title

MySQL UPDATE JOIN Queries: A Complete Guide

author Antonello Zanini tags MySQL 8 min 2025-02-04
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2025-02-03
title

How to Protect MySQL With Fail2Ban

author Antonello Zanini tags MySQL 12 min 2025-01-30
title

SQL PARTITION BY in PostgreSQL: A Guide to Window Functions and Data Segmentation

author Leslie S. Gyamfi tags SQL 7 min 2025-01-28
title

A Complete Guide to the ALTER TABLE DROP COLUMN Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-27

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.