SQL Format, Part 2 of 4
SQL can be typed in any combination of upper and lower case letters such as SELECT, select, or SeLeCt. Normally, anything that is not between quotes is typed in capital letters and is displayed using a type font where every character has the same width so that the SQL statement can be aligned for readability. The formatting of spaces and tabs in an SQL statement does not affect it’s execution, but it does affect your ability to read it. While multiple SQL statements can be placed on one line, this practice is discouraged. An SQL statement can span multiple lines and can start in any column as long as words, names, or quoted strings are not split. Here is an unformatted statement:
SELECT CUSTOMER_TBL.CUST_ID, CUSTOMER_TBL.CUST_NAME, CUSTOMER_TBL.ADDRESS, CUSTOMER_TBL.CITY, CUSTOMER_TBL.STATE, CUSTOMER_TBL.ZIP, INVENTORY_TBL.DESCRIPTION, INVENTORY_TBL.CARGO_TONS, SALES_TBL.SALE_DATE, SALES_TBL.QTY, SALES_TBL.AMOUNT FROM CUSTOMER_TBL, INVENTORY_TBL, SALES_TBL WHERE CUSTOMER_TBL.CUST_ID = SALES_TBL.CUST.ID AND SALES_TBL.INVN_ID = INVENTORY_TBL.INVN_ID;
Here is the same statement with reformatting for improved readability. Don’t worry about the details. They will be covered later.
SELECT C.CUST_ID,
C.CUST_NAME,
C.ADDRESS,
C.CITY,
C.STATE,
C.ZIP,
I.DESCRIPTION,
I.CARGO_TONS,
L.SALE_DATE,
L.QTY,
L.AMOUNT
FROM CUSTOMER_TBL C,
INVENTORY_TBL I,
SALES_TBL L
WHERE C.CUST_ID = L.CUST.ID
AND L.INVN_ID = I.INVN_ID;
Both statements are the same, but the second statement is much easier to read. Spacing and tabs have been added to the second statement, which also uses table aliases in the FROM clause for brevity. Don’t worry about the details of the statement. What is illustrated here is clarity and readability.