SQL expression examples

The examples in this section all refer to a grid that contains columns from a table of employees.

Example 1

The expression >50000 in the Criteria row in the Salary column in the grid retrieves information for employees whose salaries are less than $50,000.

Figure 17-10: Expression criteria example using a relational operator

The sample shows the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp I D, Dept I D, and Salary. Displayed in the cell for Salary Criteria is the expression less than 50000.

The SELECT statement that PocketBuilder creates is:

SELECT emp_id, dept_id, salary
FROM employee
WHERE salary < 50000

Example 2

The expression >300 in the Criteria row in the EmpId column and the expression <50000 in the Criteria row in the Salary column in the grid retrieve information for employees whose employee IDs are greater than 300 and whose salaries are less than $50,000.

Figure 17-11: Expression criteria example using default logical operator

The sample shows the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp I D, Dept I D, and Salary. Displayed in the cell for Employee I D criteria is the expression greater than 300. Displayed in Salary Criteria is the expression less than 50000.

The SELECT statement that PocketBuilder creates is:

SELECT emp_id, dept_id, salary
FROM employee
WHERE emp_id >300 AND salary <50000

Example 3

The expressions 100 in the Criteria row and >300 in the Or row for the DeptId column, together with the expression <50000 in the Criteria row in the Salary column, retrieve information for employees who belong to:

Figure 17-12: Expression criteria example using two default logical operators

Shown is the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp I D, Dept I D, and Salary. For Department I D the Criteria row shows 100 and the Or row has the expression greater than 300. The criteria row for salary shows the expression less than 50000.

The SELECT statement that PocketBuilder creates is:

SELECT emp_id, dept_id, salary
FROM employee
WHERE (dept_id = 100 AND salary < 50000)
OR dept_id > 300

Example 4

The expression IN(100, 200, 500) in the Criteria row in the DeptId column in the grid retrieves information for employees who are in department 100 or 200 or 500.

Figure 17-13: Expression criteria example using the IN relational operator

Shown is the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp I D, Dept I D, and Salary. The criteria row for the department I D column displays the expression IN ( 100, 200, 500 ).

The SELECT statement that PocketBuilder creates is:

SELECT emp_id, dept_id, salary
FROM employee
WHERE dept_id IN (100, 200, 500)

Example 5

This example shows the use of the word AND in the Or criteria row. In the Criteria row, >=500 is in the EmpId column and >=30000 is in the Salary column. In the Or row, AND <=1000 is in the EmpId column and AND <=50000 is in the Salary column. These criteria retrieve information for employees who have employee IDs from 500 to 1000 and salaries from $30,000 to $50,000.

Figure 17-14: Expression criteria example overriding the default logical operators

Shown is the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp I D, Dept I D, and Salary. In the criteria row, the employee ID column shows the expression greater than or equal to 500, and Salary shows the expression greater than or equal to 30000. In the Or row, the Employee I D column shows the expression AND less than or equal to 1000, and the Salary column displays the expression AND less than or equal to 50000.

The SELECT statement that PocketBuilder creates is:

SELECT emp_id, dept_id, salary
FROM employee
WHERE (emp_id >= 500 AND emp_id <= 1000)
AND (salary >= 30000 AND salary <= 50000)

Example 6

In a grid with three columns: Emp Last Name, Emp First Name, and Salary, the expressions LIKE C% in the Criteria row and LIKE G% in the Or row in the Emp Last Name column retrieve information for employees who have last names that begin with C or G.

Figure 17-15: Expression criteria example using the LIKE relational operator

Shown is the grid from the bottom of the Quick Select page of the Data Window wizards. At left are four labels for the rows of the grid. They are column, sort, criteria, or. Three column names display: Emp Last Name, Emp First Name, and Salary. For the Emp Last Name column, the criteria row shows the expression LIKE C %, and the Or row shows the expression  LIKE G %.

The SELECT statement that PocketBuilder creates is:

SELECT emp_last_name, emp_first_name, salary
FROM employee
WHERE emp_last_name LIKE 'C%'
OR emp_last_name LIKE 'G%'

Providing SQL functionality to users

You can allow your users to specify selection criteria in a DataWindow object using these techniques during execution: