0% found this document useful (0 votes)
46 views

Module 3

This document provides an overview of a course on fundamentals of database management systems. It discusses restricting rows returned using a WHERE clause in SQL and sorting the order of rows returned. The WHERE clause allows filtering rows based on column values and operators like equals, not equals, greater than etc. BETWEEN can filter within a range of values.

Uploaded by

Arkim Dela cerna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Module 3

This document provides an overview of a course on fundamentals of database management systems. It discusses restricting rows returned using a WHERE clause in SQL and sorting the order of rows returned. The WHERE clause allows filtering rows based on column values and operators like equals, not equals, greater than etc. BETWEEN can filter within a range of values.

Uploaded by

Arkim Dela cerna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

SOUTHERN LEYTE STATE UNIVERSITY

COLLEGE OF COMPUTER STUDIES


AND INFORMATION TECHNOLOGY

Module Three: RESTRICTING AND SORTING DATA

COURSE OVERVIEW

Course No.

Course Code IT 207/IT 207L

Descriptive Title FUNDAMENTALS IN DATABASE MANAGEMENT SYSTEM

Credit Units 3 Units

School Year/Term 1st Semester, AY: 2020-2021

Mode of Delivery Online and Modular Learning

Name of Instructor/ Professor RENE C. RADAZA, MSIT


ALEX C. BACALLA, DIT
FRANCIS REY F. PADAO, MIM
ARMAN MASANGKAY
JUNE VIC W. CADAYONA

Course Description Developing and managing efficient and effective database


applications requires understanding the fundamentals of
database management systems, techniques for the design of
databases, and principles of database administration. This
course emphasized database concepts, developments, use and
management in three main sections: database concepts,
practice, and emerging trends. Relational database systems are
the main focus, but other types, including object-oriented
databases, are studied. Practical design of data-bases and
developing database applications using modern software tools
will be emphasized.

Understand the theories and techniques in developing


database applications and be able to demonstrate the
ability to build databases using enterprise DBMS products
such as Oracle or SQL Server.

Course Outcomes Knowledge (Think)


1. Analyze an existing database system with respect to
quality issues reliability, scalability, efficiency,
effectiveness and security.
Skills (Do)
2. Design a database based on user requirements using a
widely used modelling notation and be able to perform
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

declarative query language to elicit information.


Values (Feel)
1. Adapt a sound relational design principles in database to
achieve the formulated user requirement.

SLSU Vision A high quality corporate science and technology university

SLSU Mission Produces S and T leaders and competitive professionals;


generate breakthrough research in S and T based disciplines;
transform and improve the quality of life in the communities in
the service area; and be self –sufficient and financially viable.

MODULE GUIDE

In this module, we will


continue our exploration in
the world of Object-
Oriented Programming
(OOP). OOP-based
architecture proves to be
beneficial especially when
dealing with special types of
data which need to
be packaged into one entity.
What would you do if you
have so much
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

capability at your hand? Of


course, you will use it. This
ideology will be
covered in this module
together with the principles
that surround OOP. Hard
might it be at first, however
their important role in OOP’s
implementation
will be eviden
This module explains the SQL statements that you use to perform the actions listed in the slide.
When retrieving data from a database, you may need to do the following:
 Restrict the rows of data that are displayed
 Specify the order in which the rows are displayed

LEARNING PLAN

Intended Learning Outcome: Execute a basic SELECT statement, List the


capabilities of SQL SELECT statements.

INTRODUCTION

The essential capabilities of SELECT statement are Selection, Projection and Joining.
Displaying specific columns from a table is known as a project operation. We will now focus on
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

displaying specific rows of output. This is known as a select operation. Specific rows can be selected
by adding a WHERE clause to a SELECT query. As a matter of fact, the WHERE clause appears just
after the FROM clause in SELECT query hierarchy. The sequence has to be maintained in all
scenarios. If violated, Oracle raises an exception.

KEY TO REMEMBER

Throughout this course, you will see that the words keyword, clause, and statement are used as
follows:
 A keyword refers to an individual SQL element—for example, SELECT and FROM are
