Update column in table2 based on select query which contains count() from table1 using postgresql - sql

Tables
Need to update department table's dcount column based on number of employees working in each department from employee table's dno column.
Tried using
update department set dcount=(select count() from employee INNER JOIN department ON employee.dno=department.dnumber group by dno);*
which gave an error : more than one row returned by a subquery used as an expression
Desired result is:
**dname|dnumber|dcount
Research|5|4
Admin|4|3
Headquarters|1|1**
Need help please.
Thanks in advance.
Gruheeth

Your subquery (select count() ...) returns several rows, one per employee, where postgres expect only one row from this subquery in order to update one row at a time in the department table. In this case, you case use a cte instead :
WITH list AS
(
select dno, count(*) AS dno_count
from employee
group by dno
)
update department AS d
set dcount = l. dno_count
from list AS l
where d.dnumber = l.dno ;

Related

How this query is returning me 1100 value if there is no salary against the foreign key 30

Write a query to get the minimum salary of department number 30,
the salary should add commission
select *
from emp
select *
from dept
select min(sal+comm) as 'sal+com'
from emp
where deptno = 3
select min(sal+comm) as 'sal+comm'
from emp, dept
where dept.deptno = 30
This is the question and the queries I tried they both are giving me different outputs but how its giving 1100 if there is no foreign key of 30 in emp table.
Your query has basically said for every row in emp select every row in dept and filter the results where dept has a dept no of 30. Even though you have a foreign key you still have to specify the join in the select. The FL is more about referential integrity.
Try
select min(sal+comm) as "sal+comm"
from emp inner join dept
on dept.deptno=emp.deptno
where dept.deptno=30
However because you have null in the commission column sal+comm might equal null. So use min(isnull(sal,0) + isnull(comm,0))
And finally you don't actually need the dept table as you are querying on a value that is already in the emp table so
select min(isnull(sal,0)+isnull(comm,0)) as "sal+comm"
from emp
where deptno=30
In your query, you're select from two tables, but you haven't specified a criterion to join the two tables. This result in a cross join, that means you have a cartesian product of First table tuples with second tables tuples.
You can try adding a clause where you say "and emp.deptno = dept.deptno"

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 :)

Query Multiple Columns Within a Table

I have a table of employees that are formatted as follows:
EMPLOYEE (FNAME,MINIT,LNAME,SSN(PK),BDATE,SUPERSSN(NULLABLE))
I need to query every employee and retrieve the following information:
FNAME(employee),LNAME(employee),SUPERSSN,(super)FNAME,(super)LNAME
UPDATED
After running this query:
SELECT A.FNAME,A.LNAME,A.SUPERSSN,B.FNAME,B.LNAME
FROM EMPLOYEE
A LEFT JOIN EMPLOYEE B
ON A.SUPERSSN = B.SSN;
The results were close, but when the superssn was null (CEO/Boss) it caused the remaining rows to populate as null also and did not populate with the actual supervisors ssn. I'm trying to use an IF statement to fix the problem with having a SuperSSN that is null, but I'm receiving the error: ORA-00905: missing keyword.
Below is the query that I ran that generated the error.
SELECT A.FNAME,A.LNAME,A.SUPERSSN,B.FNAME,B.LNAME
FROM EMPLOYEE A LEFT IF A.SUPERSSN <> 'NULL'
JOIN EMPLOYEE B ON A.SUPERSSN = B.SSN;
Select A.FName,
A.LNAme,
A.SuperSSN,
B.FName,
B.LName
from Employee A
Left Join Employee B
On A.SuperSSN = B.SSN

Specified Departments? - 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.

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