Limit the query results: Get 3 Most Expensive Products : Limits « Select Clause « SQL / MySQL






Limit the query results: Get 3 Most Expensive Products

  
/*

mysql> select * from Product;
+-----------+------------+----------------------+-------+
| ProductID | Name       | Description          | Price |
+-----------+------------+----------------------+-------+
|         1 | Java       | Java Demo Code       |     6 |
|         2 | Swing      | Swing Demo Code      |     7 |
|         3 | C#         | C# Demo Code         |     4 |
|         4 | JavaScript | JavaScript Demo Code |     3 |
|         5 | SQL        | SQL Demo Code        |     8 |
|         6 | Struts     | Struts Demo Code     |     9 |
+-----------+------------+----------------------+-------+
6 rows in set (0.00 sec)

mysql> /* Get 3 Most Expensive Products*/
mysql> SELECT Name, Price
    -> FROM Product
    -> ORDER BY Price DESC, Name ASC
    -> LIMIT 0,3;
+--------+-------+
| Name   | Price |
+--------+-------+
| Struts |     9 |
| SQL    |     8 |
| Swing  |     7 |
+--------+-------+
3 rows in set (0.00 sec)


*/

/* Create Database */  
Drop TABLE Product;


CREATE TABLE Product (
    ProductID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    Description VARCHAR(255) NOT NULL,
    Price DECIMAL NULL 
) Type=InnoDB;
  
  
/* Insert Data */
INSERT INTO Product (Name, Description, Price) VALUES ('Java', 
                                                       'Java Demo Code',
                                                       5.99 );
INSERT INTO Product (Name, Description, Price) VALUES ('Swing', 
                                                       'Swing Demo Code',
                                                       6.99 );
INSERT INTO Product (Name, Description, Price) VALUES ('C#', 
                                                       'C# Demo Code',
                                                       3.99 );
INSERT INTO Product (Name, Description, Price) VALUES ('JavaScript', 
                                                       'JavaScript Demo Code',
                                                       2.99 );
INSERT INTO Product (Name, Description, Price) VALUES ('SQL', 
                                                       'SQL Demo Code',
                                                       7.99 );
INSERT INTO Product (Name, Description, Price) VALUES ('Struts', 
                                                       'Struts Demo Code',
                                                       8.99 );
select * from Product;

/* Get 3 Most Expensive Products*/
SELECT Name, Price 
FROM Product 
ORDER BY Price DESC, Name ASC
LIMIT 0,3;


           
         
    
  








Related examples in the same category

1.Use LIMIT in SELECT
2.Use LIMIT
3.Another ORDER BY and limit
4.Simple LIMIT
5.Retrieving the Top Five Students
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