This article
will show you how you can write and use select statement query in different
ways. In this article i will cover below mention points:
1. Select
all columns
2. Select
Specific column
3. Select
all columns with condition
4. Select
specific column with where clause
5. Select
statement with multiple condition
6. Select
statement for removing duplicate
7. Ascending
& descending order of records
8. Select
top 10 values
9.
Aggregate function like count, sum ,min, max, avg
10.Sub
query in SQL
Please check
the below table. We will perform all the query activity in this DB table.
1. Select all columns: This query will provide all the data
present into the table
Select * from TestData;
|
2. Select Specific column: This table will help us to get
specific column data from table
Select Id,Name,Address,PhoneNo
from TestData
|
3. Select all columns with condition: This will help us to get the on the
bases of the given condition
Select * from TestData Where Id=1;
|
4. Select specific column with where clause: This will help us to get the
specific column on the bases of given conditions
Select Id,Name,Address,PhoneNo
from TestData Where Id=1
|
5. Select statement with multiple conditions: This will tell how we can get data
on the bases of multiple condition
Select Id,Name,Address,PhoneNo
from TestData Where Id=1 and
PhoneNo=00000000
|
If condition
changes we will not get anything.
Select Id,Name,Address,PhoneNo
from TestData Where Id=1 and
PhoneNo=22222222
|
The above query will return no record because the condition is getting false.
6. Select statement for removing duplicate: To remove duplicate record from
table
Select distinct PhoneNo from TestData
|
Table is having duplicate phone no.
As we
execute the query it will return only distinct value.
Ascending:
Select * from TestData order by Id asc
|
Select * from TestData order by Id desc
|
8. Select top few values: To get the specific count of values
Select top 2* from
TestData
|
9. Aggregate function like count, sum ,min, max,
avg : To perform
aggregate oration
Count: To count the total no of records
Select Count(*) from TestData
|
Sum: To get the sum of column values
Select Sum(Id) from TestData
|
Min: To get the minimum value of the column
Select min(Id) from TestData
|
Max: To get the max value of the given column no
Select max(Id) from TestData
|
Avg: To get the Average of the given column numbers
Select Avg(Id) from TestData
|
10. Sub query in SQL: This we use to get the data by
creating query inside a query
Select * from TestData Where Id =(Select Id from
TestData Where Id =3)
|
11. Select query Union: This we use to merge two tables.
Select * from
TestData
Union
Select * from TestData1
|
Here in above query we are trying to merge the two tables.
When we
execute the query we will get records as
0 comments:
Please let me know your view