SQL SELECT Statement - SELECT Multiple
The SELECT statement in SQL is used to retrieve data from a table. This statement can be used to extract one or more columns from a table.
Syntax
The syntax for SELECT multiple columns is as follows:
SELECT column_name_1, column_name_2, ..., column_name_n
FROM table_name;
Example
Consider a table named "students" with the following columns:
student_id | first_name | last_name | age |
---|---|---|---|
1 | John | Doe | 24 |
2 | Jane | Smith | 21 |
3 | Bob | Johnson | 23 |
4 | Anna | Lee | 22 |
The following SQL statement will retrieve the "first_name," "last_name," and "age" columns from the "students" table:
SELECT first_name, last_name, age
FROM students;
Output
The above SQL query will output the following result:
first_name | last_name | age |
---|---|---|
John | Doe | 24 |
Jane | Smith | 21 |
Bob | Johnson | 23 |
Anna | Lee | 22 |
Explanation
The SELECT statement is used to select three columns, "first_name," "last_name," and "age" from the "students" table. The FROM keyword specifies the table from which to retrieve the data.
Use
The SELECT statement with multiple columns is useful when we need to extract data from a table for analysis or reporting purposes.
Important Points
- Multiple columns can be selected using a comma-separated list of column names.
- The order of the columns in the SELECT statement determines the order of the columns in the resulting output.
- If a column does not exist in the table, an error will be thrown.
Summary
- The SELECT statement is used to retrieve data from a table.
- SELECT statement can retrieve one or more columns from a table.
- The syntax for SELECT multiple columns involves a comma-separated list of column names following the SELECT keyword.
- The order of the columns listed in the SELECT statement determines the order of the columns in the output.
- SELECT statement is useful when we need to extract data from a table for analysis or reporting purposes.