SELECT

Updated · 11 min read
easyPostgreSQL 18sqlselectcolumnsinterview

SELECT decides which columns appear in your result. You've used it since your first query. This lesson goes further: choosing columns on purpose, calculating new columns from existing ones, joining text together, and running SELECT with no table at all. Most of the data-retrieval queries you'll write in this course start here.

In One Minute

  • SELECT lists the columns of the result; FROM names the table they come from.
  • List only the columns you need. SELECT * is handy for exploring, but fragile in real code.
  • A column can be a calculation: fee_usd * 0.9 shows a 10% discount without changing the table.
  • || joins text: first_name || ' ' || last_name makes a full name.
  • SELECT works without FROM for quick calculations: SELECT 2 + 3;

The Basics

Here is the shape of a SELECT statement:

SQL
SELECT column1, column2, ...
FROM table_name;
PartWhat it does
SELECTStarts the statement and lists what each result row shows
column1, column2Column names, calculations or values, separated by commas, in the order you want them
FROM table_nameThe table the rows come from

In this simple query, the result has one row for each row of the table and one column for each item you list. Later lessons show how filtering, joins, grouping and limits change the number of rows. Here are three columns from courses:

SQL▶ Try it
SELECT title, technology, fee_usd
FROM courses
ORDER BY course_id;
Output
       title       | technology | fee_usd
-------------------+------------+---------
 Java Basics       | Java       |  450.00
 Java OOP          | Java       |  500.00
 Java Collections  | Java       |  550.00
 SQL Basics        | SQL        |  400.00
 JavaScript Basics | JavaScript |  450.00
 Git Basics        | Git        |  250.00
(6 rows)

SELECT picks the columns; it doesn't remove any rows. All six courses are still there, each showing only the three facts you asked for.

SELECT chooses the columns; every row stays

Why Not Always SELECT *?

SELECT * means "every column, in the table's order." It's perfect for a first look at a table you don't know. In real code, listing the columns is better. Here is what happens when someone adds a room column to courses: the SELECT * result grows a column, while the column list returns exactly what it did before. (This adds the column only in a fresh copy of the database; ALTER TABLE gets its own lesson later.)

SQL▶ Try it
ALTER TABLE courses ADD COLUMN room VARCHAR(20);
 
SELECT * FROM courses ORDER BY course_id LIMIT 2;
 
SELECT title, fee_usd FROM courses ORDER BY course_id LIMIT 2;
Output
ALTER TABLE
 
 course_id |    title    | technology |    level     | fee_usd | prerequisite_id | room
-----------+-------------+------------+--------------+---------+-----------------+------
         1 | Java Basics | Java       | Beginner     |  450.00 |            NULL | NULL
         2 | Java OOP    | Java       | Intermediate |  500.00 |               1 | NULL
(2 rows)
 
    title    | fee_usd
-------------+---------
 Java Basics |  450.00
 Java OOP    |  500.00
(2 rows)
SELECT * changes shape with the table; a column list doesn't
SELECT *A column list
Returns columns you don't need, which is more data to send and readReturns exactly what you use
Changes shape when someone adds a column, and fails if a column your code uses is removedKeeps the same shape until you change the query
Hides which columns the code depends onDocuments them in the query itself

Calculated Columns

A SELECT item can be a calculation, not just a column name. The database works it out for every row, and the table itself doesn't change. Here is every course with a 10% discount:

SQL▶ Try it
SELECT title, fee_usd, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY course_id;
Output
       title       | fee_usd | discounted
-------------------+---------+------------
 Java Basics       |  450.00 |    405.000
 Java OOP          |  500.00 |    450.000
 Java Collections  |  550.00 |    495.000
 SQL Basics        |  400.00 |    360.000
 JavaScript Basics |  450.00 |    405.000
 Git Basics        |  250.00 |    225.000
(6 rows)

AS discounted names the new column; without it, PostgreSQL would call it ?column?. You'll learn naming properly in Aliases. Notice the three decimal places: multiplying a two-decimal number by 0.9 gives a result with three. To show money with two decimals, round it with round(value, 2):

SQL▶ Try it
SELECT title, fee_usd, round(fee_usd * 0.9, 2) AS discounted
FROM courses
ORDER BY course_id;
Output
       title       | fee_usd | discounted
-------------------+---------+------------
 Java Basics       |  450.00 |     405.00
 Java OOP          |  500.00 |     450.00
 Java Collections  |  550.00 |     495.00
 SQL Basics        |  400.00 |     360.00
 JavaScript Basics |  450.00 |     405.00
 Git Basics        |  250.00 |     225.00
(6 rows)

You can use +, -, * and / in a SELECT, and mix columns with numbers. SQL Operators covers them all, including the integer division you met in Basic Data Types.

Each row's discount is worked out from its own fee

Joining Text Together

|| joins two pieces of text into one. To make a full name, join the first name, a space and the last name:

