Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL, how do I get all rows where a column's value is the lowest in the table?

I am a newbie to SQL, I am using this query to look for the minimum value in the field weight of my table.

SELECT product_id, 
       MIN(weight) 
  FROM table 
 WHERE 1;

It does show one field with the min value, but only one? But I have many products with the same minimum weight. Is there a way I could specify that I need to show all other products?

like image 509
Zenet Avatar asked Oct 13 '10 20:10

Zenet


People also ask

Can you use MIN function in WHERE clause?

The SQL MIN() function with WHERE clauseThe aggregate functions can be used in conjunction with the WHERE clause to gain further insights from our data. One of these is the MIN() function. In SQL, the MIN() function is used to compute the smallest or minimum value of numeric values in a column.

How do I get only the specific number of rows in SQL?

The SQL SELECT TOP Clause The SELECT TOP clause is used to specify the number of records to return. The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

How do you find the minimum value of multiple columns in SQL?

We can use a nested CASE statement to compare the values of multiple columns to get the minimum value. Also, we can use the nested IIF statement.


1 Answers

select * from table where weight = (select MIN(weight) from table)
like image 193
Fosco Avatar answered Oct 07 '22 14:10

Fosco