I want to be able to produce results that
If there is a commission it produces it
If there is not a commission it produces the manager_id
If there is no manager_id or commission it produces -1
I'm thinking I might have to do NLV2(NVL2(NVL2))) but I am unsure. This is the code I produced I'm sureI'm wrong.
SELECT first,
last_name,
CASE commission_pct
WHEN commission_pct IS NOT NULL THEN commision_pct
WHEN commission_pct IS NULL THEN manager_id
WHEN commission_pct AND manager_id IS NULL THEN -1
END AS "Which Function???"
FROM employees
You are looking for COALESCE which returns the first non-null value:
select
first_name,
last_name,
coalesce(commission_pct, manager_id, -1)
from employees;
Related
SELECT first_name, last_name, manager_id
CASE manager_id
WHEN manager_id IS null THEN "pip"
End manager_id
FROM assgnssql.employees;
I am trying to select list of employees, but i know some employees do not have manager_id, for these employees without manager_id (null) i want the result to display "pip" while for the rest it displays original info.
The code you want is probably:
SELECT first_name, last_name, manager_id
(CASE WHEN manager_id IS null THEN 'pip' ELSE manager_id
END) as manager_id
FROM assgnssql.employees;
Or more simply:
SELECT first_name, last_name, manager_id
COALESCE(manager_id, 'pip') as manager_id
FROM assgnssql.employees;
The two significant issues are:
Your CASE syntax is messed up. Either you use comparisons or you have CASE <value>, but not both.
Strings are delimited by single quotes.
I need to display Employee last_name and their commission amount from employees table in Oracle SQL, but the condition is if it encounter NULL value I need to print "No Commission".
For the first part I wrote:
select last_name, commission_pct from employees;
But I am unable to get how to replace NULL values with "No Commission".
You can use case expression:
select last_name
, case when commision_pct is null then 'No Commission' else commision_pct end
from employees;
or coalesce:
select last_name
, coalesce(commision_pct, 'No Commission')
from employees;
or nvl:
select last_name
, nvl(commision_pct, 'No Commission')
from employees;
P.S. In case commision_pct's datatype is not varchar you should also use cast or to_char.
For Oracle
select last_name, nvl(commission_pct,'No Commission')
from employees;
For SQL
select last_name, isnull(commission_pct,"No Commission") as commission_pct
from employees;
Another alternative, quite simple and precise:
nvl(to_char(commision_pct), 'No Commission')
Since, commision_pct is NUMBER data type, to_char will explicitly convert it into string.
It is as simple as you can see, Isnull() Used to Replace NULL values to the default value we pass there, so what i did here is If "commission_pct" having NULL value then it'll replace that with "No Commission" text, which i have passed in ISNULL() as 2nd parameter.
select last_name,
ISNULL(commission_pct,'No Commission') AS commission_pct
from employees;
select Last_Name,
decode(nvl(salarycommission_pct,'0'),0,'No Commission',salarycommission_pct) as COMM
from employees;
how if the condition like this:
Select job_id, job_title, employee_id
from jobs join job_history using (job_id);
and show all of the employee_id who has been working in there, if is it not, replacing into varchar
Can anyone explain why this query:
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name LIKE '%u%'
)
AND salary > (SELECT AVG(salary)
FROM employees);
returns way less rows than this nested one:
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name LIKE '%u%'
AND salary > (SELECT AVG(salary)
FROM employees);
)
The first returns all employees who meet the following conditions:
The employee is in a department has a "u" employee.
The employee has a salary larger than the average.
The second returns all employees who meet these conditions:
The employee is in a department that has a "u" employee who has a salary larger than the average.
The two are very different conditions. I wouldn't expect them to return the same result set.
Also, whenever you have more than one table in a query, you should use table aliases that are abbreviations of the table name and you should qualify all column names.
I need to display Employee last_name and their commission amount from employees table in Oracle SQL, but the condition is if it encounter NULL value I need to print "No Commission".
For the first part I wrote:
select last_name, commission_pct from employees;
But I am unable to get how to replace NULL values with "No Commission".
You can use case expression:
select last_name
, case when commision_pct is null then 'No Commission' else commision_pct end
from employees;
or coalesce:
select last_name
, coalesce(commision_pct, 'No Commission')
from employees;
or nvl:
select last_name
, nvl(commision_pct, 'No Commission')
from employees;
P.S. In case commision_pct's datatype is not varchar you should also use cast or to_char.
For Oracle
select last_name, nvl(commission_pct,'No Commission')
from employees;
For SQL
select last_name, isnull(commission_pct,"No Commission") as commission_pct
from employees;
Another alternative, quite simple and precise:
nvl(to_char(commision_pct), 'No Commission')
Since, commision_pct is NUMBER data type, to_char will explicitly convert it into string.
It is as simple as you can see, Isnull() Used to Replace NULL values to the default value we pass there, so what i did here is If "commission_pct" having NULL value then it'll replace that with "No Commission" text, which i have passed in ISNULL() as 2nd parameter.
select last_name,
ISNULL(commission_pct,'No Commission') AS commission_pct
from employees;
select Last_Name,
decode(nvl(salarycommission_pct,'0'),0,'No Commission',salarycommission_pct) as COMM
from employees;
how if the condition like this:
Select job_id, job_title, employee_id
from jobs join job_history using (job_id);
and show all of the employee_id who has been working in there, if is it not, replacing into varchar
I am using SQL Developer and I have this table structure:
Name Null Type
-------------- -------- ------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
DEPARTMENT_ID NUMBER(4)
SALARY NUMBER(8,2)
What I want to do is print out the department_id which has AT LEAST 2 employees with salaries greater than 10000.
I though this would be the query
select department_id
from employees
having count(select * from employees where salary > 10000) > 2
group by department_id;
but, from what I found out, you can't put a SELECT statement inside COUNT so now I an stuck and I don't know how else am I supposed to do this query. Any suggestion is welcome.
UPDATE: Please note that I want AT LEAST 2 employees to have salary > 10000, not all of them
SELECT Department_Id,
COUNT(*)
FROM Employee
WHERE Salary > 10000
GROUP BY Department_Id
HAVING COUNT(*) > 1
SQL Fiddle example.
SELECT department_id FROM employees
WHERE salary > 10000 GROUP BY department_id HAVING COUNT(*) >= 2;