VMware ESXi and vSphere Cluster Management

SQL SELECT Statement: Retrieve Columns and Rows from a Table

Learn SQL SELECT syntax to retrieve specific columns or every column from a table, understand result sets, and troubleshoot common query errors.

The SQL SELECT statement reads data from one or more database tables and returns it as a result set. A result set contains the columns and rows produced by a query.

This lesson focuses on reading data from one table. You will learn how to choose specific columns, request every column with the * wildcard, and interpret the returned rows.

Key SQL terms

  • Table: A structured collection of related rows and columns in a database.
  • Column: A named field in a table, such as name or address.
  • Row: One record in a table.
  • SELECT: The SQL keyword used to specify data to retrieve.
  • FROM: The SQL keyword that identifies the source table or tables.
  • Result set: The rows and columns returned by a query.
  • Wildcard: The asterisk (*) notation used with SELECT to request all columns.
  • Identifier: The name of a database object, such as a table or column.
  • Schema: The database structure that defines tables, columns, and related objects.

Purpose of the SELECT statement

A basic query answers two questions: Which data should be returned? and Which table contains that data? The SELECT clause identifies the columns, while the FROM clause identifies the source table.

For example, this query requests the name and city columns from the Customer table:

SELECT name, city FROM Customer;

The database evaluates the query and returns a result set with those columns and the eligible rows. Because this query has no row-filtering clause, every row in Customer is eligible to appear.

Basic SELECT syntax

SELECT column_name1, column_name2 FROM table_name;
  • SELECT begins the query and specifies the data to retrieve.
  • column_name1, column_name2 is a comma-separated list of columns. Add each requested column without repeating the comma after the final name.
  • FROM identifies the source table.
  • table_name is the name of the table that contains the requested columns.
  • The semicolon conventionally marks the end of the SQL statement. Some client tools accept a statement without it, but using it consistently improves readability and portability.

The order of the columns in the result follows the order in the SELECT list. The order of rows is not a presentation guarantee unless you later add an ordering clause such as ORDER BY.

Example Customer table

Assume the database contains a table named Customer with this schema:

Customer(id, name, address, city, state, zip)

Here is sample data from that table:

id | name          | address         | city        | state | zip
---+---------------+-----------------+-------------+-------+-----
1  | Ada Lovelace  | 12 Binary Lane   | London      | LD    | 10001
2  | Grace Hopper  | 34 Compiler Road | Arlington   | VA    | 22201
3  | Linus Torvalds | 56 Kernel Avenue | Portland    | OR    | 97205

The table has six columns. Each line after the header represents one row, or customer record.

Selecting specific columns

To return only the fields needed by an application or person, name those columns explicitly:

SELECT id, name, address FROM Customer;

The result set contains three columns, in the order id, name, and address:

id | name           | address
---+----------------+-----------------
1  | Ada Lovelace   | 12 Binary Lane
2  | Grace Hopper   | 34 Compiler Road
3  | Linus Torvalds | 56 Kernel Avenue

The source table has six columns, but this result set has only three. Every eligible customer row still appears because the query does not filter rows; the query only controls which columns are returned.

Selecting only the required columns is usually clearer and preferable to retrieving every field. It makes the intended result obvious, avoids transferring unnecessary data, and is less affected by unrelated columns added to the table later.

Changing the result column order

The column list also controls presentation order:

SELECT city, name, id FROM Customer;

This result begins with city, followed by name and id, regardless of where those columns appear in the table definition.

Selecting every column with the wildcard

The asterisk is a wildcard that requests every column in the source table:

SELECT * FROM Customer;

With no filtering clause, the result includes every column and every row:

id | name           | address         | city      | state | zip
---+----------------+-----------------+-----------+-------+-----
1  | Ada Lovelace   | 12 Binary Lane   | London    | LD    | 10001
2  | Grace Hopper   | 34 Compiler Road | Arlington | VA    | 22201
3  | Linus Torvalds | 56 Kernel Avenue | Portland  | OR    | 97205

The returned column order normally follows the table definition or the behavior of the particular database system. Do not rely on SELECT * for a custom presentation order.

Explicit columns versus the wildcard

SELECT id, name FROM Customer;

This query returns exactly two requested columns:

id | name
---+---------------
1  | Ada Lovelace
2  | Grace Hopper
3  | Linus Torvalds
SELECT * FROM Customer;

This query requests the full row, including id, name, address, city, state, and zip. An explicit column list controls the shape of the result; * requests all available columns.

Identifiers and SQL readability

Table names and column names are identifiers. In SELECT id, name FROM Customer;, Customer is the table identifier, while id and name are column identifiers.

Use uppercase SQL keywords and consistent identifier formatting to make queries easy to read:

SELECT id, name, address
FROM Customer;

SQL keywords are commonly written in uppercase, but many database systems treat keywords as case-insensitive. Identifier rules differ between systems. Table and column name case sensitivity, reserved words, and quoting conventions vary by database. Use the exact names defined by the database schema and follow the quoting syntax required by your system when an identifier contains special characters or conflicts with a reserved word.

How rows are returned

A basic query such as SELECT name FROM Customer; has no WHERE clause. Therefore, each row in Customer is eligible to appear in the result set. Selecting fewer columns does not select fewer rows; it only makes each returned row narrower.

When you need only some rows, use a WHERE clause. Row filtering is a separate SELECT concept:

SELECT id, name
FROM Customer
WHERE state = 'VA';

The important distinction is that the SELECT list chooses columns, while WHERE determines which rows qualify.

Troubleshooting SELECT queries

A column does not exist

Likely causes: The column is misspelled, its name differs from the actual schema, or it requires database-specific identifier quoting.

Resolution: Inspect the table definition and use the exact column identifier supported by the database.

A table does not exist

Likely causes: The table name is incorrect, the table belongs to another schema, or the current database context is wrong.

Resolution: Verify the active database, table name, and any required schema qualification. A system may require a qualified name such as schema_name.Customer.

The query returns more columns than intended

Cause: The query uses SELECT *.

Resolution: Replace the wildcard with an explicit list of required columns:

SELECT id, name, address FROM Customer;

The query returns every row

Cause: A basic SELECT without a filtering clause returns all rows from the source table.

Resolution: Add a WHERE clause when only a subset of rows is required.

The output columns are in the wrong order

Cause: SELECT * follows the table's column arrangement rather than a custom order.

Resolution: Name the columns explicitly in the preferred order.

Exam-relevant notes

  • SELECT specifies the data or columns to retrieve.
  • FROM specifies the source table or tables.
  • Separate multiple selected columns with commas.
  • SELECT * requests every column, not just every row.
  • Without WHERE, every source-table row is eligible for the result.
  • The actual table and column identifiers must match the database schema.
  • An explicit column list is generally clearer and safer than SELECT * when the required fields are known.

Summary

Use SELECT to read data and return a result set. Use FROM to identify the source table. List column names when you need a precise, stable result, or use * as shorthand for all columns. A query without a WHERE clause can return every row in the source table.

Continue with SQL SELECT Statement concepts such as filtering, sorting, and limiting results as you build more advanced queries.