SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL what-are-single-row-and-multiple-row-subqueries">What are single row and multiple row subqueries?

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