Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

----------------------------------------Oracle

Clauses----------------------------------------
-----------------------------------where
clause----------------------------------------------

The SQL WHERE clause is used to specify a condition while fetching the data from a
single table or by joining with multiple tables. If the given condition is
satisfied, then only it returns a specific value from the table. You should use the
WHERE clause to filter the records and fetching only the necessary records.

The WHERE clause is not only used in the SELECT statement, but it is also used in
the UPDATE, DELETE statement, etc., which we would examine in the subsequent
chapters.

Syntax
The basic syntax of the SELECT statement with the WHERE clause is as shown below.

SELECT column1, column2, columnN


FROM table_name
WHERE [condition]
You can specify a condition using the comparison or logical operators like >, <, =,
LIKE, NOT, etc. The following examples would make this concept clear.

Example- Extract ID, Name and Salary fields from the CUSTOMERS table, where the
salary is greater than 2000 −

SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE SALARY > 2000;

Example- Extract ID, Name and Salary fields from the CUSTOMERS table for a customer
with the name Hardik.

SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE NAME = 'Hardik';

-----------------------------------
DISTINCT----------------------------------------------------------------

Oracle DISTINCT clause is used to remove the duplicate records from the result set.
It is only used with SELECT statement.

Syntax:

SELECT DISTINCT expressions


FROM tables
WHERE conditions;
Parameters:
expressions:It specifies the columns that you want to retrieve.

tables: It specifies the table from where you want to retrieve records

example - SELECT DISTINCT JOB_ID FROM HR.EMPLOYEES

---------------------------------------------------FROM
Clause------------------------------------------------------------
FROM clause is a mandatory clause in SELECT expression. It specifies the tables
from which data is to be retrieved.

Syntax:

FROM table_name...

----------------------------------------------------ORDER BY
Clause----------------------------------------------------

In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result
set. The ORDER BY clause is only used with SELECT statement.

Syntax:

SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ ASC | DESC ];

Example- SELECT * FROM HR.EMPLOYEES


ORDER BY EMPLOYEE_ID DESC

Example- SELECT * FROM HR.EMPLOYEES


ORDER BY COMMISSION_PCT DESC NULLS LAST
----------------------------------------------------
AND----------------------------------------------------------------
In Oracle, AND is used in select, insert, delete or update statement for checking
two or more conditions.

Syntax
Where condition 1
AND condition 2
.
.
.
.
AND condition n

--------------------------------------------------
BETWEEN--------------------------------------------------------------------
In Oracle, BETWEEN is used to get the values from given range in select, insert,
delete or update statement

Syntax
Expression BETWEEN value1 AND value2;

SELECT * FROM HR.EMPLOYEES


WHERE DEPARTMENT_ID BETWEEN 50 AND 90

---------------------------------------------------Comparison
operators----------------------------------------------------

Comparison operator Description


= Equal
<> Not Equal
!= Not equal
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal

Example- SELECT * FROM HR.EMPLOYEES


WHERE DEPARTMENT_ID > 50

SELECT * FROM HR.EMPLOYEES


WHERE DEPARTMENT_ID <> 50

SELECT * FROM HR.EMPLOYEES


WHERE DEPARTMENT_ID != 50

--------------------------------------------------- IN
Operator-------------------------------------------------------------

ORACLE IN
In Oracle, In clause is used with SELECT, INSERT, UPDATE, or DELETE statement to
decrease the use of multiple OR conditions.

Syntax
expressions IN (value1, value2,.... value n)

Example- SELECT * FROM HR.EMPLOYEES


WHERE DEPARTMENT_ID IN (50,80)

------------------------------------IS NOT
NULL--------------------------------------------------------------------------

In oracle, IS NOT NULL is used to check not null values. It is used with select,
insert, update, and delete statements.

Syntax
expression IS NOT NULL

Note:
In IS NOT NULL, condition returns true values.
In NULL, condition returns false.

SELECT * FROM HR.EMPLOYEES


WHERE COMMISSION_PCT IS NOT NULL

------------------------------------
NULL-------------------------------------------------------------------------------
--

In oracle, IS NULL is used to check not null values. It is used with select,
insert, update, and delete statements.

SELECT * FROM HR.EMPLOYEES


WHERE COMMISSION_PCT IS NULL

------------------------------------NOT
condition-----------------------------------------------------------------------

In Oracle, NOT condition is used with SELECT, INSERT, UPDATE or DELETE statement.
It is also called NOT operator. It is used to negate the given condition.

Syntax
NOT condition

-------------------------------How can I create an Oracle table from another


table--------------------------------------
CREATE TABLE Employees
AS (SELECT *
FROM hr.employees WHERE 1=1);

You might also like