I have got a mysql table containing data about companies: id, company name, city, address, telephone.
I'm trying to write a query for getting list of cities where there are more then 10 companies within.
Is it possible to achieve that?
Try
select city, count(*) as nbPerCity from tableName group by city having nbPerCity > 10;
select city from Table group by city having count(company_name) > 10 ;
or
select s.city from
(select city,count(company_name) as counted from Table group by city) as s
where s.counted > 10
Related
SELECT Name, MAX(Population) as Population
FROM County
GROUP BY Name;
Question: This just returned 16 rows... I want to return only the ONE county which has the highest population, showing 1 row with with the name of that county and its population,, not the highest population for every county. How do I fix this?
Thanks in advance.
Order and take first
SELECT Name,
Population
FROM County
ORDER BY Population DESC LIMIT 1
with data (Population, Name) as(
Select '12345' ,'c1' union all
Select '1234500001' ,'c2' union all
Select '1234500002' ,'c3' union all
Select '12346' ,'c4' union all
Select '1234600001' ,'c4' union all
Select '1234600002' ,'c4' )
SELECT Name, Population
FROM data
where Population = (SELECT MAX(Population) FROM data)
;
I have a table with City and ComplaintType.
I am trying to create a normalization column that has the following computation:
(pseudo) select number of a particular type in a particular city) / (number of all complaints in a particular city
I currently have the following SQL:
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac,
count(*) AS count_freq,
(SELECT count(City) FROM data GROUP BY City) AS count_city
FROM data
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
Which gives me the following table:
The total complaints in a city (count_city) is incorrect. However, when I run the count_city query on it's own, the counts are correct and give the following output:
How do I correctly get my city_count associated with the number of x complaints by city so I can compute the correct fraction?
Cold hard numbers example:
Bronx & Hot Water = 79690
Bronx (total complaints) = 579363
complaint_frac = 79690 / 579363 = 0.13754761695
correlate your subquery in your main table.
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac,
count(*) AS count_freq,
(SELECT count(d1.City) FROM data d1 WHERE d1.City = d2.City GROUP BY d1.City) AS count_city
FROM data d2
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
You don't need subqueries for this, at least in MySQL 8+; window functions do the work:
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / sum(count(*)) over (partition by city) as complaint_frac,
count(*) as count_freq,
sum(count(*)) over (partition by city) as count_city
FROM data
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
I'm trying to display only zipcodes that multiple people have in my table and sort it by ascending order.
I have tried the following but it does not work.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
SELECT zipcode AS "zipcodes", count(1) as ZIP_COUNT
FROM people
GROUP BY zipcode
ORDER BY ZIP_COUNT ASC;
If you only need zipcode that appears 2+ times append (before the order) :
HAVING COUNT(1) > 1
Or (if Mysql supports aliases in HAVING clause) :
HAVING ZIP_COUNT > 1
The main issue with your query is that you didn't specify any column in the order by sections.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
So just add the zipcode to it and it will be good.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY zipcode ASC;
The following query selects zipcode(s) having multiple entries in table "people":
SELECT zipcode AS "zipcodes"
FROM people
GROUP BY zipcode
HAVING COUNT(*) > 1
ORDER BY zipcode ASC;
try this one
SELECT zipcode AS "zipcodes", count(zipcode) as noofzip
FROM people
GROUP BY zipcode
having count(zipcode)>1
order by zipcode
I'm new to mysql and I have two correctly working select statements with longest and shortest names. I can't really understand how can I join them in one statement. Can someone help, please.
Here are the queries:
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name) DESC, name
LIMIT 1;
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name), name
LIMIT 1;
The result I'm looking for is:
Country NameLength
----------------------------------------------------------
South Georgia and the South Sandwich Islands 44
Chad 4
Cuba 4
You use UNION:
SELECT Country, NameLength FROM (
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1;
UNION
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1; )a
ORDER BY NameLength DESC, Country
Table structure:
Locations: id, name, city
I want to select all the duplicates by name AND city, since multiple locations can exist with same name in each city.
select name, city
from locations
group by name, city
having count(*) >= 2