SQLBasicsbeginner

SQL Commands with Syntax and Examples

The main SQL commands by type, each with its syntax, a small example and the real result.

Published
TL;DR – Quick Answer

SQL commands are instructions that create, change, read and protect data in a database. They fall into five types: DDL, DML, DQL, DCL and TCL. The ones you use most are SELECT, INSERT, UPDATE, DELETE and CREATE TABLE.

On This Page

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:

  1. Add a student called Divya from Guntur with 76 marks in ECE.
  2. List every student with more than 70 marks, highest first.
  3. Show the department name beside each student name.
  4. Count how many students come from each city.
  5. Add a phone column, then fill it for one student.
  6. 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?
They are DDL for structure, DML for changing rows, DQL for reading rows, DCL for access and TCL for transactions. Some books place SELECT in DML and list four types.
Are SQL commands case sensitive?
Keywords such as SELECT and select work the same. Table names and stored text can be case sensitive, depending on the database and its settings.
Is TRUNCATE a DDL or DML command?
TRUNCATE is usually listed as DDL. It empties the whole table and takes no WHERE clause. In MySQL and Oracle it also commits by itself.
Do SQL commands work the same in every database?
The basic SELECT, INSERT, UPDATE and DELETE forms are nearly the same everywhere. Row limits, date functions, ALTER options and identity columns often differ.
Can I practise SQL commands without installing anything?
Yes, for most of them. A browser-based SQLite editor runs most of this page. Install MySQL or PostgreSQL to try TRUNCATE, GRANT and REVOKE.

Want to Build Your Career in Data Analytics with AI?

Join CodeBegun and train with working industry engineers — Explore the Data Analytics program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 3 December 2026 LinkedIn
Chat with us