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
Related
In task below
Hackerrank
i did that query
SELECT CITY ,length(CITY ) from STATION where length(CITY )in(
select max(length(CITY ))from STATION union select min(length(CITY ))from STATION )Group by length(CITY )
order by length(CITY ) desc,CITY ;
i recieved Error
also i make another query like that
Select max(length(CITY)),CITY from STATION Group by length(CITY) Order by length(CITY)Desc;
and recieved that error
Error
The question phrases as:
Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
You don't really need aggregation to achieve this task.
I think this would be simpler expressed with union all and two row-limiting queries (one that sorts cities by ascending length, the other by descending length):
(
select city, char_length(city) cnt_chars
from station
order by char_length(city), city limit 1
) union all (
select city, char_length(city)
from station
order by char_length(city) desc, city limit 1
)
If there is only one city in the table (or if all cities have the same length), then union might be more appropriate than union all (so the same city does not appear twice in the resultset).
Alternatively, if you are running MySQL 8.0, you can use window functions:
select city, cnt_chars
from (
select s.*,
row_number() over(order by char_length(city), city) rn_asc,
row_number() over(order by char_length(city) desc, city) rn_desc
from station s
) s
where 1 in (rn_asc, rn_desc)
Side note: char_length() is safer than length(). The former counts characters, while the latter counts bytes (if your strings have multi-byte characters, this is not what you want).
You are trying to select CITY name and length(CITY) but grouping only by length(CITY). In case when two or more cities have the some value for length(CITY) mysql does not know which of the CITY should be selected.
I need to retrieve data from a view. View will have details such as country, location_id, content_id, content_url, content_likes and .... I need to retrieve location_id which has max(content_likes) grouped by country order by sum(content_likes) desc.
Right now I am getting the correct data based on country side, but Id I am getting which is of default order. But instead of default ordered id I need to get Id which has maximum likes.
My current query is
select * from <view_name> group by country order by sum(content_likes) desc;
Data From View:
Result :
You seem to want something like:
select country_id, location_id, sum(content_likes)
from view_name vn
group by country_id, location_id
having location_id = (select vn2.location_id
from view_name vn
where vn2.country_id = vn.country_id
group by location_id
order by sum(content_likes) desc
limit 1
);
You can try this (sorry any syntax error):
select cc.*
from
(select aa.location_id, aa.country /*[, add fields if you need them...]*/,
rank() over (partition by aa.country_id order by aa.content_likes) ranking_field
from <view_name> aa
join (select country, sum(content_likes) sum_content_likes
from <view_name>
group by country) bb
on aa.country=bb.country) cc
where ranking_field=1
order by cc.sum_content_likes desc
This returns only the location with max likes in each country ordered by the total likes in each country
EDIT:
With your new example, perhaps you can do simply this:
SELECT *
FROM <<view>> aa
JOIN(SELECT country, MAX(content_likes) max_likes
FROM <<view>>
GROUP BY country) bb
ON aa.country=bb.country
AND aa.content_likes=bb.max_likes
It is two pass query but give your example result. It can return more than one row for country if more than one location has same likes number and those are the max likes in that country.
Hope this help you.
My issue resolved with below sql
select t.location_id,t.content_likes from
(select country,max(content_likes) as mcl,sum(content_likes) as scl from test_eresh
group by country) x,
test_eresh t
where t.country = x.country
and t.content_likes = x.mcl
order by x.scl desc
I'm wondering how one would sum the results from a query?
I want to know how many people live in total in the three biggest cities in Norway. I'm using mysql, the world.sql sample database in mysql workbench.
This is the closest I've gotten
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
There's a few problems here namely this gives me three results instead of one, and while using LIMIT which actually limits how many results it gives, not how many it uses.
Any ideas?
You would use a subquery:
SELECT SUM(population)
FROM (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
) cp
simply sum the result:
select sum(population) from (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) t1
select sum(population) from (SELECT population FROM city WHERE
CountryCode = 'NOR' ORDER BY population DESC LIMIT 3) temp
Read on subqueries.
Make your current query a subquery and get sum from your subquery.
SELECT SUM(population) FROM (
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) p
You query will now act as a virtual table, from which you can you can write a select query to get the sum
I have a query where I want to get the 20 most popular locations from a table, and then order them alphabetically. I'm wondering if there's a cleaner or more efficient way to do this?
SELECT
city
FROM (
SELECT
city,
count(*) AS cnt
FROM locations
GROUP BY city
ORDER BY cnt DESC
LIMIT 20
) s ORDER BY city;
Slightly cleaner:
SELECT city FROM (
SELECT city FROM locations
GROUP BY city
ORDER BY count(*) DESC
LIMIT 20
) s ORDER BY city
You don't need to retrieve the count(*) if you're not going to use it.
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