SQL Select Statement
SQL SELECT statement enables us to retrieve the data from the database. The result of SQL Select query is returned in tabular format knows as result set.
SQL Select Query Syntax
SQL Select Query has two syntaxes – one for retrieval of data from particular columns and other for retrieval of all the data that is available in the table.
Retrieval of Data from Particular Column
Please find below the syntax for retrieval of data from particular column(s) of a table.
1 2 3 |
SELECT column_name(s) FROM table_name; |
In the syntax above, the column_name should be specified between the SELECT and FROM keywords. The column_name that is specified in the query should exist in the table.
Retrieval of Data from Complete Table
1 2 3 |
SELECT * FROM table_name; |
In the syntax above, data from all the columns of the table is retrieved.
Let’s us try to understand SQL SELECT query in more detail with some examples.
SQL Select Example
Let us consider the following Employee table for all the scenarios that we will discuss as part of the example.
- We want to retrieve name of all the employees who are younger than 25 years of age.
123SELECT EmpName FROM Employee WHERE EmpAge<25;
Output: - We want to retrieve all the detail of employees who are younger than 25 years of age.
123SELECT * FROM Employee WHERE EmpAge<25;
Output:EmpId EmpName EmpAge EmpSalary 1 Amit 22 2000 3 Jason 24 2750 - We want to retrieve all the details of employees who are younger than employees having salary equal to 3000.SQL SELECT can also be used inside other select as a sub-query. Above requirement requires using select inside a sub query.
123SELECT * FROM Employee WHERE EmpAge<(SELECT EmpAge FROM Employee WHERE EmpSalary=3000);
Output:
Reference: Oracle Documentation