Home > Net >  how do i only show the min and max values in sql? my code is showing all the values
how do i only show the min and max values in sql? my code is showing all the values

Time:03-16

Hi so i am making a database and want to return only the minimum value and maximum value with other information in other columns so i want to display like the shopID and the brands and brand name

SELECT Shop.ShopID, 
       Brand.BrandID, 
       Brand.Brand_name, 
       price
FROM Brand 
INNER JOIN Shop ON Brand.BrandID = Shop.BrandID
WHERE Brand.BrandID = 1
ORDER BY Price;

ShopID  BrandID Brand_Name  Price
18         1    coke         41
 9         1    sprite       115
 1         1    fanta        141

as you can see it shows me all 3 values i know all the brands are one 1 even though they are separate brands but i essentially want to remove the middle one so it only shows me the minimum and maximum values, just not sure how to go about this, any help would be greatly appreciated.

CodePudding user response:

I think you can use MAX and MIN functions, please refer to this: https://www.w3schools.com/sql/sql_min_max.asp

Also you can check these threads:

  1. SQL Functions using MIN and MAX together
  2. How to return MAX and MIN of a value from a table?

CodePudding user response:

You should use MIN and MAX on column name. eg-SELECT MIN(column_name) FROM table_name WHERE your_condition;

Or

eg-SELECT MAX(column_name) FROM table_name WHERE your_condition;

Also if you want to use them together SELECT MIN(column_name) AS min_col, MAX(column_name) AS max_col FROM table_name;

  • Related