Search⌘ K

WITH Clauses (CTEs) for Clarity

Learn how to use WITH clauses in SQL.

Step 1: Start with a challenge

In the last lesson, you wrote nested subqueries like:

MySQL
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);

It works, but imagine now needing to use that average salary in multiple places or layer it with other filters. That’s where your query starts getting messy.

Ask yourself: How can I make this readable, like a set of clear steps?

That’s where CTEs come in.

Prompt: Rewrite this queryAlso append the SQL code given above to this prompt, and let AI teach you something new. using a WITH clause so the average salary is defined first, then used in the main query.

Powered by AI
5 Prompts Remaining
Prompt AI WidgetOur tool is designed to help you to understand concepts and ask any follow up questions. Ask a question to get started.

Step 2: Run

...