Skip to main content
All tracks
DBMS

SQL Basics

SQL (Structured Query Language) is the standard language for relational databases.

Categories​

CategoryCommands
DDL (definition)CREATE, ALTER, DROP, TRUNCATE
DML (manipulation)INSERT, UPDATE, DELETE
DQL (query)SELECT
DCL (control)GRANT, REVOKE
TCL (transaction)COMMIT, ROLLBACK, SAVEPOINT

Creating and changing data​

CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
marks INT CHECK (marks BETWEEN 0 AND 100),
city VARCHAR(30)
);

INSERT INTO student (id, name, marks, city)
VALUES (1, 'Asha', 91, 'Pune'), (2, 'Ravi', 74, 'Delhi');

UPDATE student SET marks = marks + 5 WHERE id = 2;
DELETE FROM student WHERE marks < 40;

Querying​

SELECT name, marks
FROM student
WHERE city = 'Pune' AND marks >= 60
ORDER BY marks DESC
LIMIT 10;

Logical order of evaluation: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.

Useful filters​

SELECT * FROM student WHERE name LIKE 'A%';
SELECT * FROM student WHERE city IN ('Pune', 'Delhi');
SELECT * FROM student WHERE marks BETWEEN 60 AND 90;
SELECT * FROM student WHERE city IS NULL; -- not "= NULL"
CAUTION

NULL means "unknown". Comparisons with NULL (= NULL, <> NULL) are never true; use IS NULL / IS NOT NULL.

DELETE vs TRUNCATE vs DROP​

RemovesCan use WHERERollback (typical)
DELETERowsYesYes
TRUNCATEAll rowsNoDepends on the DBMS
DROPThe whole tableNoNo
Finished reading?

Mark it complete to track your progress across the docs.

Questions and discussion

Stuck on something here, or found a clearer way to explain it? Start a thread.

All discussions
  • Ask a question or share what helped
  • React to useful answers
  • Sign in with GitHub to post

Comments are stored in GitHub Discussions and shown here by giscus. PrepVerse never sees your GitHub password.