sql query to view the associations - sql

I attended an interview recently and they gave me the following scenario in SQL:
Employee table:
Employee{
empId,
name
}
Department Table:
Department{
depId,
depName
}
Employee Department Table:
EmployeeDepartment{
empId
depId
}
I was asked to print the result with the following details :
EmpID EmpName DepName DepID
I could not think of any solution there.
I tried going thru some SO answers but could not understand how they work. Some answers suggest the usage of EXISTS and some use JOINS. But I still can't figure out how this is supposed to work.
Can anyone please tell me how to write the sql query to achieve this with and without the association table ? Specifically without using any JOINS?

select
e.EmpID
,e.EmpName
,d.DepName
,d.DepID
from EmployeeDepartment ed
inner join Employee e
on ed.EmpID = e.EmpID
inner join Department d
on ed.DepID=d.DepID
I can't see why you would use no joins, The solution below still joins, just in a silly way:
select
ed.EmpID
,(select top 1 EmpName from Employee e where e.Empid=ed.Empid) EmpName
,ed.DepId
,(select top 1 DepName from Department d where d.DepId=ed.DepId) DepName
from EmployeeDepartment ed

Related

practice sql explanation

http://studybyyourself.com/seminar/sql/exercises/8-3/?lang=en
Please provide data about all employees whose salary is higher or equal to the average salary of employees working in the same department (regardless if employees have left the company or not). Required attributes are last name, first name, salary and department name.
SELECT emp.Last_name, emp.First_name, emp.Salary, D.Name
FROM Employee AS emp
INNER JOIN Department AS D ON emp.Department_id = D.ID
WHERE emp.Salary >=
(
SELECT AVG(e.Salary)
FROM Employee AS e
GROUP BY e.Department_id
HAVING e.Department_id = emp.Department_id
)
Can anyone please help explain the solution? Specifically, what does the 'having' clause do in this case that allows the sub-query to work? I get stuck up until that point without the having clause and I expectedly get the 'subquery returns more than 1 row' error but I am not sure how the having clause is fixing the problem.

How get the right information from these tables

I am trying to get a list of all the employee names and their managers and I cannot figure out how to do it. I have attached the relational model.
In DEPT we know the department number (dept_nbr) which is attached to EMPLOYEE through emp_dept and we know the department manager (dept_mgr) which is attached to EMPLOYEE through emp_nbr
select
mgr.emp_nbr ManagerID,
emp.emp_nbr EmployeeID,
emp.emp_lname, emp.emp_fname,
mgr.emp_lname as mgr_lname, mgr.emp_fname as mgr_fname
from
dept
inner join employee as mgr on dept.dept_mgr = mgr.emp_nbr
inner join employee as emp on dept.dept_nbr = emp.emp_dept

SQL Help for Beginner please

I am learning SQL at university. Stuck one a question and any help would be appreciated.
Question:
Write a SQL statement to retrieve the first and last name as a column “fullname” of the employees which manage 2 or more projects (2pts).
Background info:
EMPLOYEE Table with 2 entries, PROJECT Table with 4 project tables. Last column is ProjectManager which has the Employee ID. Two of the projects are managed by the same employee.
What I have:
Select EMPLOYEE.FirstName+','+EMPLOYEE.LastName AS FullName
FROM EMPLOYEE
WHERE count(PROJECT.ProjectManager==EMPLOYEE.EmployeeID) > 1
EDIT:
Sorry for the confusion guys. Its a PROJECT table with 4 records. I needed to find the first and last name of the Employee whose ID was listed under 2 different project records. The employee ID went in the ProjectManager column. The answer Serif Emek gave seems to be what I needed.
That may help;
Select E.FirstName+','+E.LastName AS FullName
FROM EMPLOYEE E, PROJECT P
WHERE
E.EmployeeId = P.ProjectManager
GROUP BY E.FirstName,E.LastName, E.EmployeeId
HAVING COUNT(*) > 1
Go with the ANSI standard version
SELECT e.FirstName + ',' + e.LastName AS FullName
FROM employee AS e
INNER JOIN project AS p
OM e.EmployeeId = p.ProjectManager
GROUP BY e.FirstName, e.LastName, e.EmployeeId
HAVING COUNT(*) >= 2;
See also: ANSI vs. non-ANSI SQL JOIN syntax

Get employees who worked in more than one department with SQL query

I'm trying to figure out a query which shows the names of the employees who worked in more than 2 departments along with their wage and contact details. I have two tables employees and department. Both of these having the EmployeeName field. I know we have to use the Count function but don't really know how to create the query.
here the tablename and Fields:
Employee (employeeName, wage, contactNo)
Department (employeeName, departmentNo, hours, startDate)
You SQL query would be the following
SELECT e.employeeName, count(departmentNo) FROM Employee e
INNER JOIN Department d ON e.employeeName=d.employeeName
GROUP BY e.employeeName
HAVING COUNT(departmentNo)>2
you can use following query:
SELECT e.employeeName, count(d.departmentname)
FROM Employee e, Department d
where e.deptid=d.deptid
GROUP BY e.employeeName
HAVING COUNT(e.deptid)>=2

Deriving a column's data from a matching column in SQL

So I have a table that has, employee number, employee name, supervisor number.
I want to run a query that will retrieve employee name, employee number, supervisor name and supervisor number. Only one employee doesn't have a supervisor meaning it will have to display nulls. How would I do this? I'm using Oracle SQL Plus. My attempts haven't worked at all! Any help would be much appreciated.
SELECT ename Employee, empno Emp#, super Manager#
FROM emp;
That gets me three of the columns but to be honest I don't even know where to start to get the supervisors names.
It's for university, but I'm studying for a test it's not for an assignment so no cheating happening here :).
The following should work, and give you nulls if the employee has no supervisor:
SELECT empGrunt.ename Employee
, empGrunt.empno EmpNum
, empSuper.ename SupervisorName
, empSuper.empno SupervisorName
FROM emp empGrunt LEFT OUTER JOIN emp empSuper
ON empGrunt.super = empSuper.empno
Assuming that SupervisorNumber is a foreign key relationship back to the Employee table (where it's the EmployeeNumber of the supervisor's record), then you need to use an outer join.
What you need in this case is a left join:
select
e.EmployeeName,
e.EmployeeNumber,
s.EmployeeName as SupervisorName
from Employee e
left join Employee s on s.EmployeeNumber = e.SupervisorNumber