What is wrong with my Postgres statement? - sql

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.

Related

How do i change the result of an ORACLE select query for specific type of data?

I have a table named EMPLOYEES with columns EMPLOYEE_ID, NAME and DEPARTMENT_ID. I have to list all employees, changing the output of the attribute NAME to "---" if the department id is 10.
How can I do that? Thank you!
select
employee_id,
case when department_id = 10 then '---' else name end as name,
department_id
from employees;
Another solution using union all
select
employee_id, name, department_id
from employees WHERE department_id != 10
UNION all
select
employee_id, '---' name, department_id
from employees WHERE department_id = 10

Oracle SQL sub query

I have a practice that I should find the employees who earn more than average salary and works in the departments with employees whose last name contains the letter u
the select statement I have used was
SELECT employee_id,
last_name,
salary
FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees )
AND department_id IN(SELECT department_id
FROM employees
WHERE LOWER(last_name) LIKE '%u%')
Could anyone check this statement is suitable or not ?
thank you
That looks fine to me, assuming you mean the average salary across all departments in the database, and all employees (active or not) across all of time.
I would think you might be more interested in all active employees in this current financial year, for example.
You haven't provided the schema, so be careful to check for conditions like:
inactive departments
inactive / terminated employees
period you are interested in for comparing the salary
Your queries looks like it will work. You can rewrite it to remove all the sub-queries (that will require additional table/index scans) and just use analytic queries:
SELECT employee_id,
last_name,
salary
FROM (
SELECT employee_id,
last_name,
salary,
AVG( salary ) OVER () AS avg_salary,
COUNT( CASE WHEN LOWER( last_name ) LIKE '%u%' THEN 1 END )
OVER ( PARTITION BY department_id ) AS num_last_name_with_u
FROM employees
)
WHERE salary > avg_salary
AND num_last_name_with_u > 0;
db<>fiddle
My first Question are you getting the expected result ?
Let me break down your Query
SELECT department_id FROM employees WHERE LOWER(last_name)
Here you are selecting the department so it retrieve the department id, what is the need of selecting department Id when all you need employee_id with last name contains u so change it to employee_id instead of department_id
select avg(salary) over (partition by department_id order by employee_id)
So using partition by you must get the avg salary per department
SELECT employee_id,last_name,salary
FROM
employees
WHERE salary>(SELECT AVG(salary) OVER (PARTITION BY department_id)
FROM
employees )
AND employee_id IN
( SELECT employee_id
FROM
employees
WHERE LOWER(last_name) LIKE '%u%')
Let me know if you have any issues running it, any corrections to Query is appreciated

SQL for Mixed Case filter

I was just wondering how you would search for any mix of case. For instance, I want to find all employees with the last name 'davies', but I want to be able to find any mix of case such as 'DavIes' or 'DAVies'. This is what I've tried.
SELECT LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID = (SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = '[Dd][Aa][Vv][Ii][Ee][Ss]');
I would use UPPER or LOWER function and IN
SELECT LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (
SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE UPPER(LAST_NAME) = 'DAVIES'
);
Unfortunately it invalidates the index use.
Using UPPER
SELECT *
FROM EMPLOYEES
WHERE UPPER(LAST_NAME) = 'DAVIES'

How do I correct this script

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;

select statement oracle

I'm learning oracle sql from oracle sql fundamentals book, and i found this quiz, the answer as Oracle says 2,3 but they don't work on the sql developers
I know that they have to be like this
SELECT first_name, last_name, job_id, salary*12 "yearly_sal" FROM employees;
SELECT first_name, last_name, job_id, salary AS "yearly_sal" FROM employees;
but Oracle gives the answer as shown in the image below...
None of those answers are correct. Embedded spaces are only valid as column aliases if the alias is delimited by ". Also, + cannot be used to concatenate (what I assume to be) strings - instead you can use ||. They could be corrected as:
1.
SELECT first_name, last_name, job_id, salary*12 AS "Yearly Sal" FROM employees;
2.
SELECT first_name, last_name, job_id, salary*12 "yearly sal" FROM employees;
3.
SELECT first_name, last_name, job_id, salary AS "yearly sal" FROM employees;
4.
SELECT first_name || last_name AS name, job_Id, salary*12 "yearly sal" FROM employees;