Cases in SQL Query? - mysql

I have a Query In MySQL
Give all managers of First Bank Corporation a 10 percent salary raise unless the salary become greater than $100000; in such cases, give only a 3 percent raise.
In my Database I have following Table on which i have to work on
Employee(emp_name, street, city)
Works(emp_name, company_name, salary)
Company(company_name, city)
Manages(emp_name, manager_name)`
I have done half part of the query, now I have no idea how to do its other part
Update Works
set Salary= salary + salary * 0.10
where company_name = 'First Bank Corporation' AND
emp_name IN (Select manager_name from Manages) AND
salary > 100000;

UPDATE ...
SET salary = CASE
WHEN salary * 1.10 > 100000
THEN salary * 1.03
ELSE salary * 1.10
END
WHERE ...
-or-
UPDATE ...
SET salary = salary * CASE
WHEN salary * 1.10 > 100000
THEN 1.03
ELSE 1.10
END
WHERE ...

Related

Updating MySQL table using AVG calculated from same table

I am trying to update all rows where the salary is less than the average of all salaries in the table.
UPDATE PostOfficeStaff
SET salary = salary * 1.05
WHERE officeNo = 1
AND salary < (SELECT AVG(salary) FROM PostOfficeStaff)
It is giving me an error saying
You can't specify target table 'PostOfficeStaff' for update in FROM clause
I have tried this query without the FROM PostOfficeStaff as well and while it doesn't give me an error, it also does nothing.
How can I do this?
You can wrap it in an outer query like :
UPDATE PostOfficeStaff
SET salary = salary * 1.05
WHERE officeNo = 1
AND salary < (SELECT * FROM (
SELECT AVG(salary)
FROM PostOfficeStaff
) as t1
)
Check: https://dev.mysql.com/doc/refman/8.0/en/update.html
From the MySQL docs:
mysql> UPDATE items
> SET retail = retail * 0.9
> WHERE id IN
> (SELECT id FROM items
> WHERE retail / wholesale >= 1.3 AND quantity > 100);
ERROR 1093 (HY000): You can't specify target table 'items' for update in FROM clause
Instead, you can employ a multi-table update in which the subquery is moved into the list of tables to be updated, using an alias to reference it in the outermost WHERE clause, like this:
UPDATE items,
(SELECT id FROM items
WHERE id IN
(SELECT id FROM items
WHERE retail / wholesale >= 1.3 AND quantity < 100))
AS discounted
SET items.retail = items.retail * 0.9
WHERE items.id = discounted.id;
You can use JOIN with calculated table like:
UPDATE PostOfficeStaff
JOIN (
SELECT AVG(salary) avg_salary FROM PostOfficeStaff
) avg_salary ON avg_salary > salary
SET salary = salary * 1.05
WHERE officeNo = 1;
SQL Join fiddle

Get the employee name whose's salary is minimun with sql query

I'm trying to figure out a query which show the Out of the employees, who makes the MINIMUM money?
Emp_Table
EmpName | Salary | Gender
With the right query, the result should be 1 (employee) with the minimum salary
I tried with the following query
SELECT MIN(SALARY)
FROM Emp_Table
I don't know how can we display employee name as we have to set query on the basis of salary I'm learning the concept and googled it but didn't get a satisfactory answer
Please help me out.
Thanks.
The following should give you top 1 employee name and his/her salary which is equal to the minimum salary amongst all.
(Remove TOP 1 if you want all of them.)
SELECT TOP 1 EmpName, Salary
FROM Emp_Table
WHERE Salary = (SELECT MIN(Salary) FROM Emp_Table);
If multiple records having the same minimum salary value, then you can use the following query. It works for both SQL Server and MySql.
Query
select * from Emp_Table
where salary = (
select min(salary) from Emp_Table
);
use order by with top 1
For mssql
SELECT top 1 *
FROM Emp_Table
order by Salary asc
For mysql
SELECT *
FROM Emp_Table
order by Salary asc
limit 1
Try this:
--MySQL
SELECT E1.Employee_name
FROM Employee_table E1
WHERE E1.Salary = ( SELECT MIN(E2.Salary)
FROM Employee_table E2)
ORDER BY E1.Employee_name
LIMIT 1
--Oracle
SELECT E1.Employee_name
FROM Employee_table E1
WHERE E1.Salary = ( SELECT MIN(E2.Salary)
FROM Employee_table E2)
ORDER BY E1.Employee_name
FETCH FIRST 1 ROWS ONLY

My sql Group By case for getting highest paid employee in specific year

there is two table
1 Employee
2 salary
Employee : eId, ename, salaryId
Salary : salaryId, eId, salary, date
salary table contains monthly record of employee salary,
like:
eId date salary
1 2015-jan-01 10000
1 2015-feb-01 10000
1 2015-mar-01 10000
1 2014-jan-01 10000
1 2014-feb-01 10000
1 2014-mar-01 10000
2 2015-jan-01 10000
2 2015-feb-01 10000
2 2015-mar-01 10000
2 2014-jan-01 10000
2 2014-feb-01 10000
2 2014-mar-01 20000
so the query is to give me highest paid employee in specific year for ex: 2014
so here using group by with date and sum of salary output is :
empid - empname - sum(salary)
2 - xyz - 40000
Try it with something like this ... i just took a glance to point you toward right direction, this query might need some fixing
Select TOP 1 emp.eid, emp.ename, sal.salary
from Employee emp
join Salary sal on emp.salaryID = sal.salaryID
where DATEPART(yy,sal.date) = 2014
order by sal.salary desc
Good luck
Something like this could work
with salaries as (
select to_char(date,'yyyy') y, eld, sum(salary) s_sal
from salary
group by to_char(date,'yyyy'), eld)
select eld
from salaries s
where s_sal = (select max(s_sal) from salaries where y=s.y)
and s.y='2014';
But you didn't specify DB, so there is Oracle syntax.
And I didn't join with Employee table, but believe it isn't hard to add it.
If the requirement is to get the list of highest paid employee in each year then this can be achieved by a "Ranking" logic.
Something like this:
SELECT
Year(SalaryMonthDate) AS [Payroll Year],
EID,
Sum(Amout) AS [Annual Salary],
RANK() OVER(PARTITION BY Year(SalaryMonthDate) ORDER BY Sum(Amout) DESC) [Rank]
FROM tblSalary
GROUP BY Year(SalaryMonthDate), EID
Hope this helps.
MySQL Code,
SELECT Top 1 E.ename EmployeeName,
MAX(salary) AS EmployeeSalary
FROM Employee E
INNER JOIN Salary S
ON E.salaryID = S.salaryID
WHERE YEAR(S.Date) = 2014
GROUP BY
E.eId,
E.ename
#highest paid emp in specific year
SELECT e.empId,e.empName,SUM(s.salary),s.salDate
FROM emp e INNER JOIN salary s ON e.empId = s.empId
WHERE YEAR(s.salDate)=2009 GROUP BY s.empId ORDER BY SUM(s.salary) DESC LIMIT 1;

how to convert numbers to character in mysql while using select query

I am trying to convert numbers to a character representation.
I have a column named salary and it stores a value, e.g. 400 for employee 1. I want to display the salary of employee 1 with a select query. But, if a salary is 200 then display the salary as *, and if salary is 400 display ** and if salary is 600 than display ***.
So each * = 200
Can any one guide me how to do this ?
If there are a limited number of options (less than maybe 10), you want a case statement - https://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case.
If you want many many asterisks if someone's salary is 10,000, you could use a little logic around rpad - http://www.w3resource.com/mysql/string-functions/mysql-rpad-function.php.
This is a very trivial problem.
You have tagged it with both mysql and sql-server; these are different tools with different language variations. The answer below will work with mysql but may need some refactoring for sql-server.
Select name
, concat(salary,rpad('',floor(salary/200),'*'))
From your table
...
SQL Server:
create table #tab (
name nvarchar(100),
salary int
)
insert #tab values ('Mrs. Garrett', 100), ('Mr. Drummond', 200),
('Kimberly', 300), ('Willis', 400),
('Arnold', 500), ('Tooty', 600),
('Natalie', 700)
select name,
salary,
replicate('*', floor(salary/200))
from #tab
drop table #tab
Which produces:
name salary (No column name)
Mrs. Garrett 100
Mr. Drummond 200 *
Kimberly 300 *
Willis 400 **
Arnold 500 **
Tooty 600 ***
Natalie 700 ***
Alternatively, you could replace the replicate() with this:
case
when salary >= 600 then '***'
when salary >= 400 then '**'
when salary >= 200 then '*'
else ''
end
In MySQL you can use RPAD
SELECT RPAD('',FLOOR(salary/200), '*') AS stars
FROM table_name
And in SQL Server, you can use REPLICATE
;WITH C AS(
SELECT 200 AS salary
UNION ALL
SELECT 400 AS salary
UNION ALL
SELECT 600 AS salary
UNION ALL
SELECT 200 AS salary
UNION ALL
SELECT 200 AS salary
)
SELECT REPLICATE('*', salary/200) AS stars
,salary/200 AS salary
FROM C
Output
stars salary
* 1
** 2
*** 3
* 1
* 1

Show the names of employees of Company1 having salary greater than every employee of Company2 (SQL)

I have a Table Emp_company which has many columns and one of them is Salary. I need to know how to compare the Salaries of the employees from Company1 to the salaries of employees from Company2. Please help me if you can.
Select * from Company1 WHERE Company1.Salary > (Select Max(company2.salary) from company2)
Solution for Sourav:
TRY this :
Select * from Company WHERE Salary > (Select salary from company WHERE cname = 'Company2' ORDER by salary desc LIMIT 1) AND cname = 'company1'
You can get the max of Company2 salaries like this:
SELECT MAX(salary) FROM company_table WHERE cname="company2";
...then get that parameter and find those at company1 who earn more:
SELECT * FROM company_table WHERE cname="company1" AND salary > x;
...where x is the result of the first query.
EDIT: edited to use same table for both companies, as mentioned by poster in the comments
EDIT: the full query all on one line:
SELECT * FROM company_table WHERE cname="company1" AND salary > (SELECT MAX(salary) FROM company_table WHERE cname="company2");