0% found this document useful (0 votes)
8 views46 pages

Limiting Retrieved Data-WHERE CLAUSE

The document provides an overview of limiting and sorting retrieved data in Oracle SQL, including the use of the ORDER BY keyword for sorting results in ascending or descending order. It also covers logical operators, other operators, and functions for manipulating data, such as character, number, null handling, and date functions. Examples are provided to illustrate the usage of these SQL commands and functions.

Uploaded by

wamalaeddie2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views46 pages

Limiting Retrieved Data-WHERE CLAUSE

The document provides an overview of limiting and sorting retrieved data in Oracle SQL, including the use of the ORDER BY keyword for sorting results in ascending or descending order. It also covers logical operators, other operators, and functions for manipulating data, such as character, number, null handling, and date functions. Examples are provided to illustrate the usage of these SQL commands and functions.

Uploaded by

wamalaeddie2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

LIMITING AND SORTING RETRIEVED

DATA SETS

ORACLE SQL SERIES - Joy Tiko


www.tikojoy.com
SORTING RETRIEVED DATA

• Here we can sort the retrieved data either in ascending or descending


order.
• We use the ORDER BY keyword followed by the column we need to
base on for sorting.
• In Oracle by default results are sorted in ascending order.
• ORDER BY keyword must always be the last in any SELECT statement.
EXAMPLE 1

• SELECT first_name, last_name, salary FROM employees;


• Returns results of employee first and last name and their salary.
• Return salary in Ascending(ASC) or Descending(DESC) order.
• SELECT first_name, last_name, salary FROM employees ORDER BY
salary ASC;
• SELECT first_name, last_name, salary FROM employees ORDER BY
salary DESC;
EXAMPLE 2

• SELECT street_address, city FROM locations;

• SELECT street_address, city FROM locations ORDER BY city ASC;


• Returns cities in Ascending order.
• SELECT street_address, city FROM locations ORDER BY city DESC;
• Returns cities in Decending order.
SORT USING COLUMN ALIAS AND
COLUMS POSITIONS
• Renaming a specific column.
• SELECT first_name, last_name, AS surname, salary FROM employees
ORDER BY surname;

USE COLUMN POSITIONS


SELECT first_name, last_name, AS surname, salary FROM employees
ORDER BY 1;
SORT FOR NULL VALUES

Example.
• In Employees table there is a table called Commission_pct.
• SELECT first_name, last_name, commission_pct FROM employees;
• Returns all employees names and ther commission_pct and some don’t earn a commission known as the
( NULL Values).
• Execute a statement to return NULL values first.
• SELECT first_name, last_name, commission_pct FROM employees ORDER BY 3 NULLS FIRST;
• SELECT first_name, last_name, commission_pct FROM employees ORDER BY 3 NULLS LAST;
• COMMISSION_PCT IN DESENDING ORDER
• SELECT first_name, last_name, commission_pct FROM employees ORDER BY 3 DESC NULLS LAST;
Logical Operators
Logical operators are symbols or words used in programming and databases, like Oracle
SQL, to connect or modify conditions in a query. They help determine whether a
statement is true or false.
These include:-
• AND
• NOT
• OR
AND

• This operator evaluates to true if the conditions separated by AND are true. For
example, we need to return all employee records for employees whose salary is 10000
and the commission is null (don’t earn a commission).

SELECT first_name, last_name, salary FROM employees WHERE salary = 10000 AND
commission_pct IS NULL;
NOT
• This operator reverts the results in the brackets i.e. if the condition(s) is not true, the
operator shows a record. It produces the same results as the inequality operator. For
example, we need to return all employees whose salary is not 10000.

• SELECT first_name, last_name, salary FROM employees WHERE NOT(salary = 10000);


OR
• This operator evaluates to true if one of the conditions separated by OR
is true. For example, we need to return all employee records for
employees whose salary is 10000 or the commission is .3 and above.

• SELECT first_name, last_name, salary,commission_pct FROM employees


WHERE salary = 10000 OR commission_pct >= .3;
OTHER OPERATORS

• IN
• NOT IN
• BETWEEN
• LIKE
• IS NULL
• IS NOT NULL
• DISTINCT (UNIQUE)
IN
• This operator evaluates to true if the value exists in the list. It produces
the same results with = ANY. For example, we need to return all
employee records for employees whose salary is 10000, 9000 and
13000.

• SELECT first_name, last_name, salary FROM employees WHERE salary


IN (10000, 9000, 13000);
NOT IN

• This operator evaluates to true if the value does not exist in the list. It
produces the same results with != ALL. For example, we need to return
all employee records for employees whose salary is not 10000, 9000
and 13000.

• SELECT first_name, last_name, salary FROM employees WHERE salary


NOT IN (10000, 9000, 13000);
BETWEEN

• This operator tests for the range. Lets say we have two values x and y
and we’re testing values between the two, it evaluates to true if the
value is greater than or equal to x and lesser than or equal to y. For
example, we need to return all employee records for employees whose
salary is between 10000 and 15000.

• SELECT first_name, last_name, salary FROM employees WHERE salary


BETWEEN 10000 AND 15000;
LIKE

• This operator tests for pattern matching. It works with wildcards i.e. %
and _. Pattern-search character % is used to match any character and
any number of characters. Pattern-search _ is used to match a single
character. For example, to return all employees whose first_name starts
with letter K, we issue this command:

• SELECT first_name, last_name, salary FROM employees WHERE


first_name LIKE 'K%';
• Remember, data in the tables is case-sensitive. Therefore, when pattern matching;
ensure you use the right case in the quotes. For example, the command below returns no
records even if there are records whose first names start with letter K. We have to write K
and not k.
• SELECT first_name, last_name, salary FROM employees WHERE first_name LIKE 'k%’;

• Remember pattern-search character _ represents a single character. This command


returns employees whose first name has a u as the second character followed by other
character.
• SELECT first_name, last_name, salary FROM employees WHERE first_name LIKE '_u%';
IS NULL

• This operator evaluates to true if the value is null. For example, we


need to check for employees who are not assigned to a manager. The
manager_id column reflects the manager assigned to an employee.

• SELECT first_name, last_name, manager_id FROM employees WHERE


manager_id IS NULL;
IS NOT NULL

• This operator evaluates to true if the value is not null. For example, we
need to check for employees who are assigned to a manager. The
manager_id column reflects the manager assigned to an employee.

• SELECT first_name, last_name, manager_id FROM employees WHERE


manager_id IS NOT NULL;
DISTINCT (UNIQUE)

• This operator eliminates duplicate values i.e. each record must be


unique. Uniqueness of a record is based on a complete row of the
record and not the first column. We can use the UNIQUE keyword rather
than the DISTINCT. For example, to find unique department ids in the
employee table, we issue the following query:

• SELECT DISTINCT department_id FROM employees;


