Get top 5 foreign keys - mysql

I have a table say City. I have state as a foreign key in it. What query I need to write to get the Top 5 states which occur in the city table.
Eg
Id City State
1 Mumbai Maharashtra
2 Pune Maharashtra
3 Amritsar Punjab
Here the output I expect is to get as names of the states with count
State_Name count
Maharashtra 2
Punjab 1
SELECT p.name, count(pr.State) FROM City pr
join state p on p.Id = pr.StateId;

Use Group by with count aggregate to count the state and use Limit to filter the records
select count(1) Cnt,state
from yourtable
group by state
order by Cnt desc Limit 5

Related

MySql Numbering the rows of query based on group by

I have a table like this
id name city
-------------------------------
1 Ian London
2 John London
3 David New York
4 Sylvia Mumbai
5 Beryl New York
6 Rashan London
I would like to retrieve the data with a row numbering that is grouped on City. Like this.
name city count
-------------------------------
Ian London 0
John London 1
Rashan London 2
Beryl New York 0
David New York 1
Sylvia Mumbai 0
I have been trying with
ROW_NUMBER() OVER (PARTITION BY City ORDER BY name)-1
But the count is of all the items returned, where I would like to count the number of people in each city.
Your code should do what you want - however, you mention group by, which is irrelevant here. The query should just be:
select name, city, row_number() over(partition by city order by name) - 1 rn
from mytable
To get the results that you specify:
select name, city, row_number() over (partition by city, order by name) as count
from t
order by count(*) over (partition by city) desc, -- number of rows in city
city,
name;
You seem to want the cities ordered by the number of names in the city.

LIMIT and Group By? How do I return the lowest 100 earning customers by country? SQL

Hi I’ve a table database1
3 columns : customer_id , income , country
Customer_id
1001
1002
...
Income
5000
6000
7000
Country
SG
HK
VN
...
How do I write a query that returns the lowest 100 earning customers per country?
Is it possible to return:
Customer ID | country code
1003 SG
1004 SG
...
1007 VN
...
So on
Thanks!
On mySQL 8 you can leverage a window function for this:
SELECT * FROM
(
SELECT
country,
customer_id,
row_number() over(partition by country order by income asc) earn_rank
FROM table
)x
WHERE x.earn_rank <= 100
You can conceive that this window function will sort the rows by country then by income, then start counting up from 1. Each time the country changes the row numbering starts over from 1 again. This means that for every country there will be a row numbered 1 (with the lowest income), and a 2, 3 etc. If we then wrap it up in another outer query that selects only rows where the number is less than 101 we get 100 rows per country

AVG() function is displaying average of all employees instead of average of employees in each town per shop

I want to display the average number of employees in each town. Town can have
This is my data_base:
To clarify it's assumed that employers are part-timers so there may be a situation when they are currently not employed for the sake of exercising OUTER JOIN queries. They can also be assigned to more than 1 shop.
I found the way to do it is to use subquery for AVG function. In a subquery, I will use COUNT to count the number of employees for each town and each shop and the main query will get AVG of those numbers grouping by the town. However while logically I feel the query should be working, it is displaying wrong data. Below is a query that I created.
SELECT s.Town, AVG(a.cnt) AS `Number of employees` FROM
(SELECT COUNT(k.EmpId) AS cnt FROM `Shops and Employees` k
INNER JOIN Shops s ON s.ShopId = k.ShopId
GROUP BY s.Town) AS a, shops s
GROUP BY s.Town
I expected to see something like this:
Town name | AVG of employees per town
-------------------------------------
town1 | 3
town2 | 5
town3 | 1
town4 | 4
Instead I get this:
Town name | AVG of employees per town
-------------------------------------
town1 | 2
town2 | 2
town3 | 2
town4 | 2
Basically what I get is all employes summed divided by the number of towns. I want an average of employees for each town e.g. in town1 I have 2 shops. Shop1 has 2 employees while shop2 has 4 employees. An average for town1 should be 3.
It looks like your problem is that you're not grouping the data by shop in your employee count subquery, so that query is returning the total number of employees per town. Also, you don't need to group by town in that subquery. Without the complete table structure and sample data it's hard to be certain, but I think this is what you really want:
SELECT s.Name as `Town Name`, AVG(se.cnt) AS `AVG employees per shop`
FROM Shops s
JOIN (SELECT ShopId, COUNT(*) AS cnt
FROM `Shops and Employees`
GROUP BY ShopId) se ON se.ShopId = s.ShopId
GROUP BY s.Name

How to select one column with all distinct values based on some clause

