I have a table with values as follows(MySql):
SQL> SELECT deptno, ename FROM emp ORDER BY deptno, ename;
DEPTNO ENAME
------ ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
14 rows selected.
but I need them in the following less convenient format:
DEPTNO ENAME
------ -----------------------------------------
10 CLARK, KING, MILLER
20 ADAMS, FORD, JONES, SCOTT, SMITH
30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD
Kindly help me to achiever above result/output.
Using group_concat
SELECT deptno, group_concat(ename order by ename) as ename
FROM emp
group by deptno
ORDER BY deptno;
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 have emp table and KING is the CEO of the company, he does not have a manager.
KING should get RANK '1'.
KING is the manager for JONES,CLARK,BLAKE. So these will get RANK '2'....etc.
intermediate table for emp and manager relation:
empno ename mgr
7566 JONES KING
7782 CLARK KING
7698 BLAKE KING
7900 JAMES BLAKE
7844 TURNER BLAKE
7654 MARTIN BLAKE
7521 WARD BLAKE
7499 ALLEN BLAKE
7934 MILLER CLARK
7902 FORD JONES
7788 SCOTT JONES
7876 ADAMS SCOTT
7369 SMITH FORD
7839 KING NULL
Final output should be like:
mgr rank
KING 1
JONES 2
CLARK 2
BLAKE 2
JAMES 3
TURNER 3
MARTIN 3
WARD 3
ALLEN 3
MILLER 3
SCOTT 3
FORD 3
ADAMS 4
SMITH 4
Thanks in advance. I want this to be done in oracle.
It took me while to wrap my head around the heirarchy queries in the past ... however, once you take the time to fiddle with them, they usually aren't that tough.
SQL> with w_data as (
2 select 7566 empno, rtrim('JONES ') ename, rtrim('KING ') mgr from dual union all
3 select 7782 empno, rtrim('CLARK ') ename, rtrim('KING ') mgr from dual union all
4 select 7698 empno, rtrim('BLAKE ') ename, rtrim('KING ') mgr from dual union all
5 select 7900 empno, rtrim('JAMES ') ename, rtrim('BLAKE') mgr from dual union all
6 select 7844 empno, rtrim('TURNER') ename, rtrim('BLAKE') mgr from dual union all
7 select 7654 empno, rtrim('MARTIN') ename, rtrim('BLAKE') mgr from dual union all
8 select 7521 empno, rtrim('WARD ') ename, rtrim('BLAKE') mgr from dual union all
9 select 7499 empno, rtrim('ALLEN ') ename, rtrim('BLAKE') mgr from dual union all
10 select 7934 empno, rtrim('MILLER') ename, rtrim('CLARK') mgr from dual union all
11 select 7902 empno, rtrim('FORD ') ename, rtrim('JONES') mgr from dual union all
12 select 7788 empno, rtrim('SCOTT ') ename, rtrim('JONES') mgr from dual union all
13 select 7876 empno, rtrim('ADAMS ') ename, rtrim('SCOTT') mgr from dual union all
14 select 7369 empno, rtrim('SMITH ') ename, rtrim('FORD ') mgr from dual union all
15 select 7839 empno, rtrim('KING ') ename, NULL mgr from dual
16 )
17 select empno, ename, mgr, level
18 from w_data
19 connect by mgr = PRIOR ename
20 start with mgr IS NULL
/
21
EMPNO ENAME MGR LEVEL
---------- ------ ----- ----------
7839 KING 1
7698 BLAKE KING 2
7499 ALLEN BLAKE 3
7900 JAMES BLAKE 3
7654 MARTIN BLAKE 3
7844 TURNER BLAKE 3
7521 WARD BLAKE 3
7782 CLARK KING 2
7934 MILLER CLARK 3
7566 JONES KING 2
7902 FORD JONES 3
EMPNO ENAME MGR LEVEL
---------- ------ ----- ----------
7369 SMITH FORD 4
7788 SCOTT JONES 3
7876 ADAMS SCOTT 4
14 rows selected.
SQL>
w_data is just to fake out your test data ..
connect by mgr = prior ename
this takes each record, and compares the mgr with the previous level employee.
start with mgr is null
this tells Oracle where to start ...
Level is the depth of the heirarchy.
I believe Oracle supports CASE WHERE in the ORDER BY clause. Something like this:
SELECT [blah]
FROM [intermediate tbl]
ORDER BY
CASE WHEN mgr IS NULL THEN 1
CASE WHEN mgr = 'KING' THEN 2
... ELSE ... END
I believe there is a shorter way to do this in Oracle (my experience is with SSMS):
ORDER BY decode(mgr, 'NULL', 1, 'KING', 2, ...)
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
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 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