Choosing columns in SQL
Topic 2 of 45 · 7 minFree lessonSELECT * shows every column, which is handy for a first look. Most of the time, though, you only need a few. Asking for only those columns keeps the answer short and clear.
Naming the columns you want
List the columns after SELECT, with commas between them. You get only those columns, for every row.
SELECT name, price FROM products;The column names must match the table's exactly. You can find them in the list of tables under the editor.
Choosing the order
The columns come back in the order you list them, not the order the table keeps them in. Put the most important column first.
SELECT city, name FROM customers;One column
A single column is fine too. This is a quick way to look at one kind of fact, like the date each customer joined.
SELECT joined FROM customers;Spreading a query over lines
As queries grow, put each part on its own line. PostgreSQL ignores the extra spaces and line breaks, but people find it much clearer to read.
SELECT name, category, stockFROM products;name price as "the name column, shown with the heading price", so you get the names under the wrong heading.SELECT name price FROM products;There's no price at all, only the names, under a heading that says price. When a result looks strange, check the commas first.
SELECT * sends a lot of data nobody reads.Try it yourself
Run the code in the editor and answer 8 practice questions on choosing columns. It's free, you only need an account.