AI Hints
Homeโ€บSQL Tutorialโ€บYour First Queries
SQL ยท Lesson 5 of 126

Choosing Columns

Only what you need.

โฑ 2 min readโ–ถ Video at 6:04๐ŸŽ“ Free
Watch this lesson in the SQL courseWatch on YouTube โ†—Full SQL course

Helen is making a price list for her website. She doesn't need the stock level, or the year it was published. Just the title and the price. So instead of the star, we name the columns we want.

Choosing Columns
From the video: Choosing Columns
  • Helen needs a price list
  • Only titles and prices
  • Name the columns you want

Pick two columns

After select, we write the column names, separated by a comma. Title, comma, price. Then from books, on the next line. Putting each part on its own line makes it easier to read. Run it. Now we get all thirty books, but only the two columns we asked for. That's much easier to read.

query.sqlsql
SELECT title, price
FROM books;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚          title           โ”‚ price โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ The Silent Orchard       โ”‚ 14.99 โ”‚
โ”‚ Code of the River        โ”‚ 12.99 โ”‚
โ”‚ Stars Over Lagos         โ”‚ 9.99  โ”‚
โ”‚ A Short History of Bread โ”‚ 19.99 โ”‚
โ”‚ Python for Curious Minds โ”‚ 34.99 โ”‚
โ”‚ The Last Lighthouse      โ”‚ 13.99 โ”‚
โ”‚ Running on Empty         โ”‚ 15.99 โ”‚
โ”‚ Dragons of Ember Bay     โ”‚ 17.99 โ”‚
โ”‚ The Quiet Kitchen        โ”‚ 24.99 โ”‚
โ”‚ Numbers Don't Lie        โ”‚ 21.99 โ”‚
โ”‚ Moonlight Express        โ”‚ 11.99 โ”‚
โ”‚ Garden of Small Things   โ”‚ 8.99  โ”‚
โ”‚ SQL in a Weekend         โ”‚ 29.99 โ”‚
โ”‚ The Map Maker's Daughter โ”‚ 18.99 โ”‚
โ”‚ Five Minute Habits       โ”‚ 12.99 โ”‚
โ”‚ Crown of Ash             โ”‚ 16.99 โ”‚
โ”‚ Spice Roads              โ”‚ 27.99 โ”‚
โ”‚ Startup Stories          โ”‚ 19.99 โ”‚
โ”‚ Murder at Mill Lane      โ”‚ 10.99 โ”‚
โ”‚ Deep Water               โ”‚ 13.49 โ”‚
โ”‚ Letters to Lisbon        โ”‚ 10.49 โ”‚
โ”‚ Empires of Salt          โ”‚ 22.99 โ”‚
โ”‚ Data for Everyone        โ”‚ 31.99 โ”‚
โ”‚ The Calm Mind            โ”‚ 14.49 โ”‚
โ”‚ The Glass Forest         โ”‚ 18.49 โ”‚
โ”‚ One Pan Dinners          โ”‚ 21.49 โ”‚
โ”‚ The Pricing Game         โ”‚ 23.99 โ”‚
โ”‚ The Painted Door         โ”‚ 12.49 โ”‚
โ”‚ Night Train to Oslo      โ”‚ 14.99 โ”‚
โ”‚ Wild Coast               โ”‚ 11.49 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

You choose the order

The columns come back in the order that you write them. Here we ask for price first, then title, then author. Run it. Price is now the first column. You're in charge of the layout. We've also added limit four at the end, which we'll explain in the next lesson.

query.sqlsql
SELECT price, title, author
FROM books
LIMIT 4;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ price โ”‚          title           โ”‚  author   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 14.99 โ”‚ The Silent Orchard       โ”‚ Mira Hale โ”‚
โ”‚ 12.99 โ”‚ Code of the River        โ”‚ Tom Avery โ”‚
โ”‚ 9.99  โ”‚ Stars Over Lagos         โ”‚ Ada Obi   โ”‚
โ”‚ 19.99 โ”‚ A Short History of Bread โ”‚ Lena Fox  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Small rules that save you

A few small rules will save you from errors. Separate columns with commas, but don't put a comma after the last one. Spell the column name exactly as it is in the table. And capital letters don't matter for the keywords. Select in capitals, or in lower case, both work. Most people write keywords in capitals, so they stand out.

Commasbetween columns, not after the last
Spellingmust match the table
Capitalskeywords work either way
HabitKEYWORDS in capitals

Summary

Now you can pick exactly the columns you need, in the order you like. Next, how to see just a few rows, so big tables don't flood your screen.

Key takeaways
  • Name the columns you want
  • Separate with commas
  • Order is up to you
Next up: Just a Few Rows โ€” LIMIT and comments