AlexGH AlexGH - 3 months ago 7
SQL Question

Select Max and Min into a same column

I'm doing an exercise and I'm having problems with the output.
My output:

ABC 3 PQRS 4 //this Output is wrong


and the Output that I should be able to produce:

ABC 3
PQRS 4 //this should be my Output


This is my query:

select min(name) ,len(min(name)), max(name), len(max(name)) from customers


How could I return both?
min(name)
with
len(min(name))
and
max(name)
with
len(max(name))
in two columns???
With my query I'm returning the results but clearly in four columns and I need to return it in two columns as I showed above in the example...

Answer

Use UNION:

SELECT MIN(name) AS name, LEN(MIN(name)) AS length
FROM customers
UNION ALL
SELECT MAX(name) AS name, LEN(MAX(name)) AS length
FROM customers
Comments