Home » SQL Server SELECT Clause

SQL Server SELECT Clause

This post will introduce you to the basic SELECT the statement, how to use the statement.

SELECT on Table

Database tables are used to store the data in the database, the table contains the data in rows and column format like a spreadsheet. To understand better we are going to take the example of the student table

To query data from the table we use a select keyword, check the below syntax of the select query

column_list from schema_name.table_name

In the above syntax first, we use the select keyword, after that the list of columns, then specify the from the clause with source table_name.

SQL Server – Select Examples

for the select keyword example, we will take the example of Student table.


1. Fetch the Specific Columns

To fetch the specific columns from the table, we need to mention the column names

	FROM [Blog].[dbo].[Student]

2. Retrieve the all columns

To retrieve all the columns we need to use select *

	FROM [Blog].[dbo].[Student]

3. Sort the Records

To sort the records, we can use <a href="https://geekfrisk.com/sql-server-where/" target="_blank" rel="noreferrer noopener">where</a>clause,

SELECT * FROM [Blog].[dbo].[Student]
where City='London'

The result of the query is

In the above example, we can use Order by to maintain the order of the column, the default order is ascending

SELECT * FROM [Blog].[dbo].[Student]
where City='London' order by Student_name

The result of the above query is

4. SQL Server SELECT- Grouping the column

For grouping of the records or column values, we need to use, group by clause

SELECT City FROM [Blog].[dbo].[Student]
group by City order by City

In the select statement, we can only those columns name which is present in the group by clause or columns in the aggregate function.

The result of the above query is

5. SQL Server SELECT-Sorting the Grouping column

To sort the grouping records, we need to having a clause,

SELECT City FROM [Blog].[dbo].[Student]
group by City
having City='London'
order by City

Note: Where clause filter the rows but having filter the groups

If you want to learn more select statement then you can refer Microsoft official document.

Need help?

Read this post again, if you have any confusion or else add your questions in Community