For these two Tables write a Query that returns the email from the CustomerOrders along with the name of the Event and Order Date of the LAST event that the Customer bought.
Event Table
---------+---------------------+
| EventID | EventName |
+---------+---------------------+
| 1 | Rolling Stones |
| 2 | Ike and Tina Turner |
| 3 | Lakers vs Clipper |
| 4 | Disney on Ice |
| 5 | Yankees Vs Red Sox |
+---------+---------------------+
CustomerOrders Table
+------------------+-----------+------------+---------------------------+---------+
| idCustomerOrders | Name | Date_order | Email | EventID |
+------------------+-----------+------------+---------------------------+---------+
| 1 | Paul | 2022-02-02 | Paulryan@aol.com | 1 |
| 2 | Max | 2022-03-02 | Maxwellsmith@aol.com | 2 |
| 3 | Jack | 2022-02-02 | JacksonSmith@hotmail.com | 5 |
| 4 | Alexander | 2022-05-02 | ALexDex@gmail.com | 4 |
| 5 | Bobby | 2022-06-02 | BobbyJackson@warriors.com | 2 |
| 6 | Paul | 2022-06-02 | Paulryan@aol.com | 5 |
| 7 | Jack | 2022-05-02 | JacksonSmith@hotmail.com | 1 |
+------------------+-----------+------------+---------------------------+---------+
I can write a left join, but i'm confused on how I can get the LAST Event (most recent event).
SELECT Name, Email, event.EventName, Date_order
FROM CustomerOrders
LEFT JOIN event
ON CustomerOrders.EventID = event.EventID;
+-----------+---------------------------+---------------------+------------+
| Name | Email | EventName | Date_order |
+-----------+---------------------------+---------------------+------------+
| Paul | Paulryan@aol.com | Rolling Stones | 2022-02-02 |
| Max | Maxwellsmith@aol.com | Ike and Tina Turner | 2022-03-02 |
| Jack | JacksonSmith@hotmail.com | Yankees Vs Red Sox | 2022-02-02 |
| Alexander | ALexDex@gmail.com | Disney on Ice | 2022-05-02 |
| Bobby | BobbyJackson@warriors.com | Ike and Tina Turner | 2022-06-02 |
| Paul | Paulryan@aol.com | Yankees Vs Red Sox | 2022-06-02 |
| Jack | JacksonSmith@hotmail.com | Rolling Stones | 2022-05-02 |
+-----------+---------------------------+---------------------+------------+
I think I need to use distinct or unique keyword.
I need the output to be something like this:
+-----------+---------------------------+---------------------+------------+
| Name | Email | EventName | Date_order |
+-----------+---------------------------+---------------------+------------+
| Max | Maxwellsmith@aol.com | Ike and Tina Turner | 2022-03-02 |
| Alexander | ALexDex@gmail.com | Disney on Ice | 2022-05-02 |
| Bobby | BobbyJackson@warriors.com | Ike and Tina Turner | 2022-06-02 |
| Paul | Paulryan@aol.com | Yankees Vs Red Sox | 2022-06-02 |
| Jack | JacksonSmith@hotmail.com | Rolling Stones | 2022-05-02 |
+-----------+---------------------------+---------------------+------------+