13. Above Average Price Products
Beginner Mode
Objective
Construct an SQL query to fetch all products that are currently available in stock and priced above the average price of all available products. The resulting list should be sorted primarily by price in descending order and secondarily by rating in descending order, ensuring that products without a rating appear at the end.
Additional information
- Only include products where
stock_quantityis greater than 0. - The average price should be calculated based only on products that are in stock.
- When sorting, products with a
NULLrating should be listed after those with non-null ratings. - The query should return all columns from the
Productstable. - Handle scenarios where some products might not have a rating value.
Products
--------
product_id INT
name VARCHAR
price DECIMAL(10,2)
stock_quantity INT
rating DECIMAL(3,2) NULL
Examples
Example 1:
Output:
Input:
| Products | |||||
|---|---|---|---|---|---|
| category | id | name | price | rating | stock_quantity |
| Electronics | 1 | Premium Laptop | 1299.99 | 4.8 | 5 |
| Electronics | 2 | Basic Tablet | 299.99 | 4.2 | 0 |
| Electronics | 3 | Gaming Console | 499.99 | 4.9 | 12 |
| Accessories | 4 | Smart Watch | 399.99 | null | 8 |
| Fashion | 5 | Designer Bag | 899.99 | 4.7 | 3 |
| Electronics | 6 | Wireless Earbuds | 199.99 | 4.5 | 15 |
| category | id | name | price | rating | stock_quantity |
|---|---|---|---|---|---|
| Electronics | 1 | Premium Laptop | 1299.99 | 4.8 | 5 |
| Fashion | 5 | Designer Bag | 899.99 | 4.7 | 3 |
Code Environment
Sign in or try as guest to run your code.
Essential
SQL 0/33
Spark 0/20
Snowflake 0/22
Python 0/24
Need more practice in this area? Explore more questions →
Hulu
Revolut
Accenture
Adobe
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
Meta
OpenAI
Uber
X
DoorDash
Anthropic
Amazon
ActivisionBlizzard
Vercel
Crypto.Com
Zscaler
DeutscheBank
Apple
GoDaddy
GitLab
BMW
PayPal
Snowflake
AMD
Twilio
Atlassian
JPMorgan
NVIDIA
IBM
Databricks
Coinbase
Cisco
Robinhood
Twitter
Microsoft
Palantir
Netflix
VMware
Cloudflare
Stripe
Lyft
Salesforce
GitHub
Bloomberg
Airbnb
Walmart
SAP
HashiCorp
Instacart
Mastercard
Intel
Visa
Tesla