Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Set operators are used to combine the information of simillar data types from one or more than one table. The different types of set operators are
UNION
UNION ALL INTERSECT MINUS Set Operators can combine two or more queries into one result. The result of each SELECT statement can be treated as a set and SQL set operations can be applied on those sets to arrive at a final result. SELECT statement containing Set Operators are referred to as Compound queries and each SELECT statement in the compound query is referred to as a component query.
Here we are using two tables one is employees table and another one is job_history table in hr schema
Employees table contains currently working Employees information. . Job_history contains currently and previous Employees information.
UNION
Combines the Result of Two SELECT statements Into one result set, And then eliminates Any Duplicates Rows From That Result set. Guidelines
The number of columns and the data types of the columns being selected must be identical in all the SELECT statements used in the query. The names of the columns need not be identical.
1. Display the current and previous job details of all employees. Display each combination only once.
UNION
UNION ALL
Combines the Result of Two SELECT statements Into one result set Including the duplicates Guidelines The guidelines for UNION and UNION ALL are the same, with the following two exceptions that pertain to UNION ALL: Unlike UNION, duplicate rows are not eliminated and the output is not sorted by default. The DISTINCT keyword cannot be used
SELECT employee_id, job_id, department_id FROM employees UNION ALL SELECT employee_id, job_id, department_id FROM job_history ORDER BY employee_id;
INTERSECT
Returns Only Those Rows That are returned by each of two SELECT statements.
Guidelines
The number of columns and the data types of the columns being selected by the
SELECT statements in the queries must be identical in all the SELECT statements used in the query. The names of the columns need not be identical.
Reversing the order of the intersected tables does not alter the result. INTERSECT does not ignore NULL values.
3). Display the employee IDs and job IDs of those employees who currently have a job title that is the same as a previous job title.
intersect
SELECT employee_id, job_id FROM job_history;
Here we are using two tables one is employees table and another one is job_history table in hr schema. (Employee table) -----------------------select employee_id,job_id from employees where employee_id in(176,200,101); EMPLOYEE_ID JOB_ID ----------- ---------200 AD_ASST 101 AD_VP 176 SA_REP
----------101 200 101 200 102 201 176 176 114 122
---------AC_ACCOUNT AC_ACCOUNT AC_MGR AD_ASST T_PROG MK_REP SA_MAN SA_REP ST_CLERK ST_CLERK
Select employee_id,job_id from employees Where employee_id = 101; When we do intersect the above record will not display because he is having new positon not previous position. EMPLOYEE_ID JOB_ID ----------- ----------
101 AD_VP
Takes the result set of one select statement ans removes the those that are returned by a SELECT statement. NOTE: The number of columns and the data types of the columns being selected
by the SELECT statements in the queries must be identical in all the SELECT statements used in the query. The names of the columns need not be identical.
4) Display the employee IDs of those employees who have not changed their jobs even once.
MINUS
The above query gives the result all employee_id numbers except the results of the under the following the query. Here employees who are having 176,200(101,102,114) employee_id will not come because they changed positions .
EMPLOYEE_ID JOB_ID(compare the above query) ----------- ---------102 AD_VP 114 PU_MAN 122 ST_MAN
SET SERVEROUTPUT ON DECLARE row_updated NUMBER:=0; row_inserted NUMBER:=0; row_deleted NUMBER:=0; row_total NUMBER:=0; BEGIN INSERT into copy_emp(firstname, lastname, salary) VALUES ('Dinesh','kumar',20000); row_inserted:=SQL%ROWCOUNT; INSERT into copy_emp(firstname,lastname,salary) VALUES ('Chandu','shekar',10000); row_inserted:=SQL%ROWCOUNT+row_inserted; INSERT into copy_emp(firstname,lastname,salary) VALUES ('Dinesh','naidu',10000); row_inserted:=SQL%ROWCOUNT+row_inserted; DBMS_OUTPUT.PUT_LINE('The number of rows inserted is: '||row_inserted); BEGIN UPDATE copy_emp SET salary=salary+100 WHERE firstname LIKE '%a%'; row_updated:=SQL%ROWCOUNT; DBMS_OUTPUT.PUT_LINE('The number of rows updated is: '||row_updated); BEGIN DELETE from copy_emp WHERE firstname='Dinesh'; row_deleted:=SQL%ROWCOUNT; DBMS_OUTPUT.PUT_LINE('The number of rows deleted is: '||row_deleted); row_total:=row_inserted+row_updated+row_deleted; DBMS_OUTPUT.PUT_LINE('The total no of rows PERFORMED is: '||row_TOTAL); END; END; END; / SELECT * FROM copy_emp;
This is the one way to print the mathematical-table using PL/SQL statements.
set serveroutput on; DECLARE v_num number:=&enternum; v_result number; BEGIN FOR i IN 1..10 LOOP v_result:=i*v_num; DBMS_OUTPUT.PUT_LINE( v_num||'*'||i||'='||v_result); END LOOP; End;
SAMPLE PROGRAMS:
/* 1. Write a program to print the pascal triangle for a given input */ SET SERVEROUTPUT ON; SET VERIFY OFF; DECLARE input VARCHAR2(20):=UPPER('&input'); C VARCHAR(20); BEGIN FOR I IN 1..LENGTH(input) LOOP C:=SUBSTR(input,1,I); DBMS_OUTPUT.PUT_LINE(C); END LOOP; END; /
SET SERVEROUTPUT ON; DECLARE str VARCHAR2(100):='&str'; str1 VARCHAR2(100); n NUMBER(5); l VARCHAR2(20); BEGIN n:=LENGTH(str); FOR i IN 1..n LOOP L:=SUBSTR(str,i,1); str1:=L || str1; END LOOP; DBMS_OUTPUT.PUT_LINE(str1); END; /
SET SERVEROUTPUT ON DECLARE TYPE emp_rec_type IS RECORD (empid NUMBER, name VARCHAR2(20), jobid VARCHAR2(20), deptid NUMBER); emp_rec emp_rec_type; BEGIN SELECT employee_id,lastname,job_id,department_id INTO emp_rec FROM employees WHERE employee_id=100; DBMS_OUTPUT.PUT_LINE(emp_rec.jobid); DBMS_OUTPUT.PUT_LINE(emp_rec.name); DBMS_OUTPUT.PUT_LINE(emp_rec.deptid); END;