SQL Help for Beginner please - sql

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

Related

Find the names of projects where all employees currently working on that project work in the same department?

I am new to SQL, and am trying to figure our how to "find the names of projects where all employees currently working on the project work in the same department." For this problem, we have three tables. This problem could be simplified if this data was stored in fewer tables. But lets assume we can't modify our tables, and have to make a query.
"employeeproject" - our project table that we can refer to with our empid
empid
projectid
1
2
2
2
3
1
4
1
"project" - Project table, that we can refer to with projectid
projectid
name
1
ring
2
lord
"department" - our department table which we can refer to with empid
empid
department
1
1
2
1
3
2
4
3
I've been attempting to make a single query that can do this. Right now I have a found a way to output the number of departments working on a project, but I still need to get the name of the project to output. Any suggestions would help.
SELECT COUNT(DISTINCT d.department)
FROM employeeproject ep
LEFT JOIN project p ON p.projid = ep.projid
LEFT JOIN department d ON ep.empid = d.empid
GROUP BY p.projid;
Expected result :
project name
Lord
You can use an aggregation on the project name while using count of different departments equal to 1 as condition in an HAVING clause.
SELECT name
FROM project p
INNER JOIN employeeproject ep
ON p.projectid = ep.projectid
INNER JOIN department d
ON ep.empid = d.empid
GROUP BY name
HAVING COUNT(DISTINCT d.deparment) = 1
Here's a demo in MySQL, though this should work on most DBMS'.

Creating a sentence from multiple tables in SQL

I am trying to create this Result “Sabre is 12 years old” sentence using the code below in SQL.
select
businessname+' is ' + cast(count(e.id)as varchar(11))
from
Department d
join
EmployeeDepartmentHistory EDH on edh.departmentid = D.departmentid
join
employee e on edh.id = e.id
group by
name
+' years old'
If I were to run this section of the code
select
businessname+' is ' + cast(count(e.id)as varchar(11))
from
Department d
join
EmployeeDepartmentHistory EDH on edh.departmentid = D.departmentid
join
employee e on edh.id = e.id
group by
name
I will get "Sabre is 12"
But I am having a problem getting the "years old" string at the end.
I am getting this error message with the full code
Msg 8120, Level 16, State 1, Line 4
Column 'HumanResources.Department.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Without having some required information like:
The structure of the tables, which tables have what columns
How the tables are related to each other,
What you are trying to fetch (eg. what 'businessname' signifies here)
it becomes little difficult to provide proper suggestion.
So, assuming that:
EmployeeDepartmentHistory is the bridge table between Department
and Employee tables to take care of Many:Many relationship between
Department and Employee tables
One businessname (whichever table it belongs to) has
many employees (e.id) under it
You are trying to calculate the count of employees (e.id) for
each businessname,
This would be the right query:
SELECT businessname+' is ' + CAST(COUNT(e.id) AS VARCHAR(11)) + ' years old'
FROM Department d
JOIN EmployeeDepartmentHistory EDH on edh.departmentid=D.departmentid
JOIN employee e on edh.id=e.id
GROUP BY businessname

sql query to view the associations

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

Replacing a table value with the previous expression in oracle

Ok so I am having the following scenario
I have a table called employees and have to replaced the last names for some of the people there under the following conditions:
1-The last name must be replaced only to those employees who work on Oxford.
2-Their new last name is going to be the last name of the person that has their employee number -1 ( for instance employee#173 should have now employee#172 last name instead)
This is how I started the query:
select last_name,num_emp
from employees
where num_emp in(
select e.num_emp
from employees
join departments using(num_dept)
join office using(id_office)
where city='Oxford')
And I did a second query to make sure which values were going to replace which
Select last_name,num_emp
from employees
where num_emp in(
select (e.num_emp-1)
from employees
join departments using(num_dept)
join office using(id_office)
where city='Oxford')
Now I thought I could do this and make the code work... but it didn't:
update employees
set last_name=(select last_name
from employees
where num_emp in(
select (e.num_emp-1)
from employees
join departments using(num_dept)
join office using(id_office)
where city='Oxford')
Got error saying unexpected end of SQL command...
So I thought on making a change because I believed having too many values on the set was not the point and here is how I did it for last time:
update employees
set last_name=(select last_name
from employees)
where num_emp =(
select (e.num_emp-1)
from employees
join departments using(num_dept)
join office using(id_office)
where city='Oxford')
Got an error that says is missing right parenthesis, which I know it does not express whaat the issue is. I know I am missing something and part of the sintaxis is wrong as well as I may need to créate another table and add those values so that they get saved there and I can compare them with the original ones, but at this point I am totally blocked and can't discover what is the mistake I am doing. Please help me I'd really apprecciate it!
You are getting confused with what to update and what to update with in your statements.
Here is what to update. I use IN clauses to make it plain. An EXISTS clause would also be appropriate.
update employees
set last_name = ...
where num_dept in
(
select num_dept
from departments
where id_office in
(
select id_office
from office
where city = 'Oxford'
)
);
And here is what to update with:
set last_name =
(
select last_name
from employees prev_employee
where prev_employee.num_emp = employee.num_emp - 1
)
You should use the analytical lag function, then you also fill in gaps if for example employee 172 doesn't exist and you have to put the name of employee 171 in 173.
Your select should be something like this
with new_emp as
(select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
from employees)
select *
from new_emp
where num_emp in(
select e.num_emp
from employees e
join departments using(num_dept)
join office using(id_office)
where city='Oxford');
This select will give you the original last name, new last name, employee number.
Then afterwards your update should be this:
update employees x
set last_name = (with new_emp as
(select last_name,lag(last_name, 1, 0) over (order by num_emp) as new_last_name, num_emp
from employees)
select new_last_name
from new_emp ne
where ne.num_emp = x.num_emp)
where x.num_emp in(
select e.num_emp
from employees e
join departments using(num_dept)
join office using(id_office)
where city='Oxford');

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