keywords.
 A clause is a part of a SQL statement—for example, SELECT employee_id, last_name,
and so on.
 A statement is a combination of two or more clauses—for example, SELECT * FROM
employees.

DISCUSSION

LESSON 1: LIMITING ROWS

I
n the example in the slide, assume that you want to display all the employees in department 90.
The rows with a value of 90 in the DEPARTMENT_ID column are the only ones that are returned.
This method of restriction is the basis of the WHERE clause in SQL.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can restrict the rows that are returned from the query by using the WHERE clause. A WHERE
clause contains a condition that must be met and it directly follows the FROM clause. If the
condition is true, the row meeting the condition is returned.

In the syntax:
Restricts the query to rows that meet a condition
Is composed of column names, constants, and a comparison
WHERE
operator. It specifies a combination of one or more expressions and
logical expression
Boolean operators, and returns a value of TRUE, FALSE, or
KNOWN.

The WHERE clause can compare literals and values in columns, arithmetic expressions, or
functions. It consists of three elements:
 Column name
 Comparison condition
 Column name, constant, or list of values
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

In the example, the SELECT statement retrieves the employee ID, last name, job ID, and
department number of all employees who are in department 90.

Note: You cannot use column alias in the WHERE clause.


SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You must enclose character strings and dates in the WHERE clause within single quotation marks
(''). Number constants, however, need not be enclosed within single quotation marks.

Remember that all character searches are case-sensitive. In the following example, observe that no
rows are returned because the EMPLOYEES table stores all the last names in mixed case:

SELECT last_name, job_id, department_id


FROM employees
WHERE last_name = 'WHALEN';

Oracle databases store dates in an internal numeric format, representing the century, year, month,
day, hours, minutes, and seconds. The default date display is in the DD-MON-RR format.

Note: For details about the RR format and about changing the default date format, see the lesson
titled “Using Single-Row Functions to Customize Output.” Also, you learn about the use of single-
row functions such as UPPER and LOWER to override case sensitivity in the same lesson.

You can use comparison operators in conditions that compare one expression with another value
or expression. They are used in the WHERE clause in the following format:

Syntax

... WHERE expr operator value


SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

Example

... WHERE hire_date = '01-JAN-05'


... WHERE salary >= 6000
... WHERE last_name = 'Smith'

Remember, an alias cannot be used in the WHERE clause.

Note: The symbols != and ^= can also represent the not equal to condition.

In the first example in the slide, the SELECT statement retrieves the last name and salary from the
EMPLOYEES table for any employee whose salary is less than or equal to $3,000. Observe that
there is an explicit value supplied to the WHERE clause. The explicit value of 3000 is compared to
the salary value in the SALARY column of the EMPLOYEES table.

In the second example, the SELECT statement retrieves all rows where the last name is Abel.
Because * is used in the SELECT statement, all fields from the EMPLOYEES table would appear in
the result set.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can display rows based on a range of values using the BETWEEN operator. The range that you
specify contains a lower limit and an upper limit.

The SELECT statement in the slide returns rows from the EMPLOYEES table for any employee
whose salary is between $2,500 and $3,500.

Values that are specified with the BETWEEN operator are inclusive. Remember, you must specify
the lower limit first.

You can also use the BETWEEN operator on character values:

SELECT last_name FROM employees


WHERE last_name BETWEEN ‘King’ AND ‘Whalen’;
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can use the IN operator to test for values among a specified set of values. The condition
defined using the IN operator is also known as the membership condition.

The example in the slide displays employee IDs, last names, salaries, and manager’s employee IDs
for all the employees whose manager’s employee ID is 100, 101, or 201.

Note: The set of values can be specified in any random order—for example, (201,100,101).
The IN operator can be used with any data type. The following example returns rows from the
EMPLOYEES table, for any employee whose last name is included with the IN operator:

SELECT employee_id, manager_id, department_id


FROM employees
WHERE last_name IN ('Hartstein', 'Vargas');

