CRUD with SQLCore· 35 min read

SELECT — Reading Data

The most-used SQL command — ask the database for data.

What you will learn

  • Select all and specific columns
  • Understand the result set

SELECT

To read data back out of a table you use SELECT — by far the most-used command in SQL (the R, Read, in CRUD). You say which columns you want and which table to take them from, in the shape SELECT columns FROM table. The rows that come back are called the result set.

The -- at the end of each line is a comment — MySQL ignores everything after it, so comments are just notes for humans reading the query.

Reading data is a two-step process. Read the steps first, then look at the code:

  1. Say which columns you want — either * for every column, or a comma-separated list like name, email.
  2. Say which table to read from with FROM users — MySQL returns all the matching rows as the result set.
SELECT columns FROM table
SELECT * FROM users;                 -- all columns, all rows

SELECT name, email FROM users;       -- only these columns

SELECT name FROM users;              -- just the names

Three queries, each asking for less: the * (star) is a shortcut meaning every column, so the first line returns the whole table. The second line lists name, email so only those two columns come back. The third asks for just name. In every case you get all the rows — to limit which rows, you add a WHERE clause (the next lesson).

Note: Output (for SELECT name, email FROM users;): Asha asha@x.com Ravi ravi@x.com Meera meera@x.com MySQL returns a small grid (the result set) with one column per field you asked for and one line per matching row. Because we listed only name and email, the id, age and created_at columns are left out.

Tip: SELECT * grabs every column (handy while exploring), but in real apps list the exact columns you need — it is clearer and faster.

Q. How do you get every column from the users table?

Answer: SELECT * FROM users returns all columns for all rows. List column names to be more specific.

✍️ Practice

  1. Select all rows, then just two columns.
  2. Select a single column from your products table.

🏠 Homework

  1. Write three different SELECT queries against your data.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →