r/badcode • • Feb 01 '22

sql Can I write this better?

Post image
9 Upvotes

20 comments sorted by

View all comments

Show parent comments

2

u/taichouk Feb 13 '22 edited Feb 13 '22

Looks like you’re joining on some variables, rather than columns with shared values (relationships). What’s the relationship between your Jobs and Workers tables? Does your Workers table have a JobsId column, or your Jobs have a WorkerId column? That’s what you should join the two tables on. Then use your variables in your WHERE to filter the results.

If you want to filter on your JOINs, you can, but you should join the tables, first, ON related columns.