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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:38:26+00:00 2026-06-10T18:38:26+00:00

I need some help with conditions on left join, my query is as follows

  • 0

I need some help with conditions on left join, my query is as follows

declare @emp table (id int, name varchar(100))
insert into @emp values (1,'Emp1')
insert into @emp values (2,'Emp2')
insert into @emp values (3,'Emp3')
insert into @emp values (4,'Emp4')
insert into @emp values (5,'Emp5')

--selecT * from @emp

declare @salary table(salaryid int, empid int, salary decimal(10,2))
insert into @Salary values (3,3,10000)
insert into @Salary values (4,4,15000)
insert into @Salary values (3,5,10000)

declare @oldsalary table(oldsalaryid int, empid int, oldsalary decimal(10,2))
insert into @oldsalary values (1,1,20000)
insert into @oldsalary values (2,2,25000)

--select * from @Salary
--select * from @oldsalary

declare @rating table (salaryid int, rating varchar(10))
insert into @rating values (4, 'D')
insert into @rating values (3, 'C')
insert into @rating values (1, 'B')
insert into @rating values (2, 'A')
--select * from @rating 

select e.id, e.name, isnull(os.oldsalary, s.salary) salary, r.rating from @emp e 
left join @salary s on e.id=s.empid
left join @oldsalary os on e.id=os.empid
left join @Rating r on r.salaryid = isnull(os.oldsalaryid, s.salaryid)

and this is the output

id  name    salary  rating
1   Emp1    20000   B
2   Emp2    25000   A
3   Emp3    10000   C
4   Emp4    15000   D
5   Emp5    10000   C

As you can see from the query, if oldsalaryid is null then salaryid is used to join the rating table. So the left join is completely based on the value of the column. Is this the proper approach, by looking at the data everything seems to be showing correctly. Can I use this query?

  • 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-10T18:38:27+00:00Added an answer on June 10, 2026 at 6:38 pm

    What you are essentially telling SQL Server to do is to return all valuese from the @emp table. Then all items from the @salary table that has a match on the @emp table are being pulled in. After that, all values from the @oldsalary table that matches values on the @emp table are being pulled in. And the same with the @Rating table.

    I prefer to to go with this approach of only using left joins instead of a combination of left and right joins. It gives the person reading the code a very clear picture of what the dominant tables in a comblex join are. As a result, you won’t need to read the FROM section of the statement backwards and forwards.

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

Sidebar

Related Questions

I need some help adding conditions to a linq query. Take this small example
Need some help with a query.. I have three tables. Source id name 1
Need some help refactoring this if/else block that builds the conditions for a find
I need some help with a MySQL query I'm working on. I have data
I need some help on a MySQL query I'm trying to set up. I
I just need some help to finish turning this SQL : SELECT us.name, pt.task,
I've a query that I need some help with - As part of a
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE

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.