Your First SELECT Query

Asking the database a question

7 min read

Now that you know what tables, rows, and columns are, it is time to actually ask a database a question. In SQL, the main way you read data is with a SELECT statement.

Here is the basic shape:

SELECT column1, column2 FROM table_name;

Read that out loud almost like English: "select column1 and column2 from table_name." That is really all SELECT does, it picks the columns you want to see, from the table you name, and hands the matching rows back to you.

Choosing specific columns

Say you have a users table with columns id, username, email, and created_at, but you only care about the username and email. You would write:

SELECT username, email FROM users;

This returns every row from the users table, but only shows the username and email columns, leaving out id and created_at. Being able to pick exactly the columns you need is one of the most useful habits in SQL, it keeps your results simple and easy to read.

The star, meaning every column

Sometimes you want to see everything a table has to offer. Instead of typing out every column name, you can use a star as shorthand for all columns:

SELECT * FROM products;

This returns every column and every row from the products table. It is handy while you are exploring a table for the first time, though once you know exactly what you need, listing specific columns is usually clearer.

A quick note on style

SQL keywords like SELECT and FROM are traditionally written in capital letters, and table or column names in lowercase. This is only a convention, SQL does not actually require it, but it makes your queries much easier to read, especially once they get longer. Every statement also ends with a semicolon, which simply marks "this instruction is finished."

One more reassuring fact: SELECT only reads data, it never changes anything in your table. That makes it a completely safe statement to experiment with as much as you like.

A great way to learn is to try changing the column list yourself. If a table has id, name, and price, try selecting just name, then just price, then both together, and notice how the result changes each time.

Quick check

Q1 Which query selects only the name and price columns from a table called products?

Q2 What does SELECT * FROM users; do?

Want to save your progress? It's free.

Create a free account