Error Number: 1137 Can't reopen table: 'd' - mysql

select employee_id, first_name, last_name, department_name, location_id
from employees as e, departments as d
where d.department_id = e.department_id and (location_id)
in
(select location_id
from departments
where department_name = 'Finance')

It seems you don't need subquery to filter location_id
select employee_id, first_name, last_name, department_name, location_id
from employees as e join departments as d on d.department_id = e.department_id
where location_id in (select location_id from departments where department_name = 'Finance')
Note: it's better to use explicit join instead of comma separated join

You could use an inner join instead of an IN clause
select employee_id
, first_name
, last_name
, department_name
, d.location_id
from employees as e
INNER JOIN departments as d ON d.department_id = e.department_id
INNER JOIN ( select location_id
from departments
where department_name = 'Finance') t ON t.location_id = d.location_id
And for readability you should us explicit JOIN syntax, and not implicit syntax based on WHERE.

Related

MySQL Workplace Query

I have the following employee table:
Employee(Columns: Emp_ID, First_Name, Last_Name, DOB)
Department(Columns: Dept_Name, Dept_ID)
Dept_Emp(Columns: Emp_ID, Dept_ID)
Say I am given the employee ID of 1 and I want to find all of the employees who work in the same department as the employee with ID 1 and return their name from the Employee table. I am having trouble formulating the query for such a task.
I was thinking it would look start to look something like:
SELECT Dept_Emp.Emp_ID
FROM Dept_Emp d1, Dept_Emp d2
WHERE d1.Emp_ID = '1'
AND d1.Dept_ID = d2.Dept_ID;
On the inside of some nested query? But I am not sure, as queries get more complex I get more confused and I tend to overthink. Thank you for your time.
Simple solution would be using subquery.
SELECT Emp_ID, First_Name, Last_Name, DOB
FROM Employee
INNER JOIN Dept_Emp ON Employee.Emp_ID = Dept_Emp.Emp_ID
WHERE Dept_Emp.Dept_ID = (SELECT D1.Dept_ID FROM Dept_Emp D1 WHERE D1.Emp_Id = 1 )
Note: If an employee belongs to multiple departments use IN operator.
SELECT Emp_ID, First_Name, Last_Name, DOB
FROM Employee
INNER JOIN Dept_Emp ON Employee.Emp_ID = Dept_Emp.Emp_ID
WHERE Dept_Emp.Dept_ID IN (SELECT D1.Dept_ID FROM Dept_Emp D1 WHERE D1.Emp_Id = 1 );
You can write simple query for output:
SELECT a.Emp_ID, a.First_Name, a.Last_Name, a.DOB, b.Dept_ID FROM Dept_Emp as b LEFT JOIN Employee as a ON a.Emp_ID = b.Emp_ID WHERE b.Dept_ID=(SELECT Dept_ID FROM Dept_Emp WHERE Emp_ID = 1 LIMIT 1)

join query in sql

Here is my SQL query and result. My requirement is to display department_name with a maximum staff count but I don't know how to do that.
I want a result like department_name - SE and staff_ count - 4 only.
select d.department_name, count(staff_id) from department d, staff sf
where d.department_id = sf.staff_id
group by department_name
order by count(staff_id) desc
DEPARTMENT_NAME COUNT(SF.STAFF_ID)
------------------------------ ------------------
SE 4
EEE 2
IT 2
CSE 2
ECE 1
You need to first get the count of each department and then select department with the maximum count. Therefore, you can use sub-query as following:
SELECT d_name, MAX (staff_cnt)
FROM (SELECT d.department_name as d_name, count(staff_id) as staff_cnt
FROM department d, staff sf
WHERE d.department_id = sf.staff_id
GROUP BY department_name);
Try this
alias query
select d.department_name, count(staff_id)
from department d, staff sf
where d.department_id = sf.staff_id AND d.department_name = 'SE'
group by department_name
order by count(staff_id) desc
JOIN Query
select d.department_name, count(staff_id)
from department d
INNER JOIN staff sf ON d.department_id = sf.staff_id
WHERE d.department_name = 'SE'
group by department_name
order by count(staff_id) desc
LIMIT 1
OR Top
select TOP 1 d.department_name, count(staff_id)
from department d
INNER JOIN staff sf ON d.department_id = sf.staff_id
group by department_name
order by count(staff_id) desc
You should use LIMIT 1 for reduce the result but also use explicit join syntax for a better readability:
select d.department_name, count(staff_id)
from department d
INNER JOIN staff sf ON d.department_id = sf.staff_id
group by department_name
order by count(staff_id) desc
limit 1
Or without using limit you could try having on the max:
select d.department_name, count(staff_id) count_staff
from department d
INNER JOIN staff sf ON d.department_id = sf.staff_id
group by department_name
having count_staff = (
select max(count_staff)
from ( select d.department_name, count(staff_id) count_staff
from department d
INNER JOIN staff sf ON d.department_id = sf.staff_id
group by department_name ) t
)

