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.
Just found the solution which is rather simple. DB2 has this
LATERALkeyword which is needed for such query to work, e.g.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: