Hi
i have the table employee with columns emp_id, emp_name, salary, dep_id and the another table department with columns dep_id, dep_name, manager.
dep_id is the foreign key for employee table.
i want the query for retrieving emp_id, salary, dep_id i.e. the employee who got maximum salary in each department using group by
4 2629
Kindly post the query that you are working on.
try this.. -
create table employee_test (emp_id int, emp_name varchar(50), salary numeric(10,0), dep_id int )
-
/
-
-
create table department_test ( dep_id int, dep_name varchar(50), manager varchar(50))
-
/
-
-
insert into employee_test values(1,'san',100,1)
-
insert into employee_test values(2,'san1',50,2)
-
insert into employee_test values(3,'san2',250,1)
-
insert into employee_test values(4,'san3',350,3)
-
insert into employee_test values(5,'san4',900,1)
-
insert into employee_test values(6,'san5',25,3)
-
insert into employee_test values(7,'san6',560,1)
-
insert into employee_test values(8,'san7',11,2)
-
insert into employee_test values(9,'san44',900,1)
-
insert into department_test values (1,'dep1','rr')
-
insert into department_test values(2,'dep2','rr')
-
insert into department_test values(3,'dep3','rr')
-
-
/
-
-
select cur.dep_ID,cur.salary,emp1.emp_name from (
-
select dep.dep_ID,max(salary) as salary from employee_test emp inner join
-
department_test dep on dep.dep_id=emp.dep_id
-
group by dep.dep_ID) cur
-
left join employee_test emp1
-
on emp1.salary =cur.salary
-
/
-
~~~ Enjoy Knowledge Freedom ~~~
You can use analytical function for this -
create table employee_test (emp_id int, emp_name varchar(50), salary numeric(10,0), dep_id int )
-
/
-
-
create table department_test ( dep_id int, dep_name varchar(50), manager varchar(50))
-
/
-
-
insert into employee_test values(1,'san',100,1)
-
insert into employee_test values(2,'san1',50,2)
-
insert into employee_test values(3,'san2',250,1)
-
insert into employee_test values(4,'san3',350,3)
-
insert into employee_test values(5,'san4',900,1)
-
insert into employee_test values(6,'san5',25,3)
-
insert into employee_test values(7,'san6',560,1)
-
insert into employee_test values(8,'san7',11,2)
-
insert into employee_test values(9,'san44',900,1)
-
insert into department_test values (1,'dep1','rr')
-
insert into department_test values(2,'dep2','rr')
-
insert into department_test values(3,'dep3','rr')
-
-
/
-
-
select e.emp_id,e.dep_id,max(salary) over(partition by e.dep_id) from employee_test e;
-
if you want to show the department name then only you need to join the two tables.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul Littlefield |
last post by:
Hi
How do I select from a single table using 2 columns (surname,postcode)
as the unique criteria?
e.g.
"Surname","Forename","Salutation","Postcode"
"Bear","Daddy","Mr","CT10 7AF"...
|
by: Andre Arpin |
last post by:
I am new at sql so would appreciate some help
I have the name of a table in alocal variable is it possible to select this
table
DECLARE @name sysname
SET @name = 'tblSniffedItems'
PRINT...
|
by: Nirmala123 |
last post by:
hi...
I want to sort the table using combobox values. I give the code here.
address.html:
<html>
<head>
<title>Add a new entry</title>
</head>
|
by: bimeldip |
last post by:
Hi,
I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
|
by: Ankit |
last post by:
Hi guys i need to make a table to store a certain data using
Tkinter..I have searched on the group but i have not been able to find
a solution that would work for me..The thing is that i want my...
|
by: anureddy |
last post by:
help me
how to add datagridview columns to another table,using windowsapplications.
and set the displaymember and value member datagridviewcomboboxcolumn.
i used this below code but that not...
|
by: cb123 |
last post by:
Hello,
Please bear with me I'm new to t-sql.
I've had no luck trying to figure this out yet using sql code. I need to use an existing table (data dictionary) as input for creating a new table.
...
|
by: jelumalai |
last post by:
I am using display:none with using Table. When show the onClick using javascript. Then it will show, again i will hide, content only hidden, but that table doesn't hide.
<script>
function...
|
by: prao2005 |
last post by:
Q) How to display all the records of a table using xsl?
Solution applied -->
Suppose a class "test" has an attribute "test1" which itself is a table. "test1"
contains some a1, a2, a3...
|
by: thatcollegeguy |
last post by:
Below are my 3php and 2js files.
I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |