![]() |
VOOZH | about |
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.
SELECTstatement in SQL Server is a fundamental SQL command used to query and retrieve data from one or more tables in a database. 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;
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 |
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
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.
SELECT Name, Course
FROM Students
WHERE Course = 'CSE';
Output:
| Name | Course |
|---|---|
| Riya | CSE |
Explanation: Retrieves Name and Course for students enrolled in 'CSE'.
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: 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.
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.