SQL
SQL
Naming Rules
1.Must begin with a letter
2.Must be 1-30 characters long
3.Must contain only A-Z, a-z, 0-9, _, $ and #
4.Must not duplicate the name of another object owned
by the same user
5.Must not be an oracle reserved word
1
Data Types
Data Type Description
Syntax:
1
Creating Table
Example:
Table created.
1
The ALTER TABLE Statement
Drop a column
1
Syntax
1
Dropping a Table
1
Syntax
Example:
Table Dropped
Truncating the Table
1
Example:
Table Truncated
Renaming the table
To change the name of the table
Syntax:
Example:
1
SQL Data Manipulation Language (DML)
Syntax:
1
Example:
1 Row Created
Explicit Method:
1 Row Created.
1
Basic SELECT statement
1
Example using Basic SELECT
statement
SELECT *
FROM departments;
1
Arithmetic Expressions
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
1
Using Arithmetic Operators
1
Operator Precedence
1
Example for Operator
Precedence
1
Limiting the Rows Selected
Syntax:
1
Using the WHERE clause
SELECT employee_id,
FROM employees
WHERE department_id = 90;
1
Comparison Conditions
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Lesser than
<= Less than or equal to
<> Not equal to
1
Using Comparison Conditions
SELECT last_name, salary
FROM employees
WHERE salary <> 3000;
1
Comparison Conditions
Operator Meaning
1
Using BETWEEN, IN, LIKE, NULL Conditions
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3000;
SELECT first_name
FROM employees
WHERE first_name LIKE 's%';
1
Using AND, OR, NOT operators
SELECT last_name,job_id
FROM employees
WHERE job_id NOT LIKE
('IT_PROG','ST_CLERK',SA_REP');
1
ORDER BY Clause
1
Sorting in Descending Order
1
Group Functions
Types:
AVG
COUNT
MAX
MIN
SUM
1
GROUP BY Clause Syntax
1
Using GROUP BY clause
1
HAVING Clause Syntax
1
Using the HAVING Clause (Example)
1
SUBQUERIES
Types of Subqueries
1
SUBQUERY SYNTAX
SELECT select_list
FROM table
WHERE expr operator
( SELECT select_list
FROM table );
1
USING A SUBQUERY
Who has the salary greater than Abel's?
SELECT last_name
FROM employees
WHERE salary >
( SELECT salary
FROM employees
WHERE last_name = 'Abel');
1
Single-Row Subqueries
1
Executing Single-Row Subqueries
1
Multiple-Row Subqueries
Operator Meaning
1
USING The ANY Operator in Multiple-Row
Subqueries
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary < ANY
( SELECT salary
FROM employees
WHERE job_id = ' IT_PROG');
1
The UPDATE Statement Syntax
Modify existing rows with the UPDATE Statement.
Syntax:
Example:
UPDATE employees
SET department_id = 80
WHERE employee_id = 120;
1 row updated.
1
The DELETE Statement Syntax
Can remove the existing rows from a table by using
the DELETE statement
Syntax:
DELETE(FROM) table
[WHERE condition];
Example.
1 row deleted.
1
TCL
• COMMIT Statement - commit (make
persistent) all changes for the current
transaction
• ROLLBACK Statement - roll back (rescind)
all changes for the current transaction
DCL
• Data control commands in SQL allow you
to control access to data within the
database. These DCL commands are
normally used to create objects related to
user access and also control the
distribution of privileges among users.
Some data control commands are as
follows
COMMANDS IN DCL
– ALTER PASSWORD
– GRANT
– REVOKE
– CREATE SYNONYM
JOINS
✔ When data from more than one table in the database is required, a
join condition is used.
1
Types of JOINS
Oracle Proprietary Joins ( 8i and prior)
Equi Join
Non – Equi Join
Outer Join
Self Join
1
Equi join
Syntax:
SELECT T1.column,T2.column
FROM T1 , T2
WHERE T1.column = T2.column
1
Example:
1
Non-Equijoin
1
Example( Non Equi-join)
SELECT e.last_name, e.salary, j.gradelevel
FROM employees e ,job_grades j
WHERE e.salary
BETWEEN j.lowest sal AND j.highest sal;
1
Example of Inner Join
SELECT suppliers.supplier_id,
suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id =
orders.supplier_id;
1
USING Outerjoin
Example:
1
Functions
Functions are very powerful feature of SQL and can be
used to do the following;
1
1
1
1
Case Manipulation Function
Functions Results
1
1
1
NUMBER FUNCTONS
1
Working with DATES
➔ Oracle database stores dates in an internal numeric
format:
Century, year, month, day, hours, minutes, seconds
1
ARITHMETIC WITH DATAS
1
Using Arithmetic Operators With Dates
Output.
LAST_NAME WEEKS
king 744.56758
kochar 626.7878
1
1
1
Conversion Functions
Datatype conversion:
1
1
1
1
General Functions
These functions work with any data type and pertain
to using nulls.
1
NVL Function
Converts a NULL value to an Actual value.
- NVL (commission_pct, 0)
- NVL (hire_date, '01-JAN-97')
- NVL (job_id, 'No Job Yet')
1
USING NVL Function
SELECT last_name, salary, NVL(commission_pct,0),
(salary*12)+ (salary*12*(commission_pct,0))
FROM employees;
1
NVL2 Function
Syntax
1
USING NVL2 Function
1
NULLIF Function
Syntax
In the syntax:
expr1 is the source value compared to expr2
expr2 is the source value compared with expr1.
1
1
Thank You