Paramclasses Academy - Blog

Tutorials Details

←
Post 12 of 40
→

SQL SELECT Statement Tutorial: Retrieve Data from SQL Server with Practical Examples

SQL SELECT Statement Tutorial: Retrieve Data from SQL Server with Practical Examples

November 16, 2026

SQL SELECT Statement Tutorial: Retrieve Data from SQL Server with Practical Examples

✨ Introduction

The SELECT statement is one of the most important and frequently used SQL commands in Microsoft SQL Server. It is used to retrieve information stored inside database tables.

Whenever you work with a database, you need to view, search, and retrieve records according to your requirements. SQL Server provides the SELECT statement to perform these operations.

Imagine that you are developing a Student Management System for a college. Your database contains hundreds of student records, including student IDs, names, cities, courses, and fees.

You may need to:

  • Display all student records.

  • Retrieve only student names and cities.

  • Find students enrolled in a particular course.

  • Display students from a specific city.

  • Retrieve the first five student records.

  • Sort students according to their course fees.

  • Display unique cities from the Students table.

All these operations can be performed using the SQL SELECT statement.

In this beginner-friendly tutorial, we will learn how the SELECT statement works, understand its syntax, explore different SELECT clauses, and perform practical SQL Server queries using a Student Management Database.

???? 1. What Is the SELECT Statement in SQL Server?

The SELECT statement is used to retrieve data from one or more database tables or other queryable sources in SQL Server.

It allows developers to display complete records, retrieve specific columns, filter information, and sort query results.

Basic Syntax:

SELECT column_name
FROM table_name;

Explanation:

  • SELECT specifies the columns you want to retrieve.

  • FROM specifies the table from which data will be retrieved.

Example:

SELECT StudentName
FROM Students;

This query retrieves only the StudentName column from the Students table.

???? 2. Create a Database and Students Table

Before understanding different SELECT queries, let's create a practical database.

Step 1: Create a Database

CREATE DATABASE StudentDB;
GO

This command creates a new database named StudentDB.

Step 2: Select the Database

USE StudentDB;
GO

The USE statement selects StudentDB as the current database for subsequent queries.

Step 3: Create a Students Table

CREATE TABLE Students
(
    StudentID INT PRIMARY KEY,
    StudentName VARCHAR(100),
    City VARCHAR(50),
    Course VARCHAR(100),
    Fees DECIMAL(10,2),
    Age INT
);

Table Structure:

Column Name Data Type Description
StudentID INT Unique student ID
StudentName VARCHAR(100) Student name
City VARCHAR(50) Student city
Course VARCHAR(100) Course name
Fees DECIMAL(10,2) Course fees
Age INT Student age

Step 4: Insert Sample Records

INSERT INTO Students
(
    StudentID,
    StudentName,
    City,
    Course,
    Fees,
    Age
)
VALUES
(101, 'Rahul Sharma', 'Raipur', 'SQL Server', 15999.50, 21),
(102, 'Amit Verma', 'Durg', 'Python', 12500.00, 22),
(103, 'Priya Singh', 'Bilaspur', 'Web Development', 18000.00, 20),
(104, 'Neha Patel', 'Raipur', 'Java', 15000.00, 23),
(105, 'Rohit Kumar', 'Bhilai', 'SQL Server', 15999.50, 21),
(106, 'Anjali Gupta', 'Durg', 'Python', 12500.00, 24),
(107, 'Vikas Sahu', 'Raipur', 'Web Development', 18000.00, 22);

Step 5: Verify Inserted Records

SELECT * FROM Students;

Now our Students table contains seven sample records that we can use throughout this tutorial.

???? 3. SELECT All Records Using SELECT *

The asterisk (*) is used to retrieve all columns from a table.

Syntax:

SELECT *
FROM table_name;

Example:

SELECT *
FROM Students;

Output:

StudentID StudentName City Course Fees Age
101 Rahul Sharma Raipur SQL Server 15999.50 21
102 Amit Verma Durg Python 12500.00 22
103 Priya Singh Bilaspur Web Development 18000.00 20
104 Neha Patel Raipur Java 15000.00 23
105 Rohit Kumar Bhilai SQL Server 15999.50 21
106 Anjali Gupta Durg Python 12500.00 24
107 Vikas Sahu Raipur Web Development 18000.00 22

The SELECT * query retrieves every column and every row from the Students table.

Important: SELECT * is useful for learning and quick inspection. In production applications, specifying the required columns is generally better because it avoids retrieving unnecessary data.

