You are on page 1of 22

Queries

1:select * from all_objects where object_name like '%JOB%' and object_type = 'TABLES';

2:select * from emp where hiredate>'01-JAN-81';

3:select * from emp where hiredate>'01-JAN-81' and sal>=3000;

4:select * from emp where deptno in(20,30);

5:select * from emp where ename like 'S%';

6:select * from emp where ename like 'S%T'; name starts with S and ends with T

7:select * from emp where mgr is null;

8:Select * from emp

Use of nvl

where nvl(mgr,0)=0;

9:Select * from emp where deptno=20 or deptno=100;

10:Select * from emp where deptno not in(20,30);

11:Select sal + 50 from emp;

12:Select * from emp where (deptno=20 or deptno=30) and sal>=3000;

13:Select * from emp order by hiredate;

14:Select * from emp order by hiredate desc;

15:Select * from emp where deptno='&deptno';

16:Select * from emp where deptno='&&deptno' and job='&job';

17:select empno,ename,job,&column from emp where &condition order by &column;

18:select sysdate from dual;

19:select count (*) from emp;

20:select initcap(ename),lower(ename),upper(ename),concat(ename,mgr),substr(ename,1,3),length(ename) from emp;

21:select * from emp where substr(job,6,8)='MAN';

or

select * from emp where substr(job,6)='MAN';

22:select instr(job,'MAN') from emp;

23:select substr(job,instr(job,'MAN')) from emp;

24:select * from emp where substr(job,instr(job,'MAN'))='MAN';

25:Select * from emp where substr(job,instr(job,'MAN'),instr(job,'MAN')+2)='MAN'

26:Select replace('SMITH','M','A') from emp;

27:Select trim('H' from 'SMITH') new from emp;

28:select ename,instr(ename,'A') new from emp;

29:select trunc(99.99,-1),trunc(99.29,-1)

from dual;

30:select to_char(sysdate,'DD MONTH YYYY') from dual;

31:select to_char(sysdate,'DD MONTH YYYY hh:mm:ss') from dual;

32:select to_char(sysdate,'DD MONTH YYYY hh:mi:ss') from dual;

33:select months_between(sysdate,hiredate) months from emp;

34:select months_between(sysdate,hiredate) months,(sysdate-hiredate)/7 weeks from emp;

35:select commission_pct, nvl(commission_pct ,.5) newcommission from employees;

36:select first_name||' '||last_name "Full Name" from employees;

37:select concat(concat(first_name,' '),last_name) "Full Name" from employees;

or

select concat(first_name,concat(' ',last_name)) "Full Name" from employees;

38:select 'Employee ' || concat(first_name,concat(' ',last_name))||' '|| 'belong''s to'||' '|| department_id from employees;

39:SELECT upper(first_name),upper(concat(last_name,'_US')) new from employees;

39:select round(to_date('25-JUL-03'),'MONTH') Important from dual;

40:SELECT concat(first_name,last_name), TO_CHAR(hire_date, 'fmDdspth "of" Month RR fmHH24:MI:SS AM') HIREDATE FROM employees;

41:SELECT to_char(salary,'$00000') from employees;

41:Select last_name, job_id, salary, CASE job_id WHEN 'IT_PROG' THEN 1.10*salary

WHEN 'ST_CLERK' THEN 1.15*salary WHEN 'SA_REP' THEN 1.20*salary ELSE salary END "REVISED SALARY" from employees;

42:Select count(*),department_id from employees group by department_id;

***********************Home Work*****************************************

1:Select last_name from employees where last_name like 'J%' or last_name like 'K%' or last_name like 'L%' or last_name like 'M%';

or

Select last_name from employees where substr(last_name,1,1)in('J','K','L','M');

2:select * from employees where job_id='ST_CLERK' and hire_date > '31-DEC-1997';

3:select last_name,job_id,salary,commission_pct from employees where commission_pct is not null order by commission_pct desc;

4:SELECT last_name,salary,commission_pct, DECODE(commission_pct,null,'No','Yes') REVISED_SALARY FROM employees;

5:Select last_name,months_between(sysdate , hire_date)/12 "Length Of Service",trunc(months_between(sysdate , hire_date)/12) "Years", trunc(mod(months_between(sysdate , hire_date),12)) "Months" from employees order by months_between(sysdate , hire_date)/12 desc;

