Back to Blog
2026-05-15· 8 min read

SQL Injection Tutorial — Step-by-Step Guide with Examples | Vaarta

Learn SQL injection attacks step-by-step. Union-based, error-based, and blind SQLi techniques with real examples, detection methods, and prevention.

SQL Injection Web Hacking OWASP Tutorial

What is SQL Injection?


SQL injection (SQLi) is a code injection technique that exploits security vulnerabilities in database-driven applications. It occurs when user input is inserted into SQL queries without proper sanitization.


Why SQL Injection is Dangerous


  • Data theft Extract entire databases
  • Authentication bypass Access without credentials
  • Data modification Insert, update, or delete records
  • Server compromise Execute system commands in some cases

  • How SQL Injection Works


    Basic Authentication Bypass


    **Vulnerable Code**:

    ```sql

    SELECT * FROM users WHERE username = '$input' AND password = '$pass'

    `

    **Attack Input**:

    `

    Username: admin' OR '1'='1' --

    Password: anything

    `

    **Resulting Query**:

    ```sql

    SELECT * FROM users WHERE username = 'admin' OR '1'='1' --' AND password = 'anything'

    `

    The `--` comments out the password check, and `OR '1'='1'` is always true.


    Union-Based SQLi


    **Attack**:

    `

    ' UNION SELECT username, password FROM users --

    `

    **Result**: Returns all usernames and passwords


    Error-Based SQLi


    **Attack**:

    `

    ' AND 1=CONVERT(int, (SELECT TOP 1 table_name FROM information_schema.tables)) --

    `

    **Result**: Database structure leaked through error messages


    Blind SQLi


    **Attack**: Boolean-based

    `

    ' AND 1=1 -- (true - page loads normally)

    ' AND 1=2 -- (false - page behaves differently)

    `

    **Attack**: Time-based

    `

    '; WAITFOR DELAY '0:0:5' -- (5 second delay if vulnerable)

    `

    Real-World Examples


    Heartland Payment Systems (2008)

  • SQL injection exposed 130 million credit card numbers
  • Cost: $140 million in damages

  • Sony Pictures (2011)

  • SQL injection on a music website
  • Led to massive data breach

  • TalkTalk (2015)

  • SQL injection by teenage attacker
  • 157,000 customer records exposed

  • Detection Methods


    1. Manual Testing

    Try these inputs in login forms:

    `

    ' OR '1'='1

    admin' --

    ' UNION SELECT NULL --

    `

    2. Automated Tools

  • SQLMap Automates SQL injection detection
  • Burp Suite Tests for injection points
  • OWASP ZAP Scans for SQLi vulnerabilities

  • 3. Code Review

    Look for:

  • String concatenation in queries
  • Unescaped user input
  • Dynamic SQL construction

  • Prevention


    1. Parameterized Queries (Best)

    ```python

    cursor.execute("SELECT * FROM users WHERE username = %s", (username,))

    `

    2. ORM Usage

    ```python

    User.objects.filter(username=username)

    `

    3. Input Validation

    ```python

    if not re.match("^[a-zA-Z0-9_]+$", username):

    raise ValueError("Invalid username")

    `

    4. Stored Procedures

    ```sql

    CREATE PROCEDURE GetUser @username NVARCHAR(50)

    AS

    SELECT * FROM users WHERE username = @username

    `

    Vaarta.space and SQLi


    While our scanner focuses on DNS, SSL, and headers, understanding SQLi helps you:

  • Evaluate application security
  • Communicate with development teams
  • Implement proper security controls

  • Conclusion


    SQL injection is preventable. Use parameterized queries, validate input, and test regularly.


    Ready to check your domain security?

    Run a free scan to identify potential vulnerabilities.

    Start Free Scan