SQL
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
| Category | Full form | Commands |
|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE, RENAME |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE (and SELECT, sometimes called DQL) |
| DCL | Data Control Language | GRANT, REVOKE |
| TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT |
Core statements
| Task | Statement |
|---|---|
| Create a table | CREATE TABLE Students (Name VARCHAR(30) PRIMARY KEY, District VARCHAR(20), Marks INT, Fee INT); |
| Add a row | INSERT INTO Students VALUES ('Aamir','Srinagar',78,2500); |
| Read all | SELECT * FROM Students; |
| Filter | SELECT Name FROM Students WHERE Marks >= 80; → Charu, Eram, Hina |
| Sort | SELECT * FROM Students ORDER BY Marks DESC; |
| Unique values | SELECT DISTINCT District FROM Students; → 4 rows |
| Change data | UPDATE Students SET Marks = 45 WHERE Name = 'Farhan'; |
| Remove rows | DELETE FROM Students WHERE Marks < 40; |
| Add a column | ALTER TABLE Students ADD Age INT; |
| Delete the table | DROP TABLE Students; |
Conditions
| Operator | Example |
|---|---|
AND, OR, NOT | WHERE District = 'Jammu' AND Marks > 70 → Charu, Gurpreet |
BETWEEN | WHERE Marks BETWEEN 60 AND 80 (both ends included) |
IN | WHERE District IN ('Jammu','Srinagar') |
LIKE | WHERE Name LIKE 'A%'; % = any number of characters, _ = exactly one |
IS NULL | WHERE Fee IS NULL (never use = NULL) |
Aggregates and grouping
Aggregate functions: COUNT, SUM, AVG, MAX, MIN. COUNT(*) counts all rows; COUNT(column) skips NULLs.
| Statement | Result |
|---|---|
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.
| Join | Returns |
|---|---|
| INNER JOIN | Only rows that match in both tables. |
| LEFT JOIN | All rows of the left table, plus matches from the right (NULL where none). |
| RIGHT JOIN | All rows of the right table, plus matches from the left. |
| FULL JOIN | All rows from both. |
| CROSS JOIN | Every row with every row (Cartesian product). |
| Self join | A 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
| DELETE | TRUNCATE | DROP |
|---|---|---|
| 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.