???? 4. SELECT Specific Columns

Sometimes you do not need all the columns from a table. You may only want student names, cities, or course details.

In this situation, specify the required column names after SELECT.

Syntax:

SELECT column1, column2
FROM table_name;

Example 1: Retrieve Student Names

SELECT StudentName
FROM Students;

Example 2: Retrieve Student Names and Cities

SELECT StudentName, City
FROM Students;

Output:

StudentName City
Rahul Sharma Raipur
Amit Verma Durg
Priya Singh Bilaspur
Neha Patel Raipur
Rohit Kumar Bhilai
Anjali Gupta Durg
Vikas Sahu Raipur

Example 3: Retrieve Course and Fees

SELECT Course, Fees
FROM Students;

This query displays only the Course and Fees columns.

???? 5. SELECT DISTINCT Statement

The DISTINCT keyword is used to retrieve unique values from a column or combination of columns.

It removes duplicate combinations from the query result.

Syntax:

SELECT DISTINCT column_name
FROM table_name;

Example 1: Display Unique Cities

SELECT DISTINCT City
FROM Students;

Output:

City
Raipur
Durg
Bilaspur
Bhilai

Although multiple students belong to Raipur and Durg, DISTINCT displays each unique city only once.

Example 2: Display Unique Courses

SELECT DISTINCT Course
FROM Students;

Output:

Course
SQL Server
Python
Web Development
Java

Example 3: DISTINCT on Multiple Columns

SELECT DISTINCT City, Course
FROM Students;

When DISTINCT is applied to multiple columns, SQL Server returns unique combinations of those columns.

???? 6. SELECT TOP Statement

The TOP clause is used to retrieve a specified number or percentage of rows from a query result.

It is particularly useful when working with large database tables.

Syntax:

SELECT TOP (number)
column_name
FROM table_name;

Example 1: Retrieve First Five Students

SELECT TOP (5) *
FROM Students;

This query returns five rows from the Students table.

Example 2: Retrieve Top Three Students

SELECT TOP (3)
StudentName,
Course
FROM Students;

Example 3: Retrieve Top Three Students by Highest Fees

SELECT TOP (3)
StudentName,
Course,
Fees
FROM Students
ORDER BY Fees DESC;

Output:

StudentName Course Fees
Priya Singh Web Development 18000.00
Vikas Sahu Web Development 18000.00
Rahul Sharma SQL Server 15999.50

Example 4: Retrieve Top 50 Percent Records

SELECT TOP (50) PERCENT *
FROM Students
ORDER BY StudentID;

This query retrieves approximately half of the rows according to the specified ordering.

Important: Without ORDER BY, TOP does not guarantee which rows will be returned. Always use ORDER BY when you need a predictable result.

???? 7. Column Aliases in SELECT Statement

Aliases are used to assign temporary alternative names to columns in query results.

They make output easier to understand and improve readability.

SQL Server supports aliases using AS or a space between the column expression and its alias.

Syntax:

SELECT column_name AS alias_name
FROM table_name;

Example 1: Rename StudentName

SELECT
StudentName AS Name
FROM Students;

Example 2: Rename Multiple Columns

SELECT
StudentID AS ID,
StudentName AS FullName,
City AS StudentCity,
Fees AS CourseFees
FROM Students;

Output:

ID FullName StudentCity CourseFees
101 Rahul Sharma Raipur 15999.50
102 Amit Verma Durg 12500.00
103 Priya Singh Bilaspur 18000.00

Example 3: Alias Without AS

SELECT
StudentName FullName,
Course CourseName
FROM Students;

Aliases change the column headings in the query result. They do not rename the original table columns.

???? 8. SELECT Statement With Calculations

SQL Server allows mathematical calculations directly inside SELECT statements.

You can use arithmetic operators to calculate values without changing the original table data.

Common arithmetic operators include:

Operator Meaning Example
+ Addition Fees + 1000
- Subtraction Fees - 1000
* Multiplication Fees * 2
/ Division Fees / 2
% Modulus 10 % 3

Example 1: Add 1000 to Course Fees

SELECT
StudentName,
Fees,
Fees + 1000 AS UpdatedFees
FROM Students;

This query displays the original fees and calculated fees.

Example 2: Calculate Discounted Fees

SELECT
StudentName,
Fees,
Fees - 2000 AS DiscountedFees
FROM Students;

Example 3: Calculate Annual Fees

SELECT
StudentName,
Fees,
Fees * 2 AS AnnualFees
FROM Students;

