Specified Departments? - SQL - sql

imagine I have two tables, the "departments" table and the "employee" table.
This employee table has a column for "category".
I'd like to make a query for selecting departments that only have a specified type of employees.
Thank you.

You will need to perform a join from your departments and employee table on whatever columns link these two tables together. In the where clause, you will specify what types of employees that you want.
This will return a row for each employee, which might not be what you want. You may use the distinct function on the important columns that you're looking for in the departments table to get the final answer.

select distinct dept_id
from employee
where category = 'cat1'
and dept_id not in (select distinct dept_id
from employee
where dept_id <> 'cat1');

SELECT dept_id
FROM departments
WHERE dept_id NOT IN
(SELECT DISTINCT dept_id
FROM employee
WHERE category_id != #specified_category)
This query assumes there are no departments with no employees, since it will also return those empty departments. If that's a problem, you can add:
AND dept_id IN (SELECT distinct dept_id FROM employee)

Select d.id_department from departments d where not exists
(Select e.id_employee from employees e where e.category!=your_category and e.id_department=d.id_department) you also need to verify that department has employees.

Related

How do I use a value from the superquery inside a subquery?

I need to create a query that shows the last name of an employee, the employee id, the last name of the manager of that employee and the id of that manager.
The last name, id, and the manager id of that employee is easy to do because it is already in one row, which means that the following is sufficient:
SELECT last_name, employee_id, manager_id FROM employees WHERE manager_id IS NOT NULL;
But to get the last_name of the manager, you have to search the same table by the manager id you got from the employee. The solution I found is:
SELECT last_name,
employee_id,
(SELECT last_name FROM employees WHERE employee_id = manager_id),
manager_id
FROM employees
WHERE manager_id IS NOT NULL;
However, it seems that 'manager_id' doesn't work in the subquery (although I expected that) and the output is NULL (for the manager id, all the other columns do have values).
So my question is, how can I use the manager_id in the subquery?
Side note: The manager_id can be different for each employee, so using a constant value doesn't work.
What you need is a correlated subquery. I strongly, strongly recommend that you use table aliases and qualified column names in all your queries. However, these are particularly important with correlated subqueries.
You should write this query as:
SELECT e.last_name, e.employee_id,
(SELECT m.last_name
FROM employees m
WHERE m.employee_id = e.manager_id
),
e.manager_id
FROM employees e
WHERE e.manager_id IS NOT NULL;
The alias e is an abbreviation for the table reference to employees in the outer query. The alias m is an abbreviation for the table reference in the subquery.
Notice that all column references use the table alias. This makes the query unambiguous, can prevent unexpected errors, and makes the query much easier for you and others to understand.
You could use a self inner join ( a join with the same table)
SELECT
a.last_name
, a.employee_id
, b.last_name
, a.manager_id
FROM employees a
INNER JOIN employees b ON b.employee_id = a.manager_id;
The inner join work only if a.manager_id is not null so you can avoid this where condition
When you want to refer to a table in the outer query, you need to either use the full table name like table.field or, as in your case, if the outer query table is same as the subquery table, you need to assign an alias to the outer query table and use it in the subquery like this:
SELECT
last_name, employee_id,
(SELECT last_name FROM employees WHERE employee_id = emp_outer.manager_id),
manager_id
FROM employees emp_outer
WHERE manager_id IS NOT NULL;

How to get hive query for one to many relation with in a table

I have a employee hive table with columns name and department. where 1 employee can belongs to multiple departments.
name, department
xxx,finance
xxx,hr
xxx,transport
xxx,sale
yyy,finance
yyy,hr
yyy,transport
zzz,finace
zzz,hr
zzz,transport
zzz,sale
I need to know distinct employee name who does not belongs to "sale" department.
As of hive 0.13
Select name from employee
where employee.name not in
(select name from employee where department = 'sale')
group by name;
Hopefully names are unique across employees.
You could write a subquery to pull all names that are in sales. Then join that query's results back to your table.
select
results.name,
results.department
from
(select e.name
from employee e
where e.department='sale' group by e.name) invalid_names
right join
(select
e.name,
e.department
from employee e) results
on invalid_names.name = results.name
where invalid_names.name is null;
I'd imagine there is a better way to do this, but this should work :)

Create table error oracle

I am getting an error: ORA-01789: query block has incorrect number of result columns
when trying to create a table from data in 2 other tables. Please help, is this just a syntax error or am I combining the tables in the wrong way?
CREATE TABLE EMPDATA(ID, NAME, SALARY, DEPTNAME)
AS
SELECT e.employee_id, (e.first_name || e.last_name), e.salary
FROM employees e
UNION
SELECT d.department_name
FROM departments d;
I think you want JOIN instead of UNION:
CREATE TABLE EMPDATA(ID, NAME, SALARY, DEPTNAME)
AS
SELECT e.employee_id, (e.first_name || e.last_name), e.salary, d.department_name
FROM employees e
JOIN departments d on(d.department_id = e.department_id);
The number of coumns while using UNION should be same in the SELECT statement
You need to join to set the department and not union
CREATE TABLE EMPDATA(ID, NAME, SALARY, DEPTNAME)
AS
SELECT e.employee_id, (e.first_name || e.last_name), e.salary, d.department_name
FROM employees e
JOIN departments d on d.id = e.department_id
You might need to adjust the column names of the join condition since you did not mention the relation.
Your select query returns three columns, for the table you want four. union does union the results of queries, but they have to have same number and types of columns. Your second query has one column, first - three.
When you create a table from a subquery, the resulting columns must be of the same type and number of columns as the subquery.
It is not worth having an EMPLOYEE_ID column of type NUMBER in the DDL and having the column EMPLOYEE_ID of type DATE for example.
Maybe the column exists in the DDL and the subquery DOES NOT EXIST.
in your example you try to have the column DEPTNAME in the DDL, but in the subquery that column does not exist in the table EMPLOYEES,
If you want to have the column DEPTNAME, you have to do a JOIN with the table DEPARTMENTS.
Remaining as follows:
CREATE TABLE EMPDATA (ID, NAME, SALARY, DEPTNAME) AS
   SELECT e.employee_id, (e.first_name || e.last_name), e.salary, d.department_name
   FROM employees e,
        departments d
WHERE 1 = 1
AND d.department_id = e.department_id;
In my BLOG I have an article that talks about the DDL CREATE TABLE statement in ORACLE SQL, I'll share it for you.
There I talk about a little more things, like creating primary, foreign, insert, etc. klaves.

Show data from two tables which have same column name

I have two table in Access, Employee and Dept.
In Employee table there is empname column and deptcode column, while in Dept table there is deptcode column and deptname column.
I want to do a query which shows empname, deptcode and deptname in a new table. I have tried:
SELECT empname, deptcode, deptname
FROM employee,dept
And it cannot work as the deptcode exist in both table and it creates error. Can anyone kindly tell me how to solve this error problem?
You need to alias your tables in the FROM clause and then use the table alias in the SELECT statement.
select e.empname,d.deptcode,d.deptname
from employee e
inner join dept d
on e.deptcode = d.deptcode;
You just need to associate the columns with the tables in the form of aliases or table name itself. Something like this should work.
select employee.empname,
dept.deptcode,
dept.deptname from employee,dept
where employee.deptcode = dept.deptcode;
Note that I have added a condition to match the department code for the employees

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