Merge Queries

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

combine queries/merge queries

In the database we will call it as tables


In the power query we will call it as queries

In power query combining the tables can be done in 2ways


1) Merge queries Joins
2) Append Queries Union All

When ever we need to see the data from more tahn one table in single select
statement we need to use joins

In the real time data will be strored in to different tables


But my client want see the data from all the tables and clinet want create the
reports/visuals from more than one table
If you load the individual tables into power pivot, we will get the performance
issues
When you save any report /visuals in power bi services, and when client open the
report it's take time to load the data. The reason is, it's need to bring the data
from individual tables.

instead of loading all the tables in to power pivot join them as single table in
power query and load into power piovot

like empno, ename, job from emp table dept name,loc from dept table and manager
information from manger table

Joins will help us to to show the data from more than one table

SELECT EMP.*,DEPT.* FROM EMP INNER JOIN DEPT ON EMP.DEPTNO=DEPT.DEPTNO

* --> means all columns

In the power query if you want to merge the tables we have different join types
Types of joins
1) Cartesian join/Cross join 1%
2) Inner join/Equi join/Natural Join 10%
3)Left outer join(90%)
4)Right outer join
5)Full outer join
6) Self Join --> Joing a table to itself is called self join

1) Cartesian join/Cross join:


When ever you want to see all combinations or all probabilities of the data from
the both tables, we have to perform the cross join
But in the real time we will use very rarely

EMP DEPT

Empno Ename Sal Deptno Deptid DName Loc


1001 Srinivas 5000 10 10 sales hyd
1002 Raju 4000 20 20 hr bng
1003 Sudheer 3000 10 30 fin Che
1004 Mahesh 6000 40

Inner join:
It will give only matching records only
When ever you want to perform the inner join we need a common column in the both
tables
Common column means not the same name of the column, the data under the column
should be same

SELECT EMPNO,ENAME,JOB,SAL,EMP.DEPTNO,DNAME,LOC FROM EMP INNER JOIN DEPT ON


EMP.DEPTNO=DEPT.DEPTNO

Empno Ename Sal Deptno Dname Loc


1001 Srinivas 5000 10 Sale Hyd
1002 Raju 4000 20 hr Bng
1003 Sudheer 3000 10 sales hyd

Left Outer Join: Matching Records(Inner Join Records)+Non Mathcing records from
leftside table

EMP DEPT
Empno Ename Sal Deptno Deptid DName Loc
1001 Srinivas 5000 10 10 sales hyd
1002 Raju 4000 20 20 hr bng
1003 Sudheer 3000 10 30 fin Che
1004 Mahesh 6000 40

SELECT EMPNO,ENAME,JOB,SAL,EMP.DEPTNO,DNAME,LOC FROM EMP LEFT OUTER JOIN DEPT ON


EMP.DEPTNO=DEPT.DEPTNO

Empno Ename Sal Deptno DName Loc


1001 Srinivas 5000 10 sales hyd
1002 Raju 4000 20 hr bng
1003 Sudheer 3000 10 sales hyd
1004 Mahesh 6000 40 null null

Right Outer Join: Matching Records(Inner Join Records)+ Non matching records from
the right side

EMP DEPT
Empno Ename Sal Deptno Deptid DName Loc
1001 Srinivas 5000 10 10 sales hyd
1002 Raju 4000 20 20 hr bng
1003 Sudheer 3000 10 30 fin Che
1004 Mahesh 6000 40

SELECT EMPNO,ENAME,JOB,SAL,Dept.DEPTNO,DNAME,LOC FROM EMP RIGHT OUTER JOIN DEPT ON


EMP.DEPTNO=DEPT.DEPTNO

Empno Ename Sal Depnto DName Loc


1001 Srinivas 5000 10 sales hyd
1002 Raju 4000 20 hr bng
1003 Sudheer 3000 10 sales hyd
null null null 30 Fin Che

Full Outer Join: Matching Records(Inner Join Records)+ Non matching records from
the Left side +Non matching records from the Right side

EMP DEPT
Empno Ename Sal Deptno Deptid DName Loc
1001 Srinivas 5000 10 10 sales hyd
1002 Raju 4000 20 20 hr bng
1003 Sudheer 3000 10 30 fin Che
1004 Mahesh 6000 40

SELECT EMPNO,ENAME,JOB,SAL,Dept.DEPTNO,DNAME,LOC FROM EMP FULL OUTER JOIN DEPT ON


EMP.DEPTNO=DEPT.DEPTNO

Empno Ename Sal Deptno Danme Loc


1001 Srinivas 5000 10 sales hyd
1002 Raju 4000 20 hr bng
1003 Sudheer 3000 10 sales hyd
1004 Mahesh 6000 40 null null
null null null 30 Fin Che

LeftAnti Join: Non Matching Records from LeftTable


RightAnti Join: Non Matching Records from RightTable

T1 T2
C1 C1
1 1
2 1
2 2
3 4

You might also like