In Structured Query Language, the SELECT statement is the most regularly used command. It's used to access data in one or more database tables and views. It also retrieves the data that meets the criteria we specify.
We may also obtain a specific record from a certain column of the table by using this command. A result-set table is a table that keeps the record returned by the SELECT query.
In this SELECT syntax, Column_Name_1, Column_Name_2, ….., Column_Name_N are the name of those columns in the table whose data we want to read.
Use the following SQL SELECT syntax with the * asterisk sign to obtain all rows from all fields of the table:
Here are distinct SQL example that will assist you in executing the SELECT command for retrieving records:
We must first construct the new table and then fill it with dummy data.
Use the following query to create the Student_Records table in SQL:
The following query inserts the record of intelligent students into the Student_Records table:
The SQL query below displays all of the values for each column in the Student records table:
The output of the above query is:
Student_ID | First_Name | Address | Age | Percentage | Grade |
---|---|---|---|---|---|
201 | Akash | Delhi | 18 | 89 | A2 |
202 | Bhavesh | Kanpur | 19 | 93 | A1 |
203 | Yash | Delhi | 20 | 89 | A2 |
204 | Bhavna | Delhi | 19 | 78 | B1 |
205 | Yatin | Lucknow | 20 | 75 | B1 |
206 | Ishika | Ghaziabad | 19 | 91 | C1 |
207 | Vivek | Goa | 20 | 80 | B2 |
The values of a specific column from the Student Record table are displayed in the following query:
The output of the above query is:
Student_ID | Age | Percentage | Grade |
---|---|---|---|
201 | 18 | 89 | A2 |
202 | 19 | 93 | A1 |
203 | 20 | 89 | A2 |
204 | 19 | 78 | B1 |
205 | 20 | 75 | B1 |
206 | 19 | 91 | C1 |
207 | 20 | 80 | B2 |
The WHERE clause is used in combination with the SELECT statement to retrieve only those entries from the table that satisfy the query's condition. The WHERE clause is used in SQL not only with SELECT but also with other SQL statements such as UPDATE, ALTER, and DELETE statements.
In the syntax, we specify the condition in the WHERE clause using SQL logical or comparison operators.
We must first create the new table and then fill it with fake records.
Use the following query to create the Employee_Details table in SQL:
The following INSERT query fills up the Employee Details table with employee records:
The data from the Employee Details table is shown using the following SELECT query:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | John | Kigali | 25000 | 500 |
102 | Claude | Kigali | 29000 | 1000 |
103 | Nepo | Kigali | 35000 | 500 |
104 | Benoit | Kigali | 22000 | 500 |
The following query shows the record of those employees from the above table whose Emp_Panelty is 500:
This SELECT query displays the following table in result:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | John | Kigali | 25000 | 500 |
103 | Nepo | Kigali | 35000 | 500 |
104 | Benoit | Kigali | 22000 | 500 |
The GROUP BY clause is used in combination with the SELECT statement to display the column's common data:
We are going to select from this table:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | John | Kigali | 25000 | 500 |
102 | Claude | Kigali | 29000 | 1000 |
103 | Nepo | Kigali | 35000 | 500 |
104 | Benoit | Kigali | 22000 | 500 |
Count (Emp_Name) | Emp_Salary |
---|---|
3 | 500 |
1 | 1000 |
The HAVING clause in the SELECT statement creates a selection in those groups which are defined by the GROUP BY clause.
SELECT column_Name_1, column_Name_2, ....., column_Name_N aggregate_function_name(column_Name_2) FROM table_name GROUP BY column_Name1 HAVING ;
SELECT column_Name_1, column_Name_2, ....., column_Name_N aggregate_function_name(column_Name_2) FROM table_name GROUP BY column_Name1 HAVING ;
We are going to select data from this table:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | John | Kigali | 24000 | 500 |
102 | Claude | Kigali | 29000 | 1000 |
103 | Nepo | Kigali | 35000 | 500 |
104 | Benoit | Nyagatare | 27000 | 500 |
The following query shows the total salary of those employees having more than 800 from the above Employee_Details table:
output
SUM (Emp_Salary) | Emp_City |
---|---|
2 | Kigali |
1 | Nyagatare |
When using the SQL SELECT statement, the ORDER BY clause sorts the records or rows. The ORDER BY clause sorts the results in ascending and decreasing order. By default, only a few database systems arrange column values in ascending order.
We are going to select data from this table:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
101 | John | Kigali | 24000 | 500 |
102 | Claude | Kigali | 29000 | 1000 |
103 | Nepo | Kigali | 35000 | 500 |
104 | Benoit | Nyagatare | 27000 | 500 |
This SQL query displays the following table in result:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Panelty |
---|---|---|---|---|
104 | Benoit | Nyagatare | 27000 | 500 |
103 | Nepo | Kigali | 35000 | 500 |
102 | Claude | Kigali | 29000 | 1000 |
101 | John | Kigali | 24000 | 500 |
Total: RWF 0.00