- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
UNION, INTERSECT, and EXCEPT
Add to BookmarkIntroduction
SQL provides powerful set operations that allow combining results from multiple queries. Three key operations—UNION, INTERSECT, and EXCEPT—help in merging, filtering, and subtracting data efficiently. These operations simplify complex queries and improve data retrieval efficiency.
1. UNION
The UNION operator combines the results of two or more SELECT queries into a single result set, removing duplicate records by default.
Syntax:
SELECT column_names FROM table1
UNION
SELECT column_names FROM table2;Example:
SELECT name FROM Customers
UNION
SELECT name FROM Suppliers;This query retrieves unique names from both Customers and Suppliers tables.
UNION ALL
If duplicates should be included, use UNION ALL:
SELECT name FROM Customers
UNION ALL
SELECT name FROM Suppliers;2. INTERSECT
The INTERSECT operator returns only the common records present in both queries.
Syntax:
SELECT column_names FROM table1
INTERSECT
SELECT column_names FROM table2;Example:
SELECT name FROM Customers
INTERSECT
SELECT name FROM Suppliers;This query fetches names that exist in both Customers and Suppliers.
3. EXCEPT
The EXCEPT operator returns the records from the first query that are not present in the second query.
Syntax:
SELECT column_names FROM table1
EXCEPT
SELECT column_names FROM table2;Example:
SELECT name FROM Customers
EXCEPT
SELECT name FROM Suppliers;This query retrieves names present in Customers but not in Suppliers.
Key Considerations:
- The number of columns and their data types must match in both queries.
UNIONremoves duplicates unlessUNION ALLis used.INTERSECTandEXCEPTmay not be supported in all database systems.
Conclusion
SQL set operations—UNION, INTERSECT, and EXCEPT—offer efficient ways to manipulate and compare datasets across tables. Understanding these operations helps in building optimized queries for data analysis and reporting.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Mastering Python in 2025: A Complete Roadmap for Beginners
- Career Guide: Natural Language Processing (NLP)
- Python Challenging Programming Exercises Part 3
- AI in Cybersecurity: The Future of Digital Protection
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Store Data Into CSV File Using Python Tkinter GUI Library
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Google’s Core Update in May 2020: What You Need to Know
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- The Ultimate Guide to Starting a Career in Computer Vision
- Top 10 Blogs of Digital Marketing you Must Follow
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- AI Agents: The Future of Automation, Work, and Opportunities in 2025
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset

