Above Average Price Products
Hulu ☯️ Medium SQLAdvanced
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_quantity is greater than 0.
  • The average price should be calculated based only on products that are in stock.
  • When sorting, products with a NULL rating should be listed after those with non-null ratings.
  • The query should return all columns from the Products table.
  • 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:

Input:

Products
categoryidnamepriceratingstock_quantity
Electronics1Premium Laptop1299.994.85
Electronics2Basic Tablet299.994.20
Electronics3Gaming Console499.994.912
Accessories4Smart Watch399.99null8
Fashion5Designer Bag899.994.73
Electronics6Wireless Earbuds199.994.515

Output:
categoryidnamepriceratingstock_quantity
Electronics1Premium Laptop1299.994.85
Fashion5Designer Bag899.994.73
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →