A query can retrieve information from specified columns or from all columns in a table. To create a SQL SELECT command, you clearly specify the table name and column names. The entirety of this query is known as the SQL SELECT command.
Syntax of SQL SELECT Command
Constructing the Query: SELECT column_list FROM table-name
[Condition Filter]
[Grouping Data]
[Applying Conditions]
Sorting with [ORDER BY clause];
In which:
table-name: refers to the table from which information is retrieved.
column_list includes one or more columns from the retrieved data.
Content within parentheses is optional.
Illustrative Example of SQL SELECT Statement
Below presents the student_details database table:
Note: The aforementioned database table is utilized to elucidate SQL commands better. In reality, tables may encompass various columns and data.
For example, in the database table providing student information above, to select the names of all students in the table, you would use the query:
SELECT first_name FROM student_details;
Some Syntax Notes on SQL SELECT Statement
Note: SQL commands are case-insensitive. The SELECT statement above could be written as follows:
'Choosing the first name from students_details;'
Additionally, you can retrieve data from multiple columns. For example, to select the first name and last name of all students in the table, you would use the following query:
SELECT first_name, last_name FROM student_details;
You can also utilize clauses such as WHERE, GROUP BY, HAVING, ORDER BY with the SQL SELECT statement.
Note: In the SELECT statement syntax, only the SELECT and FROM clauses are mandatory, while clauses such as WHERE, ORDER BY, GROUP BY, HAVING are optional.
Utilizing Expressions in SQL SELECT Statement
Expressions combine multiple arithmetic operators and can be used in SELECT, WHERE, and ORDER BY clauses in SQL SELECT statement. In the following section, Mytour will explain how to use expressions in SQL SELECT statement.
If multiple operators are used within the same expression, the operators will be evaluated according to precedence order. The evaluation order includes: parentheses, division, multiplication, addition, and subtraction. Evaluation is performed from left to right within the expression.
Example of SQL SELECT Statement
If you want to display the combined name including both first name and last name of employees, the SQL SELECT statement would appear as follows:
SELECT first_name + ' ' + last_name FROM employee;
Output:
Additionally, you can provide aliases as shown below:
SELECT first_name + ' ' + last_name AS emp_name FROM employee;
Output:
To delve deeper into the SQL SELECT statement, readers are encouraged to refer to some examples in the above article by Mytour and explore additional examples. Additionally, readers can explore other articles on commands such as SQL CREATE statement on Mytour.
