SQL is providing a command using which we can easily show or check rows of a table.
In lots of situation, we may require to fetch all the rows. In simple language, we may need to see all the rows available in a table. For this we are going to use select command of SQL.
The syntax of the command is below:
SELECT * FROM TABLENAME;
What is the ‘*’ in the command?
‘*’ or asterisk is used to refer ‘all’
Here we are instructing mysql engine to show all the rows of a table.
Lets take following table as example:
Similar table is existing in our database.
A requirement came to view all the rows of the table. Therefore the syntax would be:
SELECT * FROM NEW_STUDENT_MARKS
(semicolon is optional to the end of the statement)
Now, lunch the xampp, select the database, then click on ‘SQL’ in the menu panel, command box will appear. Inside the command box, type the select statement command and click on go.
Status message says ‘Showing rows 0-4’ because we have 5 rows in the table. It is showing all the rows from the table.
In other situations, we may not require to show or fetch all the rows. We may require selective rows depending upon the condition. Therefore the syntax to fetch selective rows with condition using select statement from SQL is as below.
SELECT * FROM TABLE_NAME WHERE (CONDITION)
Lets assume, a requirement came to fetch all the information from a table who has secured 99 marks in math.
In such scenario, our select statement command would be as below.
Select * from new_student_marks where marks=99
In command box, type the above command and then click on go.
See the above table is showing only two results.
In other situations, we may require to selective single column values depending upon a condition. We can achieve this using select statement. The syntax would be as below:
Select column_name, column_name from table_name where (condition)
Suppose a requirement came to fetch or show roll_no only who has secured more than 90 in math.
Select roll_no from new_student_marks where marks>90
Type the command in the command box of sql and run it. You will get following result.
See the result is showing only one column values.
In other situation we may require to fetch multiple column values depending upon a condition.
In the same example, lets fetch roll_no, student_name and marks who has secured more than 90 in math.
The syntax is as below:
select roll_no, student_name, marks from new_student_marks where marks>90
Run the above command in the SQL command box, and you would get following result.
> Fetch roll_no, student_name from the table (With out any condition clause)
Towards the beginning of this chapter, we have learned to fetch all the rows with all the column values and the syntax was as below
Select * from table_name
But if we are required to fetch all the rows but with certain columns only then the syntax would be as below:
Select column_name,column_name, column_name from table_name;
Therefore the command would be as below:
select roll_no, student_name from new_student_marks;
Run the above command in the SQL Command box and you would get following result.