Sorting Products by Price, Stock, and Name

Scenario

The report aims to provide a prioritized list of products, showing more expensive items first but, within the same price range, listing products with lower stock to highlight potential restocking needs. Additionally, within the same price and stock level, products will be listed alphabetically by name.

SQL Query

Here’s how the SQL query would look for sorting the Products table based on these criteria:

SELECT 
    ProductID,
    ProductName,
    Price,
    Stock
FROM Products
ORDER BY Price DESC, Stock ASC, ProductName ASC;
Explanation
  • Selection: The query selects the ProductID, ProductName, Price, and Stock from the Products table.
  • ORDER BY Clause:
    • Price DESC: Products are ordered by price in descending order, starting with the most expensive.
    • Stock ASC: Within the same price category, products are sorted by stock in ascending order. This helps to identify products that are priced the same but may require urgent restocking.
    • ProductName ASC: Lastly, if products have the same price and stock level, they are ordered alphabetically by product name. This ensures a consistent order for products with similar price and stock characteristics.
Output
Example Output Format

Assuming we have a hypothetical set of product data, the output might look something like this in a tabular format:

ProductIDProductNamePriceStock
102Luxury Watch500.005
101Designer Bag450.003
105High-end Headphones450.0015
108Gourmet Chocolate120.0010
103Organic Tea120.002
107Handmade Soap15.0020
106Scented Candle15.0050