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 9223255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:02:04+00:00 2026-06-18T04:02:04+00:00

Consider the following schema: create table TableA (A1 int) create table TableB (B1 int,

  • 0

Consider the following schema:

create table TableA (A1 int)
create table TableB (B1 int, B2 int)
create table TableC (C1 int, C2 int)
create table TableD (D1 int)

And the following query:

SELECT * 
FROM TableA a
INNER JOIN TableB b ON b.B1=a.A1
INNER JOIN (SELECT TOP 1 * 
            FROM TableC c
            WHERE c.C1=b.B1 ORDER BY c.C2) foo ON foo.C1=a.A1
INNER JOIN TableD d ON d.D1=foo.C1

In SQL Fiddle (SQL Server 2008) I get the following result:

The multi-part identifier "b.B1" could not be bound.: SELECT * FROM TableA a INNER JOIN TableB b ON b.B1=a.A1 INNER JOIN (SELECT TOP 1 * FROM TableC c WHERE c.C1=b.B1 ORDER BY c.C2) foo ON foo.C1=a.A1 INNER JOIN TableD d ON d.D1=foo.C1

But replacing the INNER JOIN of the subquery by CROSS APPLY fixes the issue:

SELECT * 
FROM TableA a
INNER JOIN TableB b ON b.B1=a.A1
CROSS APPLY (SELECT TOP 1 * 
            FROM TableC c
            WHERE c.C1=b.B1 AND c.C1=a.A1 ORDER BY c.C2) foo
INNER JOIN TableD d ON d.D1=foo.C1

My questions are:

1) Why the first query fail?

2) Why the second query doesn’t fail?

3) CROSS APPLY is specific to SQL Server. Which would be the SQL Standard solution to this problem?

Important note: Don’t try to understand the logic behind TableA, …, TableD. They’re just an abstraction to a more complex query (which is awful to read). I think you’ll get the idea of the issue.

  • 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-06-18T04:02:06+00:00Added an answer on June 18, 2026 at 4:02 am

    Correlated subqueries are not allowed in from clauses. That is why the inner join version does not work. I don’t actually know the history of this. One problem is that you could get cycles in the dependencies, which while not hard to detect, make the query impossible to process.

    Your query with cross apply is one case where cross apply fits well. However, I don’t know how well it executes on large data.

    Here is an attempt to rewrite it in standard SQL:

    SELECT * 
    FROM TableA a
    INNER JOIN TableB b ON b.B1=a.A1
    INNER JOIN (select *
                from (select c.*, row_number() over (partition by c1 order by c2) 
                      FROM TableC c
                     ) c
                where seqnum = 1 and foo.C1=a.A1 and c.C1=b.B1
               ) foo
    INNER JOIN TableD d ON d.D1=foo.C1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Consider the following schema: create table A ( id int primary id ) create
Consider the following database schema: create table UserGroup ( id int not null auto_increment,
Consider a table with the following schema: id, location, starred There are many records
Consider the following DB schema: CREATE UNIQUE INDEX blah1 ON table1(length); CREATE INDEX blah2
Consider the following scenario. I have a table (a stupid_table ) in a schema
Consider following program: static void Main (string[] args) { int i; uint ui; i
Let's consider the following scenario: I've a master table with a detail table. The
Consider following schema for document { user_name : string, tweet : string } .
Given: SQL Server 2008 R2 SSRS 2008 Humble Programmer Consider two tables: CREATE TABLE

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.