union-conditionals.md (827B)
1 +++ 2 title = "Union, conditionals" 3 +++ 4 5 # Union, conditionals 6 7 `UNION` combines results of two queries. there\'s no other way to make 8 one result column to draw from different tables/columns. subqueries must 9 return tables with same number of columns and compatible data types. 10 `UNION` eliminates duplicates, `UNION ALL` concatenates and keeps 11 duplicates. 12 13 Condition expressions can be used for stuff like: 14 15 ```sql 16 SELECT CASE WHEN CAT = 'H' THEN 'Homework' 17 WHEN CAT = 'M' then 'Midterm' 18 WHEN CAT = 'F' THEN 'Final' 19 ELSE 'Unknown category' END, 20 ENO, POINTS 21 FROM STUDENTS S, RESULTS R 22 WHERE S.SID = R.SID 23 AND S.FIRST = 'Ann' AND S.LAST = 'SMITH' 24 ``` 25 26 To replace a null value, you can do the following (same result): 27 28 - `CASE WHEN x IS NOT NULL THEN x ELSE y END` 29 - `COALESCE (x, y)`