• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Another Reason To Use Unrequired Correlation Names

    September 15, 2015 Ted Holt

    Almost eight years ago I gave you two good reasons to use a correlation name when SQL did not require one. A recent experience revealed to me another good reason to use an unnecessary correlation name, and I am happy to be able to share that information with you today!

    A colleague of mine had an SQL query that ran to completion but produced the wrong results. He asked me to take a look at it, and I’m glad he did, because the experience taught me something. Let me set up the situation for you.

    Assume two tables: a table of customer orders, and a table of invoices. (I’ve omitted most of the columns (fields) such tables would have in order to shorten the code.)

    create table corders
      (  COrderNumber     dec(7),
         CInvoiceNumber   dec(7),
        primary key (COrderNumber))
    
    create table invoices
      (  InvoiceNumber    dec(7),
         InvoiceDate      date,
       primary key (InvoiceNumber))
    
    insert into COrders values
    (1,3),(2,1),(3,4),(4,2),(5,null),
    (6,null),(7,5),(8,null),(9,null),
    (10,null)
    
    insert into invoices values 
    (1,'2015-01-31'), (2,'2015-01-31'),
    (3,'2015-02-01'), (4,'2015-02-02'),
    (5,'2015-02-02')
    

    His query was something like this:

    select * from COrders
      where CInvoiceNumber in
       (select CInvoiceNumber
          from invoices)
    

    Do you see the problem with this statement? If not, why not study it until you do?

    The query runs, and believe it or not, produces the correct results using my test data.

    CORDERNUMBER  CINVOICENUMBER
    ============  ==============
              1              3  
              2              1  
              3              4  
              4              2  
              7              5  
    

    However–and this is a big however–the query gave the correct results by pure coincidence. In my colleague’s case, the result set was obviously wrong.

    In case you didn’t find it, the problem is that the subquery refers to a column–CORDERNUMBER–that is not found in the INVOICES table. SQL allows this, but not without a warning. In the green-screen Start SQL Interactive Session (STRSQL) command, you get this message:

                                 Confirm Statement
    
    You have entered a subquery that contains a correlation
    without qualification for:
      Field. . . . . . . . . . . . .:    CINVOICENUMBER
    
    
    Press Enter to confirm your SELECT statement.
    Press F12=Cancel to return and cancel your SELECT statement.
    

    If you bother to look at the preprocessor listing, an RPG program with embedded SQL produces similarly-worded message SQL0012. But who looks at preprocessor listings when the compilation succeeds? I shamefully admit that I rarely, if ever, do.

    Suppose my friend had used correlation names to qualify the column references, like this:

    select c.*
     from COrders as c
      where c.CInvoiceNumber in
       (select i.CInvoiceNumber
          from invoices as i)
    

    The GUI tool he was using would have heartlessly responded with message SQL0205, Column CINVOICENUMBER, not in table INVOICES in SOMELIB. My colleague would have had no choice but to fix the query.

    Here’s the query as he should have written it, with correlation names.

    select c.*
     from COrders as c
      where c.CInvoiceNumber in
       (select i.InvoiceNumber 
          from invoices as i)
    

    I probably overuse qualification by correlation names, but using unnecessary qualification has never got me into trouble, and I can always find better things to do than debug SQL queries.

    RELATED STORY

    Reasons to Use Unrequired Correlation Names

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    ProData Computer Services:  Zero in on the data YOU need NOW with DBU, RDB Connect and SQL/Pro.
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars.
    System i Developer:  Session Grid Posted: RPG & DB2 Summit - Chicago, October 20-22

    Reader Feedback On A Hypothetical Future IBM i System Hacker Defends DEF CON Talk on IBM i Vulns

    Leave a Reply Cancel reply

Volume 15, Number 18 -- September 15, 2015
THIS ISSUE SPONSORED BY:

ProData Computer Services
CCSS
WorksRight Software

Table of Contents

  • A First Look At SQL Descriptors
  • Another Reason To Use Unrequired Correlation Names
  • RCAC In DB2 For i, Part 3: Advanced Topics

Content archive

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

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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