Arbaaz
Arbaaz

Reputation: 321

Sql Query involving multiple tables

I have two tables..

Persons:

  empid(primary key)
  firstname
  lastname
  email

Details:

  Did(primary key)
  salary
  designation
  empid

Now I need to SELECT first name and last name of employees whose designation is Manager.

I am a beginner, I have been learning SQL from W3school (which is awesome btw), if you can suggest me where should I go after completing w3school that would be just great!

Upvotes: 1

Views: 378

Answers (4)

Bridge
Bridge

Reputation: 30701

It's pretty much as you said it, with a simple join on the column that relates the two tables together (in this case EmpID):

SELECT firstname,
       lastname
FROM   Persons
       INNER JOIN Details
         ON Persons.EmpID = Details.EmpID
WHERE  designation = 'Manager' 

As for best source of knowledge, you can't beat books, MSDN, and StackOverFlow if you ask me. There's a good few blogs around too - but they tend to be for more advanced topics. The writers tend to hang around SO anyway!

Upvotes: 1

TechHeadDJ
TechHeadDJ

Reputation: 137

Select p.fname, p.lname
from persons p, details d
where d.designation="Manager" and p.empid=d.empid

Using the single character thing, p. and d., is a way of just doing shorthand and save typing.

And as for SQL source, I am a big fan of tizag. Usually one of my main goto's for PHP, SQL, and anything, really. W3 first, Stack Overflow second, and tizag third. I always find the answer that way.

Upvotes: 0

j0nes
j0nes

Reputation: 8109

I don't know specifically if this works for SQL Server 2008, but this should be rather standard SQL:

SELECT firstname, lastname
FROM Persons
INNER JOIN Details ON Persons.empid = Details.empid
WHERE Details.designation = 'Manager'

Upvotes: 0

gabtub
gabtub

Reputation: 1470

The query you're looking for:

select t1.firstname, t1.lastname
from person t1 inner join
details t2 on t1.empid = t2.empid
where t2.designation = 'Manager'

As for learning Sql on the internet, i dont really know a great place for tutorials, but since you're using sql server 2008 i suggest you often consult the MSDN. If you're motivated you can find very indepth information there.

Upvotes: 0

Related Questions