I am attempting some SQL practice (on Udacity) and I stumbled upon an issue with the SQL JOIN exercises. I am attempting to JOIN 3 tables. The code are as follows:
MY CODE:
SELECT r.name, s.name, a.name
FROM accounts a
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON s.region_id = r.id
ORDER BY a.name;
Result: Only the name column is printed
ANSWER PROVIDED:
SELECT r.name region, s.name rep, a.name account
FROM sales_reps s
JOIN region r
ON s.region_id = r.id
JOIN accounts a
ON a.sales_rep_id = s.id
ORDER BY a.name;
Result: All 3 columns are printed.
I dont quite understand why there are different output. Would appreciate any and all feedback!
I tried joining the 3 tables and ensuring that the PK connects with the FK
I want to print the region name, representative name and account name
identifierand whatever the client is, it is unable to distinguish between them. When columns have the same name, you should always alias them so that they have a unique name in the context of the select list.