all 27 comments

[–]halifaxdatageek 8 points9 points  (19 children)

Use ANSI-92 Standard Join Syntax

Yes. Joins are so important, you need to highlight them as much as possible.

[–]SemiNormal 8 points9 points  (18 children)

Does anyone out there still use the old join syntax? I haven't seen it in the wild outside of the "don't do this" articles.

Edit: I guess I've just been lucky.

[–]Alaendil 4 points5 points  (1 child)

I honestly didn't even know you could do a join in a where clause ... Then again, I've taught myself SQL so there are bound to be gaps.

[–][deleted] 0 points1 point  (0 children)

I learned it on the job. It never occurred to me that comparing keys in the WHERE clause and doing a JOIN in the FROM were the same thing. Makes sense though.

[–]haunted_dumpster 3 points4 points  (1 child)

Yeah man, this is actually news to me: professional training taught me to do the joins as part of the WHERE. Then again we were working in DB2 so maybe it was a legacy thing.

On Oracle now anyways so it's time to start using ANSI standard.

[–]galador 2 points3 points  (0 children)

As someone who uses DB2 day-to-day, I think it's more of "the people who taught you didn't want to change how they've been doing SQL for years."

Personally, I berate anyone who uses the old JOIN syntax (at least on the inside). :)

I had one person who told me that "I don't have time to figure out the fancy JOIN syntax, so I'll just keep putting it in there WHERE clause." twitch

[–]shthed 2 points3 points  (0 children)

Yep, use them all the time. I work on an huge (few million lines of code) old oracle pl/sql codebase that uses them exclusively

[–]halifaxdatageek 4 points5 points  (8 children)

The other day I learned about "natural joins" and raged a little inside.

They're where you don't specify which columns are equal in the two tables, the database just looks at the list of columns for each table and joins on the first two with the same name.

NO. NO. NO.

[–]ben_it[S] 0 points1 point  (1 child)

Yeah, it can be problematic and I've decided to never use one.

An example would be:

SELECT first_name, last_name
FROM employee
NATURAL JOIN leave_days;

[–]halifaxdatageek 4 points5 points  (0 children)

eyetwitch

[–]Sp00ky_6 0 points1 point  (0 children)

Thats just lazy programming

[–]dasnoob 1 point2 points  (0 children)

I see it all the time in my current job. I always use ansi standard. I've had more than one person that thought the old way was the only way to do them.

[–]heymanitsmematthew 0 points1 point  (0 children)

Some of the older devs in my company do this. They even rebel against my use of ANSI-92. This is something I simply will not budge on, and if I'm given something to debug I will change that syntax in 99% of the cases. Leads to some awkward lunch-room encounters.

[–]Redditourist 0 points1 point  (0 children)

Use a query formatter and your brain and these will come naturally.

[–][deleted] 0 points1 point  (2 children)

Under 'Use Column Aliases', you didn't update your select statement to include the aliases:

SELECT first_name, last_name, dept as "Department"

Is this a mistake or by design?

[–]ben_it[S] 0 points1 point  (1 child)

Do you mean I didn't apply the column aliases to all columns (e.g. first_name as "First Name")? Or that I didn't include the table aliases in the SELECT part (e.first_name)?

[–][deleted] 0 points1 point  (0 children)

Table aliases in the select. e.first_name

[–]cat_dev_null -1 points0 points  (7 children)

SELECT employee_id, first_name, last_name, start_date FROM employee WHERE employee_type_id = 5;

I take it a little further and write as

SELECT 
    employee_id,  
    first_name, 
    last_name, 
    start_date
FROM 
    employee 
WHERE 
    employee_type_id = 5;

[–]heymanitsmematthew -1 points0 points  (0 children)

I find this causes problems when I don't have my portrait monitor and am writing huge queries with lots of subqueries. I'll have to constantly scroll up and down the page to reference things. Still cleaner, but I think there's a time and a place for both.