Databases do not just store information, they make data easily accessible. With a little help from the SELECT statement, you can retrieve any set of values from a database that you need.
In this guide, we’re going to discuss what the SQL SELECT statement is and how it works. We’ll go through examples of the SELECT statement in action to help you figure out how to use it on an SQL server.
What is SQL SELECT?
The SELECT statement selects data from a database table. The name gives it away.
You can use the SELECT statement to retrieve data from one or more tables. You can select all the rows in a table, or rows that meet a certain condition or set of conditions.
SELECT clauses use the following structure:
SELECT column1, column2 FROM table;
This statement selects the columns “column1” and “column2” from the table “table”.
It is easier to understand SQL with an example. We currently have a table called employees with the following values:
id | name | title | hired_date | salary |
1 | Thomas Carlton | Sales Representative | 09-08-2019 | 29400 |
2 | Lisa Nelson | Sales Representative | 12-09-2019 | 29400 |
3 | Victoria Carlisle | Sales Director | 04-02-2017 | 37800 |
There are three records in the database. You can learn about how this table was created in our SQL INSERT and SQL UPDATE tutorials.
To view the records inside a database, we need to use a SELECT statement.
Select Specific Columns
We’ve been asked by the executive team to produce a list of all employees and their salaries. They do not need any other information: only the employee names and salaries.
We can use a SELECT statement to retrieve this information:
SELECT ID, name, title, hired_date, salary FROM employees;
This query returns a list of all the employee names and their salaries from the employees table. The result set is:
Name | Salary |
Thomas Carlton | 29400 |
Lisa Nelson | 29400 |
Victoria Carlisle | 37800 |
You can select as many columns as you want from a table.
Select All Columns
We could select all of the columns in our table like this:
SELECT ID, name, title, hired_date, salary FROM employees;
This query is verbose or wordy. If we had more columns to select, it would be even longer. That’s where the * operator comes in handy.
The * operator selects all the columns in a table. It is also called the wildcard operator and it means everything.
SELECT * FROM employees;
This query gives us a full report of all the values in the “employees” table:
id | name | title | hired_date | salary |
1 | Thomas Carlton | Sales Representative | 09-08-2019 | 29400 |
2 | Lisa Nelson | Sales Representative | 12-09-2019 | 29400 |
3 | Victoria Carlisle | Sales Director | 04-02-2017 | 37800 |
Select Based on a Condition
You can select values from a database that meet a condition or set of conditions by using the WHERE clause.
Let’s say that we want to find a list of employees who are sales representatives. We could do this by using a WHERE statement:
SELECT * FROM employees WHERE title = 'Sales Representative';
This query selects all the columns from the employees table. Only records where the value of title is equal to Sales Representative are returned:
id | name | title | hired_date | salary |
1 | Thomas Carlton | Sales Representative | 09-08-2019 | 29400 |
We’ve written a separate tutorial on the WHERE clause that you can read. Read our tutorial on the WHERE clause.
Conclusion
The SELECT statement selects records from a database table. You can select specific columns from a table by specifying those column names in your query. You can select all columns in a database using the wildcard operator (*).
Are you up for a challenge? Write SELECT queries which:
- Retrieve a list of employee names.
- Retrieves a list of employees who have the title Sales Director.
- Retrieve a list of employee titles and salaries.
You’re now ready to use the SELECT statement like an SQL professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot