The core language is when you start naming crap with capital letters though that part is not. Which is why those of us that do databases a lot loathe when someone does this.
Here's the problem, at least with postgres and probably others. You've made your database case sensitive and now you have to use double quotes on your raw sql unless your editor is performing some syntactic sugar. ORM's usually apply the double quotes so this is a moot point until you start trying to do business intelligence, financial audits for going public, etc where people are writing TONS of raw sql. Aliasing fixes some of this, but not column names, and you end up having to write crap like this.
Honest question: why? I use PascalCase for just about everything, and singular table names (because I was told that it makes alphabetical sorting better when looking at tables. "Worker" comes before "WorkerLocation", that sort of thing)
It's just the convention that everyone uses (although I think MSSQL uses PascalCase? I don't remember). SQL is case-insensitive by default, though, so having uppercase names in your tables isn't really semantically meaningful.
Inner are better than left if you can use them. There's really nothing you can do about it other than making sure you have indexes on the join fields. Which you will have if they are primary keys.
What value are you gaining from the Job table? I don't see any reason to even include it. Change your from clause to Worker and join on the Customer table.
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.
Are you joining customer just for the where clause? If yes i'd suggest this:
select ... from ... join worker on workerid = wid where exists (select null from customer where city = 'Chicago' and customerid = cid)
i am not a fan of joining tables, you are not actually selecting from.
edit: you are joining three tables and select two fields. something is not necessary to join here imho, but impossible to say without a broader picture of your requirements
Always reference tables in column names when using joins in a statement (referring to the Region column). Prevents you from getting 'ambiguous column references' errors and ensures the right stuff is being read
22
u/yetzederixx Feb 01 '22
Yeah stop using capital letters in your schemas.
1
u/mplaczek99 Feb 01 '22
I don't think the two inner join statements are good...
4
u/[deleted] Feb 05 '22
Always reference tables in column names when using joins in a statement (referring to the Region column). Prevents you from getting 'ambiguous column references' errors and ensures the right stuff is being read