SELECT Statement in SQL

Data Query Language(DQL) OR SELECT Statement in SQL

The SELECT statement in SQL is used to retrieve data from a database and retrieve data stored in the result table as called result set. SQL is not a single language of its own rather than it is a combination of 5 different sublanguages. The select statement belongs to the Data Query language in SQL.

The most commonly used SQL command is the SELECT statement. It is used to query the database and retrieve selected data that follows our intended conditions.

Select Statement is belong to Data Query Language in SQL

Introduction to Structured Query Language (SQL)
Object-Relational Database

SELECT Statement in SQL is powerful query statement responsible for extract data from table or more then one table.

Syntax :
Select/[Distinct]/
<*/Col1,Col2,Col3,.......n>
From <Table_Name>

Example of Select Statement

SELECT * FROM student;

In Select statement must have at least a Select clause and a From clause.
Must of case select statement retrieves all columns and all rows from table. That time we are using asterisk (*). but If you want to select some specific columns then follow the below example.

Example of Select Statement

SELECT ID, Name FROM student;

Select statement can perform two types of job

  • Selection
  • Projection 
    • Column wise projection
    • Row wise projection

Selection in Select Statement

It means select all data from table and not apply any clause.

SELECT * FROM student;

Projection in Select Statement

Column wise Projection

Column restriction is a concept through which those many attributes are displayed which are mentioned in the select statement

SELECT Name, Mobile
FROM student;

Row wise Projection

Row restriction is a concept through which those many records are displayed where condition is satisfied.

SELECT Name, Mobile
FROM student
WHERE ID = 5;

Column Alias in Select Statement

Column alias is a concept through which users can provide duplicate names to an existing column.
It can be given using three different methods

Type-1

SELECT Name Name, Mobile Mobile
FROM student;

Type-2

SELECT Name 'Name', Mobile 'Mobile' --With single quotes
FROM student;

Type-3

SELECT Name as 'Student Name', Mobile as 'Mobile Number'--With single quotes and space between the word
FROM student;

Column Concatenation

Column concatenation is concept through which user can concatenation two or more ten two column to get a customized output.

SELECT 'Name : '+ name+' Mobile number : '+mobile as Student from student;

Distinct in Select Statement

  • The distinct keyword is used in conjunction with the select statement to eliminate all the duplicate records.
  • It retrieves only unique records from the table.
SELECT DISTINCT * FROM student;

SELECT Statement in SQL

In the above information, We got some basic idea about select statement in SQL. Now we will get some deep knowledge Select query in SQL.

Where Clause in Select Query

It is a concept through which those many records are displayed where a condition is satisfied. It is also called Row restriction.

The Where clause is used in most cases to restrict the number of rows. Where clause typically increases query performance by limiting the amount of data to be sent back to the client application.

Syntax for Where condition in SQL query:
Select <exp> from <T_Name> [where <condition>];

Example:

SELECT Name, Mobile
FROM student
WHERE ID = 5;

Order By Clause in SQL

It is the last clause of select statement responsible to arrange the records in a proper manner either ascending or descending order. To do so order by clause provides two attributes, asc and desc. The default order is asc.

Syntax for Order By Clause in SQL query:
Select <exp> from <T_Name> [where <condition>] [Order By <asc|desc>];

Example:

--Single column wise ordering
Select empno, ename, sal, job, deptno from emp
Order by deptno desc;
--Multi column wise ordering
Select empno, ename , job, deptno from emp
Order by deptno, sal desc;

Warm UP

  • The SELECT statement in SQL is used to retrieve data from a database and retrieve data stored in the result table as called result set.
  • SQL statements are case insensitive.
  • The select statement can perform two types of job such as Selection and Projection
  • Projection again divide into two types as Column wise projection and Row wise projection
  • Column alias is a concept through which users can provide duplicate names to an existing column.
  • Column concatenation is a concept through which users can concatenation two or more ten two-column to get a customized output.
  • The distinct keyword is used in conjunction with the Select statement to eliminate all the duplicate records.
  • Where clause is a concept through which those many records are displayed where a condition is satisfied. It is also called Row restriction.
  • Order by is the last clause of the select statement responsible to arrange the records in a proper manner either ascending or descending order.

Reference Link

Select Statement (Transact-SQL) in Microsoft SQL Server

Leave a Comment

Your email address will not be published. Required fields are marked *