0% found this document useful (0 votes)
1 views23 pages

Oracle SQL Note Book

The document provides a comprehensive overview of Oracle SQL, detailing the order of SQL clauses, data types, and commands for creating, modifying, and deleting tables and records. It covers various SQL operations including DDL, DML, DRL, TCL, and DCL, along with examples of inserting, updating, and deleting data. Additionally, it discusses constraints, composite keys, and the use of functions like CONCAT and LIKE in SQL queries.

Uploaded by

prasad.karuva
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)
1 views23 pages

Oracle SQL Note Book

The document provides a comprehensive overview of Oracle SQL, detailing the order of SQL clauses, data types, and commands for creating, modifying, and deleting tables and records. It covers various SQL operations including DDL, DML, DRL, TCL, and DCL, along with examples of inserting, updating, and deleting data. Additionally, it discusses constraints, composite keys, and the use of functions like CONCAT and LIKE in SQL queries.

Uploaded by

prasad.karuva
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/ 23

==============================ORACLE SQL=============================

order of the clauses:

from>where>group by> having>order

1.case sensitive

DATA TYPES:

1.NUMERIC

number(8)--> it can store 8 bytes or 8 characters


number(8,2)--> precission is 2(max it store 8characters and after decimal
only 2 characters.
ex: 12345678.23or 34.98
number(2,2)--> 0.99 or 33.23

2.CHAR

char
varchar()
varchar2()

3.DATE

date
=========================================================================
==

CREATING TABLE:

create table customer


(
customer_id number(8),
customer_name varchar2(100),
mobile number(10),
dob date,
city varchar2(15),
email_id varchar2(30)
);

1.DDL(Data definition language)


--->create
---->alter
---->rename
---->truncate
---->drop

NOTE: these are auto commit.

2.DML(Data manipulation language)


--->insert
--->update
---->delete

NOTE: USER COMMIT

3.DRL(Data retrival language)


----> select

4.TCL(transition control language)

----->commit
----->rolback
----->savepoint

5.DCL(data control language)

---->grant
----->revoke

=============================================================

insert:

insert into customer (customer_id,customer_name,mobile,dob,city,email_id)


values
(100000,'ARUN',7989513312,to_date('08/04/2000','mm/dd/yyyy'),'chennai','a
[email protected]');

insert into customer


values
(100001,'john',7989513313,to_date('04/08/2000','mm/dd/yyyy'),'hyd','john@
gmail.com');

note: while inserting data,if you know the column order then you dont
need to mention columns.

sql> select * from customer;

Result Set 1:

CUSTOMER_ID CUSTOMER_NAME MOBILE DOB CITY EMAIL_ID


100000 ARUN 7989513312 04-AUG-00 chennai [email protected]
100001 john 7989513313 08-APR-00 hyd [email protected]

sql> commit:
commit complete.

it means the data is saved permanently.


after commit statements ,if you perform "rollback" it wont be rollback.
=========================================================================
=

ADDING COLUMN:

alter table customer add country varchar2(20);CUSTOMER_ID CUSTOMER_NAME

100003 paul 7989513312 09-Sep-00 [email protected]


100000 ARUN 7989513316 04-Aug-00 chennai [email protected] -
100001 john 798951298 08-Apr-00 hyd [email protected] -

sql>

update customer
set country ='INDIA';

USTOMER_ID CUSTOMER_NAME MOBILE DOB CITY EMAIL_ID COUNTRY


100003 paul 7989513312 09-SEP-00 [email protected] INDIA
100000 ARUN 7989513316 04-AUG-00 chennai [email protected]
INDIA
100001 john 798951298 08-APR-00 hyd [email protected] INDIA

=========================================================================
==
drop column:
===========

sql>alter table customer


drop column city:

CUSTOMER_ID CUSTOMER_NAME MOBILE DOB EMAIL_ID COUNTRY


100003 paul 7989513312 09-SEP-00 [email protected] INDIA
100000 ARUN 7989513316 04-AUG-00 [email protected] INDIA
100001 john 798951298 08-APR-00 [email protected] INDIA

=========================================================================
==

changing existing data type to another datatype.


here we are changing mobile data type to varchar2.

1.if you want to modify existing datatype ,first you should empty that
column.for that
you should take backup .

1.backup table
2.truncate base table
3.modify datatype
4.restore the data
5.drop bkp table
creating backup table:
create table customer_bkp as select * from customer;

select * from customer_bkp;

USTOMER_ID CUSTOMER_NAME MOBILE DOB EMAIL_ID COUNTRY


100003 paul 7989513312 09-SEP-00 [email protected] INDIA
100003 paul - 09-SEP-00 [email protected]
100000 ARUN 7989513316 04-AUG-00 [email protected] INDIA
100001 john 798951298 08-APR-00 [email protected] INDIA

creating backup with no data:

create table customer_bkp1 as select * from customer where 1=2;


here backup table created with no data.

sql>
truncate table customer:

truncate comepleted.

modifying data type:

alter table customer


modify mobile varchar2(23);

sql> desc customer:


Column Null? Type
CUSTOMER_ID - NUMBER(8,0)
CUSTOMER_NAME - VARCHAR2(100)
MOBILE - VARCHAR2(23)
DOB - DATE
EMAIL_ID - VARCHAR2(30)
COUNTRY - VARCHAR2(20)

=========================================================================
=======

loading data to base table from back up table.

insert into customer (select * from customer_bkp)

CUSTOMER_ID CUSTOMER_NAME MOBILE DOB EMAIL_ID COUNTRY


100003 paul 7989513312 09-SEP-00 [email protected] INDIA
100003 paul - 09-SEP-00 banglore [email protected]
100000 ARUN 7989513316 04-AUG-00 [email protected] INDIA
100001 john 798951298 08-APR-00 [email protected] INDIA

=========================================================================
========
rename column :
alter table customer
rename column dob to date_of_birth;

CUSTOMER_ID CUSTOMER_NAME MOBILE DATE_OF_BIRTH EMAIL_ID


COUNTRY
100002 sandeep 9704975921 02-SEP-00 banglore
[email protected]
100003 sai 9704975921 09-JAN-00 punjab [email protected]
100000 ARUN 7989513316 04-AUG-00 [email protected] INDIA
100001 john 798951298 08-APR-00 [email protected] INDIA

=========================================================================
==========
rename table:

rename customer to customer1;


sql?select * from customer1;

CUSTOMER_ID CUSTOMER_NAME MOBILE DATE_OF_BIRTH EMAIL_ID


COUNTRY
100002 sandeep 9704975921 02-SEP-00 banglore
[email protected]
100003 sai 9704975921 09-JAN-00 punjab [email protected]
100000 ARUN 7989513316 04-AUG-00 [email protected] INDIA
100001 john 798951298 08-APR-00 [email protected] INDIA

=========================================================================
====
CONSTRAINTS ON ORACLE:

def: data validation before inserting data into table.


1.primary key:
i) it will not allow duplicates.
ii).it will not allow null values
iii).one primary key per table.
ex: emp_id,prod_id,trans_id,cust_id

2.NOT NULL

i).it will not allow null values.


ii).it wil allow duplicates.

3.unique

i)it will not allow duplicates


ii)it will allow null values.

4. check

data validation.

check (age>18)

5 foreign key
i).relation ship between two tables.
ii)it will accept duplicates.
iii)it should be primary key another table.
iv).any no of foreign key in table.

ex: foreign key(city_id) references city(city_id)

=========================================================================
======

create table city (cityid number(20) primary key,cityname varchar2(20));

insert into city values(10,'hyd')


insert into city values(20,'chennai');
insert into city values(30,'banglore');
insert into city values(40,'pun');

create table cust(cust_id number(10) primary key,


cust_name varchar2(20) not null,
mobile number(10) unique check(length(mobile)=10),
age number(10) check(age>=19),
city_id number(10) references city(cityid));

note : in real time we dont know which constraint is violated.so w should


use below queries.

select * from all_constraints where owner='HR' and table_name='cust';

op; here wil getconstrain name(SYS_C09883) but we cant see which column
it is belong to, so

select * from all_cons_columns where owner='HR' and table_name='cust';

op; here wil get table name,columnnames,constraint_names.

note: we dont need to check this manually so we are making join using two
tables.then we can easily know which key s violated.we can easil
identify.

select
a.owner,a.constraint_name,a.constraint_type,b.table_name,b.column_name
from all_constraints a,all_cons_columns b
where a.constraint_name=b.constraint_name and a.owner='HR' and
a.table_name='cust';

=========================================================================
=======
you can delete a record from child table.

CUST_ID CUST_NAME MOBILE AGE CITY_ID


100006 raji 7989536121 25 30
100005 ram 7989536199 25 10
100000 arun 7989513311 25 20
100001 arun 7989513312 26 30
100002 vijay 7989513313 31 10
100003 ajith 7989513314 25 30

from this table we can delete a record.

delete from cust where cust_name='ram';

CUST_ID CUST_NAME MOBILE AGE CITY_ID


100006 raji 7989536121 25 30
100000 arun 7989513311 25 20
100001 arun 7989513312 26 30
100002 vijay 7989513313 31 10
100003 ajith 7989513314 25 30
=========================================================================
===

but from parent table you cant delete any record.


select * from city;

CITYID CITYNAME
10 hyd
20 chennai
30 banglore
40 pun

delete from city where cityid=10;


ORA-02292: integrity constraint
(SQL_LHIPXMNZMQMVJGEDLKZCIKEMG.SYS_C00124765822) violated -
child record found ORA-06512: at "SYS.DBMS_SQL", line 1721

because child records found in other table so we cant delete.if you want
to delete ,
first you should delete child records from other table
and then delete record from parent table.
=========================================================================
====

ON DELETE CASCADE:

whenever we are deleting a record from parent table automatically child


record from another table also gets deleted.
this is called on delete cascade.

first relationconstraint delete


select * from all_constraints where owner='Hr' and table_nam='cust';

sys_C00098(constrant_name)

alter table cust drop constraint sys_C00098;

now add foreign key constraintt.


alter table cust
add constraint city_fk
foreign key(city_id)
references city(cityid)
on delete cascade;

now if you delete record from parent table ,parallely child records also
deleted from another table.
=========================================================================
=================================

composite key:

combination of two keys as primary key.

create table customer(


custid number(10),
cust_name varchar2(20),
mobile number(10),
primary key(cust_name,mobile));

insert into customer values(100,'chandra',7989513311);


insert into customer values(101,'chandra',7989513312);

=========================================================================
=============================

column concatination:

create table emp(


id number(10) primary key,
f_name varchar2(20),
l_name varchar2(20),
full_name varchar2(20),
email varchar2(20));

insert into emp


values(1,'chandra','babu','chandrababu','[email protected]')
insert into emp values(2,'sai','babu','csaiababu','[email protected]')
insert into emp values(3,'sree','kanth','srekanth','[email protected]')

select * from emp;

ID F_NAME L_NAME FULL_NAME EMAIL


1 chandra babu chandrababu [email protected]
2 sai babu csaiababu [email protected]
3 sree kanth srekanth [email protected]

sql>
select f_name as first_name,concat(f_name,l_name) as FULLNAME from emp;

FIRST_NAME FULLNAME
chandra chandrababu
sai saibabu
sree sreekanth

=========================================================================
==============================
create table emp(id number(10),fname varchar2(20),lname varchar2(20));

sql> select * from emp;

ID FNAME LNAME
3 sree kanth
2 sai babu
1 chandra babu
3 sree kanth

select count(distinct id) from emp;

COUNT(DISTINCTID)
3

sql>select * from emp where salary>60000;


sql>select * from emp where salary between 65000 and 70000;
select * from emp where salary not between 65000 and 70000;
sql>select * from dept where deptid=20;
sql>select * from dept where deptid in (10,20,30);
sql>select rownum,rowid from emp;
note :rowid is unique for every record ,including for duplicates.
sql>select rownum,rowid,* from emp;
sql>select rownum,rowid,e.* from emp as e;
sql>select * from rownum<=6;
note:rownum is always works with "<="only..
sql>select * from emp where commission is null;
sql>select * from emp where commission is not null;
sql>select * from emp where to_char('hire_date','yyyy')='2020'
sql>select * from emp where to_char('hire_date','DAY')='MONDAY'
SQL>select to_char('05-05-2020','DAY') from emp;

op:MONDAY
SQL>select to_char('05-05-2020','mm') from emp;
op:05
SQL>select to_char('05-05-2020','yyyy') from emp;
op:2020
SQL>select to_char('05-05-2020','dd') from emp;
op:05

select sysdate from dual;


=========================================================================
============================

concatination:

select customer_id, customer_name,concat(customer_name,mobile) as


name_mobile from customer;
CUSTOMER_ID CUSTOMER_NAME NAME_MOBILE
100000 ARUN ARUN7989513312
100001 john john7989513313

-----------------------------------------

or

select customer_id, customer_name,customer_name||mobile as name_mobile


from customer;

distinct:

select distinct customer_id from customer;

========================================================================

select * from customer where rownum<=5;

note:we cant select rownum ">= or ">" ,it works with "<" or "<=" only.
and rownum will be changed in future(because if we delete any
record,automatically rownum wil changed.

rowid is unique, it wont be change,


rowid also unique but in future if we delete any record means rownumber
will change.

=========================================================================

select rowid,rownumber,e.* from customer e;

=========================================================================
===

select emp_id,hiredare,to_char(hiredate,'yyyy') from customer;

WAQ to find who joined in 2023:

select * from customer where to_char('hire_date','yyyy')='2023';

WAQ to find who joined between 2022 and 2024:

select * from customer where to_char('hire_date','yyyy') between '2022'


and '2024';
WAQ to find who joined on monday:

select * from customer where to_char('hire_date','FMDay')='Monday'

select * from customer where to_char('hire_date','mm')='02';

=========================================================================
====

dummy table

select * from dual;


select sysdate from dual;

select current_date from dual;


select systimestamp from dual;

=========================================================================
====

Like %

select * from emp where empname like 'Sandeep%';

output:Sandeep, SandeepKumar

name should start with Sandeep and then so on...

select * from emp where empname like '%raghava';

output: sriraghava
ends with raghava

select * from emp where empname like '%babu%';

op:chandrababunaidu
in between babu should exist.

starting and ending is anywords can exist.

select *from emp where empname like _babu;

op:: cbabu (only 5 letter should present and ends with babu.

=========================================================================
===

order by:

select * from emp order by empname desc;


note: by defauld order by is >> asc

select * from emp order by sal asc ;

order by two columns:


---------------------

select * from emp order by empname asc,sal desc;

note:: first it wil sort the data according to first column, if first
column has duplicates then it sort based on second coulmn..

select * frm emo order by 2 asc,8 desc;

note: 2, and 8 are column numbers.

select * from emp order by name,sal asc;


op:
ID NAME SAL
10 chandra 300
20 chandra 400
30 sai 500
40 taman 600
50 taman 700

=======================================================================
substr:

select substr('Accenture~256',1,9) from dual;

op::
Accenture

note:: 1>> it represents position


9>> number of characters

=========================================================================
==
instr::

select instr('TCS~346','S') from dual;

op:3
----------------------------------------------------------
select instr('aa-ba-aa-ba-aa-ba','ba',1,1) from dual;

note:1 >>represets 1st position ,1>> represents 1st occurance.

op::4
----------------------------------------------------------
select instr('aa-ba-aa-ba-aa-ba','ba',1,2) from dual;
Note: 1>> it represents 1stposition, 2 >> reprsents second occuranceof
"ba"

op::10
--------------------------------------------------------------
HR~123 AND INSURANCE~256(COMBINATION OF DEPARTMENT AND DEPT ID)

FROM THESE TWO WORDS EXTRACT ONLY WORDS NOT DIGITS WITH SINGLE
LOGIC.NORMALLY WE
CAN EXTRACT WORDS BY USING SUBSTR. BUT WHEN IT COMES TO LOGIC IT APPLY
FOR ONLY SINGLE TERM.
BY USING INSTR WE CAN MAKE A LOGIC, WHICH APPLICABLE FOR ALL.

select substr('HR~123',1,instr('HR~123','~')-1) from dual;


OP:HR

select substr('insurance~256',1,instr('insurance~256','~')-1) from dual;


OP:insurance

note : here the logic is same but applied for both words(HR~123 AND
insurance~256)

Extracting the dept_id:

select substr('insurance~256',instr('insurance~256','~')+1) from dual;

op: 256

select substr('HR~123',instr('HR~256','~')+1) from dual;

OP::256

======================================================================

LPAD:

select lpad('WELCOME',15,'*') from dual;

OP:: ********WELCOME

RPAD:

select Rpad('WELCOME',15,'*') from dual;

op: WELCOME********

========================================================================
LTRIM:
select ltrim(' SANDEEP') from dual;

OP:: SANDEEEP

select rtrim('SANDEEP ') from dual;


op: SANDEEP

TRIME:

select trim(' SUN DEEP ') from dual;

op::SUN DEEP

NOTE: LEFT SPACES AND RIGHT SIDE SPACES DELETED, BUT INBETWEEN WONT BE
DELETED.

=====================================================================

REPLACE:

select replace('sun deep',' ','') from dual;

op::sundeep

select replace('java developer','java','hadoop') from dual;

hadooop developer

========================================================================
select ltrim('012021021102547021011022101022','021') from dual;

op::547021011022101022

note : it trims either o or 1 or 2,not combination(021) from left side.

select rtrim('012021021102547021011022101022','021') from dual;

012021021102547

=========================================================================
==

Translate:

select translate('ABCD-EFGH','ABCD','WXYZ') from dual;

op::WXYZ-EFGH

NOTE: POSITIONWISE IT WILL REPLACE,


A->W
B->X
C->Y
D->Z

=========================================================================
==
NVL:

it takes twp arguements.

SELECT NVL(10,20) from dual;

op:: 10

note:
nvl(arg1,arg2)

if arg1 =null
then it will return arg2

if arg1!= null
then it returns arg1.

select nvl(null,70) from dual;


op::70

========================================================================

NVL2:
it takes 3 arguements.

nvl(arg1,arg2,arg3)

select nvl2(null,20,30) from dual;

op: 30

select nvl2( 10,20,30) from dual;


op: 20

if arg1=null , then it return arg3


if arg1!= null,then it retuen arg2.

select
emp_id,emp_name,allocation_id,nvl2(allocation_id,'ALLOCATED','WAITING FOR
PROJECT') FROM DUAL;

COALESCE:

it wil return first not null value,

=========================================================================
==

round:
select round(456.56) from dual;

op:: 457
select round(456.56,1) from dual;

op: 457

select round(456.3477,2) from dual;


op: 456.35

select round(456.9765,1) from dual;

op: 457

========================================================================

MONTHSS_BETWEEN

select months_between(to_date('2025-01-01','yyyy-mm-dd'),to_date('2023-
01-01','yyyy-mm-dd')) as months from dual;

MONTHS
25
=======================================================================

days between:

select to_date('2023-30-01','yyyy-dd-mm')-to_date('2022-20-02','yyyy-dd-
mm') days from dual;

344

select sysdate from dual;


06-JUN-23 12.43.50.161362 AM US/PACIFIC

select current_date from dual;


06-JUN-23

select systimestamp from dual;


06-JUN-23 07.45.34.521613 AM +00:00

=========================================================================

joins:

there are two ways to write joins:

1.implicit method
2.ansi method

1).implicit method:
Inner join:
-----------
select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from
customer a, country b
where a.id=b.country_id

left outer join or left join:


-----------------------------

select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from


customer a, country b
where a.id=b.country_id(+)

right join:
-----------
select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from
customer a, country b
where a.id(+)=b.country_id

2).Ansi method:
---------------

select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from


customer a
join country b on a.id=b.country_id

-
=========================================================================
=

cross join:

Ansi method:

select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id from


customer a
cross join country b

note:in cross join there is no condition.

cross join in Implicit method:

select a.cust_id,a.cust_name,a.id,a.mail,b.counry,b.country_id
from customer a, country b

=========================================================================
==
Subquery:

1.single row subquery


2.multi row
3.multi column

query within a query is called subquery.

inner query is executed first and then outer query.

inner query is executed without outer query but outer query is not
executed
without innner query.

1.singlerow subquery:

inner query returns single row.thats why it is called as single row


subquery.

operators-->> =,<,>,<=,>=,!= we should use these operators in single row


subquery.

finf an employee details who is earning max salary.

select * from emp where salary=(select max(salary) from emp);

select * from where sal > (select avg(salary) from emp);


=========================================================================
=
2.multi row subquery:

inner query returns multiple rows.

operators-->>in ,any, all(we should use these operators.)

select * from emp where (dept_id,salary) in (select dept_id,max(salary)


from emp group by dept_id);

note: here inner query returns multiple rows so we should use in


operator. and also in inner query we are selecting
two columns so we should use two columns in outer clause condition.

select * from emp where dept_id < any (30,60,90) order bydept_id;

among 30,60,90,outer query return records whenever dept id is lessthan


any of theese(30,60,90)

select * from emp where dept_id > all (30,60,90) order bydept_id;

mote: outer query returns records whenver dept id is greater than all of
mentioned deptids(30,60,90).
=========================================================================
==

delete duplicate records:

delete from emp where rowid not in


(select max(rowid) from emp group by emp_id);

=========================================================================
==

duplicate records:

select count(*) from emp group by emp_id having count(*) > 1;

=========================================================================
===

union and uninAll


-----------------

select * from emp1;

EMP_ID EMP_NAMECOUNTRY MOBILE


670 NTR INDIA 12345678
671 NBK INDIA 543210
980 vivek INDIA 4975

select * from emp2;

EMP_ID EMP_NAMECOUNTRY MOBILE


670 NTR INDIA 12345678
671 NBK INDIA 543210
772 ram INDIA 9704975

select * from emp1


union
select * from emp2;

EMP_ID EMP_NAME COUNTRY MOBILE


670 NTR INDIA 12345678
671 NBK INDIA 543210
772 ram INDIA 9704975
980 vivek INDIA 4975

=================================================================
union all:

select * from emp1


union all
select * from emp2;

EMP_ID EMP_NAME COUNTRY MOBILE


670 NTR INDIA 12345678
671 NBK INDIA 543210
980 vivek INDIA 4975
670 NTR INDIA 12345678
671 NBK INDIA 543210
772 ram INDIA 9704975
=====================================================================
minus:
=======

select * from emp1


minus
select * from emp2;

EMP_ID EMP_NAMECOUNTRY MOBILE


980 vivek INDIA 4975
======================================================================

select * from emp2


minus
select * from emp1;

EMP_ID EMP_NAMECOUNTRY MOBILE


772 ram INDIA 9704975
=====================================================================

intersect:
------------

select * from emp2


intersect
select * from emp1;

EMP_ID EMP_NAMECOUNTRY MOBILE


670 NTR INDIA 12345678
671 NBK INDIA 543210
=====================================================================

Analytical fuctions or window functions:


----------------------------------------

1.rank() 2).dense_rank() 3.row_number() 4)lead() 5)lag()

1).RANK()

RANK() over(order by columnname asc|desc)

RANK() over(partition by dept_id order by columnname asc|desc)

2).DENSE_RANK() over(order by columnname asc|desc)


====================================================================

Rank()

select emp_id,first_name,salary,dept_id,rank() over(order by sal desc) as


rnk from emp;

select emp_id,first_name,salary,dept_id,rank() over(partition by dept_id


order by sal desc) as rnk from emp;

dense_rank()

select emp_id,first_name,salary,dept_id,dense_rank() over(order by sal


desc) as rnk from emp;

select emp_id,first_name,salary,dept_id,dense_rank() over(partition by


dept_id order by sal desc) as rnk from emp;

row_number()

select emp_id,first_name,salary,dept_id,row_number() over(order by sal


desc) as rnk from emp;

select emp_id,first_name,salary,dept_id,row_number() over(partition by


deptid order by sal desc) as rnk from emp;

=========================================================================
==

top 5 salaries:

select * from
(select emp_id,f_name,salary,dept_id,rank() over(order by salary desc)
rnk from emp) where rnk<=5 ;

=========================================================================
===

second highest :

select * from
(select emp_id,f_name,salary,dept_id,rank() over(order by salary desc)
rnk from emp) where rnk=2 ;

=========================================================================
===

second highest salary department wise:


select * from
(select emp_id,f_name,salary,dept_id,rank() over(partion by dept_id order
by salary desc) rnk from emp) where rnk=2 ;
=========================================================================

second highest salary deptwise and department name also should present.

select e.*,d.department_name from


(select emp_id,f_name,salary,dept_id,rank() over(partion by dept_id order
by salary desc) rnk from emp) e
join department d on e.dept_id=d.dept_id where rnk=2 ;
=========================================================================
===

LEAD():

if you want to know who is joined after this person.

select emp_id,emp_name,salary,hire_date,email,phone_number,dept_id,
lead(hire_date) over(order by hire_date) after_hire,
lead(first_name) over(order by hire_date) after_hire
from emp;

=========================================================================
========

LAG()
IF you wanted to know who is joined before this person.

select emp_id,emp_name,salary,hire_date,email,phone_number,dept_id,
lag(hire_date) over(order by hire_date) before_hire,
lag(first_name) over(order by hire_date) before_hire
from emp;

=========================================================================
=======
=========================================================================
==

=========================================================================
==

=========================================================================
===

=========================================================================
====

You might also like