TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

DBMS SQL Set Operation

DBMS SQL Set Operation with DBMS Overview, DBMS vs Files System, DBMS Architecture, Three schema Architecture, DBMS Language, DBMS Keys, DBMS Generalization, DBMS Specialization, Relational Model concept, SQL Introduction, Advantage of SQL, DBMS Normalization, Functional Dependency, DBMS Schedule, Concurrency Control etc.

<< Back to DBMS

SQL Set Operation

The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation

  1. Union
  2. UnionAll
  3. Intersect
  4. Minus
DBMS SQL Set Operation

1. Union

  • The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
  • In the union operation, all the number of datatype and columns must be same in both the tables on which UNION operation is being applied.
  • The union operation eliminates the duplicate rows from its resultset.

Syntax

SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

Example:

The First table

ID NAME
1 Jack
2 Harry
3 Jackson

The Second table

ID NAME
3 Jackson
4 Stephan
5 David

Union SQL query will be:

SELECT * FROM First 
UNION
SELECT * FROM Second;

The resultset table will look like:

ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David

2. Union All

Union All operation is equal to the Union operation. It returns the set without removing duplication and sorting the data.

Syntax:

SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

SELECT * FROM First 
UNION ALL
SELECT * FROM Second;

The resultset table will look like:

ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David

3. Intersect

  • It is used to combine two SELECT statements. The Intersect operation returns the common rows from both the SELECT statements.
  • In the Intersect operation, the number of datatype and columns must be the same.
  • It has no duplicates and it arranges the data in ascending order by default.

Syntax

SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;

Example:

Using the above First and Second table.

Intersect query will be:

SELECT * FROM First 
INTERSECT
SELECT * FROM Second;

The resultset table will look like:

ID NAME
3 Jackson

4. Minus

  • It combines the result of two SELECT statements. Minus operator is used to display the rows which are present in the first query but absent in the second query.
  • It has no duplicates and data arranged in ascending order by default.

Syntax:

SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;

Example

Using the above First and Second table.

Minus query will be:

SELECT * FROM First 
MINUS
SELECT * FROM Second;

The resultset table will look like:

ID NAME
1 Jack
2 Harry

Next TopicDBMS SQL Cursors




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf