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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:48:30+00:00 2026-06-17T04:48:30+00:00

This is a snippet from my stored Proc SELECT NULL AS StoryID , AlbumID

  • 0

This is a snippet from my stored Proc

SELECT  NULL AS StoryID
      , AlbumID
      , CAST(NULL as varchar) AS StoryTitle
      , AlbumName
      , (SELECT URL FROM AlbumPictures AS AlbumPictures_3 WHERE (AlbumID = Albums.AlbumID) AND (AlbumCover = 'True')) AS AlbumCover
      , Votes
      , CAST(NULL as Int) AS PictureId
      , 'albums' AS tableName
      , (SELECT NestedAlbums.AlbumID FROM NestedAlbums WHERE (AlbumID = Albums.AlbumID)) AS Flag
INTO #Results2
FROM Albums WHERE AlbumID IN (SELECT StringVal FROM funcListToTableInt(@whereAlbumID))

I have used nested selects in my Query above. I am curious to know whether Nested Selects are better than LEFT/Right JOINS OR should i use JOINS?

Table Albums:

enter image description here

Table NestedAlbums:

enter image description here

  • 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-17T04:48:31+00:00Added an answer on June 17, 2026 at 4:48 am

    In general writing this an explicit OUTER JOIN would be better.

    SQL Server will probably need to add an Assert to the plan with the sub query version that verifies the sub query only returns at most one row (unless this is guaranteed by a unique index). This can limit the possible transformations available. See Scalar Subqueries for more about this.

    Also (though not relevant to the example in your question as both sub queries are different) writing as an explicit JOIN allows you to use multiple columns from the joined table with one lookup whereas using separate similar subqueries would not (SQL Server has no logic to detect the common sub expressions).

    Edit:

    Following discussion in comments something like

    SELECT NULL                      AS StoryID,
           A.AlbumID,
           CAST(NULL AS VARCHAR(30)) AS StoryTitle,
           A.AlbumName,
           AP.URL                    AS AlbumCover,
           A.Votes,
           CAST(NULL AS INT)         AS PictureId,
           'albums'                  AS tableName,
           CASE
             WHEN EXISTS (SELECT *
                          FROM   NestedAlbums NA
                          WHERE  NA.AlbumID = A.AlbumID
                                 AND ( AccountId = @AccountId )) THEN 1
             ELSE 0
           END                       AS Flag
    INTO   #Results2
    FROM   Albums A
           LEFT OUTER JOIN AlbumPictures AP
             ON ( AP.AlbumID = A.AlbumID )
                AND ( AP.AlbumCover = 'True' )
    WHERE  A.AlbumID IN (SELECT StringVal
                         FROM   funcListToTableInt(@whereAlbumID)) 
    

    You may notice this still has a sub query in the SELECT list but CASE ... EXISTS will be implemented efficiently as a semi join.

    At the moment your query assumes that at most one matching row per album will be returned from AlbumPictures and would error out if this assumption is not true. This changes the semantics in that no error will be returned and you would get multiple rows with the various URLs. If you didn’t want that to happen you would need to define which URL to use and add a GROUP BY

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

Sidebar

Related Questions

This line of code is a snippet from my select statement. frdFreedays - DateDiff(dd,conReceiptToStock,GetDate())
This snippet from official website works as expected: $treeObject = Doctrine::getTable('Category')->getTree(); $rootColumnName = $treeObject->getAttribute('rootColumnName');
I tried to use this snippet from the Soundcloud API: <script src=http://connect.soundcloud.com/sdk.js> <script> SC.initialize({
I've just read this snippet from another answer : When you create a block
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
I'm having problems with the getline instruction from fstream. this is a snippet from
This is a little snippet from a little flash game I'm working on: This
This is a code snippet from O'Reilly Learning Opencv, cvNamedWindow(Example3, CV_WINDOW_AUTOSIZE); g_capture = cvCreateFileCapture(argv[1]);
I try to understand this example from jquery api in this snippet var tags
What's wrong with this snippet of code? import numpy as np from scipy import

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.