SQL Tutorial
Subqueries, Transactions and Window Functions
Move beyond basic CRUD with reusable query patterns and analytical SQL.
Concept
Subqueries can provide scalar values, sets or derived tables. Common table expressions can improve readability when a query has several logical steps.
Transactions group changes under ACID guarantees provided by the database. Window functions compute values across related rows without collapsing the result the way GROUP BY does.
Example
SELECT name, city,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY name) AS city_row
FROM students;
Type the example yourself and change at least one value. Small experiments reveal syntax and behavior faster than passive reading.
Practice Tasks
- Write a subquery that finds rows above an average value.
- Use BEGIN/ROLLBACK while testing an update.
- Use ROW_NUMBER or RANK over a partition.
Key Takeaways
- Subqueries and CTEs structure complex logic.
- Transactions protect multi-step changes.
- Window functions enable analytical calculations while retaining detail rows.
Previous
← GROUP BY and Aggregate Functions
← GROUP BY and Aggregate Functions
Track complete
Back to SQL Tutorial →
Back to SQL Tutorial →