SQL Query to select Multiple Column from two table based on a condition
I have two tables Table_Employee
,Table_Department
respectively, i wanted to select the manager who belongs to employee more than three times & also want to display depatment name along with it.
This task has to be done in a single query. Is it easy to do it ? here is my table structure .
here the Column_Empno will be the Column_Manager, which means the table is self reference
Table_Employee
Column_Empno int
Column_Fname varchar(50)
Column_Lname varchar(50)
Column_Job varchar(50)
Column_Manager int
Column_HireDate date
Column_Salary int
Column_Commision int
Column_DeptNo int
Table_Department
Col开发者_运维技巧umn_DeptNo int
Column_Dname varchar(50)
Column_Location varchar(50)
Manager + Department:
SELECT Column_Fname, Column_Lname, table_Department.Column_Dname
FROM Table_Employee
INNER JOIN table_Department ON Table_Employee.Column_DeptNo = table_Department.Column_DeptNo
or you could also write:
SELECT Column_Fname, Column_Lname, table_Department.Column_Dname
FROM Table_Employee
WHERE Table_Employee.Column_DeptNo = table_Department.Column_DeptNo
It's not tested. By the way, why do you Name your Colums "Column_..." and not just "Fname", "Lname",... and your table "Table_...." and not just "Employee" and "Department"?
select
t1.column_manager,
t2.column_dname
from
(
select column_manager, column_deptno = max(column_deptno)
from table_employee
group by column_manager
having count(*) > 3
) t1
join table_department t2 on t1.column_deptno = t2.column_deptno
精彩评论