I have a table named ‘data’ which stores the email addresses and city of users and I want to find the most popular email domain. I use the following query to to find the rows which have maximum value.
exapmple for my table:
Email Name City
dsmks@gmail.com John California
sak@gmail.com Leo sydney
dsnk@gmail.com Ross NY
askd@yahoo.com Ronny Canberra
ajs@yahoo.com Monty London
kasl@yahoo.com Jim washington
uy@hotmail.com Finn Las vegas
I have computed the answer using this query
select x.city, x.No_of_People from (select e.city, count(e.city) as No_of_People from data e group by e.city) x
where x.No_of_People = (select max(x2.No_of_People) from (select e2.city, count(e2.city) as No_of_People from data e2 group by e2.city) x2)
But i don’t wan’t to use limits as it does not return multiple rows. So I have used the following query using this answer
select
x.substring_index (email,'@',-1),
x.No_of_Users
from
(select
e.substring_index (email,'@',-1),
count(e.substring_index (email,'@',-1)) as No_of_Users
from
data e
group by
e.substring_index (email,'@',-1)) x
where
x.No_of_Users =
(select
max(x2.No_of_Users)
from
(select
e2.substring_index (email,'@',-1),
count(e2.substring_index (email,'@',-1)) as No_of_Users
from
data e2
group by
e2.substring_index (email,'@',-1)) x2)
The query that i’m using is giving this error “FUNCTION e2.substring_index does not exist”. help me.
Your syntax is wrong for using the function. The alias goes on the column, not the function. For instance, your last subquery should use this syntax:
I also named the first column, so you can refer to it outside the subquery if you want.
To count the number of occurrences in a string, use this trick: