Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Get First and Last Order per Customer

Get First and Last Order per Customer

Orders Table:



Query Explanation:

  • MIN(OrderDate) gives the first order date for each customer.

  • MAX(OrderDate) gives the last order date for each customer.

  • GROUP BY CustomerID ensures we get one row per customer.


SQL Query:

SELECT
    CustomerID,
    MIN(OrderDate) AS FirstOrderDate,
    MAX(OrderDate) AS LastOrderDate
FROM Orders
GROUP BY CustomerID;


Output: