Restricting and Sorting Data
Database Design
Muhammad Rudyanto Arief
[email protected]
Objectives
After completing this lesson, you should be able to do the
following :
– Limit the rows that are retrieved by a query
– Sort the rows that are retrieved by a query
Limiting Rows Using a Selection
EMPLOYEES
“retrieve all employees in
department 90”
Limiting the Rows That Are Selected
Restrict the rows that are returned by using the
WHERE clause.
The WHERE clause follows the FROM clause.
SELECT * | { [DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
Using the WHERE Clause
SELECT id_nasabah, nama_nasabah, alamat_nasabah
FROM nasabah
WHERE id_nasabah = 3;
Character Strings and Dates
Character strings and date values are enclosed by single
quotation marks.
Character values are case-sensitive, and date values are
format-sensitive.
SELECT id_nasabah, nama_nasabah, alamat_nasabah
FROM nasabah
WHERE nama_nasabah = ‘Neneng Djaila’;
Comparison Conditions
Perbandingan Operator
Sama dengan =
Tidak sama dengan <>
Lebih besar dari >
Lebih kecil dari <
Lebih besar atau sama dengan >=
Lebih kecil atau sama dengan <=
Tidak kurang dari !<
Tidak lebih dari !>
Tidak sama dengan !=
Menyamakan pola karakter LIKE
Nilai Null IS NULL
Match any of a list values IN (set)
Between two values (inclusive) BETWEEN...AND...
Using Comparison Conditions
SELECT last_name, salary
FROM employees
WHERE salary <= 3000 ;
Using the BETWEEN Condition
Use the BETWEEN condition 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 Condition
Use the IN membership condition 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 Condition
Use the LIKE condition 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 Condition
You can combine pattern-matching characters.
You can use the ESCAPE identifier to search for the actual % and _
symbols.
SELECT last_name
FROM employees
WHERElast_name LIKE ‘_o%’;
Using the NULL Conditions
Test for nulls with the IS NULL operator.
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL;
Logical Conditions
Operator Meaning
NOT Returns TRUE if both component
conditions are true
AND Returns TRUE if either component
condition is true
OR 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
WHEREjob_id
NOT IN (‘IT_PROG’, ‘ST_CLERK’, ‘SA_REP’);
Rules of Precedence
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition
Rules of Precedence…
SELECT last_name, job_id, salary
FROM employees
WHERE job_id
OR job_id
= ‘SA_REP’
= ‘AD_PRES’
1
AND salary > 15000;
Rules of Precedence…
SELECT last_name, job_id, salary
2
FROM employees
WHERE (job_id = ‘SA_REP’
OR job_id = ‘AD_PRES’)
AND salary > 15000;
Using the 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
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, salary*12 annsal
FROM employees
ORDER BY annsal ; 2
Sorting …
Sorting by multiple columns:
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC; 3
Summary
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 conditions
Apply the logical AND, OR, and NOT operators
Use the ORDER BY clause to sort rows of output.