Tuesday, October 11, 2016

DIFFERENCE AMONG UNION, MINUS AND INTERSECT? : SQL UNION VS MINUS VS INTERSECT DIFFERENCE

WHAT IS THE DIFFERENCE AMONG UNION, MINUS AND INTERSECT?
UNION combines the results from 2 tables and eliminates duplicate records from the result set.
MINUS operator when used between 2 tables, gives us all the rows from the first table except the rows which are present in the second table.
INTERSECT operator returns us only the matching or common rows between 2 result sets.
To understand these operators, let’s see some examples. We will use two different queries to extract data from our emp table and then we will perform UNION, MINUS and INTERSECT operations on these two sets of data.
UNION
SELECT * FROM EMPLOYEE WHERE ID = 5
UNION
SELECT * FROM EMPLOYEE WHERE ID = 6
ID
MGR_ID
DEPT_ID
NAME
SAL
DOJ
5
2
2.0
Anno
80.0
01-Feb-2012
6
2
2.0
Darl
80.0
11-Feb-2012
MINUS
SELECT * FROM EMPLOYEE
MINUS
SELECT * FROM EMPLOYEE WHERE ID > 2
ID
MGR_ID
DEPT_ID
NAME
SAL
DOJ
1
2
Hash
100.0
01-Jan-2012
2
1
2
Robo
100.0
01-Jan-2012
INTERSECT
SELECT * FROM EMPLOYEE WHERE ID IN (2, 3, 5)
INTERSECT
SELECT * FROM EMPLOYEE WHERE ID IN (1, 2, 4, 5)

ID
MGR_ID
DEPT_ID
NAME
SAL
DOJ
5
2
2
Anno
80.0
01-Feb-2012
2
1
2
Robo
100.0
01-Jan-2012

DIFFERENCE BETWEEN WHERE CLAUSE AND HAVING CLAUSE : SQL WHERE VERSUS HAVING CLAUSE

WHAT IS THE DIFFERENCE BETWEEN WHERE CLAUSE AND HAVING CLAUSE?
WHERE and HAVING both filters out records based on one or more conditions. The difference is, WHERE clause can only be applied on a static non-aggregated column whereas we will need to use HAVING for aggregated columns.
To understand this, consider this example.
Suppose we want to see only those departments where department ID is greater than 3. There is no aggregation operation and the condition needs to be applied on a static field. We will use WHERE clause here:
SELECT * FROM DEPT WHERE ID > 3
ID
NAME
4
Sales
5
Logistics
Next, suppose we want to see only those Departments where Average salary is greater than 80. Here the condition is associated with a non-static aggregated information which is “average of salary”. We will need to use HAVING clause here:
SELECT dept.name DEPARTMENT, avg(emp.sal) AVG_SAL
FROM DEPT dept, EMPLOYEE emp
WHERE dept.id = emp.dept_id (+)
GROUP BY dept.name
HAVING AVG(emp.sal) > 80
DEPARTMENT
AVG_SAL
Engineering
90

As you see above, there is only one department (Engineering) where average salary of employees is greater than 80.

WHAT IS THE DIFFERENCE BETWEEN UNION AND UNION ALL? :SQL UNION VS UNION ALL SOLVED

WHAT IS THE DIFFERENCE BETWEEN UNION AND UNION ALL?
UNION and UNION ALL both unify for add two structurally similar data sets, but UNION operation returns only the unique records from the resulting data set whereas UNION ALL will return all the rows, even if one or more rows are duplicated to each other.
In the following example, I am choosing exactly the same employee from the emp table and performing UNION and UNION ALL. Check the difference in the result.
SELECT * FROM EMPLOYEE WHERE ID = 5
UNION ALL
SELECT * FROM EMPLOYEE WHERE ID = 5
ID
MGR_ID
DEPT_ID
NAME
SAL
DOJ
5.0
2.0
2.0
Anno
80.0
01-Feb-2012
5.0
2.0
2.0
Anno
80.0
01-Feb-2012
SELECT * FROM EMPLOYEE WHERE ID = 5
UNION
SELECT * FROM EMPLOYEE WHERE ID = 5

ID
MGR_ID
DEPT_ID
NAME
SAL
DOJ
5.0
2.0
2.0
Anno
80.0
01-Feb-2012

WHAT IS THE DIFFERENCE BETWEEN JOIN AND UNION

WHAT IS THE DIFFERENCE BETWEEN JOIN AND UNION?
SQL JOIN allows us to “lookup” records on other table based on the given conditions between two tables. For example, if we have the department ID of each employee, then we can use this department ID of the employee table to join with the department ID of department table to lookup department names.
UNION operation allows us to add 2 similar data sets to create resulting data set that contains all the data from the source data sets. Union does not require any condition for joining. For example, if you have 2 employee tables with same structure, you can UNION them to create one result set that will contain all the employees from both of the tables.

SELECT * FROM EMP1
UNION
SELECT * FROM EMP2;

Difference between inner and outer join with example

What is the difference between inner and outer join? Explain with an example.
Inner Join
Inner join is the most common type of Join which is used to combine the rows from two tables and create a result set containing only such records that are present in both the tables based on the joining condition (predicate).
Inner join returns rows when there is at least one match in both tables
If none of the record matches between two tables, then INNER JOIN will return a NULL set. Below is an example of INNER JOIN and the resulting set.
SELECT dept.name DEPARTMENT, emp.name EMPLOYEE
FROM DEPT dept, EMPLOYEE emp
WHERE emp.dept_id = dept.id
Department
Employee
HR
Inno
HR
Privy
Engineering
Robo
Engineering
Hash
Engineering
Anno
Engineering
Darl
Marketing
Pete
Marketing
Meme
Sales
Tomiti
Sales
Bhuti
Outer Join
Outer Join can be full outer or single outer
Outer Join, on the other hand, will return matching rows from both tables as well as any unmatched rows from one or both the tables (based on whether it is single outer or full outer join respectively).
Notice in our record set that there is no employee in the department 5 (Logistics). Because of this if we perform inner join, then Department 5 does not appear in the above result. However in the below query we perform an outer join (dept left outer join emp), and we can see this department.
SELECT dept.name DEPARTMENT, emp.name EMPLOYEE
FROM DEPT dept, EMPLOYEE emp
WHERE dept.id = emp.dept_id (+)
Department
Employee
HR
Inno
HR
Privy
Engineering
Robo
Engineering
Hash
Engineering
Anno
Engineering
Darl
Marketing
Pete
Marketing
Meme
Sales
Tomiti
Sales
Bhuti
Logistics
The (+) sign on the emp side of the predicate indicates that emp is the outer table here. The above SQL can be alternatively written as below (will yield the same result as above):

SELECT dept.name DEPARTMENT, emp.name EMPLOYEE
FROM DEPT dept LEFT OUTER JOIN EMPLOYEE emp
ON dept.id = emp.dept_id