*****************************Joins***************************************

43:select emp.first_name,emp.last_name,emp.employee_id,emp.department_id,dept.department_name from employees emp, departments dept

where emp.department_id=dept.department_id;

44:select e.first_name,e.last_name,e.employee_id,e.department_id,d.department_name from employees e , departments d where e.department_id=d.department_id and e.department_id=20;

45:select e.first_name,e.last_name,e.employee_id,e.department_id,d.department_name,l.location_id from employees e join departments d on d.department_id=e.department_id join locations l on d.location_id=l.location_id;

46:select e.first_name,e.last_name,e.department_id,d.department_name from employees e left outer join departments d on e.department_id=d.department_id;

47:select e.first_name,e.last_name,e.department_id,d.department_name from employees e right outer join departments d on e.department_id=d.department_id;

48:Select * from employees where job_id in (select job_id from employees where employee_id=141);

49:select last_name,job_id,salary from employees where salary=(select min(salary) from employees);

******We cannot use group functions with where clause,we have to use having clause for this......

50:select department_id,avg(salary) from employees group by department_id having avg(salary)>8000;

**************************Home Work************************************** 1:Select e.first_name,e.last_name,e.employee_id,m.employee_id manager_id,m.first_name,m.last_name from employees e join employees m on (e.manager_id=m.employee_id);

2:select e.last_name employee,m.last_name manager,m.salary manager_salary,j.grade_level from employees e join employees m on (e.manager_id=m.employee_id) join job_grades j on m.salary BETWEEN j.lowest_sal AND j.highest_sal where m.salary>15000;

3:select d.department_id "Department id" , d.department_name "Department name",count(employee_id) "Number of employees",avg(salary) "Average salary",f.last_name,f.salary,f.job_id

from employees e join departments d on e.department_id=d.department_id join employees f on (f.department_id = e.department_id) group by d.department_id ,d.department_name,f.job_id,f.last_name,f.salary order by department_name;(Sure)

4:Select department_id,min(salary) from employees group by department_id having avg(salary) = (Select max(avg(salary)) from employees group by department_id);

5:select e.department_id "Department id" , d.department_name "Department name",count(employee_id) "Number of employees" from employees e join departments d on e.department_id=d.department_id group by e.department_id ,d.department_name having count(employee_id)=(select max(count(employee_id)) from employees group by department_id);

*************************Set Operators***********************************

51:select employee_id,department_id,job_id,null start_date,null end_date from employees union

select employee_id,department_id,job_id,start_date,end_date from job_history order by employee_id;

52:column page noprint select 'I am learning',2 page from dual union select 'Oracle',1 page from dual order by page;

***************************PL/SQL**************************************** 1:set serveroutput on begin dbms_output.put_line('Hello World'); end;

