×

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 Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

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 Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 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.