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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:20:15+00:00 2026-06-03T19:20:15+00:00

Hello I have tables sctucture with datas and 2 small questions. CREATE TABLE [dbo].[Parent]

  • 0

Hello I have tables sctucture with datas and 2 small questions.

CREATE TABLE [dbo].[Parent] (
    [id] [int] NOT NULL,
    [Name] [nvarchar](50) NOT NULL, 
 CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Child1] (
    [Child1Id] [int] NOT NULL,
    [ParentId] [int] NOT NULL,
    [SomeData] [int] NOT NULL,  
 CONSTRAINT [PK_Child1] PRIMARY KEY CLUSTERED 
(
    [Child1Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Child2] (
    [Child2Id] [int] NOT NULL,
    [ParentId] [int] NOT NULL,
    [SomeData] [int] NOT NULL,  
 CONSTRAINT [PK_Child2] PRIMARY KEY CLUSTERED 
(
    [Child2Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO Parent VALUES(1,'Name 1')
INSERT INTO Parent VALUES(2,'Name 2')
INSERT INTO Parent VALUES(3,'Name 3')
INSERT INTO Parent VALUES(4,'Name 4')


INSERT INTO [Child1] VALUES(1,1,50)
INSERT INTO [Child1] VALUES(2,1,125)
INSERT INTO [Child1] VALUES(3,2,255)


INSERT INTO [Child2] VALUES(1,1,2)
INSERT INTO [Child2] VALUES(2,2,4)
INSERT INTO [Child2] VALUES(3,2,8)
INSERT INTO [Child2] VALUES(4,3,16)
  1. How select all parets record with at least one child type in both tables.
    I did next query
    but I don’t know optimal way to show total count of this records

    SELECT p.Name, count(Child1) , count(Child2)

  2. How select all parets records only that exist in both tables ?
    SELECT p.Name, count(Child1) , count(Child2)

Thanks in Advice.

  • 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-03T19:20:17+00:00Added an answer on June 3, 2026 at 7:20 pm

    Query 1:

    --at least one child record in either Child1 or Child2
    select distinct p.*
    from parent p
    left outer join child1 c1 on p.id = c1.ParentId
    left outer join child2 c2 on p.id = c2.ParentId
    where coalesce(c1.ParentId, c2.ParentId) is not null
    

    Query 2:

    --at least one child record in both Child1 and Child2
    select distinct p.*
    from parent p
    inner join child1 c1 on p.id = c1.ParentId
    inner join child2 c2 on p.id = c2.ParentId
    

    Note: If you just want to show the count of parent records, change

    select distinct p.*
    

    to

    select count(distinct p.id)
    

    in either query.

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

Sidebar

Related Questions

If i have tables such as: table_1: 1, 'Hello My Name is Hal', '2009:11:02
Hello Everything is right? My problem is the following: I have 3 tables: create
I have two tables, Hello and World: Hello has two columns id(int) and id1(int,
I have two tables, like these: Table People: VARCHAR Name INTEGER Age Table Message
Hello StackOverflow community, I have 2 tables products ======= id name prices ====== id
Let's say I have two tables: TABLE A MessageID | Message 1 | Hello
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
I have two tables like this: Table Name: users emx | userid --------------- 1
Hello guys sorry if the question looks stupid to you. i have 3 tables
Hello I have a table on which I have denied SELECT privs to a

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.