Tuesday, 29 July 2014

JOINS in SQL SERVER 2008

JOINS :

These are used for retrieving the data from one or more tables at a time.

                   Joins can be used in any of following ways :
1             EQUI-JOINS
2             NON EQUI-JOINS
3             SELF JOINS
4             CROSS JOINS
5             OUTER JOINS


(I)             EQUI-JOINS : If two or more tables are combined using Equality condition we call it as a Equi-join
Ex :  Que :- write a query to get the matching records from emp and dept tables

             Select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,        
            e.deptno,d.deptno, d.dname,d.loc from emp e,dept d where 
            e.deptno=d.deptno

      In the above case it will retrieve the records from both the two tables only if the department numbers matches with the two tables, so it will not retrieve the information of deptno- 40 from dept table because a match is not available in emp tableNote : The above query will retrieve data from both the two tables but the syntax of joining the tables is not Ansi standards to write Join in Ansi standards we are given with 3 options
(1)Inner Join
(2)Cross Join
(3)Outer Join


Inner Join : using inner join we can implement Equi Join, non Equi join as well as self join also
   (a) Equi Join in Ansi Standard :

     Select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,d.deptno, d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno

Note : in Ansi standards to write a join statement we explicitly used join keyword between the tables which gives descriptiveness also to combine or join the tables with a condition we use ON keyword but not where




Loading Data from multiple tables using Equi-Join condition
          
 By using an equi-join we can combined the data of any no of tables provided all these tables contains a common column
3rd table DeptDetails structure

 






Note: my previous post you can find emp and dept table structures http://sqlanddotnetpost.blogspot.in/2014/07/employee-emp-database-for-sql-server.html

Que: Write a query to join 3 tables emp, dept and deptdetails
Using Non-Ansi Joins:
select e.ename,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,D.dname,d.loc,dd.did,dd.comments
from emp e,dept d,deptdetails dd where e.deptno=d.deptno and d.deptno=dd.deptno


Ansi-Standard Join :
select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,d.dname,d.loc,dd.did,dd.comments
from emp e inner join dept d  on e.deptno=d.deptno inner join deptdetails  dd on dd.deptno= d.deptno



(b)               Non-Equi Joins :

If we join tables with any condition other than equality condition we call it as a non-equi join

4th Table SalGrade


Que : Write a query to get data from emp and salgrade table by displaying the grade of the Employee basing on Salary
                Using Non-Ansi Join
select e.empno,e.ename,e.job,e.sal,s.grade,s.losal,s.hisal from emp e,salgrade s where e.sal between s.losal and s.hisal

Using Ansi Standard Join

select e.empno,e.ename,e.job,e.sal,s.grade,s.losal,s.hisal from emp e inner join salgrade s on e.sal between s.losal and s.hisal

Que : write to get the data using 3 tables dept,emp and sal grade

Select e.empno,e.ename,e.job,e.sal,d.deptno,d.dname,d.loc,s.grade, s.losal,s.hisal from emp e inner join dept d on e.deptno=d.deptno inner join salgrade s on e.sal between s.losal and s.hisal


(c’)   Self Jon : Joining a table to itself for getting the results is a self join

Que : write a query to get the details of employees who have sub-ordinates under then by using join

          Using Non-Ansi join

select distinct e.ename,e.ename,e.job,e.sal,e.deptno from emp e, emp m where e.empno=m.mgr

Using Ansi Standards

select distinct e.empno,e.ename,e.job,e.sal,e.deptno from emp e inner join emp m on e.deptno=m.mgr





II)  Cross Join

          If two or more tables are combined with each other without any condition we call it as a Cross join. Here each row of the first table goes and joins with each row of the second table if the 1st table has m rows and 2nd table has n rows the output will be max row

Using Non-Ansi :

Select * from emp e,dept d,deptdetails dd

Using Ansi Join :
         
          Select * from emp e cross join dept d cross join deptdetails dd



III) Outer Join

          It is an extension for the equi join i.e. in an equi join condition we will be getting the matching information from the tables used in the query. Whereas if a record(s) of a table doesn’t have a matching data in the other table that record(s) will not be retrieved.

          In case of an outer join just like our equi join will retrieve the matching data from all the tables as well as the unmatched data also. Where the unmatched data can be present in the left side of table or Right hand side table or both tables also

Outer Joins are of 3 types :
                  
1)  Left Outer Join
2)  Right Outer Join

Left outer join : Retrieves matching data from both tables as well as unmatched data from left hand side table.

Right outer Join : Retrieves the matching data from both the tables as well as unmatched data from Right side Table

Full Outer Join : Retrieves the unmatched data from both the tables + unmatched data from left side table + unmatched data from Right side table also.


Left Outer Join :
                  
Ex : select e.empno,e.ename,e.job,e.mgr,d.deptno,d.dname,d.loc from
emp e left outer join dept d on e.deptno=d.deptno
Right Outer Join :

select e.empno,e.ename,e.job,e.mgr,d.deptno,d.dname,d.loc from emp e Right outer join dept d on e.deptno=d.deptno


Full Outer Join :

select e.empno,e.ename,e.job,e.mgr,d.deptno,d.dname,d.loc from emp e full outer join dept d on e.deptno=d.deptno


Que : Write a query to get matching and unlatching data from the tables emp,dept and deptdetails and salgrade

Select e.empno,e.ename,e.job,d.deptno,d.dname,d.loc,dd.did,dd.comments from emp e right outer join dept d on e.deptno=d.deptno inner join deptdetails dd on d.deptno=dd.deptno

Note:

In the above query we are not retrieving the information from salgrade table if we want to get data from that table also with matched and unmatched data the query must be as following  


Query :

Select e.empno,e.ename,e.job,s.grade,d.deptno,d.dname,s.losal
dd.did,dd.comments
from salgrade s inner join emp e on e.sal between s.losal and s.hisal right outer join dept d
on e.deptno=d.deptno inner join deptdetails dd on d.deptno=dd.deptno



Employee (EMP) Database for SQL Server

Employee Database for SQL Server

EMP and DEPT table script  SQL Server

DROP TABLE EMP
DROP TABLE DEPT
DROP TABLE BONUS
DROP TABLE SALGRADE
DROP TABLE DUMMY
CREATE TABLE EMP
(EMPNO NUMERIC(4) NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR NUMERIC(4),
HIREDATE DATETIME,
SAL NUMERIC(7, 2),
COMM NUMERIC(7, 2),
DEPTNO NUMERIC(2))
INSERT INTO EMP VALUES
(7369, ‘SMITH’, ‘CLERK’, 7902, ’17-DEC-1980′, 800, NULL, 20)
INSERT INTO EMP VALUES
(7499, ‘ALLEN’, ‘SALESMAN’, 7698, ’20-FEB-1981′, 1600, 300, 30)
INSERT INTO EMP VALUES
(7521, ‘WARD’, ‘SALESMAN’, 7698, ’22-FEB-1981′, 1250, 500, 30)
INSERT INTO EMP VALUES
(7566, ‘JONES’, ‘MANAGER’, 7839, ‘2-APR-1981′, 2975, NULL, 20)
INSERT INTO EMP VALUES
(7654, ‘MARTIN’, ‘SALESMAN’, 7698, ’28-SEP-1981′, 1250, 1400, 30)
INSERT INTO EMP VALUES
(7698, ‘BLAKE’, ‘MANAGER’, 7839, ‘1-MAY-1981′, 2850, NULL, 30)
INSERT INTO EMP VALUES
(7782, ‘CLARK’, ‘MANAGER’, 7839, ‘9-JUN-1981′, 2450, NULL, 10)
INSERT INTO EMP VALUES
(7788, ‘SCOTT’, ‘ANALYST’, 7566, ’09-DEC-1982′, 3000, NULL, 20)
INSERT INTO EMP VALUES
(7839, ‘KING’, ‘PRESIDENT’, NULL, ’17-NOV-1981′, 5000, NULL, 10)
INSERT INTO EMP VALUES
(7844, ‘TURNER’, ‘SALESMAN’, 7698, ‘8-SEP-1981′, 1500, 0, 30)
INSERT INTO EMP VALUES
(7876, ‘ADAMS’, ‘CLERK’, 7788, ’12-JAN-1983′, 1100, NULL, 20)
INSERT INTO EMP VALUES
(7900, ‘JAMES’, ‘CLERK’, 7698, ‘3-DEC-1981′, 950, NULL, 30)
INSERT INTO EMP VALUES
(7902, ‘FORD’, ‘ANALYST’, 7566, ‘3-DEC-1981′, 3000, NULL, 20)
INSERT INTO EMP VALUES
(7934, ‘MILLER’, ‘CLERK’, 7782, ’23-JAN-1982′, 1300, NULL, 10)
CREATE TABLE DEPT
(DEPTNO NUMERIC(2),
DNAME VARCHAR(14),
LOC VARCHAR(13) )
INSERT INTO DEPT VALUES (10, ‘ACCOUNTING’, ‘NEW YORK’)
INSERT INTO DEPT VALUES (20, ‘RESEARCH’, ‘DALLAS’)
INSERT INTO DEPT VALUES (30, ‘SALES’, ‘CHICAGO’)
INSERT INTO DEPT VALUES (40, ‘OPERATIONS’, ‘BOSTON’)
CREATE TABLE BONUS
(ENAME VARCHAR(10),
JOB VARCHAR(9),
SAL NUMERIC,
COMM NUMERIC)
CREATE TABLE SALGRADE
(GRADE NUMERIC,
LOSAL NUMERIC,
HISAL NUMERIC)
INSERT INTO SALGRADE VALUES (1, 700, 1200)
INSERT INTO SALGRADE VALUES (2, 1201, 1400)
INSERT INTO SALGRADE VALUES (3, 1401, 2000)
INSERT INTO SALGRADE VALUES (4, 2001, 3000)
INSERT INTO SALGRADE VALUES (5, 3001, 9999)
CREATE TABLE DUMMY
(DUMMY NUMERIC)


INSERT INTO DUMMY VALUES (0)

Tuesday, 1 July 2014

insert selected record into another table  with structure  without creating new table

insert into MyEmp * select user_id from Emp


insert selected record to already exist table


select user_id,pwd into Table2  from Table1