Aim:- To execute Set Operators and Views in Mysql
Theory:- SQL supports few Set operations which can be performed on the table data.These are used to get meaningful results
from data stored in the table, under different special conditions.
SET OPERATORS:-
Union
Union all
Intersect
Minus
CREATING TABLES FOR SET OPERATORS(UNION and UNION ALL):-
CREATE TABLE student1(id INT,
name VARCHAR(255),
age INT
);
INSERT INTO student1(id,name,age)VALUES
(1,'Devansh Sharma',21),
(2,'Rahul Kalia',26),
(3,'Akshat Jain',34);
CREATE TABLE students(id INT,
name VARCHAR(255),
age INT
);
INSERT INTO students(id,name,age)VALUES
(1,'Devansh Sharma',21),
(2,'Sarita Kumari',26),
(3,'Rohan Dubey',34);
Union:-Query:-
SELECT *from student1UNION
SELECT * from students
Union All:-
SELECT *from student1UNION ALL
SELECT * from students
INTERSECT:-Query:-
FOR CREATING TABLE
CREATE TABLE tab1 ( Id INT PRIMARY KEY
);
INSERT INTO tab1 VALUES(1), (2), (3), (4);
CREATE TABLE tab2 (id INT PRIMARY KEY
);
INSERT INTO tab2 VALUES(3), (4), (5), (6);
FOR EXECUTION
SELECT DISTINCT Id FROM tab1INNER JOIN tab2 USING (Id);
MINUS:-Query:
FOR CREATING TABLE
CREATE TABLE t1 (
id INT PRIMARY KEY
);
CREATE TABLE t2 (
id INT PRIMARY KEY
);
INSERT INTO t1 VALUES(1),(2),(3);INSERT INTO t2 VALUES(2),(3),(4);
FOR EXECUTION
SELECT
id FROM
t1
LEFT JOIN
t2 USING (id)WHERE
t2.id IS NULL;