Retrieving the Top Five Students : Limits « Select Clause « SQL / MySQL






Retrieving the Top Five Students

  
/*

mysql> Select * from StudentExam;
+-----------+------+------------+
| StudentID | Mark | Comments   |
+-----------+------+------------+
|        10 |   46 | Java       |
|        10 |   65 | C#         |
|        10 |   79 | JavaScript |
|        11 |   66 | Java       |
|        11 |   85 | C#         |
|        11 |   99 | JavaScript |
+-----------+------+------------+
6 rows in set (0.01 sec)

mysql> /* Real command */
mysql> SELECT StudentID, AVG(Mark) AS AverageMark
    -> FROM StudentExam
    -> GROUP BY StudentID
    -> ORDER BY AverageMark DESC
    -> LIMIT 0, 5;
+-----------+-------------+
| StudentID | AverageMark |
+-----------+-------------+
|        11 |     83.3333 |
|        10 |     63.3333 |
+-----------+-------------+
2 rows in set (0.02 sec)


*/
/* Create table */
Drop TABLE StudentExam;

CREATE TABLE StudentExam (
   StudentID  INT NOT NULL,
   Mark       INT,
   Comments   VARCHAR(255)
   
)TYPE = InnoDB;

/* Insert data */
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,46,'Java');
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,65,'C#');
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,79,'JavaScript');

INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (11,66,'Java');
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (11,85,'C#');
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (11,99,'JavaScript');

Select * from StudentExam;

/* Real command */
 
SELECT StudentID, AVG(Mark) AS AverageMark
FROM StudentExam
GROUP BY StudentID
ORDER BY AverageMark DESC
LIMIT 0, 5;

           
         
    
  








Related examples in the same category

1.Limit the query results: Get 3 Most Expensive Products
2.Use LIMIT in SELECT
3.Use LIMIT
4.Another ORDER BY and limit
5.Simple LIMIT
6.ORDER and limit
7.Limits and order
8.Scrolling through the entire product table four records at a time
9.Determining the Number of Records Suppressed by LIMIT (SQL_CALC_FOUND_ROWS, FOUND_ROWS)
10.Limiting a Selection Using LIMIT
11.Use ORDER BY to sort the result set. Then you can use LIMIT to find smallest and largest values.
12.Order the rows with the largest SUM( ) values first and use LIMIT to select the first record
13.A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause
14.The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,]
15.SELECT statement includes a LIMIT clause that specifies an offset value of 3 and a row count value of 4
16.WHERE clause can include additional logical operators and expressions that further limit the results returned
17.Limit to first three columns
18.LIMIT 5 OFFSET 3
19.Delete with order by and limit clause
20.Pulling a Section from the Middle of a Result Set
21.Tell MySQL what offset to use, from which result to start limiting.
22.The default offset is 0, so by specifying an offset of 1, you're taking the second record.
23.Selecting Records from the Beginning or End of a Result Set
24.Processing the First or Last n Records
25.Limiting the Number of Results
26.But LIMIT allows you to return only that record