SQL 101: The Simplicity and Flexibility of the SELECT Statement

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

It's time to talk about the Data Manipulation Language (DML). Don't worry; it's not a new programming languagejust a fancy name to a subset of SQL instructions. I'll start with the SELECT statement.

 

Instead of starting by explaining how to build an SQL database from the ground up, let's start by discussing the things that you can use to query and change data in your DDS-defined physical files: a set of SQL instructions used to manipulate data, commonly referred to as the Data Manipulation Language, or DML. Entire books have been written about the DML; my objective here is to provide a helicopter view of the main DML instructions, accompanied by examples whenever possible.

 

Let's visit (or for some readers, re-visit) one of the most-used SQL instructions: the SELECT.

 

The Simplicity and Flexibility of the SELECT Statement

This is arguably the most-used SQL instruction. It can be used, in its simplest form, to retrieve all the records from a table with just four "words": SELECT * FROM <Table>. Naturally, this is not enough for most situations, so let's break down the SELECT statement and explain the different options at our disposal. From here on, I'll use a simplified inventory master file, named InvMst and described in the table below, to provide examples of the different scenarios.

 

The InvMst File Description

Column Name

Data Type

Length

Column Description

ItemID

Character

15,0

Item ID

LotNbr

Decimal

13,0

Lot Number

ExpDate

Date

N/A

Expiration Date

WHID

Decimal

8,0

Warehouse ID

ShelfID

Decimal

12,0

Shelf ID

ItemUn

Character

3

Item Units

ItemQty

Decimal

9,2

Item Quantity

 

This is a simplification of an actual inventory master file, so you'll see that a lot of columns are missing. That's not very important for the examples. What's important is to understand the file's structure: the first five columns provide the record's unique key; ItemID, LotNbr, WHID, and ShelfID are keys to other tables. In SQL lingo, they are this table's foreign keys. Because this is an inventory master file, this table has millions of rows and doing a simple SELECT is neither efficient nor useful.

 

Therefore, our simple four-word SELECT statement is basically useless here. To extract meaningful information from this table, I'll need to "ask" specific questions via a SELECT statement; let me show you how. The basic SELECT structure is the following:

 

SELECT <column 1>

       [, <column 2>, <column 3>…., <column n>

FROM   <table name>

[WHERE <condition 1> <logical operator> <condition 2>, etc]

 

The mandatory parts of a SELECT statement are the SELECT and FROM reserved words, the n column names or expressions (I'll explain this later), and a table name. The asterisk (*) you saw in the basic four-word statement means "all the columns of the table." Optionally, you can specify one or more conditions by using the WHERE reserved word to indicate that and by using logical operators, such as AND and"OR, just like you would do in an RPG IF statement. There are other optional components. If I wanted to know the quantity of item id 'A123' and its respective expiration date available in warehouse 333, I would write the following SELECT statement:

 

SELECT       ItemID

, ItemQty

             , ExpDate

FROM         InvMst

WHERE        ItemID = 'A123'

AND WHID = 333

 

Note that SQL is case-insensitive. I wrote the reserved words in caps just to remind you of the SELECT's structure. It's important to mention that the line breaks and indentations are purely optional; you can write the whole statement in one line, with blank spaces separating the different components. However, line breaks and indentation help improve readability, so it's a good idea to use them!

 

Having said that, let's analyze the statement: I'm retrieving the contents of three columns (ItemId, ItemQty, and ExpDate) from the InvMst table records that match the conditions specified after the WHERE reserved word. But there's more I can do with SELECT: for instance, if I wanted to sort the results by expiration date, I would specify the ORDER BY keyword, followed by the column name. The statement would be the same, with the addition of the ORDER BY keyword and the ExpDate column after it:

 

SELECT       ItemID

, ItemQty

             , ExpDate

FROM         InvMst

WHERE  ItemID = 'A123'

AND WHID = 333

ORDER BY ExpDate

 

Just like in the list of columns I specified after the SELECT, I can include multiple column names in the ORDER BY clause or I can indicate the position of the column instead. In this example, I could have written ORDER BY 3 because ExpDate is the third column in the SELECT. If I wanted the sort to occur in descending order, I would have used the DESC reserved word after ExpDate to indicate that.

 

The list of "things" in the SELECT clause doesn't necessarily have to consist of columns: you can use literal constants and expressions. These expressions can be nearly anything, from a simple "1 + 2" math operation to logical operations (1 = 0 returns a false result), SQL built-in functions, and even your functions, called Stored Procedures or User-Defined Functions (UDFs), depending on how they work, in SQL lingo.

Let's do a quick rundown of the operators at your disposal:

 

Arithmetic operatorsThe addition (+), subtraction (-), multiplication (*), and division (/) operators that you are used to from RPG are the same in SQL.

 

Comparison operators (or predicates)The usual greater than (>), less than (<), equal to (=), and not equal to (<>) operators are available; they keep the same functionality and combinations they have in RPG, such as >=, for instance, but you have additional operators available:

  • The BETWEEN operator is used to replace a field >= value1 and field <= value2 expression with the simpler field BETWEEN value1 AND value2 expression; if you add the NOT operator before this BETWEEN expression, you'll be making it equivalent to the field < value1 or field > value2 expression.
  • The LIKE operator searches for strings that have a certain pattern. If the pattern is found, a true value is returned. Here's a simple example:

SELECT ItemID

FROM         InvMst

WHERE        ItemID like 'A%'

This will select all item IDs that start with 'A', regardless of what follows the 'A' character. The underscore sign (_) represents any single character. The percent sign (%) represents a string of zero or more characters.

  • The IN operator is very useful to replace those field = value1 or field = value2 … or field = valueN expressions with a much more elegant field in (value1, value2, … valueN) expression. You can also prefix this operator with NOT, thus negating the expression.

This was probably nothing new for some readers, but an introduction to the SELECT statement was necessary to lay the foundation for more-complex queries. The next article will explain how you can use more than one table in a SELECT statement and the different ways to join those tables.

BLOG COMMENTS POWERED BY DISQUS