AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL SELECT |
|
SQL > SQL Commands >
Select
The SELECT statement in SQL is used to retrieve data from a relational database.
Key Takeaway: SELECT is the most fundamental SQL statement. Every data retrieval query begins with SELECT, followed by the columns you want and the FROM clause specifying the table.
Syntax
"table_name" is the name of the table where data is stored, and "column_name" is the name of the column containing the data to be retrieved. To select more than one column, add a comma to the name of the previous column, and then add the column name. If you are selecting three columns, the syntax will be, Note there is no comma after the last column selected. Special CaseNote that the FROM keyword appears in all of the scenarios above, since the FROM keyword is used to indicate which table(s) one is retrieving the data from. There is one special case where FROM does not exist, and that is when you are doing a mathematical operation. In this case, the syntax is simply, ExamplesWe will provide examples for each of the following four use cases: Let's use the following table to illustrate all three cases: Table Store_Information
Example 1: Select one columnTo select a single column, we specify the column name between SELECT and FROM as follows: Result:
Example 2: Select multiple columnsWe can use the SELECT statement to retrieve more than one column. To select Store_Name and Sales columns from Store_Information, we use the following SQL: Result:
Example 3: Select all columnsThere are two ways to select all columns from a table. The first is to list the column name of each column. The second, and the easier, way is to use the symbol *. For example, to select all columns from Store_Information, we issue the following SQL: Result:
Example 4: Math operationIf we want to use the SELECT statement to calculate 2+3, we issue the following SQL: Result:
ExercisesFor these exercises, assume we have a table called Users with the following columns: Table Users
1. Which of the following SQL statement is incorrect? (There can be more than one answer)
2. (True Or False) In SQL, the order of the columns in a SELECT statement must be the same as the order of the columns in the underlying table. For example, in the table Users, you must select First_Name before Last_Name. 3. (True Or False) The following two SQL statements are equivalent:
Frequently Asked QuestionsWhat does SELECT * mean in SQL?SELECT * means "select all columns." The asterisk (*) is a wildcard that tells SQL to return every column in the table without you having to name them individually. Can I SELECT from multiple tables at once?Yes. You can use SQL JOIN to combine columns from multiple tables, or list multiple tables in the FROM clause with a WHERE condition to link them together. What is the ORDER of clauses in a SELECT statement?The standard order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY. Each clause is optional except SELECT and FROM. Does SELECT change data in the database?No. SELECT is a read-only operation. It retrieves data without modifying it. To change data, you would use INSERT, UPDATE, or DELETE statements.
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.