What does where do in the below query? - sql

SELECT
COUNT(emp.empNo)
FROM
Employee emp
WHERE
NOT EXISTS (SELECT dept.empNo
FROM department dept
WHERE emp.empNo = dept.empNo);
What does the where condition(where emp.empNo = dept.empNo) signify in the above query? I get different results with and without the where condition. I'm new to Oracle. Can any one help me to understand?

your query displaying count of employees which are not present in emp table but present in dept table.
suppose we have two tables emp and dept :
emp dept
1 1
2 2
3 3
4 4
5 5
6
7
from the given table we have emp 1 to 5 in both of the tables but in dept table having 2 employees (6,7)which are not present in emp table and your query is displaying count for those emp i.e 2

The query means that you're looking for only those employees, for which it does not exist a department with the same empNo as the employee's empNo.
I guess this is a query to find those employees which are not managers of any department (if we assume that the department's empNo is the empNo of the department's manager).
Still, it would be better if you provide the schema of the employee and the department tables.

The query is basically looking for the number of employees who don't belong to a department.
NOT EXISTS means that the query enclosed returns no rows. So, for any employee where no matching rows are found in the Department table they are counted.

Same as saying
SELECT
COUNT(emp.empNo)
FROM
Employee emp
WHERE
emp.EmpNo NOT IN ( SELECT
empNo
FROM
department)

Related

Creating dynamic query which has also has a sub-query

I use two queries in SQL Server, which I would like to combine and create a single query. Tried few options but of not much success.
Query 1
select emp_id, name, age
from employee
where age > 50
Query 2
select dept_id, dept_name
from department
where emp_id = 'COMPANY.ID' + emp_id
The issue in combining the two queries is, though query 1 can return multiple rows, I can't use a subquery to directly use emp_id from query 1 in query 2 since the emp_id in query 2 has a prefix of 'COMPANY.ID.'+emp_id. Any suggestions?
COMPANY.ID is a constant that gets prefixed to emp_id before saving it in department table.
Example employee table
emp_id name age
-----------------------------
123 John 45
345 Susan 34
789 Pat 66
Example department table
emp_id dept_id dept_name
-----------------------------------------------------------------------
COMPANY.ID.123 123 Accounting
COMPANY.ID.345 123 Accounting
Hope these examples help understand my dataset
I understand that you are trying to pull out the departments that have at least one employee older than 50.
One solution would be to use an EXISTS condition with a correlated subquery:
SELECT dept_id, dept_name
FROM department d
WHERE EXISTS (
SELECT 1
FROM employee e
WHERE
CONCAT('COMPANY.ID.', e.emp_id) = d.emp_id
AND e.age > 50
)
Demo on DB Fiddle
Is this what you want as
Query 2 is totally wrong and would never match instead would have used like %CompanyId%
select distinct dept_id, dept_name
from
department where empid IN (
select emp_id from
employee where age > 50 and empid
like '%CompanyId%' )
You can combine the two tables using join
SELECT d.dept_id, d.dept_name
FROM department d
INNER JOIN employee e
ON CONCAT('COMPANY.ID.', e.emp_id) = d.emp_id
WHERE e.age > 50

SQL IF two IDs are equal then select

I am trying to get correct result but I can not find right way to get.
I have this table:
I would like to get result like this one
This is my code:
SELECT employee_ID, first_name, manager_id ,
(SELECT first_name from employees where MANAGER_ID= employee_id)
from employees
I got only first 3 columns and fourth one is empty because its wrong select
Do self join to the same table
select emp1.employee_ID, emp1.first_name, emp1.manager_id,emp2.first_name as manager_name from employees emp1
left join employees emp2
on emp1.manager_id=emp2.employee_id;
Sorry my bad explanation, I have 5 columns, first one in not in the picture because its only ID of row, my first row is Employee id, second one is first_name, third one is manager id and fourth one should be first name of manager. Manager name is that when manager id is equal to employee id, so e.g. employee id = 206, first name is William, manager id is 205. and name should be Shelley because Shelley's employee ID is 205

Count and group by not working as expected

