For instance I have a table called employees where it consists of "Employee ID", "First Name", "Last Name", "Manager ID". To count the subordinate of each manager, I tried to self-joining between the 2 tables.
SELECT e1.first_name, e1.last_name, COUNT(e1.employee_id)
FROM employee e1 INNER JOIN e2 ON e1.employee_id = e2.manager_id
GROUP BY e1.first_name, e1.last_name
Am I right? Also, if I want to join with other tables after self-joining, is the joining statement right?
FROM ((self-joining) INNER JOIN other tables ON "common column")
Combining the first and last name:
SELECT CONCAT(e1.first_name,' ',e1.last_name) "Full Name", COUNT(e1.employee_id)
FROM employee e1 INNER JOIN e2 ON e1.employee_id = e2.manager_id
GROUP BY "Full Name"
I can't compile this....What is wrong?
JOINclause ande2.last_nameisn't in theGROUP BYclause but in the list of columns without an aggregation function. Besides that, why don't you just try what you have in a small example and see if it gets you what you want? And yes, you can add otherJOINclauses the way you showed. But usually you don't need (and want) the parenthesis.