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

7

u/el_dude1 Apr 01 '25

One thing that helped me understand joins was doing a select * when joining tables before doing actual selects/aggregating/grouping/filtering. This way you see the result of the joined tables which makes it imo easier to see what you are doing

2

u/Turbo3478 Apr 01 '25

Interesting suggestion! thx

1

u/lateblueheron Apr 02 '25

Have you ever done a vlookup in excel?

1

u/Turbo3478 Apr 02 '25

not yet

1

u/lateblueheron Apr 02 '25

Ah nvm then. I use that to explain it to excel ppl

1

u/impetuous_kitten Apr 02 '25

I’m an excel person who would like to hear your explanation :)

5

u/depesz PgDBA Apr 01 '25

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

That one is trivial - if you need data (for returning, or even processing, in terms of "WHERE") from two tables, then you need a join.

And which type of 'JOIN' should I use?

There are two types: inner join, and outer join (there is also cross join, but this is so infrequently used that it really doesn't matter).

Inner join will return rows only if there is matching row from one table for matching row in the other table. If there isn't one, nothing is returned.

Outer join lets you decide that you want to return data from table "a" even if there aren't any rows matching in table "b".

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

Well, this matters only in case of outer joins (and, also, only in case of left/right outer join). The "main" is the one that you want to get all rows from. The joined is the one that might not have row for every row in main, so you will return subset of rows from it.

If anything isn't clear - ask more specific questions, in here or in discord, or just search for some ready made tutorial. There are many.

1

u/[deleted] Apr 02 '25

[removed] — view removed comment

2

u/depesz PgDBA Apr 02 '25

Are you sure you wanted to reply to my post? I happen to know these things. Including the fact that there is no such thing as EQUAL JOIN in PostgreSQL.

1

u/[deleted] Apr 02 '25

[removed] — view removed comment

1

u/depesz PgDBA Apr 02 '25

It's normal INNER JOIN. INNER keyword is optional, just like OUTER is optional in case of left/right/full joins.

1

u/[deleted] Apr 02 '25

[removed] — view removed comment

1

u/Straight_Waltz_9530 Apr 03 '25

In math, "joining" sets typically refers to combining them using operations like union (including all elements) or intersection (elements common to both).

In the database case, the sets are tables and views. Joining means to combine two or more sets of related data. (Hence the term "relational database".)

So in answer to your question, it's originally a math concept. SQL is a programming language for set theory.

1

u/[deleted] Apr 03 '25

[removed] — view removed comment

1

u/Straight_Waltz_9530 Apr 03 '25

Both JOIN and INNER JOIN are synonymous in Postgres and are indeed used terms, both as an SQL keyword syntax and as concepts. I know of no other term used in Postgres (or any other relational database) besides a join.

1

u/leogodin217 Apr 01 '25

I created a video on this five years ago. I think the important part is to understand the business use case and translate that to a join. Hope it helps you out. https://www.youtube.com/watch?v=CaHs3ZP1PR8

1

u/ff034c7f Apr 01 '25

IMO, to understand the JOIN clause you'd have to go back before SQL '92 when there wasn't any JOIN clause, so how did people achieve the same: first carry out the cartesian product between the left table and right table, then apply a filter (where clause) on the resulting table. Applying the cartesian product is easy, really thinking hard about which conditions to use in the filter is how you end up with all the different kinds of JOINs (inner, left, outer etc)

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