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 Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Introduction of ER Model The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
DBMS Tutorial â Learn Database Management System Database Management System (DBMS) is a software used to manage data from a database. A database is a structured collection of data that is stored in an electronic device. The data can be text, video, image or any other format.A relational database stores data in the form of tables and a NoSQL databa
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read