SQL Query with two conditions - sql

I'm new to SQL queries, so I have some problems making them. I'm using SQL Server Management Studio.
My task is to select regional groups of departments where average of salary + commission of employees is less than 2500.
My SQL statement:
select regional_group
from LOCATION
join DEPARTMENT on location.location_id = DEPARTMENT.location_id
join EMPLOYEE on DEPARTMENT.department_id = EMPLOYEE.department_id
where EMPLOYEE.department_id in (select avg(salary + commission)
from employee)
Structure of the database

you have to put condition in where clause of inner query
select regional_group
from LOCATION
join DEPARTMENT on location.location_id = DEPARTMENT.location_id
join EMPLOYEE on DEPARTMENT.department_id = EMPLOYEE.department_id
where EMPLOYEE.department_id in (select department_id
from employee
where salary + commission < 2500)

SQL Query
Select L.Reginal_group
From Employee E
Join Department D ON
D.Department_id = E.Department_id
Join Location L ON
D.Location_id = L.Location_id
WHERE avg(E.salary+E.commission) < 2500

Related

Complex query - retrieve employees working in both two specific departments

I'm trying to figure a way to retrieve employees working in two different departments.
I have 3 simple tables:
employee (employee_id, employee_name)
department (department_id, department_name)
working (eid, did, work_time)
So I have tried to write a SQL query:
select employee_name
from employee, working,department
where eid = employee_id
and did = department_id
and department_name = 'software'
and dname = 'hardware';
But it doesn't work, what is my problem?
The problem is that you are requiring department to be both 'software' and 'hardware'. Also, dname is not a field.
Correcting your query:
select employee_name
from employee, working, department
where eid = employee_id and did = department_id
and (department_name = 'software' or department_name = 'hardware');
But I would prefer this kind of query:
SELECT DISTINCT e.employee_name
FROM employee e
JOIN working w ON w.eid = e.employee_id
JOIN department d ON d.department_id = w.did
WHERE d.department_name IN ('software', 'hardware');
That is to get employees that work in any of the two departments (or both).
If you want only employees that work in both departments, try this:
SELECT e.employee_id, e.employee_name
FROM employee e
JOIN working w ON w.eid = e.employee_id
JOIN department d ON d.department_id = w.did
WHERE d.department_name IN ('software', 'hardware')
GROUP BY e.employee_id HAVING COUNT(DISTINCT d.department_id) = 2;
Would something like this work for you?
SELECT
count(*) as cnt,
employee.employee_name
FROM
employee
JOIN working ON working.eid = employee.employee_id
JOIN department ON department.department_id = working.did
WHERE
department.department_name = 'software' or department.department_name = 'hardware'
GROUP BY employee.employee_name
HAVING cnt > 1
This would count each employee who is linked both software or hardware department. Or you can leave WHERE clause away to get all employees working more than one departments.
What is my problem?
There is no dname column in your tables.
You can simplify the problem as you don't need the department table since the working table contains the department id in the did column.
Then you need to GROUP BY each employee and find those HAVING a COUNT of two DISTINCT department ids:
SELECT MAX(e.employee_name)
FROM employee e
INNER JOIN working w
ON e.employee_id = w.eid
GROUP BY e.employee_id
HAVING COUNT(DISTINCT w.did) = 2
If you want to consider only the software and hardware departments then:
SELECT MAX(e.employee_name)
FROM employee e
INNER JOIN working w
ON e.employee_id = w.eid
INNER JOIN department d
ON w.did = d.department_id
WHERE d.department_name IN ('software', 'hardware')
GROUP BY e.employee_id
HAVING COUNT(DISTINCT w.did) = 2
You can easily obtain employees who work in one specific department:
select *
from Employee e inner join
Working w on e.employee_id = w.eid inner join
Department d on w.did = d.department_id
where d.name = 'software'
Now ambiguity cames. If you want to get all employees work either in software or in hardware:
-- Employees who work at either software or hardware or both departments
select *
from Employee e inner join
Working w on e.employee_id = w.eid inner join
Department d on w.did = d.department_id
where d.name = in ('software', 'hardware')
If you want to get employees who works in both software and hardware departments:
-- Employees who work in both hardware and software deparments simultaneously
select *
from Employee e inner join
Working w on e.employee_id = w.eid inner join
Department d on w.did = d.department_id
where d.name = 'software'
intersect
select *
from Employee e inner join
Working w on e.employee_id = w.eid inner join
Department d on w.did = d.department_id
where d.name = 'hardware'

