SQL

Retrieve Orders with Customer Details from Last 30 Days

Fetch recent customer orders, joining customer and order tables with a date-based filter for active or recent transactions.

SELECT
  o.order_id,
  o.order_date,
  o.total_amount,
  c.customer_name,
  c.email
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days'
ORDER BY o.order_date DESC,
         o.total_amount DESC;
How it works: This SQL query retrieves essential details about orders placed in the last 30 days, along with the corresponding customer information. It uses an `INNER JOIN` to combine data from the `orders` and `customers` tables based on their shared `customer_id`. The `WHERE` clause filters records to include only orders from the specified date range using `CURRENT_DATE - INTERVAL '30 days'` (syntax may vary slightly between databases, e.g., `DATE_SUB(CURDATE(), INTERVAL 30 DAY)` for MySQL). Finally, `ORDER BY` sorts the results by order date (descending) and then by total amount (descending), making recent and high-value orders easy to spot.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs