简体   繁体   中英

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!

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!

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.

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'
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM