Introduction
In this article, we will learn about SQL SELECT statements. The SELECT statement retrieves data from a table in SQL Server.
SQL SELECT statement
SQL SELECT statement retrieves the data from one or more database tables. The SELECT statement can fetch all records or fetch a set of records based on a condition such as where, filter, sort, group by, etc.
Here are some of the use cases of the SQL SELECT statement.
- Retrieving data from a single table or multiple tables
- Filtering data based on conditions
- Sorting data
- Aggregating data
- Grouping data
- Joining tables
- Limiting the number of rows returned
Method 1. Retrieving data from a single table or multiple tables
We can use the syntax below to retrieve the data from a table.
SELECT * FROM [Your_Table_Name];
In the above syntax, the * symbol retrieves all columns from a table (Enter table name in place of [Your_Table_Name]).
For example, I want to retrieve the data from a table named EmployeeDetail.
SELECT * FROM EmployeeDetail;
The above SQL statement retrieves all column data from the EmployeeDetail table, and the output looks like the following.

Note SELECT * is not recommended unless you need all the column's data. Use SELECT Column1, Column2 fromTable_Name format for better performance.
SELECT [Column1, Column2, Column3] FROM [Your_Table_Name];
The following SELECT statement uses column names to select data from a table. For example, I want to fetch the records for two columns only (EmpName and EmpAddress) from a table EmployeeDetail.
Select EmpName, EmpAddress from EmployeeDetail
The above SQL statement retrieves all data from two columns, and the output looks like the following.

Method 2. Filtering Data Based on Conditions
Filtering data based on conditions is a fundamental aspect of SQL (Structured Query Language). SQL allows you to specify conditions to filter the data you retrieve from a database, so you can retrieve only the data that meets specific criteria.
The basic syntax for filtering data in SQL is the WHERE clause, which comes after the FROM clause in a SQL query. The WHERE clause specifies a condition that must be true for a row to be included in the result set.
Here is an example of a SQL query that retrieves all rows from a table called "OrderDetail" where the value in the "OrderName" column is equal to "Apple":
SELECT * FROM OrderDetail WHERE OrderName = 'Apple';
The WHERE clause specifies the condition that the "OrderName" column must equal "Apple." Only rows that meet this condition will be included in the result set. In this statement, the "*" means that all columns should be included in the result set.
You can also use other operators in the WHERE clause, such as "<, ">", "<=", ">=", "!=", "<>", "LIKE", "IN", "NOT IN", "BETWEEN," and "NOT BETWEEN." These operators allow you to specify more complex conditions for filtering your data.
For example, here is a SQL query that retrieves all rows from a table called "OrderDetail," where the value in the "OrderDate" column is between January 1, 2021, and December 31, 2021:
SELECT * FROM OrderDetail WHERE OrderDate BETWEEN '2021-01-01' AND '2021-12-31';
In this example, the "BETWEEN" operator is used to specify the range of dates for the "order_date" column.
Overall, filtering data based on conditions is an essential skill in SQL, and the WHERE clause is a powerful tool for selecting only the data you need from a database.
Output

Method 3. Sorting data
In SQL, you can sort data using the ORDER BY clause. The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns.
The basic syntax for sorting data in SQL is as follows:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Here's an explanation of the various components of the syntax
- SELECT Is used to select the columns that you want to retrieve from the table.
- FROM Is used to specify the name of the table that you want to query.
- ORDER BY Is used to sort the result set in ascending (ASC) or descending (DESC) order.
- column1, column2, etc., are the names of the columns that you want to sort by.
For example, let's say we have a table called OrderDetail with columns OrderId, OrderName, OrderAddress, and OrderDate. We can retrieve all the rows in the table sorted by salary in descending order like this
SELECT OrderId,OrderName,OrderAddress,OrderDate FROM OrderDetail ORDER BY OrderName DESC
This will return all the rows in the employees table sorted by salary in descending order.
You can also sort by multiple columns by specifying more than one column in the ORDER BY clause. For example, if we wanted to sort the employees table by age in ascending order and then by salary in descending order, we could use the following SQL statement:
SELECT OrderId,OrderName,OrderAddress,OrderDate FROM OrderDetail ORDER BY OrderName DESC
This will return all the rows in the OrderDetail table sorted by age in ascending order and then by salary in descending order.
Output

