---
title: "CASE Vs UNION Which is Better in SQL"
description: "How to choose either UNION or CASE in the SQL query is best explained."
url: https://srinimf.com/2017/10/21/sql-query-with-case-logic-avoid-union-why/
date: 2017-10-21
modified: 2026-10-25
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2017/10/pexels-photo-3764402.jpeg?fit=1871%2C1300&ssl=1
categories: ["DB2", "SQL"]
tags: ["CASE instead of UNION", "DB2 Tips and Solutions", "SQL case w3schools"]
type: post
lang: en
---

# CASE Vs UNION Which is Better in SQL

Here is an example of SQL CASE and SQL Union. Here it shows [which is better](https://srinimf.com/2015/01/25/cobol-static-call-and-dynamic-call/). Click here for SQL Tips and Tricks

## SQL Union

Below is the SQL query used by UNION. The example adopted from [Craig Mullin’s blog](http://www.craigsmullins.com)

```
SELECT CREATOR, NAME, &apos;TABLE&apos;FROM SYSIBM.SYSTABLESWHERE TYPE = &apos;T&apos;UNIONSELECT CREATOR, NAME, &apos;VIEW &apos;FROM SYSIBM.SYSTABLESWHERE TYPE = &apos;V&apos;UNIONSELECT CREATOR, NAME, &apos;ALIAS&apos;FROM SYSIBM.SYSTABLESWHERE TYPE = &apos;A&apos;ORDER BY NAME;
```

In the above case, the lines of SQL are more, and it takes a lot of time to process it. So, whenever you want to use multiple UNION statements, it is always better to use a CASE statement. Let me share an example of how it can be re-write using CASE.

Related[DB2 Tough Questions (1 of 4)](https://srinimf.com/2012/01/19/db2-tough-questions-1-of-4/)

## SQL Case

```
SELECT CREATOR, NAME,CASE TYPEWHEN &apos;T&apos; THEN &apos;TABLE&apos;WHEN &apos;V&apos; THEN &apos;VIEW &apos;WHEN &apos;A&apos; THEN &apos;ALIAS&apos;ENDFROM SYSIBM.SYSTABLESORDER BY NAME;
```

Related Posts

- [How to Improve Your SQL Queries](https://srinimf.com/2015/02/05/db2-how-to-write-best-sql-queries/)DateFebruary 5, 2015
- [Grouping Sets: How to Simplify SQL Query](https://srinimf.com/2015/05/27/reduce-sql-complexity-with-grouping-sets-in-db2-v11/)DateMay 27, 2015
- [DB2: How to Use Set Operators Example SQL](https://srinimf.com/2017/12/13/db2-set-operators-to-never-miss-these/)DateDecember 13, 2017

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2017%2F10%2F21%2Fsql-query-with-case-logic-avoid-union-why%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2017%2F10%2Fpexels-photo-3764402.jpeg%3Ffit%3D1200%252C834%26ssl%3D1&description=CASE%20Vs%20UNION%20Which%20is%20Better%20in%20SQL)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2017/10/21/sql-query-with-case-logic-avoid-union-why/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2017/10/21/sql-query-with-case-logic-avoid-union-why/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2017/10/21/sql-query-with-case-logic-avoid-union-why/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2017/10/21/sql-query-with-case-logic-avoid-union-why/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20CASE%20Vs%20UNION%20Which%20is%20Better%20in%20SQL&body=https%3A%2F%2Fsrinimf.com%2F2017%2F10%2F21%2Fsql-query-with-case-logic-avoid-union-why%2F&share=email)
-

### Like this:

Like Loading…
