Skip to content

Your first query

Read a table with SELECT, FROM and LIMIT, name what you compute, and use the SQL page's Tables list so you never mistype a table.

Introductory ~7 min8 steps

What runs in CodeSplash

You will be able to

  • Write a query with SELECT, FROM and LIMIT
  • Compute a new column and name it with AS
  • Find tables and columns in the Tables list
01

SQL asks; it doesn't change

Read

SQL is how you ask a table a question. A SELECT reads rows and hands you a result, and your data stays as it was.

CodeSplash's warehouse is DuckDB, and every dataset in your project is a table you can query. Add the sample first:

Palmer penguins

It becomes the table palmer_penguins_sample. Every query in this track uses it, so you can paste them as they are.

02

Run your first query

In the app

Open SQL (⌘4), paste this into a tab, and press Run or ⌘↵.

SELECT species, island, body_mass
FROM palmer_penguins_sample
LIMIT 3;

SELECT names the columns, FROM names the table and LIMIT caps the rows. The result appears under Results.

The first three rows are Adelie penguins from Torgersen, weighing 3,750, 3,800 and 3,250 grams.

03

Find your tables

Explore

You shouldn't have to remember names. On the left, Tables lists every table. Press a table's arrow to see its columns. Insert puts the full name in the editor, and clicking a column inserts that too.

Try it: clear the editor, type SELECT, click bill_len and bill_dep in the panel, then type FROM and insert the table. The editor also completes names as you type.

Datasets live in the raw schema and the results of recipes under analytics, and you can leave the schema off.

04

What does this do?

Predict

Predict first

What do you expect from SELECT * FROM palmer_penguins_sample LIMIT 5;?

Returns one columnReturns every columnAn error
Reveal

Every column, for five rows. * means all columns. It's handy for a quick look, but name your columns in anything you keep: your query then says what it needs, and it won't change meaning if a column is added.

05

Compute a column

Explore

SELECT can compute as well as read. AS gives the result a name.

SELECT species,
       body_mass,
       body_mass / 1000.0 AS mass_kg
FROM palmer_penguins_sample
LIMIT 5;

Change it to show flipper length in centimeters. The new column exists only in this result. Nothing is added to your table.

Add -- a note at the end of any line to leave yourself a comment.

06

How many rows?

Checkpoint

Run this, then answer.

SELECT count(*) AS n
FROM palmer_penguins_sample;

Check yourself

How many rows does the table have?

Show the answer

Answer: 344

count(*) counts rows. That's your first summary, and it's worth doing first on any table: it tells you whether you got the data you expected.

Check yourself

You run a query with LIMIT 10. What happens to the other rows?

Show the answer

Answer: b

A SELECT only reads. Your table is unchanged, whatever you ask.

Check yourself

What does body_mass / 1000.0 AS mass_kg do?

Show the answer

Answer: b

The result has a new column. To keep it, you'd save the result or build it in a recipe, which later lessons cover.

07

Take it further

In the app

Follow the guided version, which averages {{value}} by {{group}} and checks each step as you go:

A summary of one column with its interval and shape is a method in itself, once you've seen the rows:

08

Recap

Recap
  • SELECT columns FROM a table, with LIMIT to cap rows.
  • A query reads and never changes the table.
  • The Tables list inserts names for you.