SQL Query to Find All Customers Who Have Placed an Order in the Last 30 Days

This technical problem involves writing a SQL query to find all customers who have placed an order in the last 30 days. The example input data consists of a customers table with id and name columns, and an orders table with id, customer_id, and order_date columns. The example output lists the id and name columns for all customers who have placed an order in the last 30 days.

Problem

Write a SQL query to find all customers who have placed an order in the last 30 days.
Example input:
customers table:
id | name
1 | John
2 | Jane
3 | Joe
4 | Melissa
orders table:
id | customer_id | order_date
1 | 1 | 2020-01-15
2 | 2 | 2020-02-01
3 | 3 | 2020-02-15
4 | 1 | 2020-03-01
5 | 2 | 2020-03-15
6 | 4 | 2020-03-20
Example output:
1 | John
2 | Jane
3 | Joe
4 | Melissa

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT c.id, c.name
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY c.id, c.name;

A.I. Evaluation of the Solution

This solution is complete and solves the problem. The approach is to join the customers and orders table on the customer id and then filter for orders that have been placed in the last 30 days. This approach is sensible and efficient.

Evaluated at: 2022-11-12 06:16:14