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

Lab-lecture-week-02(2)

The document covers SQL operators, including character strings, comparison operators, and logical operators, as well as their usage in querying data. It explains how to restrict and sort data using various operators like BETWEEN, IN, LIKE, and IS NULL, along with rules of precedence. Additionally, it introduces substitution variables for dynamic querying in SQL statements.

Uploaded by

jessie.tang1022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab-lecture-week-02(2)

The document covers SQL operators, including character strings, comparison operators, and logical operators, as well as their usage in querying data. It explains how to restrict and sort data using various operators like BETWEEN, IN, LIKE, and IS NULL, along with rules of precedence. Additionally, it introduces substitution variables for dynamic querying in SQL statements.

Uploaded by

jessie.tang1022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lab Week – 2

SQL Operators, Restricting


and Sorting Data
SQL operators
• In this session
– Character strings
– Comparison operators
– Rules of precedence
– Sorting and ordering
Character Strings and Dates
• Character strings and date values are
enclosed in single quotation marks.
• Character values are case sensitive and
date values are format sensitive.
• The default date format is DD-MON-YY.
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen' ;
Comparison Operators

Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> or != Not equal to


Using the Comparison Operators

SELECT last_name, salary


FROM employees
WHERE salary <= 3000 ;
Other Comparison Operators

Operator Meaning

BETWEEN Between two values (inclusive)


...AND...

IN(list) Match any of a list of values

LIKE Match a character pattern

IS NULL Is a null value


Using the BETWEEN Operator
• Use the BETWEEN operator to display
rows based on a range of values.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;

Lower limit Upper limit


Using the IN Operator
• Use the IN operator to test for values in a
list.
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201) ;
Using the LIKE Operator
• Use the LIKE operator to perform
wildcard searches of valid search string
values.
• Search conditions can contain either
literal characters or numbers.
• % denotes zero or many characters.
• _ denotes one character.
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%' ;
Using the LIKE Operator

– You can combine pattern-matching characters:


SELECT last_name
FROM employees
WHERE last_name LIKE '_o%' ;

– You can use the ESCAPE identifier to search for


the actual % and _ symbols.
Using the IS NULL Operator
•Test for nulls with the IS NULL operator.

SELECT last_name, manager_id


FROM employees
WHERE manager_id IS NULL ;
Logical Operators

Operator Meaning

AND Returns TRUE if both component


conditions are TRUE
OR Returns TRUE if either component
condition is TRUE

NOT Returns TRUE if the following


condition is FALSE
Using the AND Operator

AND requires both conditions to be true:


SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%' ;
Using the OR Operator

OR requires either condition to be true:


SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%' ;
Using the NOT Operator

SELECT last_name, job_id


FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Rules of Precedence
Order Evaluated Operator
1 Artimetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 NOT logical condition
7 AND logical condition
8 OR logical condition

Note: Override rules of precedence by using parentheses.


Rules of Precedence

SELECT last_name, job_id, salary


FROM employees
WHERE job_id = 'SA_REP' 1
OR job_id = 'AD_PRES'
AND salary > 15000;

SELECT last_name, job_id, salary


FROM employees
WHERE (job_id = 'SA_REP' 2
OR job_id = 'AD_PRES')
AND salary > 15000;
ORDER BY Clause
– Sort retrieved rows with the ORDER BY clause:
• ASC: ascending order, default
• DESC: descending order
– The ORDER BY clause comes last in the
SELECT statement:

SELECT last_name, job_id, department_id, hire_date


FROM employees
ORDER BY hire_date ;
Sorting in Descending Order
– Sorting in descending order:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ; 1
– Sorting by column alias:
SELECT employee_id, last_name, salary*12 annsal
FROM employees 2
ORDER BY annsal ;
– Sorting by multiple columns:
SELECT last_name, department_id, salary
FROM employees 3
ORDER BY department_id, salary DESC;
Substitution Variables

... salary = ? …
… department_id = ? …
... last_name = ? ...

I want
to query
different
values.
Substitution Variables

– Use iSQL*Plus substitution variables to:


• Temporarily store values with single-ampersand (&)
and double-ampersand (&&) substitution
– Use substitution variables to supplement the
following:
• WHERE conditions
• ORDER BY clauses
• Column expressions
• Table names
• Entire SELECT statements
Using the & Substitution Variable
•Use a variable prefixed with an
ampersand (&) to prompt the user for a
value:
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
Lab Activities
• Complete SQL lab exercise

You might also like