Write a query to display the highest average obtained from the students - mysql

enter image description here
Data in Mark table:
Value Subject_ID Student_ID
------ ----------- ------------
91 1 1
90 2 1
90 3 1
100 4 9
67 5 9
80 5 4
90 4 4
I have tried the query:
select round(avg(value),2) as avg_mark from Mark;
Expected Output:
enter image description here
I have tried the following code, it executes successfully and gives the desired result, however it fails to clear test case and IDK why?

and gives the desired result
Well.. it might give your desired result but it doesn't give their desired result, 90.33
The question asks for the highest average, which means you need to perform an average per student, then max to find out what the highest average was (Student 1 has the highest average, with 90.33). Perform the AVG in a subquery grouped by student id, then MAX it in an outer query. Perform the rounding in the outer, not the inner (it doesn't matter in this case, but in terms of habit you should strive to perform roundings last - work in as many dp as possible, then round just before output to prevent rounding errors compounding)
I haven't done it for you because this strongly seems like homework. Make an attempt at it using the description i gave above and I'll be happy to help out with anything that's gone wrong

Related

How to sum specific rows and columns in SQL?

pnr mnd pris
1 1 600
1 7 900
2 1 600
2 7 600
3 1 40
3 7 40
I have trouble how to sum specific rows on the columns. Looking at the above, the table is called travel and it has 3 columns:
pnr - Personal Number
mnd - Month
Pris - Price
So what I want is to sum total of the price for the a specific month, so in this case, it should be 1240 USD and month 1. For the month 7, it should be 1540 USD.
I have trouble to do the query correct. So far from I have tried is this:
SELECT t.rnr, t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
The result I get is 3720 USD which I have no idea how the SQL managed to calculate this for me.
Appreciate if someone could please help me out!
For this you need to drop the pnr column from the output (it is not relevant and will cause your data to split) and add a GROUP BY:
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
GROUP BY t.mnd
Live demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b34ec2bb9c077c2d74ffc66748c5c142
(The use of an aggregate function without grouping, as you've got now, is not a standard SQL feature and can often be turned off in MySQL. If turned on, you might not always get the result you expected/intended.)
just group your result with mnd column
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
group by t.mnd

How to retrieve rows with particular number of different column values in SQL?

I am implementing an exam portal. I have teacher and student as users.
Teachers generate question set for particular subject for taking exam. He has 4 options based on exam full marks ( 20 marks, 50 marks, 80 marks and 100 marks ), duration is also fixed during marks selection as 30 min, 60 min, 90 min, and 120 mins.
I have a question table. That has field for question, answer and level(Easy, Medium, Hard). Easy question 1 marks, Medium level question 2 marks and Hard level question 3 marks.
Any number of questions could be added to the set by teacher. And questions will be fetched randomly from database. Also each student should get 6 easy, 4 medium and 2 hard level question in a set of 20 marks, i.e. total 12 questions to be fetched randomly following criteria of levels. Similarly, for 50 marks set, 15 EASY level, 10 MEDIUM level and 3 HARD level questions have to be fetched and so on.
Please avoid all other conditions you think must be present and help me with forming an mysql query or just help me with some clues of what sql clause should I use.
I'd use a union select two times additionally to first query so each select gets a set by question level.
From just the title, I would guess you are looking for
HAVING COUNT(col) = 7

mysql - get the average of the output average

I have 3 table. final,milestone and milestonewp consider that the three tables is foreigned key like milestonewp<--FK--milestone<--FK--Final .Then I have a column for determining the average of the milestonewp for a certain foreign key. Then getting that average to be average again to be displayed to the final table.Here is my visual representation
milestonewp
condition | mile_id
20 1
20 1
30 1
21 2
21 2
31 2
40 3
30 3
50 3
How can I average the average that the chart above will produce?
I'm trying to work on this
select avg(milewp_condition)
from logs_pms_r_milestone_wp
where mile_id=1;
but i dont have any idea how it can produce for the other mile_id
EDIT
The above code will produce something like this
avg(milewp_condition)
0
0
0
so then, i also want to average that 3 rows.
If I understand well this should be what you look for:
SELECT AVG(milewp_condition)
FROM logs_pms_r_milestone_wp
GROUP BY mile_id;
If you want to average all, just do:
SELECT AVG(milewp_condition)
FROM logs_pms_r_milestone_wp;
Regards

Calculate max value of list of numbers with a maximum combination of "x"

ok, i'm not sure if i can explain this right.
Lets say i have a table with three columns (id, price, maxcombo)
maybe there's like 5 rows in this table with random numbers for price. 2. id is just incremental unique key)
maxcombo specified if that price can be in a combination of up to whatever number it is.
If x was 3, i would need to find the combination that has the maximum value of the sum 1-3 columns.
So say the table had:
1 - 100 - 1
2 - 50 - 3
3 - 10 - 3
4 - 15 - 3
5 - 20 - 2
the correct answer with be just row id 1.
since 100 alone (and can only be alone based on the maxcombo number)
is greater than say 50 + 20 + 15 or 20 + 15 or 10 + 20 etc.
Does that make sense?
I mean i could just calculate all the diff combinations and see which has the largest value, but i would imagine that would take a very long time if the table was larger than 5 rows.
Was wondering any math genius or super dev out there had some advice or creative way to figure this out in a more efficient manner.
Thanks ahead of time!
I built this solution to achieve the desired query. However, it hasn't been tested in terms of efficiency.
Following the example of colums 1-3:
SELECT max(a+b+c) FROM sample_table WHERE a < 3;
EDIT:
Looking at:
The correct answer will be just row id 1
...I considered maybe I misunderstood your question, and you want the query just obtain the rowid. So, I made this other one:
SELECT a FROM sum_combo WHERE a+b+c=(
SELECT max(a+b+c) FROM sum_combo WHERE a > 3
);
Which would for sure take too long in larger tables than just 5 rows.

Count the number of times a record appears and place in a bucket, SSRS

More head banging for something that I'm sure is an easy solve. I'm attempting to count the number of accounts that appear in a dataset more than once, but less than 6 times. I can't even get the code to count the number of instances that accounts appear more than 2x's.
123
123
456
456
111
111
111
I should be able to say that the above dataset based on my query = 3. I want to count 123 as 1 instance, 456 as 1 instance and 111 as 1 instance since they all appear greater than or equal to 2 times. After I figure this out, I should be able to get to the point where I can do my buckets. But, I can't seem to breakthrough this part yet.
I have tried this, and it works, but doesn't give the result I want. It gives me the number of records in the data set.
=IIF(count(Fields!AcctID.Value) >= 2, Count(Fields!Instance.Value),0)
I have also tried this, and it also gives me the same result as the code above:
=Count(IIF(Fields!AcctID.Value >= 2, Fields!Instance.Value,0))