CRUD — Create, Read, Update, Delete — is what almost every database operation boils down to. This chapter is the practical core: putting rows in, getting the right rows out, and changing or removing them safely.
INSERT — create rows
INSERT INTO students (name, email, package)VALUES ('Aanya', 'aanya@example.com', 6.50);-- Several rows at onceINSERT INTO students (name, package) VALUES('Rohan', 8.00),('Priya', 7.25);
SELECT & WHERE — read rows
SELECT chooses columns; WHERE filters rows. List only the columns you need — SELECT * is convenient but wasteful in real queries:
SELECT name, package FROM studentsWHERE package > 6AND enrolled >= '2026-01-01';-- Useful WHERE operatorsWHERE name LIKE 'A%' -- starts with AWHERE package BETWEEN 5 AND 8 -- inclusive rangeWHERE status IN ('placed','offer') -- any of a listWHERE email IS NULL; -- missing value
Sorting & paging
SELECT name, package FROM studentsORDER BY package DESC -- highest firstLIMIT 10; -- top 10-- Pagination: page 3 of 10-row pagesSELECT * FROM students ORDER BY id LIMIT 10 OFFSET 20;
GROUP BY — summarise
Aggregate rows into groups and compute per-group values. WHERE filters rows before grouping; HAVING filters the groups after:
SELECT programme, COUNT(*) AS students, AVG(package) AS avg_pkgFROM studentsWHERE package > 0GROUP BY programmeHAVING students >= 5ORDER BY avg_pkg DESC;
UPDATE & DELETE — change & remove
UPDATE students SET package = 9.0 WHERE id = 1;DELETE FROM students WHERE enrolled IS NULL;
Practice
Insert 8 students, then write queries for: the top 3 by package, the count of students per programme, all students enrolled this year, and an update that gives everyone in one programme a raise. Test each risky change with a matching SELECT first.
Key takeaways
- 01INSERT adds rows (single or multiple); SELECT…WHERE reads and filters them.
- 02Filter with LIKE, BETWEEN, IN and IS NULL; select only the columns you need.
- 03ORDER BY sorts; LIMIT/OFFSET paginate.
- 04GROUP BY summarises into groups; WHERE filters rows, HAVING filters the resulting groups.
- 05Always include a WHERE on UPDATE/DELETE — preview it as a SELECT first.
