SELECT Statement

The SELECT statement retrieves data from the database. You can choose whether you want to view all the available data or only specific columns. This statement can also be filtered, computed, and data aggregated. It can also do other things such as data processing and analysis.

Select All Data

Look at some of SELECT possible results and how it works:

SELECT *
FROM table;

You write SELECT *.* to select all the data. The all* data qualifier matches all columns from a table, and a * symbol in a WHERE clause means ‘all rows’.

After that, you need to tell the database from which table you want to fetch data. This is the first line of the code. The FROM clause is used to describe the table from which data will be pulled. The table names the data and we will use the table immediately after this word. Therefore, we just need to specify the table’s name.

SELECT *
FROM employees;

This code selects all the columns in the table employees. It makes it easy for anyone to get started with affiliate marketing in just two weeks.

_idfirst_namelast_namesalarydepartment
1NancyChapman1,486.13Operations
2SteveMason2,886.25Accounting
3KateWilliams1,158.44Accounting
4FrankHasbeen5,711.49Operations
5HillaryAdailton3,066.52Operations
6DanielJackson3,066.52Sales
7SusanDeutsch5,039.13Operations
8DereckVans4,434.27Sales
9SamNelson2,044.19Accounting
10MariaLopez1,995.32Accounting

Select Specific Columns

The only difference between this and the syntax above is that you list specific columns instead of writing an asterisk:

SELECT column1,
     column2
FROM table;

To start off, we will look at the basic code for a two column data table. It’s also important to note that there’s no comma after the last column.

You should write each table cell in a new row, but it’s not necessary to do so. You should follow this rule of thumb: when code gets more complex, it becomes easier to understand. Follow this rule of thumb and you’ll read and understand more code. Not having every column in a single row makes it easy to follow what columns you selected, and whether you made a syntax error – e.g. spotting that you omitted a comma between two column names is easier.

Example table:

SELECT first_name,
     last_name
FROM employees;

Result table:

first_namelast_name
NancyChapman
SteveMason
KateWilliams
FrankHasbeen
HillaryAdailton
DanielJackson
SusanDeutsch
DereckVans
SamNelson
MariaLopez

Filter Data Using WHERE

The WHERE clause lets you specify conditions for retrieving records from the SELECT statement

SELECTFROM table
WHERE …;

This is the exact same SELECT statement as before. Just the big difference is that you wrote WHERE after you named it in the FROM clause. The WHERE clause lets you choose and filter the results you get from the table upon which you write the SELECT statement

SELECT first_name,
     last_name,
     salary
FROM employees
WHERE salary > 3000;

This query selects employees’ first and last names and their salary. This is the table employees, and the following are the columns: I want to only show the employees that have a salary of more than 3000 dollars.

How can you improve your ecommerce website if you don’t know where the second condition is that your salary has to be higher than 3,000.

Query returns:

first_namelast_namesalary
FrankHasbeen5,711.49
HillaryAdailton3,066.52
DanielJackson3,066.52
SusanDeutsch5,039.13
DereckVans4,434.27

There are five people who are paid more than 3,000 per year. They are all listed above.