2:Select 'father'||''''||'s day' New from dual;

3:set serveroutput on variable basic_percent number; variable pf_percent number; set autoprint on declare today date:=sysdate; tomorrow today %type;

begin :basic_percent:=45; :pf_percent:=12; tomorrow := sysdate+1; dbms_output.put_line('Hello World'); dbms_output.put_line('Today is -' || today ); dbms_output.put_line('Tomorrow is -' || tomorrow ); end;

4:set serveroutput on DECLARE weight NUMBER(3) := 600; message VARCHAR2(255) := 'Product 10012'; new_locn VARCHAR2(50) := 'Asia'; BEGIN DECLARE weight NUMBER(3) := 1; message VARCHAR2(255) := 'Product 11001'; new_locn VARCHAR2(50) := 'Europe'; BEGIN weight := weight + 1; new_locn := 'Western ' || new_locn; dbms_output.put_line('weight at position 1 is ' || weight); dbms_output.put_line('Location at position 1 is ' ||new_locn); END;

weight := weight + 1; message := message || ' is in stock'; new_locn := 'Western ' || new_locn; dbms_output.put_line('Message at position 2 is ' || message); dbms_output.put_line('Weight at position 2 is ' || weight); dbms_output.put_line('Location at position 2 is ' ||new_locn); END;

5:set serveroutput on DECLARE customer VARCHAR2(50) := 'Womansport'; credit_rating VARCHAR2(50) := 'EXCELLENT'; name VARCHAR2(25) := 'Adidas'; BEGIN DECLARE customer NUMBER(7) := 201; name VARCHAR2(25) := 'Unisports'; BEGIN credit_rating :='GOOD'; dbms_output.put_line('Customer at position 1 is '||customer); dbms_output.put_line('Name at position 1 is '||name); dbms_output.put_line('Credit rating at position 1 is '||credit_rating); END; dbms_output.put_line('Customer at position 2 is '||customer); dbms_output.put_line('Name at position 2 is '||name);

dbms_output.put_line('Credit rating at position 2 is '||credit_rating); END;

6:set serveroutput on --Creating bind variables variable basic_percent number; variable pf_percent number; set autoprint on declare today date:=sysdate; tomorrow today %type; fname varchar2(15); emp_sal number(10); begin /* Initializing the bind variables */ tomorrow := sysdate+1; SELECT first_name, salary INTO fname, emp_sal FROM employees WHERE employee_id=100; :basic_percent:=.45 * emp_sal; :pf_percent:=.12 * :basic_percent; dbms_output.put_line('Hello '|| fname); dbms_output.put_line('Your salary is ' || emp_sal ); dbms_output.put_line('Your contribution towards PF is ' || :pf_percent );

end;

7:set verify off set serveroutput on ACCEPT empno PROMPT 'Please enter your employee id.' --Creating bind variables variable basic_percent number; variable pf_percent number; set autoprint on declare today date:=sysdate; tomorrow today %type; fname varchar2(15); emp_sal number(10); empno number(10)= &empno; begin /* Initializing the bind variables */ tomorrow := sysdate+1; SELECT first_name, salary,employee_id INTO fname, emp_sal,empno FROM employees WHERE employee_id=100; :basic_percent:=.45 * emp_sal;

:pf_percent:=.12 * :basic_percent; dbms_output.put_line('Hello '|| fname); dbms_output.put_line('Your salary is ' || emp_sal ); dbms_output.put_line('Your contribution towards PF is ' || :pf_percent ); end;

8: set serveroutput on declare empno number(6) := &empno; deptno number(6) := &deptno; begin EXECUTE IMMEDIATE ' create table employee_details as select * from employees '; end;

********************PL/SQL Practice 4************************************

1: set serveroutput on declare max_deptno number(10); begin select max(department_id) into max_deptno from departments; dbms_output.put_line('The maximum department no. is '||max_deptno); end;

2:set serveroutput on variable dept_id number; set autoprint on declare max_deptno number; dept_name departments.department_name%type:='Education'; begin

select max(department_id) into max_deptno from departments;

:dept_id:=max_deptno + 10;

INSERT into departments(department_id,department_name, location_id) values( :dept_id, dept_name, null);

dbms_output.put_line('The maximum department no. is '||max_deptno); dbms_output.put_line('sql%rowcount gives '||sql%rowcount); end; / select * from departments where department_id=:dept_id;

3: set serveroutput on variable dept_id number;

set autoprint on declare max_deptno number; dept_name departments.department_name%type:='Education'; begin

select max(department_id) into max_deptno from departments;

:dept_id:=max_deptno + 10;

INSERT into departments(department_id,department_name, location_id) values( :dept_id, dept_name, null);

delete from departments where department_id=1000;

update departments set location_id=3000 where location_id=:dept_id;

/**dbms_output.put_line('The maximum department no. is '||max_deptno); dbms_output.put_line('sql%rowcount gives '||sql%rowcount);**/ end; / select * from departments

where department_id=:dept_id; delete from departments where department_id=:dept_id;

***********************Practice 5**************************************** 1: BEGIN FOR i in 1..10 LOOP IF i = 6 or i = 8 THEN null; ELSE INSERT INTO messages(results) VALUES (i); END IF; END LOOP; COMMIT; END; / select * from messages;

2:

****************************Practice 6*********************************** 1:set serveroutput on set verify off define countryid = CA; declare country_record countries%Rowtype; begin select * into country_record from countries where country_id = '&countryid';

dbms_output.put_line('country id: '||country_record.country_id); dbms_output.put_line('country name: '||country_record.country_name); dbms_output.put_line('region: '||country_record.region_id); end;

2: