SQL ORDER BY

Last updated: August 26, 2026.

ORDER BY sorts a query result by one or more expressions. Without it, row order is not guaranteed.

Sort by multiple columns

SELECT order_id, customer_name, order_total, placed_at
FROM orders
ORDER BY placed_at DESC, order_id DESC;

Sort calculated or null values

SELECT product_name, unit_price * quantity AS line_total
FROM order_items
ORDER BY line_total DESC, product_name ASC;

Add a unique tie-breaker when pagination must be stable. Null ordering and case-sensitive text ordering vary by database engine and collation.

admin

admin

Leave a Reply

Your email address will not be published.