0% found this document useful (0 votes)
306 views18 pages

SQL Queries

This document contains multiple SQL queries using date/number formatting functions like TO_CHAR, TO_DATE, TO_NUMBER, NVL, NVL2, DECODE, CASE, and aggregate functions like COUNT, SUM, AVG, MIN, MAX to format dates, numbers, handle null values, and perform calculations on employee data from the HR schema. The queries demonstrate various ways to format dates, numbers, concatenate strings, handle null values, and calculate statistics.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
306 views18 pages

SQL Queries

This document contains multiple SQL queries using date/number formatting functions like TO_CHAR, TO_DATE, TO_NUMBER, NVL, NVL2, DECODE, CASE, and aggregate functions like COUNT, SUM, AVG, MIN, MAX to format dates, numbers, handle null values, and perform calculations on employee data from the HR schema. The queries demonstrate various ways to format dates, numbers, concatenate strings, handle null values, and calculate statistics.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 18

select to_char(sysdate,'ddth Yyyysp') from dual; select to_char(sysdate,'fmmonth,yyyyspth') from dual; select first_name,last_name, to_char(hire_date,'fmDay, " the "

fmddth " of " fmM onth, Yyyysp "."') START_DATE from employees where upper(to_char(hire_date,'FMDAY'))='SATURDAY'; select select select select select select select select to_date('25-dec-2010') from dual; to_date('25-DEC') from dual;--encounter an error to_date('25-DEC','DD-MON') from dual; to_date('25-dec-2010 18:03:45','dd-mon-yyyy HH24:MI:SS')from dual; to_date('25-dec-2010 18/03/45','dd-mon-yyyy hh24/MI/ss') from dual; to_date('25-dec-2010 18/03/45','dd-mon-yyyy hh24:MI:ss') from dual; to_date('25-dec-10','fxDD-MON-YYYY') from dual;--generates an error to_date('25-dec-2010','fxDD-MON-YYYY') from dual;

select first_name,last_name,hire_date from employees where hire_date>to_char('01/12/2000') order by hire_date; select first_name from employees where to_char(hire_date,'dd-mon')=to_char(sysdate,'dd-mon'); select to_number('$1,000.55') FROM dual;--generates error select to_number('$1,000.55','$999,999.99') from dual; select first_name,phone_number,to_number(substr(phone_number,5),'999.9999')*1000 0 LOCAL_NUMBER from employees where department_id=30; select to_number(123.45,'999.9') from dual; select to_char(123.45,'999.9') FROM dual; select length(to_char(to_date('28/10/09','dd/mm/rr'),'fmMonth')) from dual; select to_char(salary,'$99,999') salary from employees; select to_char(salary,'$99G999') salary from employees; select '$'||substr(salary,1,mod(length(salary),3))||','||substr(salary,mod(lengt h(salary),3)+1) from employees; select nvl(1234) from dual;--generates error select nvl(null,1234) from dual; select nvl(substr('abc',4),'No Substring exist!') from dual; select Last_name, salary, commission_pct, (nvl(commission_pct,0)*salary+100) "Mo nthly Commission" from employees where last_name like 'E%'; select Last_name,salary, commission_pct, commission_pct*100+1000 Montly_Salary from employees where last_name like 'E%'; select nvl2(1234,1,'Not Null') from dual;--generates error select nvl2(null,124,4231) from dual;

select nvl2(124,135,4) from dual; select nvl2(substr('abc',2),'Not bc','No Substring.') from dual; select nullif(123,123) from dual; select nullif(124,123) from dual; select nullif(234,233+1) from dual; select nullif('24-JUL-2009','24-JUL-09') from dual; -- the two strings above are not same be carefull that those are consodered as s tring not date select nullif(to_date('24-JUL-2009'),to_date('24-JUL-09')) from dual; select last_name,salary,commission_pct, nvl2(commission_pct,'Commission Earner','Not a Commission Earner') Employee_type from employees where last_name like 'G%'; select first_name,last_name,email, nvl2(nullif(substr(first_name,1,1)||upper(last_name), email),'Email does not mat ch Pattern','Match Found!') pattern from employees where length(first_name)=4; SELECT first_name,last_name, Department_id, nvl2(nullif(length(first_name),length(last_name)),'Different Length','Same Lengt h') Name_lengths from employees where department_id=100; select coalesce(null,null,null) from dual; select coalesce(null,null,null,'a string') from dual; select coalesce(substr('abc',4),'Not bc','No substring') from dual; select coalesce(state_province,postal_code,city),postal_code,state_province,city from locations where country_id in ('UK','IT','JP'); SELECT decode(1234,123,'123 is a match.') from dual; select decode(1234,123,'123 is a match.','123 is not a Match.') from dual; select decode('search','comp1','true1','comp2','true2','search','true3', substr('2search',2,6),'true4','false') from dual; select distinct country_id,decode(country_id,'DR','Southern Hemisphere','AU','So uthern Hemisphere' ,'Northern Hemisphere') Hemisphere from countries order by hemisphere; select case substr('1234',1,3) when '134' then '134 is a Match.' when '124' then '124 is a Match.' when concat('1','23') then concat('1','23') || ' is a Match.' else 'No match found!' end from dual; select last_name,hire_date,trunc(months_between(sysdate,hire_date)/12) Years, trunc(months_between(sysdate,hire_date)/60) "Years Divide by 5", case trunc(months_between(sysdate,hire_date)/60) when 0 then 'Intern' when 1 then 'Junior'

when 3 then 'Senior' when 2 then 'Intermediate' else 'Furniture' end Loyalty from employees where department_id in (60,10,30); select last_name,hire_date,trunc(months_between(sysdate,hire_date)/12) years, trunc(months_between(sysdate,hire_date)/60) "Year Divide by 5", case when trunc(months_between(sysdate,hire_date)/60) <1 then 'Intern' when trunc(months_between(sysdate,hire_date)/60) <2 THEN 'Junior' when trunc(months_between(sysdate,hire_date)/60) <3 THEN 'Intermediate' when trunc(months_between(sysdate,hire_date)/60) <4 THEN 'Senior' else 'Furniture' END Loyalty FROM employees where department_id in (60,10,30); select state_province, decode(state_province,'Washington','Headquarters','Texas','Oil Wells','Californi a', city,'New Jersey', street_address) Location_info from locations where country_id='US' order by location_info; select to_number(123.45,'999999.9') from dual;//It will generates an err select to_number(123.45,'999999.99') from dual; select to_char(123.45,'999999.9') from dual; select to_char(sysdate,'fmMONTH,YEAR') FROM dual; select to_char(sysdate,'DDth MONTH') from dual; select to_char(to_date(to_char(sysdate,'DD'),'DD'),'YEAR') from dual; select nvl2(nullif('CODA','SID'),'SPANIEL','TERRIER') from dual; select nvl(substr('AM I NULL',10),'YES I AM') FROM dual; select decode(to_char(sysdate,'MM'),'02','TAX DUE','PARTY') FROM dual; -- RUN THE FOLLOWING QUERY IN OE USER select cust_first_name, cust_last_name, cust_email, date_of_birth, decode(to_char(date_of_birth,'dd') - to_char(sysdate,'dd'),-2,'Day Before Yester day',-1,'Yesterday', 0,'Today',1,'Tomorrow',2,'Day after Tomorrow','Later this week') Birthday from customers where to_char(date_of_birth,'MM')=to_char(sysdate,'MM') and to_number(to_char(date_of_birth,'dd')) between to_char(sysdate,'dd')-2 and t o_char(sysdate,'dd')+7 order by to_char(date_of_birth,'dd,mm'); -- Above query in different way select cust_first_name, cust_last_name, cust_email, date_of_birth, case to_char(date_of_birth,'dd')-to_char(sysdate,'dd') when -2 then 'Day Before Yesterday' when -1 THEN 'Yesterday'

when 0 then 'Today' when 1 then 'Tomorrow' when 2 then 'Day after Tomorrow' else 'Later in this Week' end Birthday from customers where to_char(date_of_birth,'MON')=TO_CHAR(sysdate,'MON') AND to_char(date_of_birth,'dd') - to_char(sysdate,'dd') between -2 and 7 order by to_char(date_of_birth,'DDMM'); ---------------------------------------------------------------------select count(*), department_id from employees group by department_id order by department_id; select count(distinct department_id) from employees; select count(all department_id) from employees; select count(*) from employees; select count(commission_pct) from employees; select count(distinct commission_pct) from employees; select count(hire_date),count(manager_id) from employees; select count(*),count(distinct nvl(department_id,0)),count(distinct nvl(job_id,0 )) from employees; select avg(department_id) from employees; select avg(distinct department_id) from employees; select avg(all department_id) from employees; select select select select select avg(commission_pct) from employees; avg(nvl(commission_pct,0)) from employees; avg(nvl(commission_pct,1)) from employees; avg(salary),avg(distinct salary) from employees; avg(2) from employees;

select last_name,job_id,(Sysdate-hire_date)/365.25 "Years Worked" from employees where job_id='IT_PROG'; select avg((sysdate-hire_date)/365.25) FROM employees where job_id='IT_PROG'; select sum(distinct department_id) from employees; select sum(department_id) from employees; select select select SELECT select select select select select select select select select sum(commission_pct) from employees; sum(nvl(commission_pct,0)) from employees; sum(nvl(commission_pct,1)) from employees; sum(3) from employees; sum(salary),sum(DISTINCT salary) FROM employees; sum(sysdate-hire_date)/365.25 "Total Year worked by All" from employees; sum(hire_date) from employees;-- Generates error max(distinct department_id),min(department_id) from employees; max(hire_date),min(hire_date) from employees; max(end_date),min(end_date) from job_history; max(commission_pct),min(commission_pct) FROM employees; max(job_id),min(job_id) from employees; min(salary),max(salary) FROM employees where job_id='SA_REP';

select round(avg(length(country_name))) from countries;

select count(distinct job_id) from employees; select count(*) "Total No. of Employees", sum(salary) "Total Salary",min(salary) "Min Salary", max(Salary) "Max Salary" from employees; select sum(commission_pct), nvl(department_id,0) from employees where nvl(department_id,0) in (40,80,0) group by department_id; select avg(sum(commission_pct)) FROM employees where nvl(department_id,0) in (40,80,0) group by department_id; select sum(avg(length(last_name))) FROM employees group by department_id; select count(distinct nvl(department_id,0)) from employees; select distinct department_id from employees; select max(salary), count(*) FROM employees group by department_id order by department_id; select variance(department_id) from employees; select variance(distinct department_id) from employees; select STDDEV(department_id) from employees; select stddev(distinct department_id) from employees; select emp.employee_id,department_id,emp.manager_id,departments.manager_id from employees emp join departments using (department_id) where department_id>80; select * from locations natural join countries; select * from locations,countries where locations.country_id=countries.country_id; SELECT * from jobs natural join countries; select * from jobs, countries; select * from regions natural join countries where country_id='US'; select * from sales_regions natural join countries where country_id='US'; -- Generates Error desc Employees; desc job_history; select employee_id,job_id,department_id, emp.last_name, hire_date, jh.end_date from employees emp natural join job_history jh; select * from locations join countries using(country_id); select * from locations, countries where locations.country_id=countries.country_id; select * from jobs join countries using;-- generates error

select emp.last_name, emp.department_id, jh.end_date, job_id, employee_id from job_history jh join employees emp using(employee_id,job_id); select * from departments d join employees e on (e.employee_id=d.department_id); select * from departments d,employees e where e.employee_id=d.department_id; select E.employee_id,e.last_name,j.start_date,e.hire_date,j.end_date,j.job_id pr evious_job, e.job_id current_job from employees E join job_history j on (j.start_date=e.hire_date); select e.first_name||' '||e.last_name||' is manager of the '|| d.department_name ||' department.' Managers from employees e join departments d on(e.employee_id=d.manager_id); select r.region_name,c.country_name,l.city, d.department_name from departments d natural join locations l natural join countries c natural join regions r; SELECT r.region_name,c.country_name,l.city,d.department_name from departments d join locations l ON (d.location_id= l.location_id) join countries c on (c.country_id= l.country_id) join regions r on (r.region_id= c.region_id); select r.region_name,c.country_name,l.city,d.department_name from departments d join locations l using(location_id) join countries c using(country_id) join regions r using(region_id); select d.department_name from departments d join locations l on(l.location_id=d.location_id) where d.department_name like 'P%'; select d.department_name from departments d join locations l on (l.location_id= d.location_id and d.department_name like 'P% '); select r.region_name,c.country_name,l.city,d.department_name,e.last_name,e.salar y from employees e join departments d on(e.department_id= d.department_id and e.salary>12000) join locations l on(l.location_id=d.location_id) join countries c on(c.country_id= l.country_id) join regions r on (r.region_id=c.region_id); SELECT e.job_id current_job,last_name ||' can earn twice their salary by changin g job to: '|| j.job_id Options,e.salary Current_salary, j.max_salary potential_max_salary from employees e join jobs j on (2*e.salary<j.max_salary) where e.salary>5000 order by last_name;

select * from family; select f1.Name Mum,F3.name Dad, F2.Name Child from family f1 join Family f2 on(f2.mother_id=f1.id) join family f3 on(f2.father_id= f3.id); select e.last_name,e.employee_id,m.last_name,e.department_id from employees e join employees m on(e.manager_id=m.employee_id and e.department_id in (10,20,30) ) order by e.department_id; select e.employee_id,e.department_id EMP_DEpt_id,d.department_id dept_dept_id,d. department_name from departments d left outer join employees e on (d.department_id= e.department_id) where d.department_name like 'P%'; select e.employee_id,e.department_id emp_dept_id,d.department_id dept_dept_id,d. department_name from departments d join employees e on (d.department_id=e.department_id) where d.department_name like 'P%'; select city,l.location_id "L.Location_ID", D.location_id "D.Location_ID" from locations l left outer join departments d on (l.location_id=d.location_id); select e.last_name,d.department_name from departments d right outer join employees e on (d.department_id=e.department_id) where e.last_name like 'G%'; select jh.job_id "JOBS IN JOB_HISTORY",E.JOB_ID "JOBS IN EMPLOYEES" from job_history jh right outer join employees e on(jh.job_id=e.job_id) order by jh.job_id nulls LAST; select e.last_name,d.department_name from departments d full outer join employees e on (e.department_id= d.department_id) where e.department_id is null; select d.department_name, d.department_id from departments d left outer join employees e on (d.department_id=e.department_id) where e.department_id is null; select employee_id,last_name,region_name from employees join regions on (mod(employee_id,4)+1)= region_id order by employee_id; select department_name,nvl(last_name,'No Employee') from employees right outer join departments using (department_id); select * from jobs cross join job_history;

select * from jobs j cross join job_history jh where j.job_id='AD_PRES'; select r.region_name,c.country_name from regions r cross join countries c where r.region_id in (3,4) order by r.region_name,c.country_name; select count(*) from employees cross join departments; ---------------------- Use following script in OE user account -------------select c.cust_first_name,c.cust_last_name,p.product_name,p.list_price from customers c join orders using(customer_id) join order_items using(order_id) join product_information p using(product_id) where p.list_price>1000; -------------------------------------------------------------------------------select sysdate Today,(select count(*) from departments) Dept_Count, (select count(*) from employees) Employee_Count from dual; select last_name from employees where employee_id in ( select manager_id from employees); select max(salary), country_id from (select salary,department_id,location_id,country_id from employees natural join departments natural join locations) group by country_id; SELECT last_name from employees where salary< (select avg(salary) from employees); select department_name from departments where department_id in (select distinct department_id from employees); -- the following 2 queries will result the same as above query select distinct department_name from departments join employees using(department_id); select department_name from departments inner join employees ON employees.department_id=departments.department_id group by department_name; select avg(salary),country_id from (select salary,department_id,location_id,country_id from employees natural join departments natural join locations) group by country_id; select (select max(salary) from employees)*(select max(commission_pct) from empl oyees)/100 "Max Commission" from dual; select select select select last_name from employees where department_id in ( department_id from departments where location_id in ( location_id from locations where country_id = ( country_id from countries where country_name='United Kingdom')));

select last_name from employees where department_id in ( select department_id from departments where department_name like 'IT%')

and salary >(select avg(salary) from employees); select p.last_name,p.department_id from employees p where p.salary < (select avg(s.salary) from employees s where s.department_id= p.department_id); select last_name from employees where salary > ( select salary from employees where last_name = 'Tobias') order by last_name; select last_name from employees where salary > ( select salary from employees where last_name = 'Taylor') order by last_name;--Generates an error -- following two queries are the solution for above problem select last_name from employees where salary > all( select salary from employees where last_name = 'Taylor') order by last_name; select last_name from employees where salary > ( select max(salary) from employees where last_name = 'Taylor') order BY last_name; select select select select last_name from employees where manager_id in ( employee_id from employees where department_id in ( department_id from departments where location_id in( location_id from locations where country_id = 'UK')));

select job_title from jobs natural join employees group by job_title having avg(salary)=(select max(avg(salary))from employees group by job_id); select last_name from employees where salary>all( select salary from employees where department_id=80); --OR-select last_name from employees where salary>( select max(salary) from employees where department_id=80); select last_name from employees where department_id = ( select department_id from departments where department_name='&Department_Name'); -- Better than above -select last_name from employees where department_id = ( select department_id from departments where upper(department_name) like upper('% &department_name%')); -- Better than above --select last_name from employees WHERE department_id in ( select department_id from departments where upper(department_name) like upper('% &department_name%')); -- Alternative -select last_name,department_name from employees join departments on employees.de partment_id=departments.department_id where departments.department_id in ( select department_id from departments where upper(department_name) like upper('% &department_name%')); select employee_id from employees where salary < all (select salary from employe es where

department_id=10); select employee_id from employees where salary < (select min(salary) from employ ees where department_id=10); select employee_id from employees where salary not >= any (select salary from em ployees where department_id=10);-- generates an error select employee_id from employees e join departments d on e.department_id= d.department_id where e.salary < (select min(salary) from employees) and d.department_id=10; -- no result select last_name from employees where salary > any (select salary from employees where last_name='Taylor') order by last_name; select last_name from employees where salary not < (select min(salary) from employees where last_name='Taylor') order by last_name;-- generates error select region_name from regions; select region_name from regions union select region_name from regions; select region_name from regions union all select region_name from regions; select region_name from regions union all select region_name from regions order by region_name; select region_name from regions intersect select region_name from regions; select region_name from regions minus select region_name from regions; create table old_dept(deptno number,dname char(20),dated date); create table new_dept(dept_id number(38),dname varchar2(14), started timestamp(6 )); insert insert insert insert into into into into old_dept old_dept new_dept new_dept values(10,'Accounts',sysdate); values(20,'Support',sysdate); values(10,'Accounts',sysdate); values(30,'Admin',sysdate);

SELECT * from old_dept union all select * from new_dept; select * from old_dept union select * from new_dept; select deptno,trim(dname),trunc(dated) from old_dept union select dept_id,trim(dname),trunc(started) FROM new_dept; select * from old_dept intersect select * from new_dept; select deptno,trim(dname),trunc(dated) from old_dept intersect select dept_id,trim(dname),trunc(started) from new_dept; select * FROM old_dept minus select * from new_dept; select dept_id,trim(dname),trunc(started) FROM new_dept minus select deptno,trim(dname),trunc(dated) from old_dept;

select department_id,count(1) from employees where department_id in (20,30,40) group by department_id; select * from regions; insert into regions values(101,'Great Britain'); insert into regions values(&regionid,'&regnName'); insert into regions values((select max(region_id)+1 from regions),'Oceania'); select * from regions; commit; create table emp_no_name (department_id NUMBER(4), job_id VARCHAR2(10), salary number(8,2), commission_pct number(2,2), hire_date date); create table emp_non_sales ( employee_id number(6), department_id number(4), salary number(8,2), hire_date date); create table emp_sales ( employee_id number(6), salary number(8,2), commission_pct number(2,2), hire_date date); insert all when 1=1 then into emp_no_name (department_id,job_id,salary,commission_pct,hire_date) values (department_id,job_id,salary,commission_pct,hire_date) when department_id <> 80 then into emp_non_sales (employee_id,department_id,salary,hire_date) values (employee_id,department_id,salary,hire_date) when department_id = 80 then into emp_sales (employee_id,salary,commission_pct,hire_date) values (employee_id,salary,commission_pct,hire_date) select employee_id,department_id,job_id,salary,commission_pct,hire_date from employees; select * from emp_no_name; select * from emp_non_sales; select * from emp_sales; update employees set salary=10000 where employee_id=206; update employees set salary=salary*1.1 where last_name='Cambrault'; update employees set salary=salary*1.1 where department_id in ( select department_id from departments where department_name like '%&Which_Dept%' );

update employees set department_id=80,commission_pct=( select min(commission_pct) from employees where department_id=80) where employee_id=206; update regions set region_name='Scandinavia' where region_id =101; update regions set region_name='Iberia' where region_id>100; update regions set region_id=region_id+(select max(region_id) from regions) where region_id in (select region_id from regions where region_id>100); select * from regions; commit; delete from regions where region_id=204; DELETE FROM regions; delete from regions where region_id in ( select region_id from regions where region_name='Iberia'); select * from regions; commit; select * from regions; insert into regions values(100,'UK'); select * from regions; commit; select * from regions; rollback; select * from regions; delete from regions where region_id=100; select * from regions; commit; select * from regions; create table tab(col varchar(10)); INSERT INTO TAB VALUES('one'); savepoint first; INSERT INTO TAB VALUES('two'); savepoint second; INSERT INTO TAB VALUES('three'); rollback to savepoint second; rollback to savepoint first; commit; delete from tab; rollback;

select * from tab; select object_type from user_object group by object_type; select object_type from all_objects group by object_type; create table "tab"(col varchar2(4)); create table "with space"("-hyphen" date); insert into "with space" values(sysdate); select "-hyphen" from "with space"; select distinct owner from all_objects; select object_type,count(*) from user_objects group by object_type; select table_name,cluster_name,iot_type from user_tables; describe regions; select column_name,data_type,nullable from user_tab_columns where table_name='REGIONS'; create table typcst(d_col date,n_col number(4,2),c_col varchar2(20)); insert into typcst values(to_date('30-08-99'),to_number('20.209'),'Inserted corr ectly'); insert into typcst values('23-08-98',44.3,'Casted automatically'); select * from typcst; desc employees; desc departments; select column_name, data_type, nullable, data_length, data_precision, data_scale from user_tab_columns where table_name='EMPLOYEES'; create table emp_dept as select first_name||' '||last_name ename, department_name dNAME,round(sysdate-hire_date+1) services from employees natural join departments order by dname,ename; select * from emp_dept where rownum < 4; alter table emp_dept drop unused columns; alter table emp_dept rename column services to no_of_days; select * from emp_dept where rownum < 6; alter table emp_dept read write; insert into emps select employee_id,last_name,salary,department_id from employee s; commit; create table dept(

deptno number(2,0) constraint dept_deptno_pk primary key constraint dept_deptno_ck check (deptno between 10 and 90), dname varchar2(20) constraint dept_dname_nn not null); create table emp_cpy( empno number(4,0) constraint emp_c_empno_pk primary key, ename varchar2(20) constraint emp_c_ename_nn not null, mgr number (4,0) constraint emp_c_mgr_fk references emp_cpy (empno), dob date, hiredate date, deptno number(2,0) constraint emp_c_deptno_fk references dept(deptno) on delete set null, email varchar2(30) constraint emp_c_email_uk unique, constraint emp_c_hiredate_chk check (hiredate >= dob + 365*16), constraint emp_c_email_chk check ((instr(email,'@') > 0) and (instr(email,'.') > 0))); create table emp as select employee_id empno, last_name ename, department_id deptno from employees; create table dept as select department_id deptno, department_name dname from departments; alter table emp add constraint emp_pk primary key (empno); alter table dept add constraint dept_pk primary key (deptno); alter table emp add constraint dept_fk foreign key (deptno) references dept on delete set null; insert into dept values(10,'New Department');//generates err insert into emp values(9999,'New emp',99);//generates err truncate table dept;//generates err drop table emp; drop table dept; create view hr.emp_fin as select hire_date,job_id,salary, commission_pct,department_id from employees; select * from emp_fin; select department_name,sum(salary) from departments natural join emp_fin GROUP by department_name; create view dept_dal as select department_name,sum(salary) total_sal from departments left outer join employees using(department_id) group by department_name order by department_name; select * from dept_dal order by total_sal; create or replace view emp_dept as select /*+USE_HASH (EMPLOYEES DEPARTMENTS)*/ department_name,last_name from departments natural join employees; select * from emp_dept; create or replace view loc1800 as select department_id,department_name,location_id from departments where location_id=1800 with check option; insert into loc1800 values(99,'QA',2100);

create force view ex_staff as select employee_id,last_name,left_date from employees where left_date is not null; --generates warning select * from user_objects where object_type='VIEW'; select * from ex_staff; -- generates error alter table employees add left_date date; select * from ex_staff; alter view ex_staff compile; drop view ex_staff; alter table employees drop column left_date; desc employees; create view emp_anon_v as select hire_date,job_id,salary,commission_pct,department_id from employees; create view dept_anon_v as select department_id,department_name,location_id from departments; create view dept_sum_v as select e.department_id,count(1) staff,sum(e.salary) salaries, d.department_name from emp_anon_v e join dept_anon_v d on e.department_id=d.department_id group by e.department_id,d.department_name; select * from dept_sum_v; insert into dept_anon_v values(99,'TMP_DEPT',1800); select * from dept_anon_v; select * from departments where department_id=99; insert into emp_anon_v values(sysdate,'AC_MGR',10000,0,99);--generates an error update emp_anon_v set salary=salary*1.1; select * from employees; rollback; select max(avg_sal) from (select avg(salary) avg_sal from employees group by dep artment_id); select max(salaries/staff) from dept_sum_v; select * from hr.employees; create synonym emp_p_syn for employees; select * from emp_p_syn;

drop synonym emp_p_syn; create synonym emp_s for emp_anon_v; create synonym dept_s for dept_anon_v; create synonym deptsum_s for dept_sum_v; desc emp_anon_v; desc emp_s; select * from deptsum_s; insert into dept_s values(99,'Temp Dept',1800); insert into emp_s values(sysdate,'AC_MGR',10000,0,99); update emp_s set salary=salary*1.1; select * from emp_s; rollback; select max(salaries/staff) from deptsum_s; drop view emp_anon_v; select * from dept_sum_v; alter view dept_sum_v compile; drop view dept_anon_v; drop view dept_sum_v; select * from emp_s; alter synonym emp_s compile; drop synonym emp_s; drop synonym dept_s; drop synonym deptsum_s; create table orders(order_number number(4) primary key,order_date date,customer_ no number(4)); create table order_lines(order_number number(4) references orders,line_number nu mber(4), item_number number(4),quantity number(4,1)); create sequence order_seq start with 10; create sequence line_seq start with 10; INSERT INTO orders VALUES(order_seq.nextval,sysdate,1000 ); insert into order_lines values(order_seq.currval,line_seq.nextval,111,1); insert into order_lines values(order_seq.currval,line_seq.nextval,222,2); commit; select * from orders; select * from order_lines; insert into orders values(order_seq.nextval,sysdate,1002); rollback; insert into orders values(order_seq.nextval,sysdate,1003); select * from orders; alter sequence order_seq cache 100;

drop drop drop drop

sequence order_seq; sequence line_seq; table order_lines; table orders;

create sequence seq1 start with 10 nocache maxvalue 15 cycle; select seq1.nextval from dual; select seq1.nextval from dual; create table seqtest(c1 number,c2 varchar2(10)); alter table seqtest add constraint seqtest_pk primary key(c1); CREATE sequence seqtest_pk_s; -- execute in session A insert into seqtest values(seqtest_pk_s.nextval,'First'); commit; --execute in session B insert into seqtest values(seqtest_pk_s.nextval,'Second'); -- execute in session A insert into seqtest values(seqtest_pk_s.nextval,'Third'); commit; select * from seqtest; --execute in session B select * from seqtest; rollback; select * from seqtest; drop sequence seqtest_pk_s; drop sequence seq1; drop table seqtest; select * from regions; select rowid,region_id,region_name from regions; select * from regions where rowid='AAAR5YAAFAAAACPAAC'; select count(*) from employees where last_name between 'A%' and 'Z%'; create table dept(deptno number,dname varchar2(20)); create table emp(empno number,surname varchar2(10),forename varchar2(10), dob date,deptno number); create unique index dept_i1 on dept(deptno); create unique index emp_i1 on emp(empno); create index empi2 on emp(surname,forename); create bitmap index emp_i3 on emp(deptno); alter table dept add constraint dept_pk primary key(deptno); alter table emp add constraint emp_pk primary key(empno); alter table emp add constraint emp_fk foreign key(deptno) references dept; drop index empi2; create index empi2 on emp(surname,forename,dob); drop index emp_i3; drop index empi2; drop index emp_i1;--generates error alter table emp drop constraint emp_pk; drop index emp_i1; drop table emp; drop table dept; drop index dept_i1;--generates error

create table emps as select * from employees; desc emps; create unique index emp_empid_i on emps(employee_id); insert into emps(employee_id,last_name,email,hire_date,job_id) values(198,'Watson','jw@bplc.co.za',sysdate,'IT_PROG');--generates error create index emps_name_i on emps(first_name,last_name); create index emps_tel_i on emps(phone_number); create bitmap index emps_mgr_i on emps(manager_id); create bitmap index emps_dept_i on emps(department_id); alter table emps add constraint emps_empid_pk primary key(employee_id); alter table emps add constraint emps_email_uk unique(email); alter table emps add constraint emps_phone_uk unique(phone_number); select index_name,index_type,uniqueness from user_indexes where table_name='EMPS'; drop table emps; select index_name from user_indexes where table_name='EMPS';

You might also like