• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: SQL Facts Of UNION And ORDER BY

    April 10, 2017 Ted Holt

    UNION and ORDER BY are powerful SQL features, but put the two together and you may get some strange and frustrating error messages. Fortunately for us DB2 for i professionals, there are easy ways to make the two collaborate and cooperate. Today is a great day to be sure we understand them.

    We need some data for examples. For some of the queries, I use a customer master table (file) and a vendor master table. These are a company’s trading partners, so the two have many attributes in common, especially names and addresses. I also use two sales history tables, one each for the years 2015 and 2016. Businesses commonly use such tables to speedily access summary data that does not change. Such a table would have the same data in various formats (e.g. monthly sales, quarterly sales, yearly sales). In these examples, I use the yearly sales.

    The following tables show the column (field) names for these database tables.

    CUST VENDOR
    CUSTNBR VENDORID
    NAME NAME
    CITY CITY
    STATE STATE
    ZIP ZIPCODE

     

    SALES2015, SALES2016
    CUSTNBR
    YEARLY
    (other columns omitted)

    Fact 1: ORDER BY follows the last subselect and applies to the entire union.

    A common mistake that people make is to put an ORDER BY clause on each subselect. Doing so is not allowed because it is unnecessary. The system combines the result sets from all the subselects, then sorts the combined data. In this example, trading partners are retrieved in order by city within state.

    select state, city, name, custnbr 
      from cust                     
    union all                         
    select state, city, name, vendorid
      from vendor
    order by state, city              
    

    Fact 2: You can sort on column names that are in every component result sets. When combining data for trading partners, for example, you can sort on NAME, CITY, and STATE.

    select custnbr, name, city, state, zip
      from cust
    union all
    select vendorid, name, city, state, zipcode
      from vendor
    order by name
    

    Fact 3: If a column has different names in different subselects, you must rename one or more of them in order to generate an acceptable column name. In this query, I rename the ZIPCODE column of the vendor table to ZIP.

    select custnbr, name, city, state, zip
      from cust
    union all
    select vendorid, name, city, state, zipcode as zip
      from vendor
    order by zip
    

    Fact 4: You may use column numbers in the ORDER BY clause. (This is one of my favorite ways to sort a union.) In this case, the columns do not have to have consistent names across all the subselects. In this example, I order the data by the fifth column, which is ZIP in the customer table and ZIPCODE in the vendor table.

    select custnbr, name, city, state, zip
      from cust
    union all
    select vendorid, name, city, state, zipcode
      from vendor
    order by 5
    

    Fact 5: When using UNION to create a table, you can’t use column numbers. Instead, you must be sure to name the columns consistently across all subselects. There are two ways to do so.

    The first way is to use correlation names.

    create table mylib.tpwork
     as
    (select custnbr as ID, name, city, state, zip
      from cust
    union all
    select vendorid as ID, name, city, state, zipcode as zip
      from vendor)
    with data
    

    The second way is to list the columns in the CREATE TABLE command.

    create table mylib.tpwork (ID, name, city, state, zip)
     as
    (select custnbr, name, city, state, zip
      from cust
    union all
    select vendorid, name, city, state, zipcode
      from vendor)
    with data
    

    Fact 6: You can use ORDER BY in the component SELECTs by enclosing the expressions in parentheses. The only time this makes sense is when you need an ORDER BY to help with row selection. Find the top two customers for the years 2015 and 2016.

     (select * from sales2015  
      order by yearly desc    
      fetch first 2 rows only)
    union all
    (select * from sales2016  
      order by yearly desc    
      fetch first 2 rows only)
    

    As with any union, we can add another ORDER BY clause for the entire union to sort the final result set.

     (select * from sales2015  
      order by yearly desc    
      fetch first 2 rows only)
    union                     
    (select * from sales2016  
      order by yearly desc    
      fetch first 2 rows only)
    order by yearly desc      
    

    Fact 7: You can’t use expressions when ordering a union. (This is a bummer.)

    select custnbr, name, city, state, zip           
      from bbcust                                    
    union all                                        
    select vendorid, name, city, state, zipcode      
      from bbvendor                                  
    order by case when state = 'MN' then 0 else 1 end
    

    The system heartlessly responds with the message “ORDER BY expression is not valid.” Maybe someday.  I can dream.

    Fact 8: These principles also apply to INTERSECT and EXCEPT. I did not include examples of those because unlike UNION, I don’t find them very useful.

    The bottom line: Don’t panic when you see those goofy error messages. In most cases, you can find a way around them.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: IBM i, ORDER BY, SQL, UNION

    Sponsored by
    ARCAD Software

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Watch this webinar where we showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch the replay now!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Blockchain: A Link In Your Long Term IBM i Plan SQL To The Rescue

    2 thoughts on “Guru: SQL Facts Of UNION And ORDER BY”

    • S Sims says:
      April 10, 2017 at 12:23 pm

      For Fact 7, you can use the union as a sub-query to allow the use of an expression for the ordering. An extra layer, but it gives you the result you are looking for.

      select * from (
      select custnbr, name, city, state, zip
      from bbcust
      union all
      select vendorid, name, city, state, zipcode
      from bbvendor
      ) as bb
      order by case when state = ‘MN’ then 0 else 1 end

      Reply
    • Ted Holt says:
      April 14, 2017 at 12:35 pm

      Thanks, S Sims. You’re right, and I didn’t think of that technique when I was writing the article. I just wanted to point out a place where expressions would not work. But I’m glad you mentioned it, because that’s the technique I need for a project I’m working on at the moment.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 24

This Issue Sponsored By

  • Profound Logic Software
  • COMMON
  • ASNA
  • WorksRight Software
  • Manta Technologies

Table of Contents

  • Why Is IBM Giving AIX Shops Better Deals Than IBM i Shops?
  • SQL To The Rescue
  • Guru: SQL Facts Of UNION And ORDER BY
  • Blockchain: A Link In Your Long Term IBM i Plan
  • Assessing The Ransomware Threat On IBM i

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle