Select Statement in MS SQL Server
Last Updated :
17 Sep, 2024
The SELECT
statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database.
This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to manipulate and analyze the data.
In this article, We will learn about Select Statement in SQL Server by understanding various examples.
Select Statement in SQL Server
- The
SELECT
statement in SQL Server is a fundamental SQL command used to query and retrieve data from one or more tables in a database.
- It allows us to specify the columns and rows want to retrieve, apply filtering conditions, and perform various operations on the data.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition
GROUP BY column
HAVING condition
ORDER BY column [ASC|DESC];
Explanation:
SELECT
: Specifies the columns to retrieve from the table.
FROM
: Indicates the table or tables from which to retrieve the data.
WHERE
: Applies a filter to select specific rows that meet the condition.
GROUP BY
: Groups rows that have the same values in specified columns into summary rows, often used with aggregate functions.
HAVING
: Filters groups of rows based on a specified condition, similar to the WHERE
clause but used for grouped data.
ORDER BY
: Sorts the result set by one or more columns, in ascending (ASC) or descending (DESC) order.
Syntax:
select*
from
table_name;
Points to Remember
- In the real time databases, select* isn't recommended for use because it retrieves the data more than your requirements.
- It results in slow functioning of the application.
- In case, the user adds more columns to the table, the select* statement retrieves all the columns including the new ones resulting in crashing of the application.
Example of Select statement in MS SQL Server
To understand the Select statement in MS SQL Server we will use the below table with the various SQL Server Operators and so on.
Roll number | Name | Course |
---|
111 | Riya | CSE |
112 | Apoorva | ECE |
113 | Mina | Mech |
114 | Rita | Biotechnology |
115 | Veena | Chemical |
116 | Deepa | EEE |
Exampe 1: Select All Columns from the Table
SELECT *
FROM Students;
Output:
RollNumber | Name | Course |
---|
111 | Riya | CSE |
112 | Apoorva | ECE |
113 | Mina | Mech |
114 | Rita | Biotechnology |
115 | Veena | Chemical |
116 | Deepa | EEE |
Explanation: Retrieves all columns for all rows in the Students
table
Example 2: Select Specific Columns
SELECT Name, Course
FROM Students;
Output:
Name | Course |
---|
Riya | CSE |
Apoorva | ECE |
Mina | Mech |
Rita | Biotechnology |
Veena | Chemical |
Deepa | EEE |
Explanation: Retrieves only the Name
and Course
columns.
Example 3: Select with a WHERE Clause
SELECT Name, Course
FROM Students
WHERE Course = 'CSE';
Output:
Explanation: Retrieves Name
and Course
for students enrolled in 'CSE'.
Example 4: Select with Sorting
SELECT Name, Course
FROM Students
ORDER BY Name ASC;
Output:
Name | Course |
---|
Apoorva | ECE |
Deepa | EEE |
Mina | Mech |
Rita | Biotechnology |
Riya | CSE |
Veena | Chemical |
Explanation: Retrieves Name
and Course
, sorted by Name
in ascending order.
Example 5: Select with Aggregate Functions
-- Example: Counting number of students in each course
SELECT Course, COUNT(*) AS NumberOfStudents
FROM Students
GROUP BY Course;
Output:
Course | NumberOfStudents |
---|
CSE | 1 |
ECE | 1 |
Mech | 1 |
Biotechnology | 1 |
Chemical | 1 |
EEE | 1 |
Explanation: Retrieves the count of students in each course.
Conclusion
The SELECT
statement is an indispensable tool in SQL Server for data retrieval and manipulation. It supports various operations, including filtering with WHERE
, grouping with GROUP BY
, sorting with ORDER BY
, and applying aggregate functions. While using SELECT *
retrieves all columns from a table, it's generally advised to select only the columns you need to avoid unnecessary data processing and potential performance issues.
Similar Reads
SQL Server SELECT INTO Statement SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applic
6 min read
Insert Into Select statement in MS SQL Server The INSERT INTO SELECT statement in SQL Server is a versatile feature that enables you to efficiently copy data from one or more tables into another table. This functionality is essential for tasks such as data transfer, backup creation, and data merging.In this article, We will learn to Insert Into
4 min read
Select top in MS SQL Server Prerequisite - Select in MS SQL Server Suppose that a user wants to extract the top students from the whole institution but has to use some complex queries to extract the data. To avoid complexity, the user can use 'Select Top'. 'Select Top' extracts the limited number of rows. This results in accur
2 min read
What is Nested Select Statement in SQL Server SQL Server is a powerful relational database management system. Sql Server is very good with its robustness and scalability. SQL Server operates as a client-server structure, imparting centralized management and scalability for huge-scale applications and enterprise-stage solutions. It offers advanc
3 min read
Insert Statement in MS SQL Server The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table.In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the help
4 min read
SQL SELECT INTO Statement The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
MySQL SELECT Statement The MySQL SELECT statement is essential for fetching data from tables. It retrieves information and stores it in a result table, often referred to as a result set. Widely used in MySQL, SELECT is a fundamental command for querying databases. This article covers the basics of SELECT syntax and explor
4 min read
SET vs SELECT in SQL In SQL, SET and SELECT are powerful commands that are commonly used for assigning values to variables and retrieving data. While both are essential, they serve different purposes and are optimized for different scenarios. Understanding the differences between these two commands can greatly enhance o
5 min read
SQL CASE Statement The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
SQL - SELECT from Multiple Tables with MS SQL Server In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we con
3 min read