Multiple ways to implement a query
As we all know, a query can be implemented in several ways. But only one will be implemented among them:the one that takes the least cost when compared to the rest. For demonstration purposes, let's consider we have a payments table, which makes customer id entry along with the payment date when the customer clears his payment.
Now the general request from the management team is something like, "Get me the customers who've paid so far".
For this requirement, we can write queries like the following:
Approach 1: Joining tables
CREATE TABLE payments (id integer, amount numeric, paydate date); postgres=# EXPLAIN ANALYZE SELECT c.id FROM customers C, payments P WHERE c.id=p.id AND p.paydate<=now()::date; QUERY PLAN --------------------------------------------------------------- Hash Join (cost=16236.00..36268.00 rows=412000 width=4) (actual ...