How do I join 3 tables using mysql - sql

I have the following tables:
TABLE: companies c
FIELDS: id, name, description
TABLE: departments d
FIELDS: id, name, description
TABLE employees e
FIELDS: id, company_id, department_id, name
I'm trying to get a list of all employees for company 1 and also get the department name (d.name) into the results of the row.
Here's my original query:
SELECT e.*, c.name as company
FROM employees e, companies c
WHERE e.company_id=c.id AND e.company_id=1;
What should I add to this query to get the department name (d.name) to appear in each row of the query? Also... e.department_id may be equal to 0 in some cases since there are no departments for a specific company.
Thanks for the help everyone!

SELECT e.id, e.name, e.department_id, c.name, d.name
FROM employees AS e
LEFT JOIN departments AS d ON e.department_id = d.id
LEFT JOIN companies AS c ON e.company_id = c.id
WHERE e.company_id = 1;
Generally you can mix-match joins in any order, as long as any columns/tables you require in a join have already been joined previously. In this case, you're slurping up data from two separate tables that are related via the employees data, and neither of them are co-dependent, so you can order the two LEFT JOIN lines in any order.
As well, with a LEFT join, you get all rows from the table on the "left" side of the join (in this case, the employees table) and matching rows (if any) from the right table (companies/departments). If there's no matching rows in either companies or departments, then any columns coming from those tables will be NULL in the result set.

Related

How to use count and order by with inner join in mysql?

employee table
department table
How to display department name and number of employees working for that department.
My SQL code:
SELECT department.dname , employee.count(*)
FROM employee
INNER JOIN department
ON dno=dnumber
ORDER BY department.dname;
The correct syntax is:
select d.dname, count(*)
from employee e inner join
department d
on d.dno = e.dnumber
group by d.dname
order by d.dname;
Notes:
You are describing an aggregation query with one row per department. That suggests GROUP BY.
Table aliases make the query easier to write and to read.
Qualify all column names. I have to guess what table dno comes from. There should be no reason to guess.

SQL Query - select name with max projects and least projects

I need to create a query that displays the Manager and Department name for the Department with the most number projects and least number of projects based on this database scheme. I can't figure out how to determine the most amount and least amount of projects.
Database Scheme
I'd start by joining together any needed data. You're going to need Manager name, so we'll need to join the employee table.
SELECT
e.fname, d.dname
FROM
department d
LEFT JOIN
employee e ON e.ssn = d.mgr_ssn
LEFT JOIN
(SELECT
department.dnumber, COUNT(*) [projects]
FROM
department
LEFT JOIN
project on department.dnumber = project.dnum
GROUP BY
department) total ON d.dnumber = total.dnumber
HAVING
total.projects = MAX(total.projects)

Join a table in SQL based off two columns?

I have two tables:
Employees (columns: ID, Name)
and
employee partners (EmployeeID1, EmployeeID2, Time)
I want to output EmployeName1, EmployeeName2, Time instead of imployee ids.
(In other words, replace the ids with names, but in two columns at a time)
How would I do this? Would JOIN be the appropriate command?
you need to join the employee table 2 times as the employee partners table acts as many to many connection.
The select should be:
SELECT emp1.name, emp2.name, em.time
FROM Employees emp1
JOIN employee_partners em ON emp1.id = EmployeeID1
JOIN Employees emp2 on emp2.id = EmployeeID2
Often in these situations, you want to use LEFT JOIN:
SELECT e1.name as name1, e2.name as name2, em.time
FROM employee_partners ep LEFT JOIN
Employees e1
ON e1.id = ep.EmployeeID1 LEFT JOIN
Employees e2
ON e2.id = ep.EmployeeID2;
Notes:
The LEFT JOINs ensure that you do not lose rows if either of the employee columns is NULL.
Use tables aliases; they make the query easier to write and to read.
Qualify all columns names; that is, include the table name so you know where the column is coming from.
I also added column aliases so you can distinguish between the names.

Joining two tables in SQL Server when one table has huge data and other has few data

If I want to join two tables (not inner join), Left table has huge data (millions of record), and right table has few records. What should I prefer (Left or Right Outer join) and why.
Well first of all That join is independent of the size of the tables
Well I think this depends what data you want either from Left table or from right table let us assume you have two tables Employees which has millions of records let us put this in right and Department which has 10 record put it in left Now each employee has one department.
Employee
EmpID
DepartmentId
Department
DepartmentId
Department Name
Now Suppose You want to know which employee belongs to which Department Use This Query.
Select e.empId,d.DepartmentName
from employee e
join department d
on e.departmentid=d.departmentid
Now Suppose You want to know which employee has now assigned any department
use the below query
Select e.empId,d.DepartmentName
from employee e
left join department d
on e.departmentid=d.departmentid
where d.departmentid is null
Now Suppose You want to know how many employees which depratment use the below query
Select d.[Department Name],COUNT(e.empID) from Employee e
left join Department d
on e.DepartmentId=d.DepartmentId
group by d.[Department Name]
For more information about joins please use the below image

access sql inner join and count in 2 tables

I have a question about sql queries in access (i just started like 3 days ago)
so there are 2 tables: employes and departments, what i want is to create a query which will show the name and id of each employe and how many departments he manages.
Employee( ID, NAME, social_security_of_employe)
Department(department_id, social_security_of_employee)
(social security is my primary key)
my try was:
SELECT E.ID, E.NAME, COUNT(D.SOCIAL_SECURITY) AS NUMBER_OF_DEPARTMENTS
FROM EMPLOYEE E
INNER JOIN DEPARTMENT D
WHERE D.SOCIAL_SECURITY=E.SOCIAL_SECURITY
GROUP BY SOCIAL_SECURITY
thanks in advance
The correct syntax for MS Access is:
SELECT E.ID, E.NAME, COUNT(D.SOCIAL_SECURITY) AS NUMBER_OF_DEPARTMENTS
FROM EMPLOYEE as E INNER JOIN
DEPARTMENT as D
ON D.SOCIAL_SECURITY = E.SOCIAL_SECURITY
GROUP BY E.ID, E.NAME;
Changes:
MS Access requires as for table aliases.
The WHERE clause should be an ON clause.
The columns in the GROUP BY need to match the unaggregated columns in the SELECT.
You need to group by all non-aggregated columns listed in the select list, i.e.:
SELECT E.ID, E.NAME, COUNT(D.SOCIAL_SECURITY) AS NUMBER_OF_DEPARTMENTS
FROM EMPLOYEE E
INNER JOIN DEPARTMENT D
ON D.SOCIAL_SECURITY=E.SOCIAL_SECURITY
GROUP BY E.ID, E.NAME;
and not on SOCIAL_SECURITY (you are using the COUNT aggregate on it)
Because E.ID and E.NAME are directly correlated, there will only be a single level of grouping.