What are the shortest and longest trips in the mytable table? : MIN « Aggregate Functions « SQL / MySQL






What are the shortest and longest trips in the mytable table?

        
mysql>
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  rec_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name            VARCHAR(20) NOT NULL,
    ->  trav_date       DATE NOT NULL,
    ->  miles           INT NOT NULL,
    ->  PRIMARY KEY (rec_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO mytable (name,trav_date,miles)
    ->  VALUES
    ->          ('Ben','2010-11-30',152),
    ->          ('Suzi','2010-11-29',391),
    ->          ('Henry','2010-11-29',300),
    ->          ('Henry','2010-11-27',96),
    ->          ('Ben','2010-11-29',131),
    ->          ('Henry','2010-11-26',115),
    ->          ('Suzi','2010-12-02',502),
    ->          ('Henry','2010-12-01',197),
    ->          ('Ben','2010-12-02',79),
    ->          ('Henry','2010-11-30',203)
    -> ;
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT MIN(miles) AS shortest, MAX(miles) AS longest
    -> FROM mytable;
+----------+---------+
| shortest | longest |
+----------+---------+
|       79 |     502 |
+----------+---------+
1 row in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Returns the minimum value from a group of values: MIN()
2.And to find the lowest commission that any of the sales staff is earning, use this:
3.Use MIN( ) or MAX( ) With a GROUP BY clause
4.MIN( ) and MAX( ) also work with expressions or values that are derived from column values.
5.Do calculation with aggregate function
6.Controlling String Case Sensitivity for MIN( ) and MAX( )
7.What are the lowest and highest U.S. state populations?
8.To make bstr not case sensitive, you can convert the values to a given lettercase: