0

I have two tables as below:

  1. Students table

    STUDENT TABLE

  2. work table:

    Work table

My query is:

select stud_name
from students
  inner join work on work.library=students.stud_id
where work_id=2

It returns name of student correctly. But please tell me is it possible to retrieve two students name via inner join . Example : get RON and JACK name as the out put in a single result set.

I tried:

select  stud_name,stud_name
from students
  inner join work on work.library=students.stud_id,work.sports=students.stud_id
where work_id=2

But as expected I got a massive error.

3
  • dba.stackexchange.com
    – AK_
    Commented Apr 15, 2015 at 13:31
  • @AK_ DBA.SE would have been an option. It is also in scope on Stack Overflow (one of our default migration targets) and this question currently has 3 out of 5 votes to migrate to SO. Please note that a question shouldn't be cross posted on multiple sites.
    – user289086
    Commented Apr 15, 2015 at 13:46
  • @MichaelT I'm not a moderator on SE, can't see the Mods votes, and don't care much for how SE is managed. I just directed the OP to a good place to find an answer to his question.
    – AK_
    Commented Apr 15, 2015 at 13:53

1 Answer 1

0

Try

Select a.stud_name, b.stud_name from students a, students b, work w
where w.work_id = 2
and a.stud_id = w.library
and b.stud_id = w.sports

The work table is the central part in this and you need the students table twice indepently to get each student.

1
  • 1
    While this will work in MSSQL it is no longer recommended to use the old join syntax. Using inner join is the preferred method as *= was depricated in 2005 and if memory is serving me right was removed in 2012 (it may be 2014 but it is definitely not there in 2014) Commented Apr 15, 2015 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.