Row Restriction
Content
1) Restricting Rows Using WHERE Clause
2) Restricting Rows Using Comparison Operators
3) Restricting Rows Using SQL Operators
4) Restricting Rows Using Logical Operators
5) LIKE Operator
6) Working with NULL data
7) Sorting Data
1
3RITechnologies
Row Restriction
Restricting Rows Using WHERE Clause
Get only those rows which satisfy specified condition
Syntax
SELECT columnname1, columnname2,……….
FROM tablename
WHERE condition;
Example
SELECT employee_id,First_name,job_id,department_id
FROM employees
WHERE department_id = 50;
2
3RITechnologies
Arithmetic Operators:
Used to perform arithmetic calculations on data.
Operator Meaning
+ Add
- subtract
* multiplication
/ division
SQL>select employee_name,salary,salary+1000
from employees;
3
3RITechnologies
Row Restriction
Restricting Rows Using Comparison
Operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> / != Not equal to
Example
SELECT employee_id,First_name,salary
FROM employees
WHERE Salary >= 6000;
4
3RITechnologies
Row Restriction
Logical Operators
Operator Meaning
AND Returns TRUE if both conditions are TRUE
OR Returns TRUE if either condition is TRUE
NOT Returns TRUE if the condition is FALSE
AND – Checks if both conditions are TRUE then only returns
TRUE
Example
SELECT Employee_id, First_name,job_id, salary
FROM employees
WHERE Salary>= 10000 AND job_id
5 = ‘SH_CLERK’; 3RITechnologies
Row Restriction
Logical Operators
OR – Checks if either of the condition is TRUE
Example
SELECT Employee_id, First_name,job_id, salary
FROM employees
WHERE Sal>= 1000 OR job = ‘SH_CLERK’;
6
3RITechnologies
Row Restriction
Logical Operators
NOT – Checks if condition is FALSE
Example
SELECT Employee_id, First_name,job_id,
salary FROM employees
WHERE NOT job = ‘SH_CLERK’;
7
3RITechnologies
Row Restriction
Restricting Rows Using SQL Operators
Operator Meaning
BETWEEN ……. AND Between two values
IN Match any of a list of values
LIKE Match a character pattern
IS NULL Is a NULL value
Example
SELECT employee_id,first_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
8
3RITechnologies
Row Restriction
Restricting Rows Using SQL Operators
IN Operator – returns the number of rows that are
matching in the list
Example
SELECT employee_id, first_name, salary, manager_id
FROM employees
WHERE manager_id IN (148, 149);
9
3RITechnologies
Row Restriction
Restricting Rows Using SQL Operators
LIKE Operator
- To search for a specified pattern in a particular
column
- Percent(%) represents multiple characters
- Underscore(_) for one single character
Example
SELECT employee_id, first_name
FROM employees
WHERE FIRST_NAME LIKE ‘S%’;
10
3RITechnologies
Row Restriction
Working with NULL Data
NULL Value
- Represents unknown piece of data
- Means value is not known
Comparing with NULL
- IS NULL Undefined
- IS NOT NULL something definded
Example
SELECT employee_id, first_name,commission_pct
FROM employees
WHERE commission_pct IS NULL;
11
3RITechnologies
Concatenation operator
Used to merge two or more columns. Represented by
symbol ‘ ||’.
Ex:
Select employee_id || salary from employees;
Literals:
Ex: select First_name ||’is a’||job_id from employees;
12
3RITechnologies
Distinct Keyword
To eliminate duplicate rows use distinct keyword in
select statement.
Ex:
1. select DISTINCT * from <table_name>
2. Select DISTINCT department_id from employees;
13
3RITechnologies
Row Restriction
Sorting Data
ORDER BY Clause
- Sorts output based on column specified
- Default order is ascending
- Add DESC if you want to sort in descending order
- You can sort by multiple columns
Example
SELECT first_name, job_id, salary
FROM employees
ORDER BY Salary
SELECT first_name, job_id, salary
FROM employees
ORDER BY Salary DESC
14
3RITechnologies
Summary
Restricting Rows Using WHERE Clause
Restricting Rows Using Comparison Operators
=, <, <=, >, >=,<>
Restricting Rows Using Logical Operators
AND, OR, NOT
Restricting Rows Using SQL Operators
BETWEEN….AND, IN, LIKE
Comparing data with NULL
IS NULL, IS NOT NULL
Sorting Data
ORDER BY, Default order Ascending, DESC for
descending. 15
3RITechnologies