These calculations are performed only in the query result. They do not permanently update the table values.

???? 9. SELECT Statement With WHERE Clause

The WHERE clause is used to filter records based on specified conditions.

It allows you to retrieve only the rows that match your requirements.

Syntax:

SELECT column_name
FROM table_name
WHERE condition;

Example 1: Find Students From Raipur

SELECT *
FROM Students
WHERE City = 'Raipur';

Output:

StudentID StudentName City Course Fees Age
101 Rahul Sharma Raipur SQL Server 15999.50 21
104 Neha Patel Raipur Java 15000.00 23
107 Vikas Sahu Raipur Web Development 18000.00 22

Example 2: Find Students Enrolled in SQL Server

SELECT *
FROM Students
WHERE Course = 'SQL Server';

Example 3: Find Students With Fees Greater Than 15000

SELECT *
FROM Students
WHERE Fees > 15000;

Example 4: Find Students Older Than 21

SELECT *
FROM Students
WHERE Age > 21;

The WHERE clause helps retrieve only the required information instead of displaying every record.

???? 10. SELECT Statement With ORDER BY

The ORDER BY clause is used to sort query results in ascending or descending order.

SQL Server supports two sorting options:

  • ASC – Ascending order.

  • DESC – Descending order.

Ascending order is the default when no sorting direction is specified.

Syntax:

SELECT column_name
FROM table_name
ORDER BY column_name ASC;

Example 1: Sort Students by Name

SELECT *
FROM Students
ORDER BY StudentName ASC;

This query displays student names in alphabetical order.

Example 2: Sort Students by Highest Fees

SELECT *
FROM Students
ORDER BY Fees DESC;

Example 3: Sort Students by Age

SELECT *
FROM Students
ORDER BY Age ASC;

Example 4: Sort Using Multiple Columns

SELECT *
FROM Students
ORDER BY City ASC, StudentName ASC;

This query sorts records by city first and then sorts student names within each city.

???? 11. Combining SELECT, DISTINCT, WHERE and ORDER BY

SQL Server allows you to combine multiple clauses in a single SELECT query.

This makes it possible to retrieve specific information, remove duplicate values, filter records, and sort the results.

Example 1: Find Unique Courses From Raipur

SELECT DISTINCT Course
FROM Students
WHERE City = 'Raipur'
ORDER BY Course ASC;

This query:

  1. Retrieves course information.

  2. Filters students from Raipur.

  3. Displays unique course names.

  4. Sorts the results alphabetically.

Example 2: Display Top Three Highest-Paid Courses

SELECT TOP (3)
StudentName,
Course,
Fees
FROM Students
WHERE Fees >= 15000
ORDER BY Fees DESC;

Example 3: Display Students Between Ages 20 and 23

SELECT *
FROM Students
WHERE Age BETWEEN 20 AND 23
ORDER BY Age ASC;

These combined queries are useful for retrieving information according to multiple business requirements.

???? 12. SQL SELECT Logical Query Processing Order

Although a SELECT statement is written starting with SELECT, SQL Server logically processes its clauses in a different order.

The simplified logical processing order is:

Order Clause Purpose
1 FROM Identifies the data source
2 WHERE Filters individual rows
3 SELECT Determines the output expressions
4 DISTINCT Removes duplicate result rows
5 ORDER BY Sorts the result
6 TOP Limits the returned rows

Example:

SELECT DISTINCT TOP (3)
City
FROM Students
WHERE Fees > 12000
ORDER BY City ASC;

Understanding logical query processing helps you understand why certain SQL queries work and why some aliases cannot be referenced in the WHERE clause.

???? 13. Practical Student Management Database Project

Now let's perform a complete practical exercise using the StudentDB database.

Step 1: Select the Database

USE StudentDB;
GO

Step 2: Display All Students

SELECT *
FROM Students;

Step 3: Display Student Names and Courses

SELECT
StudentName,
Course
FROM Students;

Step 4: Find Students From Raipur

SELECT *
FROM Students
WHERE City = 'Raipur';

Step 5: Display Unique Courses

SELECT DISTINCT Course
FROM Students;

Step 6: Find Students With Fees Greater Than 15000

SELECT
StudentName,
Course,
Fees
FROM Students
WHERE Fees > 15000;

Step 7: Display the Top Three Students by Fees

SELECT TOP (3)
StudentName,
Fees
FROM Students
ORDER BY Fees DESC;

Step 8: Sort All Students Alphabetically

SELECT *
FROM Students
ORDER BY StudentName ASC;

Step 9: Display Students Between Ages 20 and 22

SELECT *
FROM Students
WHERE Age BETWEEN 20 AND 22;

Step 10: Display Final Student Information

SELECT
StudentID AS ID,
StudentName AS Student,
City AS Location,
Course AS CourseName,
Fees AS TotalFees,
Age AS StudentAge
FROM Students
ORDER BY StudentID ASC;

By completing these practical steps, you will understand how SELECT retrieves information from SQL Server tables and how different clauses help you work with database records.

???? 14. Common Mistakes While Using SELECT

Beginners often make mistakes when writing SELECT queries.

1. Forgetting the FROM Clause

Incorrect:

SELECT StudentName;

Correct:

SELECT StudentName
FROM Students;

For a table-based query, FROM identifies the source table.

2. Using an Incorrect Column Name

If a column does not exist in the specified table, SQL Server returns an invalid column name error.

Solution: Check the actual column names before executing the query.

3. Forgetting Single Quotes Around Text Values

Incorrect:

SELECT *
FROM Students
WHERE City = Raipur;

Correct:

SELECT *
FROM Students
WHERE City = 'Raipur';

Text values should be enclosed in single quotes.

4. Using DISTINCT Unnecessarily

DISTINCT removes duplicate result rows and may require additional processing.

Solution: Use DISTINCT only when unique results are actually required.

5. Using TOP Without ORDER BY

TOP without ORDER BY does not guarantee a particular selection of records.

Solution: Specify ORDER BY when you need predictable results.

6. Using SELECT * in Every Query

Retrieving unnecessary columns can increase data transfer and reduce query efficiency.

Solution: Select only the columns required by your application.

???? 15. Best Practices for SQL SELECT Statements

  1. Select only the columns you actually need.

  2. Use meaningful column aliases for readable results.

  3. Use WHERE conditions to avoid retrieving unnecessary rows.

  4. Use DISTINCT only when duplicate removal is required.

  5. Use ORDER BY whenever a particular sorting order is needed.

  6. Combine TOP with ORDER BY for predictable limited results.

  7. Use proper table and column names.

  8. Test queries before using them in production applications.

  9. Understand the logical processing order of SELECT clauses.

  10. Use appropriate filters when working with large tables.

???? 16. Frequently Asked Questions (FAQs)

Q1. What is the SELECT statement in SQL Server?

The SELECT statement is used to retrieve information from one or more SQL Server tables or other queryable sources.

Q2. What is the use of SELECT * in SQL Server?

SELECT * retrieves all columns from the specified table or query source.

Q3. What is the difference between SELECT and SELECT DISTINCT?

SELECT can return duplicate rows, while SELECT DISTINCT removes duplicate result rows.

Q4. What is the use of the TOP clause?

TOP limits the number or percentage of rows returned by a SELECT query.

Q5. What is a column alias in SQL Server?

A column alias is an alternative name displayed for a column or expression in a query result.

Q6. Can we perform calculations inside SELECT statements?

Yes. SQL Server supports arithmetic operations such as addition, subtraction, multiplication, and division inside SELECT queries.

Q7. What is the use of WHERE in SELECT?

WHERE filters records based on specified conditions.

Q8. What is the difference between ASC and DESC?

ASC sorts values in ascending order, whereas DESC sorts values in descending order.

Q9. Can we use DISTINCT with multiple columns?

Yes. DISTINCT can be applied to multiple columns to return unique combinations of their values.

Q10. What is the correct order of SELECT query clauses?

The common written structure is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and TOP is placed in the SELECT clause. The logical processing order is different from the written order.

???? Conclusion

The SELECT statement is one of the most essential SQL Server commands because it allows developers to retrieve and analyze information stored in database tables.

In this tutorial, we learned how to retrieve all records using SELECT *, select specific columns, remove duplicate results using DISTINCT, limit records using TOP, and rename columns using aliases.

We also explored mathematical calculations, filtering records using WHERE, sorting data using ORDER BY, combining different clauses, and understanding the logical processing order of SELECT statements.

Finally, we performed a practical Student Management Database exercise to understand how SELECT queries are used in real-world database applications.

At Param Classes, we focus on helping students and beginners learn SQL Server through simple explanations, practical SQL queries, and real-world database projects. Keep practising different SELECT queries to strengthen your SQL skills and become confident in database development.

???? Learn. Practice. Build with Param Classes!

←
Post 12 of 40
→