SQL: The language of Db2

The language that you use to access the data in Db2 tables is the structured query language (SQL). SQL is a standardized language for defining and manipulating data in a relational database.

The SQL language consists of SQL statements.

By using SQL statements, you can accomplish activities in the following categories, among others:

  • Define, modify, or drop data objects such as tables by using various forms of CREATE, ALTER, and DROP statements. These statements are formally categorized as SQL schema statements, and sometimes informally as Data Definition Language (DDL) statements.
  • Retrieve, insert, update, or delete data in tables by using SELECT, INSERT, UPDATE, and MERGE statements. These statements are formally categorized as SQL data statements, and sometimes informally as Data Manipulation Language (DML) statements.
  • Start of changeAuthorize users to access specific resources such as tables or views by using various forms of GRANT, REVOKE, and TRANSFER OWNERSHIP statements. These statements are formally categorized as SQL schema statements, but they are also sometimes informally categorized as Data Control Language (DCL) statements.End of change

For a list of supported SQL statements by category, see Categories of SQL statements.

When you write an SQL statement, you specify what you want done, not how to do it. To access data for example, you need only to name the tables and columns that contain the data. You do not need to describe how to get to the data.

In accordance with the relational model of data, the database uses these principles:

  • The database is perceived as a set of tables.
  • Relationships are represented by values in tables.
  • Data is retrieved by using SQL to specify a result table that can be derived from one or more tables.

Db2 transforms each SQL statement, that is, the specification of a result table, into a sequence of operations that optimize data retrieval. This transformation occurs when the SQL statement is prepared. This transformation is also known as binding.

All executable SQL statements must be prepared before they can run. The result of preparation is the executable or operational form of the statement.

As the following example illustrates, SQL is generally intuitive.

Begin general-use programming interface information.


Assume that you are shopping for shoes and you want to know what shoe styles are available in size 8. The SQL query that you need to write is similar to the question that you might ask a salesperson, "What shoe styles are available in size 8?" Just as the salesperson checks the shoe inventory and returns with an answer, Db2 retrieves information from a table (SHOES) and returns a result table. The query looks like this:

Assume that the answer to your question is that two shoe styles are available in a size 8: loafers and sandals. The result table looks like this:

End general-use programming interface information.

SQL standards

Db2 for z/OS® is developed based on industry SQL standards. For more information, see Industry standards and Db2 for z/OS.

Syntax diagrams for SQL statements

For more information about the conventions for syntax diagrams in the Db2 product documentation, see How to read syntax diagrams.

Where to find PDF format and online content

The following table shows where to find the PDF format and online content from this section of the Db2 for z/OS product documentation.