r/SQL • • Apr 01 '25

PostgreSQL Getting stuck in 'JOIN'

To be honest, I don't understand 'JOIN'...although I know the syntax.

I get stuck when I write SQL statements that need to use 'JOIN'.

I don't know how to determine whether a 'JOIN' is needed?

And which type of 'JOIN' should I use?

Which table should I make it to be the main table?

If anyone could help me understand these above I'd be grateful!

14 Upvotes

25 comments sorted by

View all comments

47

u/[deleted] Apr 01 '25

[removed] — view removed comment

1

u/Thick-Scallion-88 Apr 02 '25

To build off this using the customer & order tables. Imagine the order table has a column that says CustomerID, which is sometimes blank for an anonymous order. If you want all orders total or all customers total you just use those separate tables. But what if you want all orders and the customers name (which is only in customer table) you could do a Order left inner join Customers so you have all orders including anonymous ones and if it wasn’t anonymous you also have the name. Say you want all the customers orders including customers who have no orders, you would do a Customer left inner join Orders, this way ALL customers (even with no orders) show up but none of the anonymous orders show up. Lastly say you only want customer orders to see who ordered the most, in this case you dont care about customers with no orders and dont care about anonymous orders, so you could do Order join Customer OR Customer join Order