Lets say we have Three employees in Employee_Info table:
Emp_Name
Month
salary
emp1
2021-01
10000
emp1
2021-02
13000
emp1
2021-03
10000
emp2
2021-01
15000
emp2
2021-02
15000
emp2
2021-03
12000
emp3
2021-01
20000
emp3
2021-02
20000
emp3
2021-03
13000
I have to write a select sql query with an output like this:
Emp_Name
Month
salary
emp1
2021-01
10000
emp1
2021-02
13000
emp1
2021-03
10000
emp1
2021-03
33000
emp2
2021-01
15000
emp2
2021-02
15000
emp2
2021-03
12000
emp2
2021-03
42000
emp3
2021-01
20000
emp3
2021-02
20000
emp3
2021-03
13000
emp3
2021-03
53000
As you see the new row which should have the sum of all salaries for a particular employee.
Probably the simplest method is just to use union all:
select emp_name, month, salary
from t
union all
select emp_name, max(month), sum(salary)
from t
group by emp_name
order by emp_name, month, salary;
Note that if you want the cumulative salary on each row, then you can use window functions:
select t.*,
sum(salary) over (partition by emp_name order by month) as running_salary
from t;
Here is a db<>fiddle.
Related
I want a SQL query that prints a list of employee names who have been employed for less than 10 months having salary>2000. Sort this result by ascending emp_id.
I tried this but gave me an error stating extract is no longer used.:
select * from emp where
salary>2000 and (months_between(date, hire_date))<=10
order by emp_no asc ;
How do I form the query?
SELECT *
FROM Emp
WHERE Salary > 2000
AND Hire_Date >= DATE_SUB(CURRENT_DATE, INTERVAL 10 MONTH)
ORDER BY emp_no ASC;
Emp_No
Employee_Name
Job
ManagerID
Hire_Date
Salary
Comm
DepartmentID
7566
JONES
MANAGER
7839
2021-04-02
2975.00
null
20
7698
BLAKE
MANAGER
7839
2021-05-01
2850.00
null
30
7782
CLARK
MANAGER
7839
2021-06-09
2450.00
null
10
7788
SCOTT
ANALYST
7566
2021-07-13
3000.00
null
20
7839
KING
PRESIDENT
null
2021-11-17
5000.00
null
10
7902
FORD
ANALYST
7566
2021-12-03
3000.00
null
20
Demo on db<>fiddle here
SELECT name FROM Employee WHERE Salary > 2000 AND months < 10 ORDER BY employee_id ASC;
I made this test on RackerRank on 04-25-2022:
The expected result only includes the name field, if you include any other fields you will get the message "wrong answer" as a result. All fields in order by clause may be in the list of fields, thus the only way I got it was:
select main.name from
(select employee_id, name from employee where salary>2000 and months<10 order by employee_id asc) as main;
I am having trouble with this question I can't seem to get the count correct
on each department and only select the highest one as well as excluding
"DALLAS"
THIS IS THE QUESTION
"Write a SQL statement to display the name and location of all departments
(except the departments located in Dallas) with the highest number of
employees.
You cannot use join operations in your SQL statement (e.g., … FROM department,
employee WHERE …, department INNER JOIN employee ON …)."
DEPARTMENT_ID DEPARTMENT_NAME LOCATION
------------- -------------------- --------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 IT DALLAS
50 EXECUTIVE NEW YORK
60 MARKETING CHICAGO
6 rows selected
EMPLOYEE_ID EMPLOYEE_NAME JOB_TITLE SUPERVISOR_ID HIRE_DATE SALARY COMMISSION DEPARTMENT_ID
----------- -------------------- -------------------------------------------------- ------------- --------- ---------- ---------- -------------
7839 KING PRESIDENT 20-NOV-01 5000 50
7596 JOST VICE PRESIDENT 7839 04-MAY-01 4500 50
7603 CLARK VICE PRESIDENT 7839 12-JUN-01 4000 50
7566 JONES CHIEF ACCOUNTANT 7596 05-APR-01 3000 10
7886 STEEL PUBLIC ACCOUNTANT 7566 08-MAR-03 2500 10
7610 WILSON BUSINESS ANALYST 7596 03-DEC-01 3000 20
7999 WOLFE TEST ANALYST 7610 15-FEB-02 2500 20
7944 LEE REPORTING ANALYST 7610 04-SEP-06 2400 20
7900 FISHER SALES EXECUTIVE 7603 06-DEC-01 3000 500 30
7921 JACKSON SALES REPRESENTATIVE 7900 25-FEB-05 2500 400 30
7952 LANCASTER SALES CONSULTANT 7900 06-DEC-06 2000 150 30
7910 SMITH DATABASE ADMINISTRATOR 7596 20-DEC-01 2900 40
7788 SCOTT PROGRAMMER 7910 15-JAN-03 2500 40
7876 ADAMS PROGRAMMER 7910 15-JAN-03 2000 40
7934 MILLER PROGRAMMER 7876 25-JAN-02 1000 40
8000 BREWSTER TBA 22-AUG-13 2500
8100 PHILLIPS TBA 7839 21-AUG-13 2800
7400 SMITH VICE PRESIDENT 7839 16-FEB-01 4300 50
7700 ANDRUS PUBLIC ACCOUNTANT 7566 18-FEB-02 2500 10
7601 SAMPSON PROGRAMMER 7910 09-JAN-01 2500 40
7588 DODSON TEST ANALYST 7610 02-AUG-08 2500 20
7888 SANDY SALES CONSULTANT 7900 05-AUG-04 2500 30
22 rows selected
SELECT DEPARTMENT_NAME,
location,
count(*)
FROM DEPARTMENT
WHERE department_id IN ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
group by department_NAME, location
ORDER BY location;
DEPARTMENT_NAME LOCATION COUNT(*)
-------------------- -------------------- ----------
MARKETING CHICAGO 1
SALES CHICAGO 1
ACCOUNTING NEW YORK 1
EXECUTIVE NEW YORK 1
you can try using sub-queries if you are limited in not using joins
SELECT *
FROM (SELECT d.department_name,
d.location,
(SELECT COUNT(employee_id)
FROM employee e
WHERE e.department_id = d.department_id) no_employees
FROM department d
WHERE d.location <> 'DALLAS'
) t
WHERE no_employees = (SELECT COUNT(employee_id)
FROM employee
WHERE department_id IN (SELECT DISTINCT department_id
FROM department
WHERE location <> 'DALLAS')
GROUP BY department_id
ORDER BY 1 DESC
LIMIT 1)
Result
department_name location no_employees
SALES CHICAGO 4
EXECUTIVE NEW YORK 4
Am trying to the find the department with maximum count and then retrieve the corresponding name and location without using joins
SELECT (SELECT DEPARTMENT_NAME, location
FROM DEPARTMENT
WHERE department_id = q.department_id) ,
q.ct countofdept
FROM
(SELECT count(*) ct, department_id
FROM EMPLOYEE
WHERE department_id in ( SELECT department_id
FROM department
WHERE UPPER(location) <> 'DALLAS'
)
GROUP BY department_id
ORDER BY ct desc
LIMIT 1) q
Below is table , I need output which provide me all columns with group by date1 which has sum of salary ( > 6000) for particular date.
source table ( INput table ) :
id name date1 salary
1 JOHNSON 1990-12-17 1800
2 HARDING 1990-12-17 5200
3 TAFT 1990-12-17 2500
4 HOOVER 1990-04-02 2700
5 LINCOLN 1990-04-02 2250
6 GARFIELD 1990-04-02 5400
7 POLK 1997-09-22 2500
8 GRANT 1997-09-22 320
Desired output table (below) : ( last 2 entry 7,8 is not present)
id name date1 salary sum(salary)
1 JOHNSON 1990-12-17 1800 9500
2 HARDING 1990-12-17 5200 9500
3 TAFT 1990-12-17 2500 9500
----------
4 HOOVER 1990-04-02 2700 10350
5 LINCOLN 1990-04-02 2250 10350
6 GARFIELD 1990-04-02 5400 10350
-----------
(Last two values should not come because for that date sum of salary is < 6000)
Oracle Setup:
CREATE TABLE table_name (id, name, date1, salary ) AS
SELECT 1, 'JOHNSON', DATE '1990-12-17', 1800 FROM DUAL UNION ALL
SELECT 2, 'HARDING', DATE '1990-12-17', 5200 FROM DUAL UNION ALL
SELECT 3, 'TAFT', DATE '1990-12-17', 2500 FROM DUAL UNION ALL
SELECT 4, 'HOOVER', DATE '1990-04-02', 2700 FROM DUAL UNION ALL
SELECT 5, 'LINCOLN', DATE '1990-04-02', 2250 FROM DUAL UNION ALL
SELECT 6, 'GARFIELD', DATE '1990-04-02', 5400 FROM DUAL UNION ALL
SELECT 7, 'POLK', DATE '1997-09-22', 2500 FROM DUAL UNION ALL
SELECT 8, 'GRANT', DATE '1997-09-22', 320 FROM DUAL;
Query:
SELECT *
FROM (
SELECT t.*,
SUM( salary ) OVER ( PARTITION BY date1 ) AS sum_salary
FROM table_name t
)
WHERE sum_salary >= 6000;
Output:
ID NAME DATE1 SALARY SUM_SALARY
---------- -------- ------------------- ---------- ----------
4 HOOVER 1990-04-02 00:00:00 2700 10350
6 GARFIELD 1990-04-02 00:00:00 5400 10350
5 LINCOLN 1990-04-02 00:00:00 2250 10350
3 TAFT 1990-12-17 00:00:00 2500 9500
2 HARDING 1990-12-17 00:00:00 5200 9500
1 JOHNSON 1990-12-17 00:00:00 1800 9500
select * from
(
select id, name,
sum(salary) over (partition by date1) sum_sal
from table_name
) where sum_sal >= 6000;
Below query will work on both sql server 2008 and Mysql :-
select * from (
select a.id,a.name,a.date1,a.salary,
(select sum(salary) from table_name b where b.date1=a.date1) Sum_Salary
from
table_name a
) c
where Sum_Salary >=6000
order by id
Output :-
id name date1 salary Sum_Salary
1 JOHNSON 1990-12-17 1800.00 9500.00
2 HARDING 1990-12-17 5200.00 9500.00
3 TAFT 1990-12-17 2500.00 9500.00
4 HOOVER 1990-04-02 2700.00 10350.00
5 LINCOLN 1990-04-02 2250.00 10350.00
6 GARFIELD 1990-04-02 5400.00 10350.00
write query to get result as below
emp
-----------------------------
empno ename dept sal
------------------------------
1001 a cse 1000
1211 b cse 5000
---------------
sum 6000
---------------
3511 x ese 4000
1471 y ese 3000
----------------
sum 7000
sum ------------------------------------------- 13000
Sum of individual dept
select dept, sum(sal) as sal_sum
from emp
group by dept
order by dept
Total sum
select sum(sal) as total_sum
from emp
I have this table (named EMP) in MySQL:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ----- ------- ---- -------- -------- ------- ------
7369 SMITH CLERK 7902 13-JUN-83 800.00 20
7499 ALLEN SALESMAN 7698 15-AUG-83 1,600.00 300.00 30
7521 WARD SALESMAN 7698 26-MAR-84 1,250.00 500.00 30
7566 JONES MANAGER 7839 31-OCT-83 2,975.00 20
7654 MARTIN SALESMAN 7698 05-DEC-83 1,250.00 1,400.00 30
7698 BLAKE MANAGER 7839 11-JUL-84 2,850.00 30
7782 CLARK MANAGER 7839 14-MAY-84 2,450.00 10
7788 SCOTT ANALYST 7566 05-MAR-84 3,000.00 20
7839 KING PRESIDENT 09-JUL-84 5,000.00 10
7844 TURNER SALESMAN 7698 04-JUN-84 1,500.00 .00 30
7876 ADAMS CLERK 7788 04-JUN-84 1,100.00 20
7900 JAMES CLERK 7698 23-JUL-84 950.00 30
7902 FORD ANALYST 7566 05-DEC-83 3,000.00 20
7934 MILLER CLERK 7782 21-NOV-83 1,300.00 10
How can I write a Select query that returns the year (HIREDATE column) when there are most number of employees (rows) registered?
What about
SELECT YEAR(hiredate) FROM emp
GROUP BY YEAR(hiredate)
ORDER BY COUNT(*) DESC
LIMIT 1
?
What about:
SELECT COUNT(1), YEAR(HIREDATE) FROM EMP GROUP BY YEAR(HIREDATE);
TRY
SELECT count(*) as total FROM emp GROUP BY hiredate ORDER BY total DESC LIMIT 1;
Try this:
select year(hiredate), count(*) from emp
group by year(hiredate) order by 2 desc limit 1