SELECT company_name, contact_nameFROM customersORDER BY company_name DESC, contact_nameLIMIT10; company_name | contact_name -----------------------------------+------------------------- Wolski Zajazd | Zbyszek Piestrzeniewicz Wilman Kala | Matti Karttunen White Clover Markets | Karl Jablonski Wellington Importadora | Paula Parente Wartian Herkku | Pirkko Koskitalo Vins et alcools Chevalier | Paul Henriot Victuailles en stock | Mary Saveley Vaffeljernet | Palle Ibsen Trails Head Gourmet Provisioners | Helvetius Nagy Tradição Hipermercados | Anabela DominguesSELECT orderNumber, orderlinenumber, quantityOrdered * priceEach as final_priceFROM orderdetailsORDER BY final_price DESCLIMIT10;-- orders order_id | product_id | total_price ----------+------------+--------------------10981 | 38 | 1581010865 | 38 | 1581010353 | 38 | 1054010417 | 38 | 1054010889 | 38 | 1054010424 | 38 | 1032910897 | 29 | 990310372 | 38 | 843210816 | 38 | 790510540 | 38 | 7905(10rows)-- using alias in order bySELECT first_name, last_name as surnameFROM actorsORDER BY surname DESC ; first_name | surname ------------+--------- Ziyi | Zhang Billy | Zane Sean | Young Jin-seo | Yoon Ji-tae | Yoo-- NULLS FIRST AND LASTselect*from actorsorder by gender NULLSLASTLIMIT5; first_name | last_name | gender | date_of_birth ------------+-----------+--------+--------------------- Malin | Akerman | F | 1978-05-1200:00:00 Julie | Andrews | F | 1935-10-0100:00:00 Ivana | Baquero | F | 1994-06-1100:00:00 Lorraine | Bracco | F | 1954-10-0200:00:00 Alice | Braga | F | 1983-04-1500:00:00
ORDER BY on multiple columns
The following statement selects the first name and last name from the customer table and sorts the rows by the first name in ascending order and last name in descending order:
SELECT first_name, last_nameFROM customerORDER BY first_name ASC, last_name DESC;
In this example, the ORDER BY clause sorts rows by values in the first name column first. And then it sorts the sorted rows by values in the last name column.
As you can see clearly from the output, two customers with the same first name Kelly have the last name sorted in descending order.
DISTINCT
SELECT DISTINCT regionFROM customersWHERE country ='USA'LIMIT10; region -------- NM CA AK WYOR MT ID WA(8rows)
Distinct COUNT
SELECTCOUNT(DISTINCT region)FROM customersWHERE country ='USA'; count -------8