Lesson Overview
In this lesson, students learn how to combine the output of multiple queries using UNION, UNION ALL, INTERSECT, and MINUS. They also learn how to insert, update, and delete records from tables, and how to manage transactions safely using COMMIT, ROLLBACK, and SAVEPOINT.
Main SQL Structure
Set Operators
UNIONcombines results and removes duplicatesUNION ALLcombines results and keeps duplicatesINTERSECTreturns common rows onlyMINUSreturns rows from first query not found in second query
DML Statements
INSERTadds new rowsUPDATEchanges existing rowsDELETEremoves rowsCOMMITsaves changes permanentlyROLLBACKcancels changesSAVEPOINTcreates a point to roll back to
Important Notes Before Using Set Operators
- The number of columns in both SELECT statements must be the same.
- The data types of corresponding columns must be compatible.
- The column names in the final result are taken from the first SELECT statement.
ORDER BYshould come at the end of the full set operation.
Examples
Combine employees and job history rows, while removing duplicates.
Combine employees and job history rows, while keeping duplicates.
Return only common rows between employees and job_history.
Return rows موجودة في employees وغير موجودة في job_history.
Return department IDs الموجودة في departments وغير موجودة في employees.
Return employee IDs that do not appear as manager IDs.
The HR department needs a list of department IDs for departments that do not contain the job ID
ST_CLERK.
Return department IDs for departments that do NOT have manager 100.
Insert a new department into the departments table.
Create a savepoint before updating data.
Update department name for the inserted row.
Use COMMIT to save changes or ROLLBACK to undo them.
Delete the inserted department row.
Additional Practice Examples
Key Explanations
UNION vs UNION ALL
UNION removes duplicate rows, while UNION ALL keeps all rows. If performance matters and duplicates are acceptable, UNION ALL is faster.
MINUS
MINUS returns rows that exist in the first query but do not exist in the second query. It is very useful for reports like “departments without employees” or “jobs not used yet.”
INSERT / UPDATE / DELETE
These are DML statements because they change the data inside tables.
Always be careful when using them, especially UPDATE and DELETE without a WHERE clause.
COMMIT / ROLLBACK / SAVEPOINT
COMMIT saves the changes permanently. ROLLBACK cancels changes. SAVEPOINT lets you return to a specific point in the transaction.
Questions
Tasks
Task 1
Write a query to return all department IDs that appear in employees but do not appear in departments.
Task 2
Insert a new department, then create a savepoint, update the department name, and finally roll back to the savepoint.
Task 3
Write two queries using UNION and UNION ALL on the same data, then compare the outputs.
Challenge Task
Build a full report that finds department IDs that do not have:
- employees with job
ST_CLERK - manager 100
- any rows in the employees table
MINUS more than once and compare your output carefully.