7. Functions—Single
RowSQL SERIES - Joy Tiko
ORACLE
www.tikojoy.com
SINGLE-ROW FUNCTIONS.

 A function is a program called upon whenever needed.

 We use functions to perform different tasks with the data.

 Single-row functions work on a single row of data.

 They are grouped into character functions [UPPER, LOWER, INITCAP, CONCAT, LENGTH, LPAD,
RPAD, LTRIM, RTRIM, SUBSTR, REPLACE], number functions [ABS, FLOOR, CEIL, ROUND], null
handling functions [NVL, NVL2], date functions [SYSDATE, MONTHS_BETWEEN, ADD_MONTH

17
CHARACTER FUNCTIONS
LOWER

• This function converts characters to lower case. It takes one argument


or parameter i.e. LOWER(x) where x is the character string to be
converted in lowercase. For example, when we check out data in the
email column of the employees table, email addresses are in
uppercase. This shouldn’t be like that. We need to have them in lower
case. And here is where our LOWER function comes handy. We take the
email column in the LOWER function to be the parameter. Here is the
code
• SELECT first_name, email, LOWER(email) FROM employees;
• SELECT first_name, email, LOWER(email) AS "NEW EMAIL" FROM
employees;
UPPER

• This function converts characters to upper case. It takes one argument


or parameter i.e. UPPER(x) where x is the character string to be
converted in uppercase. For example, we need to have all last names in
upper case. We take the last_name column as the parameter to be
worked on.

• SELECT UPPER(last_name) AS LAST_NAME, first_name FROM


employees;
INITCAP

• This function converts the first character of a character string to upper


case and the rest in lower case i.e. sentence case. It takes one
argument or parameter i.e. INITCAP(x) where x is the character string
to be converted in sentence case. For example, we need to convert all
job_ids in the jobs table to sentence case.

• SELECT job_id "CURRENT JOB_ID", INITCAP(job_id) "NEW JOB_ID" FROM


employees;
SUBSTR

• It takes three arguments i.e. SUBSTR(x, y {, z}). This function returns a


portion of a character string x that is z characters long starting at
position y. For example, lets return a portion of department names with
7 characters from position 4.

• SELECT department_name, SUBSTR(department_name, 4, 7) AS


PORTION FROM departments;
• Parameter z in SUBSTR(x, y {, z}) is optional. If not specified, the
function considers the remaining characters in x from position y to be
the z value. Let us modify our previous example to include this bit
(omitting parameter z. Remember it determines how long a returned
string will be).

• SELECT department_name, SUBSTR(department_name, 4, 7) "OLD


PORTION", SUBSTR(department_name, 4) "NEW PORTION" FROM
departments;
LPAD

• The function takes three arguments ie. LPAD(x, y {,z}). It returns


character string x that is expanded into y characters long using z to fill
in the remaining space on the left side of x in case x doesn’t make y
characters. If x is more than the specified characters in y, it is
truncated to y characters. Confused, right ? ; don’t worry. Let use an
example. Lets say we want all first_names to be 10 characters long. For
those that don’t have ten characters, # symbol will be used to fill in the
remaining space to make 10 characters.

• SELECT last_name, LPAD(last_name, 10, '#') PADDING FROM


employees;
RPAD

• Works the same way as the LPAD. However, this time the filling
happens at the right side of the x string. The example below adds a
* to the right side of the salary column for any salary that doesn’t make
it to 6 figures.

• SELECT last_name, RPAD(salary, 6, '*') SALARY FROM employees;


LTRIM

• It takes two parameters i.e. LTRIM (x {, y}). It returns x without leading


characters specified in y. In case no leading characters specified in y
appear in x, x is returned unchanged. The y parameter is optional and
if omitted, x will be returned unchanged. For example, lets use remove
‘Gr’ from all last names that start with letter G.

• SELECT last_name, LTRIM(last_name, 'Gr') FROM employees WHERE


last_name LIKE 'G%';
RTRIM

• It works the same way as LTRIM. However, this time the trimming
happens on the right side of the x value (removes the trailing
characters specified in y that appear in x. Lets remove ‘es’ from all last
names ending with ‘es’.

• SELECT last_name, RTRIM(last_name, 'es') FROM employees WHERE


last_name LIKE '%es';
LENGTH

• It takes one argument i.e. LENGTH (x). It returns the number of


characters in x. Let us get the number of characters for each
department name. Even a space is considered a character.

• SELECT department_name, LENGTH(department_name) CHARACTERS


FROM departments;
CONCAT

• It takes two arguments i.e. CONCAT(x, y). It joins x to y. If x is NULL, y is


returned and vice-versa. Add @gmail.com to all emails
• SELECT first_name, last_name, CONCAT(email, '@gmail.com') FROM
Employees;
• From the output, emails are joined with an upper case. We can include this lower
case by nesting the CONCAT function.

• SELECT first_name, last_name, LOWER(CONCAT(email, '@gmail.com’)) FROM


Employees;
• Can give it a column Alias “Email

• SELECT first_name, last_name, LOWER(CONCAT(email, '@gmail.com’)) AS


Emails FROM Employees;
REPLACE

• It takes three arguments i.e. REPLACE (x, y, z). It returns x with all
occurrences of y replaced with z. Let us replace all occurrences of es
with 4% in the department names that have ‘es’ in their names.

• SELECT first_name, last_name, REPLACE(first_name, 'e' ,'a') AS


NEW_FIRST_NAME FROM Employees;
NUMBER FUNCTIONS
ABS

• Takes a single value. Returns absolute (positive) values.


• SELECT –salary, ABS(-salary) FROM employees;
FLOOR

• Takes one parameter i.e FLOOR(x). It returns a whole number that is


lower or equal to x. For example, we need to get the floor of 32.8. To do
that, we have numbers: 32, 32.1, 32.2, … 32.8, 32.9, 33. So, the whole
number that is lower than 32.8 is 32.

• SELECT FLOOR(32.8) FROM dual;


CEIL

• Takes one parameter i.e CEIL(x). It returns a whole number that is


greater or equal to x. For example, we need to get the ceil of 32.8. To
do that, we have numbers: 32, 32.1, 32.2, … 32.8, 32.9, 33. So, the
whole number that is greater than 32.8 is 33.

• SELECT CEIL(32.8) FROM dual;


ROUND

• It takes two parameters i.e. ROUND(x, y). It returns x rounded to y


digits to the right of the decimal.

• SELECT ROUND(3200.8568, 2) ROUND FROM dual;


• NB : The dual table is a dummy table accessible to all oracle users
NULL HANDLING FUNCTIONS
NVL

• NVL stands for Null Value Logic. It handles the NULL values in case we
need to replace them with a value. It takes two arguments i.e. NVL(x,
y). It returns y if x is NULL. If x is not NULL, x is returned. For example,
there are some employees who don’t earn a commission. For those that
don’t earn commission, .4 should be given as new commision.
• SELECT first_name, commission_pct from employees;
• SELECT first_name, commission_pct, NVL(commission_pct, ‘.4’) AS
NEW_COMMISSION FROM employees;
NVL2

• It works like the NVL. It takes three arguments i.e. NVL(x, y, z). It
returns z if x is NULL and returns y if x is NOT NULL. For example, there
are some employees who don’t earn a commission. For those that don’t
earn commission, .4 should be given as new commission + 10 should
be given.

• SELECT first_name, commission_pct, NVL2(commission_pct,


commission_pct , .4+10) AS NEW_COMMISSION FROM employees;
DATE FUNCTIONS
SYSDATE

• It doesn’t take any parameter. It returns the current date and time for the operating
system of the computer the database resides. Works the same way as CURRENT_DATE
function

• SELECT SYSDATE FROM dual;

• Don’t worry that yours doesn’t look like mine. You can change the date format using
this command:
ALTER SESSION SET NLS_DATE_FORMAT = ‘DD-MON-YYY HH:MI:SS AM’; Then re-run the
SYSDATE function
SYSTIMESTAMP

• Takes no argument. It returns the current database date and time.

• SELECT SYSTIMESTAMP FROM dual;


MONTHS_BETWEEN

• It takes two arguments i.e. MONTHS_BETWEEN(x, y). It returns the


number of months that exist between y and x dates. Let us determine
the months that exist between the current date and the hire date in the
employees table.

• SELECT first_name, MONTHS_BETWEEN(SYSDATE, hire_date) FROM


employees;
8. Functions—
Group
ORACLE SQL SERIES - Joy Tiko
www.tikojoy.com
GROUP FUNCTIONS.

 Sometimes referred to as aggregate functions.

 They work on multiple rows and return a single value.

 They include MAX, MIN, COUNT, AVG, etc.

 We can use group functions to group data using GROUP BY keyword.

 When the group function columns are mixed with non-group function columns, we include all
the non-group function columns in the GROUP BY clause.

 To filter the grouped results, we use HAVING keyword.

19
THE END

You might also like