I have table like this:
name | salary
Tom | 10000
Mary | 20000
Jack | 30000
Lisa | 40000
Jake | 60000
I need an update query to update the salary column depending on the values it contains.
Salaries need to increase by:
5000 for values between 10000 to 15000
7000 for values between 15000 to 20000
8000 for values between 20000 to 30000
10000 for values between 40000 to 60000
Try using the CASE statement within the UPDATE command
UPDATE
[yourtablename]
SET
salary =
CASE
WHEN salary BETWEEN 10000 AND 15000 THEN salary + 5000
WHEN salary BETWEEN 15000 AND 20000 THEN salary + 7000
WHEN salary BETWEEN 20000 AND 30000 THEN salary + 8000
WHEN salary BETWEEN 40000 AND 60000 THEN salary + 10000
ELSE salary
END
Something like this:
UPDATE YourTable
SET salary = CASE
WHEN salary > 10000 AND salary <= 15000 THEN salary + 5000
WHEN salary > 15000 AND salary <=20000 THEN salary + 7000
.
.
.
END
Just use an UPDATE statement with a CASE statement with the required logic in it:
UPDATE SalaryTable
SET Salary =
(CASE WHEN Salary BETWEEN 10000 AND 14999 THEN Salary + 5000
WHEN Salary BETWEEN 15000 AND 19999 THEN Salary + 7000
WHEN Salary BETWEEN 20000 AND 29999 THEN Salary + 8000
WHEN Salary BETWEEN 40000 AND 59000 THEN Salary + 10000
ELSE Salary
END)
I've used BETWEEN which evaluates greater than or equal to and less than or equal to, hence the values like 14999.
Also, you have a gap between 30000 and 40000, which isn't picked up, but I'm assuming this is down to it being dummy data.
Reference:
SQL BETWEEN
SQL CASE
This should work:
UPDATE T
SET T.Salary += CASE
WHEN T.salary BETWEEN 10000 AND 15000 THEN 5000
WHEN T.salary BETWEEN 15000 AND 20000 THEN 7000
WHEN T.salary BETWEEN 20000 AND 30000 THEN 80000
WHEN T.salary BETWEEN 40000 AND 60000 THEN 10000
ELSE 0
END
FROM YourTable AS T
Related
Employee Salary Department
A 1000 IT
B 2000 IT
C 3000 IT
D 4000 HR
E 2000 HR
F 1500 IT
G 7000 HR
Write a query to get results like below -
Employee Salary Next highest salary(in same department)
Department
A 1000 1500 IT
B 1500 2000 IT
.
.
E 2000 4000 HR
D 4000 None HR
select employee, salary
, lead(salary) over(partition by department order by salary) as next_salary
, department
from table
My problem is I just wanna show all the unpaid rows:
I have this data:
id name commission paid
1 James 15000 2000
2 Curry 15000 15000
3 Durant 15000 0
4 Wade 15000 5000
5 Harden 15000 15000
I wanted to get only the rows that are not gonna be equal to 0, I wanted to use where clause for commission - paid if it is greater than 0
Output that I would expect is:
id name commission paid
1 James 15000 2000
3 Durant 15000 0
4 Wade 15000 5000
SELECT * FROM table_name WHERE (commission - paid) > 0
Or
SELECT * FROM table_name WHERE commission!=paid
This will work too because either subtraction will be greater than zero or 0 (assuming paid amount is always less than commission)
You can use the following solution using a simple calculation on WHERE:
SELECT * FROM table_name WHERE commission - paid > 0
You can also use the following:
SELECT * FROM table_name WHERE commission > paid
Note: In this case someone can overpay the commission. Using != or <> the overpay would be found as not paid.
demo: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=a391576f2df2c5d7c8616485b2c70099
To get all paid rows you can use the following:
SELECT * FROM table_name WHERE commission - paid <= 0
or ...
SELECT * FROM table_name WHERE commission <= paid
you can use the following query given below using WHERE clause
SELECT * FROM commision_table_name WHERE (commission - paid) > 0;
Suppose Sal of emp table is not in sequence order, I have to update a table in descending order based on SAL.
From my basics, It is possible to create a new table based on descending order from predefined table as shown code below.
SQL>create table emp123 as select * from emp order by sal desc;
My question is, I want to update a table directly into a database instead of creating a new table.
Example:: Change from 1st table to 2nd table
SQL> SELECT * FROM SECOND;
ENO ENAME SAL COMM MONSAL ANNSAL
2 MK 2000 2000 24000
1 SK 3000 200 3200 38400
5 AK 1000 100 1100 13200
3 BK 4000 20 4020 48240
4 FK 1000 500 1500 18000
SQL> SELECT * FROM SECOND ORDER BY ENO;
ENO ENAME SAL COMM MONSAL ANNSAL
1 SK 3000 200 3200 38400
2 MK 2000 2000 24000
3 BK 4000 20 4020 48240
4 FK 1000 500 1500 18000
5 AK 1000 100 1100 13200
Tables have no defined order.
It is the application's duty to do things in a certain sequence if it wants it so.
I know how to use cumulative sum in its basic formulation, with code like this:
Table Name: Employees
dept_id salary
-------------
10 1000
10 1000
10 2000
10 3000
20 5000
20 6000
20 NULL
SELECT dept_id,
salary,
SUM(salary) OVER(PARTITION BY dept_id
ORDER BY salary ASC
rows unbounded preceding) cum_sum
FROM Employees;
dept_id salary cum_sum
--------------------------
10 1000 1000
10 1000 2000
10 2000 4000
10 3000 7000
20 5000 5000
20 6000 11000
20 NULL 11000
But how do I limit the cumulative sum to only N preceding rows?
For example, limit cumulative sum to current row and previous two rows.
dept_id salary cum_sum
--------------------------
10 1000 1000
10 1000 2000
10 2000 4000
10 3000 6000
20 5000 5000
20 6000 11000
20 NULL 11000
The SQL syntax is:
SELECT dept_id,
salary,
SUM(salary) OVER(PARTITION BY dept_id
ORDER BY salary ASC
rows between <N> preceding and current row) cum_sum
FROM Employees;
I am new to Oracle.
I want to find the name who got the second maximum salary from a table?
Here the example table:
R.no name employee_id salary
201 Sanjay 78781 1000
202 Mohan 78782 2500
203 Viji 78783 5000
204 Vinay 78784 3000
205 Ishanth 78785 8000
select *
from (
select name,
salary,
dense_rank() over (order by salary desc) as rnk
from table_name
) t
where rnk = 2
SQLFiddle example: http://sqlfiddle.com/#!4/e93c3/1