If characters or dates are used in a list, they must be enclosed within single quotation marks ('').
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You may not always know the exact value to search for. You can select rows that match a character
pattern by using the LIKE operator. The character pattern–matching operation is referred to as a
wildcard search. Two symbols can be used to construct the search string.

Symbol Description
% Represents any sequence of zero or more characters
_ Represents any single character

The SELECT statement in the slide returns the first name from the EMPLOYEES table for any
employee whose first name begins with the letter “S.” Note the uppercase “S.” Consequently, names
beginning with a lowercase “s” are not returned.

The LIKE operator can be used as a shortcut for some BETWEEN comparisons. The following
example displays the last names and hire dates of all employees who joined between January 2015
and December 2015:

SELECT last_name, hire_date


FROM employees
WHERE hire_date LIKE '%15';
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The % and _ symbols can be used in any combination with literal characters. The example in the
slide displays the names of all employees whose last names have the letter “o” as the second
character.

When you need to have an exact match for the actual % and _ characters, use the ESCAPE identifier.
For example, to find the last name and job ID of all the employees whose job ID contains 'SA_',
use the following statement:

SELECT last_name, job_id


FROM employees
WHERE job_id LIKE 'SA\_%' ESCAPE '\';
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

There are two types of NULL conditions:


 IS NULL tests for NULL values.
 IS NOT NULL tests for values that are not NULL.
A null value means that the value is unavailable, unassigned, unknown, or inapplicable. Therefore,
you cannot test with =, because a null cannot be equal or unequal to any value. The example in the
slide retrieves the last_name and manager_id of all employees who do not have a manager.

Here is another example: To display the last name, job ID, and commission for all employees who
are not entitled to receive a commission, use the following SQL statement:

SELECT last_name, job_id, commission_pct


FROM employees
WHERE commission_pct IS NULL;
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

A logical condition combines the results of two or more component conditions to produce a single
result based on those conditions, or it inverts the result of a single condition. A row is returned
only if the overall result of the condition is true.

Three logical operators are available in SQL:


 AND
 OR
 NOT
All the examples so far have specified only one condition in the WHERE clause. You can use several
conditions in a single WHERE clause using the AND and OR operators.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

In the example, both the component conditions must be true for any record to be selected.
Therefore, only those employees who have a job title that contains the string ‘MAN’ and earn
$10,000 or more are selected.

All character searches are case-sensitive, that is, no rows are returned if ‘MAN’ is not uppercase.
Further, character strings must be enclosed within quotation marks.

AND Truth Table

The following table shows the results of combining two expressions with AND:

AND TRUE FALSE NULL


TRUE TRUE FALSE NULL
FALSE FALSE FALSE FALSE
NULL NULL FALSE NULL
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

In the example, either component condition can be true for any record to be selected. Therefore,
any employee who has a job ID that contains the string ‘MAN’ or earns $10,000 or both is selected.

OR Truth Table

The following table shows the results of combining two expressions with OR:

OR TRUE FALSE NULL


TRUE TRUE TRUE TRUE
FALSE TRUE FALSE NULL
NULL TRUE NULL NULL
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The example in the slide displays the last name and job ID of all employees whose job ID is not
IT_PROG, ST_CLERK, or SA_REP.

NOT Truth Table

The following table shows the result of applying the NOT operator to a condition:

NOT TRUE FALSE NULL


FALSE TRUE TRUE/FALSE
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

LESSON 2: RULES OF PRECEDENCE FOR OPERATORS IN AN EXPRESSION

The rules of precedence determine the order in which expressions are evaluated and calculated.
The table in the slide lists the default order of precedence. However, you can override the default
order by using parentheses around the expressions that you want to calculate first.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

1. Precedence of the AND Operator: Example

In this example, there are two conditions:

 The first condition is that the department ID is 80 and the salary is greater than
$10,000.
 The second condition is that the department ID is 60.
Therefore, the SELECT statement reads as follows:

