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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:04:34+00:00 2026-06-11T22:04:34+00:00

I wonder how better to write the following query to Microsoft SQL Server. I

  • 0

I wonder how better to write the following query to Microsoft SQL Server.

I have three tables: surveys, survey_presets and survey_scenes. They have the following columns:

CREATE TABLE [dbo].[surveys](
    [id] [int] IDENTITY(1,1) NOT NULL,
     [nvarchar](255) NOT NULL,
    [creation_time] [datetime] NOT NULL,
)
CREATE TABLE [dbo].[survey_presets](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [survey_id] [int] NOT NULL,
    [preset_id] [int] NOT NULL,
)
CREATE TABLE [dbo].[survey_scenes](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [survey_id] [int] NOT NULL,
    [scene_id] [int] NOT NULL,
)

Both survey_presets and survey_scenes have foreign keys on surveys for survey_id column.

Now I want to select all surveys with the count of corresponding presets and scenes for each. Here is the “pseudo-query” of what I want:

SELECT
    surveys.*,
    COUNT(survey_presets, where survey_presets.survey_id = surveys.id), 
    COUNT(survey_scenes, where survey_scenes.survey_id = surveys.id)
FROM surveys
ORDER BY suverys.creation_time

I can do a mess with SELECT DISTINCT, JOIN, GROUP BY, etc., but I’m new to T-SQL and I doubt my query will be optimal in any sense.

  • 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-11T22:04:35+00:00Added an answer on June 11, 2026 at 10:04 pm

    I would do the counting in subqueries to avoid cartesian products. As you might have a few matching rows in presets and also a few in scenes resulting count might be multiplied. You might write simple join query and avoid the multiplication by counting distinct survey_presets.id and distinct survey_scenes.id though.

    SELECT
        surveys.*,
        isnull(presets_count, 0) presets_count,
        isnull(scenes_count, 0) scenes_count
    FROM surveys
    LEFT JOIN 
    (
      SELECT survey_id,
             count(*) presets_count
        FROM survey_presets
       GROUP BY survey_id
    ) presets
      ON surveys.id = presets.survey_id
    LEFT JOIN 
    (
      SELECT survey_id,
             count(*) scenes_count
        FROM survey_scenes
       GROUP BY survey_id
    ) scenes
      ON surveys.id = scenes.survey_id
    ORDER BY surveys.creation_time
    

    How it works

    You can introduce a special kind of subquery called derived table to FROM section of your query. Derived table is defined as normal query enclosed in parenthesis and followed by table alias. It cannot use any column from outer query, but can expose columns you use in ON section to join derived table to main body of the query.

    In this case derived table simply count rows grouped by id; joins connect the counts to surveys.

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

Sidebar

Related Questions

wonder whether someone can help me with the following one... I have a struct
I wonder about that can I write native SQL to add or delete operations
I wonder which solution is better. I have to declare some variables in view,
I wonder if there any better ideas to solve the problem below, I have
I wonder if there is better way to get max from column c1 and
I wonder if there is a better way to get the last digit of
Is a simple exercice, probably some solution better than others, but I wonder which
wonder if someone could help me with a little problem. I have session values
I know Haskell a little bit, and I wonder if it's possible to write
I'm working on a multi-user internet database-driven website with SQL Server 2008 / LinqToSQL

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.