I have a mysql table having register number, dob, student name, sex, total mark in a semester in each row.
I would like to get records of 10 Boys and 5 girls in descending order of total marks in a single SQL query.
My MySQL dialect is rusty but this should do the trick
(SELECT * FROM Students WHERE sex = 'Male' ORDER BY TotalMarks DESC LIMIT 10)
UNION ALL
(SELECT * FROM Students WHERE sex = 'Female' ORDER BY TotalMarks DESC LIMIT 5)
It's a single query, mind you.
Try this code:
select
a.studentID,
a.studentName,
a.dob,
a.totalMark
from
(
select
studentID,
studentName,
dob,
totalMark
from
students
where
sex='M'
order by
studentMark desc
limit 10
union all
select
studentID,
studentName,
dob,
totalMark
from
students
where
sex='F'
order by
totalMark desc
limit 5
)
order by
totalMark desc
try this
select * from
(select reg_number, dob, student_name, sex, total_marks where sex='male'
order by total_marks desc limit 10
union
select reg_number, dob, student_name, sex, total_marks where sex='female'
order by total_marks desc limit 5) a order by total_marks desc
Related
I am using PHP MyAdmin. I want the result of this query to be Willard Wildcats and 7, but I can't figure out how to do it by using the max function rather than the order by and limit functions. I need to use the max function
Select Team_Name, Count(*)
From Number_3
Group By Team_Name
The result of this query should be
Willard Wildcats and 7
Use LIMIT clause :
SELECT Team_Name, Count(*) AS CNT
FROM Number_3
GROUP BY Team_Name
ORDER BY CNT DESC
LIMIT 1;
If you are limited with LIMIT clause then use subquery :
SELECT Team_Name, Count(*) AS CNT
FROM Number_3
GROUP BY Team_Name
HAVING Count(*) = (SELECT MAX(CNT)
FROM (SELECT Team_Name, Count(*) AS CNT
FROM Number_3
GROUP BY Team_Name
) t
);
If you are working latest version of MySQL then it will be more easier with ranking function :
SELECT t.*
FROM (SELECT Team_Name, Count(*) AS CNT,
DENSE_RANK() OVER (ORDER BY Count(*) DESC) AS Seq
FROM Number_3
GROUP BY Team_Name
) t
WHERE Seq = 1;
You need to write where condition and select a column using max function
Select Team_Name, Count(*) From Number_3 where Team_Name=(select max(Count(*)) from Number_3)
I have a Table Name Called tbl_events and it does have following columns
Id,Event_Name, District,Branch_Type,Points
I need a Sum of Points Column Where Branch_Type=2; and Sum of Points Divided by 10 Where Branch_Type=2 after that I have to Add those Two Values And Group that Result by District and Order by Desc. I tried this Query but Seems to something wrong Can anyone help, please?
Select (t1.B_Points + t2.D_Points) as T_Points,District From
(Select Sum(Points)*.1 as B_Points ,District From tblstudents Where Branch_Type=3 group by District)t1
Left Join(Select Sum(Points) as D_points, District From tblstudents Where Branch_Type=2 group by District)t2 on
(t1.District=t2.District) Order by Desc
You need to add column alias T_Points in order by
Select (t1.B_Points + t2.D_Points) as T_Points,District
From
(
Select Sum(Points)*.1 as B_Points ,District From tblstudents
Where Branch_Type=3 group by District
)t1
Left Join
(
Select Sum(Points) as D_points, District From tblstudents
Where Branch_Type=2 group by District
)t2 on t1.District=t2.District
Order by T_Points Desc
You seem to just want conditional aggregation:
select district,
sum(case when branch_type = 3 then 0.1 * points
when branch_type = 2 then points
else 0
end) as t_points
from tblstudents
group by district;
If you want to order by the points descending, then add:
order by t_points desc
Note: This assumes that you want districts that have neither branch type. If that is not an issue, move the logic to the where clause:
select district,
sum(points * (case when branch_type = 3 then 0.1 else 1.0 end) as t_points
from tblstudents
where branch_type in (2, 3)
group by district
order by t_points desc;
i have tried a lot without success, i want to display only the maximum average of student marks from the list of student as shown on the table below.
My table
i want to get the result as below
expected output
what i have done so far
SELECT MAX(a.Total_Qty) As TotalMax,a.studentId
FROM(
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId) AS a
Inner query will give you the list of averages for each student.
Then we order (descending) by their average score and finally we get the top 1 (Limit 1)
SELECT a.studentId, a.Total_Qty as MaxAvg
FROM(
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId)
AS a
Order by a.Total_Qty Desc
Limit 1
Alternatively:
SELECT AVG( s.marks ) AS Total_Qty,s.studentId
FROM results s
WHERE s.stream = 'Form One'
GROUP BY s.studentId
Order By AVG( s.marks ) Desc
Limit 1
(UNTESTED) I hope it helps
if you are using MSSQL :
SELECT TOP(1) studentId, AVG(marks) FROM results GROUP BY studentId
ORDER BY MAX(AVG(marks)) Desc
if you are using SQL :
SELECT studentId, AVG(marks) FROM results GROUP BY studentId
ORDER BY MAX(AVG(marks)) Desc Limit 0,1
I have a table with 3 columns:
Name department salary
How can I determine using one query to find 3rd highest salary in each department?
One way is to LIMIT a correlated subquery, but it's not especially efficient:
SELECT department, (
SELECT salary
FROM my_table t2
WHERE t2.department = t1.department
ORDER BY salary DESC
LIMIT 2, 1
)
FROM my_table t1
GROUP BY department
In addition to eggyal's excellent answer, here's a query that will give you the names, too, of those that have salary equal to the third (in each department):
SELECT
t.name, t.department, t.salary AS third_salary
FROM
( SELECT DISTINCT department
FROM tableX
) AS d
JOIN
tableX AS t
ON t.department = d.department
AND t.salary =
( SELECT tt.salary -- notice that this
FROM tableX AS tt -- subquery is
WHERE tt.department = d.department -- exactly the same as
ORDER BY tt.salary DESC -- the one in
LIMIT 1 OFFSET 2 -- #eggyal's answer
) ;
This RANK question is similar to this one:
MySQL, Get users rank
I you can thy this:
SELECT s.*,
(
SELECT COUNT(*)
FROM salaries si
WHERE si.salary >= s.salary AND si.department = s.department
) AS rank
FROM salaries s
WHERE s.rank = 3
Try this:
SELECT name, department, salary
FROM (SELECT name, department, salary, IF(#dept=(#dept:=department), #auto:=#auto+1, #auto:=1) indx
FROM employee e, (SELECT #dept:=0, #auto:=1) A
ORDER BY department, salary DESC ) AS A
WHERE indx = 3;
My current query reads:
SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM `entry_rate`
WHERE 1
GROUP BY entry_id
cat_id relates to different categories: 1, 2, 3 or 4
Is there a way I can find the maximum average for each user in each category without setting up an additional table? The return could potentially be 4 maximum avg_rate for each user_id
Visit the link below for example:
http://lh5.ggpht.com/_rvDQuhTddnc/S8Os_77qR9I/AAAAAAAAA2M/IPmzNeYjfCA/s800/table1.jpg
May not be the most efficient way:
select user_id, cat_id, MAX(avg_rate)
FROM (
SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM entry_rate
GROUP BY entry_id, user_id, cat_id) t
GROUP BY user_id, cat_id
You can make this statement.
SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM 'entry_rate'
GROUP BY entry_id
order by AVG( rating ) DESC
limit 1
this make the result order by avg(rating) and select the first row. and can make the limit 1,1 to select the second max element
SELECT s.user_id,s.cat_id,max(s.avg_rate) FROM (
SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM entry_rate
GROUP BY entry_id,user_id,cat_id) as s
GROUP BY s.user_id,s.cat_id
SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate
FROM entry_rate
GROUP BY entry_id
order by avg_rate desc limit 1;