Syntax : SELECT * FROM Table_Name; |
For Example : Consider the table of student_details. FROM clause can be used to fetch all the data present in the student_details table. Below is the query.
Table : Student_details
Query : SELECT * FROM student_details; |
Syntax : SELECT * FROM Table_Name WHERE “Condition”; |
For example : Consider the table of student_details. WHERE clause can be used to fetch specific data present in the student_details table. Below is the query.
Table : student_details
Query : SELECT * FROM student_table WHERE Roll_No = 1; |
Syntax : SELECT DISTINCT Column_Name1, Column_Name2,… FROM Table_Name; |
For example : Consider the table of student_details. Distinct clause can be used to fetch unique data present in the student_ details table. Below is the query.
Table : student_detailsQuery : SELECT DISTINCT City FROM student_details; |
Syntax : SELECT Column1, Column2,… FROM Table_Name GROUP By Column1; |
For example : Consider the table of student_details. GROUP BY clause can be used to fetch summarized data present in the student_ details table on the basis of gender. Below is the query.
Table : student_details
Query : SELECT Count(Roll_No), Gender FROM student_details GROUP By Gender; |
Syntax : SELECT Column1, Column2,… FROM Table_Name GROUP By “Condition” HAVING “Condition”; |
For example : Consider the table of student_details. HAVING clause can be used to fetch data present in the student_ details table on the basis of location. Below is the query.
Table : student_details
Query : SELECT Count(Roll_No), Location FROM student_details GROUP BY Location HAVING Count(Roll_No) >= 2; |
Syntax : SELECT Column1, Cloumn2,… FROM Table_Name ORDER BY Column_Name ; (Default/Ascending); |
OR
Syntax : SELECT Column1, Cloumn2,… FROM Table_Name ORDER BY Column_Name ASC; (For Ascending Order); |
OR
Syntax : SELECT Column1, Cloumn2,… FROM Table_Name ORDER BY Column_Name DESC; (For Descending Order); |
Example-1 : Consider the table of student_details. To sort the data according to the location to which student belongs can be done by executing the below query.
Table : student_details
Query : SELECT * FROM student_details ORDER BY Location DESC; |
NOTE : Sorting based upon alphabets of location i.e. “N” for Noida, “G” for Ghaziabad and “D” for Delhi. Similarly above sorting can be done in ascending order too using “ASC” in place of “DESC”.
Example-2 : ORDER BY clause also supports SQL aggregate functions. Consider the same table used above and below is the sample query for ORDER BY with SQL aggregate functions.
Query : SELECT Count(Roll_No), Location From student_details GROUP BY Location ORDER BY Count(Roll_No) DESC; |