Recap - With Queries
Part of the Fundamentals section of Coddy's SQL journey. Lesson 50 of 72.
Challenge
EasyAvailable 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
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns9Multiple tables
Basic Join Part 1Basic Join Part 2Recap - JoinSelf joinRecap - Self JoinUnionSimplify queries, WITH keywordRecap - With QueriesRecap - Real Estate ContractorPractice on your own: SQL playground