Sql query for Distinct names for max salary - sql

1 shreya cpn 10000
2 shreya cpna 100000
3 shreya cpnaa 20000
4 preeti buld 30000
5 preeti bulda 20000
6 preeti buldan 25000
1 sushil mal 30000
1 sushil male 10000
1 sushil maleg 15000
9 abc ada 15000
this is my table...
and this is my query select * from stu where sal in (select MAX(sal)from stu group by name)
and ans is-
2 shreya cpna 100000
4 preeti buld 30000
1 sushil mal 30000
1 sushil maleg 15000
9 abc ada 15000
10 sss sfsfs 12000
its displaying 2 sushil...where i want only distinct names for that...
plzz give me suggestions...

If you need only name and salary columns in you output you can simply try this code:
select name, MAX(sal) from stu group by name
You can use columns in selection that you are grouping on as well as other columns with aggregated function.
In case you need all the other columns you will have to use join.
select s1.*
from stu s1
join (select name, MAX(sal) as sal from stu group by name) as s2
on s1.name = s2.name and s1.sal = s2.sal

Related

SQL query to join two tables without common column in oracle DB

I have 2 tables employee and job_role as below. I need to write a SQL query to find designation of each employee by joining this table.
Input Table
Employee
e_id e_name Salary
-----------------------
1 ABC 1000
2 CDE 2000
3 GHI 3500
4 JKL 5000
5 MNO 4000
6 XYZ 3000
Job_role
Designation Sal_min Sal_max
-------------------------------
Associate 1000 2000
Lead 2001 3000
Manager 3001 5000
Problem: if salary from employee table is in the range sal_min to sal_max, find the designation
Desired output:
e_id e_name Salary Designation
-----------------------------------
1 ABC 1000 Associate
2 CDE 2000 Associate
3 GHI 3500 Manager
4 JKL 5000 Manager
5 MNO 4000 Manager
6 XYZ 3000 Lead
The ON clause can consist of other operations than =. Here you can use <= and >= (or BETWEEN if you like).
SELECT E.E_ID,
E.E_NAME,
JR.DESIGNATION
FROM EMPLOYEE E
LEFT JOIN JOB_ROLE JR
ON JR.SAL_MIN <= E.SALARY
AND JR.SAL_MAX >= E.SALARY;
(Note: I used LEFT JOIN for the case that there are any employees where the salary doesn't match. I guessed you rather want to see them with a NULL designation than not at all.)

How to query data with sequence of duplicated rows

I have the following table with data:
ID SAL
_______________
1 1500
1 1500
1 2000
1 1500
2 2500
2 2500
3 1500
I want to query the same data BUT with new column that gives me a sequence of each duplicated row (based on id and sal columns).
My desired query results should be like this:
ID SAL ROW_SEQ
_______________________
1 1500 1
1 1500 2
1 2000 1
1 1500 3
2 2500 1
2 2500 2
3 1500 1
Any one has any idea PLEASE!..
You want to use a ROW_NUMBER() function here.
SELECT ID, SAL, ROW_NUMBER() OVER (PARTITION BY ID, SAL, ORDER BY SAL) AS ROW_SEQ
FROM MyTable
ORDER BY ID, SAL
By partitioning over your two desired fields, it will give you a ranking within each group in a sequential manner.
The documentation on it can be found here:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm

How to UPDATE a table based on descending order of SALARY or SNO?

Suppose Sal of emp table is not in sequence order, I have to update a table in descending order based on SAL.
From my basics, It is possible to create a new table based on descending order from predefined table as shown code below.
SQL>create table emp123 as select * from emp order by sal desc;
My question is, I want to update a table directly into a database instead of creating a new table.
Example:: Change from 1st table to 2nd table
SQL> SELECT * FROM SECOND;
ENO ENAME SAL COMM MONSAL ANNSAL
2 MK 2000 2000 24000
1 SK 3000 200 3200 38400
5 AK 1000 100 1100 13200
3 BK 4000 20 4020 48240
4 FK 1000 500 1500 18000
SQL> SELECT * FROM SECOND ORDER BY ENO;
ENO ENAME SAL COMM MONSAL ANNSAL
1 SK 3000 200 3200 38400
2 MK 2000 2000 24000
3 BK 4000 20 4020 48240
4 FK 1000 500 1500 18000
5 AK 1000 100 1100 13200
Tables have no defined order.
It is the application's duty to do things in a certain sequence if it wants it so.

Select statement with multiple rows from condition on values in single column affecting more than one column

I have the table below.Using salary as condition I want to get multiple rows. Below is current table call it employee.
empid | name | salary
-----------------------------------
1 A1 alex 20000
2 B2 ben 4500
3 C1 carl 14000
compare the salary to certain fixed values, and every time the salary is larger than the fixed value, show a record in output.Also output a calculated tax column.Below is a step closer to my final result
select e.*,tax= (case when salary<6000 then tax=0.06 *salary,when salary between 6000 and 18000 then tax= 0.06 *(salary -6000),else tax =0 ),m.incometype,
from employee e
left join
(
select 0 as threshold, 101 as incometype
union
select 5999 as threshold, 102 as incometype
union
select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid
Desired ouput would be:
empid | name | salary | incometype | tax
----------------------------------------------
1 A1 alex 20000 101 360
2 A1 alex 20000 102 720
3 A! alex 20000 103 0
4 B2 ben 4500 101 270
5 C1 carl 14000 101 360
6 C1 carl 14000 102 480
this is a question further to Select statement with multiple rows from condition on values in single column
this may help you
select e.*,
case
when salary<6000 then (0.06*salary)
when salary between 6000 and 1800 then (0.06*(salary -6000))
when m.incometype=103 then 0
end as tax
,m.incometype,
from employee e
left join
(
select 0 as threshold, 101 as incometype
union
select 5999 as threshold, 102 as incometype
union
select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid

how to write this query from 2 tables

Hear no relation have 2 table
"employee Table"
columns
Empid Empname Salary Age
1 abc 2000 20
2 xyz 3000 26
3 ijk 4000 32
4 mno 5000 50
"Groupname" table
columns
Groupid Groupname Min Max
1 young 18 25
2 middle 26 35
3 old 36 60
then i need result using both tables
empid empname age group
1 abc 2000 20 young
2 xyz 3000 26 middle
3 ijk 4000 32 middle
4 mno 5000 50 old
select empid,empname, salary, grp.groupname from employee emp left
outer join groupname grp
on ( emp.age > grp.[min] and emp.age <
grp.[max])
Try this:
select Empid, Empname, Salary, Age,Groupname
from Employee,GroupNameTable
where Age between Min and max