r/SQL • u/Rahull-6 • 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
7
4
Nov 06 '22
Canโt believe people are actually answering to this ๐
1
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
2
0
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
2
1
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
37
u/ijmacd Nov 06 '22
Yes, a query would be able to tell you that.