Free mock test
SQL

SQL

Databases and Programming · 3 min read

SQL (Structured Query Language) is the standard language of relational databases. It is not case-sensitive for keywords. A statement ends with a semicolon. Examples below use a table Students(Name, District, Marks, Fee) holding the same eight rows as the Excel sample sheet.

Categories of SQL commands

CategoryFull formCommands
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATE, RENAME
DMLData Manipulation LanguageINSERT, UPDATE, DELETE (and SELECT, sometimes called DQL)
DCLData Control LanguageGRANT, REVOKE
TCLTransaction Control LanguageCOMMIT, ROLLBACK, SAVEPOINT

Core statements

TaskStatement
Create a tableCREATE TABLE Students (Name VARCHAR(30) PRIMARY KEY, District VARCHAR(20), Marks INT, Fee INT);
Add a rowINSERT INTO Students VALUES ('Aamir','Srinagar',78,2500);
Read allSELECT * FROM Students;
FilterSELECT Name FROM Students WHERE Marks >= 80; → Charu, Eram, Hina
SortSELECT * FROM Students ORDER BY Marks DESC;
Unique valuesSELECT DISTINCT District FROM Students; → 4 rows
Change dataUPDATE Students SET Marks = 45 WHERE Name = 'Farhan';
Remove rowsDELETE FROM Students WHERE Marks < 40;
Add a columnALTER TABLE Students ADD Age INT;
Delete the tableDROP TABLE Students;

Conditions

OperatorExample
AND, OR, NOTWHERE District = 'Jammu' AND Marks > 70 → Charu, Gurpreet
BETWEENWHERE Marks BETWEEN 60 AND 80 (both ends included)
INWHERE District IN ('Jammu','Srinagar')
LIKEWHERE Name LIKE 'A%'; % = any number of characters, _ = exactly one
IS NULLWHERE Fee IS NULL (never use = NULL)

Aggregates and grouping

Aggregate functions: COUNT, SUM, AVG, MAX, MIN. COUNT(*) counts all rows; COUNT(column) skips NULLs.

StatementResult
SELECT COUNT(*) FROM Students WHERE District = 'Jammu';3
SELECT AVG(Marks) FROM Students;71.5
SELECT District, AVG(Marks) FROM Students GROUP BY District;Anantnag 69, Baramulla 40, Jammu 76, Srinagar 83
SELECT District, AVG(Marks) FROM Students GROUP BY District HAVING AVG(Marks) > 70;Jammu 76, Srinagar 83

WHERE filters rows before grouping; HAVING filters groups after grouping. Aggregate functions cannot be used in WHERE.

Joins

A join combines rows from two tables on a matching column.

JoinReturns
INNER JOINOnly rows that match in both tables.
LEFT JOINAll rows of the left table, plus matches from the right (NULL where none).
RIGHT JOINAll rows of the right table, plus matches from the left.
FULL JOINAll rows from both.
CROSS JOINEvery row with every row (Cartesian product).
Self joinA table joined with itself.

Example: SELECT s.Name, c.CourseName FROM Student s INNER JOIN Course c ON s.CourseID = c.CourseID;

Constraints

PRIMARY KEY (unique, not null), FOREIGN KEY, UNIQUE, NOT NULL, CHECK (a condition), DEFAULT (a starting value).

Important differences

DELETETRUNCATEDROP
DML. Removes chosen rows (with WHERE). Can be rolled back.DDL. Removes all rows, keeps the structure. Faster.DDL. Removes the whole table, structure and data.

Order in which a query is processed: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.

Keep practising

Notes are only half the work. Test yourself with questions and discuss doubts with other aspirants.

Prepared for exam revision from standard computer textbooks, Microsoft documentation and official sources. Features differ between Excel and Word versions (2016, 2019, 2021, 365) so check which version your exam follows. Found a mistake? Tell us.