sql topics
TRANSCRIPT
-
7/30/2019 SQL Topics
1/9
SQL
TOPICSByNaresh Kumar B. N
-
7/30/2019 SQL Topics
2/9
Page 2 of 9
INDEX1. JOINS ............................................................................................................................. 3
1.1 Types of Joins ............................................................................................................... 3
1.1.1 Equi join ................................................................................................................. 3
1.1.2 Cross Joins .............................................................................................................. 4
1.1.3 Self Joins ................................................................................................................ 4
1.1.4 Inner Joins .............................................................................................................. 4
1.1.5 Outer Joins ............................................................................................................. 5
2. MERGE ........................................................................................................................... 7
2.1 Sub query ..................................................................................................................... 73. UNION ........................................................................................................................... 8
4. PROCEDURES ................................................................................................................. 9
-
7/30/2019 SQL Topics
3/9
Page 3 of 9
1. JOINS
A join is a query that combines rows from two or more tables or views. Most join queries
contain at least one join condition. Joins are needed when the required data is stored inmore than one table. The tables that I am using are as follows,
DEPARTMENT
Department_ID Name Location_ID
10 Prime sourcing 122
20 Flex cube 124
30 OFSAA 123
40 NGP 167
1.1 Types of Joins1.1.1 Equi join
An equi join is a join with a join condition containing an equality operator. An equijoin
combines rows that have equivalent values for the specified columns.
Example: To Display employees with their designations
Select e.employee_id, e.employee_name, j.designationFrom employee e, job j
Where e.job_id=j.job_id;
EMPLOYEE_ID EMPLOYEE_NAME DESIGNATION
37252 NARESH Asst.Consultant
37251 CHAITANYA Asst.Consultant
37254 RAVI Staff consultant
37255 KASHYAP Sr.Consultant
LOCATION
Location_ID Location
122 bangalore
123 Chennai
124 Mumbai
167 Delhi
JOB
Job_ID Designation
667 Asst.Consultant
668 Sr.Consultant
669 Staff consultant
EMPLOYEE
EMPLOYEE
_ID
EMPLOYEE_
NAME
JOB_
ID
MANAGE
R_ID
DATE_OF_
JOIN
SALAR
Y
DEPARTMENT_
ID37252 NARESH 667 7902 13-08-2012 25000 10
37251 CHAITANYA 667 7698 20-02-2010 25000 30
37254 RAVI 669 7839 04-04-2009 30000 10
37255 KASHYAP 668 7839 15-06-2012 35000 40
-
7/30/2019 SQL Topics
4/9
Page 4 of 9
1.1.2 Cross Joins
If you dont specify the join condition it will automatically results in cross join or Cartesian
product of the participating tables. This join combines each row of one table with each
row of the other.
Example:
Select d.name, l. location
From location l, d.department;
Name Location
Prime sourcing BANGALORE
Prime sourcing CHENNAI
Prime sourcing MUMBAI
Prime sourcing DELHI
Flex cube BANGALORE
Flex cube CHENNAIFlex cube MUMBAI
Flex cube DELHI
OFSAA BANGALORE
OFSAA CHENNAI
OFSAA MUMBAI
OFSAA DELHI
NGP BANGALORE
NGP CHENNAI
NGP MUMBAI
NGP DELHI
1.1.3 Self Joins
A self join is a join of a table to itself. You can notice the same table name used twice in
the from clause with aliases.
Example: To Show the no. of employees working under every manager.
Select m.manager_id, count (*) from employee e,
employee m where e.employee_id=m.manager_id group bym.manager_id
1.1.4 Inner Joins
An inner join (sometimes called a simple join) is a join of two or more tables that returns
only those rows that satisfy the join condition.
Example: TO Display the employees with their department name and location.
Select employee_id, employee_name,name
from employee e, department dwhere e.department_id=d.department_id;
OR
Select employee_id, employee_name, name
From employee e inner join department dOn e.department_id=d.department_id;
-
7/30/2019 SQL Topics
5/9
Page 5 of 9
Employee_id employee_name name
37254 RAVI Prime sourcing
37252 NARESH Flex cube
37251 CHAITANYA OFSAA
37255 KASHYAP NGP
1.1.5 Outer Joins
An outer join returns all rows that satisfy the join condition and also returns some or all of
those rows from one table for which no rows from the other satisfy the join condition.
There are different types of outer joins.
They are
1.1.5.1 Left Outer Join
This join returns all the rows which satisfy the join condition and also the rows from left
table of the condition which does not satisfy the condition.Example: Display employee details with all departments.
Select last_name, d.department_id, d.nameFrom employee e, department d
Where e.department_id (+) = d.department_id;
Or
Select last_name, d.department_id, d.namefrom employee left outer join department d on
e.department_id = d.department_id;
Last_name Department_id Name
RAVI 10 Prime sourcing
NARESH 20 Flex cube
CHAITANYA 30 OFSAA
KASHYAP 40 NGP
1.1.5.2 Right outer join
This join returns all the rows which satisfy the join condition and also the rows from right
table of the condition which does not satisfy the condition.
Example:
Select e.employee_name, d.department_id, d.namefrom employee e right outer join department d one.department_id=d.department_id and d.name in('Prime sourcing', 'NGP');
Last_name Department_id Name
RAVI 10 Prime sourcing
20 Flex cube
30 OFSAA
KASHYAP 40 NGP
-
7/30/2019 SQL Topics
6/9
Page 6 of 9
1.1.5.3 Full outer join
This join returns all the rows which satisfy the join condition and also the rows from both
the tables of the condition which does not satisfy the condition.
Example:
Select e.employee_name, d.department_id, d.nameFrom employee e full outer join department d one.department_id=d.department_id and d.name in ('Primesourcing', 'NGP');
Last_name Department_id Name
RAVI 10 Prime sourcing
20 Flex cube
30 OFSAA
KASHYAP 40 NGP
CHAITANYANARESH
-
7/30/2019 SQL Topics
7/9
Page 7 of 9
2. MERGE
We will use the MERGE statement to select rows from one or more sources for update or
insertion into a table or view. You can specify conditions to determine whether to update
or insert into the target table or view.Consider the following tables for example
Now, if I want to add 20 marks for each student in STUDENT2 when id matches otherwiseinsert the non existing records into STUDENT2
Merge into STUDENT2 s2 USING (select id, nameFrom STUDENT) s1 ON (s2.id=s1.id)
When matched thenupdate set s2.marks=s2.marks+20When not matched then
insert (id, MARKS) values (s1.id, 15);
ID MARKS
37252 70
37253 50
37251 15
37250 15
2.1 Sub query
A subquery is a SELECT statement which is used in another SELECT statement. Subqueries
are very useful when you need to select rows from a table with a condition that depends
on the data of the table itself. You can use the subquery in the SQL clauses including
WHERE clause, HAVING clause, FROM clause etc.
Example: To Find out no.of employees working in prime sourcing department.
SELECT * FROM EMPLOYEEWHEREDEPARTMENT_ID= (SELECT DEPARTMENT_IDFROM DEPARTMENTWHERE NAME='Prime sourcing);
EMPLOYE
E_ID
EMPLOYE
E_NAMEJOB_ID
MANAGER
_ID
DATE_OF
_JOINSALARY
DEPARTM
ENT_ID
NARESH 37252 667 7902 13-AUG-12 25000 10RAVI 37254 669 7839 04-APR-09 30000 10
Student1
ID NAME37252 Naresh
37251 Chaitanya
37250 Kashyap
37253 Ravi
Student 2
ID MARKS
37252 50
37253 30
-
7/30/2019 SQL Topics
8/9
Page 8 of 9
3. UNION
UNION takes as arguments two nested tables and returns a nested table whose values
are those of the two input nested tables. The two input nested tables must be of the same
type, and the returned nested table is of the same type as well.
The ALL keyword instructs Oracle to return all elements that are in the two inputs
nested tables, including duplicate values and duplicate NULL occurrences. This is the
default.
The DISTINCT keyword instructs Oracle to eliminate duplicates from the returned
nested table, including duplicates ofNULL, if they exist.
Example:
List out the ALL designations in prime sourcing and Ngp
SELECT DESIGNATIONFROM JOBWhere job_id in(SELECT JOB_IDFROM EMPLOYEEWHERE DEPARTMENT_ID= (SELECT DEPARTMENT_IDFROM DEPARTMENT WHERE NAME=NGP))
UNIONSELECT DESIGNATIONFROM JOBWHERE JOB_ID IN
(SELECT JOB_IDFROM EMPLOYEEWHERE DEPARTMENT_ID= (SELECT DEPARTMENT_IDFROM DEPARTMENTwhere name='OFSAA'));
Output:
DESIGNATION
Asst.Consultant
Sr.Consultant
-
7/30/2019 SQL Topics
9/9
Page 9 of 9
4. PROCEDURES
A procedure is a named PL/SQL block which performs one or more specific task.
Procedure has a header and a body. The header consists of the name of the procedure
and the parameters or variables passed to the procedure. The body consists ordeclaration section, execution section and exception section similar to a general PL/SQL
Block.
Example: Procedure for giving the details of employee where employee id given as an
input parameter.
Pl/SQL
Create or replaceProcedure myproc(EID IN NUMBER) asENAME varchar2 (20);
DOJ DATE;BeginSELECT EMPLOYEE_NAME, DATE_OF_JOIN INTO ENAME, DOJ FROMNBN_EMPLOYE WHERE EMPLOYEE_ID=EID;DBMS_OUTPUT.PUT_LINE ('EMPLOYEE_NAME:'||ENAME);DBMS_OUTPUT.PUT_LINE ('DATE_OF_JOIN:'||DOJ);End;
SQL
EXECUTE myproc(37252);
Output:
EMPLOYEE_NAME: NARESHDATE_OF_JOIN:13-AUG-12