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.
_id | first_name | last_name | salary | department |
1 | Nancy | Chapman | 1,486.13 | Operations |
2 | Steve | Mason | 2,886.25 | Accounting |
3 | Kate | Williams | 1,158.44 | Accounting |
4 | Frank | Hasbeen | 5,711.49 | Operations |
5 | Hillary | Adailton | 3,066.52 | Operations |
6 | Daniel | Jackson | 3,066.52 | Sales |
7 | Susan | Deutsch | 5,039.13 | Operations |
8 | Dereck | Vans | 4,434.27 | Sales |
9 | Sam | Nelson | 2,044.19 | Accounting |
10 | Maria | Lopez | 1,995.32 | Accounting |
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_name | last_name |
Nancy | Chapman |
Steve | Mason |
Kate | Williams |
Frank | Hasbeen |
Hillary | Adailton |
Daniel | Jackson |
Susan | Deutsch |
Dereck | Vans |
Sam | Nelson |
Maria | Lopez |
Filter Data Using WHERE
The WHERE clause lets you specify conditions for retrieving records from the SELECT statement
SELECT …
FROM 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_name | last_name | salary |
Frank | Hasbeen | 5,711.49 |
Hillary | Adailton | 3,066.52 |
Daniel | Jackson | 3,066.52 |
Susan | Deutsch | 5,039.13 |
Dereck | Vans | 4,434.27 |
There are five people who are paid more than 3,000 per year. They are all listed above.