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

Just a Few Rows

LIMIT and comments.

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

The customers table has five hundred rows. Real company tables can have millions. If you select everything, your screen fills up with rows you'll never read. So we need a way to say, just show me a few.

Just a Few Rows
From the video: Just a Few Rows
  • 500 customers
  • Real tables have millions
  • Just show me a few

LIMIT

Limit goes at the very end of the query. Limit three means, give me at most three rows. Run it. Three customers, and no flood. Use limit whenever you're exploring a table for the first time. It's quick, and it's polite to the database.

query.sqlsql
SELECT first_name, city
FROM customers
LIMIT 3;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ first_name โ”‚   city    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Tara       โ”‚ Toronto   โ”‚
โ”‚ Grace      โ”‚ Melbourne โ”‚
โ”‚ Finn       โ”‚ Toronto   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Notes for humans

You can also leave notes in your query. Two dashes start a comment, and SQL ignores everything after them, until the end of the line. A comment can sit on its own line, or at the end of a line. Run it. The comments don't change the answer at all. They're for you, and for the next person who reads your query.

query.sqlsql
-- Helen wants a quick look at her books
SELECT title, genre   -- just two columns
FROM books
LIMIT 3;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       title        โ”‚  genre   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ The Silent Orchard โ”‚ Mystery  โ”‚
โ”‚ Code of the River  โ”‚ Thriller โ”‚
โ”‚ Stars Over Lagos   โ”‚ Romance  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

List some customers

Your turn. Show the first name, last name, and country of the first five customers. Pause the video, and try it. Here's one way. Select the three columns, from customers, limit five.

โœ๏ธ Exercise: List some customers

Show first_name, last_name and country for 5 customers

Show hint

Three columns, one table, and LIMIT

Show solution
solution.sqlsql
SELECT first_name, last_name, country
FROM customers
LIMIT 5;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ first_name โ”‚ last_name โ”‚  country  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Tara       โ”‚ Brown     โ”‚ Canada    โ”‚
โ”‚ Grace      โ”‚ Lopez     โ”‚ Australia โ”‚
โ”‚ Finn       โ”‚ Haddad    โ”‚ Canada    โ”‚
โ”‚ Daniel     โ”‚ Ali       โ”‚ UK        โ”‚
โ”‚ Daniel     โ”‚ Ali       โ”‚ Australia โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

You can now pick columns, limit rows, and leave comments. That's a real skill already. Next, the most useful word in SQL. Where. It lets you ask for only the rows you care about.

Key takeaways
  • LIMIT shows a few rows
  • -- starts a comment
  • Explore before you dig
Next up: Filtering with WHERE โ€” only the rows you want