Summary -
In this topic, we described about the below sections -
Why Select Table?
Generally, table contains the data stored as rows and columns. The table data may be a backup or a transactional or information or some other purposeful data. These data can be used in different places for different purposes. To use the data, first it should get retrieved from the table.
For example, a table contains the bank transactions information. If account holder wants to see what transactions performed in the current month for their account. That information should be retrieved from the table and gets displayed.
SELECT statement fulfils the requirement to get the data retrieved from database table.
How to use SELECT Statement?
SELECT Statement is used to fetch all the rows or specific rows data from the table. By using SELECT Statement, we can run multiple queries on one table with the help of different clauses and operators. Some of the most commonly used queries are listed as below -
Syntax -
To retrieve the specified columns from the table are -
SELECT column1, column2, …., columnN
FROM table_name
To retrieve all the columns information –
SELECT *
FROM table_name
[WHERE condition];
Example –
Consider employee_details table with emp_id, emp_name, designation, manager_id, date_of_hire, salary and dept_id for understanding queries.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019--09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Retrieve all columns data from the table.
Requirement – Fetch the all columns data present in the employee_details table. The query is as follows -
SELECT * FROM employee_details;
By executing above query, we can get whole information of the employee_details table. The output was shown as below -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019--09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Get specified columns data from the table.
Requirement – Fetch the emp_id, emp_name columns data from the employee_details table. The query was as follows -
SELECT emp_id, emp_name FROM employee_details;
By executing above query, we can get all rows information of emp_id, emp_name columns from employee_details table. The output was shown as below -
emp_id | emp_name |
---|---|
001 | Employee1 |
002 | Employee2 |
003 | Employee3 |
004 | Employee4 |
005 | Employee5 |
006 | Employee6 |
007 | Employee7 |
008 | Employee8 |
009 | Employee9 |
Scenario – Get distinct values of a column from table.
Requirement – Fetch distinct designations from employee_details table. The query was as follows -
SELECT DISTINCT designation FROM employee_details;
By executing above query,we can get distinct designations from employee_details table. The output was shown as below -
designation |
---|
Analyst |
Clerk |
Director |
Manager |
Salesman |