Set Operations


Set operations in SQL allow you to combine the results of two or more queries into a single result set. These operations include UNION, UNION ALL, INTERSECT, and EXCEPT (or MINUS in some SQL dialects). Each of these operations has its specific use case and behavior:

UNION: Combines the results of two or more SELECT queries, removing duplicate rows.
UNION ALL: Combines the results of two or more SELECT queries, including all duplicates.
INTERSECT: Returns only the rows that are common to the results of two SELECT queries.
EXCEPT (or MINUS): Returns the rows from the first SELECT query that are not in the result of the second SELECT query.

Implementing Set Operations in MySQL

UNION:
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;

UNION ALL:
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;

INTERSECT: 
(MySQL does not support INTERSECT directly, but it can be simulated using INNER JOIN or subqueries)

SELECT column1, column2 FROM table1
WHERE (column1, column2) IN (
    SELECT column1, column2 FROM table2
);

EXCEPT:
MySQL does not support EXCEPT directly, but it can be simulated using a LEFT JOIN or subqueries with NOT IN

SELECT column1, column2 FROM table1
WHERE (column1, column2) NOT IN (
    SELECT column1, column2 FROM table2
);



let's create a complete example including the table schema and set operations in MySQL.
Step 1: Create Tables and Insert Data

First, we'll create two tables, employees and contractors, and insert some sample data into them.
-- Create employees table
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);

-- Create contractors table
CREATE TABLE contractors (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);

-- Insert data into employees table
INSERT INTO employees (id, name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');

-- Insert data into contractors table
INSERT INTO contractors (id, name) VALUES
(2, 'Bob'),
(4, 'Dave'),
(5, 'Eve');


Step 2: Perform Set Operations
UNION Example

Combines the results of both tables, removing duplicates.
SELECT name FROM employees
UNION
SELECT name FROM contractors;

name                                              
--------------------------------------------------
Alice                                             
Bob                                               
Charlie                                           
Dave                                              
Eve
UNION ALL Example

Combines the results of both tables, including duplicates.
SELECT name FROM employees
UNION ALL
SELECT name FROM contractors;

name                                              
--------------------------------------------------
Alice                                             
Bob                                               
Charlie                                           
Bob                                               
Dave                                              
Eve

INTERSECT Example (using INNER JOIN)

Finds the names that are common to both tables.
SELECT e.name 
FROM employees e
INNER JOIN contractors c ON e.id = c.id;

name                                              
--------------------------------------------------
Bob

EXCEPT Example (using LEFT JOIN)

Finds names that are in the employees table but not in the contractors table.
SELECT e.name 
FROM employees e
LEFT JOIN contractors c ON e.id = c.id
WHERE c.id IS NULL;

name                                              
--------------------------------------------------
Alice                                             
Charlie 
Alternatively, using NOT IN:
SELECT name 
FROM employees
WHERE id NOT IN (SELECT id FROM contractors);

name                                              
--------------------------------------------------
Alice                                             
Charlie  

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

Procedures in PL/SQL

Cursors in PL/SQL