SQL is how applications and analysts talk to Db2. Whether you write COBOL with EXEC SQL, explore data in SPUFI, or call Db2 from a mid-tier service, you use the same language ideas: tables, predicates, and statements that retrieve or change data. This lesson introduces SQL on Db2 for z/OS, defines essential terminology, and surveys the main statement families you will meet throughout this tutorial series.
SQL means Structured Query Language. It is a declarative language: you describe what data you want or what change you want, and Db2’s optimizer chooses an access path to do it. That differs from procedural COBOL, where you spell out loops and file reads step by step.
On Db2 for z/OS, SQL is the application interface to relational data. You can:
SQL is not JCL, not a utility control statement, and not a CICS command—though all of those may surround an application that uses SQL. Keeping those layers straight prevents confusion when something fails.
A COBOL program reading a VSAM KSDS usually navigates records with keys and loops. SQL works on sets. One SELECT can return thousands of rows matching a predicate. One UPDATE can change every row that satisfies a WHERE clause. That power is why a missing WHERE clause on DELETE is dangerous—and why thinking in sets is the core SQL skill.
12345-- Set-oriented: all open orders for one customer SELECT ORDER_ID, ORDER_DATE, STATUS FROM ORDERS WHERE CUST_ID = 'C10021' AND STATUS = 'OPEN';
Db2 decides whether to use an index, scan a table space, or apply another strategy. Your job as a beginner is to write clear, correct SQL; later lessons cover explaining and tuning access paths.
| Term | Meaning |
|---|---|
| Table | Named set of rows and columns |
| Row | One record in a table |
| Column | Named attribute with a data type |
| Predicate | Condition in WHERE/HAVING that filters rows |
| Result table | Rows returned by a query |
| Null | Unknown or missing value (not zero or blank) |
| Cursor | Named control structure to fetch query rows one at a time in a program |
| Host variable | Program variable exchanged with SQL (for example :WS-CUST-ID) |
A few more phrases appear early. A schema qualifies object names (for example HR.EMPLOYEE). A qualified name includes that qualifier. A literal is a constant written in SQL text, such as 'OPEN' or 100. A expression computes a value from columns, literals, operators, and functions. You will deepen each term in later pages; learn the vocabulary now so examples make sense.
| Category | Examples | Purpose |
|---|---|---|
| Query | SELECT | Retrieve rows; produce a result table |
| DML | INSERT, UPDATE, DELETE, MERGE | Change data in tables |
| DDL | CREATE, ALTER, DROP | Define or change objects |
| DCL | GRANT, REVOKE | Control privileges |
| Transaction | COMMIT, ROLLBACK | End or undo a unit of work |
SELECT retrieves data. You name columns (or use * carefully), identify the FROM table or join, and optionally filter with WHERE, group with GROUP BY, filter groups with HAVING, and order with ORDER BY. The output is a result table. In programs, a singleton SELECT INTO host variables returns one row; multi-row results usually need a cursor.
1234567891011EXEC SQL SELECT CUST_NAME INTO :WS-CUST-NAME FROM CUSTOMER WHERE CUST_ID = :WS-CUST-ID END-EXEC. IF SQLCODE = 0 DISPLAY 'OK' ELSE DISPLAY 'SQLCODE=' SQLCODE END-IF.
Data Manipulation Language statements change table contents:
DML participates in a unit of work. Changes become permanent at COMMIT (or the environment’s syncpoint) and disappear on ROLLBACK if not committed. Always consider WHERE clauses on UPDATE and DELETE.
123456UPDATE ACCOUNT SET BALANCE = BALANCE - 50.00 WHERE ACCT_ID = '0001234567'; INSERT INTO AUDIT_LOG (ACCT_ID, ACTION, AMOUNT) VALUES ('0001234567', 'WITHDRAW', 50.00);
Data Definition Language creates and maintains objects: tables, indexes, views, databases, table spaces, and more. Application developers may have limited DDL authority in production; DBAs often own CREATE/ALTER/DROP there. Still, every developer should read DDL to understand the model they query.
1234567CREATE TABLE EMPLOYEE ( EMP_ID CHAR(6) NOT NULL, EMP_NAME VARCHAR(40) NOT NULL, DEPT_ID CHAR(3), HIRE_DATE DATE, PRIMARY KEY (EMP_ID) );
Data Control Language centers on GRANT and REVOKE. Privileges control who can SELECT, UPDATE, or run packages, among other rights. Security design is a full topic; beginners should know that “SQL failed with authorization error” often means missing privilege, not a syntax typo.
COMMIT makes the current unit of work permanent. ROLLBACK undoes it. In CICS, syncpoint coordinates Db2 with other resources. In batch, commit frequency affects lock duration and restart design. SQL data changes and transaction control always travel together in real systems.
Static SQL is coded in the program, precompiled or coprocessed, and bound into a package or plan. Access paths are chosen at bind time (with some reoptimization options). Dynamic SQL builds statement text at run time and prepares it then. Both are valid. Mainframe COBOL systems historically favor static SQL for predictability; distributed applications often use dynamic SQL with parameter markers.
After executable SQL in a host program, check SQLCODE (and prefer SQLSTATE for portability where your standards say so). Zero means success. +100 often means no row found for a singleton SELECT or end of cursor data. Negative codes are errors. Ignoring SQLCODE is a common production defect.
Next lessons teach how to read syntax diagrams, how identifiers and qualified names work, and then literals, predicates, and operators. Later sections cover SELECT deeply, joins, subqueries, and DML patterns. Architecture pages explained where SQL runs; this page explains what SQL is. Together they form the foundation for every Db2 application lesson.
SQL is a polite way to talk to a giant spreadsheet keeper named Db2. Instead of walking through every drawer yourself, you say: “Please show me all the red toys” (SELECT), “Put this new toy in the box” (INSERT), “Change this label” (UPDATE), or “Throw away the broken ones” (DELETE). You can also say “Build a new labeled box” (CREATE TABLE). Db2 figures out how to do it; you say what you want. When you finish a game that changed two toys, you either keep both changes (COMMIT) or put them back (ROLLBACK).
1. What does SQL stand for in the Db2 context?
2. Which statement category includes SELECT?
3. CREATE TABLE belongs to which category?
4. What is a result table?
5. In embedded SQL on z/OS, where do you usually check success after a statement?