SELECT and WHERE
SELECT
The basic statement in SQL is SELECT
. We use it to retrieve tables, which is the main action of SQL queries.
It reads much like plain English: we SELECT
specific columns FROM
a specific table.
SELECT [columns] FROM <table>
For example, if we have the table employees:
id | first_name | last_name | salary | date_hired |
---|---|---|---|---|
1 | John | Smith | 75000 | 2020-01-01 |
2 | Jane | Williams | 80000 | 2019-03-14 |
3 | Bill | Johnson | 90000 | 2018-07-22 |
We can SELECT
any subset of columns we need.
For example, if we wanted to get a smaller table that only has the columns id
, first_name
, and salary
, we can use the SELECT
statement to get them FROM
employees
.
We just need to separate the names of the columns we need with commas:
SELECT id, first_name, salary
FROM employees
The output would look like this:
id | first_name | salary |
---|---|---|
1 | John | 75000 |
2 | Jane | 80000 |
3 | Bill | 90000 |
If we wanted to select all the columns from a specific table, we could just use the wildcard (*)
For example, the query
SELECT * FROM employees
would just return us the whole table employees
.
Finally, we can also rename columns through the AS
keyword.
For example,
SELECT id,
first_name AS name,
last_name AS surname
FROM employees
would retrieve us a table with three columns, with first_name
renamed as name
and last_name
renamed as surname
:
id | name | surname |
---|---|---|
1 | John | Smith |
2 | Jane | Williams |
3 | Bill | Johnson |
WHERE clause
The WHERE
clause is a clause that lets us filter the rows returned by our SELECT
statement. To use it, we must add it after the SELECT
.
For example,
SELECT * FROM employees
WHERE salary >= 80000
would return all the columns from the employees
table for which salaries are greater than or equal to 80000. This leaves out the first row because it has a salary of 75000:
id | first_name | last_name | salary | date_hired |
---|---|---|---|---|
2 | Jane | Williams | 80000 | 2019-03-14 |
3 | Bill | Johnson | 90000 | 2018-07-22 |
We can include the usual relationships in the filtering condition after the WHERE
clause:
Equal | = |
---|---|
Not Equal | != |
Less than | < |
Greater Than | > |
Less than or equal to | <= |
Greater than or equal to | >= |
We can also use the logical connectors NOT
, AND
, and OR
to combine conditions, so we can write queries like this:
SELECT first_name, last_name FROM employees
WHERE salary >= 80000 AND date_hired >= 2019-01-01
This would return the field’s first_name and last_name for every employee with a salary higher than 80000 hired since 2019. Only Jane Williams satisfies the two conditions, so our table will have just one row:
first_name | last_name |
---|---|
Jane | Williams |
What we’ve learned so far:
When starting with SQL, the execution order of different clauses may be hard to follow.
Therefore, in the introductory courses, we will finish the lessons with example queries that use clauses we learned up to that point.
Then, we will show their order of execution and the actions they perform.
For now, this is it:
Example query
SELECT first_name AS name, last_name
FROM employees
WHERE salary > 7500 AND last_name != "Williams"
Execution order
FROM
-> Gets the input table.WHERE
-> Filters rows according to a condition.SELECT
-> Selects the columns we need.AS
-> Renames columns.
42%
CompletedYou have 32 sections remaining on this learning path.