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

The Archive Base Latest Questions

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

Folks, All am looking for is to extend the INNER JOIN. Let me just

  • 0

Folks,
All am looking for is to extend the INNER JOIN. Let me just get to the point. I have two tables Dept & Emp. One Dept can have multiple Emp’s & not the other way around.

Table Dept
Dept_id   Dept_Name
1         IT
2         HR
3         Other

Table Emp
Emp_id   Dept_id   Emp_Name
11       1         John
12       1         Jill
13       2         Jack
14       3         Jared
15       1         Jim
16       1         Jarret
17       2         Jacob

I need to JOIN it on Dept_id

Expected Results
Dept_id   Dept_name Emp_id   Emp_Name
1         IT        11       John
NULL      NULL      12       Jill
NULL      NULL      15       Jim
NULL      NULL      16       Jarret
2         HR        13       Jack
NULL      NULL      17       Jacob
3         Other     14       Jared

Hope I conveyed what i want precisely. Its just a regular Inner Join on a Foreign Key Constraint. But, I want the values from the First Table (Dept) to be NULL-ed except for the First Match. That being said, I don’t care what’s the first match. See it below – Just the result for the Dept_id 1.

Expected Results (Only for Dept_id = 1)
It could be

Dept_id   Dept_name Emp_id   Emp_Name
1         IT        11       John
NULL      NULL      12       Jill
NULL      NULL      15       Jim
NULL      NULL      16       Jarret

OR

Dept_id   Dept_name Emp_id   Emp_Name
1         IT        15       Jim
NULL      NULL      12       Jill
NULL      NULL      11       John
NULL      NULL      16       Jarret

OR

Two other possibilities.

Thanks in advance. Sorry for the long explanation even though its a simple case.

  • 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-18T22:34:16+00:00Added an answer on June 18, 2026 at 10:34 pm

    I agree with Ic. that this is a crazy query that seems to be doing something that your application should be doing instead of your sql query, that being said, it’s fun to write those queries 🙂

    SELECT CASE WHEN RowNumber = 1 THEN Dept_id ELSE NULL END AS Dept_id,
           CASE WHEN RowNumber = 1 THEN Dept_name ELSE NULL END AS Dept_name,
            Emp_id, Emp_Name
    FROM Dept d
    INNER JOIN 
    (SELECT ROW_NUMBER() OVER (PARTITION BY Dept_id ORDER BY Emp_Name) AS RowNumber, 
           Dept_id, Dept_name, Emp_id, Emp_Name
    FROM Emp ) t on t.Dept_id = d. Dept_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks I have a sealed class as follows. I want to extend this sealed
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Any help at all is appreciated here folks. I'm building a web app in
folks! I have project in cc.net and this project nay start by 3 ways
Folks I am surprised to see my code working, that I dont have any
Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
Hey folks. I'm looking for the best way to do something simple in symfony.
How do you folks retrieve all objects in code upfront? I figure you can
First of all thanks to folks who are currently involved in the development of
Do any of you git gurus have a script that will help with folks

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.