SQL SELECT Basics: Choosing Columns, Expressions and Aliases
The SELECT clause from the ground up: SELECT * versus a column list, calculated columns, arithmetic and operator precedence, integer division, division by zero, string concatenation with || and CONCAT, functions, column aliases, and SELECT without FROM.
kvi5hnuCode Sprout team member11 min readOn this page
Every query starts with SELECT. It decides which columns appear in the result and what's in them. This article covers the SELECT clause and the simplest FROM: one table. Filtering rows with WHERE comes in the next three articles.
SELECT select_list
FROM table_name;
All the examples in Part 2 use the same small products table from the Trailhead shop:
| product_id | name | category | brand | price | stock | weight_g |
|---|---|---|---|---|---|---|
| 1 | Ridge 2 tent | tents | Alpina | 249.99 | 12 | 2100 |
| 2 | Summit 4 tent | tents | Alpina | 489.00 | 3 | 4300 |
| 3 | Glow head torch | lighting | Lumen | 34.50 | 40 | 90 |
| 4 | Basecamp lantern | lighting | NULL | 27.00 | 15 | NULL |
| 5 | Trail 30 backpack | packs | Kestrel | 89.95 | 22 | 1150 |
| 6 | Day 18 backpack | packs | Kestrel | 54.00 | 0 | 640 |
| 7 | Merino socks | clothing | Woolly | 16.00 | 75 | 80 |
SELECT * and column lists
SELECT * returns every column, in the order they were defined in the table. A column list returns only the columns you name, in the order you name them. Both appear in the first example, along with a third kind of item: an expression that computes a new value.
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
-- All columns, in table order
SELECT * FROM products WHERE product_id <= 2 ORDER BY product_id;
-- Chosen columns, in the order you list them
SELECT name, price FROM products WHERE category = 'packs' ORDER BY product_id;
-- Expressions with aliases
SELECT name,
price,
stock,
price * stock AS stock_value,
ROUND(price * 0.9, 2) AS sale_price,
UPPER(category) AS category,
'in catalogue' AS note
FROM products
WHERE category = 'tents'
ORDER BY product_id;
-- An expression without an alias gets a made-up name
SELECT price * stock FROM products WHERE product_id = 3;
product_id | name | category | brand | price | stock | weight_g
------------+---------------+----------+--------+--------+-------+----------
1 | Ridge 2 tent | tents | Alpina | 249.99 | 12 | 2100
2 | Summit 4 tent | tents | Alpina | 489.00 | 3 | 4300
(2 rows)
name | price
-------------------+-------
Trail 30 backpack | 89.95
Day 18 backpack | 54.00
(2 rows)
name | price | stock | stock_value | sale_price | category | note
---------------+--------+-------+-------------+------------+----------+--------------
Ridge 2 tent | 249.99 | 12 | 2999.88 | 224.99 | TENTS | in catalogue
Summit 4 tent | 489.00 | 3 | 1467.00 | 440.10 | TENTS | in catalogue
(2 rows)
?column?
----------
1380.00
(1 row)
A few things to notice:
- The column list chose
nameandpriceand left everything else out. The result has exactly the columns you ask for. price * stock,ROUND(price * 0.9, 2)andUPPER(category)are calculated columns. They don't exist in the table. The database computes them for each row and they exist only in the result.'in catalogue'is a literal. It repeats the same value on every row. That's occasionally useful for labelling rows, for example when combining results withUNION(article 15).- The last query's calculated column had no alias, so PostgreSQL named it
?column?. Other products invent different names, such asprice * stockin MySQL and SQLite, or no name at all in SQL Server. Always alias calculated columns. ORDER BY product_idmakes the row order predictable. Without it, rows can come back in any order (article 9).

Should you use SELECT *?
SELECT * is fine for exploring a table by hand. In application code, views and reports, list the columns explicitly:
- Stability. If someone adds, drops or reorders a column,
SELECT *silently changes what your code receives. Code that reads columns by position breaks, and code that maps by name may suddenly receive a large new column it never needed. - Performance. Fetching columns you don't use costs disk reads, memory and network bandwidth. It can also stop the database from answering the query from an index alone (article 55).
- Readability. A column list documents what the query actually depends on.
Column aliases
An alias renames a result column. You write it after the expression, usually with the keyword AS:
SELECT price * stock AS stock_value, -- with AS (clearest)
price * stock stock_value_2, -- AS is optional, but easy to misread
name AS "Product name" -- quoted: keeps spaces and capitals
FROM products;
Rules and conventions:
ASis optional in the select list, but always write it. Without it, a forgotten comma such asSELECT name price FROM productssilently turnspriceinto an alias forname, and you get one column instead of two.- An unquoted alias follows the same rules as other identifiers: letters, digits and underscores, case-folded. A quoted alias (
"Product name") can contain anything and keeps its case. Use quoted aliases only for column headings in a finished report. - An alias can reuse a column name, as
UPPER(category) AS categorydid in the example. - Aliases are visible in
ORDER BY, but not inWHERE,GROUP BYorHAVINGin standard SQL, because of the logical processing order (article 4).
Arithmetic expressions
The arithmetic operators are +, -, * and /, plus a remainder operator: % in PostgreSQL, MySQL, SQL Server and SQLite, or the MOD(a, b) function, which works in PostgreSQL, MySQL and Oracle. Multiplication, division and remainder bind tighter than addition and subtraction, and operators of the same level run left to right. Parentheses override everything, and they're worth adding whenever the order isn't obvious to a reader.
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
-- SELECT without FROM: handy for trying out expressions
SELECT 2 + 3 * 4 AS precedence,
(2 + 3) * 4 AS parentheses,
7 / 2 AS int_division,
7 % 2 AS remainder,
7 / 2.0 AS decimal_division,
CAST(7 AS DECIMAL(6, 2)) / 2 AS cast_first;
-- NULL in, NULL out
SELECT name, weight_g, ROUND(weight_g / 1000.0, 2) AS weight_kg
FROM products
WHERE product_id IN (3, 4)
ORDER BY product_id;
-- Dividing by zero is an error, and the whole statement fails
SELECT name, ROUND(price / stock, 2) AS price_per_unit
FROM products
ORDER BY product_id;
-- NULLIF turns a zero divisor into NULL, so that row gets NULL instead
SELECT name, stock, ROUND(price / NULLIF(stock, 0), 2) AS price_per_unit
FROM products
WHERE category = 'packs'
ORDER BY product_id;
precedence | parentheses | int_division | remainder | decimal_division | cast_first
------------+-------------+--------------+-----------+--------------------+--------------------
14 | 20 | 3 | 1 | 3.5000000000000000 | 3.5000000000000000
(1 row)
name | weight_g | weight_kg
------------------+----------+-----------
Glow head torch | 90 | 0.09
Basecamp lantern | |
(2 rows)
psql:/workspace/solution.sql:36: ERROR: division by zero
name | stock | price_per_unit
-------------------+-------+----------------
Trail 30 backpack | 22 | 4.09
Day 18 backpack | 0 |
(2 rows)
Four lessons hide in this output:
- Integer division truncates.
7 / 2is3, because both operands are integers. PostgreSQL, SQL Server and SQLite all behave this way. MySQL returns3.5000and Oracle returns3.5. To get a decimal result everywhere, make one operand a decimal:7 / 2.0, orCAST(7 AS DECIMAL(6, 2)) / 2. This catches people out most often when computing averages or percentages from integer counts. - NULL propagates. Any arithmetic with a NULL operand gives NULL. The lantern's weight is unknown, so its weight in kilograms is unknown too. Article 8 shows how to substitute a value with
COALESCE. - Division by zero is an error. The
Day 18 backpackhas zero stock, and the whole statement failed. Not one row was returned, even for the products with stock. A query either succeeds as a whole or fails as a whole. PostgreSQL, SQL Server and Oracle raise this error. MySQL and SQLite instead return NULL forx / 0in a query (MySQL with a warning), which hides the problem rather than solving it. NULLIF(x, 0)avoids the error.NULLIFreturns NULL when its two arguments are equal, so the divisor becomes NULL instead of zero, and that one row gets NULL. This is the standard idiom for safe division.
String expressions
The standard operator for joining strings is ||. PostgreSQL, Oracle, SQLite and SQL Server 2025 support it. MySQL doesn't by default, as article 2 explains. There's also a CONCAT() function, available in PostgreSQL, MySQL, SQL Server and Oracle (which only accepts two arguments).
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
-- Standard concatenation with ||
SELECT name || ' (' || category || ')' AS label
FROM products
WHERE product_id IN (1, 4)
ORDER BY product_id;
-- || with a NULL gives NULL; CONCAT treats NULL as an empty string
SELECT name,
brand || ' ' || name AS with_pipes,
CONCAT(brand, ' ', name) AS with_concat,
CONCAT_WS(' / ', brand, category, name) AS with_separator
FROM products
WHERE product_id IN (3, 4)
ORDER BY product_id;
label
-----------------------------
Ridge 2 tent (tents)
Basecamp lantern (lighting)
(2 rows)
name | with_pipes | with_concat | with_separator
------------------+-----------------------+-----------------------+------------------------------------
Glow head torch | Lumen Glow head torch | Lumen Glow head torch | Lumen / lighting / Glow head torch
Basecamp lantern | | Basecamp lantern | lighting / Basecamp lantern
(2 rows)
The difference between the two is NULL handling. The lantern has no brand, so:
brand || ' ' || nameis NULL: NULL propagates through||like it does through arithmetic. (Oracle is the exception: it treats NULL as an empty string in||.)CONCAT(brand, ' ', name)skips the NULL, but kept the separator, so the result starts with a space. MySQL is the exception: itsCONCATreturns NULL if any argument is NULL, so useCONCAT_WSorCOALESCEthere.CONCAT_WS(separator, ...)("with separator") puts the separator between the non-NULL arguments only, so a missing value doesn't leave a stray separator. It's available in PostgreSQL, MySQL and SQL Server.CONCAT_WSisn't part of the SQL standard.
Functions
A scalar function takes values from one row and returns one value, such as ROUND(price, 1), UPPER(name), LENGTH(name) or ABS(x). Part 8 covers the important ones by data type. Aggregate functions such as SUM and COUNT are a different kind: they summarize many rows into one, and Part 4 covers them.
Function names are where dialects differ most. For example, the length of a string is LENGTH in PostgreSQL, MySQL, Oracle and SQLite but LEN in SQL Server, and the current date and time is CURRENT_TIMESTAMP in the standard, NOW() in PostgreSQL and MySQL, SYSDATE in Oracle, and GETDATE() in SQL Server. When there's a standard spelling that your product supports, prefer it.
SELECT without FROM
To evaluate an expression without reading a table, most products let you leave out FROM:
SELECT 2 + 3, UPPER('trailhead'), CURRENT_DATE; -- PostgreSQL, MySQL, SQL Server, SQLite, Oracle 23ai+
Older Oracle versions require a FROM clause, so they provide a one-row dummy table called DUAL. You'll see SELECT SYSDATE FROM dual all over existing Oracle code. MySQL also accepts FROM DUAL, for compatibility.
Table names and qualified columns
You can prefix a column with its table name, as in products.price. That's called a qualified column name. With one table it's optional, but once queries join several tables (Part 3), you'll need it whenever two tables have a column with the same name. A table alias keeps the prefix short:
SELECT p.name, p.price
FROM products AS p -- Oracle doesn't accept AS here: write FROM products p
WHERE p.stock > 0;
What's current
- Oracle 23ai and later allow
SELECTwithoutFROM, soFROM DUALis no longer required, although it still works. - SQL Server 2025 supports the standard
||operator. Unlike+, it converts non-string operands to text, so1 || 'a'gives'1a'instead of an error. CONCATandCONCAT_WSare available in all the major products except that Oracle'sCONCATtakes exactly two arguments and Oracle has noCONCAT_WS. SQLite added both in version 3.44.
Common questions
Does the order of columns in the select list matter? Only for the result: columns come out in the order you list them. It has no effect on which rows are returned or on performance.
Can I select the same column twice?
Yes. SELECT name, name FROM products is valid and returns two identical columns. It's usually pointless, but selecting the same column with different transformations, such as price and ROUND(price), is common.
Why did my calculated column get a strange name?
Because you didn't alias it. Each product makes up its own name, such as ?column? in PostgreSQL. Write AS something.
What's the difference between 'name' and name in a select list?
name is the column, so you get each product's name. 'name' is a string literal, so you get the word "name" on every row. Single quotes always mean a string.
Does ROUND(2.5) give 2 or 3?
For exact DECIMAL and NUMERIC values, PostgreSQL, MySQL, Oracle and SQL Server round half away from zero, so ROUND(2.5) is 3 and ROUND(-2.5) is -3. (SQL Server requires the second argument: ROUND(2.5, 0).) For floating-point values, the result can differ by product and because the stored binary value may be slightly above or below .5. Article 38 covers rounding and number types.
Why isn't 0.1 + 0.2 exactly 0.3?
With DECIMAL/NUMERIC values it is exactly 0.3. With floating-point types (REAL, DOUBLE PRECISION, FLOAT), it's 0.30000000000000004, because those types store binary approximations. Use DECIMAL for money (article 35).
Summary
- The select list decides the result's columns: column names,
*, expressions, function calls and literals. - Use
SELECT *for exploring, and explicit column lists in code. - Always alias calculated columns with
AS. Aliases are usable inORDER BYonly, in standard SQL. *,/and%bind tighter than+and-. Use parentheses when in doubt.- Integer division truncates in PostgreSQL, SQL Server and SQLite. Make one operand a decimal for a decimal result.
- Arithmetic and
||with a NULL give NULL. Division by zero fails the whole statement in PostgreSQL, SQL Server and Oracle (MySQL and SQLite return NULL), so guard withNULLIF(divisor, 0). ||is the standard concatenation operator.CONCATskips NULLs (except in MySQL, where it returns NULL), andCONCAT_WSadds separators only between non-NULL values.
Practice questions
What does this query return in PostgreSQL, and why?
SELECT 10 / 4 AS a, 10 / 4.0 AS b, 10 % 4 AS c, -7 / 2 AS d;
Answer
a is 2, because integer division truncates. b is 2.5000000000000000, because one operand is a decimal. c is 2, the remainder. d is -3: integer division truncates toward zero, so -3.5 becomes -3, not -4.
A developer wants each product's price including 20% tax, and writes SELECT name, price + price * 20 / 100 AS gross FROM products. Is the result right? Would price * (1 + 20 / 100) also work?
Answer
The first expression is right. * and / run before +, and they run left to right, so it computes price * 20, then divides by 100, then adds the price. Because price is a decimal, the division keeps its decimals.
The second is wrong: 20 / 100 is integer division, which gives 0, so the expression becomes price * 1, which is just the price. Write price * 1.20 or price * (1 + 20.0 / 100).
Write a query that shows each product as one string such as Alpina: Ridge 2 tent, and shows just the name when the brand is missing.
Answer
SELECT CONCAT_WS(': ', brand, name) AS label
FROM products
ORDER BY product_id;
CONCAT_WS skips the NULL brand along with its separator, so the lantern appears as Basecamp lantern. With brand || ': ' || name, the lantern's label would be NULL. With CONCAT(brand, ': ', name), it would be : Basecamp lantern, or NULL in MySQL.
Why does this query return one column instead of two?
SELECT name price FROM products;
Answer
There's no comma, so price is treated as an alias for name (AS is optional). The result has one column, called price, containing the product names. Always separating items with commas and always writing AS before aliases makes this mistake easy to see.
Previous: How a SQL Query Is Processed: The Logical Order of SELECT Clauses
Next: Filtering Rows With WHERE: Comparison and Logical Operators

More in SQL Course
Quiz: SQL Dialect Differences (PostgreSQL, MySQL, SQL Server, Oracle, SQLite)
Twenty-four questions on how the major SQL databases differ: limiting rows, concatenation, empty strings, identifier case, upserts, identity columns, transactional DDL, error handling, isolation defaults, booleans, set operations, RETURNING, dates and typing.
Quiz: SQL Delicate Details
Twenty-five questions on SQL's subtle corners: NULLs in aggregates, IN and CHECK, sort order of NULLs, rounding, CHAR padding, window frame defaults, identity gaps, LIKE escapes, empty groups, set operations and multi-column updates.
Quiz: SQL Interview Questions
Thirty SQL questions of the kind asked in developer and analyst interviews: WHERE vs HAVING, DELETE vs TRUNCATE, keys, joins, duplicates, Nth-highest values, window functions, ACID, isolation, locking, indexes, views, procedures and performance.
Quiz: SQL Course Final Exam (All 15 Parts)
The final exam for the SQL course: thirty questions across all fifteen parts, from query processing, NULLs, joins and aggregation through windows, data changes, types, design, indexes, security, transactions, procedural SQL and tuning.
Discussion
Sign in to ask a question or share what you learned.