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';
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
I have a query using an IN clause. How would I execute to choose all values if there is no input for the bind variable?
SELECT first_name, last_name, employee_number
FROM employee
WHERE employee_number IN (:p_employee_num)
You may add logic to the WHERE clause which also allows a null bind variable:
SELECT first_name, last_name, employee_number
FROM employee
WHERE employee_number IN (:p_employee_num) OR :p_employee_num IS NULL;
For a NULL bind variable, the IN clause above would never be true, but the IS NULL check would always be true, thereby returning every record.
One method would be:
WITH e as (
SELECT first_name, last_name, employee_number
FROM employee
WHERE employee_number IN (:p_employee_num)
)
SELECT first_name, last_name, employee_number
FROM e
UNION ALL
SELECT first_name, last_name, employee_number
FROM employee
WHERE NOT EXISTS (SELECT 1 FROM e);
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'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;
I have to concatenate First_Name and Last_Name but when I do that that show incorrect syntax near as.
My code:
select COUNT(*) as Leaves,
Employee_Admission.Emp_Id,
First_Name,
Last_Name as Name
from LeaveAssign
join Employee_Admission on LeaveAssign.Emp_Id= Employee_Admission.Emp_Id
WHERE D_Id='3'
group by Employee_Admission.Emp_Id,
First_Name,
Last_Name as Name
union
select 0 as 'Leaves',
Employee_Admission.Emp_Id,
First_Name,
Last_Name as Name
from Employee_Admission
where Emp_ID not in (Select Emp_ID from LeaveAssign) and D_Id='3'
group by does not contain as Keyword
Just Try this
select COUNT(*) as Leaves,
Employee_Admission.Emp_Id,
First_Name,
Last_Name as Name
from LeaveAssign
join Employee_Admission on LeaveAssign.Emp_Id= Employee_Admission.Emp_Id
WHERE D_Id='3'
group by Employee_Admission.Emp_Id,
First_Name,
Last_Name
union
select 0 as 'Leaves',
Employee_Admission.Emp_Id,
First_Name,
Last_Name as Name
from Employee_Admission
where Emp_ID not in
(Select Emp_ID
from LeaveAssign)
and D_Id='3'
you cannot give alias in group by statement, hence it is giving you error. Remove as from the group by statement and it wont give you error. And if you want to concatenate Firstname and Lastname, do it like
First_Name + ' ' + Last_Name as Name
Like this:
select COUNT(*) as Leaves,Employee_Admission.Emp_Id,First_Name + ' ' + Last_Name as Name
from LeaveAssign join Employee_Admission on LeaveAssign.Emp_Id= Employee_Admission.Emp_Id
WHERE D_Id='3'