×

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT statement is also used to retrieve the selected records from the selected column name.

We use the WHERE clause in the SELECT query to retrieve the selected records from the table by applying the condition. With the WHERE clause, we can use the comparison, arithmetic, bitwise, and logical operators.

The first syntax of the SELECT statement is as follows:

SELECT * FROM Table_Name;

The asterisk (*) symbol retrieves all the records from the table without applying any conditions in the query.

The second syntax of the SELECT statement is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4, Column_Name_5 FROM Table_Name;   

The above second syntax is used to retrieve records from the specified columns in the query table.

The third syntax of the SELECT statement with the WHERE Clause is as follows:

SELECT * FROM Table_Name WHERE conditions;

In the third syntax, we used the WHERE clause in the query to retrieve or display selected records from the table by applying the conditions in the query.
Let’s understand the SQL SELECT statement with the help of examples.

Examples of SQL SELECT Statement

Consider the already existing table, which has the following data:

Table Name:- Diploma_Student

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

Example 1: Write a query to retrieve the student’s information from the Diploma_Student table.

SELECT * FROM Diploma_Student;

The above example fetched the student's information from the Diploma_Student table.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL SELECT Statement

SQL SELECT Statement with the WHERE Clause

Example 2: Write a query to retrieve the information of those students from the Diploma_Student table whose student department id is 2

SELECT * FROM Diploma_Student WHERE Department_Id = 2;

In this example, we fetch the information whose department id is 2 from the table. Here, we applied the condition using the WHERE clause in the SELECT statement.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202112Vaibhav Lokhande859092808582862
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202229Manthan Koli857584788280812
SQL SELECT Statement

SQL SELECT Statement with the ORDER BY Clause

Example 3: Write a query to retrieve the student’s information from the Diploma_Student table in the descending order by Total column.

SELECT * FROM Diploma_Student ORDER BY Total DESC;

In this query, we fetched the student’s information from the Diploma_Student table in the descending order by Total column.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202114Sonali Patole959092889290914
202222Venkatesh Iyer908987909291903
202113Yash Dhull908894878590893
202230Mayur Jain808887909290881
202112Vaibhav Lokhande859092808582862
202226Shwetali Bhagwat908085889080866
202221Anuja Wanare858886828485857
202115Axar Patel858082869284851
202228Utkarsh Rokade858080908484845
202225Akshay Agarwal858078889082847
202227Mayuri Wagh808085808285824
202229Manthan Koli857584788280812
202220Prajwal Lokhande808585757880814
202224Aakash Jain807572748580787
202116Meena Mishra787580748577783
202118Sakshi Patil807874788077782
202117Mahesh Kumbhar758075788076775
202119Sopan Bhore706875758080752
202223Anushka Sen707571748078751
SQL SELECT Statement

SQL SELECT Statement with the GROUP BY Clause

Example 4: Write a query to retrieve the student’s information from the Diploma_Student table GROUP BY First_Sem column.

SELECT * FROM Diploma_Student GROUP BY First_Sem;

The above example fetched the student's information from the Diploma_Student table GROUP BY First_Sem column.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202119Sopan Bhore706875758080752
202117Mahesh Kumbhar758075788076775
202116Meena Mishra787580748577783
202118Sakshi Patil807874788077782
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202111Vaishnavi Patil949188859592916
202114Sonali Patole959092889290914
SQL SELECT Statement

SQL SELECT Statement with the AND – OR Operator

Example 5: Write a query to retrieve the information of those students from the Diploma_Student table whose student department id is 2 and the percentage of sixth sem is greater than 75.

SELECT * FROM Diploma_Student WHERE Department_Id = 2 AND Sixth_Sem > 75;

In the above example, we fetched the information of those students from the Diploma_Student table whose student department id is 2 and the percentage of sixth sem is greater than 75.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202112Vaibhav Lokhande859092808582862
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202229Manthan Koli857584788280812
SQL SELECT Statement

Example 6: Write a query to retrieve the information of those students from the Diploma_Student table whose First_Sem percentage is greater than 80 or Fifth_Sem percentage is greater than 80.

SELECT * FROM Diploma_Student WHERE First_Sem > 85 OR Fifth_Sem > 80;

In the above example, we fetched the information of those students from the Diploma_Student table whose First_Sem percentage is greater than 80 or Fifth_Sem percentage is greater than 80.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL SELECT Statement

SQL SELECT Statement with the BETWEEN – LIKE Operator:

Example 7: Write a query to retrieve the student's information from the Diploma_Student table where the Second_Sem percentage is exist between 75 and 85.

SELECT * FROM Diploma_Student WHERE Second_Sem BETWEEN 75 AND 85;

In the above example, we fetched the student's information from the Diploma_Student table, where the student Second_Sem percentage is exist between 75 and 85.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202220Prajwal Lokhande808585757880814
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
SQL SELECT Statement

Example 8: Write a query to retrieve the student's information from the Diploma_Student table where the student's name starts with the letter 'A'.

SELECT * FROM Diploma_Student WHERE Student_Name LIKE ‘A%’;

In the above example, we fetched the student's information from the Diploma_Student table, where the student's name starts with the letter 'A'.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202115Axar Patel858082869284851
202221Anuja Wanare858886828485857
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
SQL SELECT Statement

Related Topics

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.