SELECT Query
Select query is used to retrieve data from a tables. It is the most used SQL query. We can retrieve complete tables, or partial by mentioning conditions using WHERE clause.
Syntax of SELECT Query
SELECT column-name1, column-name2, column-name3, column-nameN from table-name;
Example for SELECT Query
Conside the following Student table,
| S_id | S_Name | age | address | 
|---|---|---|---|
| 101 | Adam | 15 | Noida | 
| 102 | Alex | 18 | Delhi | 
| 103 | Abhi | 17 | Rohtak | 
| 104 | Ankit | 22 | Panipat | 
SELECT s_id, s_name, age from Student.
The above query will fetch information of s_id, s_name and age column from Student table
| S_id | S_Name | age | 
|---|---|---|
| 101 | Adam | 15 | 
| 102 | Alex | 18 | 
| 103 | Abhi | 17 | 
| 104 | Ankit | 22 | 
Example to Select all Records from Table
A special character asterisk 
* is used to address all the data(belonging to all columns) in a query. SELECT statement uses * character to retrieve all records from a table.SELECT * from student;
The above query will show all the records of Student table, that means it will show complete Student table as result.
| S_id | S_Name | age | address | 
|---|---|---|---|
| 101 | Adam | 15 | Noida | 
| 102 | Alex | 18 | Delhi | 
| 103 | Abhi | 17 | Rohtak | 
| 104 | Ankit | 22 | Panipat | 
Example to Select particular Record based on Condition
SELECT * from Student WHERE s_name = 'Abhi';
| 103 | Abhi | 17 | Rohtak | 
Example to Perform Simple Calculations using Select Query
Conside the following Employee table.
| eid | Name | age | salary | 
|---|---|---|---|
| 101 | Adam | 26 | 5000 | 
| 102 | Ricky | 42 | 8000 | 
| 103 | Abhi | 22 | 10000 | 
| 104 | Rohan | 35 | 5000 | 
SELECT eid, name, salary+3000 from Employee;
The above command will display a new column in the result, showing 3000 added into existing salaries of the employees.
| eid | Name | salary+3000 | 
|---|---|---|
| 101 | Adam | 8000 | 
| 102 | Ricky | 11000 | 
| 103 | Abhi | 13000 | 
| 104 | Rohan | 8000 | 
No comments:
Post a Comment