Basic SQL Commands (CRUD Operations)
Now that you understand:
- How to create databases and tables
- Data types
- Constraints
It’s time to learn how to work with data inside tables.
This is where SQL becomes powerful.
1. What is SQL?
SQL (Structured Query Language) is a language used to communicate with relational databases.
In MySQL, SQL is used to:
- Create data
- Read data
- Update data
- Delete data
These four operations are called CRUD operations:
| Letter | Meaning | SQL Command |
| C | Create | INSERT |
| R | Read | SELECT |
| U | Update | UPDATE |
| D | Delete | DELETE |
These are the foundations of database programming.
2. SELECT – Reading Data
The SELECT statement is used to retrieve data from a table.
Basic Syntax
SELECT column_name FROM table_name;
Example
SELECT name FROM Students;
This retrieves only the name column.
Selecting All Columns
SELECT * FROM Students;
* means all columns.
3. INSERT – Adding Data
The INSERT statement is used to add new records.
Syntax
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Example
INSERT INTO Students (id, name, age)
VALUES (1, 'Rahul', 20);
Inserting Multiple Rows
INSERT INTO Students (id, name, age)
VALUES
(2, 'Anita', 21),
(3, 'Aman', 19);
Important:
Always match column order with values.
4. UPDATE – Modifying Data
The UPDATE statement changes existing data.
Syntax
UPDATE table_name
SET column_name = value
WHERE condition;
Example
UPDATE Students
SET age = 22
WHERE id = 1;
This updates only the student with id = 1.
⚠ Important:
If you forget the WHERE clause:
UPDATE Students SET age = 22;
This updates all rows.
Always use WHERE carefully.
5. DELETE – Removing Data
The DELETE statement removes records.
Syntax
DELETE FROM table_name
WHERE condition;
Example
DELETE FROM Students
WHERE id = 3;
This deletes only the student with id = 3.
⚠ Warning:
If you write:
DELETE FROM Students;
All rows will be deleted.
6. WHERE Clause – Filtering Data
The WHERE clause is used to filter records.
Example
SELECT * FROM Students
WHERE age > 20;
This shows students older than 20.
Common Operators
| Operator | Meaning |
| = | Equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| != | Not equal |
Example:
SELECT * FROM Students
WHERE name = 'Rahul';
WHERE makes queries powerful because it allows condition-based filtering.
7. LIMIT & OFFSET
These are used to control how many rows are returned.
LIMIT
SELECT * FROM Students
LIMIT 5;
This returns only 5 rows.
OFFSET
SELECT * FROM Students
LIMIT 5 OFFSET 5;
This skips the first 5 rows and returns the next 5.
Used for:
- Pagination
- Large datasets
- Displaying limited results
8. Writing Readable SQL Queries
Good SQL is not just correct — it should be readable.
1. Use Proper Formatting
Bad:
select * from students where age>20;
Good:
SELECT *
FROM Students
WHERE age > 20;
2. Use Meaningful Column Names
Avoid:
- col1
- data1
Use:
- student_name
- date_of_birth
3. Write Keywords in Uppercase
Common convention:
- SELECT
- INSERT
- UPDATE
- DELETE
- WHERE
- LIMIT
This improves clarity.
4. Add Comments
-- Get students older than 20
SELECT * FROM Students
WHERE age > 20;
Readable SQL is easier to debug and maintain.
Final Summary
Basic SQL commands allow you to perform CRUD operations:
- SELECT → Read data
- INSERT → Add data
- UPDATE → Modify data
- DELETE → Remove data
The WHERE clause filters data.
LIMIT & OFFSET control the number of results.