Method 4. Aggregating data
Aggregating data in SQL involves combining and summarizing data from multiple rows or groups of rows in a table. SQL provides several built-in functions for aggregating data, including:
-
COUNT() This function returns the number of rows that match a specified condition.
-
SUM() This function returns the sum of a specified numeric column.
-
AVG() This function returns the average of a specified numeric column.
-
MIN() This function returns the minimum value of a specified column.
-
MAX() This function returns the maximum value of a specified column.
To use these functions to aggregate data in SQL, you can include them in your SELECT statement along with the GROUP BY clause, which groups the data by one or more columns. For example, the following SQL query finds the total number of customers for each country in the "Customers" table.
SELECT OrderName, COUNT(OrderId) as Orderdetails
FROM OrderDetail
GROUP BY OrderName;
This query groups the data by the "OrderName" column and counts the number of rows for each group using the COUNT() function. The results are returned with the column headers "OrderName" and "OrderDetails."
Output

Method 5. Grouping data
Grouping data in SQL allows you to aggregate and summarize data based on one or more columns in a table. The most common way to group data in SQL is to use the GROUP BY clause.
Here is an example of how to group data in SQL: Suppose you have a table named "orders" with columns "order_id," "customer_id," "order_date," and "order_amount." To group the total order amounts by customer, you can use the following SQL query:
SELECT customer_id, SUM(order_amount) as total_order_amount
FROM orders
GROUP BY customer_id;
This query will group the orders table by customer_id and calculate the total order amount for each customer. The result will include two columns: customer_id and total_order_amount.
You can also group data by multiple columns. For example, if you want to group the total order amounts by customer and year, you can use the following SQL query:
SELECT customer_id, YEAR(order_date) as order_year, SUM(order_amount) as total_order_amount
FROM orders
GROUP BY customer_id, order_year;
This query will group the orders table by customer_id and order_year and calculate the total order amount for each combination of customer and year. The result will include three columns: customer_id, order_year, and total_order_amount.
Method 6. Joining tables
Joining tables in SQL is a fundamental operation for working with relational databases. A join operation combines rows from two or more tables based on a related column or set of columns.
- Identify the tables you want to join and their relationship: Before joining tables, you need to understand their relationship. This is usually done by looking at the database schema or documentation. Here are the basic steps for joining tables in SQL
- Choose the join type. Several types of join in SQL exist, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. The choice of join type depends on the data you want to retrieve.
- Specify the join condition. The join condition specifies how the tables are related. It typically involves matching values in a column or set of columns in each table.
- Write the SQL statement: Once you have identified the tables, chosen the join type, and specified the join condition, you can write the SQL statement to perform the join.
Here is an example of a basic INNER JOIN operation
SELECT *
FROM OrderDetail
INNER JOIN OrderDetails
ON OrderDetail.OrderId = OrderDetail.OrderId;
In this example, the INNER JOIN combines rows from table1 and table2 based on the matching values in the column of each table. The * symbol indicates that all columns from both tables should be included in the result set.
Output








. 





Mageshwaran RPosted Nov 16, 2019, 9:08 AM
Great,very useful to me..
Onkar SharmaPosted Nov 7, 2019, 7:29 AM
Nice Article sir...
Pankajkumar PatelPosted Sep 18, 2019, 11:37 PM
Good one ...
Mahesh ChandPosted Sep 18, 2019, 10:36 PM
Good start. Nicely done!
Ashok JangidPosted Sep 18, 2019, 7:37 AM
Very nice naresh....
Madan ShekarPosted Sep 18, 2019, 3:57 AM
Nice information