Professional Documents
Culture Documents
LAB 2 - Limit Rows Selected
LAB 2 - Limit Rows Selected
HR
At the end of this lesson, students should be able to:
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.
HR
Now, select employee’s last name, salary their department in employees table:
HR
So, here is the SQL to do it.
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.
HR
SELECT last_name, salary, department_id
FROM employees
WHERE last_name = ‘king’;
Will this SQL give you any result? Justification?
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’;
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’;
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.
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:
If characters or dates are used in the list, they must be enclosed with single
quotation marks ('').
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 ends their last name with SELECT last_name
‘s’ FROM employees
WHERE last_name LIKE ‘%s’;
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.
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:
HR
IS NULL / IS NOT NULL
HR
Now, retrieve all the last names of those employees who receives commission.
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.
HR
AND requires both the component conditions to be true:
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
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?
HR
Use substitution (replacement) variables to:
Temporarily store values with single-ampersand (&) and double-ampersand
(&&) substitution
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:
HR
Oracle Academy, Database Programming with SQL
HR