SQL Select Statement
SQL Select statement can be used to select or retrieve records from a database table or multiple tables as mentioned below.
- Retrieve data from one or more tables using a single SELECT statement.
- Retrieve data from all columns or selected columns from a table.
- Retrieve data from specific records that satisfy a certain condition or a combination of conditions, using conditional clauses.
In the previous chapter, we used the INSERT statement to insert records into a table.
Now, let's see how to retrieve the records using the SELECT statement.
Syntax
Here is the basic syntax of a SELECT statement.
- The table name is represented as table_name.
- The table columns are represented as column1, column2, and ao on.
- The asterisk
*
is a wildcard character that means everything, means all columns.
Use the below query to retrieve data from all columns.
SELECT * FROM table_name;
Use the below query to retrieve data from specific columns.
SELECT column1_name, column2_name, columnN_name FROM table_name;
Select All from Table
Use the below query to retrieve data from all columns of a table.
SELECT * FROM employees;
After successful execution, the output looks something like this.
employee_id | employee_name | gender | birth_date | hire_date | salary |
1 | Robin Hood | M | 1990-10-10 | 2010-10-15 | 25000 |
2 | Tony Blank | M | 1982-08-07 | 2010-01-05 | 89000 |
3 | Andrew Russel | M | 1998-05-04 | 2012-02-20 | 28000 |
Select Columns from Table
Use the below query to retrieve data from specific columns of a table, which can be used when we don't need data from all the columns.
SELECT employee_id, employee_name, hire_date, salary FROM employees;
After successful execution, the output looks something like this.
employee_id | employee_name | hire_date | salary |
1 | Robin Hood | 2010-10-15 | 25000 |
2 | Tony Blank | 2010-01-05 | 89000 |
3 | Andrew Russel | 2012-02-20 | 28000 |
Select from Multiple Tables
You will learn about this in a separate chapter.
Overall
We now know how to create and execute a SELECT statement to retrieve data from a table.