In my role I review a wide variety of SQL code.
Some is highly professional that is above my skill level so I absorb as much learning as I can from it.
On the other side is self taught SQLers from the business world who are trying to build weird and wonderful reports with SQL held together with sticky tape and don’t truly comprehend the code that is written.
An example – I see the following a lot with people who don’t understand the data and trying to program stability in their code like this:
SELECT
A.BDATE,
A.ID,
A.CUST_ID,
C.CUSTOMER_NAME,
C.PHONE
FROM dbo.ACCOUNTS A
LEFT OUTER JOIN dbo.CUSTOMER C
ON C.BDATE = @DATE
C.CUST_ID = A.CUST_ID
WHERE
A.BDATE = @DATE AND
A.ACCOUNT_TYPE = 'XX';
This means if for some reason if there is an integrity problem between ACCOUNTS and CUSTOMER; they will still get the account data on the report.
The problem arises when the business requirement of the report evolve and the developer needs to filters on the left joined table.
For instance – “Report on Accounts on type XX but remove deceased customers so we don’t ring them”
I quite often see this:
SELECT
A.BDATE,
A.ID,
A.CUST_ID,
C.CUSTOMER_NAME,
C.PHONE
FROM dbo.ACCOUNTS A
LEFT OUTER JOIN dbo.CUSTOMER C
ON C.BDATE = @DATE
C.CUST_ID = A.CUST_ID
WHERE
A.BDATE = @DATE AND
A.ACCOUNT_TYPE = 'XX'
AND
C.DECEASED = 'N';
Without knowing it; the developer has broken their “Get all account XX report” business rule by turning their LEFT OUTER JOIN into effectively a INNER JOIN.
The correct way to write the query is:
SELECT
A.BDATE,
A.ID,
A.CUST_ID,
C.CUSTOMER_NAME,
C.PHONE
FROM dbo.ACCOUNTS A
LEFT OUTER JOIN dbo.CUSTOMER C
ON C.BDATE = @DATE
C.CUST_ID = A.CUST_ID
--------------------------------
AND
C.DECEASED = 'N'
--------------------------------
WHERE
A.BDATE = @DATE AND
A.ACCOUNT_TYPE = 'XX' ;
You can get a good gauge of the skill level of the developer by checking their LEFT OUTER JOINS. If they using them on every join where you know there is integrity between the two data objects; you can have an educated guess that you’re reviewing a novice developer.
This is where you have to pay extra attention to their joins and where predicates and make them aware of the consequences of the code.