You are on page 1of 9

SQL TOPICS

By Naresh Kumar B. N

INDEX
1. 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 ..................................................................................................................... 7 3. UNION ........................................................................................................................... 8 4. PROCEDURES ................................................................................................................. 9

Page 2 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 in more than one table. The tables that I am using are as follows,
LOCATION Location_ID 122 123 124 167 Location bangalore Chennai Mumbai Delhi DEPARTMENT Department_ID 10 20 30 40 Name Prime sourcing Flex cube OFSAA NGP EMPLOYEE EMPL OYEE _ID 37252 37251 37254 37255 EMPLOYEE_ NAME NARESH CHAITANYA RAVI KASHYAP JOB_ ID 667 667 669 668 MANAGE R_ID 7902 7698 7839 7839 DATE_OF_ JOIN 13-08-2012 20-02-2010 04-04-2009 15-06-2012 SALAR Y 25000 25000 30000 35000 DEPART MENT_ ID 10 30 10 40 Location_ID 122 124 123 167 Job_ID 667 668 669 JOB Designation Asst.Consultant Sr.Consultant Staff consultant

1.1

Types of Joins

1.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.designation From employee e, job j Where e.job_id=j.job_id; EMPLOYEE_ID 37252 37251 37254 37255 EMPLOYEE_NAME NARESH CHAITANYA RAVI KASHYAP DESIGNATION Asst.Consultant Asst.Consultant Staff consultant Sr.Consultant

Page 3 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 Prime sourcing Prime sourcing Prime sourcing Prime sourcing Flex cube Flex cube Flex cube Flex cube OFSAA OFSAA OFSAA OFSAA NGP NGP NGP NGP 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 by m.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 d where e.department_id=d.department_id; OR Select employee_id, employee_name, name From employee e inner join department d On e.department_id=d.department_id; Location BANGALORE CHENNAI MUMBAI DELHI BANGALORE CHENNAI MUMBAI DELHI BANGALORE CHENNAI MUMBAI DELHI BANGALORE CHENNAI MUMBAI DELHI

Page 4 of 9

Employee_id 37254 37252 37251 37255 1.1.5 Outer Joins

employee_name RAVI NARESH CHAITANYA KASHYAP

name Prime sourcing Flex cube OFSAA NGP

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.name From employee e, department d Where e.department_id (+) = d.department_id; Or Select last_name, d.department_id, d.name from employee left outer join department d on e.department_id = d.department_id; Last_name RAVI NARESH CHAITANYA KASHYAP
1.1.5.2 Right outer join

Department_id 10 20 30 40

Name Prime sourcing Flex cube OFSAA NGP

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.name from employee e right outer join department d on e.department_id=d.department_id and d.name in ('Prime sourcing', 'NGP'); Last_name RAVI Department_id 10 20 30 40 Name Prime sourcing Flex cube OFSAA NGP

KASHYAP

Page 5 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.name From employee e full outer join department d on e.department_id=d.department_id and d.name in ('Prime sourcing', 'NGP'); Last_name RAVI Department_id 10 20 30 40 Name Prime sourcing Flex cube OFSAA NGP

KASHYAP CHAITANYA NARESH

Page 6 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

ID 37252 37251 37250 37253

Student1 NAME Naresh Chaitanya Kashyap Ravi

Student 2 ID 37252 37253 MARKS 50 30

Now, if I want to add 20 marks for each student in STUDENT2 when id matches otherwise insert the non existing records into STUDENT2 Merge into STUDENT2 s2 USING (select id, name From STUDENT) s1 ON (s2.id=s1.id) When matched then update set s2.marks=s2.marks+20 When not matched then insert (id, MARKS) values (s1.id, 15); ID 37252 37253 37251 37250 2.1 Sub query MARKS 70 50 15 15

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 EMPLOYEE WHERE DEPARTMENT_ID= (SELECT DEPARTMENT_ID FROM DEPARTMENT WHERE NAME='Prime sourcing);
DEPARTM ENT_ ID

EMPLOYE E_ID

EMPLOYE E_NAME

JOB_ID

MANAGER _ID

DATE_OF _JOIN

SALARY

NARESH RAVI

37252 37254

667 669

7902 7839

13-AUG-12 04-APR-09

25000 30000

10 10

Page 7 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 of NULL, if they exist. Example:
List out the ALL designations in prime sourcing and Ngp

SELECT DESIGNATION FROM JOB Where job_id in (SELECT JOB_ID FROM EMPLOYEE WHERE DEPARTMENT_ID= (SELECT DEPARTMENT_ID FROM DEPARTMENT WHERE NAME=NGP)) UNION SELECT DESIGNATION FROM JOB WHERE JOB_ID IN (SELECT JOB_ID FROM EMPLOYEE WHERE DEPARTMENT_ID= (SELECT DEPARTMENT_ID FROM DEPARTMENT where name='OFSAA')); Output: DESIGNATION Asst.Consultant Sr.Consultant

Page 8 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 or declaration 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 replace Procedure myproc(EID IN NUMBER) as ENAME varchar2 (20); DOJ DATE; Begin SELECT EMPLOYEE_NAME, DATE_OF_JOIN INTO ENAME, DOJ FROM NBN_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: NARESH DATE_OF_JOIN:13-AUG-12

Page 9 of 9

You might also like