SELECT
customer.customer_id,
customer.first_name,
customer.last_name,
customer.email,
COUNT(rental.rental_id) AS rental_count,
SUM(payment.amount) AS total_payment
FROM
customer
LEFT JOIN
rental ON customer.customer_id = rental.customer_id
LEFT JOIN
payment ON rental.rental_id = payment.rental_id
GROUP BY
customer.customer_id
ORDER BY
total_payment DESC;
- This SQL statement is designed to retrieve valuable customer information, specifically the customer's ID, first name, last name, email, the total number of rentals they've made, and the total amount they've paid.
- The
LEFT JOIN
clause is used to combine rows from the customer
, rental
, and payment
tables based on the customer's ID and the rental's ID.
- The
GROUP BY
clause is used to group the results by the customer's ID, and the ORDER BY
clause is used to sort the results in descending order based on the total amount paid by the customer. This will help in determining who the good customers are, as they are likely to be the ones who have made the most rentals and payments.