0% found this document useful (0 votes)
5 views18 pages

Set Operations in SQL

The document provides an overview of set operations in SQL, including UNION, INTERSECT, and EXCEPT, detailing their functionality, differences, and examples. It explains how to use these operations to combine or compare result sets from multiple SELECT queries while adhering to specific rules regarding column compatibility and data types. Additionally, it covers the use of variables and conditional statements in SQL, highlighting their syntax and practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Set Operations in SQL

The document provides an overview of set operations in SQL, including UNION, INTERSECT, and EXCEPT, detailing their functionality, differences, and examples. It explains how to use these operations to combine or compare result sets from multiple SELECT queries while adhering to specific rules regarding column compatibility and data types. Additionally, it covers the use of variables and conditional statements in SQL, highlighting their syntax and practical applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Set Operations in SQL

1.UNION

The UNION operator in SQL is used to combine the result sets of two or more SELECT
queries into a single result set. The result set will contain distinct values from both queries.
UNION eliminates duplicate records between the SELECT statements by default.

How it works:

 It takes the output of multiple SELECT statements and combines them into a single
result.
 Each SELECT query must have the same number of columns and the columns must
have compatible data types.
 Duplicates are removed automatically, meaning only unique rows will appear in the
result set.
 It can be used to combine results from different tables, or multiple queries targeting
the same table.

Types of UNION

1. UNION

 The UNION operator combines the results of two or more SELECT queries.
 It removes duplicate rows between the queries.

2. UNION ALL

 The UNION ALL operator also combines the results of two or more SELECT queries.
 It includes all rows from both queries, including duplicates.

Difference between UNION and UNION ALL

Feature UNION UNION ALL

Duplicates Removes duplicate rows Includes all rows, even duplicates

Slower due to duplicate


Performance Faster, as it doesn’t check for duplicates
elimination
Feature UNION UNION ALL

Only distinct rows are All rows from both queries are returned,
Result
returned including duplicates

Example of UNION and UNION ALL

Example with UNION (Removing duplicates):

SELECT emp_name FROM Employees WHERE department = 'Sales'


UNION
SELECT emp_name FROM Employees WHERE department = 'Marketing';

Result (if duplicates exist):

emp_name
Alice

Bob

Charlie

Example with UNION ALL (Including duplicates):

SELECT emp_name FROM Employees WHERE department = 'Sales'


UNION ALL
SELECT emp_name FROM Employees WHERE department = 'Marketing';

Result (including duplicates):

emp_name
Alice

Bob

Alice

David

Charlie

 UNION removes any duplicate emp_name across the two departments,


while UNION ALL keeps all the rows, even the duplicates.
Difference Between JOIN and UNION

Feature JOIN UNION


Combines columns from two or
Combines rows from two or
Operation more tables based on a
more queries.
condition.
Results in a single row with
Results in a single column of
Result Type multiple columns from different
rows from multiple queries.
tables.
Used to relate data from Used to combine results from
Used For
multiple tables. different SELECT queries.
Returns rows based on the type
Removes duplicates (with
of JOIN (INNER, LEFT, RIGHT,
Duplicates UNION) or includes duplicates
FULL). Duplicates can be present
(with UNION ALL).
based on join condition.
Requires the same number of
Requires the columns to be from columns with compatible data
Columns
different tables. types from multiple SELECT
statements.
Result Produces a wide result set with Produces a long result set
Structure multiple columns. with multiple rows.
Can be faster (especially with
May be slower due to the UNION ALL) because it does
Performance complexity of matching rows not need to match
from multiple tables. rows.

How They Work Differently:

 JOIN is used to combine columns from multiple tables based on some related field
(e.g., matching IDs). It allows you to get data from multiple sources in a single row.
For example, when you need to get information about employees along with their
department details, you would use a JOIN.
 UNION is used to combine rows from multiple SELECT queries, meaning it appends
the results of one query below another. It’s used when you need to bring together
results from different data sources or the same data source, where the queries are
returning data in the same column structure.

Quick Example:

1. JOIN Example:

SELECT emp_name, department_name


FROM Employees e
JOIN Departments d ON e.department_id = d.department_id;

 This query will combine data from the Employees and Departments tables
by matching the department_id.

2. UNION Example:

SELECT emp_name FROM Employees WHERE department = 'Sales'


UNION
SELECT emp_name FROM Employees WHERE department = 'Marketing';

 This query will combine rows from two queries that return employee
names from the Sales and Marketing departments.

In summary, JOIN works by combining columns from different tables, while


UNION works by combining rows from multiple queries.

2. INTERSECT

