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

  • Home
  • SEARCH
  • 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 9206107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:21+00:00 2026-06-18T00:06:21+00:00

I have a table like below (using SQL server 2008 R2): CREATE TABLE [dbo].[Data](

  • 0

I have a table like below (using SQL server 2008 R2):

CREATE TABLE [dbo].[Data](
    [Id] [int] NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [IsBad] [bit] NOT NULL
) ON [PRIMARY]
GO

Insert into Data  values(100,'Book!',1)
Insert into Data  values(100,'Booklki**',1)
Insert into Data  values(100,'Book',0)
Insert into Data  values(100,'New Book ~~',1)
Insert into Data  values(100,'New Book',0)
Insert into Data  values(100,'B00k…>',1)
Insert into Data  values(101,'Tim3#',1)
Insert into Data  values(101,'%Timer%',1)
Insert into Data  values(101,'T1mer**',1)
Insert into Data  values(101,'Tim6',1)
Insert into Data  values(101,'Time@me',1)
Insert into Data  values(102,'ABC',0)
Insert into Data  values(102,'CDE',0)

I need to select all the ID which are having all IsBad = 1. So, querying above table will return ID: 101 . It must not return 102 or 100 because these IDs are having at least one IsBad=0.

I tried below query

select id,count(distinct isBad) as Total
from Data
group by id
having count(distinct isBad)= 1 

This query includes the IDs which are having all IsBad=0. but I dont need that. I tried to add some more conditions in having clause using AND , but getting error.

How to proceed ? Any help is appreciated.

EDIT: I need to run the query against a table having 50 Million records. So, the query needs to be optimized to return the result in less time.

  • 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-18T00:06:22+00:00Added an answer on June 18, 2026 at 12:06 am
    select  *
    from    Data d1
    where   not exists 
            (
            select  *
            from    Data d2
            where   d1.id = d2.id
                    and d2.IsBad = 0
            )
    

    Live example at SQL Fiddle.

    If you’re just looking for the id, you can use:

    select  distinct id
    ... rest of the query is the same ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008. I have a table like below: ID Name
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
I'm using SQL Server 2008. I have a database table that looks like this
I am using sql server 2000 I have a table like below : lr_no
e.g. If I have a table like below:- create table Categories(CategoryId int primary key,
I have a history table like below CREATE TABLE [dbo].[t_PettyCash_History] ( [TableName] [varchar] (500)
Using SQL Server 2005 I have a table with the following columns id name
In SQL server 2008 R2, I have a table with column containing nvarchar(max) data.
I am using sql server 2008. I have to insert records in temporary table
I have a table in SQL Server that is structured like this: id Name

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.