SELECT and columns

Topic 1 of 4 in Querying a table10 min readBeginnerFree course

Overview

The SELECT list decides what each row of your result looks like. It can name existing columns, but it can also calculate brand-new ones, rename them, and drop duplicates. In this lesson you’ll learn everything that goes between SELECT and FROM.

Picking columns

The columns after SELECT decide the shape of the result. You choose which columns appear and in what order; the table itself doesn’t change.

SELECT picks columns. The customers table has columns id, first_name, last_name, email, city and country. The query SELECT first_name, city FROM customers returns a result with only those two columns, in that order: Asha Pune, Ben Leeds, Chen Singapore and so on. The result has only the columns you name, in the order you name them. SELECT * returns every column.

SQL
SELECT city, first_name
FROM customers;
Text
   city    | first_name 
-----------+------------
 Pune      | Asha
 Leeds     | Ben
 Singapore | Chen
 Lagos     | Dee
 Madrid    | Elena
 Pune      | Farhan
 Seattle   | Grace
 Lyon      | Hugo
 Mumbai    | Isha
 Austin    | Jack
(10 rows)

You can list the same column twice, or mix columns from the table with values that aren’t in it — anything that produces a value is allowed.

Calculated columns

Any expression can appear in the select list, and it’s calculated separately for every row. Arithmetic uses +, -, * and /:

SQL
SELECT name, price, price * 1.2
FROM products
WHERE category_id = 3;
Text
       name        | price | ?column? 
-------------------+-------+----------
 Pour-Over Dripper | 24.00 |   28.800
 Burr Grinder      | 89.00 |  106.800
 Electric Kettle   | 59.90 |   71.880
 Glass Teapot      | 32.00 |   38.400
(4 rows)

Each equipment product’s price with 20% tax added. The calculation doesn’t change the table — it only exists in this result.

Notice the column heading ?column?. A calculated column has no natural name, so PostgreSQL makes one up. That’s ugly in a report and useless in application code, which is why you’ll almost always name calculated columns.

Naming columns with AS

AS gives any column — calculated or not — a name in the result. That name is called an alias:

SQL
SELECT name AS product,
       price,
       round(price * 1.2, 2) AS price_with_tax
FROM products
WHERE category_id = 3;
Text
      product      | price | price_with_tax 
-------------------+-------+----------------
 Pour-Over Dripper | 24.00 |          28.80
 Burr Grinder      | 89.00 |         106.80
 Electric Kettle   | 59.90 |          71.88
 Glass Teapot      | 32.00 |          38.40
(4 rows)

round(value, 2) is a function that rounds to two decimal places; you’ll meet many more in the next chapter. Aliases follow the same rules as other names: lower case with underscores works everywhere. If you want spaces or capital letters, wrap the alias in double quotes:

SQL
SELECT name AS "Product", stock AS "Units in stock"
FROM products
WHERE stock < 10;
Text
      Product      | Units in stock 
-------------------+----------------
 Decaf House Blend |              0
 Burr Grinder      |              6
 Glass Teapot      |              0
(3 rows)

Strictly, AS is optional — price * 1.2 price_with_tax also works — but writing it makes the intent obvious and avoids the missing-comma surprise you saw in the previous lesson.

Joining text together

The || operator glues text values together, which is handy for display columns:

SQL
SELECT first_name || ' ' || last_name AS full_name,
       city || ', ' || country AS location
FROM customers
WHERE country = 'India';
Text
 full_name  |   location    
------------+---------------
 Asha Rao   | Pune, India
 Farhan Ali | Pune, India
 Isha Patel | Mumbai, India
(3 rows)

You’ll see more text functions — upper, length, concat_ws and others — in Text functions.

Removing duplicates with DISTINCT

Which countries do our customers live in? Selecting the column lists every customer’s country, with repeats:

SQL
SELECT country FROM customers;
Text
  country  
-----------
 India
 UK
 Singapore
 Nigeria
 Spain
 India
 USA
 France
 India
 USA
