Subquery
A subquery is a SELECT inside another SQL. WHERE marks = (SELECT MAX(marks) FROM students); the topper. The inner query runs as a value or as a table.
Scalar subquery returns one value. IN (SELECT roll FROM …) returns a list. FROM (SELECT …) AS t is a derived table and needs an alias.
Correlated subquery uses the outer row (WHERE m.roll = s.roll) and can be slow. A JOIN is often clearer and faster.
Trap: subquery that returns two rows where one value was expected. Trap: correlated subquery in a huge table without an index.
On the example next to this theory — Subquery — uNION merges result sets and drops duplicate rows.
WHERE marks = (SELECT MAX(marks) FROM students);