September 20, 2026
SQL Fundamentals โ Complete Beginner Guide
Introduction

By Shadow
10 min read
SQL Fundamentals โ Complete Beginner Guide
Introduction
Databases are one of the most important components of modern web applications.
Websites and applications use databases to store and manage information such as users, products, orders, messages, and authentication data.
For anyone learning web penetration testing, understanding databases and SQL is essential because many web applications rely on SQL queries to process user input.
This guide covers the fundamental concepts of databases, SQL, DBMS, database structure, CRUD operations, filtering, operators, functions, and relationships between tables.
1. What is a Database?
A database is an organized collection of data that can be stored, accessed, managed, and analyzed.
Databases are used in many types of applications, including:
- Authentication systems
- Social media platforms
- Streaming services
- E-commerce websites
- Banking systems
- Content management systems
- Web applications
A simplified web application architecture looks like:
User
โ
Web Application
โ
Database
โ
Stored DataUser
โ
Web Application
โ
Database
โ
Stored DataFor example, an e-commerce application might store:
Users
Products
Orders
Payments
AddressesUsers
Products
Orders
Payments
Addresses2. Types of Databases
There are two major categories worth understanding at the beginner level:
Relational Databases โ SQL
Relational databases store structured data using tables.
A table consists of:
Rows
ColumnsRows
ColumnsExamples include:
- MySQL
- MariaDB
- PostgreSQL
- Oracle Database
- Microsoft SQL Server
- SQLite
Relational databases are commonly used when data has clearly defined relationships and structures.
Examples:
Users
Products
Orders
Customers
TransactionsUsers
Products
Orders
Customers
TransactionsNon-Relational Databases โ NoSQL
NoSQL databases do not necessarily use traditional tables, rows, and columns.
They can store data using formats such as:
- Documents
- Key-value pairs
- Graphs
- Wide-column structures
Examples include:
- MongoDB
- Redis
- Cassandra
NoSQL databases are useful when applications need flexible data structures or specific scalability models.
Important distinction
SQL
โ Usually structured relational data
NoSQL
โ Flexible non-relational data modelsSQL
โ Usually structured relational data
NoSQL
โ Flexible non-relational data modelsNoSQL does not simply mean "no structure"; it means the database does not primarily follow the traditional relational model.
3. Tables, Rows, and Columns
Understanding the structure of a relational database is essential.
Table
A table is a collection of related data.
Example:
Books
--------------------------------
id | name | author
--------------------------------
1 | Book A | Author 1
2 | Book B | Author 2
3 | Book C | Author 3Books
--------------------------------
id | name | author
--------------------------------
1 | Book A | Author 1
2 | Book B | Author 2
3 | Book C | Author 3Column
A column defines an attribute of the stored data.
Examples:
id
name
email
author
priceid
name
email
author
priceEach column normally has a specific data type.
Row
A row represents one complete record.
For example:
1 | Book A | Author 11 | Book A | Author 1is one row in the Books table.
4. Common Data Types
Databases support different types of data.
Common examples include:
String
Used for text.
Alice
hello@example.com
Book AAlice
hello@example.com
Book AInteger
Used for whole numbers.
1
25
1001
25
100Float / Decimal
Used for numbers containing decimal values.
19.99
3.14
99.5019.99
3.14
99.50Date / Time
Used for dates and timestamps.
2026-09-20
2026-09-20 14:30:002026-09-20
2026-09-20 14:30:00The exact data types available can vary between database systems.
5. Primary Keys
A Primary Key (PK) uniquely identifies a row in a table.
Example:
Books
-------------------
id (PK)
-------------------
1
2
3Books
-------------------
id (PK)
-------------------
1
2
3The primary key should uniquely identify each record.
Important characteristics:
- Uniquely identifies a record
- Cannot contain duplicate values
- A table has one primary key constraint
- It can consist of one column or multiple columns
For a simple example:
id | name
-----------
1 | Book A
2 | Book B
3 | Book Cid | name
-----------
1 | Book A
2 | Book B
3 | Book CThe id column identifies each book.
6. Foreign Keys
A Foreign Key (FK) creates a relationship between tables.
For example:
Books
----------------
id
author_id โ FKBooks
----------------
id
author_id โ FKand:
Authors
----------------
id โ PK
nameAuthors
----------------
id โ PK
nameThe relationship can be represented as:
Books.author_id
|
โ
Authors.idBooks.author_id
|
โ
Authors.idA foreign key usually references a primary key or another candidate key in another table.
Unlike a primary key, a foreign key can contain repeated values because multiple records may reference the same record.
7. Primary Key vs Foreign Key
Primary KeyForeign KeyUniquely identifies a recordReferences a record in another tableEnforces uniquenessDoes not have to be uniqueUsed to identify rowsUsed to create relationshipsOne primary key constraint per tableMultiple foreign key constraints can existCommonly referenced by foreign keysCommonly references another table's key
Easy way to remember
PK = Unique identifier
FK = Link between tablesPK = Unique identifier
FK = Link between tables8. What is SQL?
SQL stands for Structured Query Language.
It is used to communicate with relational database systems.
SQL can be used to:
- Retrieve data
- Insert data
- Update data
- Delete data
- Create databases
- Create tables
- Modify tables
- Manage database structures
Example:
SELECT * FROM users;SELECT * FROM users;This asks the database to return all columns from the users table.
9. What is a DBMS?
DBMS stands for Database Management System.
A DBMS is software used to create, manage, store, and interact with databases.
Examples include:
- MySQL
- MariaDB
- PostgreSQL
- Oracle Database
- Microsoft SQL Server
- SQLite
- MongoDB
One important distinction:
Database
= Stored data
DBMS
= Software that manages the database
SQL
= Language used to communicate with a relational DBMSDatabase
= Stored data
DBMS
= Software that manages the database
SQL
= Language used to communicate with a relational DBMSMongoDB is a DBMS, but it is a NoSQL database system rather than a traditional relational SQL DBMS.
10. Connecting to MySQL
A common MySQL login command is:
mysql -u root -pmysql -u root -pThe options mean:
-u
โ Specify username
root
โ Username
-p
โ Prompt for password-u
โ Specify username
root
โ Username
-p
โ Prompt for passwordAfter successful authentication, you may see:
mysql>mysql>This indicates that you are inside the MySQL command-line client.
11. Creating a Database
To create a database:
CREATE DATABASE database_name;CREATE DATABASE database_name;For example:
CREATE DATABASE library;CREATE DATABASE library;This creates a database called library.
12. Listing Databases
To display available databases:
SHOW DATABASES;SHOW DATABASES;Example output might include:
information_schema
library
mysql
performance_schemainformation_schema
library
mysql
performance_schema13. Selecting a Database
Before working with tables inside a database, select it:
USE database_name;USE database_name;Example:
USE library;USE library;You are now working with the library database.
14. Deleting a Database
A database can be permanently removed using:
DROP DATABASE database_name;DROP DATABASE database_name;Example:
DROP DATABASE library;DROP DATABASE library;Important: DROP DATABASE permanently removes the database and its contents.
15. Creating a Table
A table can be created with:
CREATE TABLE table_name (
column_name data_type
);CREATE TABLE table_name (
column_name data_type
);For example:
CREATE TABLE books (
id INT,
name VARCHAR(100),
price DECIMAL(10,2)
);CREATE TABLE books (
id INT,
name VARCHAR(100),
price DECIMAL(10,2)
);This creates a table containing:
id
name
priceid
name
price16. Showing Tables
To list the tables in the currently selected database:
SHOW TABLES;SHOW TABLES;17. Describing a Table
To inspect the structure of a table:
DESCRIBE books;DESCRIBE books;or:
DESC books;DESC books;This can show information such as:
- Column names
- Data types
- Keys
- Whether NULL values are allowed
- Default values
- Additional attributes
This is an important command when performing database enumeration.
18. Altering a Table
The ALTER TABLE statement modifies an existing table.
For example:
ALTER TABLE books
ADD description TEXT;ALTER TABLE books
ADD description TEXT;This adds a new description column.
19. Dropping a Table
To permanently delete a table:
DROP TABLE books;DROP TABLE books;This removes the table and its data.
20. CRUD Operations
CRUD represents the four fundamental operations performed on data.
C = Create
R = Read
U = Update
D = DeleteC = Create
R = Read
U = Update
D = DeleteThese correspond to:
INSERT
SELECT
UPDATE
DELETEINSERT
SELECT
UPDATE
DELETE21. CREATE โ INSERT
The INSERT statement adds a new record.
Example:
INSERT INTO books
(id, name)
VALUES (1, 'Book A');INSERT INTO books
(id, name)
VALUES (1, 'Book A');Multiple values can also be inserted:
INSERT INTO books (id, name)
VALUES
(1, 'Book A'),
(2, 'Book B'),
(3, 'Book C');INSERT INTO books (id, name)
VALUES
(1, 'Book A'),
(2, 'Book B'),
(3, 'Book C');22. READ โ SELECT
The SELECT statement retrieves data.
To retrieve everything:
SELECT * FROM books;SELECT * FROM books;The * means all columns.
To retrieve specific columns:
SELECT name, price
FROM books;SELECT name, price
FROM books;This returns only the selected columns.
23. UPDATE
The UPDATE statement modifies existing records.
Example:
UPDATE books
SET name = 'New Book'
WHERE id = 1;UPDATE books
SET name = 'New Book'
WHERE id = 1;The WHERE clause is important because it specifies which record should be modified.
Without an appropriate WHERE condition, an UPDATE statement can affect multiple or all rows.
24. DELETE
The DELETE statement removes records.
Example:
DELETE FROM books
WHERE id = 1;DELETE FROM books
WHERE id = 1;Again, the WHERE clause determines which rows are deleted.
Without a WHERE clause:
DELETE FROM books;DELETE FROM books;all rows in the table may be deleted.
25. WHERE Clause
The WHERE clause filters records based on a condition.
Example:
SELECT *
FROM books
WHERE id = 1;SELECT *
FROM books
WHERE id = 1;This returns only records where the id is 1.
WHERE is one of the most important SQL concepts because it controls which records are affected by a query.
26. Comparison Operators
SQL provides operators for comparing values.
OperatorMeaning=Equal!=Not equal<>Not equalGreater than<=Less than or equal>=Greater than or equal
Examples:
SELECT * FROM books WHERE price > 20;
SELECT * FROM books WHERE id != 5;SELECT * FROM books WHERE price > 20;
SELECT * FROM books WHERE id != 5;27. AND
AND requires all conditions to be true.
Example:
SELECT *
FROM books
WHERE price > 10 AND price < 50;SELECT *
FROM books
WHERE price > 10 AND price < 50;Both conditions must be satisfied.
28. OR
OR requires at least one condition to be true.
Example:
SELECT *
FROM books
WHERE id = 1 OR id = 2;SELECT *
FROM books
WHERE id = 1 OR id = 2;The result can contain records matching either condition.
29. NOT
NOT reverses a condition.
Example:
SELECT *
FROM books
WHERE NOT id = 1;SELECT *
FROM books
WHERE NOT id = 1;This selects records where the condition is not true.
30. BETWEEN
BETWEEN checks whether a value falls within a range.
Example:
SELECT *
FROM books
WHERE id BETWEEN 2 AND 4;SELECT *
FROM books
WHERE id BETWEEN 2 AND 4;This selects values within the specified range.
31. LIKE
LIKE is used for pattern matching.
For example:
SELECT *
FROM books
WHERE name LIKE '%book%';SELECT *
FROM books
WHERE name LIKE '%book%';The % wildcard represents zero or more characters.
Common patterns include:
LIKE 'book%'LIKE 'book%'Starts with book.
LIKE '%book'LIKE '%book'Ends with book.
LIKE '%book%'LIKE '%book%'Contains book.
32. DISTINCT
DISTINCT removes duplicate values from the result.
Example:
SELECT DISTINCT author
FROM books;SELECT DISTINCT author
FROM books;Instead of showing the same author multiple times, SQL returns each unique value once.
33. ORDER BY
ORDER BY sorts query results.
Example:
SELECT *
FROM books
ORDER BY price;SELECT *
FROM books
ORDER BY price;By default, the results are generally sorted in ascending order.
For descending order:
SELECT *
FROM books
ORDER BY price DESC;SELECT *
FROM books
ORDER BY price DESC;For ascending order:
SELECT *
FROM books
ORDER BY price ASC;SELECT *
FROM books
ORDER BY price ASC;34. LIMIT
LIMIT restricts the number of rows returned.
Example:
SELECT *
FROM books
LIMIT 5;SELECT *
FROM books
LIMIT 5;This returns up to five rows.
It is useful when working with large datasets.
35. NULL Values
NULL represents a missing or unknown value.
It is important to understand that:
NULL โ 0
NULL โ ''
NULL โ FALSENULL โ 0
NULL โ ''
NULL โ FALSETo check for NULL values, use:
SELECT *
FROM users
WHERE email IS NULL;SELECT *
FROM users
WHERE email IS NULL;To find values that are not NULL:
SELECT *
FROM users
WHERE email IS NOT NULL;SELECT *
FROM users
WHERE email IS NOT NULL;You should not normally use:
WHERE email = NULLWHERE email = NULLbecause NULL requires IS NULL or IS NOT NULL.
36. String Functions
SQL provides functions for manipulating text.
CONCAT()
Combines strings.
SELECT CONCAT(first_name, ' ', last_name)
FROM users;SELECT CONCAT(first_name, ' ', last_name)
FROM users;GROUP_CONCAT()
Combines values from multiple rows into a single string in database systems that support it, such as MySQL.
Example:
SELECT GROUP_CONCAT(name)
FROM users;SELECT GROUP_CONCAT(name)
FROM users;SUBSTRING()
Extracts part of a string.
Example:
SELECT SUBSTRING('Cybersecurity', 1, 4);SELECT SUBSTRING('Cybersecurity', 1, 4);This extracts a portion of the string.
LENGTH()
Returns the length of a string.
Example:
SELECT LENGTH('Cybersecurity');SELECT LENGTH('Cybersecurity');37. Aggregate Functions
Aggregate functions perform calculations across multiple rows.
COUNT()
Counts rows.
SELECT COUNT(*)
FROM users;SELECT COUNT(*)
FROM users;SUM()
Adds numeric values.
SELECT SUM(price)
FROM products;SELECT SUM(price)
FROM products;MAX()
Returns the largest value.
SELECT MAX(price)
FROM products;SELECT MAX(price)
FROM products;MIN()
Returns the smallest value.
SELECT MIN(price)
FROM products;SELECT MIN(price)
FROM products;Another important aggregate function is:
AVG()
Calculates the average value.
SELECT AVG(price)
FROM products;SELECT AVG(price)
FROM products;38. GROUP BY
GROUP BY groups rows that have the same value.
For example:
SELECT author, COUNT(*)
FROM books
GROUP BY author;SELECT author, COUNT(*)
FROM books
GROUP BY author;This can show how many books belong to each author.
A simplified result could look like:
Author | COUNT
----------------
Alice | 3
Bob | 5
Charlie | 2Author | COUNT
----------------
Alice | 3
Bob | 5
Charlie | 239. HAVING
HAVING filters grouped results.
Example:
SELECT author, COUNT(*)
FROM books
GROUP BY author
HAVING COUNT(*) > 2;SELECT author, COUNT(*)
FROM books
GROUP BY author
HAVING COUNT(*) > 2;The important difference is:
WHERE
โ Filters rows before grouping
HAVING
โ Filters groups after groupingWHERE
โ Filters rows before grouping
HAVING
โ Filters groups after grouping40. SQL JOINs
Relational databases are designed around relationships between tables.
JOIN statements allow data from multiple tables to be combined.
Suppose we have:
Users
----------------
id
nameUsers
----------------
id
nameand:
Orders
----------------
id
user_id
productOrders
----------------
id
user_id
productThe user_id can reference Users.id.
INNER JOIN
Returns matching records from both tables.
SELECT users.name, orders.product
FROM users
INNER JOIN orders
ON users.id = orders.user_id;SELECT users.name, orders.product
FROM users
INNER JOIN orders
ON users.id = orders.user_id;LEFT JOIN
Returns all rows from the left table and matching rows from the right table.
SELECT users.name, orders.product
FROM users
LEFT JOIN orders
ON users.id = orders.user_id;SELECT users.name, orders.product
FROM users
LEFT JOIN orders
ON users.id = orders.user_id;If a user has no matching order, the order fields can contain NULL.
41. SQL Comments
SQL supports comments, although the exact syntax can vary between database systems.
A common single-line comment syntax is:
-- comment-- commentMySQL also supports:
# comment# commentComments are useful when documenting SQL queries and understanding existing database code.
42. SQL Statement Structure
A basic SQL query often follows this structure:
SELECT column
FROM table
WHERE condition
ORDER BY column
LIMIT number;SELECT column
FROM table
WHERE condition
ORDER BY column
LIMIT number;For example:
SELECT name, price
FROM books
WHERE price > 20
ORDER BY price DESC
LIMIT 5;SELECT name, price
FROM books
WHERE price > 20
ORDER BY price DESC
LIMIT 5;This query:
- Selects
nameandprice - From the
bookstable - Keeps books costing more than 20
- Sorts them from highest to lowest price
- Returns up to five results
43. SQL Fundamentals for Web Security
Understanding SQL is especially important for web penetration testing.
A typical web application might receive input:
/search?id=10/search?id=10The application could use that value in a database query.
Conceptually:
HTTP Request
โ
Web Application
โ
SQL Query
โ
Database
โ
ResponseHTTP Request
โ
Web Application
โ
SQL Query
โ
Database
โ
ResponseIf developers incorrectly construct SQL queries using untrusted input, SQL Injection may become possible.
This is why learning SQL fundamentals should come before learning tools such as SQLMap.
You should understand what the application is doing before trying to automate the testing.
44. SQL vs SQL Injection
These concepts should not be confused.
SQL
A legitimate language used to communicate with relational databases.
SQL Injection
A security vulnerability that occurs when untrusted input can improperly influence an SQL query.
SQL
โ Language
SQL Injection
โ VulnerabilitySQL
โ Language
SQL Injection
โ VulnerabilitySQL itself is not a vulnerability.
45. Quick SQL Cheat Sheet
Database
CREATE DATABASE database_name;
SHOW DATABASES;
USE database_name;
DROP DATABASE database_name;CREATE DATABASE database_name;
SHOW DATABASES;
USE database_name;
DROP DATABASE database_name;Tables
CREATE TABLE table_name (...);
SHOW TABLES;
DESCRIBE table_name;
ALTER TABLE table_name ...;
DROP TABLE table_name;CREATE TABLE table_name (...);
SHOW TABLES;
DESCRIBE table_name;
ALTER TABLE table_name ...;
DROP TABLE table_name;CRUD
INSERT INTO table_name (...) VALUES (...);
SELECT * FROM table_name;
UPDATE table_name
SET column = value
WHERE condition;
DELETE FROM table_name
WHERE condition;INSERT INTO table_name (...) VALUES (...);
SELECT * FROM table_name;
UPDATE table_name
SET column = value
WHERE condition;
DELETE FROM table_name
WHERE condition;Filtering
WHERE
AND
OR
NOT
LIKE
BETWEENWHERE
AND
OR
NOT
LIKE
BETWEENSorting and Limiting
ORDER BY
LIMIT
DISTINCTORDER BY
LIMIT
DISTINCTAggregation
COUNT()
SUM()
AVG()
MAX()
MIN()
GROUP BY
HAVINGCOUNT()
SUM()
AVG()
MAX()
MIN()
GROUP BY
HAVINGRelationships
JOIN
INNER JOIN
LEFT JOINJOIN
INNER JOIN
LEFT JOINString Functions
CONCAT()
GROUP_CONCAT()
SUBSTRING()
LENGTH()CONCAT()
GROUP_CONCAT()
SUBSTRING()
LENGTH()Key Takeaways
After studying SQL Fundamentals, I should be able to explain:
- What a database is
- The difference between SQL and NoSQL
- What a relational database is
- What tables, rows, and columns represent
- What primary and foreign keys are
- What SQL is
- What a DBMS is
- How to connect to MySQL
- How to create and manage databases
- How to create and manage tables
- How CRUD operations work
- How
SELECTretrieves data - How
WHEREfilters data - How comparison and logical operators work
- How
LIKEperforms pattern matching - How
ORDER BYsorts results - How
LIMITrestricts results - How
DISTINCTremoves duplicates - How
NULLvalues work - How aggregate functions work
- How
GROUP BYandHAVINGwork - How tables can be connected using JOINs
- Why SQL knowledge is important for web penetration testing
Conclusion
SQL fundamentals are an important foundation for anyone interested in web security and penetration testing.
Before studying SQL Injection or using automated tools such as SQLMap, it is important to understand how databases are structured and how SQL queries work.
The core concepts to remember are:
Database
โ Stores data
DBMS
โ Manages the database
SQL
โ Communicates with relational databases
Table
โ Stores related records
Row
โ Represents one record
Column
โ Represents an attribute
Primary Key
โ Uniquely identifies a record
Foreign Key
โ Creates a relationship between tables
CRUD
โ Create, Read, Update, DeleteDatabase
โ Stores data
DBMS
โ Manages the database
SQL
โ Communicates with relational databases
Table
โ Stores related records
Row
โ Represents one record
Column
โ Represents an attribute
Primary Key
โ Uniquely identifies a record
Foreign Key
โ Creates a relationship between tables
CRUD
โ Create, Read, Update, DeleteOnce these fundamentals are clear, topics such as SQL Injection, SQLMap, database enumeration, and web application security become much easier to understand.
Main lesson: don't memorize SQL commands only. Understand what each query does to the database and why the result changes depending on the conditions you provide.