DML – Basic Select Queries
In this session, you will learn:
• Understanding Select Statement
• How to retrieve all the columns from a table
• How to retrieve specific columns from a table
• How to sort the result of a query
• How to eliminate the repetition of same data
DML – Select statement
• This statement tells the database to fetch information from a
table.
Syntax
SELECT *|{[DISTINCT] column|expression [alias]...}
FROM <table_name>;
How to select all columns from a table
SELECT statement uses (*) character to retrieve all records from a table.
Example
Customer SELECT * from Customer
Customer
Customer_ FirstName Street City Zip_Cod Phone
Id e
100 ABC Central Chenna 641088 9852754812
i
101 XYZ Velacherry Chenna 641088 5872526262
i
103 FEG Lakeview Ooty 465424 4541565996
How to select specific columns from a table
Example
Customer SELECT FirstName, Phone
from Customer
Customer
FirstName Phone
ABC 9852754812
XYZ 5872526262
FEG 4541565996
How to sort the results of a query
The ORDER BY clause orders or sorts the result of a query.
Example
Customer SELECT Customer_Id,FirstName
from Customer
order by Customer_Id asc;
desc;
Customer
Customer_Id
Customer_Id FirstName
FirstName
100
100
103 ABC
ABC
FEG
101
101
102 XYZ
XYZ
PQR
102
103
101 PQR
FEG
XYZ
103
102
100 FEG
PQR
ABC
How to remove repetition of same data
DISTINCT clause will eliminate the repetitive appearance of same data
Example
Customer SELECT City from Customer
Chennai
Customer
City
Chenna
i
Chenna
Ooty
SELECT DISTINCT City i
Ooty
from Customer
Chenna
i
THANKS