The INTERSECT operator in SQL is used to return the common rows that appear in the
results of two or more SELECT statements. It only includes rows that exist in both result
sets.

 It eliminates duplicates by default, meaning it will only show the distinct rows that
are common in both queries.
 The two SELECT queries must have the same number of columns with compatible
data types for each corresponding column.
3. EXCEPT

The EXCEPT operator in SQL returns the rows from the first SELECT query that do not exist
in the second SELECT query. It essentially provides the difference between the two result
sets.

 Similar to INTERSECT, it removes duplicates by default.


 The two SELECT queries must have the same number of columns with compatible
data types for each corresponding column.

How INTERSECT and EXCEPT Work Differently:

Feature INTERSECT EXCEPT

Returns common rows from two SELECT Returns rows from the first query not
Operation
queries. found in the second query.

Duplicates Removes duplicates from the result set. Removes duplicates from the result set.

Requires two SELECT queries with the Requires two SELECT queries with the
Query
same number of columns and compatible same number of columns and compatible
Structure
data types. data types.

Returns only rows that appear in both Returns rows that appear in the first query
Result
queries. but not the second.

Used to find the common elements Used to find the difference between two
Use Case
between two datasets. datasets.

Examples:

1. INTERSECT Example:

Let's say we have two tables: Sales_Employees and Marketing_Employees.

Sales_Employees

emp_name
Alice

Bob

Charlie

Marketing_Employees
emp_name
Alice

David

Charlie

Using INTERSECT, we find the common employees who work in both the Sales and
Marketing departments.

SELECT emp_name FROM Sales_Employees


INTERSECT
SELECT emp_name FROM Marketing_Employees;

Result:

emp_name
Alice

Charlie

 The result will include only the employees Alice and Charlie, as they are present in
both tables.

2. EXCEPT Example:

Using EXCEPT, we can find the employees who work in Sales but not in Marketing

SELECT emp_name FROM Sales_Employees


EXCEPT
SELECT emp_name FROM Marketing_Employees;

Result:

emp_name
Bob

 The result will include only Bob, as he works in Sales but not in Marketing.

Working of INTERSECT and EXCEPT:


 INTERSECT works by finding the overlap between the two SELECT queries. It will only
return rows that appear in both result sets, ensuring the returned rows are common
to both queries.
 EXCEPT works by returning rows from the first query that do not exist in the second
query. It essentially returns the difference between the first and second result sets.

Rules to Follow While Using Set Operations

1. Number of Columns Should Be the Same


All SELECT queries involved in the set operation must have the same number of
columns.
2. Compatible Data Types for Each Column
The corresponding columns in each SELECT query must have compatible data types.
3. Column Order Matters
The order of columns in each SELECT query must be the same. The first column in
the first SELECT query should match the first column in the second query, and so on.
4. Duplicate Removal (for UNION and INTERSECT)
By default, duplicates are removed in UNION and INTERSECT. If duplicates need to
be kept, use UNION ALL instead.
5. NULL Handling
NULL values are considered distinct in set operations, and their behavior will depend
on the operation used (e.g., UNION removes duplicates, including NULLs).
6. Set Operations Work on Result Sets
Set operations combine or compare entire result sets, not individual rows from
different tables.

Combining Columns Using UNION - Addressing Inaccurate Data with


NULL
When asked to combine different columns such as City and Age from the Employee table
using UNION, the challenge arises due to data type mismatches between these columns.
City is a string (text), while Age is an integer. Attempting to combine these directly would
result in inaccurate data or errors. Here’s how we address this problem.

1. Inaccurate Data:
Initially, if you try to directly combine the City and Age columns without making any
adjustments, you would face issues because the columns have different data types.

Here’s the incorrect approach that would lead to inaccurate data:


SQL Query (Inaccurate Data):
SELECT city FROM Employee
UNION
SELECT age FROM Employee;
Problem:

 The first query selects City (a string), and the second query selects Age (an integer).
 UNION requires both queries to return the same number of columns and
compatible data types. Since City is a string and Age is an integer, the data types are
not compatible, and this results in inaccurate or undefined behavior.

Inaccurate Result:

Since the data types don't match, running this query would result in an error or cause data
misalignment. It's important to understand that UNION tries to combine two columns into
one, but if the data types do not match, it won’t work as expected.

2. Accurate Data with NULL:


To resolve this issue and make the data accurate, we need to ensure that both queries
return the same number of columns and compatible data types. This can be achieved by
adding a NULL value in place of missing data in each query.

SQL Query (Accurate Data):


SELECT emp_name, gender, city, NULL AS age FROM Employee
UNION
SELECT emp_name, gender, NULL AS city, age FROM Employee;
Explanation:

 In the first SELECT query, we select emp_name, gender, and city (from the Employee
table) and add NULL for the age column to align the number of columns in both
SELECT queries.
 In the second SELECT query, we select emp_name, gender, and age (from the Employee
table) and add NULL for the city column.

This ensures that both queries return the same number of columns and each query has
compatible data types in the columns. Now, UNION will work correctly, combining the results
without errors or misalignment.

Accurate Result Set:


emp_name gender city age

Alice Female Mumbai NULL

Bob Male Pune NULL


emp_name gender city age

Charlie Male Delhi NULL

Diana Female Bangalore NULL

Alice Female NULL 25

Bob Male NULL 30

Charlie Male NULL 28

Diana Female NULL 22

Key Points:

1. Data Alignment: By using NULL for the missing data in the second query, we ensure
that the number of columns matches between the two queries and that the data
types align correctly.
2. Handling Different Data Types: By aligning the columns correctly and using NULL for
the missing values, we avoid issues related to data type mismatches (string vs.
integer).
3. UNION Removes Duplicates: The UNION operation will remove any duplicate rows
between the two queries, providing a distinct result set.

Variables in SQL
In SQL (especially in T-SQL, which is used in Microsoft SQL Server), variables are used to
store data temporarily during the execution of a batch or procedure. Variables are prefixed
with @ to differentiate them from column names or other identifiers.

Why is @ Used Before a Variable?


 The @ symbol is used to indicate a local variable.
 It tells SQL Server that the identifier is not a column name but a user-defined
variable.

Syntax 1: Declaring a Variable Onl


DECLARE @myVar INT;

This creates a variable named @myVar of type INT (integer), but it does not assign a value
yet.

Syntax 2: Initializing After Declaration


DECLARE @myVar INT;
SET @myVar = 10;

You first declare the variable and then set its value using the SET keyword.

Syntax 3: Declaring and Initializing in One Line


DECLARE @myVar INT = 10;

This is a shorthand method to declare and initialize the variable in the same line.

Printing the Value of a Variable


To print or display the value of a variable:

PRINT @myVar;

Or using SELECT:

SELECT @myVar AS 'Value';

Summary Example

-- Declare and initialize variable


DECLARE @name VARCHAR(50) = 'John';

-- Print the value


PRINT @name;

-- OR
SELECT @name AS 'EmployeeName';
This displays the value of the variable either in the message window (PRINT) or as a result set
(SELECT).

 To print the value of a variable as a message using SELECT, you can use string
concatenation. Here's how you can do it:

Example: Printing Variable as Message using SELECT

DECLARE @empName VARCHAR(50) = 'John';

SELECT 'The employee name is: ' + @empName AS Message;

Output:

Message

The employee name is: John

Explanation:

 'The employee name is: ' is a string message.


 + @empName appends the value of the variable to that message.
 AS Message is used to give a name to the output column.

This is how you show a custom message with variable value using SELECT.

To set a new value to a variable in SQL, you use the SET keyword.

Syntax:

SET @VariableName = NewValue;

Example:

DECLARE @age INT;

-- First value
SET @age = 25;

-- Change value later


SET @age = 30;

-- Print updated value


SELECT 'Updated Age: ' + CAST(@age AS VARCHAR) AS Message;

Output:

Message

Updated Age: 30

Important Note:

 If the variable is not a string, like INT, you must use CAST or CONVERT to change it to a string
before using it in message printing.

 Conditional Statements in SQL: IF...ELSE and CASE

✅ IF...ELSE Statement

 The IF...ELSE statement is used to control the flow of execution in SQL, mainly in
stored procedures, functions, and scripts.
 It checks a condition and executes one block of code if the condition is true ( IF), and
another block if it is false (ELSE).
 It is procedural logic, not used directly inside SELECT queries.
 It helps perform decision-based actions like validations or logging.

✅ CASE Statement

 The CASE statement is a conditional expression used inside SQL queries, especially in
the SELECT, ORDER BY, or WHERE clauses.
 It checks multiple conditions and returns a value when the first condition is met.
 It works like an IF...ELSE IF ladder.
 It is used for row-level decision making (i.e., one value for each row).
 There are two types:
o Simple CASE: Compares a single expression to values.
o Searched CASE: Uses full conditions in each WHEN.

🔍 Summary:

Feature IF...ELSE CASE

Inside queries
Usage Area Scripts, procedures (SELECT,
WHERE)

Conditional
Type Procedural logic
expression

Statement/block- Row-level
Control Level
level control value return

Handled
Needs multiple within one
Multiple Checks
IF...ELSE IF blocks CASE
structure

1. Using IF...ELSE Statement

DECLARE @Day INT = 3;

IF @Day = 1
PRINT 'Monday - Start of the week';
ELSE IF @Day = 2
PRINT 'Tuesday - Keep going';
ELSE IF @Day = 3
PRINT 'Wednesday - Midweek check';
ELSE IF @Day = 4
PRINT 'Thursday - Almost there';
ELSE IF @Day = 5
PRINT 'Friday - Weekend ahead';
ELSE IF @Day = 6
PRINT 'Saturday - Relax';
ELSE IF @Day = 7
PRINT 'Sunday - Rest and reset';
ELSE
PRINT 'Invalid Day';
Feature IF...ELSE CASE

2. Using CASE Statement

DECLARE @Day INT = 5;

SELECT
CASE
WHEN @Day = 1 THEN 'Monday - Start of the week'
WHEN @Day = 2 THEN 'Tuesday - Keep going'
WHEN @Day = 3 THEN 'Wednesday - Midweek check'
WHEN @Day = 4 THEN 'Thursday - Almost there'
WHEN @Day = 5 THEN 'Friday - Weekend ahead'
WHEN @Day = 6 THEN 'Saturday - Relax'
WHEN @Day = 7 THEN 'Sunday - Rest and reset'
ELSE 'Invalid Day'
END AS Message;

✅ Understanding EXISTS and NOT EXISTS in SQL

🔹 What is EXISTS?

 EXISTS checks whether a subquery returns any


rows.
 If rows are returned → result is TRUE → main
query runs.
 If no rows → result is FALSE → main query is
skipped.

🔹 What is NOT EXISTS?

 NOT EXISTS is the opposite of EXISTS.


 It returns TRUE only when the subquery returns
no rows.

🎯 Real Example for Better Understanding

✅ Sample Tables:
Feature IF...ELSE CASE

-- Table: Employees
EmpID | Name
------+-------
1 | Amit
2 | Ravi
3 | Priya

-- Table: Salaries
EmpID | Salary
------+--------
1 | 30000
3 | 35000

🔸 1. Using EXISTS: Find employees who have a


salary

SELECT Name
FROM Employees E
WHERE EXISTS (
SELECT 1
FROM Salaries S
WHERE S.EmpID = E.EmpID
);

🔸 Result:

Amit
Priya

Only those whose EmpID exists in the Salaries table are


selected.

🔸 2. Using NOT EXISTS: Find employees who do


NOT have a salary

SELECT Name
FROM Employees E
WHERE NOT EXISTS (
SELECT 1
FROM Salaries S
WHERE S.EmpID = E.EmpID
);
Feature IF...ELSE CASE

🔸 Result:

Only Ravi's EmpID is not found in the Salaries table.

🔍 Why use SELECT 1 inside EXISTS?


 It doesn’t matter what you select.
 We use SELECT 1 (or any constant) just to
check if a row exists, not to return it.

What are Looping Statements in SQL?

Looping statements in SQL are used to execute a block of


SQL code repeatedly until a specific condition is met.
They are mostly used in stored procedures and scripts
where repetitive tasks like calculations, row-by-row
operations, or controlled data manipulation are needed.

SQL is not a procedural language by default, so looping


is generally done using T-SQL (in SQL Server) or PL/SQL
(in Oracle).

🔹 Types of Looping Statements in SQL Server (T-


SQL)

1. WHILE Loop
o Repeats a block of code while a condition
is TRUE.
o It is the most common loop in T-SQL.
o Syntax:

WHILE <condition>
BEGIN
-- SQL statements
END
Feature IF...ELSE CASE

o Example:

DECLARE @Counter INT = 1;

WHILE @Counter <= 5


BEGIN
PRINT 'Counter is: ' + CAST(@Counter AS
VARCHAR);
SET @Counter = @Counter + 1;
END

2. BREAK
o Exits the loop immediately when a
condition is met.
o Example:

IF @Counter = 3
BREAK;

3. CONTINUE
o Skips the remaining code in the current
iteration and starts the next loop cycle.

🔍 Where to Use Loops in SQL?

 Complex calculations across multiple rows


 Batch processing
 Row-wise operations when cursors are avoided
 Temporary operations inside stored procedures

✅ Example: Print Numbers 1 to 5 Using a WHILE


Loop

DECLARE @i INT = 1;

WHILE @i <= 5
BEGIN
PRINT 'Current number is: ' + CAST(@i AS VARCHAR);
SET @i = @i + 1;
END
Feature IF...ELSE CASE

🔍 Explanation:

 @i is a counter variable starting from 1.


 WHILE runs as long as @i <= 5.
 PRINT outputs the current number.
 SET increments the counter.

You might also like