Subquery joining same table

So i have an employees table:
I'm trying to create a query that displays all the info of this employee but i'm a little stumped at the Employee_Reports_To bit. What i want to appear is the First_Name and Last_Name of the Employee_ID that's stored in this column (Eg Joe Bloggs ID 1, reports to employee Joanne Blog ID 50)
Would this need a join or just a simple select. The database is having issues with the link when i try..
SELECT employee.*, departments.Department_Name,
jobTitle.Job_Title,
(SELECT manager.First_Name, manager.Last_Name
FROM Employee manager
INNER JOIN employee AS employeeReportsTo
ON manager.Employee_Reports_To = manager.Employee_ID
) AS Reports_To
FROM Employee employee
LEFT JOIN Departments AS departments
ON departments.Departments_ID = employee.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = employee.Job_Title_ID
ORDER BY `employee`.`Record_Active` DESC,
`employee`.`First_Name` ASC
Any assistance would be appreciated!
Try this solution:
SELECT emp.*,
manager.First_Name,
manager.Last_Name,
departments.Department_Name,
jobTitle.Job_Title
FROM Employee emp
INNER JOIN Employee manager
ON emp.Employee_Reports_To = manager.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = emp.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = emp.Job_Title_ID
ORDER BY emp.Record_Active DESC,
emp.First_Name ASC
It's always better to use Join instead of using Sub Query.
EDITED:
Since column Employee_Reports_To can be null so it's better to use LEFT JOIN instead of INNER JOIN. Like This:
SELECT emp.*,
manager.First_Name,
manager.Last_Name,
departments.Department_Name,
jobTitle.Job_Title
FROM Employee emp
LEFT JOIN Employee manager --Changed Join here
ON emp.Employee_Reports_To = manager.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = emp.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = emp.Job_Title_ID
ORDER BY emp.Record_Active DESC,
emp.First_Name ASC
Looking to your code you could use two time Employee (with alias e1 and e2) one for employee and one for the related manager
SELECT e1.*
, departments.Department_Name
, jobTitle.Job_Title
, e2.First_Name
,e2.Last_Name
FROM Employee e1
INNER JOIN Employee e2 ON e1.Employee_Reports_To = e2.Employee_ID
LEFT JOIN Departments AS departments
ON departments.Departments_ID = e1.Departments_ID
LEFT JOIN Job_Title AS jobTitle
ON jobTitle.Job_Title_ID = e1.Job_Title_ID
ORDER BY e1.`Record_Active` DESC,
e1.`First_Name` ASC

how to display the name of the departments that has the least student count

how to write a query to display the name of the departments that have the least student count. Sort the result based on department name in ascending order
select d.department_name from
(select dd.department_name, count(di.department_id) as id from student di
join department dd on di.department_id=dd.department_id group by dd.department_name) d,
(select min(count(*)) as new from student group by department_id) d2
where d.id=d2.new;
select d.department_name from Department d, Student s where
d.department_id = s.department_id
group by d.department_name
having count(s.student_id)<=all
(select count(s.student_id) from Department d, Student s where
d.department_id = s.department_id
group by d.department_name)
order by department_name;
Try this.
select d.department_id, d.department_name
from Department d
join Student s on d.department_id = s.department_id
group by d.department_id
having count(s.student_id) = (select min(count(s2.student_id))
from student s2
join department d2
on s2.department_id = d2.department_id
group by d2.department_id)
order by d.department_name
You must join the 2 tables to have the needed information.
You'll also have to group them by the selected information such that you can count the students.
And lastly, you place the condition. Needing a subquery to retrieve the minimum number of students.
select department_name
from Department
join Student
on Department.department_id=Student.department_id
having count(*) in
( select min(count(*)) from Student group by department_id)
group by Department.department_id,department_name
order by department_name asc;

How to join these two tables in MySQL?

Here are my tables:
employees
id
name
salary
dept_id
departments
id
name
SELECT employees.id, employees.name, empolyees.dept_id, departments.id,
departments.name
FROM employees, departments
WHERE employees.dept_id = departments.dept_id
ORDER BY employees.name;
Am I joining these two tables right?
Better use the explicit join syntax
SELECT e.id, e.name, e.dept_id, d.id, d.name
FROM empolyees e
INNER JOIN departments d ON e.dept_id = d.id
ORDER BY e.name
And you used departments.dept_id which does not exist. It is departments.id
could say:
SELECT e.id, e.name, empolyees.dept_id, d.id, d.name
FROM employees e
inner join departments d on e.dept_id = d.dept_id
ORDER BY e.name;