Professional Documents
Culture Documents
1:select * from all_objects where object_name like '%JOB%' and object_type = 'TABLES';
6:select * from emp where ename like 'S%T'; name starts with S and ends with T
Use of nvl
where nvl(mgr,0)=0;
or
29:select trunc(99.99,-1),trunc(99.29,-1)
from dual;
or
38:select 'Employee ' || concat(first_name,concat(' ',last_name))||' '|| 'belong''s to'||' '|| department_id from employees;
40:SELECT concat(first_name,last_name), TO_CHAR(hire_date, 'fmDdspth "of" Month RR fmHH24:MI:SS AM') HIREDATE 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;
***********************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
3:select last_name,job_id,salary,commission_pct from employees where commission_pct is not null order by commission_pct desc;
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***************************************
where emp.department_id=dept.department_id;
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;
48:Select * from employees where job_id in (select job_id from employees where employee_id=141);
******We cannot use group functions with where clause,we have to use having clause for this......
**************************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***********************************
52:column page noprint select 'I am learning',2 page from dual union select 'Oracle',1 page from dual order by page;
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);
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;
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
:dept_id:=max_deptno + 10;
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;
:dept_id:=max_deptno + 10;
/**dbms_output.put_line('The maximum department no. is '||max_deptno); dbms_output.put_line('sql%rowcount gives '||sql%rowcount);**/ end; / select * from departments
***********************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';
2: