Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this statement shorter / order by

Tags:

sql

can I write this statement shorter?

Select S_name 
from 
  Supplier
  JOIN Nation ON Supplier.S_nationkey = Nation.N_nationkey 
  JOIN Region on Nation.n_regionkey = region.R_regionkey 
Where
  Region.r_name = 'AFRICA' 
Union
Select C_name
from 
  Customer 
  JOIN Nation ON Customer.C_Nationkey = Nation.N_nationkey 
  JOIN Region on Nation.N_regionkey = Region.R_regionkey 
Where
  Region.R_name = 'AFRICA'

and I want to order my output by name, But i dont know why, cause I do have C_name and S_Name as Output?!

thanks

like image 267
h0ppel Avatar asked Jul 02 '26 06:07

h0ppel


2 Answers

If you want all the data in the same column then you can place a SELECT around it and then do an ORDER BY.

    Select S_name As Names
    from Supplier 
    JOIN Nation 
        ON Supplier.S_nationkey = Nation.N_nationkey 
    JOIN Region 
        on Nation.n_regionkey = region.R_regionkey 
    Where Region.r_name = 'AFRICA' 
    Union
    Select C_name As Names
    from Customer 
    JOIN Nation 
        ON Customer.C_Nationkey = Nation.N_nationkey 
    JOIN Region 
        on Nation.N_regionkey = Region.R_regionkey 
    Where Region.R_name = 'AFRICA'
    ORDER BY Names

If you don't need the data in the same column, then you could do it this way:

Select S_name, c.C_name
from Supplier 
JOIN Nation 
    ON Supplier.S_nationkey = Nation.N_nationkey 
JOIN Region 
    on Nation.n_regionkey = region.R_regionkey 
JOIN Customer c
    on Nation.N_nationkey = c.C_Nationkey
Where Region.r_name = 'AFRICA' 
ORDER BY S_name, c.c_name
like image 149
Taryn Avatar answered Jul 03 '26 21:07

Taryn


As for order by clause, try

ORDER BY 1

at the end of your query

like image 20
Eugene Avatar answered Jul 03 '26 20:07

Eugene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!