Find who gets the maximum salary in each location [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two table as follows:
employ_offical:
id name salary
1 raj 5000
2 ram 8000
3 balu 3000
4 david 4000
employee personal:
name location
raj india
ram china
balu india
david china
From these two tables, I need to know how to retrieve who gets maximum salary in every location; in one location one person should come under these category.
How to retrieve who gets maximum salary in every location?

Here is one way to solve this:
select p.location, p.name, o.salary
from employ_offical o,
employee_personal p
where o.name = p.name
and o.salary = (
select max(o2.salary)
from employ_offical o2,
employee_personal p2
where o2.name = p2.name
and p2.location = p.location);
There is a SQL Fiddle here.
Please note that I have joined on name here as it is the only join available, but from a design perspective it would be much better to have id in both tables and join on id.

This should work:
SELECT eo.name, ep.location, eo.salary
FROM employ_offical eo
INNER JOIN employee_personal ep ON eo.name = ep.name
WHERE eo.salary = (SELECT MAX(salary)
FROM employ_offical eo1
INNER JOIN employee_personal ep1 ON eo1.name=ep1.name
WHERE ep.location = ep1.location
)
It might not be the most efficient or pretty way to do this though.
Note that if multiple persons in the same location share the max salary for that location this query will return all of them.

Related

How to SUM the data properly? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Have issue I cannot sum data in the right way. Do not know what is the issue and why query is not summing all the info.
Here is the query :
select m.store, SUM(s.salePrice) as Price ,s.Date, d.name
from market m
inner join distributor d on d.marketId = m.marketId
inner join sales s on s.distributorId = d.distributorId
group by s.Date,m.store,d.name
Outcome of the query :
store | price | date | distributor |
----------------------------------------------------
Store MAX -10000 22-05-2019 RedBull
Store MAX 25000 22-05-2019 RedBull
Store Z -2000 22-05-2019 RedBull
Store Z 15000 22-05-2019 RedBull
What I want as outcome :
store | price | date | distributor |
----------------------------------------------------
Store MAX 15000 22-05-2019 RedBull
Store Z 13000 22-05-2019 RedBull
Why SQL did not include (-) operators in SUM function?
If you can help please advice, thanks!
This would be occurring because something is not the same:
The store names are not the same, but look similar.
The dates are not the same. Perhaps they have a time component, and that is not showing.
The distributor is not the same.
I am guessing that the store names are okay, because they are looked up in the table. You can check if only using that works by using aggregation functions on the other columns:
select m.store, SUM(s.salePrice) as Price,
max(s.Date), max(d.name)
from market m join
distributor d
on d.marketId = m.marketId join
sales s
on s.distributorId = d.distributorId
group by m.store;
If this does not produce duplicates, then you know that store is okay and you can check the other columns. When you find them, you will need to investigate the values to figure out how to fix them.
First replace the d.name to distributor from the "select" and "group by" as I do not see "name" field from your output.

Need a Query ( u can use cross join there) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Here table name is cricket and column is team:
team
----
IND
BAN
PAK
SRI
I need a query which can show the result like this:(see down table)
team_a team_b
------ ------
IND BAN
IND PAK
IND SRI
BAN PAK
BAN SRI
PAK SRI
Can anyone help me with that?
You need a CROSS JOIN with a self join on the table.
select a.team as team_a
, b.team as team_b
from cricket a
cross join cricket b
where a.team != b.team
/
The trick is the WHERE clause, which prevents joining the same team, because obviously IND can't play IND.
If you don't want to generate reciprocal pairs of matches then change the WHERE criteria to use less than instead of equality:
select a.team as team_a
, b.team as team_b
from cricket a
cross join cricket b
where a.team < b.team
/
Here is a SQL Fiddle demo.

SQL Query combination [closed]

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 years ago.
Improve this question
I have following data on table User.
EMPID FIRSTNAME LASTNAME MANAGER
---------------------------------------
10017 Pawan Kumar 3448
3448 Anwar Sadad 1088
1088 Anand R 3525
I have 3 queries
SELECT * FROM USERS WHERE EMPID='10017';
SELECT FIRSTNAME,LASTNAME,MANAGER FROM USERS WHERE EMPID='3448';
SELECT FIRSTNAME,LASTNAME FROM USERS WHERE EMPID='1088';
Please help me combine these three query or a new query where I can select first and last name of the Manager of employ with empid 3448 who is the manager of 10017.
How can I select 1088 (Anand R) who is the Manager two level manager of 10017 (Pawan Kumar)
answer to your question will be this quesry if i have got your question right
SELECT FIRSTNAME,LASTNAME
FROM USERS
WHERE EMPID=(SELECT MANAGER
FROM USERS
WHERE EMPID=(SELECT MANAGER
FROM USERS
WHERE EMPID='10017'));
if you want to select the manager and again manager of employee with empid 10017

Approver and Submitter Same Person in SQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to identify situations in a vacation booking database where an approver and submitter are the same person. Data looks like this:
TIME VACATION BOOKING ACTION NAME
1:00:00 1 SUBMIT Mike
1:01:00 1 APPROVE Mike
1:02:00 2 SUBMIT Jane
1:03:00 2 APPROVE Mike
Is "Count" the most efficient way to do this in SQL Queries?
I would want to "catch" the Mike results in Vacation Booking 1 above.
You could use count, but I would prefer a self-join
SELECT * FROM Bookings B1
INNER JOIN Bookings B2
ON B1.[Vacation Booking]= B2.[Vacation Booking]
AND B1.Action = 'SUBMIT'
AND B2.Action = 'APPROVE'
AND B1.Name = B2.Name

I need to use a COUNT operation in Access SQL

I have the following tables:
PATIENT PRACTICE
- PATIENT_ID - PRACTICE_ID
- PATIENT_NAME - PRACTICE_NAME
- PRACTICE_ID
These tables keep track of patients who attend medical practices. I need to use a COUNT operation to count the number of patients that each practice has, then order the results by ascending order of number of patients.
The following is the desired output:
PracticeName NumberOfPatients
North Medical 3
East Medical 4
South Medical 5
West Medical 6
I have tried this so far, but it doesn't count it as I expected:
SELECT BRANCH.BRANCH_NAME, COUNT(EMPLOYEE.EMP_NUM)
FROM EMPLOYEE, BRANCH
GROUP BY BRANCH.BRANCH_NAME, EMPLOYEE.EMP_NUM;
This is a pretty trivial question and you should have done some rudimentary research before asking it, which is why someone voted your question down.
For the sake of helping the new guy out, here is a query that will give you those results.
SELECT pr.Practice_Name, COUNT(pa.Patent_ID) as NumberOfPatients
FROM practice pr INNER JOIN patient pa ON pr.practice_id = pa.practice_id
GROUP BY pr.practice_name
ORDER BY COUNT(pa.patientid)
You seem to be rather new to SQL, so your question is a little bit fuzzy. Try this:
select pra.practice_name,
count(pat.patient_id) as NumberOfPatients
from patient as pat inner join practice as pra on (pat.practice_id = pra.practice_id)
group by pra.practice_name
And learn the basics of joining tables to get a better understanding of SQL.