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

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"

Related

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

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 ;

INNER JOIN and Count POSTGRESQL

I am learning postgresql and Inner join I have following table.
Employee
Id Name DepartmentId
1 John S. 1
2 Smith P. 1
3 Anil K. 2
Department
Department
Id Name
1 HR
2 Admin
I want to query to return the Department Name and numbers of employee in each department.
SELECT Department.name , COUNT(Employee.id) FROM Department INNER JOIN Employee ON Department.Id = Employee.DepartmentId Group BY Employee.department_id;
I dont know what I did wrong as I am new to database Query.
When involving all rows or major parts of the "many" table, it's typically faster to aggregate first and join later. Certainly the case here, since we are after counts for "each department", and there is no WHERE clause at all.
SELECT d.name, COALESCE(e.ct, 0) AS nr_employees
FROM department d
LEFT JOIN (
SELECT department_id AS id, count(*) AS ct
FROM employee
GROUP BY department_id
) e USING (id);
Also made it a LEFT [OUTER] JOIN, to keep departments without any employees in the result. And COALESCE to report 0 employees instead of NULL in that case.
Related, with more explanation:
Query with LEFT JOIN not returning rows for count of 0
Your original query would work too, after fixing the GROUP BY clause:
SELECT department.name, COUNT(employee.id)
FROM department
INNER JOIN employee ON department.id = employee.department_id
Group BY department.id; --!
That's assuming department.id is the PRIMARY KEY of the table, in which case it covers all columns of that table, including department.name. And you may want LEFT JOIN like above.
Aside: Consider legal, lower-case names exclusively in Postgres. See:
Are PostgreSQL column names case-sensitive?

finding a value in multi-values column

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

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.

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