SELECT Operation
The SELECT statement is used to retrieve data from a SQL Server table.
SELECT is one of the most important SQL commands. It allows us to view records stored in database tables. We can retrieve all columns, specific columns, or only those records that satisfy a particular condition.
What is SELECT?
The SELECT statement is used to retrieve records from one or more tables.
It does not modify the existing data. It only reads and displays the requested information.
SELECT Syntax
SELECT column1, column2
FROM table_name;
The SELECT clause specifies the columns that we want to retrieve, while the FROM clause specifies the table from which the data should be retrieved.
Example Table
For the following examples, we will use the Students table.
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 22 | Pune |
| 102 | Priya | 23 | Mumbai |
| 103 | Amit | 21 | Nashik |
| 104 | Neha | 22 | Pune |
SELECT All Columns
To retrieve all columns from a table, we can use the asterisk *.
SELECT *
FROM Students;
Output
The query returns all columns and all records from the Students table.
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 22 | Pune |
| 102 | Priya | 23 | Mumbai |
| 103 | Amit | 21 | Nashik |
| 104 | Neha | 22 | Pune |
SELECT Specific Columns
We can retrieve only the columns that we need instead of retrieving all columns.
SELECT StudentName, Age
FROM Students;
Output
| StudentName | Age |
|---|---|
| Rahul | 22 |
| Priya | 23 |
| Amit | 21 |
| Neha | 22 |
SELECT with WHERE
The WHERE clause is used to retrieve only those records that satisfy a specified condition.
SELECT *
FROM Students
WHERE City = 'Pune';
Output
Only students whose City is Pune are returned.
| StudentId | StudentName | Age | City |
|---|---|---|---|
| 101 | Rahul | 22 | Pune |
| 104 | Neha | 22 | Pune |
SELECT with Numeric Condition
We can also use comparison operators with numeric columns.
SELECT *
FROM Students
WHERE Age > 21;
SELECT with ORDER BY
ORDER BY is used to sort the retrieved records.
SELECT *
FROM Students
ORDER BY Age ASC;
ASC sorts the records in ascending order, while DESC sorts them in descending order.
SELECT DISTINCT
DISTINCT is used to remove duplicate values from the result.
SELECT DISTINCT City
FROM Students;
Practical Example
Suppose we want to display the names and cities of all students from the Students table.
SELECT StudentName, City
FROM Students;
The result will contain only StudentName and City.
Important Points
- SELECT is used to retrieve data.
- SELECT does not modify existing records.
- The * symbol retrieves all columns.
- Specific columns can be selected by writing their column names.
- WHERE can be used to filter records.
- ORDER BY can be used to sort records.
- DISTINCT can be used to remove duplicate values.
Summary
In this section, we learned how the SELECT statement retrieves data from SQL Server tables. We covered selecting all columns, selecting specific columns, filtering records using WHERE, sorting records using ORDER BY and retrieving unique values using DISTINCT.