Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query to select Multiple Column from two table based on a condition

Tags:

sql

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 

Column_DeptNo    int
Column_Dname     varchar(50)
Column_Location  varchar(50)
like image 958
Jpaul Avatar asked Oct 04 '11 11:10

Jpaul


1 Answers

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"?

like image 106
Anna Völkl Avatar answered Nov 15 '22 06:11

Anna Völkl