select count(*),manager_id
from departments
group by manager_id;
this is my idea of how to, but it dosent gives me the amount of employees for each manager
I suspect you are selecting from the wrong table, the logic seems correct but the fact that you are selecting from a table call departments is a little suspicious.
Do you have an employee tables? Does it contains a manager_id column? If so:
select count(*),manager_id
from employees
group by manager_id;
If employee tables has only department_id column then :
SELECT d.manager_id,count(*)
FROM employees e
INNER JOIN departments d
ON(e.department_id = d.id)
Using the sample table from the HR schema
select MANAGER_ID, count(*), count(distinct EMPLOYEE_ID)
from HR.EMPLOYEES
group by MANAGER_ID
order by 1 nulls first;
gives
MANAGER_ID COUNT(*) COUNT(DISTINCTEMPLOYEE_ID)
---------- ---------- --------------------------
1 1
100 14 14
101 5 5
102 1 1
Note the first row, with manager IS NULL - i.e. there is a one employee without a manager.
Not also that I use both count(*)and count(distinct EMPLOYEE_ID). This is not relevant for this table, where EMPLOYEE_ID is PK, but in general case the former returns the number of record the latter the number of employees (which can be lower).
I have faced a similar problem of yours.
Try adding inside the count the id that you are counting ie employeeid.
If it still doesnt work after that try adding the same id in the group by.

Updating rows based on another table

I created a table called SALE_REP with 4 columns, employee_id, name, salary and commission_pct.
I populated the SALE_REP table from an existing EMPLOYEES table. Now I want to update the salaries of some of the employees in the SALE_REP table using a subquery based on the values from another table;
update sale_rep
set salary = (select salary from employees
where job_id = 'ad_vp')
where employee_id = (select employee_id from employees
where commission_pct = 0.35);
But this gets;
SQL Error: ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
What am I doing wrong, and how can I perform the update successfully?
You have two subqueries, and if you data is based on the standard HR schema's employee table then both will return multiple rows.
The second is straightforward; the = needs to be in:
where employee_id in (select employee_id from employees
where commission_pct = 0.35)
... which matches three rows in the employee table in my schema. (Although as you said your sale_rep table is populated from employees and has the commission_pct column you don't really need to use a subquery here - the filter value exists on the table you're updating, so you could potentially just do where commission_pct = 0.35).
The first is more difficult and you'll need to define what you want to actually happen. You could, for example, pick the highest salary from those matching the specified job_id:
set salary = (select max(salary) from employees
where job_id = 'SA_REP')
I've picked 'SA_REP' as that looks like it might be a sales rep, and that has 30 matching rows with 20 different values in my schema. Based on your edit, there are two rows in my schema for AD_VP, but both have the same salary; in which case using max() might be OK, or you could use distinct:
set salary = (select distinct salary from employees
where job_id = 'AD_VP')
But that is very specific to the data you have at this point in time, so it isn't going to be a good general pattern.
Putting those together you could do:
select employee_id, salary from sale_rep where commission_pct = 0.35;
EMPLOYEE_ID SALARY
----------- ----------
156 10000
157 9500
158 9000
update sale_rep
set salary = (select max(salary) from employees
where job_id = 'AD_VP')
where employee_id in (select employee_id from employees
where commission_pct = 0.35);
3 rows updated.
select employee_id, salary from sale_rep where commission_pct = 0.35;
EMPLOYEE_ID SALARY
----------- ----------
156 17000
157 17000
158 17000
More generally there probably needs to be some connection between the row you're updating and the value you're choosing though, other than the filter on commission percentage, which doesn't look related to the job. That does seem to be what you want in this case though.

select column from table with only matched data from another table

I need to select 2 column from table with matched data from another table or cell be null ,
table 1 named "emp" contain emp_name ,emp_id
table 2 named "salary" contain emp_sal, emp_id
I need to create select query
have all emp_name, emp_id and emp_sal (for only the employees will take sale) or be Null
thanks for help now ((((update ))))
first thanks for help
i used
SELECT emp.emp_id,emp.emp_name,salary.emp_sal FROM emp LEFT JOIN salary ON emp.emp_id = salary.emp_id;
it work but with a lot of duplication and i need to make this query with day ...
i create another table named "day" i need query appear day i entered in this table
this table have only one column and i record ((day user entered and saved in "day.user_day"))
i need to link this three tables
together
and lets make it easy we will change salary to attendance ...
i need to query all names and id in date and apear all employee what ever thy have time or not
like when i search only in day 4/8/2014
name id time
john 1 04/08/2014 06:00
man 2 null
scsv 3 04/08/2014 07:00
You want a LEFT JOIN:
SELECT * FROM emp LEFT JOIN salary ON emp.emp_id = salary.emp_id;
This will return all employees along with their emp_sal, which will be NULL if it's not in the salary table.
If what I read is right, you want to get the employee name and salary, returning all employees regardless if they have an entry in salary. If that is correct, this should work:
SELECT
e.emp_name,
e.emp_id,
s.emp_sal
FROM
emp AS e
LEFT JOIN salary AS s ON e.emp_id = s.emp_id
If, however, you only wanted the employees with an entry in the salary table, change LEFT JOIN to be INNER JOIN.