Summary: in this tutorial, you will learn how to use the basic SQL SELECT statement to query data from a table.
When you work with databases, querying data from a table is one of the most common tasks that you have to deal with on a regular basis. To query data from one or more tables, you use the SQL SELECT statement.
SQL SELECT statement syntax
The following illustrates the SQL SELECT statement syntax:
GROUP BY column1
ORDER BY column1,column2 ASC | DESC
The SQL SELECT statement is made up of several clauses:
- SELECT: specifies which columns in database tables to include in the result or result set.
- FROM: specifies the tables that you want to query data from. You can also use the FROM clause to join multiple tables.
- WHERE: filters unwanted records or rows in the result.
- GROUP BY: groups rows by common column values.
- HAVING: uses with the GROUP BY clause to filter unwanted groups.
- ORDER BY: sorts the result by one or more columns.
The SQL SELECT statement only requires the
SELECT clause to function properly. The other clauses e.g.,
ORDER BY are optional. However, you always need to combine several clauses together to construct practical queries.
In this tutorial, we are going to focus on the
FROM clauses. Let’s get started.
Select all data from a table
Before you want to query the data, ask yourself a question: which table do I get the data from? to answer this question you need to use the
FROM clause. For example, to get all data from the
employees table, you use the following query:
In this query, you put the
employees table after the keyword
FROM, and asterisk character (
*) after the
SELECT keyword. The query asks the database server to return all rows and all columns of the
employees table. The asterisk (
*) character is the shorthand of all columns.
You can specify all the columns of the
employees table explicitly in the
SELECT clause as follows:
The result set is the same as the first query because all the columns of the
employees tables are specified in the
Select a subset of columns in a table
In practice, you rarely get the data from all the columns of a table. You may only get the data from a subset of columns. In order to do so, you need to specify the needed columns in the
SELECT clause. For example, if you want to get only
lastName of employees, you can use the following query:
If multiple columns are listed, they have to be separated by a comma (
,) for example:
SQL SELECT statement with other elements
The SQL SELECT clause accepts not only the columns of a table but also the following elements:
- Strings or Numbers
- SQL function calls
- User-defined function calls.
The following example illustrates how to use the SQL SELECT statement with a simple expression:
SELECT 1 + 1
This example shows you how to combine a SQL string function named concat, which combines two or more string into one, to displays employee’s full name:
SELECT CONCAT(LastName,', ',FirstName) AS fullname
A SQL alias is used to format the output in this example.
In this tutorial, you have learned how to use the basic SQL SELECT statement to query data from table. In addition, you have also learned how to use SELECT statement with expression and function call.