Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's suppose and EMP_Table consists of following columns
EMPID,SAL,DEPTID,MGRID
Req: List all managers whose salary is > all of team members salary
Sample Input:
EmpID SAL DEPTID MGRID
101 10000 A 102
102 30000 A 102
103 15000 A 102
104 10000 B 106
105 30000 B 106
106 45000 B 106
107 50000 B 106
108 20000 C 109
109 50000 C 109
110 45000 C 109
Expected output:
EmpID SAL DEPTID MGRID
102 30000 A 102
109 50000 C 109
SELECT M.EmpID, M.Salary, M.DepId, M.MgrId
FROM dbo.Employee E INNER JOIN
dbo.Employee M ON
E.EmpID = M.MgrId
WHERE E.Salary >= (SELECT MAX(DE.Salary) FROM dbo.Employee DE WHERE DE.DepId = E.DepId)
AND M.Salary >= E.Salary
Related
My Input data is :
Id salary
101 1000
101 1000
102 2500
102 3000
105 5000
105 5000
105 5000
106 12
106 142
106 12
Output :
102 2500
102 3000
106 12
106 142
106 12
I mean based on the id , I want to find out which Id has different salary. If the salary is the same in all the records , I want to discard those records. Kindly help.
select *
from t
where id in(
select Id
from t
group by Id
having max(salary) <> min(salary)
)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
Fiddle
SELECT `Id`, `salary` FROM salary
WHERE Id IN( SELECT Id FROm salary GROUP By Id HAVING COUNT(DISTINCT SALARY) > 1)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
fiddle
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have 3 tables like
TAB_1
ID
NUMBER
1
101
2
102
3
103
4
104
5
105
6
106
7
107
8
108
9
109
10
110
TAB_2
ID
NUMBER
1
101
2
102
3
105
TAB_3
ID
NUMBER
1
104
2
107
3
110
The output needs to be:
ID
NUMBER
1
103
2
106
3
108
4
109
I think u can use NOT IN with Subqueries
Like;
SELECT
*
FROM
Table_1
WHERE
Number NOT IN (Select number from Table_2) and
Number NOT IN (Select number from Table_3)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table employees
EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE JOB_ID SALARY DEPARTMENT_ID
-------------------------------------------------------------------------------------------
100 Steven King 17-JUN-03 AD_PRES 24000 90
101 Neena Kochhar 21-SEP-05 AD_VP 17000 90
102 Lex De Haan 13-JAN-01 AD_VP 17000 90
103 Alexander Hunold 03-JAN-06 IT_PROG 9000 60
104 Bruce Ernst 21-MAY-07 IT_PROG 6000 60
105 David Austin 25-JUN-05 IT_PROG 4800 60
106 Valli Pataballa 05-FEB-06 IT_PROG 4800 60
107 Diana Lorentz 07-FEB-07 IT_PROG 4200 60
109 Daniel Faviet 16-AUG-02 FI_ACCOUNT 9000 100
110 John Chen 28-SEP-05 FI_ACCOUNT 8200 100
111 Ismael Sciarra 30-SEP-05 FI_ACCOUNT 7700 100
112 Jose Manuel Urman 07-MAR-06 FI_ACCOUNT 7800 100
113 Luis Popp 07-DEC-07 FI_ACCOUNT 6900 100
and table departments
DEPARTMENT_ID DEPARTMENT_NAME
-------------------------------
30 Purchasing
50 Shipping
60 IT
90 Executive
100 Finance
I need to write 3 queries:
Get department name and number of employees in each department sorting ascending
I tried this
select count(e.employee_id), d.department_name
from t_employees e, t_departments d
where d.department_id = e.department_id
group by e.department_name
order by count(e.employee_id);
Not sure if it will work since I can not really try it.
Get department name and average salary where average salary by department is more or equal 5000
Result table
DEPARTMENT_NAME AVERAGE_SALARY
---------------------------------
Executive 19333,333
IT 5760
Finance 7920`
select d.department_name, AVG(e.salary) as AVARAGE_SALARY
from t_departments d, t_employees e
where d.department_id = e.department_id
and avg(e.salary) >= '5000'
group by d.department_name;
Get first name and last name of employees who have salary bigger than the average salary from the department they work in
Result table
FIRST_NAME LAST_NAME
---------------------------------
Steven King
Alexander Hunold
Bruce Ernst
Daniel Faviet
John Chen
I have no code for this one.
You're quite close.
I don't have your tables so I'll use Scott's sample schema. Try to apply it to your data model.
First:
SQL> select d.dname, count(*) cnt
2 from emp e join dept d on e.deptno = d.deptno
3 group by d.dname
4 order by cnt desc;
DNAME CNT
-------------- ----------
SALES 6
RESEARCH 5
ACCOUNTING 3
Second: not where, but having:
SQL> select d.dname, avg(e.sal) avg_sal
2 from emp e join dept d on e.deptno = d.deptno
3 group by d.dname
4 having avg(e.sal) > 2000; --> this
DNAME AVG_SAL
-------------- ----------
ACCOUNTING 2916,66667
RESEARCH 2175
SQL>
Third:
SQL> with avgsal as
2 (select deptno,
3 round(avg(sal), 1) avgsal
4 from emp
5 group by deptno
6 )
7 select e.deptno, e.ename, e.sal, a.avgsal
8 from emp e join avgsal a on a.deptno = e.deptno
9 where e.sal > a.avgsal
10 order by e.deptno;
DEPTNO ENAME SAL AVGSAL
---------- ---------- ---------- ----------
10 KING 5000 2916,7
20 JONES 2975 2175
20 SCOTT 3000 2175
20 FORD 3000 2175
30 ALLEN 1600 1566,7
30 BLAKE 2850 1566,7
6 rows selected.
SQL>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I Have following table which contains details of Employee
EmpId EmpName Mgr Salary Dept
1 Deepak 2 20000 1
2 Annu NULL 22000 1
3 Jai 2 19500 1
4 Jitendra 1 18000 2
5 Vaishali 1 18000 2
6 Philip 4 15000 3
I wants to show salary of each dept with each employee details,if it repeats no issues as shown below
EmpId EmpName Mgr Salary Dept DeptSal
1 Deepak 2 20000 1 61500
2 Annu NULL 22000 1 61500
3 Jai 2 19500 1 61500
4 Jitendra 1 18000 2 36000
5 Vaishali 1 18000 2 36000
6 Philip 4 15000 3 15000
You should look into the SUM() OVER(PARTITION) windowing functions in SQL Server.
See this MSDN link
This link should help you in solving your problem.
If you are still compelled to get a solution rather than understanding how you can solve these type of problems, then answer is mentioned in a single line as spoiler below
select *, DeptSal=sum(Salary) over (partition by Dept ) from t
This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 9 years ago.
There are two tables in my Oracle database.
First table is (customers)-
customer_id Customer_name Customer_age Customer_address salary
103 Giriraj Rathi 22 Kolkata 12000
100 Subir Adhikari 22 Bolpur 10000
101 Rakesh Chatterjee 21 Tarkeshwar 8000
102 Jayanta Patra 20 Tarkeshwar 9000
104 Abhi Karmakar 22 Burdwan 8000
105 Mainak Manna 21 Burdwan 9000
106 Subho Gupta 20 Kolkata 10000
107 Aritra Das 23 Kolkata 7000
108 Pradip Paul 22 Kolkata 5000
109 Sourav Banerjee 22 Bolpur 9000
Second table is (Orders):
Order_id Order_date customer_id amount
200 12-03-13 100 1100
201 09-05-13 101 1400
202 07-04-12 103 2500
204 29-05-13 104 2400
203 09-02-13 105 9000
205 18-06-13 106 2100
206 09-07-13 107 1600
207 18-05-13 108 2900
209 18-04-13 109 2400
Now I wanted to join both the tables. So I used the query:
select customer_id,
customer_name,
customer_address,
order_id,order_date,
amount
from customers,
orders
where customers.customer_id=orders.customer_id;
I Googled about the error and found this happens when there is ambiguity in the SQL code itself, but in this case I see nothing.
It is always a good idea to add the table name/alias to the column like this
select c.customer_id,
c.customer_name,
c.customer_address,
o.order_id,
o.order_date,
o.amount
from customers c
inner join orders o on c.customer_id = o.customer_id
If you don't then the DB don't know which column to take and both tables have a column named customer_id.