Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

MUHAMMAD HAMIZ MOHD RADZI

HR
At the end of this lesson, students should be able to:

 Limiting rows with:

 The WHERE clause


 The comparison conditions using =, <=, BETWEEN, IN, LIKE, and NULL conditions
 Logical conditions using AND, OR, and NOT operators

 Rules of precedence for operators in an expression


 Sorting rows using the ORDER BY clause
 Substitution variables

HR
 Imagine you want to know your friend’s name during the ice-breaking session and
suddenly your friend talk about his/her relationship with his/her partner. What
would you feel?
 Yes… that’s what we called as information overload.
 Sometimes, we just want certain things of data, but we were given all data in the
database.
 So, how can we limit data so that certain rows from database will be fetched?
 This is the job of WHERE clause.

HR
 In previous lab, we’ve learned about:
SELECT*|{[DISTINCT] column | expression alias]..}
FROM table;

 Now, we need to a clause if we want to fetch only certain rows from the table:
SELECT*|{[DISTINCT] column | expression alias]..}
FROM table;
[WHERE condition(s)];

 WHERE clause is the condition part that you want from the database.

 Condition must have these:


 Column name or expression
 Comparison Operator
 Value to be compared with

HR
 Now, select employee’s last name, salary their department in employees table:

SELECT last_name, salary, department_id


FROM employees;

 Here, you get all 107 rows from employees


table as you have no condition in your query.

 So, now, find the last name, salary and


department for all employees in department
90.

 Take a look here, you have a condition now,


which, you want the result for employees in
department 90 now.

HR
 So, here is the SQL to do it.

SELECT last_name, salary, department_id


FROM employees
3. Value
WHERE department_id = 90;

1. Column name
2. Comparison
operator

HR
CHARACTER AND DATES IN WHERE CLAUSE:

 If the column or value that you want to search is a character or a date, you need to
enclose the value with single quotation mark ‘ ’.
 Remember, characters are case sensitive and dates are format sensitive.
 The default date format is DD-MON-YYYY
 Now, try to find employees with last name equal to King.

SELECT last_name, salary, department_id


FROM employees
WHERE last_name = ‘King’;

HR
SELECT last_name, salary, department_id
FROM employees
WHERE last_name = ‘king’;
 Will this SQL give you any result? Justification?

 Now, try to find employees that was hired in 28-SEP-2005.

SELECT last_name, hire_date


FROM employees
WHERE hire_date = ’28-SEP-2005’;

HR
SELECT last_name, hire_date
FROM employees
WHERE hire_date = ’19-Feb-2005’;
 Will this SQL give you any result? Justification?

HR
 As you saw on the previous slides, the = sign can be used in the WHERE clause.
 In addition to the "equal to" operator (=), other comparison operators can be used
to compare one expression to another:

HR
DATA TYPE NUMBER CHARACTER DATE
/
OPERATOR

= SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date


FROM employees FROM employees FROM employees
WHERE salary = 5000; WHERE last_name = ‘King’; WHERE hire_date = ’19-FEB-
2005’;

> SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date
FROM employees FROM employees FROM employees
WHERE salary > 5000; WHERE last_name > ‘King’; WHERE hire_date > ’19-FEB-
2005’;

>= SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date
FROM employees FROM employees FROM employees
WHERE salary >= 5000; WHERE last_name >= ‘King’; WHERE hire_date >= ’19-FEB-
2005’;

HR
DATA TYPE NUMBER CHARACTER DATE
/
OPERATOR

< SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date
FROM employees FROM employees FROM employees
WHERE salary < 5000; WHERE last_name < ‘King’; WHERE hire_date < ’19-FEB-
2005’;

<= SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date
FROM employees FROM EMPLOYEES FROM employees
WHERE salary <= 5000; WHERE last_name <= ‘King’; WHERE hire_date <= ’19-FEB-
2005’;

!= SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date


FROM employees FROM employees FROM employees
WHERE salary != 5000; WHERE last_name != ‘King’; WHERE hire_date != ’19-FEB-
2005’;

HR
BETWEEN OPERATOR
 The BETWEEN...AND operator is used to select and display rows based on a range
of values.
 When used with the WHERE clause, the BETWEEN...AND condition will return a
range of values between and inclusive of the specified lower and upper limits.

NUMBER CHARACTER DATE

SELECT last_name, salary SELECT last_name, salary SELECT last_name, hire_date


FROM employees FROM employees FROM employees
WHERE salary BETWEEN 5000 AND WHERE last_name BETWEEN ‘King’ WHERE hire_date BETWEEN ’01-
7000; AND ’Zlotkey’; FEB-2005’ AND ‘28-FEB-2005;

HR
IN OPERATOR
 The IN operator can be used with any data type.
 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) ;

HR
 The following example returns a row from the EMPLOYEES table, for any
employee whose last name is included in the list of names in the WHERE clause:

SELECT employee_id, manager_id, department_id


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

 If characters or dates are used in the list, they must be enclosed with single
quotation marks ('').

 Note: The IN operator is internally evaluated by the Oracle server as a set of OR


