SELECT statement is used to retrieve data from the database.
In it’s basic form, a select statement looks as shown below:
Here is an example of select statement used to retrieve the current date from the database.
SELECT current_date;
You can perform simple arithmetic operations in the database and use select statement to view the output:
SELECT 3232+5629+37589;
In Relational databases, data is stored in tables. SELECT statement is used to retrieve data that is stored in the database tables.
Example:
Consider an employee table as shown below:
id | first_name | last_name | department | salary |
101 | bryan | walker | sales | 50,000 |
102 | alex | carpenter | accounts | 55,000 |
103 | sam | white | sales | 60,000 |
104 | john | barry | sales | 54,000 |
105 | bill | wood | accounts | 65,000 |
106 | robert | hopkins | support | 70,000 |
107 | david | lee | support | 72,000 |
108 | jack | sears | support | 65,000 |
109 | tom | young | sales | 55,000 |
Below is the SQL query to return all the data from employee table. ‘*’ is short to return all the columns of a table without listing all the column names.
SELECT * FROM employee; id first_name last_name department salary --- --------- --------- ---------- ------- 101 bryan walker sales 50000 102 alex carpenter accounts 55000 . . .
To return only the selected columns from a table, you need to list all the column names in your select statement. Example, below is the query to return just the id, first name and last name columns from the employee table.
SELECT id, first_name, last_name FROM employee; id first_name last_name --- ---------- ---------- 101 bryan walker 102 alex carpenter . . .