0% found this document useful (0 votes)
12 views63 pages

Oracle 715am

Uploaded by

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

Oracle 715am

Uploaded by

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

Thursday, March 6, 2025 8:55 AM

Notes link: Admin Mounika (Only whatsapp )


bit.ly/oracledbnotes 7337313426

oracle software link:


bit.ly/oracle21csoftware

oracle software installation video link:


bit.ly/oracle21cinstallation

Oracle SQL & PLSQL @ 7:15 AM IST by Mr Shiva Chaitanya sir


Day-1 https://youtu.be/ZS48xauc34o
Day-2 https://youtu.be/1uqtxBq0NqA
Day-3 https://youtu.be/DP33wadQRd0

oracle 715am Page 1


ORACLE
Wednesday, March 5, 2025 7:25 AM

Data Store
Database
DBMS
RDBMS
Metadata

RUN or ANALYZE

BANK

Branches Opens account


Customers Deposits amount RUN
Transactions Withdraws amount
Products Transfers funds
Creditcards
.
. In 2023 ? ANALYZE
In 2024 ?

Amazon online shopping

Products
Places order RUN
Customers
Delivers products
Sellers
Orders
Wishlist
ANALYZE
. In 2023 ?
. In 2024 ?

GOAL:
Storing business data permanently.

oracle 715am Page 2


In java:

Variable:
• Variable is used to store the data.
• Variable is temporary.
x
int x;
30
x=30;

Object:
• Object is used to store the data.
• Object is temporary.

Employee e1 = new Employee(1234,"RAJU",8000);

e1

EMPID ENAME SAL


1234 RAJU 8000

File • It is permanent
Database • It is permanent
[best way]

File Database
• It is suitable for single user. • It is suitable for multiple users

• We can store small amounts of • We can store large amounts oof


data. data.

• Less security. • More security.

Data Store:
• Data Store is a location where data is available.

oracle 715am Page 3


Examples:
BOOKS, FILES, DATABASES

Database:
• Database is a kind of data store.
• Database is a location where organization's
business data stored permanently.

Examples:

COLLEGE DB BANK DB

COURSES BRANCHES
STUDENTS CUSTOMERS
MARKS TRANSACTIONS
FEE .
. .
.

• Database contains interrelated data


in an organized form.

BANK DB
TABLE Customers
TABLE Employee

Custid Cname Ccity PAN ACNO


EMPID ENAME SAL
1234 KIRAN 15000
1235 SAI 12000

Transactions Branches
TXNI T_DATE_TIM T_TYPE ACN IFSC_CODE CITY STATE COUNTRY
D E O

DBMS:

oracle 715am Page 4


• DBMS => DataBase Management System / Software.
• DBMS is a software.
• It is used to create and maintain the database.

Evolution of DBMS:

Before 1960s Books


In 1960s FMS => File Management Software
In 1970s HDBMS => Hierarchical DBMS
NDBMS => Network DBMS
In 1976 E.F. Codd => RDBMS Concept
ORACLE company Founder Larry Ellison
In 1977 Larry Ellison estd a company:
Software Development Laboratories
In 1979 Renamed company:
Relational Software Inc.

Introduced ORACLE s/w

In 1983 Renamed company:


ORACLE carp.

RDBMS:

• RDBMS => Relational DataBase Management System / Software.

• RDBMS is a software that is used to create and maintain the


database in the form of tables.

Examples:
ORACLE, SQL SERVER, DB2, Postgre SQL, MY SQL

ORACLE Is a product of ORACLE company


SQL SERVER MICROSOFT
DB2 IBM
Postgre SQL Postgre Forum [a group of companies]
MY SQL SUN MICRO SYSTEMS [ORACLE]

oracle 715am Page 5


ORACLE
ORACLE DB SERVER

SQL => QUERIES => ORACLE


PL/SQL => PROGRAMS =>

Duration: 2 and half months


Mon to sat
Sundays are holidays

Class duration => 1and half hour

7.20 to 8.50

Google classroom => upload notes

ORACLE
SQL
Metadata PL/SQL

ORACLE SQL SERVER


SQL
SQL T-SQL
PL/SQL

oracle 715am Page 6


Thursday, March 6, 2025 7:24 AM

Data Store Is a location => data


Examples:
Book, File, database

Database Data store => loc => org bus data stored permanently

DBMS Software => to create and maintain the database

RDBMS DBMS => Software => to create and maintain the


database in the form of tables

Examples:
ORACLE, SQL SERVER, DB2, …..

TABLE:
• TABLE is a collection of rows and columns.

Example:
Table / Relation / Entity

EMPLOYEE
EMPID ENAME SAL Column / Field / Attribute / Property
1234 KIRAN 20000
1235 SAI 15000 Row / Record / Tuple / Entity Instance

Metadata:
• Metadata is the data about the data.
• It tells about the data.
• It can be also called as "Data Definition".

Examples:
Column name, Table name, data type, field size

EMPLOYEE
oracle 715am Page 7
EMPLOYEE
EMPID ENAME SAL
Number(4)
1234 KIRAN 20000
RAJU error
25-DEC-24 error
123
123456 ERROR

oracle 715am Page 8


Syllabus
Thursday, March 6, 2025 7:48 AM

ORACLE

Module-1: TABLES [SQL]


Module-2: PL/SQL
Module-3: Other DB Objects [SQL]

Module-1: Tables [SQL]

SQL commands DDL, DRL, DML, TCL, DCL


Built-in functions String functions, Number, date,
analytic, aggregate …
Clauses Group By, Order By, Having, Offset, Fetch,
….
Joins Inner Joins => Equi, Non-equi
Outer => left outer, right, full
Self
cross
Sub queries Non-correlated
single row sq, multi row, inline, scalar
Correlated
Constraints PRIMARY KEY, NOT NULL, UNIQUE,
FOREIGN KEY, …
Set operators Union, Union all, intersect, minus

Module-2: PL/SQL

PL/SQL Basics Data types, declare, assign, print, read,


Using sql commands in pl/sql
Control Structures Conditional, Looping, Jumping
CURSORS Steps,
Implicit cursor,
Explicit Cursor => Simple cursor, Ref
.
.
COLLECTIONS Associative array, Nested table, v-array
Bulk Bind, Bulk Collect
oracle 715am Page 9
Bulk Bind, Bulk Collect
Exception Handling built-in exceptions, user-defined exceptions
Pragma exception_init()
Stored procedures
Stored functions
Packages
Triggers
Working with LOBS BFILE, BLOB
Dynamic SQL

Module-3: Other DB Objects [SQL]

Views Simple view, Complex view


Indexes B-tree Index => simple, composite, function-based,
unique
Bitmap Index
Sequences Sequence, identity
Materialized views Refreshing
Synonyms

SQL DEVELOPER

oracle 715am Page 10


ORACLE
Thursday, March 6, 2025 8:07 AM

ORACLE:

• ORACLE is a Relational DataBase Management


Software [RDBMS].

• Relation => Table

• It is used to create and maintain the database in the


form of tables.

• Using this, we can store, manipulate and retrieve


the data of database.

Manipulate => 3 actions => insert / update / delete

Emp joined => INSERT


Emp sal increased => UPDATE
Emp left from org => DELETE

Retrieve => getting back => opening existing data

Checking the balance


Transaction statement
Searching for products

• In 1979, ORACLE 2nd version introduced.


They didn't 1st version.

• Latest version is: ORACLE 23ai

oracle 715am Page 11


ORACLE => RDBMS
SQL DATABASE
TABLES
PL/SQL ROWS & COLS

To communicate with ORACLE DB we


can use 2 languages. They are:
• SQL
• PL/SQL

SQL:

• SQL => Structured Query Language.


• SQL is a query language.
• In SQL, we develop the queries to communicate
with ORACLE DB.

• Query => request / command / instruction.


• Query is request that is sent to DB SERVER.

• SQL is a Non-Procedural Language [no programs].


It means, in SQL, we will develop any set of
statements or programs. Just we develop the
queries.

• SQL is Unified Language.


It is a common language to work with many
relational databases.

ORACLE SQL SERVER DB2


DATABASE DATABASE Database
TABLES TABLES Tables

oracle 715am Page 12


DATABASE DATABASE Database
TABLES TABLES Tables

SQL SQL SQL

• SQL provides commands.


• SQL provides functions.

Notes link:

bit.ly/oracledbnotes

oracle 715am Page 13


Thursday, March 6, 2025 8:08 AM

BANK DB

Branches Table
IFSC_CODE CITY STATE

Customers
Custid Cname Aadhar PAN .. ..

Transactions
Txn_Id T_type T_date_time Acno Amount

Employees
Empid Ename Job Sal

.
.

oracle 715am Page 14


SQL Commands
Friday, March 7, 2025 7:26 AM

ORACLE => RDBMS


Queries [reqs]
SQL DATABASE
TABLES
PL/SQL rows and cols
Programs

SQL provides 5 sub languages. They are:


• DDL
• DRL / DQL
• DML
• TCL
• DCL / ACL

DDL Create
Alter
• DDL => Data Definition Language
• Data Definition => Metadata Drop
Flashback [oracle 10g]
• It deals with metadata Purge [oracle 10g]

Truncate

Rename

DRL / DQL Select

• DRL => Data Retrieval Language


• DQL => Data Query Language

Retrieve => opening existing data


oracle 715am Page 15
• Retrieve => opening existing data
[getting back]

• It deals with data retrievals.

DML Insert
Update
• Data Manipulation Language Delete

• Manipulation => 3 actions Insert All [oracle 9i]


Insert / update / delete Merge [oracle 9i]

• It deals with data manipulations

TCL Commit
Rollback
• Transaction Control Language Savepoint

• It deals with transactions.

DCL / ACL Grant


Revoke
• Data Control Language
• Accessing Control Language

• It deals with data accessibility

oracle 715am Page 16


CREATE
Friday, March 7, 2025 7:48 AM

CREATE:

• CREATE command is used to create oracle database


objects like tables, views, indexes … etc

ORACLE DB OBJECTS:

TABLES
VIEWS
INDEXES
SEQUENCES
MATERIALIZED VIEWS
SYNONYMS

PROCEDURES
FUNCTIONS
PACKAGES
TRIGGERS

Syntax to create the Table:

CREATE TABLE <table_name>


(
<column_name> <data_type> [, [ ] Optional
<column_name> <data_type> , <> Any
.
.]
);

Till ORACLE 21c A table can have max of 1000 columns only
In ORACLE 23ai A table can have max of 4096 columns

oracle 715am Page 17


Data Types in ORACLE SQL:

Data Type tells,


• How much memory should be allocated
• Which type of data should be accepted

ORACLE SQL provides following data types:

Character related Char(n)


Varchar2(n)
• These are used to hold Long
Strings CLOB
• String => a group of chars
nChar(n)
Examples: nVarchar2(n)
'RAJU' , 'INDIA', 'HYD' nCLOB

Integer related Number(p)


Integer
• These are used to hold Int
integers.

• Integer => num without


decimal places

Examples:
1234, 78, 567, 17

Floating point related Number(p, s)


Float
• These are used to hold float Binary_float
values. Binary_double

• Float => num with decimal


places

Examples:
20000.00, 5000.80, 67.89

oracle 715am Page 18


20000.00, 5000.80, 67.89

Date and time related Date


Timestamp [oracle 9i]
• These are used to hold date or
time values.

Examples:
17-FEB-24
6-MAR-25 10.30.15.123456 AM

Binary related BFILE


BLOB
• These are used to maintain
multimedia objects like
Images, audios, videos,
animations, documents, ..

Character Related data types:


All character related data types can accept letters, digits and
special chars

Char(n):
• n => max num of chars
• It is used to hold strings.
• It is fixed length data type.
• It is used to hold fixed length chars.
• Extra memory will be filled with spaces.
• Default size: 1
• Max memory: 2000 Bytes [2000 chars]

Varchar2(n):
• n => max num of chars
• It is used to hold strings.
• It is variable length data type.
• It is used to hold variable length chars.
• Extra memory will be removed.
Default size: no default size

oracle 715am Page 19


• Default size: no default size
• Max memory: 4000 bytes [4000 chars]

T1
F1 CHAR(10) F2 VARCHAR2(10)
10 RAJU6spaces RAJU 4
10 NARESH4spaces NARESH 6

Fixed Variable
length length

Examples:

Country_Code CHAR(3) ENAME VARCHAR2(10)


---------------------- --------------
IND RAJU
AUS NARESH
WIN

PAN_CARD_NUMBER CHAR(10) Mail_id VARCHAR2(30)


-------------------------------- ------------------
ABCDE1234F [email protected]
[email protected]

CREATE TABLE t10 CREATE TABLE t11


( (
F1 CHAR F1 VARCHAR2
); );
Output: Output:
Table created ERROR

CHAR = CHAR(1) Default size: no default size

Default size => 1

oracle 715am Page 20


NOTE:
• VARCHAR2 data type can hold max of 4000 chars.
To hold more than 4000 chars we can use
LONG
CLOB => best

LONG:
• It is used to hold large amounts of chars.
• It is variable length data type.
• It has some restrictions:
○ A table can have only 1 column as LONG type
○ We cannot use built-in functions on LONG type
• Max memory: 2 GB

CLOB:
• CLOB => Character Large Object.
• It is used to hold large amounts of chars.
• It is variable length data type.
• A table can have multiple columns as CLOB type.
• We can use built-in functions on CLOB type.
• Ma memory: 4 GB

Examples:
Feedback CLOB
Product_features CLOB
Experience_summary CLOB

Char(n) • ASCII code char data types.


Varchar2(n) • Single byte char data types.
LONG • English only.
CLOB

oracle 715am Page 21


nChar(n) • UNI code char data types.
nVarchar2(n) • Multi Byte char data types.
nCLOB • English + other lang chars.

n => national

Nchar(n) • It is fixed length data type


• Max memory: 2000 Bytes [1000 chars]

Nvarchar2(n) • It is variable length data type


• Max memory: 4000 Bytes [2000 chars]

nCLOB • It is used to hold more than 2000 chars

In C:
char ch='A'; // 1Byte ASCII code char data type

In Java:
char ch='A'; // 2Bytes UNI code char data type

ASCII:
• It is a coding system.
• 256 chars coded.
• Ranges from 0 to 255.
• ASCII = English letters + digits + special chars
• 255 => 1111 1111 [1 Byte]

UNI:
It is a coding system.
oracle 715am Page 22
• It is a coding system.
• Extension of ASCII.
• 65536 chars coded.
• Ranges from 0 to 65535.
• UNI = ASCII + other language chars
• 65535 => 1111 1111 1111 1111 [2 Bytes]

EMP

ENAME State GENDER MAIL_ID Exp_summary


VARCHAR2(15) CHAR(2) CHAR VARCHAR2(20) CLOB
RAJu TG M
SRAVAN AP M

Integer related data types:

Number(p):

• P => precision => max num of digits.


• It is used to hold Integers.
• P => 1 to 38

Examples:

-9999 TO 9999
EMPID NUMBER(4) MOBILENUM NUMBER(10)
-------------
1001 AADHAR_NUMBER NUMBER(12)
1002
123 Credit_card_number NUMBER(16)
9999
10000 ERROR

-999 TO 999
Max marks: 100
oracle 715am Page 23
-999 TO 999
Max marks: 100

Maths_marks NUMBER(3)
----------------------
56
678
999
1000 ERROR

-99 TO 99
AGE NUMBER(2)
-------
17
21
20
19
31
99
100 ERROR

NOTE:
INTEGER = INT = NUMBER(38)

CREATE TABLE t25


(
F1 INTEGER,
F2 INT,
F3 NUMBER(38)
);
Output:
Table created.

DESC t25
Output:
NAME TYPE
------------------------------------------
oracle 715am Page 24
------------------------------------------
F1 NUMBER(38)
F2 NUMBER(38)
F3 NUMBER(38)

Floating point related data types:

NUMBER(p,s):

• P => precision => max num of digits


• S => scale => max num of decimal places
• It is used to hold float values.

Examples:

Max marks: 100


-999.99 TO 999.99
5 subjects
500/5 = 100.00
AVRG NUMBER(5,2)
-------------
67.89
100.00
567.34
999.99
1000 ERROR
123.456789 => 123.46 p=5 s=2
123.453789 => 123.45

456.78934 => 456.79


456.78434 => 456.78

-999999.99 TO -999999.99

SALARY NUMBER(8,2) Max sal:


---------------
40000.00 100000.00
100000.00

oracle 715am Page 25


100000.00
100000.00
600000.00
1000000.00 ERROR

-9.9 TO 9.9

Height NUMBER(2,1)
--------------
5.3
5.8
5.6
6.5
9.9
10 ERROR

Float 21 Bytes
Binary_float 4 Bytes
Binary_double 8 Bytes

Date and time related data types:

Date:
• It is used to hold date values.
• Default date format: DD-MON-YY.
• Fixed length data type.
• Max memory: 7 Bytes.
• It can hold up to seconds only.
• It cannot hold fractional seconds.
• It does not display time by default.
• Default time: 12.00.00 AM

Examples:
Date_of_joining DATE
Ordered_date DATE
Delivered_date DATE

oracle 715am Page 26


Delivered_date DATE

Timestamp:
• Introduced in oracle 9i.
• It is used to hold date and time values.
• Default timestamp format: DD-MON-YY HH.MI.SS.FFFFFF AM.
• Fixed length data type.
• Max memory: 11 Bytes.
• It can hold up to fractional seconds.
• It display time by default.
• Default time: 12.00.00.000000 AM

Examples:
Txn_date_time TIMESTAMP
Ordered_date_time TIMESTAMP
Delivered_date_time TIMESTAMP
Login_date_time TIMESTAMP

date
timestamp

Date values Date and time values

By default By default it displays time


It will not display time

Does not hold Fractional seconds


Fractional seconds

7 bytes
11 bytes

EMPLOYEE
oracle 715am Page 27
EMPLOYEE

EMPID ENAME State EXP_SUMMARY DOJ SAL


NUMBER(4) VARCHAR2(10) CHAR(2) CLOB DATE NUMBER(8,2)

1234 RAJU TG 25-DEC-24 100000.00


1235 SRAVAN AP

ATTENDANCE
EMPID LOG_IN_DATE_TIME
NUMBER(4) TIMESTAMp
1234 11-MAR-25 7.30.15.123456 AM

oracle 715am Page 28


Wednesday, March 12, 2025 7:32 AM

oracle software link:


bit.ly/oracle21csoftware

oracle software installation video link:


bit.ly/oracle21cinstallation

Client ORACLE DB SERVER

Instance DB
SQL PLUS / requests
Query processing emp
Runs services Cust
TOAD /
responses .

SQL DEVELOPER RAM HARD DISK

ORACLE:
• ORACLE is a Server side software.
• Using it, we can maintain the DB in the form of tables.

• The machine in which we install ORACLE s/w is called


"ORACLE DB SERVER".

• It contains 2 memories:
○ INSTANCE [RAM]
○ DATABASE [HARD DISK]

DB SERVER = INSTANCE + DB
oracle 715am Page 29
DB SERVER = INSTANCE + DB

SQL PLUS:

• It is client side software.


• SQL PLUS software is used to connect to server
and communicate with DB.
• Using this software we can submit requests to
ORACLE in the form of queries or programs.

NOTE:
When we install ORACLE software along with
ORACLE SQL PLUS software will be installed.

Opening SQL PLUS:

• Press windows + R. Displays RUN dialog box.


• Type "sqlplus". Click on OK.

oracle 715am Page 30


Creating User
Wednesday, March 12, 2025 8:19 AM

Creating User:

Syntax:

CREATE USER <username>


IDENTIFIED BY <password>;

Example:

Create a user with username c##batch715am


With the password nareshit:

Login as DBA:

Username: system
Password: nareshit
[at the time of ORACLE installation you have given password]

SQL> CREATE USER c##batch715am


IDENTIFIED BY nareshit;
Output:
User created.

SQL> GRANT connect, resource, unlimited tablespace


TO c##batch715am;
Output:
Grant succeeded.

c##ravi Common user


raju Local user

oracle 715am Page 31


To connect to server from SQL prompt:

Syntax:
CONN[ECT] <username>/<password>

Example:

SQL> CONN c##batch715am/nareshit


Output:
Connected

To disconnect from server:

Syntax:
DISC[ONNECT]

SQL> DISC

TO clear screen:

Syntax:
CL[EAR] SCR[EEN]

Example:

SQL> CL SCR

TO see current username:

SQL> SHOW USER

oracle 715am Page 32


Thursday, March 13, 2025 7:31 AM

SQL Commands:

DDL DRL DML TCL DCL


metadata retrievals manipulations transactions accessibility
Create Select Insert Commit Grant
Alter Update Rollback Revoke
Delete Savepoint
Drop
Flashback Insert All
Purge Merge

Truncate

Rename

oracle 715am Page 33


Creating tables
Thursday, March 13, 2025 7:38 AM

Create:
• It is used to create the tables.

Syntax:

CREATE TABLE <table_name>


(
<column_name> <data_type> [, [ ] Optional
<column_name> <data_type> , <> Any
.
.]
);

Till Oracle 21c, a table can have max of 1000 columns.


In Oracle 23ai, a table can have max of 4096 columns.

Insert:
• It is used to insert the records [rows].
• Using Insert command we can:
○ Insert single record
○ Insert limited column values
○ Insert multiple records using parameters

Syntax:

INSERT INTO <table_name>[(<column_list>)]


VALUES(<value_list>);

Examples on creating tables:


oracle 715am Page 34
Examples on creating tables:

Example-1:

STUDENT
SID SNAME AVRG
1001 ABC 67.89
1002 XY 55.66

CREATE TABLE student


(
sid NUMBER(4),
sname VARCHAR2(10),
avrg NUMBER(5,2) --creates table in DB
);
Output:
Table created.

Inserting single record:

1001 ABC 67.89

INSERT INTO student


--inserts in instance
VALUES(1001, 'ABC', 67.89);
Output:
1 row created.

1002 XY 55.66

INSERT INTO student


VALUES(1002, 'XY', 55.66); --inserts in instance
Output:
1 row created.

COMMIT;
--changes in instance will
Be moved to DB [save]

oracle 715am Page 35


Be moved to DB [save]

Inserting limited column values:

1003 AA

INSERT INTO student VALUES(1003, 'AA');


Output:
ERROR: not enough values

INSERT INTO student(sid,sname) VALUES(1003, 'AA');


Output:
1 row created.

1004 89.23

INSERT INTO student(avrg, sid) VALUES(89.23, 1004);


Output:
1 row created.

COMMIT;

○ Inserting multiple records using parameters:

Parameter:
It is used to read the value

Syntax:
&<text>

INSERT INTO student VALUES(&sid, '&sname', &avrg);


Output:
Enter value for sid: 1005

oracle 715am Page 36


Enter value for sid: 1005
Enter value for sname: B
Enter value for avrg: 45.56
INSERT INTO student VALUES(1005, 'B', 45.56);
1 row created.

/
Output:
Enter value for sid: 1006
Enter value for sname: C
Enter value for avrg: 66.55

/
Output:
Enter value for sid: 1007
Enter value for sname: D
Enter value for avrg: 92.13

NOTE:
/ R[UN] It runs recent command in memory [above command]

Example-2:

EMPLOYEE
EMPID ENAME STATE SAL DOJ
1234 KIRAN MH 20000.00 17-AUG-20
1235 SAI TG 18000.00 25-DEC-23

CREATE TABLE employee


(
Empid NUMBER(4),
Ename VARCHAR2(10),
State CHAR(2),
Sal NUMBER(8,2),
Doj DATE
oracle 715am Page 37
Doj DATE
);
Output:
Table created.

1234 KIRAN MH 20000.00 17-AUG-20

INSERT INTO employee


VALUES(1234, 'KIRAN', 'MH', 20000, '17-AUG-2020');

string
DOJ DATE
------------ Implicit conversion
17-AUG-20 date

NOTE:
Types of conversions:

2 types:
• Implicit Conversion
• Explicit Conversion

Implicit Conversion:
If conversion is done implicitly by ORACLE

Explicit Conversion:
If conversion is done using built-in function

ORACLE supports implicit conversion.


But, don't depend on it. Because,
Implicit Conversion degrades performance.

1235 SAI TG 18000.00 25-DEC-23


oracle 715am Page 38
1235 SAI TG 18000.00 25-DEC-23

INSERT INTO employee


VALUES(1235, 'SAI', 'TG', 18000, to_date('25-DEC-2023'));
string

DOJ DATE
To_date()
----------------
Explicit conversion
25-DEC-23 date

1236 SRAVAN AP 19000 Today's date

INSERT INTO employee


VALUES(1236, 'SRAVAN', 'AP', 19000, sysdate);

Sysdate:
• It is a built-in function.
• It returns current system date.

COMMIT;

SELECT * FROM employee;

Example-3:

ATTENDANCE
EMPID LOGIN_DATE_TIME
1234 13-MAR-25 10.30.0.0 AM
1235 13-MAR-25 2.20.0.0 PM
1236 Today's date current time

oracle 715am Page 39


CREATE TABLE attendance
(
Empid NUMBER(4),
Login_date_time TIMESTAMP
);
Output:
Table created

1234 13-MAR-25 10.30.0.0 AM

INSERT INTO attendance


VALUES(1234, '13-MAR-2025 10.30.0.0 AM');
string

Implicit conversion
Login_date_time timestamp
---------------------------------------------
13-MAR-25 10.30.0.0 AM timestamp

1235 13-MAR-25 2.20.0.0 PM

INSERT INTO attendance


VALUES(1235, to_timestamp('13-MAR-2025 2.20.0.0 PM'));
string

Login_date_time timestamp To_timestamp()


--------------------------------------------- Explicit Conversion
13-MAR-25 2.20.0.0 PM timestamp

1236 Today's date current time

INSERT INTO attendance

oracle 715am Page 40


INSERT INTO attendance
VALUES(1236, systimestamp);

COMMIT;

SELECT * FROM attendance;

Systimestamp:
• It is a built-in function.
• It returns current system date and time

oracle 715am Page 41


Saturday, March 15, 2025 7:47 AM

To see table structure:

Syntax:
DESC[RIBE] <table_name>

Example:
DESC student

Output:
NAME TYPE
SID NUMBER(4)
SNAME VARCHAR2(10)
AVRG NUMBER(5,2)

To see list of tables created by a user:

User_tables:
• It is a system table / readymade table / built-in table.
• It maintains tables information.

DESC user_tables

SELECT table_name FROM user_tables;


Output:
TABLE_NAME
--------------------
STUDENT
oracle 715am Page 42
STUDENT
EMPLOYEE
ATTENDANCE

oracle 715am Page 43


Setting pagesize and linesize
Saturday, March 15, 2025 8:04 AM

Setting pagesize and linesize:

SQL> SHOW ALL


Output:
PAGESIZE 14
LINESIZE 80

Setting pagesize:

Syntax:
SET PAGES[IZE] <value>

Example:
SQL> SET PAGES 300

Setting line size:

Syntax:
SET LINES[IZE] <value>

Example:
SQL> SET LINES 200

SQL> SET PAGES 300


SQL> SET LINES 200 (or) SQL> SET PAGES 300 LINES 200

oracle 715am Page 44


Column Alias
Saturday, March 15, 2025 8:26 AM

Column Alias:
It is used to change column heading in output.

Syntax:
<column> [AS] <column_alias>

Example:

Display all emp names and salaries.


Display ename column heading as A, sal column
heading as B:

SELECT ename AS A, sal AS B


FROM emp;
Output:
A B
SMITH 800
ALLEN 1600

oracle 715am Page 45


DRL
Friday, March 14, 2025 8:33 AM

DRL / DQL:

• DRL => Data Retrieval Language


• DQL => Data Query Language

• It deals with data retrievals.


• Retrieve [getting back] => opening existing data

ORACLE SQL provides only 1 DRL command.


i.e: SELECT

SELECT:
• SELECT command is used to retrieve the data
from table.
• Using SELECT command we can select:
○ All columns, all rows
○ All columns, specific rows
○ Specific columns, all rows
○ Specific columns, specific rows

Syntax:

SELECT <column_list>
FROM <table_name>
[WHERE <condition>];

SQL ENGLISH
QUERIES SENTENCES
CLAUSES WORDS

NOTE:
Till ORACLE 21c, FROM clause is mandatory.
In ORACLE 23ai, FROM clause is optional.

Example:
In ORACLE 21c SELECT 100+200; --error

oracle 715am Page 46


In ORACLE 21c SELECT 100+200; --error

SELECT 100+200 FROM dual;


Output:
300

In ORACLE 23ai SELECT 100+200;


Output:
300

Examples on SELECT:

All columns, all rows:

Display all columns and all rows of STUDENT table:

SELECT * FROM student;

* All columns

Implicitly ORACLE rewrites above query as following:

SELECT sid, sname, avrg FROM student;

○ All columns, specific rows:

Display the student record whose sid is 1002:

SELECT *
FROM student
WHERE sid=1002;

○ Specific columns, all rows:

Display all students names and avrg marks:

SELECT sname, avrg


oracle 715am Page 47
SELECT sname, avrg
FROM student;

• Specific columns, specific rows:

Display sname and avrg of sid 1002:

SELECT sname, avrg


FROM student
WHERE sid=1002;

All cols, all rows SELECT * FROM student;


All cols, spec rows SELECT * FROM student
WHERE sid=1002;
Spec cols, all rows SELECT sname, avrg FROM student;
Spec cols, spec, SELECT sname, avrg FROM student
rows WHERE sid=1002;

All cols SELECT *


Spec columns SELECT sname, avrg
All rows Don't write WHERE condn
Spec rows WHERE condn

Operators in ORACLE SQL:

Operator:
Operator is a symbol that is used to perform operations like
arithmetic or logical operations.

ORACLE SQL provides following operators:

Arithmetic + - * /

Relational (or) > < >= <= = != / <> / ^=


oracle 715am Page 48
Relational (or) > < >= <= = != / <> / ^=
Comparison equals not equals

Logical AND OR NOT

Special / IN NOT IN
Comparison BETWEEN AND NOT BETWEEN AND
LIKE NOT LIKE
IS NULL IS NOT NULL

ANY
ALL
EXISTS

Set UNION UNION ALL INTERSECT MINUS

Concatenation ||

Arithmetic operators:

+ - * /

Examples on Arithmetic operators:

Calculate annual salary of all emps:


ENAME SAL ANNUAL_SAL

SELECT ename, sal, sal*12


FROM emp;
Output:
ENAME SAL SAL*12
SMITH 800 9600

SELECT ename, sal, sal*12 AS annual_sal


FROM emp;
Output:

oracle 715am Page 49


Output:
ENAME SAL ANNUAL_SAL

SELECT ename, sal, sal*12 AS annual salary


FROM emp;
Output:
ERROR

SELECT ename, sal, sal*12 AS "Annual Salary"


FROM emp;

Output:
ENAME SAL Annual Salary

Calculate TA, HRA, TAX and GROSS salary of all emps.


10% on sal => TA
20% on sal => HRA
5% on sal => TAX
GROSS = sal + ta + hra - tax

ENAME SAL TA HRA TAX GROSS

SELECT ename, sal,


Sal*0.1 AS TA,
Sal*0.2 AS HRA,
Sal*0.05 AS TAX,
Sal + sal*0.1 + sal*0.2 - sal*0.05 AS gross
FROM emp;

Assignment:

STUDENT
SID SNAME M1 M2 M3
1001 A 50 80 70
1002 B 66 55 89

oracle 715am Page 50


Calculate total marks and avrg marks of all students
SNAME TOTAL AVRG

Relational operators / Comparison operator:

> < >= <= = != / <> / ^=

Syntax:
<column> <relational operator> <value>

Relational operator is used to compare column value


with 1 value.

Examples on relational operators:

Display all managers records:


ENAME JOB SAL
MANAGER

SELECT ename, job, sal


FROM emp
WHERE job='manager';
Output:
No rows selected

MANAGER = manager FALSE


CLERK = manager FALSE

SELECT ename, job, sal


FROM emp
WHERE job='MANAGER';

NOTE:
SQL is not case sensitive language.
String comparison is case sensitive.
oracle 715am Page 51
String comparison is case sensitive.

Display the emp record whose empno is 7521:

SELECT * FROM emp WHERE empno=7521;

Display the emp records whose salary is 3000 or more:


ENAME SAL

SELECT ename, sal


FROM emp
WHERE sal>=3000;

Display the emp records whose salary is 1250 or less:


ENAME SAL

SELECT ename, sal


FROM emp
WHERE sal<=1250;

Display the emp records who are working in deptno 20:


ENAME SAL DEPTNO
20

SELECT ename, sal, deptno


FROM emp
WHERE deptno=20;

Display the emp records who joined after 1981:

1-JAN-1981
.
31-DEC-1981
1-JAN-1982
2-JAN-1982 > '31-DEC-1981'
.
.

ENAME HIREDATE

SELECT ename, hiredate


oracle 715am Page 52
SELECT ename, hiredate
FROM emp
WHERE hiredate>'31-DEC-1981';

Display the emp records who joined before 1981:

.
< '1-JAN-1981'
30-DEC-1980
31-DEC-1980
1-JAN-1981
.
.
31-DEC-1981

ENAME HIREDATE

SELECT ename, hiredate


FROM emp
WHERE hiredate<'1-JAN-1981';

Display all emp records except managers:


ENAME JOB SAL

SELECT ename, job, sal


FROM emp
WHERE job!='MANAGER';

Display all emp records except deptno 20 emps:


ENAME SAL DEPTNO

SELECT ename, sal, deptno


FROM emp
WHERE deptno!=20;

Logical Operators:

oracle 715am Page 53


These are used to perform logical operations like Logical
AND, Logical OR, Logical NOT.

ORACLE SQL provides following Logical Operators:


AND
OR
NOT

AND:
It is used to perform logical AND operations.

Syntax:
<condition1> AND <condition2>

OR:
It is used to perform logical OR operations.

Syntax:
<condition1> OR <condition2>

AND All conditions should be satisfied


OR At least 1 condition should be satisfied

Truth table:

C1 => condition1
C2 => condition2

C1 C2 C1 AND C2 C1 OR C2
T T T T
T F F T
F T F T
F F F F

Examples on AND, OR:

Display all managers and clerks records:


ENAME JOB SAL

oracle 715am Page 54


ENAME JOB SAL

SELECT ename, job, sal


FROM emp
WHERE job='MANAGER' OR job='CLERK';

Display the emp records who are working in deptno 20and 30:
ENAME SAL DEPTNO

SELECT ename,sal, deptno


FROM emp
WHERE deptno=20 OR deptno=30;

Display the emp records whose empnos are


7499, 7698, 7788:

SELECT *
FROM emp
WHERE empno=7499 AND empno=7698 AND empno=7788;

EMPNO
------------
7369
7499
7698
7900

Display the emp records whose names are


ALLEN, BLAKE and SCOTT:

SELECT * FROM emp


WHERE ename='ALLEN' OR ename='BLAKE' OR ename='SCOTT';

Display the emp records whose salary is b/w 2000 and 3000
(or)
Display the emp records whose salaries are 2000 or more and
yhat should be 3000 or less:
ENAME SAL

SELECT ename, sal

oracle 715am Page 55


SELECT ename, sal
FROM emp
WHERE sal>=2000 AND sal<=3000;

Display the emp records whose empno are b/w


7600 to 7900:

SELECT * FROM emp


WHERE empno>=7600 AND empno<=7900;

Display the emp records who joined in 1982:

1-JAN-1982
2-JAN-1982
.
31-DEC-1982

SELECT ename, hiredate


FROm emp
WHERE hiredate>='1-JAN-1982' AND Hiredate<='31-DEC-1982';

Display the managers records whose salary is more than 2500:


ENAME JOB SAL

SELECT ename, job, sal


FROM emp
WHERE job='MANAGER' AND sal>2500;

Display the managers records who joined after may 1981:


ENAME JOB SAL HIREDATE

SELECT ename, job, sal, hiredate


FROM emp
WHERE job='MANAGER' AND hiredate>'31-MAY-1981';

Display the managers records whose salary is more than 2500


And those hiredate should be after april 1981:
ENAME JOB SAL HIREDATE
oracle 715am Page 56
ENAME JOB SAL HIREDATE

SELECT ename, job, sal, hiredate


FROM emp
WHERE job='MANAGER' AND sal>2500 AND hiredate>'30-APR-1981';

Display the managers and clerks whose salaries are more than 2500:
ENAME JOB SAL

SELECT ename, job, sal


FROM emp
WHERE (job='MANAGER' OR job='CLERK') AND sal>2500;

Display all emp records except managers and clerks:

SELECT *
FROM emp
WHERE NOT(job='MANAGER' OR job='CLERK');

NOT truth table:


Condition NOT(condition)
T NOT(T) => F
F NOT(F) => T

Display all emp records except deptno 30 emps:


ENAME SAL DEPTNO

SELECT ename, sal, deptno


FROM emp
WHERE NOT(deptno=30);

DEPTNO
----------
20
30

oracle 715am Page 57


Special Operators / Comparison Operators:

IN:

Syntax:
<column> IN(<value_list>)

• It is used to compare column value with a list of values.


• If column value is in list then condition is TRUE.
• If column value is not in list then condition is FALSE.

Display the emp records whose salary is


800 or 1250 or 3000:
ENAME SAL

SELECT ename, sal


FROM emp
WHERE sal=800 OR sal=1250 OR sal=3000;

(or)

SELECT ename, sal


FROM emp
WHERE sal IN(800, 1250, 3000);

SAL WHERE sal IN(800, 1250, 3000)


5000 5000 IN(800, 1250, 3000) FALSE
3000 3000 IN(800, 1250, 3000) TRUE
2000 2000 IN(800, 1250, 3000) FALSE
1250 1250 IN(800, 1250, 3000) TRUE

Display all managers and clerks records:


ENAME JOB SAL

SELECT ename, job, sal


FROM emp
WHERE job IN('MANAGER', 'CLERK');
oracle 715am Page 58
WHERE job IN('MANAGER', 'CLERK');

JOB WHERE job IN('MANAGER', 'CLERK')


MANAGER MANAGER IN('MANAGER', 'CLERK') T
ANALYST ANALYST F
CLERK CLERK T
SALESMAN SALESMAN F

Display the emp records whose empnos are


7499, 7698, 7788:

SELECT * FROM emp


WHERE empno IN(7499, 7698, 7788);

Display the emp records whose emp names are


ALLEN, BLAKE and SCOTT:

SELECT * FROM emp


WHERE ename IN('ALLEN', 'BLAKE', 'SCOTT');

Display the emp records who are working in deptno 10 and 20:
ENAME SAL DEPTNO

SELECT ename, sal, deptno


FROM emp
WHERE deptno IN(10, 20)
ORDER BY deptno ASC;

Display all emp records except managers and clerks:


ENAME JOB SAL

SELECt ename, job, sal


FROM emp
WHERE job NOT IN('MANAGER', 'CLERK');

JOB WHERE job NOT IN('MANAGER', 'CLERK')

oracle 715am Page 59


JOB WHERE job NOT IN('MANAGER', 'CLERK')
MANAGER MANAGER F
ANALYST ANALYST T

Display the emp records who are not working in deptno 10 and 20:
ENAME SAL DEPTNO

SELECt ename, sal, deptno


FROM emp
WHERE deptno NOT IN(10, 20);

BETWEEN AND:

Syntax:
<column> BETWEEN <lower> AND <upper>

• It is used to compare column value wit range of values.


• If column value falls between range then condition is TRUE.
• If column value does not falls between range then condition is FALSE.

Examples on BETWEEN AND:

Display the emp records whose salary is 2000 or more and


3000 or less
(or)
Display the emp records whose salary is between 2000 and
3000:
ENAME SAL

SELECT ename, sal


FROM emp
WHERE sal>=2000 AND sal<=3000;

(or)

SELECT ename, sal


FROM emp
WHERE sal BETWEEN 2000 AND 3000;

oracle 715am Page 60


SAL WHERE sal BETWEEN 2000 AND 3000
2500 2500 BETWEEN 2000 AND 3000 T
3000 3000 BETWEEN 2000 AND 3000 T
5000 5000 BETWEEN 2000 AND 3000 F
1250 1250 BETWEEN 2000 AND 3000 F

Display the emp records whose empnos are between


7600 and 7900:

SELECT * FROM emp


WHERE empno BETWEEN 7600 AND 7900;

Display the emp records who joined in 1982:


ENAME HIREDATE

SELECT ename, hiredate


FROM emp
WHERE hiredate BETWEEN '1-Jan-1982' AND '31-DEC-1982';

Example:

Display previous 3 months transactions of acno 1234.


[dec, jan, feb]

TRANSACTIONS
Txn_Id T_date ACNO T_type Amount
123456 5-DEC-24 1234 D 50000
123457 5-DEC-24 1235 D 70000
123458 7-DEC-24 1234 W 20000

SELECT * FROM transactions


WHERE acno=1234 AND
t_date BETWEEN '1-DEC-2024' AND '28-FEB-2025';

Display the emp records whose salary is less than 2000 or more

oracle 715am Page 61


Display the emp records whose salary is less than 2000 or more
than 3000
[sal should not be b/w 2000 and 3000]:
ENAME SAL

SELECT ename, sal


FROM emp
WHERE sal NOT BETWEEN 2000 AND 3000;

SAL sal NOT BETWEEN 2000 AND 3000


2500 2500 NOT BETWEEN 2000 AND 3000 F
5000 5000 NOT BETWEEN 2000 AND 3000 T

Display the emp records who are not joined in 1981:


ENAME HIREDATE

SELECT ename, hiredate


FROM emp
WHERE hiredate NOT BETWEEN '1-JAN-1981' AND '31-DEC-1981';

LIKE:

In windows OS,

To search for all jpg files *.jpg


To search for jpg files which are A*.jpg
started with A
To search for jpg files in which 2nd ?A*.jpg
letter is A

In windows

Wildcard Char Purpose


* Replaces 0 or any
? Replaces 1 char

NOTE:
oracle 715am Page 62
NOTE:
In ORACLE SQL

% Replaces 0 or any
_ Replaces 1 char

oracle 715am Page 63

You might also like