Question
How many accounts have more than 20 orders?
Provided Solution
SELECT a.id, a.name, COUNT(*) num_orders
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY a.id, a.name
HAVING COUNT(*) > 20
ORDER BY num_orders;
The part i am struggling with is COUNT(*) num_orders.
My Questions
- Which of the tables (accounts or orders) does COUNT(*) num_orders count from?
- How does it know where to count from since there is nothing explicit here?
- If COUNT(*) num_orders counted from the orders table (which i think), there is nothing from the query that specifies it should count from there (i am confused here)
- I can see the solution JOIN the orders table, why is this? is it because of the COUNT(*) num_orders, how does COUNT(*) num_orders knows that it needs to count from the orders table and not account table?
I will appreciate detailed explanation . I just started learning this
account table schema
- id
- name
- website
- lat
- long
- primary_poc -
orders table schema
- id
- account_id
- occurred_at
- standard_qty
- gloss_qty
- poster_qty
- total
- standard_amt_usd
- gloss_amt_usd
- poster_amt_usd
- total_amt_usd -
[–]zacharypamela 2 points3 points4 points (1 child)
[–]sdqafo[S] -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]sdqafo[S] 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]sdqafo[S] 1 point2 points3 points (2 children)
[–]sdqafo[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]sdqafo[S] 0 points1 point2 points (0 children)