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;
Related
I have a query like below. I want to create a table and convert null values to 0. But my query failed. How can I write it correctly ?
CREATE TABLE TABLE_2
AS
SELECT *
FROM TABLE_1 1 = 2;
INSERT INTO TABLE_2 (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER,
HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT, 0), MANAGER_ID, DEPARTMENT_ID)
SELECT *
FROM TABLE_1;
Both your statements contain syntax errors.
The CREATE TABLE ... SELECT AS statement requires a valid query: your query is missing the WHERE keyword.
CREATE TABLE TABLE_2
AS
SELECT *
FROM TABLE_1 where 1 = 2;
Insert statements can have two projections. The optional project is the list of columns in the target table. Your list includes NVL(COMMISSION_PCT, 0) which is not a valid column name, but a declaration of the data manipulation you wish to apply to the column from the source table. Consequently you need to include it in the projection of the SELECT statement.
INSERT INTO TABLE_2
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER,
HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT, 0), MANAGER_ID, DEPARTMENT_ID
FROM TABLE_1;
I have published a working demo on db<>fiddle.
INSERT INTO TABLE_2(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY,COMMISSION_PCT,MANAGER_ID, DEPARTMENT_ID)
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT,0), MANAGER_ID, DEPARTMENT_ID
FROM TABLE_1
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
I want to run the query
select first_name, last_name, distinct salary from employees
But it throws an error. While if I use this select distinct salary, first_name, last_name from employees it runs.
I want o/p in the form of first column should be first_name then last_name then distinct salary.
try this!
SELECT Salary, First_Name, Last_Name
FROM table_name
GROUP BY Salary
the above should return a list of first_name and last_name of people who share the same salary.
If your data set contains duplicate rows you may want to do this to get rid of duplicate rows:
WITH salaries
AS ( SELECT DISTINCT Salary,
First_Name,
Last_Name
FROM table_name )
SELECT Salary,
First_Name,
Last_Name
FROM salaries
GROUP BY Salary;
Here's the trick, you should insert first to temp table where you distinct the salary, then after that , you can now select the data in your temp table with your desired arrangement of columns.
select distinct salary, first_name, last_name * into #temp from employees
Then after the distinct, you can now do what you want in the second query without the distinct.
select first_name, last_name, salary from #temp
I wonder if it is possible to use OR operation between columns in a SELECT query?
select
first_name,
last_name
from
employee
where
(first_name OR last_name)='&enter_search_string';
Just my preference:
SELECT first_name, last_name
FROM employee
WHERE '&enter_search_string' IN (first_name, last_name)
You neeed to use it like this:
select
first_name,
last_name
from
employee
where first_name='first_name' OR last_name='last_name';