Are you

SQL SELECT Statement


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.

Syntax of SELECT Statement

SELECT Column_Name_1, Column_Name_2, ....., Column_Name_N FROM Table_Name;

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:

SELECT * FROM table_name;

Examples of SELECT Statement

SELECT * FROM Student_Records;

Here are distinct SQL example that will assist you in executing the SELECT command for retrieving records:

Examples

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:

CREATE TABLE Student_Records
(
Student_Id Int PRIMARY KEY,
First_Name VARCHAR (20),
Address VARCHAR (20),
Age Int NOT NULL,
Percentage Int NOT NULL,
Grade VARCHAR (10)
) ;

The following query inserts the record of intelligent students into the Student_Records table:

INSERT INTO Student_Records VALUES (201, Akash, Delhi, 18, 89, A2),
(202, Bhavesh, Kanpur, 19, 93, A1),
(203, Yash, Delhi, 20, 89, A2),
(204, Bhavna, Delhi, 19, 78, B1),
(05, Yatin, Lucknow, 20, 75, B1),
(206, Ishika, Ghaziabad, 19, 51, C1),
(207, Vivek, Goa, 20, 62, B2);

The SQL query below displays all of the values for each column in the Student records table:

SELECT * FROM Student_Records;

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:

SELECT Student_Id, Age, Percentage, Grade FROM Employee;

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

SELECT Statement with WHERE clause

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.

Syntax of SELECT Statement with WHERE clause

SELECT * FROM Name_of_Table WHERE [condition];

In the syntax, we specify the condition in the WHERE clause using SQL logical or comparison operators.

Example of SELECT Statement with WHERE clause

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:

CREATE TABLE Employee_Details
(
Employee_ID INT AUTO_INCREMENT PRIMARY KEY,
Emp_Name VARCHAR (50),
Emp_City VARCHAR (20),
Emp_Salary INT NOT NULL,
Emp_Panelty INT NOT NULL
) ;

The following INSERT query fills up the Employee Details table with employee records:

INSERT INTO Employee_Details (Employee_ID, Emp_Name, Emp_City, Emp_Salary, Emp_Panelty) VALUES (101, Anuj, Ghaziabad, 25000, 500),
(102, Tushar, Lucknow, 29000, 1000),
(103, Vivek, Kolkata, 35000, 500),
(104, Shivam, Goa, 22000, 500);

The data from the Employee Details table is shown using the following SELECT query:

SELECT * FROM Employee_Details;
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:

SELECT * FROM Employee_Details WHERE Emp_Panelty = 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

SQL SELECT Statement with GROUP BY clause

The GROUP BY clause is used in combination with the SELECT statement to display the column's common data:

Syntax of SELECT Statement with GROUP BY clause

SELECT column_Name_1, column_Name_2, ....., column_Name_N aggregate_function_name(column_Name2) FROM table_name GROUP BY column_Name1;

Example of SELECT Statement with GROUP BY clause

We are going to select from this table:

SELECT * FROM Employee_Details;
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
SELECT COUNT (Emp_Name), Emp_Salary FROM Employee_Details GROUP BY Emp_Panelty;
Count (Emp_Name) Emp_Salary
3 500
1 1000

SQL SELECT Statement with HAVING clause

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_Name2) FROM table_name GROUP BY column_Name1;

Syntax of SELECT Statement with HAVING 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 ;

Example of SELECT Statement with HAVING 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 ;

We are going to select data from this table:

SELECT * FROM Employee_Details;
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:

SELECT SUM (Emp_Salary), Emp_City FROM Employee_Details GROUP BY Emp_City HAVING SUM(Emp_Salary)>25000;

output

SUM (Emp_Salary) Emp_City
2 Kigali
1 Nyagatare

SELECT Statement with ORDER BY clause

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.

Syntax of SELECT Statement with ORDER BY clause

SELECT Column_Name_1, Column_Name_2, Column_Name_3, ....., column_Name_N FROM table_name WHERE [Condition] ORDER BY[column_Name_1, column_Name_2,, Column_Name_3 ....., column_Name_N asc | desc ];

Example of SELECT Statement with ORDER BY clause in SQL

We are going to select data from this table:

SELECT * FROM Employee_Details;
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

Output:

This SQL query displays the following table in result:

SELECT * FROM Employee_Details ORDER BY Employee_Id DESC;
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
Next

Courses