Vous êtes sur la page 1sur 8

Capabilities of SQL statement: Selection-->Ability to fetch rows and records Projection-->Ability to fetch columns Join-->Ability to fetch related records

from multiple tables JOIN --A join is a query that combines rows from two or more tables. --The join condition compares two columns each from a different table. --If there are more than two tables that are to be joined then it is the optimiz er that decides the order in which the tables will be joined based on join condi tions, indexes and availiable statistics. SQL> conn hr/hr Connected. SQL> desc employees Name ----------------------------------------EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID SQL> desc departments Name ----------------------------------------DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID

Null? Type -------- ---------------------------NOT NULL NUMBER(6) VARCHAR2(20) NOT NULL VARCHAR2(25) NOT NULL VARCHAR2(25) VARCHAR2(20) NOT NULL DATE NOT NULL VARCHAR2(10) NUMBER(8,2) NUMBER(2,2) NUMBER(6) NUMBER(4) Null? -------NOT NULL NOT NULL Type ---------------------------NUMBER(4) VARCHAR2(30) NUMBER(6) NUMBER(4)

--Joining two tables using Oracle Proprietory join SQL> select employee_id, first_name from employees,departments where employees.d epartment_id=departments.department_id; EMPLOYEE_ID ----------198 199 200 201 202 203 204 205 206 100 101 FIRST_NAME -------------------Donald Douglas Jennifer Michael Pat Susan Hermann Shelley William Steven Neena

--SQL syntax **natural join:It automatically uses common columns present in the tables that a re joined. also known as equijoin. Ignores the null values.

SQL> desc departments Name ----------------------------------------DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID SQL> desc locations Name ----------------------------------------LOCATION_ID STREET_ADDRESS POSTAL_CODE CITY STATE_PROVINCE COUNTRY_ID

Null? -------NOT NULL NOT NULL

Type ---------------------------NUMBER(4) VARCHAR2(30) NUMBER(6) NUMBER(4)

Null? Type -------- ---------------------------NOT NULL NUMBER(4) VARCHAR2(40) VARCHAR2(12) NOT NULL VARCHAR2(30) VARCHAR2(25) CHAR(2)

SQL> select department_name, city from departments natural join locations; DEPARTMENT_NAME -----------------------------IT Shipping Administration Purchasing Executive Finance Accounting Treasury Corporate Tax Control And Credit Shareholder Services DEPARTMENT_NAME -----------------------------Benefits Manufacturing Construction Contracting Operations IT Support NOC IT Helpdesk Government Sales Retail Sales Recruiting DEPARTMENT_NAME -----------------------------Payroll Marketing Human Resources Sales Public Relations 27 rows selected. --Any SQL statement has the word "JOIN" that is a SQL code. CITY -----------------------------Southlake South San Francisco Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle CITY -----------------------------Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle Seattle CITY -----------------------------Seattle Toronto London Oxford Munich

SQL> select employee_id, last_name from employees natural join departments; EMPLOYEE_ID ----------202 206 101 102 104 105 106 107 109 110 111 EMPLOYEE_ID ----------112 113 115 116 117 118 119 129 130 131 132 EMPLOYEE_ID ----------150 151 152 153 154 155 184 185 186 187 LAST_NAME ------------------------Fay Gietz Kochhar De Haan Ernst Austin Pataballa Lorentz Faviet Chen Sciarra LAST_NAME ------------------------Urman Popp Khoo Baida Tobias Himuro Colmenares Bissot KAPOOR Marlow Olson LAST_NAME ------------------------Tucker Bernstein Hall Olsen Cambrault Tuvault Sarchand Bull Dellinger Cabrio

32 rows selected. --Internally this fired .. SQL>select employee_id, last_name from employees,departments where employees.department_id=departments.department_id and employees.manager_id=departments.manager_id **Using clause --If we have two tables which have more than one column in common I can specify the column that should be used as the join condition explicitly with the help of using clause.

SQL> select last_name,salary from employees join departments using(manager_id); LAST_NAME SALARY ------------------------- ---------Hartstein 13000 Fay 6000 Gietz 8300 Kochhar 17000 De Haan 17000 Ernst 6000 Austin 4800 Pataballa 4800 Lorentz 4200 Faviet 9000 Chen 8200 LAST_NAME SALARY ------------------------- ---------Sciarra 7700 Urman 7800 Popp 6900 Raphaely 11000 Khoo 3100 Baida 2900 Tobias 2800 Himuro 2600 Colmenares 2500 Weiss 8000 Fripp 8200 LAST_NAME SALARY ------------------------- ---------Kaufling 7900 Vollman 6500 Mourgos 5800 Bissot 3300 KAPOOR 2800 Marlow 2500 Olson 2100 Russell 14000 Partners 13500 Errazuriz 12000 Cambrault 11000 LAST_NAME SALARY ------------------------- ---------Zlotkey 10500 Tucker 10000 Bernstein 9500 Hall 9000 Olsen 8000 Cambrault 7500 Tuvault 7000 Sarchand 4200 Bull 4100 Dellinger 3400 Cabrio 3000 44 rows selected.

**ON CLAUSE --For tables wherein the column data is the same but the column names are differ ent. SQL> 2 3 4 5 6 create table emps ( eid,fname,did) as select employee_id,first_name,department_id from employees;

Table created. SQL> 2 3 4 5 6 7 create table depts ( dept_id,dp_name) as select department_id,department_name from departments;

Table created. SQL> select eid,fname,dp_name from emps join depts on(emps.did=depts.dept_id); EID ---------198 199 200 201 202 203 204 205 206 100 101 FNAME -------------------Donald Douglas Jennifer Michael Pat Susan Hermann Shelley William Steven Neena DP_NAME -----------------------------Shipping Shipping Administration Marketing Marketing Human Resources Public Relations Accounting Accounting Executive Executive

**Cross-Join --first method SQL>select last_name, department_name from employees cross join departments; --second method SQL>select employee_id,department_name from employees,departments; **Self-Join --Suppose if you want to find out the manager for any employee then you will hav e to query the employees table itself since the manager is also an employee. --In this case we use a self join. SQL> select e.employee_id as emp,m.first_name as manager from employees e,employ ees m where e.manager_id=m.employee_id; EMP ---------198 199 MANAGER -------------------Kevin Kevin

200 201 202 203 204 205 206 101 102

Neena Steven Michael Neena Neena Neena Shelley Steven Steven

SQL> select first_name as emp,first_name as man from employees e join employees m on(e.manager_id=m.employee_id); select first_name as emp,first_name as man from employees e join employees m on( e.manager_id=m.employee_id) * ERROR at line 1: ORA-00918: column ambiguously defined #Correction SQL> select e.first_name as emp,m.first_name as man from employees e join employ ees m on(e.manager_id=m.employee_id); EMP -------------------Donald Douglas Jennifer Michael Pat Susan Hermann Shelley William Neena Lex **Non Equi Join SQL> create table job_grades 2 ( 3 grade char, 4 minsal number, 5 maxsal number 6 ); Table created. SQL> insert into job_grades values('&grade',&minsal,&maxsal); Enter value for grade: A Enter value for minsal: 0 Enter value for maxsal: 2000 old 1: insert into job_grades values('&grade',&minsal,&maxsal) new 1: insert into job_grades values('A',0,2000) 1 row created. SQL> / Enter value for grade: B Enter value for minsal: 2001 Enter value for maxsal: 4000 MAN -------------------Kevin Kevin Neena Steven Michael Neena Neena Neena Shelley Steven Steven

old new

1: insert into job_grades values('&grade',&minsal,&maxsal) 1: insert into job_grades values('B',2001,4000)

1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> select first_name,salary,grade from employees,job_grades where employees.sa lary between job_grades.minsal and job_grades.maxsal; FIRST_NAME SALARY G -------------------- ---------- TJ 2100 B Hazel 2200 B Steven 2200 B James 2400 B Ki 2400 B Randall 2500 B Karen 2500 B Joshua 2500 B Peter 2500 B Martha 2500 B James 2500 B FIRST_NAME SALARY G -------------------- ---------- Donald 2600 B Douglas 2600 B Guy 2600 B Randall 2600 B Irene 2700 B John 2700 B Sigal 2800 B Girard 2800 B grade: E minsal: 8001 maxsal: 10000 into job_grades values('&grade',&minsal,&maxsal) into job_grades values('E',8001,10000) grade: D minsal: 6001 maxsal: 8000 into job_grades values('&grade',&minsal,&maxsal) into job_grades values('D',6001,8000) grade: C minsal: 4001 maxsal: 6000 into job_grades values('&grade',&minsal,&maxsal) into job_grades values('C',4001,6000)

Mozhe Vance Shelli

2800 B 2800 B 2900 B

FIRST_NAME SALARY G -------------------- ---------- Michael 2900 B Timothy 2900 B Kevin 3000 B Anthony 3000 B Jean 3100 B Alana 3100 B Alexander 3100 B Curtis 3100 B Samuel 3200 B Julia 3200 B Stephen 3200 B **Outer join --Left outer join --right outer join --full outer join #Left outer join SQL>select first_name,department_name from employees left outer join departments using(department_id); --Fetches all the rows of the table to the left of the join clause over here emp loyees. #Right outer join SQL>select first_name,department_name from employees right outer join department s using(department_id); --Fetches all the rows of the table to the right of the join clause over here em ployees #Full outer join select first_name,department_name from employees full outer join departments usi ng(department_id); **Joining more than two tables SQL>select first_name,department_name,city from employees e, departments d, locations l where e.department_id=d.department_ id and d.location_id=l.location_id; ---Display departments who do not have any employees --select employee_id, department_name from departments left outer join employees using(department_id) where first_name is null

Vous aimerez peut-être aussi