Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

SQL and XQuery tutorial for IBM DB2, Part 2: Basic queries

The fundamentals of SQL queries

Pat Moffatt (, Information Management Program Manager, IBM Academic Initiative, IBM
Pat Moffatt is the Information Management Program Manager for the IBM Academic Initiative. Through the Academic Initiative program, she ensures that appropriate Information Management resources are made available to help faculty integrate Information Management software into their curriculum. To learn more about this program, visit
Bruce Creighton (, Skills Segment Planner, IBM
Bruce Creighton is a Skills Segment Planner in the Information Management Education Planning and Development department. In this role, he plans investment in educational content and balances the investment between areas where IBM can attain revenue and those where the requirement for skills development are important enough to provide free education.
Jessica Cao, Training Tools Developer, IBM
Jessica Cao is an Arts and Science and Computer Science student at McMaster University. She expects to complete her combined honours degree in April 2009. Jessica is working in IBM Toronto lab's DB2 Information Management Skills Channel Planning and Enablement Program to take advantage of her interest in programming, editing, and writing.

Summary:  Through a series of simple examples, this tutorial illustrates how to retrieve data from an IBM® DB2® database with standard SQL SELECT statements. This tutorial describes how to retrieve rows from a relational database table, retrieve specific columns, retrieve specific rows, reform logical operations on retrieved data, and use wildcard characters in search conditions. This tutorial is Part 2 of the SQL & XQuery tutorial for IBM DB2 series.

View more content in this series

Date:  03 Aug 2006
Level:  Introductory PDF:  A4 and Letter (97 KB | 25 pages)Get Adobe® Reader®

Activity:  39943 views

Using the Percent Sign (%) wildcard


Which cities are in districts that begin with the letters Min?

Example query

		SELECT district, hq_city
		WHERE district LIKE 'Min%';



Using wildcard characters

Previous queries have expressed conditions that match complete character strings. With the LIKE predicate and the two wildcard characters, the percent sign (%) and the underscore (_), you can also express conditions that match a portion of a character string (a substring).

The percent (%) wildcard matches any character string. For example:

  • like 'TOT%' is true for any string that begins with 'TOT'.
  • like '%ZERO%' is true for any string that contains the text 'ZERO'.
  • like '%FRESH' is true for any string that ends with 'FRESH' and does not contain trailing blanks. Trailing blanks in character data are deemed significant when LIKE constraints are applied.

The percent sign (%) can also be used to search for a null character string--zero (0) characters.

The underscore wildcard (_) matches any one character in a fixed position. For example:

  • like '_EE_' is true for any four-letter string whose two middle characters are 'EE'.
  • like '%LE_N%' is true for any string that contains the pattern 'LE_N'. The strings 'CLEAN', 'KLEEN', and 'VERY KLEEN' all match this pattern.

About the query

The example query retrieves the names of all districts that begin with the characters 'Min' and lists the cities in these districts. The wildcard percent sign (%) allows for any character combination (including blank spaces) after the 'n' in 'Min', but characters that precede the 'n' must match the character pattern exactly as stored.

Usage notes

A LIKE condition is true when its pattern matches a substring in a column. If the pattern contains no wildcard characters, the pattern must match the column entry exactly.

For example, the following condition is true only when the column entry contains the character string APRIL and nothing else:

       month LIKE 'APRIL'

In other words, this condition is equivalent to:

       month = 'APRIL'

The LIKE predicate can be used only on columns that contain character strings.

10 of 15 | Previous | Next


Zone=Information Management, XML
TutorialTitle=SQL and XQuery tutorial for IBM DB2, Part 2: Basic queries