SQL commands are the instructions you send to a database. They create tables, add and change rows, read data, control access and save or cancel work. This page lists the main commands by type, gives the syntax of each one and shows a small example you can run.
All examples use the same two tables, so run them from top to bottom. If you are new to databases, read what SQL is first.
Types of SQL commands in DBMS
A DBMS (database management system) such as MySQL, PostgreSQL or Oracle accepts SQL commands. Textbooks group SQL commands in DBMS into five types by the job they do.
| Type | Full form | Job | Commands |
|---|---|---|---|
| DDL | Data Definition Language | Build or change the structure | CREATE, ALTER, DROP, TRUNCATE, RENAME |
| DML | Data Manipulation Language | Change the rows | INSERT, UPDATE, DELETE |
| DQL | Data Query Language | Read the rows | SELECT |
| DCL | Data Control Language | Control access | GRANT, REVOKE |
| TCL | Transaction Control Language | Save or cancel a group of changes | COMMIT, ROLLBACK, SAVEPOINT |
Some books put SELECT inside DML and list only four SQL commands types. Both answers are accepted. Say which grouping you follow.
Interview note: Be ready to say why TRUNCATE is DDL even though it removes rows. It empties the whole table as a structure-level operation and takes no WHERE clause. The full comparison is in TRUNCATE vs DELETE.
DDL commands with syntax and examples
DDL commands work on the table itself, not on single rows.
CREATE TABLE
Syntax: CREATE TABLE table_name (column type constraint, ...);
CREATE TABLE departments (
dept_id INTEGER PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE students (
student_id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
city VARCHAR(50),
marks INTEGER CHECK (marks BETWEEN 0 AND 100),
dept_id INTEGER REFERENCES departments(dept_id)
);
This makes two empty tables. dept_id in students points to a row in departments. Data types and constraints are covered in CREATE TABLE in SQL.
One warning for MySQL users: MySQL ignored this short inline REFERENCES form for many years. In MySQL, write the key on its own line as FOREIGN KEY (dept_id) REFERENCES departments(dept_id).
ALTER TABLE
Syntax: ALTER TABLE table_name ADD COLUMN column type;
ALTER TABLE students ADD COLUMN email VARCHAR(100);
Old rows get NULL in the new column, unless you give a default. ALTER can also rename or drop a column. The exact wording changes between databases.
TRUNCATE, DROP and RENAME
These three are short, and two of them destroy data.
| Command | Syntax | Result |
|---|---|---|
| TRUNCATE | TRUNCATE TABLE students; |
Removes every row, keeps the table |
| DROP | DROP TABLE students; |
Removes the rows and the table |
| RENAME | ALTER TABLE students RENAME TO learners; |
Changes the table name |
SQLite has no TRUNCATE command. Use DELETE FROM students; there. SQL Server renames tables with the sp_rename procedure instead of ALTER.
Common mistake: Running DROP or TRUNCATE to "undo" a bad insert. In MySQL and Oracle these commands commit on their own, so ROLLBACK will not bring the data back. Take a backup first.
DML commands with examples
DML commands change the rows inside a table.
INSERT
Syntax: INSERT INTO table_name (columns) VALUES (values);
INSERT INTO departments (dept_id, dept_name)
VALUES (1, 'CSE'), (2, 'ECE'), (3, 'Civil');
INSERT INTO students (student_id, name, city, marks, dept_id)
VALUES (1, 'Asha', 'Hyderabad', 82, 1),
(2, 'Ravi', 'Warangal', 67, 2),
(3, 'Meena', 'Hyderabad', 91, 1),
(4, 'Kiran', 'Vijayawada', 45, 2);
Always name the columns. Your insert then keeps working when someone adds a column later.
UPDATE
Syntax: UPDATE table_name SET column = value WHERE condition;
UPDATE students SET marks = 50 WHERE student_id = 4;
Only Kiran's row changes, from 45 to 50. Without WHERE, every student would get 50 marks.
DELETE
Syntax: DELETE FROM table_name WHERE condition;
DELETE FROM departments WHERE dept_name = 'Civil';
One row goes. The table and the other rows stay.
Pro tip: Before an UPDATE or DELETE, run a SELECT with the same WHERE. If it shows the rows you expect, change only the first line of the command.
The SELECT command and its clauses
SELECT is the command you will use most. Syntax in writing order:
SELECT columns
FROM table
JOIN other_table ON condition
WHERE row_condition
GROUP BY columns
HAVING group_condition
ORDER BY columns
LIMIT n;
Here is a filter with sorting:
SELECT name, marks
FROM students
WHERE city = 'Hyderabad'
ORDER BY marks DESC;
| name | marks |
|---|---|
| Meena | 91 |
| Asha | 82 |
Next, a join with grouping:
SELECT d.dept_name,
COUNT(*) AS total,
AVG(s.marks) AS avg_marks
FROM students s
JOIN departments d ON d.dept_id = s.dept_id
GROUP BY d.dept_name
HAVING AVG(s.marks) > 55
ORDER BY avg_marks DESC;
| dept_name | total | avg_marks |
|---|---|---|
| CSE | 2 | 86.5 |
| ECE | 2 | 58.5 |
WHERE filters rows before grouping. HAVING filters the groups after it. For more, see the SELECT statement, SQL joins and GROUP BY.
SQL Server uses SELECT TOP 5 ... and Oracle uses FETCH FIRST 5 ROWS ONLY where MySQL, PostgreSQL and SQLite use LIMIT 5.
TCL and DCL commands
COMMIT, ROLLBACK and SAVEPOINT
A transaction is a group of changes that must succeed or fail together.
| Command | Meaning |
|---|---|
BEGIN; or START TRANSACTION; |
Start the group |
SAVEPOINT name; |
Mark a point you can return to |
ROLLBACK TO name; |
Undo the work done after that point |
ROLLBACK; |
Undo the whole group |
COMMIT; |
Save the group for good |
BEGIN;
UPDATE students SET marks = marks + 5 WHERE student_id = 2;
SAVEPOINT after_ravi;
UPDATE students SET marks = 0;
ROLLBACK TO after_ravi;
COMMIT;
SELECT name, marks FROM students ORDER BY student_id;
| name | marks |
|---|---|
| Asha | 82 |
| Ravi | 72 |
| Meena | 91 |
| Kiran | 50 |
The dangerous "set everyone to 0" update was cancelled. Ravi's 5 extra marks were kept. The article on SQL transactions explains why this matters in banking and orders.
GRANT and REVOKE
These commands decide who can do what. They work in server databases such as MySQL, PostgreSQL and Oracle. SQLite has no users, so it does not support them.
GRANT SELECT, INSERT ON students TO report_user;
REVOKE INSERT ON students FROM report_user;
After both lines, report_user can still read the table but cannot add rows.
SQL commands practice online
You can do SQL commands practice online in any browser-based SQL editor. Pick one that lets you create your own tables, because read-only editors cover SELECT but not DDL or DML. SQLite playgrounds run most of this page. To practise GRANT and TRUNCATE, install MySQL or PostgreSQL on your laptop.
Try these in order, using the tables above:
- Add a student called Divya from Guntur with 76 marks in ECE.
- List every student with more than 70 marks, highest first.
- Show the department name beside each student name.
- Count how many students come from each city.
- Add a
phonecolumn, then fill it for one student. - Start a transaction, delete all students, then roll it back.
Answers to the first four:
INSERT INTO students (student_id, name, city, marks, dept_id)
VALUES (5, 'Divya', 'Guntur', 76, 2);
SELECT name, marks FROM students WHERE marks > 70 ORDER BY marks DESC;
SELECT s.name, d.dept_name
FROM students s
JOIN departments d ON d.dept_id = s.dept_id;
SELECT city, COUNT(*) AS total FROM students GROUP BY city;
Query 2 returns Meena 91, Asha 82, Divya 76 and Ravi 72.
Quick reference: which SQL command do you need?
| You want to | Use |
|---|---|
| Make a table | CREATE TABLE |
| Add a column | ALTER TABLE ... ADD |
| Add rows | INSERT INTO |
| Change values in rows | UPDATE ... SET ... WHERE |
| Remove some rows | DELETE FROM ... WHERE |
| Remove all rows fast | TRUNCATE TABLE |
| Remove the table | DROP TABLE |
| Read data | SELECT ... FROM |
| Save a group of changes | COMMIT |
| Cancel a group of changes | ROLLBACK |
| Give access | GRANT |
| Take access away | REVOKE |
Learn SELECT, INSERT, UPDATE and DELETE first, since they cover daily work. Learn CREATE TABLE and transactions next. When you can write the practice answers without looking, test yourself with SQL interview questions. To learn SQL along with Excel and Power BI, see the data analytics course at CodeBegun, taught in a Madhapur classroom and live online.
Frequently Asked Questions
What are the five types of SQL commands?
Are SQL commands case sensitive?
Is TRUNCATE a DDL or DML command?
Do SQL commands work the same in every database?
Can I practise SQL commands without installing anything?
Want to Build Your Career in Data Analytics with AI?
Join CodeBegun and train with working industry engineers — Explore the Data Analytics program

