IBM documents Db2 SQL with syntax diagrams—visual maps that show which keywords are required, which clauses are optional, and how lists repeat. Learning to read those diagrams is a career skill: the SQL Reference becomes usable instead of intimidating. This page covers SQL syntax basics, how to read Db2 syntax diagrams, and how to write SQL comments safely.
Syntax is the grammar of the language: the legal order of keywords, identifiers, operators, and punctuation. Semantics is what a legal statement means. A statement can be syntactically valid and still wrong for the business (update the wrong rows) or fail at run time (object not found, wrong data type). Beginners should separate “Db2 rejected my grammar” from “Db2 ran it and I dislike the result.”
Db2 SQL statements are built from tokens: keywords (SELECT), identifiers (EMPLOYEE), operators (=, AND), literals ('A01'), special characters (parentheses, commas), and comments. Whitespace generally separates tokens; formatting is for humans. Your team style guide still matters for reviews.
1234SELECT EMP_ID, EMP_NAME FROM EMPLOYEE WHERE DEPT_ID = 'A01' ORDER BY EMP_NAME;
That statement follows a familiar pattern: SELECT list, FROM object, optional WHERE, optional ORDER BY. Syntax diagrams formalize patterns like this for every statement, including ones with many optional branches.
IBM’s Db2 manuals use railroad diagrams. You read them left to right, top to bottom, following the path of the line. The following conventions appear throughout the SQL Reference (wording adapted for learners; always check the manual’s “How to read syntax diagrams” section for the edition you use).
| Symbol / style | Meaning |
|---|---|
| ►►── / beginning marker | Start of the statement or fragment |
| ───► continuation | Diagram continues on the next line |
| ►── continued | This line continues a previous line |
| ───►◄ end marker | End of the statement or fragment |
| UPPERCASE | Keyword; type as shown (case-insensitive in SQL) |
| lowercase-name | Variable: replace with your value |
Keywords appear in UPPERCASE in diagrams (FROM, WHERE, JOIN). You must spell them as shown; SQL treats them case-insensitively when you type. Variables appear in lowercase italic-style names such as table-name or search-condition. You replace those with real names or with fragments defined in other diagrams.
The main horizontal line is the required spine of the statement. Optional items usually appear on a branch above or below the main line—you may take the branch or skip it. Alternate choices appear as parallel branches where you pick one path. Repeating items often show a loop arrow so you can return and add another list element (for example more columns in a select list).
12345678Conceptual SELECT fragment (not an official IBM drawing): ►►─ SELECT ─┬─ * ───────────────────┬─ FROM table-name ──►◄ └─ column-list ─────────┘ column-list: ├── column-name ┬───────────────────┤ └─ , column-name ←──┘ (repeat with commas)
If the diagram shows parentheses, commas, periods, asterisks, or operators, type them. They are not decoration. Beginners often drop commas between select-list items or forget parentheses around expressions—both are syntax errors.
Diagrams frequently reference other nonterminals such as expression, search-condition, or fullselect. That means you must also follow the rules for those diagrams. Professional SQL reading is recursive: open the referenced section when you need a clause you have not memorized.
Free-form SQL tools let you break lines between tokens. Embedded SQL in COBOL sits inside EXEC SQL blocks; COBOL margin rules still apply to the surrounding source, and many sites keep SQL verbs aligned for readability. Dynamic SQL strings in host variables must contain complete, legal statement text when prepared.
123456EXEC SQL SELECT COUNT(*) INTO :WS-COUNT FROM EMPLOYEE WHERE DEPT_ID = :WS-DEPT END-EXEC.
Comments document intent without changing meaning. Db2 SQL supports common comment forms you will see in scripts and generated SQL.
Two consecutive hyphens start a simple comment. The comment runs to the end of the line. Use them to label predicates, note ticket numbers, or temporarily disable a clause while testing in a scratch pad (prefer version control over commenting out production logic long term).
1234SELECT EMP_ID, EMP_NAME -- directory fields only FROM EMPLOYEE WHERE DEPT_ID = 'A01'; -- active department filter -- AND STATUS = 'ACTIVE' -- temporarily ignored in this test
Bracketed comments begin with /* and end with */. They can span multiple lines. Nested bracketed comments are not something to rely on; keep comments simple. Avoid placing comment delimiters inside string literals accidentally—your string may end early and the rest looks like broken SQL.
12345678SELECT EMP_ID, EMP_NAME FROM EMPLOYEE /* Multi-line note: Used by HR roster report. */ WHERE HIRE_DATE >= DATE('2020-01-01');
Inside the SQL statement text, use SQL comment syntax. Outside EXEC SQL in COBOL, use COBOL comments (for example * in the indicator area). Mixing them up is a frequent beginner mistake: a COBOL * does not comment out SQL keywords inside the EXEC SQL block the way newcomers expect.
Suppose a simplified diagram says: keyword DELETE, keyword FROM, variable table-name, optional branch with keyword WHERE plus search-condition. Valid statements include deleting all rows from a table (dangerous) or deleting with a predicate (usual).
123456-- Optional WHERE taken DELETE FROM TEMP_STAGE WHERE LOAD_DATE < CURRENT DATE - 7 DAYS; -- WHERE branch skipped means "all rows" — confirm intentionally! -- DELETE FROM TEMP_STAGE;
The diagram teaches the option. Professional judgment decides whether skipping WHERE is acceptable. Syntax skill plus operational caution belong together.
When Db2 returns a syntax error, read the message token pointing near the failure, then open the statement’s syntax diagram and compare clause order. That workflow scales to unfamiliar statements better than guessing.
A syntax diagram is like a treasure map for a sentence. The thick path shows words you must say, like “please” and “thank you.” Side paths show extra words you may add, like “with whipped cream.” Loopy arrows mean you can name more toppings, comma after comma. Uppercase words are fixed magic words. Lowercase blanks are spaces where you pencil in your own names. Comments are sticky notes on the map that the cook ignores when making the food.
1. In IBM SQL syntax diagrams, what do uppercase words usually represent?
2. What do lowercase names like column-name usually mean in a syntax diagram?
3. Which comment form starts with two hyphens?
4. What do brackets or optional branches in syntax diagrams generally indicate?
5. Must punctuation shown in a syntax diagram (parentheses, commas) be typed?