SQL▶ Try it
SELECT first_name || ' ' || last_name AS full_name, city
FROM students
ORDER BY student_id;
Output
     full_name      |     city
--------------------+---------------
 Srihitha Anamaneni | San Francisco
 Advitha Anamaneni  | San Francisco
 Navika Anamaneni   | Dallas
(3 rows)

The ' ' in the middle is a text value containing one space; without it the names would run together.

If either side of || is NULL, the whole result is NULL. Advitha and Navika have no email, so joining text onto their email gives nothing at all:

SQL▶ Try it
SELECT first_name, 'Email: ' || email AS contact
FROM students
ORDER BY student_id;
Output
 first_name |                contact
------------+---------------------------------------
 Srihitha   | Email: srihitha.anamaneni@example.com
 Advitha    | NULL
 Navika     | NULL
(3 rows)

The concat() function behaves differently: it skips NULL values, so concat('Email: ', email) would give Email: for them. NULL Values shows the usual ways to handle missing values.

|| is the standard way to join text, and PostgreSQL, Oracle and SQLite support it. MySQL normally uses CONCAT(), because there || means logical "or" unless the PIPES_AS_CONCAT setting is turned on (MySQL only). SQL Server has traditionally used + (SQL Server only) and CONCAT(); newer releases also accept ||. Tabs showing each database's version are coming to this course.

|| joins text; a NULL on either side makes the result NULL

SELECT Without a Table

SELECT doesn't need a table. Leave out FROM and it calculates a single row from the values you give it, which makes it a handy calculator for testing an expression:

Without FROM, SELECT works out one row from the values you give it
SQL▶ Try it
SELECT 2 + 3 AS answer, 'Fee Tracker' AS app;
Output
 answer |     app
--------+-------------
      5 | Fee Tracker
(1 row)

This works in PostgreSQL, MySQL, SQL Server and SQLite. Oracle versions before 23ai require a FROM, so there you'll see FROM dual, a one-row table that exists for this purpose (Oracle only).

Common Mistakes

A comma after the last column. SELECT title, fee_usd, FROM courses fails with syntax error at or near "FROM". Commas go between items, not after the last one.

Thinking a calculated column changes the table. SELECT fee_usd * 0.9 only shows a discount in the result. The fees in courses stay the same; changing data takes an UPDATE.

Forgetting the space when joining text. first_name || last_name gives SrihithaAnamaneni. Join a ' ' in between.

Leaving SELECT * in finished code. It may work today, but schema changes alter its result: an added column returns data your code doesn't expect, and a removed column it relies on causes failures. Listing the columns makes the query's dependencies explicit.

Interview Corner

Why is SELECT * considered bad practice in production code?

It returns columns the code doesn't use, which means more data over the network and in memory. It can also prevent an index-only scan when the selected columns aren't all available from an index. And it ties the result's shape to the table: an added column changes what the code receives, and a removed one the code relies on causes failures. A column list is clearer and stable.

Does a calculated column in SELECT change the data?

No. SELECT fee_usd * 0.9 calculates and returns a value for each result row; the stored fee is untouched. Statements such as INSERT, UPDATE, DELETE and MERGE change table data, and TRUNCATE removes all of a table's rows.

What does this return? SELECT 7 / 2, 7.0 / 2;

3 and 3.5000000000000000. When both operands are integers, PostgreSQL performs integer division and cuts off the fractional part. When one operand is a decimal (numeric), it uses numeric division and keeps fractional digits; how many it shows depends on the values involved.

Practice

Challenge: Show each payment's payment_id, its amount_usd, and a column called with_fee that adds a $2.50 processing fee to the amount. Sort by payment_id.

Show solution
SQL▶ Try it
SELECT payment_id, amount_usd, amount_usd + 2.50 AS with_fee
FROM payments
ORDER BY payment_id;
Output
 payment_id | amount_usd | with_fee
------------+------------+----------
          1 |     450.00 |   452.50
          2 |     250.00 |   252.50
          3 |     250.00 |   252.50
          4 |     550.00 |   552.50
          5 |     200.00 |   202.50
          6 |     225.00 |   227.50
          7 |     225.00 |   227.50
(7 rows)

Each row's with_fee is its own amount plus 2.50. The payments table is unchanged; the extra column exists only in this result.

Quick check: SELECT

4 questions
1What does SELECT title, fee_usd FROM courses; return?
2Why is SELECT * risky in finished code?
3Which query shows every fee with $10.00 added, without changing the table?
4Navika's email is NULL. What does 'Email: ' || email return for her?

Key Takeaways

You want toWrite
Choose columnsSELECT title, fee_usd FROM courses;
Every column (exploring)SELECT * FROM courses;
A calculated columnSELECT fee_usd * 0.9 AS discounted FROM courses;
Round money to centsround(fee_usd * 0.9, 2)
Join textfirst_name || ' ' || last_name
Calculate without a tableSELECT 2 + 3;

Was this tutorial helpful?

Lesson 10 of 115