SQLAI.ai

find customers who didn't rent a movie in the last 12 months but rented a movie in the 12 months before that

            
SELECT c.customer_id, c.first_name, c.last_name FROM customer c WHERE NOT EXISTS ( SELECT 1 FROM rental r WHERE r.customer_id = c.customer_id AND r.rental_date BETWEEN NOW() - INTERVAL '12 months' AND NOW() ) AND EXISTS ( SELECT 1 FROM rental r WHERE r.customer_id = c.customer_id AND r.rental_date BETWEEN NOW() - INTERVAL '24 months' AND NOW() - INTERVAL '12 months' );
  • This SQL statement is designed to find customers who didn't rent a movie in the last 12 months but rented a movie in the 12 months before that.
  • The NOT EXISTS clause is used to exclude customers who have rented a movie in the last 12 months.
  • The EXISTS clause is used to include customers who have rented a movie in the 12 months before the last 12 months.
  • Public
  • ·
  • Tue, 11 Jul 2023 11:55:59 GMT