conditions, such as a=value1 or a=value2 or a=value3.
 Therefore, using the IN operator has no performance benefits and is used only
for logical simplicity.

HR
LIKE OPERATOR

• A manager may know that an employee's last name starts with "S" but doesn't
know the employee's entire name.
• Fortunately, in SQL, the LIKE condition allows you to select rows that match either
characters, dates, or number patterns.
• Two symbols -- the (%) and the underscore (_) -- called wildcard characters, can be
used to construct a search string.
• The percent (%) symbol is used to represent any sequence of zero or more
characters.
• The underscore (_) symbol is used to represent a single character.

HR
STATEMENT SQL
Find employees that contains ‘a’ in their last SELECT last_name
name FROM employees
WHERE last_name LIKE ‘%a%’;

Find employees that starts their last name SELECT last_name


with ‘K’ FROM employees
WHERE last_name LIKE ‘K%’;

Find employees that ends their last name with SELECT last_name
‘s’ FROM employees
WHERE last_name LIKE ‘%s’;

Find employees that have ‘i’ as second SELECT last_name


character in their last name. FROM employees
WHERE last_name LIKE ‘_i%’;

HR
STATEMENT SQL
Find employees that were hired in 2005 SELECT last_name, hire_date
FROM employees
WHERE hire_date LIKE ‘%2005’;

Find employees that were hired in May 2005 SELECT last_name, hire_date
FROM employees
WHERE hire_date LIKE ‘%May-2005’;

HR
You can use the ESCAPE identifier to search for the actual % and _ symbols.

ESCAPE Identifier: HOT TOPIC

 When you need to have an exact match for the actual % and _ characters, use the
ESCAPE identifier. This option specifies what the escape character is. If you want
to search for strings that contain SA_, you can use the following SQL statement:

SELECT employee_id, last_name, job_id


FROM employees WHERE job_id LIKE '%SA\_%’
ESCAPE '\';

HR
IS NULL / IS NOT NULL

 The IS NULL condition tests for unavailable, unassigned, or unknown data.


 IS NOT NULL tests for data that is available in the database.
 In the example on the next slide, the WHERE clause is written to retrieve all the last
names of those employees who do not have a manager.

SELECT last_name, manager_id


FROM employees
WHERE manager_id IS NULL;

HR
 Now, retrieve all the last names of those employees who receives commission.

SELECT last_name, commission_pct


FROM employees
WHERE commission_pct IS NOT NULL;

HR
 In SQL, it is often desirable to be able to restrict the rows returned by a query based on
two or more conditions.
 As the manager of a fast food business, you may need to know the names of your staff
who are either cooks or order takers.
 You don't need or want the entire staff list, you just want a subset of it.
 Conditional operators such as AND, NOT, and OR make these types of requests easy to
do.
 A logical operator combines the results of two or more conditions to produce a single
result.
 A result is returned ONLY IF the overall result of the condition is true.

 AND  Returns TRUE if both conditions are true.


 OR  Returns TRUE if either condition is true.
 NOT  Returns TRUE if the condition is false.

HR
 AND requires both the component conditions to be true:

SELECT employee_id, last_name, job_id, salary


FROM employees
WHERE salary >= 10000
AND job_id LIKE '%MAN%' ;
 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.

HR
 OR requires either component condition to be true:
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%' ;

HR
NOT Operator
SELECT last_name, job_id
FROM employees
WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;

HR
Rules of Precedence

HR
SELECT last_name, job_id, salary SELECT last_name, job_id, salary
FROM employees FROM employees
WHERE job_id = 'SA_REP' WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES' OR job_id = 'AD_PRES')
AND salary > 15000; AND salary > 15000;

HR
 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;

HR
 Sort in descending order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;

HR
 Sort with column alias
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal ;

HR
 Sorting by using the column’s numeric position:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY 3;

HR
 Sorting by multiple columns:
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;

HR
 Sometimes, you want different value to be inserted in your condition.
 What if we want to search for employees with salary 7000. Then next one you want
to have any random value. So, you need to write multiple SQLs in order to do that.
SELECT last_name, salary
FROM employees What if this value is random?

WHERE salary = 7000;

 But, there is a substitution variable, that can do that for you.

HR
 Use substitution (replacement) 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

HR
 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 ;

HR
 Use single quotation marks for date and character values:
SELECT last_name, department_id, salary*12
FROM employees
WHERE job_id = '&job_title';

HR
SELECT employee_id, last_name, job_id,&column_name
FROM employees
WHERE &condition
ORDER BY &order_column ;

HR
 Use double ampersand (&&) if you want to reuse the variable value without
prompting the user each time. The user sees the prompt for the value only once :
SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name ;

HR
In this lesson, you should have learned how to:

 Use the WHERE clause to restrict rows of output:


 Use the comparison conditions
 Use the BETWEEN, IN, LIKE, and NULL operators
 Apply the logical AND, OR, and NOT operators
 Use the ORDER BY clause to sort rows of output:
 Use ampersand substitution to restrict and sort output at
run time

HR
 Oracle Academy, Database Programming with SQL

HR

You might also like