Lesson 2
Choosing columns: SELECT
For readers who finished lesson 1 and can run select * from a table.
- Choosing columns
- Computing a column
- Naming it with AS
- The missing comma
- Removing duplicates with DISTINCT
- 4 exercises
Every query runs on real PostgreSQL, in your browser. It downloads once, about 6 MB, then starts in a few seconds.
For a reference page on the same statements, read SQL SELECT Statement: Syntax, Examples, and Query Order.
The idea
The list after SELECT decides the columns
Instead of the star, write the columns you want, separated by commas. The answer has those columns, in the order you list them, and still one row for each row of the table.
Try it
Choose two columns of products
select name, price from products;
Still 26 rows, but only the two columns you listed.
The idea
A column can be computed from others
An expression in the list, such as price * 1.2, is worked out for every row: here, the price with 20 % tax.
select name, price, price * 1.2 from products;
PostgreSQL names the third column ?column?, since an expression has no name of its own.
Try it
AS gives a column its name
Write as and a name after the expression. The name is what the column is called in the answer; the table itself does not change.
select name, price, price * 1.2 as price_with_tax from products;
The third column is now called price_with_tax.
Watch out
A missing comma renames a column
Without the comma between name and price, PostgreSQL reads price as a new name for name, the way it reads as:
select name price from products;
| price |
|---|
| Wireless Mouse |
| Mechanical Keyboard |
| USB-C Hub |
The query runs without an error: one column, headed price, full of product names. When an answer has fewer columns than you asked for, look for a missing comma.
The idea
DISTINCT keeps one copy of each row
Write DISTINCT right after select, and rows that repeat in the answer appear only once.
Try it
List the product categories
select distinct category from products;
6 categories. Without distinct, the same query returns 26 rows, one per product.
In DbSchema
Run the same query on your own database

The SQL Editor in DbSchema runs the queries of this lesson on any database you connect to, and shows the rows in a grid under the query.
The Community Edition is free. Every download starts with a 15-day trial of all features.
Exercise 1 of 4
Show the name and the price of every product.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, price from products
Exercise 2 of 4
Show the first name, the last name and the email of every customer.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select first_name, last_name, email from customers
Exercise 3 of 4
Show every product's name, its price, and its price with 20 % tax, in a column named price_with_tax.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, price, price * 1.2 as price_with_tax from products
Exercise 4 of 4
Show each product category once.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select distinct category from products