“Select the row if an employee’s department ID is 80 and earns more than $10,000,
or if the employee’s department ID is 60.”
2. Using Parentheses: Example

In this example, there are two conditions:

 The first condition is that the department ID is 80 or 60.


 The second condition is that the salary is greater than $10,000.
Therefore, the SELECT statement reads as follows:

“Select the row if an employee’s department ID is 80 or 60, and if the employee


earns more than $10,000.”

LESSON 3: SORTING ROWS USING THE ORDER BY CLAUSE


SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The order of rows that are returned in a query result is undefined. The ORDER BY clause can be
used to sort the rows. You can specify an expression, an alias, or a column position as the sort
condition.
You can specify multiple expressions in the ORDER BY clause. Oracle Database first sorts rows
based on their values for the first expression. Rows with the same value for the first expression are
then sorted based on their values for the second expression, and so on.

Syntax

SELECT expr
FROM
[WHERE table
[ORDER BY condition(s)] numeric_position} [ASC | DESC]];
{column, expr,

In the syntax:

ORDER BY Specifies the order in which the retrieved rows are displayed
ASC Orders the rows in ascending order (this is the default order)
DESC Orders the rows in descending order

If the ORDER BY clause is not used, the sort order is undefined, and the Oracle server may not fetch
rows in the same order for the same query twice. Use the ORDER BY clause to display the rows in a
specific order.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The default sort order is ascending. Let us look at the other characteristics:
 Numeric values are displayed with the lowest values first (for example, 1 to 999).
 Date values are displayed with the earliest value first (for example, 01-JAN-92 before
01-JAN-95).
 Character values are displayed in the alphabetical order (for example, “A” first and “Z” last).
 Null values are displayed last for ascending sequences and first for descending sequences.
 You can also sort by a column that is not in the SELECT list.
Examples

1. To reverse the order in which the rows are displayed, specify the DESC keyword after
the column name in the ORDER BY clause. The example in the slide sorts the result by
the department_id.
2. You can also use a column alias in the ORDER BY clause. The slide example sorts the
data by annual salary.
Note: Use the keywords NULLS FIRST or NULLS LAST to specify whether returned rows containing
null values should appear first or last in the ordering sequence.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

Examples
3. You can sort query results by specifying the numeric position of the column in the
SELECT clause. The example in the slide sorts the result by the department_id as this
column is at the third position in the SELECT clause.
4. You can sort query results by more than one column. You list the columns (or SELECT
list column sequence numbers) in the ORDER BY clause, delimited by commas. The
results are ordered by the first column, then the second, and so on for as many columns
as the ORDER BY clause includes. If you want any results sorted in descending order,
your ORDER BY clause must use the DESC keyword directly after the name or the
number of the relevant column. The result of the query example shown in the slide is
sorted by department_id in ascending order and also by salary in descending order.
LESSON 4: SQL ROW LIMITING CLAUSE IN A QUERY
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

Limiting the number of rows returned by a query can be valuable for reporting, analysis, data
browsing, and other tasks.

With the new SQL SELECT syntax, you can limit the number of rows that are returned in the result
set using the row_limiting_clause.

Queries that order data and then limit row output are widely used and are often referred to as
TopN queries. Top-N queries sort their result set and then return only the first n rows.

Note that there are certain limitations of the SQL row_limiting_clause:


 You cannot specify this clause with the for_update_clause.
 You cannot specify this clause in the subquery of a DELETE or UPDATE statement.
 If you specify this clause, then the SELECT list cannot contain the sequence pseudocolumns
CURRVAL or NEXTVAL.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You specify the row_limiting_clause in the SQL SELECT statement by placing it after the
ORDER BY clause. Note that an ORDER BY clause is required if you want to sort the rows for
consistency.
 OFFSET: Use this clause to specify the number of rows to skip before row limiting begins.
The value for offset must be a number. If you specify a negative number, offset is treated as
0. If you specify NULL or a number greater than or equal to the number of rows that are
returned by the query, 0 rows are returned.
 ROW | ROWS: Use these keywords interchangeably. They are provided for semantic clarity.
 FETCH: Use this clause to specify the number of rows or percentage of rows to return.
 FIRST | NEXT: Use these keywords interchangeably. They are provided for semantic clarity.
 row_count | percent PERCENT: Use row_count to specify the number of rows to return.
Use percent PERCENT to specify the percentage of the total number of selected rows to
return. The value for percent must be a number.
 ONLY | WITH TIES: Specify ONLY to return exactly the specified number of rows or
percentage of rows. Specify WITH TIES to return additional rows with the same sort key as
the last row fetched. You must specify the ORDER BY clause for additional rows to be
returned.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The first example in the slide returns the first five employees after sorting the rows in ascending
order of the employee_id.

The second example in the slide returns the next set of five employees after sorting the rows in
ascending order of the employee_id.

Note: If employee_id is assigned sequentially by the date when the employee joined the
organization, these examples give us the top 5 employees and then employees 6-10, all in terms of
seniority.

LESSON 5: SUBSTITUTION VARIABLES


SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

So far, all the SQL statements were executed with predetermined columns, conditions, and their
values. Suppose that you want a query that lists the employees with various jobs and not just those
whose job_ID is SA_REP. You can edit the WHERE clause to provide a different value each time you
run the command, but there is also an easier way.

By using a substitution variable in place of the exact values in the WHERE clause, you can run the
same query for different values.

You can create reports that prompt users to supply their own values to restrict the range of data
returned, by using substitution variables. You can embed substitution variables in a command file
or in a single SQL statement. A variable can be thought of as a container in which values are
temporarily stored. When the statement is run, the stored value is substituted.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can use single-ampersand (&) substitution variables to temporarily store values.
You can also predefine variables by using the DEFINE command. DEFINE creates and assigns a
value to a variable.

Restricted Ranges of Data: Examples


 Reporting figures only for the current quarter or specified date range
 Reporting on data relevant only to the user requesting the report
 Displaying personnel only within a given department
Other Interactive Effects

Interactive effects are not restricted to direct user interaction with the WHERE clause. You can also
use it for:

 Obtaining input values from a file rather than from a person


 Passing values from one SQL statement to another
Note: Both SQL Developer and SQL*Plus support substitution variables and the
DEFINE/UNDEFINE commands.

When running a report, users often want to restrict the data that is returned dynamically. SQL*Plus
and SQL Developer provide this flexibility with user variables. Use an ampersand (&) to identify
each variable in your SQL statement. However, you do not need to define the value of each variable.

Notation Description
Indicates a variable in a SQL statement; if the variable does
&user_variable not exist, SQL*Plus or SQL Developer prompts the user for a
value (the new variable is discarded after it is used.)
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The example in the slide creates a SQL Developer substitution variable for an employee number.
When the statement is executed, SQL Developer prompts the user for an employee number and
then displays the employee number, last name, salary, and department number for that employee.

With the single ampersand, the user is prompted every time the command is executed if the
variable does not exist.

When SQL Developer detects that the SQL statement contains an ampersand, you are prompted to
enter a value for the substitution variable that is named in the SQL statement.

After you enter a value and click the OK button, the results are displayed.

Note that if the substitution variable is referenced twice, even in the same command, then you are
prompted twice. Different values can be entered at each prompt.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

In a WHERE clause, you must enclose date and character values within single quotation marks. The
same rule applies to the substitution variables.

Enclose the variable with single quotation marks within the SQL statement itself.

The slide shows a query to retrieve the employee names, department numbers, and annual salaries
of all employees based on the job title value of the SQL Developer substitution variable.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can use the substitution variables not only in the WHERE clause of a SQL statement, but also as
substitution for column names, expressions, or text.

Example

The example in the slide displays the employee number, last name, job title, and any other column
that is specified by the user at run time, from the EMPLOYEES table. For each substitution variable
in the SELECT statement, you are prompted to enter a value, and then click OK to proceed.

If you do not enter a value for the substitution variable, you get an error when you execute the
preceding statement.

Note: A substitution variable can be used anywhere in the SELECT statement.

You can use the double-ampersand (&&) substitution variable if you want to reuse the variable
value without prompting the user each time. The user sees the prompt for the value only once.

In the example in the slide, the user is asked to give the value for the variable column_name only
once. The value that is supplied by the user (department_id) is used for both display and ordering
of data. If you run the query again, you will not be prompted for the value of the variable.

SQL Developer stores the value that is supplied by using the DEFINE command; it uses it again
whenever you reference the variable name. After a user variable is in place, you need to use the
UNDEFINE command to delete it:

UNDEFINE column_name;
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The example in the slide creates a SQL*Plus substitution variable for an employee number. When
the statement is executed, SQL*Plus prompts you for an employee number and then displays the
employee number, last name, salary, and department number for that employee.

LESSON 6: DEFINE AND VERIFY COMMANDS


SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

The example shown in the slide creates a substitution variable for an employee number by using
the DEFINE command. At run time, this displays the employee number, name, salary, and
department number for that employee.

Because the variable is created using the SQL Developer DEFINE command, the user is not
prompted to enter a value for the employee number. Instead, the variable value is automatically
substituted in the SELECT statement.

The EMPLOYEE_NUM substitution variable is present in the session until the user undefines it or
exits the SQL Developer session.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

You can use the VERIFY command to confirm the changes in the SQL statement. Setting SET
VERIFY ON forces SQL Developer to display the text of a command after it replaces substitution
variables with values.

To see the VERIFY output, you should use the Run Script (F5) icon in the SQL Worksheet. SQL
Developer displays the text of a command after it replaces substitution variables with values, on
the Script Output tab as shown in the slide.

The example in the slide displays the new value of the EMPLOYEE_ID column in the SQL statement
followed by the output.

SQL*Plus System Variables

SQL*Plus uses various system variables that control the working environment. One of the variables
is VERIFY. To obtain a complete list of all the system variables, you can issue the SHOW ALL
command on the SQL*Plus command prompt.

Summary:
Summary

In this lesson, you should have learned how to:


 Limit the rows that are retrieved by a query
 Sort the rows that are retrieved by a query
 Use ampersand substitution to restrict and sort output at run time
 restricting and sorting rows that are returned by the
SELECT statement.
 how to implement various operators and
conditions.
SOUTHERN LEYTE STATE UNIVERSITY
COLLEGE OF COMPUTER STUDIES
AND INFORMATION TECHNOLOGY

By using the substitution variables, you can add flexibility to your SQL statements. This enables
the queries to prompt for the filter condition for the rows during run time.

Post Test
References

1. Connolly, T.M, & Begg, C.E. (2005). Database systems: a practical approach to design,
implementation, and management. Pearson Education.
2. Elmasri, R., & Navathe, S. (2010). Fundamentals of Database systems. Addison-Wesley
Publishing Company.
3. Silberschatz, A. Korth, H.F., & Sudarshan, S. (1997) Database system concept (Vol. 4). New
York: McGraw-Hill.
4. MySQL Documentation. [Online]. Available: https://dev.mysql.com/doc. Retrieved on 2018,
August 6
5. MySQL Workbench [Online]. Available: https://www.msql.com/products/workbench/.
Retrieved on 2018, August 6
6. MySQL GUI Tools. [Online]. Available: https://downloads.mysql.com/archives/gui/.
Retrieved on 2018, August 6
7. XAMPP – Database Server. [Online]. Available:
https://www.apachefriends.org/index.html/. Retrieved on 2018, August 6
8. PHP Web Development. [Online]. Available: https://github.comredlinx/PHP-Web-
Development. Retrieved on 2018, August 6
9. PHP Online Documentation for MySQL Drivers and Plugins. [Online]. Available:
http://php.net/manualen/set.msqlinfo.php. Retrieved on 2018, August 6
10. http://www.oracle.com/technology/products/database/sql_developer/index.html
11. http://download.oracle.com/oll/tutorials/SQLDeveloper/index.htm

You might also like