SQL COALESCE: A Display Default Does Not Repair Missing Data
A missing database value can create a display problem, but a display fallback is not the same as correcting the source. COALESCE…
Combining two query results does not always mean removing repeated rows. UNION and UNION ALL make different choices. Before selecting one, decide whether identical result rows represent duplicates or separate real events.
Imagine result A contains Ada and Ben. Result B contains Ben and Cara. UNION returns Ada, Ben and Cara: three distinct rows. UNION ALL keeps all four rows, including Ben twice. PostgreSQL documents that UNION removes duplicate result rows unless ALL is used. The results need the same number of columns with compatible types.
If each row represents a distinct person, removing the repeated name might appear useful, although a name alone is usually a weak identifier. If each row represents an activity, two entries for Ben could be two real activities. Removing one would lose information.
SELECT person FROM morning_activity
UNION ALL
SELECT person FROM afternoon_activity;
This sketch keeps the repeated person values. It does not guarantee row order. Add an explicit ordering requirement if the report needs a particular display sequence.
Do not use UNION simply to make a count smaller. Agree the identity of a record and how duplicates should be defined. In a Malta data interview, explaining the intended meaning is more valuable than choosing an operator without checking the business question.
Explore software, cloud and data roles that fit your skills and ambitions.
Explore opportunitiesA missing database value can create a display problem, but a display fallback is not the same as correcting the source. COALESCE…
A query plan helps explain how PostgreSQL expects to run a query. It is not a simple good-or-bad score. Before using plan…
Sometimes a report needs to know whether a related record exists, not display every related row. SQL EXISTS expresses that question directly.…