Search code examples
sqlgroupwise-maximum

SQL : get Min and Max value in one column


I have an employee table with a name and a salary.

I want to print these 2 columns with only 2 records in them, the name of my highest and lowest payed employee.

It should look something like this:

Name      Salary  
------------------
James       800  
Samanth    3000

The following code is NOT what I want, I want the min and the max in 2 columns with 2 names representing each value

SELECT 
    name, MIN(salary), MAX(salary)
FROM
    employee

Solution

  • I Finally figured out a simple code for what i wanted.

    select emp_name, salary
    from employees
    where salary = (select max(salary) from employees)
    union all
    select emp_name, salary
    from employees
    where salary = (select min(salary) from employees);
    

    I didn't know about Union. Thank you all for your contribution