7 examples to learn SQL select from statement

The select statement of SQL

  • The select statement is used to retrieve data from database’s tables.
  • The SQL select statement retrieves existing data from the tables in the form of the result-sets.
  • The select from statement can retrieve whole data from a table in a single statement.
  • You can also fetch specific columns or fields data by using the Select query.
  • You can limit the fetched records, as per requirement, by using the Where clause with select statement
  • The Select statement can also fetch data from more than one tables in a single select statement, explained in the SQL table joins chapter.

An example of Select

Select with where clause example

See examples below to learn more about how you can use the select command to fetch whole table’s data, specific columns, and limited rows data.

Syntax of Select statement

The general syntax of the select statement is:

Select * from table_name

The above command will fetch whole data from the given table by using SQL select from query.

To retrieve only specific columns:

You can specify only a few columns that are needed rather fetching the whole table data in the select from command. That will also help in reducing the load on the server.

Select col1, col2, col3 from table_name

To fetch only limited rows as per given criteria

To fetch limited rows you can use the Where clause and specify the condition(s) in the select from statement. The general syntax is:

Select * from table_name

Where col_1 = val1

Examples of using select SQL statement

Let us show you examples of using the select with our created database and tables. We will use the same database, test_db and tbl_employee table, that we created in the Create table chapter, to show a few examples of the Select command.

Just to remind this is how our example table is created in the test_db:

Select example – retrieving whole data

This is how we can retrieve whole table data of tbl_emploee, by using the Select statement.

The select statement will be:

Demo table after select statement

Retrieving only specific columns

Let us suppose, we only need employee names and salaries from the tbl_employee table. While the other information is not useful in the current scenario. In that case, we should only fetch the required columns rather fetching the whole table by using select statement. It will reduce overhead in the server as well.

The SQL statement will be:

DEMO table after select statement

Select from example of retrieving only limited rows

Generally SQL tables may contain hundreds of thousands of rows over the period of time, depending on a project nature. For example, customer table of a departmental store may get thousand of entries each day to its database with item details.

What if a customer returns after a day or so to return an item and shows the receipt and the Store department has to confirm this by comparing to its database by entering receipt id or customer id?

If that table contains thousands of rows, logically it should only return required rows of data rather whole table.

This example shows you how to fetch only single row of data based on emp_id, from our example table tbl_employee by using the select from statement. We will use the Where clause in SQL select query.

The Select query will be:

Demo Table after select with where clause

Note that, you can change the order of columns in a select statement than the column sequence in a table physically.

Retrieving data from multiple tables

You can also use the Select command to retrieve data from multiple tables. One of the ways is to use the joins. Due to its complexity, we have dedicated a few chapters to SQL join and its types. The links are given below:

Retrieve data by using join clause

The inner join

The left join

Examples of using join in select statement

Following are a few online examples of using the join with the select statement.

An example of inner join
A simple join example

Using functions in the SQL queries

You can also use built-in functions in the select statement of SQL. For example aggregate functions to perform the calculation on columns data. Similarly, getting the total count of the rows, sum of a numeric column etc.

The SQL functions are explained in their respective chapters with examples in following links:

Functions of SQL

The Count function

The Sum function

A few examples of using the functions in the Select statement are:

A count function example
A sum function example


Was this article helpful?

Related Articles

Leave A Comment?