In this post we are going to learn how the select statement works in MySql.
A select statement is used to retrieve the data from the database in the form of tables. The select statement produces result set as a resultant. A result set is a combination of rows and columns. We can also obtain subset of rows or columns or both of its combination by this select statement.
Let us see the syntax of select statement.
The select statement contains different clauses. Let us go through with the most commonly used clauses and syntax of them:
1. select expression:
This represents a column which we want to retrieve. But there should be at least one select_expression.
Before that just make sure that on which database you are performing.
Ex: Consider 8+6 is an expression.
We can also change the column_name by using ‘as’ keyword. ‘as’ is an optional clause. ‘as’ acts as ‘alias’ name for row/column/expression.
Here 8+6*2 expression is renaming as Add_mul. We can also rename this without using ‘as’ clause.
If you observe the above two outputs we can conclude that ‘as’ is optional clause.
2. select expression from tablename;
This syntax is used to retrieve a column/multiple column/all columns. From the table. If you want to display the total data from the table we can use “*” (asterisk) character.
The syntax is as follows;
|select * from table_name;|
If you want to retrieve only ‘name’ column from the ‘Players’ table.Then
Similarly we can also retrieve two columns from the table which is as follows.
We can also replace the ‘name’ column with ‘first_name’ and it is shown in below;
Here you can observe that the name column is renamed with first_name.
3.select * from table_name where condition;
The select statement with where clause includes with conditions. ‘Where’ clause filters the rows or records from the table.
Ex: The condition for the following query is to display the name and sport column which is belonging only to “Russia”.
If you want to display the all the data where the sport column contains “Tennis”.Then,
Similarly we can display the data where name=”Dhoni”.
To watch this tutorial post on YouTube Channel.