SQL or Structured Query Language is all about storing, retrieving and manipulating data in the database. The most important aspect of SQL is retrieval of data from database. SQL Select statement is used to retrieve data from tables or views.
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.
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
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.
SELECT EmpName FROM Employee WHERE EmpAge<25;
EmpName Amit Jason
- We want to retrieve all the detail of employees who are younger than 25 years of age.
SELECT * FROM Employee WHERE EmpAge<25;
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.
SELECT * FROM Employee WHERE EmpAge<(SELECT EmpAge FROM Employee WHERE EmpSalary=3000);
Reference: Oracle Documentation