Subqueries
Pissa, the expanding pizza delivery enterprise, is now taking your expertise on identifying some trends.
They want to streamline its pizza offerings by identifying underperforming pizza types. Your task is to find the pizza types that are ordered less frequently than the average for all types.
This is a part of the course
“Introduction to Snowflake”
Exercise instructions
- Complete the
JOIN
clauses to join thepizzas
, andorder_details
tables appropriately. - Use the
GROUP BY
clause to group the results byname
andcategory
from thepizza_type
table. - Fill in the subquery to find the
AVG
oftotal_quantity
. - Order the results by
total_quantity
in ascending order.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
SELECT
pt.name,
pt.category,
SUM(od.quantity) AS total_quantity
FROM pizza_type AS pt
-- Join pizzas and order_details table
___ AS pz
ON pt.pizza_type_id = pz.pizza_type_id
___ AS od
ON pz.pizza_id = od.pizza_id
-- Group by name and category
___
HAVING SUM(od.quantity) < (
-- Calculate AVG of total_quantity
SELECT ___
FROM (
SELECT SUM(od2.quantity) AS total_quantity
FROM pizzas AS pz2
JOIN order_details AS od2
ON pz2.pizza_id = od2.pizza_id
GROUP BY pz2.pizza_type_id
) AS sub
)
-- Order by total_quantity in ascending order
___
This exercise is part of the course
Introduction to Snowflake
This course will take you from Snowflake's foundational architecture to mastering advanced SnowSQL techniques.
In Chapter 3, you'll advance your skills in Snowflake SQL. You'll begin by exploring diverse join methods and building complex queries with subqueries and CTEs. We'll emphasize query optimization, showing you ways to enhance the speed and efficiency of your SQL tasks. At the end, we'll delve into handling semi-structured data like JSON.
Exercise 1: Joining in SnowflakeExercise 2: NATURAL JOINExercise 3: The world of JOINSExercise 4: Subquerying and Common Table ExpressionsExercise 5: SubqueriesExercise 6: Understanding CTEExercise 7: CTEsExercise 8: Snowflake Query OptimizationExercise 9: Essentials of query optimizationExercise 10: Early filteringExercise 11: Query historyExercise 12: Handling semi-structured dataExercise 13: PARSE_JSON & OBJECT_CONSTRUCTExercise 14: Querying JSON dataExercise 15: JSONifiedExercise 16: Wrap-upWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.