NULL in queries
1 / 13 · ≈ 9 min left
The problem
The tea shop’s marketing report says 0 customers have never ordered, yet Emma and Farid have an account and no order. The query behind the report uses NOT IN, and one order has a NULL customer_id. You rewrite the report’s queries one by one in psql, on PostgreSQL.
customers (id, name, city, phone) has 6 rows: Ana Costa, Ben Okafor, Chloé Martin, Dmitri Ivanov, Emma Schulz and Farid Benali. Ben, Dmitri and Farid have a NULL phone. Emma and Farid have no order.
orders (id, customer_id, status, coupon, shipped_at) has 9 rows. Guest order 107 has a NULL customer_id. coupon is WELCOME10 on 2 orders, TEA5 on 2, NULL on 3, and an empty string '' on 2, saved by an old checkout form. shipped_at is a date on the 4 shipped orders and NULL on the other 5.