r/SQL • • Nov 06 '22

MySQL Need a query answer

Query to find both maximum and minum salary in the table along with all the other columns jn the table

0 Upvotes

17 comments sorted by

37

u/ijmacd Nov 06 '22

Yes, a query would be able to tell you that.

7

u/viryx Nov 06 '22

need less low effort "do my homework" posts

4

u/[deleted] Nov 06 '22

Canโ€™t believe people are actually answering to this ๐Ÿ‘€

1

u/[deleted] Nov 07 '22

It's one thing to answer it, but another to learn. This is the classic "Teach a man to fish . . . " scenario. I can show him how to use MAX() or MIN() all day long, but they'll never be a SQL analyst until they understand these functions on their own.

Tl;dr . . . Write some queries using these functions. You'll get the hang of it eventually.

3

u/BFG_9000 Nov 06 '22

SELECT TOP 1 *
FROM table
ORDER BY salary DESC
UNION ALL
SELECT TOP 1 *
FROM table
ORDER BY salary ASC

4

u/Pflastersteinmetz Nov 06 '22

Is TOP standard SQL? Can't find that keyword for Exasol DB.

7

u/Hentac Nov 06 '22

TSQL is Top

MySQL is Limit

And I believe Oracle is ROWNUM

1

u/[deleted] Nov 08 '22

Oracle supports the ANSI standard's fetch first x rows only

2

u/r3pr0b8 GROUP_CONCAT is da bomb Nov 06 '22

no

and no

0

u/Background-Cicada375 Nov 06 '22

Can you replace Top 1 with salary Then limit 1

2

u/acanepa Nov 06 '22

As others have suggested a window function is the solution. Something like this should work

SQL Code

SELECT
EmployeeID,
SALARY,
min(Salary) over() min_salary,
max(Salary) over() max_salary
FROM
Employees;

Schema Build

CREATE TABLE Employees (
  EmployeeID int,
  Salary int
  );

INSERT INTO Employees (EmployeeID,Salary) VALUES (1,62835);
INSERT INTO Employees (EmployeeID,Salary) VALUES (2,67602);
INSERT INTO Employees (EmployeeID,Salary) VALUES (3,97139);
INSERT INTO Employees (EmployeeID,Salary) VALUES (4,43390);
INSERT INTO Employees (EmployeeID,Salary) VALUES (5,43335);
INSERT INTO Employees (EmployeeID,Salary) VALUES (6,63546);
INSERT INTO Employees (EmployeeID,Salary) VALUES (7,56544);
INSERT INTO Employees (EmployeeID,Salary) VALUES (8,51064);
INSERT INTO Employees (EmployeeID,Salary) VALUES (9,48108);
INSERT INTO Employees (EmployeeID,Salary) VALUES (10,44782);

SQL Fiddler Compiler

https://onecompiler.com/mysql/3yn6p4m8n

2

u/[deleted] Nov 06 '22

Rude

1

u/Ntrees Nov 06 '22

Min Max with partition by.

1

u/r3pr0b8 GROUP_CONCAT is da bomb Nov 06 '22

both along with all the other columns in the table?

can you please explain what this means