The given SQL statement is a PostgreSQL query that retrieves a list of customers who rented items last month but have not rented anything this month. Here's a breakdown of the query:
The query starts with a WITH
clause, which is used to define two temporary tables (also known as Common Table Expressions or CTEs) - last_month_rentals
and this_month_rentals
.
last_month_rentals
CTE:
customer_id
from the rental
table.rental_date
column, considering only the rentals that happened last month.this_month_rentals
CTE:
customer_id
from the rental
table.rental_date
column, considering only the rentals that happened this month.The main SELECT
statement retrieves all columns from the customer
table (using c.*
).
The query then joins the customer
table (aliased as c
) with the last_month_rentals
CTE (aliased as lmr
) on the customer_id
column. This means that it will only consider customers who rented items last month.
The query also performs a LEFT JOIN
with the this_month_rentals
CTE (aliased as tmr
) on the customer_id
column. This means that it will include all customers who rented items last month, regardless of whether they rented items this month or not.
Finally, the WHERE
clause filters the results to only include customers for whom the customer_id
in the this_month_rentals
CTE is NULL
. This means that the final result will only include customers who rented items last month but have not rented anything this month.