Limiting Retrieved Data-WHERE CLAUSE
Limiting Retrieved Data-WHERE CLAUSE
DATA SETS
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.
• 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.
• 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.
• 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.
• 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:
• 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.
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
• 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.
• 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’.
• 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.
• 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.
• 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
• 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
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.
19
THE END