Ok, simple question: I have a table
create table people (
id int auto_increment not null primary key,
name varchar(32),
city varchar(32),
age int
);
with the query
SELECT city, age
FROM people
WHERE age=(SELECT max(age) from people);
I can get the output of the city and age of the oldest person.
City Age
Denver 95
How do I get the max age per city, i.e.
City Age
Atlanta 90
Cincinnati 87
Denver 95
SELECT city, MAX(age)
FROM people
GROUP BY city
This is a use of MySQL aggregate functions. By providing an additional column and specifying it in the GROUP BY, you are telling MySQL to only apply the aggregation function (in our case MAX) within those matching records.
You can read more about aggregates here.
Your query can be simplified like
SELECT city, age
FROM people
order by age desc
limit 1
Related
Table name: Offices
CompanyID OfficeLocation
01 USA
01 Africa
03 USA
04 USA
04 Israel
04 Germany
05 Africa
I need to get CompanyID that have most records in this table (04 for table above)
I tried:
SELECT CompanyID
GROUP BY CompanyID
HAVING COUNT(*) = (
SELECT COUNT (*) FROM EntitySite
GROUP BY CompanyID
ORDER BY COUNT(*) DESC
)
Dsnt work at all(
You can use next simple query:
SELECT CompanyID, COUNT(OfficeLocation) CountOfficeLocation
FROM EntitySite
GROUP BY CompanyID
ORDER BY CountOfficeLocation DESC
LIMIT 1;
MySQL query online
Firstly, question is abstract, data has no obvious relation (different id's have different countries), no know requirement what id is considered when counting (first, last or random).
However, here is query from what I get, this will return most common first and least common last.
SELECT CompanyID
FROM
(
SELECT count(*) as c,
CompanyID
FROM Offices
GROUP BY CompanyID
) T
ORDER BY c desc
I would like to get statistics on my dataset,
for example this is my dataset:
FirstName LastName Country City BirthMonth
Donald Trump England London Jan
Bill Gates England London Sep
Donald Suther England York Sep
Donald Suther Germany Berlin Jan
and this is my 'group_by' list:
[['FirstName', 'LastName'], ['Country', 'City'], ['BirthMonth']]
I would like to get the following statistics:
group by FirstName & LastName:
FirstName LastName Count
Donald Trump 1
Donald Suther 2
Bill Gates 1
group by Country & City:
Country City Count
England London 2
England York 1
Germany Berlin 1
group by BirthMonth:
BirthMonth Count
Jan 2
Sep 2
my query would look like this:
select FirstName, LastName, Country, City, BirthMonth
from my_table
where <some conditions to filter rows only from certain timestamp>
now I have two options:
a. return all values to server and process there (I'm using python) - this query includes many rows and causes overhead
b. query multiple times, each time grouping by the specific fields
select FirstName, LastName, count(*) as group_by
from my_table
where ...
group by FirstName, LastName
c. is there a third option of having one query, and return all different 'group_by's?
and another question:
from (a) and (b) which is better?
I'll note that the group by has limited options - for example 'FirstName' has only 20 options, this means that (b) will result each time less than 20*(num of group by in query rows) < 40, but (a) will result 20^5 rows which is tons of data
to simplify I'll assume there aren't more than 2 columns in a group_by each time,
(which is actually the current situation, it might grow in the future but currently I can use a solution that takes that into account)
You can combine each grouped query with the others using UNION. Fill in the unused columns in each subquery with NULL.
SELECT FirstName, LastName, NULL AS Country, NULL AS City, NULL AS BirthMonth, COUNT(*) AS count
FROM my_table
GROUP BY FirstName, LastName
UNION ALL
SELECT NULL, NULL, Country, City, NULL, COUNT(*)
FROM my_table
GROUP BY Country, City
UNION ALL
SELECT NULL, NULL, NULL, NULL, BirthMonth, COUNT(*)
FROM my_table
GROUP BY BirthMonth
Below are the 2 tables:
Employee(id, name, salary, dept_id, supervisor_id)
Department(dept_id,name)
Write a query IN MySQL or Oracle:
Select all the names of supervisor whose age is greater than 60.
Select all the names of the department where at least 1 Employee is there.
I don't see age column.(not enough information)
select distinct(name) from Department where dept_id in(select dept_id from Employee);
I'm having a hard time wording what I need/wording the search result, so apologies if this is a stupid question/has been answered before. I'm trying to write a query in SQL for a table such as below:
Country Unique_ID
US 123
US 124
UK 125
Australia 126
That will output the follow table:
Country Count_Distinct
US 2
UK 1
Australia 1
All 4
I know I can select the countryid and count distinct the country codes, and I know I can just count distinct the countryid codes to get the "All" number. I can't figure out how to write a query to get the follow output that's not two separate queries.
If you need information or clarification please let me know. Thanks!
Use WITH ROLLUP:
select Country, count(distinct Unique_ID) Count_Distinct
from mytable
group by Country
with rollup
If you want the text "All" (you get a null for the country by default), wrap it in another query to change the null to "All":
select coalesce(Country, "All") Country, Count_Distinct
from (
select Country, count(distinct Unique_ID) Count_Distinct
from mytable
group by Country
with rollup
) x
Can you please try this :
select country,count(distinct unique_id) as count distinct
from table
group by rollup(country)
i.e. a table called students (Assume there is only one student with that name, e.g. there isnt two students called John Smith) with
studentName, studentScore, subject
assume table has this
John Smith 40 maths
bob grey 20 english
anne hank 23 english
John Smith 30 english
anne grey 10 maths
I am trying to find the most highest scored student by calculating the maximum of averages of all students
this here will select the highest average but not the student name with that average:
SELECT MAX(avgStudentScore)
FROM (SELECT AVG(studentScore) AS avgStudentScore FROM students GROUP BY studentName) t
thx
If all you need is the name, you can do something like this:
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
order by avgStudentScore desc
limit 1
This will return only the first row of the query. Since it's ordered by avgStudentScore, it will return the student with the top average.
The above solution is the simplest and fastests, but it's not the only one. If you want to do it "the hard way" (with subqueries), what you need to do is:
Calculate the average for each student
Get the highest average
Filter the student with the highest average
So... let's do it the hard way ;)
select a.studentName
from
(
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
where
a.avgStudentScore = (
select max(avgStudentScore)
from (
select avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
)
Notice that with this approach the final result might not be unique (i.e. there may be one ore more students with the same average and that average is the highest).
Use this query:
SELECT studentName, max( mark ) as maxMark FROM `student` GROUP BY studentName