CodingNeed.

1 · Read the data · 12 MIN

Read a table before writing a query

A table holds rows of the same shape; a result is another table.

The practice database contains learners. Each row represents one learner and each column has a role: id identifies the row, name is text, city may be unknown and points is an integer. SELECT chooses the columns you want to see; FROM names the source table. ORDER BY defines a repeatable order. SQL is not a loop you write for every row: you describe the result and the database finds it. The setup below is already loaded when you run the lesson.

Keep the source rows visible. Predict which rows and columns will remain after each operation.

Before you start

No installation. The tables below are loaded automatically for every run; edits do not touch the CodingNeed account database.

New words, explained

row
One record, such as one learner.
column
One named property shared by records.
primaryKey
A value uniquely identifying each row.

Follow the example step by step

  1. Inspect the four source rows in the table preview.
  2. Find the two selected column names.
  3. Read FROM learners as the source, then ORDER BY id as the result order.
  4. Notice that output rows contain only the selected columns.
Practice table: learners
idnamecitypoints
1AdaDelhi20
2LinNULL (unknown)0
3GraceDelhi35
4SamPune20
Practice table: enrollments
learner_idcourse
1React
1SQL
3SQL

You are ready to move on when: Return only the name column from learners, ordered by id. Do not include id in the output.

Read the example

SELECT id, name
FROM learners
ORDER BY id;
Check the expected output
[[1,"Ada"],[2,"Lin"],[3,"Grace"],[4,"Sam"]]

Your challenge

Return only the name column from learners, ordered by id. Do not include id in the output.

Common trap

SELECT * returns every column; use an explicit list when the result contract names particular columns.

Further reading: SQLite SELECT reference

Next lesson: Keep matching rows with WHERE

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.