how to display null value attributes? - sql

This is the question:
Create a query to list all the employees who joined this organization before any clerks
were hired and who earn more than any manager.
This is what I have so far:
select ename
from emp
where hiredate<any(select hiredate from emp
where job='CLERK')
and job!='CLERK'
and sal>any(select sal from emp
where empno=super);
-But one of the employees don't have a supervisor (which is null) so it doesn't show any of the employees.

Something like this?
SELECT ename
FROM emp
WHERE hiredate < ANY (
SELECT hiredate
FROM emp
WHERE job = 'CLERK'
)
AND job <> 'CLERK'
AND sal > ANY (
SELECT sal
FROM emp
WHERE empno IN (
SELECT super
FROM emp
WHERE super IS NOT NULL
)
);
Is it a homework? If yes, should be labelled as such.

Related

Why are these 2 queries giving different outputs?

Query no 1:-
SELECT COUNT(ENAME)
FROM EMP
WHERE
JOB IN 'MANAGER'
OR JOB IN 'ANALYST'
AND SAL IN (
SELECT SAL + NVL (COMM,0)
FROM EMP
WHERE SAL LIKE '%0')
GROUP BY JOB;
The Query 1 gives me the following output:-
COUNT(ENAME)
------------
3
2
Query no 2:-
SELECT COUNT(ENAME)
FROM EMP
WHERE
JOB = ANY (
SELECT JOB
FROM EMP
WHERE JOB IN ('MANAGER', 'ANALYST')
)
AND SAL IN (
SELECT SAL + NVL (COMM,0)
FROM EMP
WHERE SAL LIKE '%0'
)
GROUP BY JOB;
The Query 2 gives me the following output:-
COUNT(ENAME)
------------
2
2
Summary: The AND operator has higher precedence than the OR operator. To fix it, use brackets around the OR expression.
AND has higher precedence than OR so your first query is the equivalent of (with added brackets and indentation to emphasise the precedence):
SELECT COUNT(ENAME)
FROM EMP
WHERE JOB IN 'MANAGER'
OR ( JOB IN 'ANALYST'
AND SAL IN (
SELECT SAL + NVL (COMM,0)
FROM EMP
WHERE SAL LIKE '%0'
)
)
GROUP BY JOB;
So you are finding either: managers with any salary; or analysts whose salary equals the salary plus commission of any other employee.
Your second query is the equivalent of:
SELECT COUNT(ENAME)
FROM EMP
WHERE ( JOB IN 'MANAGER'
OR JOB IN 'ANALYST'
)
AND SAL IN (
SELECT SAL + NVL (COMM,0)
FROM EMP
WHERE SAL LIKE '%0'
)
GROUP BY JOB;
Which finds: any employee who is either a manager or an analyst and whose salary equals the salary plus commission of any other employee.
You could rewrite your first query to:
SELECT COUNT(ENAME)
FROM EMP
WHERE JOB IN ('MANAGER', 'ANALYST')
AND SAL IN (
SELECT SAL + NVL (COMM,0)
FROM EMP
WHERE SAL LIKE '%0'
)
GROUP BY JOB;
JOB IN 'MANAGER' OR ...
means all managers OR the next predicate
please put parentheses to achieve the expected result
JOB IN ('MANAGER', 'ANALYST')

SQL Server 2014 Management Studio update query

I have a table Employee with those columns
empid, empname, job, hiredate, sal, comm, deptno
I want to verify if this is correct:
Number of employees in each department
How many people there are in each type of job in each department
Display the department and number of employees in department with fewer than 6 employees
Find the employee name and its salary who is earning maximum salary in dept 20
Here is what I have tried:
Query #1:
select DEPTNO, count(*) AS NO_OF_PERSONS
from EMP
group by DEPTNO;
Query #2:
select job, count(*) AS NO_OF_PERSONS
from EMP
group by job;
Query #3:
update EMPLOYEE
set sal = sal + 1000
where com > 2500;
And I am unable to do the 4th part.
I hope that below queries will help you.
no of employees in each dept?
SELECT DEPTNO,
count(*) AS NO_OF_PERSONS
FROM EMP
GROUP BY DEPTNO;
how many people are there in each type of job in each department?
SELECT job,
deptno,
count(*) AS NO_OF_PERSONS
FROM EMP
GROUP BY job,
deptno;
display the department and no of employees in department with fewer than 6 employee.
SELECT deptno,
count(*)
FROM emp
GROUP BY deptno
HAVING count(*) < 6;
find the employee name and its salary who is earning maximum salary in dept 20.
SELECT Max(salary_amount),
empid
FROM EMP
WHERE deptno = 20
GROUP BY empid;
You can start learning Basic SQL here
select e1.name, e1.sal
from EMP e1
where e1.DEPTNO = 20
and not exists(select *
from EMP e2
where e2.DEPTNO = 20
and e2.sal > e1.sal);

How to list the employees who are senior to most recently hired employee working under a particular manager in the below schema?

I have a schema of emp table defined as emp ( empno ,ename ,job ,mgr ,hiredate) and I have to evaluate the query for the problem statement :
List the employees who are senior to most recently hired employee working under king.
I wrote the query as :
select hiredate from emp where hiredate < ANY
( select e.hiredate from emp e where e.mgr= (select e.empno from emp e
where e.ename='KING') order by hiredate desc )
This query is giving syntax error ,please help me out .
Use max function instead-
select hiredate
from emp
where hiredate < ( select max(hiredate)
from emp
where mgr= (select e.empno
from emp e
where e.ename='KING'));
To get employees working under King who joined in the last 7 days, you can try:
SELECT e.*
FROM emp e
WHERE e.mgr = (SELECT empno FROM emp WHERE ename='KING') AND
e.hiredate > DATEADD(day, -7, GETDATE())
Note that the subquery to find King's employee number is not correlated. So this is the friendly sort of subquery.
Use a max to find the greatest hire date, and a join instead of the subquery.
select * from emp where hiredate <
(select max(e.hiredate)
from emp e
join emp mg on e.mgr = mg.empno
where mg.ename = 'KING')
I guess you have this error message
The ORDER BY clause is invalid in views, inline functions, derived
tables, subqueries, and common table expressions, unless TOP or FOR
XML is also specified.
So you should add a TOP 1 to have it work (but well, is this really more readable ?)
select hiredate from emp where hiredate < ANY
(select TOP 1 e.hiredate from emp e where e.mgr= (select e.empno from emp e
where e.ename='KING') order by hiredate desc )
SELECT *
FROM emp
WHERE hiredate < ANY (SELECT hiredate
FROM emp
WHERE ename = 'MILLER');

SQL select rows without duplicates

Practicing some SQL, we have to get the name of the employees whose salary is the greatest of his department. But if in any department there were more than one employer with the greatest salary, we would not have to consider that department.
We got the first part but not the second one (because there are two employees with the same greatest salary (3,000) in the same department (20)).
This is what we did:
SQL> SELECT ename, sal, deptno FROM emp a
WHERE sal >= ALL (SELECT sal FROM emp WHERE deptno=a.deptno)
ORDER BY sal;
And this is what we got:
ENAME SAL DEPTNO
---------- ------- ------
BLAKE 2,850 30
FORD 3,000 20
SCOTT 3,000 20
KING 5,000 10
4 filas seleccionadas.
Any help will be useful, thank you!
SELECT ename, sal, deptno
FROM emp a
WHERE not exists (
SELECT *
FROM emp
WHERE deptno=a.deptno
and sal >= a.sal
and ename != a.ename)
ORDER BY sal;
with cte as
( SELECT ename, sal, deptno
, row_number() over (partition by deptno order by sal desc) as rn
FROM emp
)
select ename, sal, deptno from cte where rn = 1
except
select ename, sal, deptno from cte where rn = 2
order by sal
if this does not work in oracle - it used to be also tagged mssql
You can have what you need with some analytic functions:
select ename,
deptno,
sal
from (
select ename,
deptno,
sal,
row_number() over(partition by deptno order by sal desc) AS num,
count(1) over(partition by deptno, sal) AS count
from emp
)
where num = 1
and count = 1
The inner query orders by salary and counts the number of employees with the same salary in the same department; the outer one simply filters for employees with the maximum salary, where only one employee has that salary in the department.
With a different approach, simply modifying your query, you can try:
SELECT ename, sal, deptno FROM emp a
WHERE sal >= ALL (SELECT sal FROM emp WHERE deptno=a.deptno)
and (select count(1) from emp b where a.deptno = b.deptno and a.sal = b.sal) = 1
The first way gives better performance, with a single table scan, while the second one needs a nested query, thus being less efficient
try to use GROUP BY column_name`, it will show the record without duplicate.

sql subqueries and group by

I'm having an assignment for my sql class which i can't seem to figure out. This is the description of the select wanted:
Show all employees of which the salary is higher than the average of the colleagues in their department, only for the departments with at least 4 employees.
I've been able to find parts of the query like
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno);
to get the names of the employees which earn more than the avg.
or
select count(deptno)
from emp
group by
deptno having count(deptno) > 4;
to getthe number of employees in each department.
But somehow it doesn't work linking them together. Maybe someone can help me shine a light on this on.
Just put your second query in with an AND clause:
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno)
and deptno in (select deptno
from emp
group by
deptno having count(deptno) > 4);
You can use Having Clause in Conjunction with Group By
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno)
having count(*)>4;
select ename
from (
select deptno, count(deptno)
from emp
group by deptno
having count(deptno) > 4) valid_depts join emp ON emp.deptno=valid_depts.deptno
where sal > any (select avg(sal)
from emp
group deptno);