In this article, we will learn all SELECT-Operations of T-SQL at one-place. It contains huge combinations of examples.
The SELECT statement is used to retrieve rows from one or more tables avaliable in your SQL database. The rows retrieved are known as a result set.
1. T-SQL to select all columns of a table.
1. T-SQL to select all columns of a table.
SELECT * FROM tblStudent
2. T-SQL to select particular column of a table.
SELECT StudentName , FatherName FROM tblStudent
3. Select using tables ALIAS.
SELECT S.*, C.CourseName
FROM tblStudent AS S, tblCourse AS C;
4. Select using Arithmetic operations on columns
SELECT StudentName +' '+ FatherName AS Name
FROM tblStudent
5. Remove duplicate records from result set
SELECT DISTINCT StudentName,FatherName,State,CourseCode
FROM TblStudent
6. T-SQL to sort the result set in ascending or descending order
SELECT StudentName,FatherName,State,CourseCode
FROM TblStudent
ORDER BY StudentName DESC
-- By default ORDER BY clause sort data in ascending order. Use "DESC" to sort data in descending order.
7. Use CASE statement with SELECT T-SQL
SELECT CASE
WHEN (TotalMarks BETWEEN 0 AND 33) THEN 'low marks'
WHEN (TotalMarks BETWEEN 34 AND 50) THEN 'avg marks'
WHEN (TotalMarks BETWEEN 51 AND 60) THEN 'above avg'
ELSE 'Good'
END AS ResultStatus
FROM tblStudent
Note: In SELECT statement, We can not used CASE statement in FROM and WHERE clauses.
8. Filter rows with where clause
SELECT * FROM tblStudent where StudentName='Kapil'
0 Comments