TExcelTAD
Home › SQL › SQL SELECT

SQL SELECT

The SELECT statement reads data. Its result is always a new table — called a result set — with the columns you asked for.

sql
SELECT column1, column2, ...
FROM table_name;

Choosing columns

List the columns you want, separated by commas, in the order you want them:

sql · try it yourself
SELECT name, city, country
FROM suppliers
ORDER BY name;
name                | city      | country
--------------------+-----------+------------
Alpine Dairy Co.    | Zurich    | Switzerland
Byte Press          | Bangalore | India
Crunch Time Snacks  | Austin    | USA
Golden Crust Bakery | Lyon      | France
Green Valley Farms  | Valencia  | Spain
Hearth & Home       | Oslo      | Norway
Highland Roasters   | Edinburgh | UK
Volt Gadgets        | Osaka     | Japan
(8 rows)

Tip: ORDER BY sorts the result. Without it, the database may return rows in any order — we will use it in almost every example so the output is predictable.

Selecting every column

The asterisk * means "all columns, in table order":

sql · try it yourself
SELECT * FROM categories WHERE category_id <= 3;
category_id | name      | description
------------+-----------+------------------------------------
1           | Beverages | Coffee, tea, juices and soft drinks
2           | Bakery    | Bread, pastries and cakes
3           | Dairy     | Milk, cheese and yogurt
(3 rows)

SELECT * is handy for exploring, but in application code list the columns explicitly: the query keeps working when someone adds a column, and the database transfers less data.

Calculated columns

A select list can contain expressions, not just column names. Give a calculated column a readable name with AS:

sql · try it yourself
SELECT name,
       price,
       stock,
       price * stock AS stock_value
FROM products
WHERE category_id = 7
ORDER BY name;
name           | price | stock | stock_value
---------------+-------+-------+------------
Cast Iron Pan  | 38    | 10    | 380
Chef Knife     | 45    | 15    | 675
Coffee Grinder | 59.9  | 9     | 539.1
(3 rows)

Expressions can use arithmetic (+ - * / %), functions such as ROUND() or UPPER(), and the string concatenation operator ||:

sql · try it yourself
SELECT first_name || ' ' || last_name AS full_name,
       UPPER(title) AS title,
       ROUND(salary / 12.0, 2) AS monthly_salary
FROM employees
WHERE manager_id = 1
ORDER BY employee_id;
full_name     | title              | monthly_salary
--------------+--------------------+---------------
Victor Ames   | SALES MANAGER      | 8166.67
Lin Zhou      | OPERATIONS MANAGER | 7916.67
Elena Popescu | DATA ANALYST       | 6000
(3 rows)
Database Concatenation
SQLite, PostgreSQL, Oracle `a
MySQL CONCAT(a, b) (`
SQL Server a + b or CONCAT(a, b)

SELECT without a table

In SQLite (and PostgreSQL, MySQL, SQL Server) you can select plain values — useful as a calculator or to test functions:

sql · try it yourself
SELECT 7 * 6 AS answer, 'TAD' || ' Market' AS shop, 10 / 4 AS int_div, 10 / 4.0 AS real_div;
answer | shop       | int_div | real_div
-------+------------+---------+---------
42     | TAD Market | 2       | 2.5
(1 row)

Note that dividing two integers gives an integer (10 / 4 = 2); make one side a decimal number to get 2.5. Oracle needs FROM dual for table-less selects.

Test yourself

What does SELECT * FROM products return?

  1. Only the first column
  2. All columns of every row
  3. The number of rows
  4. Only the primary key

Which keyword gives a calculated column a name?

  1. NAME
  2. AS
  3. LABEL
  4. SET

In SQLite, what is SELECT 10 / 4;?

  1. 2.5
  2. 2
  3. 3
  4. An error