Skip to main content

Set operations in Sql server

 SQL Server set operation

set operation allows us combine or select common data from more 2 tables/ queries. If you want to get data from more than two tables then you will have to repeat set operator between each query.

There are basically 3 types of set operations like 
  1. Union 
  2. Union all (special case A + B)
  3. Intersection
  4. Except (Minus) 
For demo we will use the below query to create a table and use it throughout out examples

CREATE TABLE Product1 (ProductID INT, ProductName VARCHAR(15)) 

CREATE TABLE Product2 (ProductID INT, ProductName VARCHAR(15)) 

INSERT INTO Product1 VALUES (1,'Pen'),(3,'Notebook'),(4,'Pin'),(6,'Punch'),(7,'Glue')

INSERT INTO Product2 VALUES (1,'Pen'),(2,'Stapler'),(4,'Pin'),(8,'Book'),(9,'Glitter')

UNION

union allows us to combine data from two different queries/tables. If the data is common between 1st and 2nd query then it gives distinct data. The output of union is sorted. 

A = {1,2,3,6}, B = {2,3,4}

UNION - A U B = {1,2,3,4,6}

Example

SELECT ProductID, ProductName FROM Product1
UNION
SELECT ProductID, ProductName FROM Product1

UNION ALL

union all is slightly different than union where data from 2nd query directly gets appended below the first query. Union is always faster than union operation as the data is simple appended and not distinct or sorting operation is done. 

SELECT ProductID, ProductName FROM Product1
UNION ALL
SELECT ProductID, ProductName FROM Product1

INTERSECT

Intersect gives you the common data between two queries. 
A = {1,3,5}
B = {1,5,7}

A ∩ B = {1,5}

SELECT ProductID, ProductName FROM Product1
INTERSECT
SELECT ProductID, ProductName FROM Product1

EXCEPT (MINUS)

This operator gives the result from first query which are not present in 2nd query 

A = {1,3,5}
B = {1,2,5}

A - B ={3}

SELECT ProductID, ProductName FROM Product1
EXCEPT
SELECT ProductID, ProductName FROM Product1

Considerations of set operations

While applying set operator make sure the data type of the column position between the query must be same. for e.g in above queries ProductID in query 1 and 2 at position 1 are both INT and ProductName is in position 2 with data type as VARCHAR. Since set operation allows columns of same data types as same position, make sure that you places columns in correct position, for instance you should not swap two varchar field else you might get wrong result.
 although the query will compile correctly.

Common questions which you can get from set operations are below. 
  • Difference between union and union all
  • Get list of products which were sold in july-2021 but not in june-2021.
  • Get list of product which were sold both in june-2021 and july-2021

Comments

Popular posts from this blog

DataZen Syllabus

INTRODUCTION TO DATAZEN PRODUCT ELEMENTS ARCHITECTURE DATAZEN ENTERPRISE SERVER INTRODUCTION SERVER ARCHITECTURE INSTALLATION SECURITY CONTROL PANEL WEB VIEWER SERVER ADMINISTRATION CREATING AND PUBLISHING DASHBOARDS CONNECTING TO DATASOURCES DESIGNER CONFIGURING NAVIGATOR CONFIGURING VISUALIZATION  PUBLISHING DASHBOARD WORKING WITH MAP  WORKING WITH DRILL THROUGH DASHBOARDS

PowerBI Interview Questions and Answers

Power BI Interview Questions – General Questions 1). What is self-service business intelligence? Ans: Self-Service Business Intelligence (SSBI) is an approach to data analytics that enables business users to filter, segment, and, analyse their data, without the in-depth technical knowledge in statistical analysis, business intelligence (BI). SSBI has made it easier for end users to access their data and create various visuals to get better business insights. Anybody who has basic understanding of the data can create reports to build intuitive and shareable dashboards. 2). What are the parts of Microsoft self-service business intelligence solution? Ans: Microsoft has two parts for Self-Service BI  Excel BI Toolkit – It allows users to create interactive report by importing data from different sources and model data according to report requirement.  Power BI – It is the online solution that enables you to share the interactive reports and queries that you have created using ...

MS BI Syllabus

Microsoft Business Intelligence Course Syllabus SSRS – SQL Server Reporting Services  Getting Started 1. Understanding Reporting (Authoring,Management,Delivery) 2. Installing Reporting (Native Mode, SharePoint Integration mode) 3. Building your first report  Authoring Reports 1. Developing Basic Reports (RDL,wizard,designer,datasource,dataset,formatting) 2. Working with expressions (expression to calculate value, Agg functions, exp for objects) 3. Organizing Data (Data Regions, Table, Matrix, Chart, List) 4. Advance Report (Parameter, drill down, drill through, links, 5. Report Model (Data Source, Data Source View, Model , Report Builder 3.0)  Managing Report ( Report Manager) 1. Managing Content (deploying report, folders, linked reports, datasources, value etc) 2. Managing Security (Item Level , Site navigation, localhost – sql) 3. Managing Server Config (Config Manager, Report Manager, Report Server DB)  Delivering Report 1. Accessing Report (Viewing...