BeginnerCoder

Lesson 10 — Scalar subqueries

Scalar subqueries in SELECT

A scalar subquery returns exactly one value (one row, one column), so it can be used anywhere a value fits — including the SELECT list. Here it adds the overall average to every row, so you can compute each product's difference from it.

SELECT name, price - (SELECT AVG(price) FROM products) AS diff
FROM products

Why it matters: "compared to average" columns are everywhere in dashboards, and this computes them in one query.

Want it another way?
Task

Replace the 0 with a scalar subquery for the average price. Expected: differences -12.0, 3.0, 9.0.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.