Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only one column after sorting in SQL?

Tags:

sql

I have a table called 'customers', and I have to sort it first by country and by city, which I have successfully done.

Using this code:

SELECT *
FROM customers
ORDER BY Country, City

But from the output that I have, how do I print only the list of cities?

My table has several attributes or columns such as companyName, contactName, etc...

Thank you very much.

like image 864
Nayana Avatar asked Jan 23 '26 21:01

Nayana


2 Answers

SELECT City FROM customers ORDER BY Country, City

Replace * with the columns you want to show - Cityin your case.

like image 115
juergen d Avatar answered Jan 26 '26 09:01

juergen d


The SELECT criteria determines the columns displayed, while the WHERE criteria determines what rows are displayed :)

In your case it would be: SELECT City FROM customers ORDER BY Country, City

The * represents a 'wildcard' which in this case means display all.

If you wished to display both Country and city it would be:SELECT City, Country FROM customers ORDER BY Country, City

The order of the columns is determined by which order you write them in the SELECT statement.

like image 33
SamuelDavis Avatar answered Jan 26 '26 10:01

SamuelDavis