Suppose that I have these tables,and example data (the real table I am working on has more columns)
table `users`
id | name | zip |
---|---|---|
1 | John | 201 |
table `region`
zip | city | state | country |
---|---|---|---|
22 | a | b | c |
table `purchase`
id | user_id | price |
---|---|---|
2 | 3 | 2 |
Should I use subquery
,cte
or join
when those tables have billions of records (so efficiency matters) and
- query which city,state,country does the user id=1 lives (Yield 1 row)
- query the name, city, price of a purchase order by price desc limit 100 (Get details on top 100 purchases).
- query the number of the purchases, sum of price of users whose name begins with "Dr"
- query the average spending(sum of price) of users in each city in country "c"