finding a value in multi-values column - sql

I have 2 tables as following:
Tam trying to get the department name of each employee (DepName column in table Emp table) from Dep table:
I have written this query:
update Emp
set DepName= (
select DepName
from Dep
where array_to_string(EmpID, ',') like EmpID
);
It did not update the table Emp with the requested information, although I haven't got any error. Any help?

You can do:
update emp
set dept = d.depname
from dep
where emp.empid = any (dep.empid);
Having pointed that out, you should not do this. Instead, I would suggest that you have a proper link to the department table and use join to bring in the department name.

you have to convert id int to character varying array data type and then use contains operator with table dept and update as usual
UPDATE emp t1
SET dept = dname
from dept t2
where t2.eid #> concat(concat('{',(t1.id::text)),'}') ::varchar[]
https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e5c24b26b3479b68adf0b17c2050f715

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 result in following scenario

I have table
EMP(id int primary key, name varchar2(15), mgrID int).
Now this table contain all employees(including worker and manager) in company. mgrID column contain id of employee to whom they are reporting.
I want to list the name of worker who is not manager along with their name of manager.
What to do for such query.
I tried nested select query as follows:
select name, (select name from EMP where mgerID is NULL)
as Manager from EMP;
Will this query give proper result?
You could use a self-join:
SELECT e.name AS name, m.name AS manager_name
FROM emp e
JOIN emp m ON e.mgrid = m.id
Your query should fail because you sub-query is uncorrelated and will return multiple results if you have multiple top-level managers.
select name
, (select name from EMP b where b.ID = a.mgerID ) as Manager
from EMP a;
I think the self-join is the more canonical solution, but you should understand the correlated subquery as well as it has many application.

does anyone know how to list the empid and the name of all supervisors if supervisors are in employee table too?

I have a table that contains empid, name, salary, hiredate, position and supervisor (which includes empid, not the name). How do I list the empid and name of all supervisors?
The output has to have to columns supervisor (and a list of their empid) and their names. This is the create statement used to create the Employee table:
/* Create table Employee */
IF OBJECT_ID('Employee', 'U') IS NOT NULL
DROP TABLE Employee
GO
CREATE TABLE Employee (
emp_id NCHAR(5),
name NVARCHAR(20),
position NVARCHAR(20),
hire_date DATETIME,
salary MONEY,
bcode NCHAR(3),
supervisor NCHAR(5)
)
I have tried a variety of statements using having statement and count but they don't seem to work.
select emp_id, name from employee where position='manager';
I tried this but it doesn't work. Anyone smart that knows how to do it?
You will have to join the table back on itself:
select a.name, a.position, a.hiredate, a.salary, a.supervisorid,
isnull(b.name, '') as SupervisorName
from EmployeeTable a
left join EmployeeTable b
on a.SupservisorID=b.ID
The left join will make sure that the employees who do not have supervisors are returned, and isnull(b.name, '<NONE>') can be used if you would like to have something other than NULL as a value in those cases.
SELECT e.empid ,ISNULL(b.name, 'No supervisor') SupervisorName
FROM employee e LEFT JOIN employee b
ON e.supervisorid = b.empid
Inner join will leave out the people who do not have a supervisor , Use left join to get all the employees
If you want supervisors only, you just need to select rows whose emp_id values are found in the supervisor column:
SELECT
SupervisorID = emp_id,
SupervisorName = name
FROM dbo.Employee
WHERE emp_id IN (SELECT supervisor FROM dbo.Employee)
;

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

SQL update in single query

I have 2 tables: employee and employeedetails.
employee looks like this:
id name
----------------
1 Suresh
2 Ram
3 Ravi
employeedetails looks like this:
empid salary
----------------
1 10000
2 5000
3 40000
I want to update salary field of a particular person. For this i know the employee name of the first table. Based on this, I need to write a single query that will update the salary field with single query. How can I do this?
For example, if I have to update Ravi's salary details, how can I do it in a single query?
update employeedetails
inner join employee on employeedetails.empid = employee.id
set salary = 1000
where employee.name = 'Ram'
Try this :
update employeedetails set salary=1000000 where empid in (select id from employee where name='suresh');
MERGE INTO employeedetails
USING employee
ON employeedetails.empid = employee.id
AND employee.name = 'Ravi'
WHEN MATCHED THEN
UPDATE
SET salary = 10000;
UPDATE employeedetails det
SET salary=100
WHERE EXISTS ( SELECT NULL
FROM employee emp
WHERE name='Ravi'
AND det.empid = emp.id
)
;