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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:18:11+00:00 2026-05-23T00:18:11+00:00

I have the table with rows: ID CountryCode Status ———– ———– ———– 2 PL

  • 0

I have the table with rows:

ID          CountryCode Status
----------- ----------- -----------
2           PL          1
3           PL          2
4           EN          1
5           EN          1

and by the query

SELECT *
  FROM [TestTable]
  WHERE Status = 1 AND CountryCode NOT IN (SELECT CountryCode
  FROM [TestTable]
  WHERE Status != 1)

I get all countrycodes which hasn’t status value = 2

ID          CountryCode Status
----------- ----------- -----------
4           EN          1
5           EN          1

I feel that this query could be more simpler and clearer.

How can I change it?

Best regards

EDIT

PL can’t be in result because has a record with status 2

EDIT

Script to create and fill table:

USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TestTable](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [CountryCode] [nvarchar](2) NOT NULL,
    [Status] [int] NOT NULL
) ON [PRIMARY]

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'PL', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'PL', -- CountryCode - nvarchar(2)
            2  -- Status - int
            )

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'EN', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )
INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'EN', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )
  • 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-23T00:18:12+00:00Added an answer on May 23, 2026 at 12:18 am

    First: Never use SELECT * in often used code. Especially in production. Call out your columns.

    Soap-Box over.

    Note: i haven’t tried this, and I don’t currently have the management studio installed, so I can’t test it. But I think you want something like this:

    Select Id, CountryCode, Status
    From [TestTable] t
    Where Status <> 2
    And Not Exists(select status from [TestTable] t2 
                                 where t2.Status = 2 
                                 and t2.CountryCode = tt.CountryCode)
    

    At the least, you’ve got the right idea: if you only want the CountryCodes which do not (on any record) correspond to Status = 2, you need to get everything with status 1, and then exclude any existing rows with have a matching row with status 2. I may have the specific syntax for Not Exists incorrect, though.

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

Sidebar

Related Questions

I have the table with rows: ID CountryCode Status ----------- ----------- ----------- 2 PL
I have table rows of data in html being filled from a CGI application.
I have a table with N rows, and I wanna select N-1 rows. Suggestions
I have a table with multiple rows in each row is a select list
I have a table with rows similar to the following. These rows get updated
I have a table, with rows which all have a cell of a div
how remove html table rows after 2nd row? if in table have 5 row,
I have table inside a div tab. The table has 40 rows in it
On my pages I have a table with rows that typically look like this:
I have a table with multiple rows. Each row is a form. I want

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.