I am fairly new to sql and would like to ask for your assistance regarding this query. Is there another way to rewrite this?
Select *
From emp
Where emp_no IN (
Select emp_no
From dept_emp
Where dept_no = 'd002'
);
Any assistance is greatly appreciated.
Thanks.
You can use exists:
select *
from emp
where exists(
select 1 from dept_emp where dept_emp.emp_no = emp.emp_no and dept_no = 'd002'
)
also maybe inner join work:
select emp.*
from emp
join dept_emp
on dept_emp.emp_no = emp.emp_no
and dept_no = 'd002'
you can use an inner join
select *
from emp
inner join (
Select emp_no
From dept_emp
Where dept_no = 'd002'
) t on emp_no.id = t.emp_no
or without subselect
select *
from emp
inner join dept_emp on emp_no.id = dept_emp.emp_no
and dept_emp.dept_no = 'd002'
You can use join like this:
select * from emp e, dept_emp d where e.emp_no = d.emp_no and d.dept_no = 'd002'
- If have the relation between two tables best-way you can used join. other wise you can used inner query (sub select)
- When you used join in RDBMS it will faster that inner query.
- join must be indexed based like: primary key and foreign key. Other wise executing cost will be high. non-index query time consumed maximum.
Related
database:
I have a query that needs a name of employee(employee table) whose sum of total_sale(works_with table)>34000.
This means firstly group emp_id by sum(total_sale) and then sum(total_sale)>34000 and then return their names from employee table.
You can use the group by and having as follows:
select e.emp_id, e.first_name, e.last_name
from employee e
join works_with w on w.emp_id = e.emp_id
group by e.emp_id, e.first_name, e.last_name
having sum(w.total_sale)>34000
You can do this with a correlated subquery:
select e.*
from employee e
where (select sum(ww.total_sale)
from works_with ww
where ww.emp_id = e.emp_id
) > 34000;
Because this avoids the outer aggregation, this can often be faster than a method that uses group by explicitly. In particular, this can take advantage of an index on works_with(emp_id, total_sale).
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)
I have two tables: employees and offices.
I want to create a view with all the columns from 'employees', but only two columns of 'offices'.
Also, I want to select only employees who have unique job titles. I'm trying to do it with the following code, but it returns the following error:
#1248 - Every derived table must have its own alias.
I'm using the following query:
SELECT employees.*, offices.officeCode, offices.phone
FROM (
SELECT DISTINCT employees.jobTitle
)
JOIN offices ON employees.officeCode = offices.officeCode
offices table:
employees table:
Desired result:
employeeNumber|jobTitle|firstName|officeCode|city|state|country
including only the first 6 employees from the sample image (as 'Sales Rep' is a repeated jobTitle, the employees with it wouldn't be included).
Current query has a number of issues:
As error indicates, derived table or subquery does not have an alias.
Incomplete SELECT query in derived table without FROM clause: (SELECT distinct emplyees.jobtitle)
Retrieving columns from a table not referenced in data sources of query (i.e., employees)
Therefore, consider joining the two tables with a count check on unique job title:
SELECT e.*, o.officeCode, o.phone
FROM employees e
INNER JOIN offices o
ON e.officeCode = o.officeCode
WHERE e.jobTitle IN
(SELECT sub.jobTitle
FROM employees sub
GROUP BY sub.jobTitle
HAVING COUNT(*) = 1)
With this query:
SELECT e.*
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees WHERE employeeNumber <> e.employeeNumber AND jobTitle = e.jobTitle)
you get all the employees with jobTitle that does not have any other employee.
So join it to offices:
SELECT e.employeeNumber, e.jobTitle, e.firstName, o.*
FROM (
SELECT e.*
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM employees WHERE employeeNumber <> e.employeeNumber AND jobTitle = e.jobTitle)
) e INNER JOIN offices o
ON e.officeCode = o.officeCode
Just use window functions:
SELECT e.*, o.officeCode, o.phone
FROM (SELECT e.*, COUNT(*) OVER (PARTITION BY jobTitle) as job_cnt
FROM employees e
) e JOIN
offices o
ON e.officeCode = o.officeCode
WHERE job_cnt = 1;
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
I have 3 tables:
Employee (EmployeeNo(PK),shopID(FK1) employeeName, employeeAddress)
Shop (shopID(PK), shopName, shopAddress)
Comment (EmployeeNo(FK), employeeComments)
Is it possible to run a query showing the names of all the employees who have left a comment in the comments table even if the employee name is not in the table?
It is actually a small query that you can easily learn.
Following query will return Employee Name who left the comment and the Comment left by him
SELECT employeeName, employeeComments
FROM Employee INNER JOIN Comment ON (Employee.EmployeeNo = Comment.EmployeeNo)
Select employeeName from employee,comment
where employee.employeeNo = comment.employeeNo
I think it is a simple join which you have to use.
SELECT E.employeeName
FROM Employee E
INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;
SELECT DISTINCT e.EmployeeName
FROM [Employee] AS e
RIGHT JOIN [Comment] AS c
ON e.EmployeeNo = c.EmployeeNo
OR
SELECT e.employeeName
FROM [Employee] AS e
WHERE e.EmployeeNo IN (
SELECT EmployeeNo
FROM [Comment]
)
Try this:
SELECT DISTINCT E.employeeName
FROM Employee E INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;
select e.employeeName,
c.employeeComment
from Employee e
right join Comment c on
e.EmployeeNo=c.EmployeeNo
this will give you employee name & comment
To display the employees who have commented we have following query:
SELECT DISTINCT e.employeeName
FROM Employee e
WHERE e.EmployeeNo IN (SELECT EmployeeNo
FROM Comment)