15
Fri, Nov
2 New Articles

TechTip: A Review of SQL Selection Predicates

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

SQL is a powerful tool for querying data from iSeries databases. Part of what makes SQL so powerful is its predicates, used to select certain rows or groups of rows. A predicate, according to IBM's documentation, "specifies a condition that is true, false, or unknown about a given row or group." In an SQL SELECT phrase, the predicate is what is usually coded with the WHERE and HAVING clauses.

So, what are the predicates? There are several, giving us a wide variety of ways to select or reject data.

The Basic Predicate

The basic predicate is a simple comparison of two values:

Expression1 rel-op Expression2

The relation operator (rel-op) is one of these:

  • = Equal to
  • <> Not equal to
  • < Less than
  • <= Less than or equal to
  • > Greater than
  • >= Greater than or equal to

The two expressions must be of the same data type. An expression can be a subselect that returns one row with one column.

The expressions are evaluated and then compared according to rel-op. The predicate will be true or false, unless one or both of the expressions evaluate to null; in this case, the predicate is unknown. Either expression can contain host variables.

The Quantified Predicate

The quantified predicate compares the value of an expression to each of a group of values from a subselect. It uses this form:

Expression1 rel-op quantifier (subselect)

The subselect selects a single column/value and returns any number of rows, including zero rows (an empty subselect). Expression1 is then compared to the values of all the rows, using a quantifier:

  • ALL--If the quantifier is ALL, the predicate is true if the comparisons between the expression and each of the values are all true. The predicate is also true if the subselect is empty.
  • ANY--The predicate is true if the comparison between the expression and at least one of the values is true. The predicate is false if the subselect is empty.

To better understand quantified predicates, consider the following simple quantified predicate:

where :VAR >= ALL (select FldA from MyFile)

Let's say the subselect returns a table of five rows: (2, 4, 6, 8, 10). We are using ALL. So in order for the predicate to be true, VAR must be greater than or equal to (>=) each value in the subselect's table. If VAR is 15, the predicate is true, because 15 is greater than or equal to 2, 4, 6, 8, and 10. If VAR is 9, then the predicate is false because it is not greater than or equal to 10 even though it is greater than or equal to 2, 4, 6, and 8. The comparison between VAR and the values does not evaluate to true for ALL the values.

Now consider this:

where :VAR >= ANY (select FldA from MyFile) 

In order for the predicate using ANY to be true, VAR must be greater than or equal to (>=) at least one of the values in the table. If VAR is 9, the predicate is true, since VAR is greater than or equal to four of the values. If VAR is 1, the predicate is false, because VAR is not greater than or equal to ANY of the values.

SOME can also be used as a quantifier; it is the same as ANY.

For an ALL predicate, if there are no false relationships and any of the values are null, the predicate is unknown. For ANY, if there are no true relationships and any of the values are null, the predicate is unknown.

The BETWEEN Predicate

This predicate compares an expression to a range of expression values.

Expression1 BETWEEN ExpressionA AND ExpressionB

The predicate is true if Expression1 is between ExpressionA and ExpressionB. In other words, Expression1 >= ExpressionA and Expression1 <= ExpressionB.

You can use NOT BETWEEN as well. In this case, the predicate is true if Expression1 is not within the range.

Here's an example:

where :VAR between 1 and 10

If VAR is 5, the predicate is true. If VAR is 15, the predicate is false.

where :VAR not between 1 and 10

If VAR is 5, the predicate is false. If VAR is 15, the predicate is true.

The DISTINCT Predicate

This predicate is similar to the basic predicate, with the following differences:

  • Only equality and inequality are determined.
  • Null values are accounted for, so this predicate will not be unknown.

Expression1 IS DISTINCT FROM Expression2

This predicate is true if Expression1 has a different value than Expression2. If one expression but not the other is null, the predicate is true; these are different values. If both expressions are null, the predicate is false; these are the same value.

Expression1 IS NOT DISTINCT FROM Expression2

This predicate is true if Expression1 has the same value as Expression2. If one expression but not the other is null, the predicate is false; these are different values. If both expressions are null, the predicate is true; these are the same value.

The EXISTS Predicate

This predicate simply tests to see if a subselect returns rows.

EXISTS (subselect)

If the subselect returns rows, the predicate is true. If the subselect returns zero rows, the predicate is false. The values of the rows don't matter.

You can use NOT EXISTS as well. In this case, the predicate is true if the subselect does not return rows.

This is a good predicate to use to select only rows from a table where matching rows exist in another table:

select * from FileA A
where exists (select * from FileB where KeyB = A.KeyA)

In this statement, rows in FileA are selected only if the value in field KeyA can be found in the field KeyB in FileB.

The IN Predicate

This predicate determines whether an expression can be found among a set list of values. There are two forms. The first form explicitly lists the set of values to check:

Expression1 IN (Value1, Value2, ... ValueN)

If Expression1 evaluates to any of the values in the set, the predicate is true.

The other form is a set produced by a subselect:

Expression1 IN (subselect)

The subselect selects a single column/value and returns any number of rows. If Expression1 is equal to any of the values in the result set, the predicate is true. This predicate is equivalent to the quantified predicate:

Expression1 = ANY (subselect)

You can use NOT IN as well. In this case, the predicate is true if Expression1 does not evaluate to any of the values in the set.

Here's an example:

where :VAR IN (2,4,6,8,10)

If VAR is 6, the predicate is true.

where :VAR NOT IN (2,4,6,8,10)

If VAR is 6, the predicate is false.

The LIKE Predicate

This predicate matches a string to a string pattern. This is the format:

Expression1 LIKE Pattern

If Expression1 matches the Pattern, the predicate is true.

You can use NOT LIKE as well. In this case, the predicate is true if Expression1 does not match the Pattern.

The string pattern has wildcard characters to define how the string should match:

  • _ The underscore represents one character. The character could be anything.
  • % The percent sign represents zero or more characters.
  • Any other character, including the space, represents itself.

Consider the string 'Donovan McNabb'. The following patterns would match:

  • 'D_n_v_n McN_bb'--Each underscore represents a character (the vowels in this case).
  • 'D%'--The letter D followed by zero or more characters.
  • 'D%M%'--The letter D followed by zero or more characters, followed by the letter M, followed by zero or more characters.
  • '% _%'--Zero or more characters followed by a space, followed by one character, followed by zero or more characters.
  • '%McNabb'--Zero or more characters followed by McNabb.
  • '%M_Nabb%'--Zero or more characters followed by the letter M, followed by any one character, followed by zero or more characters.

The following patterns would not match:

  • '%McNabb '--Why not? The string has no spaces at the end. This pattern has a space at the end. So the pattern is looking for a space at the end that isn't in the string.
  • '%M__Nabb%'--Why not? There is only one character between the M and Nabb, not two.
  • 'DONOVAN%'--Why not? The pattern is case-sensitive.
  • 'McNabb%'--Why not? There is no wildcard at the beginning of the pattern, and the string does not begin with McNabb.

What if you need to look for an underscore or a percent sign in the string? In other words, you can't use it as a wildcard because it is actually in the string? Then you use this form of the LIKE predicate:

Expression1 LIKE Pattern ESCAPE 'EscapeChar'

The EscapeChar is a single character used as an escape character. When you need to look for the existence of the underscore, the percent sign, or the escape character itself in the string, precede it with the escape character.

Consider the string 'Tom_Brady'. Assume your escape char is a backslash (). The pattern 'Tom_%' would mean Tom, followed by any single character, followed by zero or more characters. 'Tom_Brady' would match, but so would 'Tom Brady' or 'Tom*Brady'.

However, the string contains an actual underscore, and if you wanted to include that in your pattern, the pattern would be 'Tom_%'. Because of the escape character, the underscore becomes a literal underscore that has to exist in the string, instead of a wildcard. The pattern now would mean 'Tom', followed by the underscore, followed by zero or more characters. 'Tom_Brady' would match this pattern, but 'Tom Brady', and 'Tom*Brady' would not.

Remember, trailing spaces in a pattern are part of the pattern. If a string does not have the same trailing spaces, it will not match. This becomes an issue if you use a host variable for the pattern. The best way around it is to pad your host variable with the percent symbol (%). For example, a 10-character host variable VAR would contain 'Donovan%%%' or 'Tom%%%%%%%'.

The NULL Predicate

This predicate compares an expression to the null value.

Expression1 IS NULL

If Expression1 is null, the predicate is true.

You can use IS NOT NULL as well. In this case, the predicate is true if Expression1 is not null.

Combining Predicates

Predicates can be combined using AND and OR. The results of the combined predicates are shown here, from the truth table from IBM's documentation. P and Q are any predicates.

http://www.mcpressonline.com/articles/images/2002/Tip--SQL PredicatesV400--061705.png
(Click image to enlarge.)

Predicates are SQL's powerful means of selecting rows of data from your files. Hopefully, this review will help you to make use of them. Query away!

Doug Eckersley is the iSeries programmer with a premier homebuilder in Columbus. He has been programming on the iSeries for 10 years and has been in the business for 15. He is certified by IBM. He is also a long-suffering Philadelphia Eagles fan.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: