Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:43:39+00:00 2026-05-24T23:43:39+00:00

With this standard authors/books setup: CREATE TABLE authors ( id int NOT NULL, name

  • 0

With this standard authors/books setup:

CREATE TABLE authors (
    id int NOT NULL,
    name varchar(255) NOT NULL
)

CREATE TABLE books (
    id int NOT NULL,
    name varchar(255) NOT NULL,
    author_id int NOT NULL,
    sold int NOT NULL
)

INSERT INTO authors VALUES (1, 'author 1')
INSERT INTO authors VALUES (2, 'author 2')

INSERT INTO books VALUES (1, 'book 1', 1, 10)
INSERT INTO books VALUES (2, 'book 2', 1, 5)
INSERT INTO books VALUES (3, 'book 3', 2, 7)

this query somehow doesn’t work:

SELECT
    (
        SELECT
            count(*)
        FROM
            (
                SELECT
                    books.name
                FROM
                    books
                WHERE
                    books.author_id = authors.id
                    AND books.sold > (
                        SELECT
                            avg(sold)
                        FROM
                            books
                        WHERE
                            books.author_id <> authors.id
                    )
            ) AS t
    ) AS good_selling_books
FROM
    authors
WHERE
    authors.id = 1

The error message is:

SQL0204N “AUTHORS.ID” is an undefined name. SQLSTATE=42704

It looks like DB2 loses track of the outermost table after getting 3 levels deep into a subquery?

NOTE: This is just a fabricated query so it may not make much sense (and can be easily rewritten to only have 2 levels nesting which works fine). I just want to confirm if DB2 indeed has such a glaring shortcoming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:43:40+00:00Added an answer on May 24, 2026 at 11:43 pm

    Just found the solution which is rather simple. DB2 has this LATERAL keyword which is needed for such query to work, e.g.

    SELECT
        (
            SELECT
                count(*)
            FROM
                LATERAL( -- this is the only change
                    SELECT
                        books.name
                    FROM
                        books
                    WHERE
                        books.author_id = authors.id
                        AND books.sold > (
                            SELECT
                                avg(sold)
                            FROM
                                books
                            WHERE
                                books.author_id <> authors.id
                        )
                ) AS t
        ) AS good_selling_books
    FROM
        authors
    WHERE
        authors.id = 1
    

    The solution came from this blog post https://www.ibm.com/developerworks/mydeveloperworks/blogs/SQLTips4DB2LUW/entry/scoping_rules_in_db2125?lang=en, where the author also noticed the same shortcoming in DB2:

    But DB2 also didn’t jump two levels up to S.c1. I suppose it could but, alas, it does not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try this command to upload a file (standard.xml) into table book the file
Any reasons why this can not be standard behavior of free() ? multiple pointers
In our data access layer at work we have this standard implementation where the
I'm trying to use this fairly standard line of code in my app: [[UIApplication
As the question states, what date format is ds1248083197360 ? Is this a standard
This is the standard scenario: User registers on the site User receives an account
I have this exported file of some weird (standard for this industry!) format, which
Can standard pointers in .Net do this? Or does one need to resort to
I have the VS2005 standard edition and MS says this: Note: The Windows Service
Is there a standard method I can use in place of this custom method?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.