SELECT
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
SELECTlists the columns of the result;FROMnames 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.9shows a 10% discount without changing the table. ||joins text:first_name || ' ' || last_namemakes a full name.SELECTworks withoutFROMfor quick calculations:SELECT 2 + 3;
The Basics
Here is the shape of a SELECT statement:
SELECT column1, column2, ...
FROM table_name;| Part | What it does |
|---|---|
SELECT | Starts the statement and lists what each result row shows |
column1, column2 | Column names, calculations or values, separated by commas, in the order you want them |
FROM table_name | The 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:
SELECT title, technology, fee_usd
FROM courses
ORDER BY course_id; 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.
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.)
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;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 * | A column list |
|---|---|
| Returns columns you don't need, which is more data to send and read | Returns exactly what you use |
| Changes shape when someone adds a column, and fails if a column your code uses is removed | Keeps the same shape until you change the query |
| Hides which columns the code depends on | Documents 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:
SELECT title, fee_usd, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY course_id; 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):
SELECT title, fee_usd, round(fee_usd * 0.9, 2) AS discounted
FROM courses
ORDER BY course_id; 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.
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:
SELECT first_name || ' ' || last_name AS full_name, city
FROM students
ORDER BY student_id; 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:
SELECT first_name, 'Email: ' || email AS contact
FROM students
ORDER BY student_id; 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.
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:
SELECT 2 + 3 AS answer, 'Fee Tracker' AS app; 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
SELECT payment_id, amount_usd, amount_usd + 2.50 AS with_fee
FROM payments
ORDER BY payment_id; 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.
Key Takeaways
| You want to | Write |
|---|---|
| Choose columns | SELECT title, fee_usd FROM courses; |
| Every column (exploring) | SELECT * FROM courses; |
| A calculated column | SELECT fee_usd * 0.9 AS discounted FROM courses; |
| Round money to cents | round(fee_usd * 0.9, 2) |
| Join text | first_name || ' ' || last_name |
| Calculate without a table | SELECT 2 + 3; |
Was this tutorial helpful?