Every clause you need for the IEB Data & Information Management section, explained with runnable examples.
Every query starts with SELECT (which columns) and FROM (which table). * means every column.
SELECT * FROM Students;
SELECT first_name, last_name FROM Students;WHERE filters which rows are returned. Combine conditions with AND / OR, and use single quotes around text values.
SELECT * FROM Students WHERE grade = 11;
SELECT * FROM Students
WHERE grade = 12 AND gender = 'F';ORDER BY sorts the result set. ASC (default) is smallest/A first, DESC is largest/Z first.
SELECT first_name, last_name
FROM Students
ORDER BY last_name ASC;COUNT, SUM, AVG, MIN and MAX summarise many rows into a single value.
SELECT COUNT(*) FROM Students;
SELECT AVG(score) FROM Marks;
SELECT MAX(score), MIN(score) FROM Marks;GROUP BY splits rows into buckets (e.g. one bucket per subject) so an aggregate function runs separately for each bucket. HAVING filters groups, the way WHERE filters rows.
SELECT subject_id, AVG(score) AS avg_score
FROM Marks
GROUP BY subject_id
HAVING AVG(score) >= 60;A JOIN combines rows from two tables that share a related column (usually a primary key / foreign key pair).
SELECT st.first_name, st.last_name, m.score
FROM Students st
JOIN Marks m ON st.student_id = m.student_id;A query nested inside another query โ the inner query runs first and feeds the outer one.
SELECT first_name, last_name
FROM Students
WHERE student_id IN (
SELECT student_id FROM Marks WHERE score > 90
);The three statements that change data instead of just reading it.
INSERT INTO Students (student_id, first_name, last_name, grade, gender)
VALUES (11, 'Thabo', 'Radebe', 10, 'M');
UPDATE Students SET grade = 11 WHERE student_id = 11;
DELETE FROM Students WHERE student_id = 11;Ready to put it into practice?
Every technique above shows up in the SQL Practice problem set.
Try SQL Practice โ