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
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
Employee
Eid ename salary
1 charles 3000
2 kiran 2000
3 naveen 8000
4 manju 7000
Department
deptid dname
101 charles
102 kiran
103 naveen
104 manju
Register
deptid eid
101 1
102 2
103 3
104 4
write a query to print (employee) ename and (department)dname.
SELECT a.ename, b.depart, c.eid
FROM employee a
INNER JOIN department b on a.ename=b.dname
INNER JOIN register c on b.deptid=c.deptid
where a.ename=b.dname and b.deptid=c.deptid;
In Oracle you can SELECT data from a table and request interactive user input to be used in the WHERE condition. This is done by specifying &column in the WHERE statement and user input will be prompted and used in its place. For example:
SELECT Ename, Deptno, Job, Sal FROM Emp
WHERE Sal &GiveNegationState BETWEEN 1100 AND 2850
SQL> /
Enter value for givenegationstate: NOT
ENAME DEPTNO JOB SAL
---------- ---------- --------- ----------
KING 10 PRESIDENT 5000
JONES 20 MANAGER 2975
JAMES 30 CLERK 950
FORD 20 ANALYST 3000
SMITH 20 CLERK 800
SCOTT 20 ANALYST 3000
Can this be done in MySQL?
I have a table that looks like this:
posid sales eid
1 20 001
1 20 002
1 30 001
2 30 001
1 30 002
2 30 001
1 30 002
2 20 002
2 10 002
I want to write a query that would give me sum of sales for each employee on particular pos. the result needs to be like this.
pos id emp id sales
1 001 50
1 002 80
2 001 60
2 002 30
How would I do this?
Use group by:
select t.posid
, t.eid
, sum(t.sales) as sales_by_posid
from mytable t
group by t.posid, t.eid
order by sales_by_posid desc
SELECT
posID, empID, sum(sales)
FROM your_table
GROUP BY posID, empID
ORDER BY posID, empID
Here's a group by tutorial: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php