I am having problems implementing code to perform a couple calculations and setting column aliases.
Situation description:
The management wants to increase salary of all employees by 10%. Write a query to display empid, current salary, increased salary and incremented amount.
EmpId and Salary being existing columns in the table EmpDetails.
My Oracle SQL attempt:
SELECT
EmpId,
Salary AS 'Current Salary',
Salary*1.10 AS 'New Salary',
Salary*1.10-Salary AS 'Incremented Amount'
FROM EmpDetails
The output from my attempt is nothing. The interpreter doesn't support error messages and has issues displaying seemly valid code when it is incorrect for the problem for seemly any reason. This has left me confused while learning Oracle SQL syntax so the problem is likely the syntax in my attempt.
Is there way valid way to do this as a verbose select query like I attempted or maybe a simpler way for someone newer to SQL that uses multiple Alter/Update/Select commands?
enclose the alias with double quote.
SELECT
EmpId,
Salary AS "Current Salary",
Salary*1.10 AS "New Salary",
Salary*1.10-Salary AS "Incremented Amount"
FROM EmpDetails
Related
I'm having much trouble with the Oracle Live SQL tutorials for the Oracle Apex Workspace. In particular, I'm stuck on Module 3: Inserting data because of the following:
The tutorial wants me to run the following code to add to the employees table:
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Sam Smith','Programmer',
5000,
(select deptno
from departments
where name = 'Development'));
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Mara Martin','Analyst',
6000,
(select deptno
from departments
where name = 'Finance'));
insert into EMPLOYEES
(name, job, salary, deptno)
values
('Yun Yates','Analyst',
5500,
(select deptno
from departments
where name = 'Development'));
However, when I attempt to run this code (which was copied and pasted from the tutorial into my Oracle Apex Workspace), I get the following error:
ORA-01427: single-row subquery returns more than one row ORA-06512: at "SYS.DBMS_SQL", line 1721
I checked my Objects aka tables to ensure that there were no tables with duplicate column names, however, I don't have any tables with any duplicates.
Can anyone help me with this? I'm quite new to both SQL and the Oracle Apex workspace.
This suggests that your departments has more than one row for certain names. You can determine which:
select name
from departments
group by name
having count(*) > 1;
There probably should not be duplicates. You probably accidentally loaded the data twice into the table, or something like that.
I'm having a HR table in hive database with different columns, two of them are department and number_of_projects. The query I need to find is "In which department total number of projects is greater than 10% of overall project"
I have written code as below:
SELECT department,
SUM(Number_Of_projects) as total_projects_dep
FROM Hr
GROUP BY department
HAVING SUM(Number_Of_projects) > (SELECT CAST(0.1*SUM(Number_Of_projects)AS INT) FROM hr);
hive is throwing following error:
FAILED: ParseException line 1:126 cannot recognize input near 'SELECT'
'CAST' '(' in expression specification
The same query I have executed in mysql, its working fine and giving correct result. Whereas hive is not accepting greater than symbol between queries.
Can some one guide me how to alter the above query to work in hive.
Using analytics functions:
SELECT department, total_projects_dep
FROM
(
SELECT department,
SUM(Number_Of_projects) over(partition by department) as total_projects_dep,
SUM(Number_Of_projects) over() as total_projects
FROM Hr
)s
WHERE total_projects_dep > CAST(0.1*total_projects AS INT)
GROUP BY department, total_projects_dep --this can be removed if there is only one record per department
;
i have this task:
Select department id, the longest time of working based on months and time of the person who was hired as the first one
I wrote sthg like this:
SELECT department_id, min(hire_date) as earliest_hire_date, sysdate-hire_date dni
FROM employees
it generates the following error:
*ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"*
i use oracle. Do you have any idea how to fix it?
You need to find a record that meets a certain criteria (which generally means you are going to need a WHERE clause). The criteria for the WHERE clause is the record with the min(hire_date). So:
SELECT department_id, hire_date, sysdate-hire_date
FROM employees
WHERE hire_date = (SELECT min(hire_date) FROM employees);
Or something along those lines. IF this returns more than one record, you could probably just toss a GROUP BY or DISTINCT in there. If that doesn't squash the multiples then you'll have to implement further logic to pick the winner (in the even that multiple departments share the same "earliest hiring date").
You could also do something similar to what you were doing (Aggregating at min(hire_date)) and then order by that field picking out the top most record.
As for the error you were facing in your attempt, when you use an Aggregate function like sum(), avg(), min() and similar, then fields that are not being aggregated must be in the GROUP BY clause, which was absent in your query.
You would fix your problem by using min() in the select:
select department_id, min(hire_date) as earliest_hire_date,
sysdate - min(hire_date) as dni
from employees
group by department_id;
I'm not sure if this does what you really need -- your question doesn't have sample data, desired results, or a clear explanation of what you want. On the other hand, it does fix the syntax error.
I will explain the problem I am stuck on. I have a table named empl02 which contains Lastname, salary, and position for all the employees. I am asked to display last,name,salary, position for all employees making more money than the highest paid member of a certain 'position', we will call this position server. I cannot just do something simple like...
SQL> select Lastname,salary,position FROM empl02
2 WHERE
3 SAL > 125000;
Rather, it must be dynamic. I feel the logic is pretty simple I'm just not sure how to translate it into SQL. I am thinking something along the lines of
"SELECT Lastname,salary,position from empl02 where salary > MAX(SALARY) of position(server)" what is a way to translate this task to SQL?
You need to retrieve the "reference" salary as a sub-query:
select lastname, salary, position
from empl02
where salary > (select max(salary)
from empl02
where position = 'manager');
For example I have table department and employee
I want to find the names of the employees together with budget of department employees belong to
Inside employee table it contain employee name which is ename
department table it contain budget for department
The command that I used
SELECT ENAME FROM EMPLOYEE
UNION
SELECT BUDGET FROM DEPARTMENT
ORDER BY ENAME;
but I keep getting expression must have same data type as corresponding expression error
Can someone explain to me What is wrong with my concept and how to obtain the result .
As the error says. It looks as though ENAME is a string and BUDGET is a numeric.
It sounds like you want to return these two values in a query along the lines of:
SELECT ENAME, SUM(BUDGET)
FROM EMPLOYEE INNER JOIN BUDGET
ON EMPLOYEE.ID = BUDGET.ID
GROUP BY ENAME
Note I'm making assumptions as to your data types, randomly throwing an ID out there, and assuming you want to sum the data. Oh, and the platform. The SQL should be pretty standard I guess, but this is T-SQL.
I guess You're trying to union two tables which are having different datatype, and union requires datatype of both columns should be same.
http://technet.microsoft.com/en-us/library/ms180026.aspx
See the msdn says The data types must be compatible.