← SQL on CSV
Lesson 1 of 15 by Arif Aslam 6 minute read

SELECT and LIMIT

Every SQL query you will ever write is a sentence with the same two words at the front: what you want, and where it lives. SELECT is the what. FROM is the where. Everything else in this course is decoration on those two.

The file you are querying

One hundred rows of furniture sales, one row per day from 1 January to 29 June 2025. Seven columns. Here are the first five rows, exactly as they appear in the file:

dateregionproductchannelunitsunit_pricerevenue
2025-01-01SouthGlade StoolOnline2256.051233.10
2025-01-02WestEmber RugOnline13168.352188.55
2025-01-04WestBeacon LampRetail1037.53375.30
2025-01-06SouthDelta ShelfRetail988.15793.35
2025-01-08SouthFjord TablePartner4339.891359.56

The file is loaded as a table called sales, and because it is the only file on this page it also answers to csv. Both names work everywhere in this lesson. Real tools name the table after the file, so sales.csv becomes sales, and that is worth getting used to.

Run something

The editor below already holds a query. Press Run, or hit Ctrl + Enter. The first run has to fetch the engine, so give it a couple of seconds; every run after that is instant.

What the star means

SELECT * means "give me every column". It is the right first move on a file you have never seen, because it tells you the column names and roughly what is in them. It is a poor habit on a file you already know: you pull columns you do not need, the result is wider than your screen, and on a big file you make the engine read data it could have skipped. By lesson 2 you will be naming columns instead.

The word FROM answers where. Here it is a CSV that a WebAssembly database is reading directly off a byte buffer in your tab. In a warehouse it would be a table on a server. The grammar does not change, which is the whole reason SQL is worth learning once and using for twenty years.

Why LIMIT exists

LIMIT 5 tells the engine to stop once it has five rows. On a hundred-row file that is a convenience. On a hundred-million-row file it is the difference between an answer and a locked-up tab, and it is the reason experienced people type it reflexively while they are still exploring.

A limit larger than the file is not an error. Ask for five hundred rows from a hundred-row file and you get a hundred rows, no complaint. That behavior is useful: you can set a safety limit well above what you expect and only notice it when something has gone wrong.

OFFSET skips rows before the limit starts counting. LIMIT 5 OFFSET 20 gives you rows twenty-one to twenty-five. This is how pagination works, and it comes with a warning that most tutorials leave out: without an ORDER BY, "the first twenty rows" is not a defined idea. A database is allowed to hand rows back in whatever order it found them, and a parallel scan of a large file genuinely will change its mind between runs. On this small file the order is stable, so the exercise below is safe. On anything real, pair OFFSET with an ORDER BY, which is lesson 5.

Things that will bite you today

SQL does not care about capitals or line breaks. select * from csv limit 5 is the same query as the shouty version. The convention of putting keywords in capitals is for humans reading a forty-line query at midnight, and it is worth keeping.

It does care about the order of the clauses. SELECT, then FROM, then LIMIT. Writing LIMIT 5 SELECT * is a syntax error, and the message you get back will point at roughly the right place. Read those messages. DuckDB's are unusually good, and getting comfortable with them now saves you an hour a week later.

Exercises

1. Twelve rows

Show every column, but only the first twelve rows.

Hint

One number in the starter query needs changing. Nothing else does.

2. The whole file

Now show all one hundred rows. There is more than one way to do this, and the shortest one is the point.

Hint

LIMIT is optional. A query without one returns everything.

3. Skip the first twenty

Return five rows, starting from the twenty-first row of the file.

Hint

OFFSET goes after LIMIT: LIMIT n OFFSET m.

4. Ask for too much

Ask for five hundred rows from this hundred-row file. Look at the row count under the grid and notice that nothing complains.

Hint

Exactly what it sounds like: LIMIT 500.

What to look up next

If you want to run ahead: search for "SQL logical processing order". It explains why a query is written SELECT-first but executed FROM-first, and once you have seen that diagram, half of the confusing error messages in SQL stop being confusing.