Careeroza
Rules & Sorting
Rules for Set Operators
- Every
SELECTmust have the same number of columns. - Matching columns must have compatible data types.
- Column names in the result come from the first
SELECT. ORDER BYmay appear only once, at the very end, and sorts the combined result.
Sorting the Combined Result
Example
Employees and customer contacts in one alphabetical list:
SELECT FirstName AS Name, 'Employee' AS Type FROM Employees
UNION ALL
SELECT ContactName, 'Customer contact' FROM Customers
ORDER BY Name
LIMIT 6;Result: 6 rows
| Name | Type |
|---|---|
| Aarav Shah | Customer contact |
| Ananya | Employee |
| David | Employee |
| Emma Clark | Customer contact |
| Fatima | Employee |
| Ingrid Olsen | Customer contact |
Set Operators vs Joins
Joins combine tables side by side (more columns). Set operators stack results on top of each other (more rows).