Menu
Coddy logo textTech

Recap - With Queries

Part of the Fundamentals section of Coddy's SQL journey. Lesson 50 of 72.

challenge icon

Challenge

Easy

Available tables and columns:

  • <b>devices_specs</b>: <b>device_id</b>, <b>width</b>, <b>height</b>, <b>num_features</b>, <b>opinion</b>
  • <b>devices_score</b>: <b>device_id</b>, <b>score</b>

A device's quality is measured by (width/height)*num_features.

We want to find all of the overrated devices. Fetch all of the devices where the device's opinion is greater than the average quality and the device's score is less than the average quality. Return only the device_id.

To solve it, use the WITH clause to create a subquery that calculates the average quality and reuse it in the main query.

Try it yourself

-- Step 1: name a temporary result set that holds one single average value
WITH avg_quality AS (
    SELECT ____ AS avg_q
    FROM devices_specs
)
-- Step 2: bring the two device tables together, then attach the temporary result set
SELECT dsp.device_id
FROM devices_specs dsp
INNER JOIN devices_score dsc ON ____
INNER JOIN avg_quality aq
-- Step 3: keep the devices that sit on opposite sides of that average
WHERE ____

All lessons in Fundamentals

Practice on your own: SQL playground