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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:04:04+00:00 2026-05-30T10:04:04+00:00

I am using SQL Server 2008 R2 I am trying to write a single

  • 0

I am using SQL Server 2008 R2

I am trying to write a single query that will return only exactly what I need. I will drop in a MovieID and get back a list of ALL genres. If the movie represents a specific genre (has an associated record in the junction table), the Checked value will be 1. If not, then 0.

My result set should look like this:

GenreID    Genre    Checked
1          ABC      0
2          DEF      1
3          HIJ      0
4          KLM      1

My First table is named Genres. It looks like this:

GenreID    Genre
1          ABC
2          DEF
3          HIJ
4          KLM

My second table is named Movies. It looks like this:

MovieID   Title
1         Blah
2         Foo
3         Carpe
4         Diem

My third table is a junction table named Movies_Genres. It looks like this:

MovieID   GenreID
1         2
1         1
1         4
2         1
2         3
3         4
4         1

I would normally, do a couple of queries and a couple of loops to handle this, but I want to really just make the database do the work here. How do I tweak my query so that I can get the resultset that I need with just a single query?

Here’s the starting query:

SELECT  GenreID, 
        Genre
FROM    Genres

Thanks in advance for your help!!!

  • 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-30T10:04:06+00:00Added an answer on May 30, 2026 at 10:04 am
    SELECT g.GenreID, g.Genre, Checked = CASE WHEN EXISTS
          (SELECT 1 FROM dbo.Movies_Genres AS mg
             INNER JOIN dbo.Movies AS m
             ON mg.MovieID = m.MovieID
             WHERE mg.GenreID = g.GenreID
             AND m.MovieID = @MovieID) THEN 1 ELSE 0 END
    FROM dbo.Genres AS g
    ORDER BY g.GenreID;
    

    If there is a unique constraint or primary key on dbo.Movies_Genres(MovieID, GenreID) then this can be simply:

    SELECT g.GenreID, g.Genre, Checked = COUNT(mg.GenreID)
        FROM dbo.Genres AS g
        LEFT OUTER JOIN dbo.Movies_Genres AS mg
        ON g.GenreID = mg.GenreID
        AND mg.MovieID = @MovieID
        GROUP BY g.GenreID, g.Genre;
    

    …since the count for any genre can only be 0 or 1 given a single @MovieID.

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

Sidebar

Related Questions

Using SQL Server 2008 Reporting services: I'm trying to write a report that displays
I am trying to write an aggregate udf for using Sql Server 2008 and
I am using SQL Server 2008, and trying to run the following query in
I am using sql server 2008 and I'm trying to build a query for
I'm using SQL Server 2008 (non-R2) and trying to use Report Builder 3.0. When
I am using SQL Server 2008 developer edition. I was trying to attach the
I am using SQL Server 2008 & 2005 (Express). I'm trying to extract part
I've just installed SQL Server 2008 Developer edition and I'm trying to connect using
I'm trying to export records from SQL Server 2008 to mdb file using OpenDataSource.
I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids

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.