(10 rows)

DISTINCT keeps one copy of each distinct row:

SQL
SELECT DISTINCT country FROM customers;
Text
  country  
-----------
 Singapore
 France
 Nigeria
 UK
 Spain
 USA
 India
(7 rows)

Seven countries. The order is not alphabetical — DISTINCT doesn’t sort, and without ORDER BY the database returns rows in whatever order is convenient. (You’ll fix that in the next lesson.)

DISTINCT applies to the whole row, not just the first column. With two columns, it removes rows where both values repeat:

SQL
SELECT DISTINCT country, city
FROM customers
WHERE country IN ('India', 'USA');
Text
 country |  city   
---------+---------
 India   | Mumbai
 India   | Pune
 USA     | Austin
 USA     | Seattle
(4 rows)

Pune appears once even though two customers live there, but India still appears twice because there are two different Indian cities. (IN (…) means “equal to any of these” — more on it in the next lesson.)

Use DISTINCT deliberately. If a query returns duplicates you didn’t expect, adding DISTINCT hides the symptom without fixing the cause — usually a join that matches more rows than you think. You’ll learn to spot that in chapter 5.

Converting types with CAST

Sometimes a value has the wrong type for what you want to do. CAST(value AS type) converts it, and PostgreSQL also offers the shorter value::type:

SQL
SELECT CAST('42' AS integer) + 1 AS from_text,
       '2026-12-04'::date AS a_date,
       price::integer AS whole_price
FROM products
WHERE id = 1;
Text
 from_text |   a_date   | whole_price 
-----------+------------+-------------
        43 | 2026-12-04 |          15
(1 row)

Casting 14.50 to an integer rounds it to 15. If a value can’t be converted, you get an error rather than a silent wrong answer:

SQL
SELECT 'twelve'::integer;
Text
ERROR:  invalid input syntax for type integer: "twelve"
LINE 1: SELECT 'twelve'::integer;
               ^

:: is PostgreSQL-specific; CAST(… AS …) is standard SQL and works in every database. Both are common in PostgreSQL code, so it’s worth recognising them.

Expressions without a table

Because the select list is just a list of expressions, SELECT without FROM makes a handy calculator for checking how something behaves before you use it in a bigger query:

SQL
SELECT 10 / 4 AS integer_division,
       10 / 4.0 AS decimal_division,
       'Bean' || ' & ' || 'Leaf' AS shop;
Text
 integer_division |  decimal_division  |    shop     
------------------+--------------------+-------------
                2 | 2.5000000000000000 | Bean & Leaf
(1 row)

10 / 4 is 2, not 2.5: when both sides are integers, PostgreSQL does integer division and throws away the remainder. Make one side a decimal and you get the precise answer. You’ll come back to this trap in Numbers and math.

Summary of the select list

You write You get
SELECT * every column
SELECT a, b columns a and b, in that order
SELECT a * 2 a calculated column called ?column?
SELECT a * 2 AS doubled the same, named doubled
SELECT DISTINCT a, b each distinct combination of a and b once
SELECT a::integer a converted to an integer

Tip: when building a complicated column, start with a small SELECT on a few rows (add WHERE id = 1) and check the value is right before running it over the whole table.

Exercise

What is the stock worth?

The shop's accountant wants to know how much money is tied up in stock. Write a query that returns, for every product, its name, price, stock, and a calculated column named stock_value equal to price multiplied by stock. Then add a second query that returns the distinct status values used in the orders table, so the accountant knows which states an order can be in.

SQL
SELECT name, price, stock
FROM products;

-- the distinct order statuses

Summary

  • SELECT returns only the columns you name, in the order you name them; SELECT * returns them all.
  • Columns can be expressions: arithmetic, function calls and text concatenation are calculated for every row.
  • AS gives a column a name; quote the name with double quotes if it has spaces or capitals.
  • DISTINCT removes duplicate rows from the result, looking at all the selected columns together.
  • CAST(value AS type), or PostgreSQL's value::type, converts between types.