Lesson 12 of 12 · 22 min

Choosing a readable query structure

By the end of this lesson you can look at a multi-step question, choose the clearest structure for it, and rewrite an existing query into an equivalent one that is easier to read, test and change.

You now have six tools for multi-step questions: scalar subqueries, IN lists, EXISTS / NOT EXISTS, correlated subqueries, derived tables and CTEs (plus joins and set operations). Most questions can be written with several of them, and all versions return the same rows. The difference is how quickly a reader can understand the query, check it, and change it safely.

Here is a query that works. Read it and try to say, in one sentence, what it returns:

Included with a Formula Quest membership

This lesson, its 3 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons