You are on page 1of 8

Database Queries:

1. Display those employees whose salary keeps decimal digit?


Ans: select empno, ename, sal from emp where sal like %.%
2. Display 3% of salary upto two decimal digit?
Ans: select empno, ename, to_char(sal,9999.99) from emp
3. Display the employees whose comm. Is more than 25% of his salary?
4. Display the name of 05 chars and ends with S
5. Display the dept which having 2 or more employees of same job
6. Display the employees who earn the highest salary in each department.
Ans: SELECT a.last_name, a.salary, a.department_id, b.maxsal
FROM employees a,
( SELECT department_id, max(salary) maxsal
FROM employees
GROUP BY department_id ) b
WHERE a.department_id = b.department_id
AND a.salary = b.maxsal;
Analysis:
1. find the max salary of each department by using group by function
2. Assign this select statement for table B
3. Table A & Table B compare with department id & table A sal with Table B
maxsal

7. To delete duplicate Rows in table if primary key not assigned


delete from r_emp r1 where rowid < (select max(rowid) from r_emp r2 where
r1.emp_id=r2.emp_id)

Date format changes query, to change date format like MMDDYYYY to


YYYYMMDD
select to_char(to_date('06302011','MMDDYYYY'),'YYYYMMDD')
Replacing the space inbetween the words in a string
select replace(trim(lgl_acct_desc),chr(32),'') from SAPU_BLTB_STG1;
Input: Beginning retained earnings

out put: Beginningretainedearnings


changing the date value to claender month end day
last_day<column name>

Changing date to specified format


to_char(sysdate,'DD-MON-YYYY hh:mm:ss AM') output is 06-SEP-2011
10:09:06 AM

to_date : In Oracle/PLSQL, the to_date function converts a string to a date.


To_char : In Oracle/PLSQL, the to_char function converts a number or date to a string.

to_date('2003/07/09',
'yyyy/mm/dd')

would return a date value of July 9, 2003.

select * from error_log where to_char(err_date,'DD-MONYY')=to_char(sysdate,'DD-MON-YY')

to access the other schema tables in one schema

Nth Highest Salary


SELECT SALARY FROM EMP e1 WHERE (n-1) = (SELECT COUNT(*)
FROM EMP e2 WHERE e2.SALARY>e1.SALARY)

To know null values records count

Select count (NVL (x103_tax_exempt, 0)) from B2STG.GPDJW_X103_GC01IWB_STG1;

grant select on country to B2STG;

create table RTL as select x103_acct_num from GPDJW_X103_GC01IWB_STG1 GP left


outer join

CONSMR_CNTRCT_APPLICATION_STG1 CC on cc.acct_num= GP.x103_acct_num


where GP.x103_curr_os_ind=1 and gp.x103_LOB_CD='L' and cc.LOAN_TYPE_CD in
('B','D','M','J','L','P','R','W','Z');

To find all email addresses in a table that exists more than once:
SELECT email, COUNT(email) AS NumOccurrences FROM users GROUP BY email
HAVING ( COUNT(email) > 1 )
To find rows that occurs exactly once:
SELECT email FROM users GROUP BY email HAVING ( COUNT(email) = 1 )
SELECT COUNT FROM ((SELECT X103_CONTRACT_DT FROM
gpdjw_x103_gc01Iwb_stg1
MINUS
select d_exposure_start_date FROMb2rvl.stg_loan_contracts))
select * from error_log where to_char(err_date,'YYYY-MM-DD') ='2011-12-26'
order by err_date
select * from error_log where err_src='SAPU file'and to_char(Err_Date,'YYYYMM-DD')=To_char(sysdate,'YYYY-MM-DD')

Self join to know employee who is reporting to whom


select e2.eno,e2.ename,e2.supname,e1.dnum from emp e1
join emp e2 on e1.eno= e2.supno
In Query
select distinct BR_NUM from b2stg.stg_dealer_concat_stg1
where TRIM(FUNDING_SOURCE)||TRIM(DEALER_ID)||TRIM(LOAN_TYPE_CD)||TRIM(ACCT_NUM) in
(select v_exposure_id from b2stg.stg_lc_contracts_stg2 where v_lv_code is null
)

Composite key
A composite key is a primary key that consists of more than one column. Oracle
will create a composite primary key index on the table to implement and enforce the
composite key. Composite keys are also known as concatenated or aggregate
keys.

select emp.emple_id, emp.EMPLE_SUR_NM,tm.team_id,emp.team_role_id,act.actv_id,


act.actv_nm ,ipr.emple_dly_ipr_dt,ipra.EMPLE_DLY_ACTV_UNIT_QTY
from sx6gcso.CS_EMPLE_DLY_IPR_ACTIVITY_T ipra
inner join sx6gcso.cs_employee_daily_ipr_t ipr on ipr.emple_dly_ipr_id =
ipra.emple_dly_ipr_id
inner join sx6gcso.cs_employee_t emp on emp.emple_id=ipr.emple_id
inner join sx6gcso.cs_team_role_t tr on ipr.team_role_id= tr.TEAM_role_id
inner join sx6gcso.cs_team_t tm on ipr.team_id = tr.team_id
inner join sx6gcso.cs_area_t ar on ipr.area_id= ar.area_id
inner join sx6gcso.cs_activity_t act on act.area_id = ar.area_id and
act.actv_id = ipra.actv_id
where ar.area_id=3 and emp.emple_stat_cd='N' and tm.team_id=11 and
ipr.emple_dly_ipr_dt = to_date('09-05-2011','DD-MM-YYYY')
and emple_id=52;

select emp.emple_id, emp.EMPLE_SUR_NM,


--tm.team_id,emp.team_role_id,
act.actv_id, act.actv_nm

,ipr.emple_dly_ipr_dt,ipra.EMPLE_DLY_ACTV_UNIT_QTY

from sx6gcso.CS_EMPLE_DLY_IPR_ACTIVITY_T ipra


inner join sx6gcso.cs_employee_daily_ipr_t ipr on ipr.emple_dly_ipr_id =
ipra.emple_dly_ipr_id
inner join sx6gcso.cs_employee_t emp on emp.emple_id=ipr.emple_id
--inner join sx6gcso.cs_team_role_t tr on ipr.team_role_id= tr.TEAM_role_id
--inner join sx6gcso.cs_team_t tm on ipr.team_id = tr.team_id
inner join sx6gcso.cs_area_t ar on ipr.area_id= ar.area_id
inner join sx6gcso.cs_activity_t act on act.area_id = ar.area_id and
act.actv_id = ipra.actv_id
where ar.area_id=3 and emp.emple_stat_cd='N'

--and tm.team_id is not null


and ipr.emple_dly_ipr_dt = to_date('09-05-2011','DD-MM-YYYY')
and actv_id=200;

select emp.emple_id, emp.EMPLE_SUR_NM,tm.team_id,emp.team_role_id,act.actv_id,


ipr.emple_dly_ipr_dt,ipra.EMPLE_DLY_ACTV_UNIT_QTY from
sx6gcso.CS_EMPLE_DLY_IPR_ACTIVITY_T ipra
inner join sx6gcso.cs_employee_daily_ipr_t ipr on ipr.emple_dly_ipr_id =
ipra.emple_dly_ipr_id
inner join sx6gcso.cs_employee_t emp on emp.emple_id=ipr.emple_id
inner join sx6gcso.cs_team_role_t tr on emp.team_role_id= tr.TEAM_role_id
inner join sx6gcso.cs_team_t tm on tm.team_id = tr.team_id
inner join sx6gcso.cs_area_t ar on tm.area_id= ar.area_id
inner join

sx6gcso.cs_activity_t act on act.area_id = ar.area_id

where ar.area_id=3 and emp.emple_stat_cd='N' and tm.team_id=11 and


ipr.emple_dly_ipr_dt = to_date('09-05-2011','DD-MM-YYYY');

RANK
Let's assume we want to assign a sequential order, or rank, to people within a department based on
salary, we might use the RANK function like.
SELECT empno,
deptno,
sal,
RANK() OVER (PARTITION BY deptno ORDER BY sal) "rank"
FROM
emp;
EMPNO
DEPTNO
SAL
rank
---------- ---------- ---------- ---------7934
10
1300
1
7782
10
2450
2
7839
10
5000
3
7369
20
800
1
7876
20
1100
2
7566
20
2975
3
7788
20
3000
4
7902
20
3000
4
7900
30
950
1
7654
30
1250
2
7521
30
1250
2

7844
7499
7698

30
30
30

1500
1600
2850

4
5
6

SQL>
What we see here is where two people have the same salary they are assigned the same rank. When
multiple rows share the same rank the next rank in the sequence is not consecutive.

DENSE_RANK
The DENSE_RANK function acts like the RANK function except that it assigns consecutive ranks.
SELECT empno,
deptno,
sal,
DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal) "rank"
FROM
emp;
EMPNO
DEPTNO
SAL
rank
---------- ---------- ---------- ---------7934
10
1300
1
7782
10
2450
2
7839
10
5000
3
7369
20
800
1
7876
20
1100
2
7566
20
2975
3
7788
20
3000
4
7902
20
3000
4
7900
30
950
1
7654
30
1250
2
7521
30
1250
2
7844
30
1500
3
7499
30
1600
4
7698
30
2850
5
SQL>

FIRST and LAST


The FIRST and LAST functions can be used to return the first or last value from an ordered sequence.
Say we want to display the salary of each employee, along with the lowest and highest within their
department we may use something like.
SELECT empno,
deptno,
sal,

MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY


deptno) "Lowest",
MAX(sal) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno)
"Highest"
FROM
emp
ORDER BY deptno, sal;
EMPNO
DEPTNO
SAL
Lowest
Highest
---------- ---------- ---------- ---------- ---------7934
10
1300
1300
5000
7782
10
2450
1300
5000
7839
10
5000
1300
5000
7369
20
800
800
3000
7876
20
1100
800
3000
7566
20
2975
800
3000
7788
20
3000
800
3000
7902
20
3000
800
3000
7900
30
950
950
2850
7654
30
1250
950
2850
7521
30
1250
950
2850
7844
30
1500
950
2850
7499
30
1600
950
2850
7698
30
2850
950
2850

Hierarchy query

10
0

10
1
55
5

55
88
Data in Table

45
5

10
3

10
2
18
0

57
8

36
0

26
0

26
3

26
1

27
0

Query to find root parent employee id is


SELECT eid "EmployeeNo", CONNECT_BY_ROOT eid "Managerid",
LEVEL-1 "EmpLevel"

FROM hier WHERE LEVEL > 1

CONNECT BY NOCYCLE PRIOR eid = mid

You might also like