I essentially like to have one query which I'll execute one time and like to have the result (no multiple query execution) and definitely, the query should use simple MySQL structure (no complex/advanced structure to be used like BEGIN, loop, cursor).
Say I've two tables.
1st Table = Country (id(PK), name);
2nd Table = Businessman (id(PK), name, city, country_id(FK))
Like to SELECT all countries, whose businessmen are from distinct cities. No two businessmen exist in one country, who are from the same city. If so, that country will not be selected by the SELECT clause.
Country
id name
1 India
2 China
3 Bahrain
4 Finland
5 Germany
6 France
Businessman
id name city country_id
1 BM1 Kolkata 1
2 BM2 Delhi 1
3 BM3 Mumbai 1
4 BM4 Beijing 2
5 BM5 Paris 6
6 BM6 Beijing 2
7 BM7 Forssa 4
8 BM8 Anqing 2
9 BM9 Berlin 5
10 BM10 Riffa 3
11 BM11 Nice 6
12 BM12 Helsinki 4
13 BM13 Bremen 5
14 BM14 Wiesbaden 5
15 BM15 Angers 6
16 BM16 Sitra  3
17 BM17 Adliya 3
18 BM18 Caen 6
19 BM19 Jinjiang 2
20 BM20 Tubli 3
21 BM21 Duisburg 5
22 BM22 Helsinki 4
23 BM23 Kaarina 4
24 BM24 Bonn 5
25 BM25 Kemi 4
In this respect, China and Finland shouldn't be listed.
I've attempted using count and group by, but no luck.
Can you please help me to build up this query.
Here it is, all you need is to join Businessman table and count cities and distinct cities and if they equal that means all businessmen are from different cities:
SELECT
c.`id`,
c.`name`,
COUNT(b.`id`) AS BusinessmanCount,
COUNT(b.`city`) AS CityCount,
COUNT(DISTINCT b.`city`) AS DistinctCityCount
FROM `countries` c
INNER JOIN Businessman b ON c.`id` = b.`country_id`
GROUP BY c.`id`
HAVING CityCount = DistinctCityCount
For minified version what you exactly need:
SELECT
c.`id`,
c.`name`
FROM `countries` c
INNER JOIN Businessman b ON c.`id` = b.`country_id`
GROUP BY c.`id`
HAVING COUNT(b.`city`) = COUNT(DISTINCT b.`city`)
Well, I think we should have waited for you to show your own query, because one learns best from mistakes and their explanations. However, now that you've got answers already:
Yes, you need group by and count. I'd group by cities to see if I got duplicates. Then select countries and exclude those that have duplicate cities.
select *
from country
where id not in
(
select country_id
from businessmen
group by city, country_id
having count(*) > 1
);
You need either nested aggregations:
select *
from Country
where id in
(
select country_id
from
(
select city, country_id,
count(*) as cnt -- get the number of rows per country/city
from Businessman
group by city, country_id
) as dt
group by country_id
having max(cnt) = 1 -- return only those countries where all counts are unique
)
Or compare two counts:
select *
from Country
where id in
(
select country_id
from Businessman
group by country_id
having count(*) = count(distinct city) -- number of cities is equal to umber of rows
)

How to group by on a highest value

So, for example i've got the following table;
ID COUNTRY VALUE
--------------------- -------------------- --------------------
1 India 12000
2 India 11000
3 UK 11000
4 India 15000
5 Canada 11000
And I would like to group by Country but only have the country with the highest value show up, if I would just use a group by query like:
SELECT * FROM countries GROUP BY country
I would get;
ID COUNTRY VALUE
--------------------- -------------------- --------------------
1 India 12000
3 UK 11000
5 Canada 11000
Where the value for india would be 12000. I would like the query to group on the highest value for the group by on country like:
ID COUNTRY VALUE
--------------------- -------------------- --------------------
3 UK 11000
4 India 15000
5 Canada 11000
So it's grouped on the highest value which is 15000.
DEMO
SELECT s1.ID, s1.COUNTRY, s1.VALUE
FROM countries s1
LEFT JOIN countries s2
ON s1.VALUE < s2.VALUE
AND s1.COUNTRY = s2.COUNTRY
WHERE s2.COUNTRY IS NULL;
OUTPUT
NOTE: But be carefull of ties. In that case you can get one random from those ties.
You can use the MAX aggregate function.
select
country,
max(value) value
from countries
group by
country
See the live example.
Edit: The original solution was only correct due to the nature of the data. I've removed the ID from the first query, to correct the mistake. Here is another solution (based on #Juan Carlos Oropeza's work - thank you) that will return the ID and eliminate the ties.
select
min(x.id) id,
x.country,
x.value
from (
select
c.*
from countries c
left join countries c1 on c.value < c1.value and c.country = c1.country
where c1.country is null
) x
group by
x.country,
x.value
;
See the live example - I've modified the data to cover edge cases mentioned above.