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
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;
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;
i have table as
Dept
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
and another table as
Emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 12/17/1980 800 NULL 20
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30
7566 JONES MANAGER 7839 4/2/1981 2975 NULL 20
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30
7698 BLAKE MANAGER 7839 5/1/1981 2850 NULL 30
7782 CLARK MANAGER 7839 6/9/1981 2450 NULL 10
7788 SCOTT ANALYST 7566 12/9/1982 3000 NULL 20
7839 KING PRESIDENT NULL 11/17/1981 5000 NULL 10
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30
7876 ADAMS CLERK 7788 1/12/1983 1100 NULL 20
7900 JAMES CLERK 7698 12/3/1981 950 NULL 30
7902 FORD ANALYST 7566 12/3/1981 3000 NULL 20
7934 MILLER CLERK 7782 1/23/1982 1300 NULL 10
My question : List ALL the department names and their employee count.
Count should include only those employees whose hire date is greater than 1981
Result: should be like this
DNAME EMPCOUNT
ACCOUNTING 1
OPERATIONS 0
RESEARCH 2
SALES 0
please help
SELECT DNAME,
count(emp.deptno) AS EMPCOUNT
FROM DEPT
LEFT JOIN emp ON DEPT.DEPTNO = emp.DEPTNO
WHERE YEAR(hiredate)>1981
GROUP BY emp.deptno
I am trying to find an average of past records in the database based on a specific time frame (between 9 and 3 months ago) if there is no value recorded for a recent sale. the reason for this is recent sales on our website sometimes do not immediately collect commissions so i am needing to go back to historic records to find out what a commission rate estimate might be.
Commission rate is calculated as:
total_commission / gross_sales
It is only necessary to find out what an estimate would be if a recent sale has no "total_commission" recorded
here is what i have tried so far but i think this is wrong:
SELECT
cs.*
,SUM(cs2.gross_sales)
,SUM(cs2.total_commission)
FROM
(SELECT
sale_id
, date
, customer_code
, customer_country
, gross_sales
, total_commission
FROM customer_sale cs ) cs
LEFT JOIN customer_sale cs2
ON cs2.customer_code = cs.customer_code
AND cs2.customer_country = cs.customer_country
AND cs2.date > cs.date - interval 9 month
AND cs2.date < cs.date - interval 3 month
GROUP BY cs.sale_id
so that data would be structured as follows:
sale_id date customer_code customer_country gross_sales total_commission
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000
8 2014-04-02 cust2 france 20000
9 2014-04-02 cust3 united states 15000
10 2014-04-02 cust4 france 14000
11 2014-04-02 cust5 united states 13000
12 2014-04-02 cust6 france 12000
so I would need to output results from the query similar to this: (based on sales between 9 and 3 months ago from the same customer_code in the same customer_country)
sale_id date customer_code customer_country gross_sales total_commission gross_sales_past total_commission_past
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000 10000 1500
8 2014-04-02 cust2 france 20000 20000 3000
9 2014-04-02 cust3 united states 15000 15000 2250
10 2014-04-02 cust4 france 14000 14000 2100
11 2014-04-02 cust5 united states 13000 13000 1950
12 2014-04-02 cust6 france 12000 12000 1800
Your query looks mostly right, but I think your outer query needs to be GROUP BY cs.sale_id (assuming that sale_id is unique in the customer_sale table, and assuming that the date column is datatype DATE, DATETIME, or TIMESTAMP).
And I think you want to include a join predicate so that you match only match "past" rows to those rows where you don't have a total commission, e.g.
AND cs.total_commission IS NULL
And I don't think you really need an inline view.
Here's what I came up with:
SELECT cs.sale_id
, cs.date
, cs.customer_code
, cs.customer_country
, cs.gross_sales
, cs.total_commission
, SUM(ps.gross_sales) AS gross_sales_past
, SUM(ps.total_commission) AS total_commission_past
FROM customer_sale cs
LEFT
JOIN customer_sale ps
ON ps.customer_code = cs.customer_code
AND ps.customer_country = cs.customer_country
AND ps.date > cs.date - INTERVAL 9 MONTH
AND ps.date < cs.date - INTERVAL 3 MONTH
AND cs.total_commission IS NULL
GROUP
BY cs.sale_id
Appropriate indexes will likely improve performance of the query. Likely, the EXPLAIN output will show "Using temporary; Using filesort", and that can be expensive for large sets.
MySQL will likely be able to make use of a covering index for the JOIN:
... ON customer_sale (customer_code,customer_country,date,gross_sales,total_commission).
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