I need to get number of employees per depatment

This gets me the number of employees per department
SELECT department, COUNT(idEmployees) empCount
FROM employees
GROUP BY department;
You could join on departments to get the buildingID, and then group by that:
SELECT buildingID, COUNT(*)
FROM employees e
JOIN department d ON e.department = d.departmentID
GROUP BY buildingID
i think this is the solution
select d.buildingID, count(e.ID)
from department d
inner join employees e
on d.departmentID = e.department
group by d.buildingID
This question can be answered in two ways. Firstly, if all employees of a department must be attached in a building. Secondly few employees of a department are attached to a building.
For one where employees are attached to a building it's mandatory
SELECT d.buildingId
, COUNT(e.id) count_employee
FROM departments d
INNER JOIN employees e
ON d.departmentid = e.department
GROUP BY d.buildingId
For second where employees are attached to a building but it's optional. In that case LEFT JOIN is used.
SELECT b.buildingId
, COALESCE(t.count_employee, 0) count_employee
FROM building b
LEFT JOIN (SELECT d.buildingId
, COUNT(e.id) count_employee
FROM departments d
INNER JOIN employees e
ON d.departmentid = e.department
GROUP BY d.buildingId) t
ON b.buildingId = t.buildingId
If a building is attached with multiple departments and one employee is assigned with multiple departments then count building wise same employee is only onetime not multiple times. In that case DISTINCT keyword is used inside COUNT().
SELECT d.buildingId
, COUNT(DISTINCT e.id) count_employee
FROM departments d
INNER JOIN employees e
ON d.departmentid = e.department
GROUP BY d.buildingId

MySQL LEFT JOIN with GROUP BY and WHERE clause query

Tables that look like bellow
How to write SQL Query to read all department names with an employee count salary > 1000, if a department not found in employee we required to show as zero counts in output.
You can use left join and aggregation:
select d.name, count(e.id)
from departments d left join
employees e
on e.department_id = d.id and e.salary >= 10000
group by d.name

Hive SQL + FROM not in to JOIN

I have a query with NOT IN clause,need to convert into join statement.
SELECT EMP_NBR
FROM employees not in (select emp_id from departments where dept_id = 10 and division = 'sales')
I think the proper transformation would be a left join:
select EMP_NBR
from employees e join
departments d
on e.dept_id = d.dept_id and
d.dept_id = 10 and
d.division = 'sales'
where d.dept_id is null;
Note: I added what I consider to be correct JOIN conditions.
not in could be mimicked in SQL using just not in the where clause, e.g.
SELECT EMP_NBR FROM employees inner join department on
employees.emp_id =departments.emp_id
where NOT (dept_id = 10 and division = 'sales')

How do I structure this SQL SELECT query?

I have two tables and I need to make a certain select.
First table is Employe that has colums Name *Departament_ID* and Salary
The second table is Departament that has colums Departament ID and *Departament_Name*
My SQL script has to get me the name of the departament, the maximum salay and minimum salary where Departament_ID is '30'
Something like this should do the trick (note: all spellings are exactly as you gave in your first post).
SELECT d.Department_Name AS name,
MAX(e.salary) AS max_salary,
MIN(e.salary) AS min_salary
FROM Department d
LEFT JOIN Employe e ON d.Department_ID = e.Department_ID
WHERE d.Department_ID = 30
SELECT d.department_name,
MIN(e.salary) AS 'Minimum Salary',
MAX(e.salary) AS 'Maximum Salary'
FROM department d,employee e
WHERE d.department_id=30 AND d.department_id=e.department_id
GROUP BY d.department_name
SELECT max(sal) as MaximumSalary
,min(sal) as MinimumSalary
,department.Departament_Name
,employee.Name
FROM employee
INNER JOIN department ON
employee.departmentid = department.department_id
WHERE department.department_id = 31
GROUP BY department.department_id
max and min function will give you the maximum and minimum value of the employee table for the salary column. This will require you to use GROUP BY because they need to calculate the maximum and minimum from a group of data witch in this case is the maximum/minimum by department. We group by department id because it's a unique key of the table. The inner join is here to join the two tables you have. It's an inner join because we want to have the department into the query.
SELECT d.Department_Name AS name,
MAX(e.salary) AS max_salary,
MIN(e.salary) AS min_salary
FROM Department d
Inner JOIN Employee ON d.Department_ID = e.Department_ID
WHERE d.Department_ID = 30