Don’t get left behind, learn SQL for beginners
If you’re new to working with data, one of the first tools you’re likely to encounter is SQL, which stands for Structured Query Language. SQL is the standard language used to communicate with relational databases. Whether you’re analyzing employee records, customer data, or sales transactions, SQL provides a simple yet powerful syntax for retrieving the data you need. This article introduces you to the foundational elements of a SQL query: the SELECT
, FROM
, and WHERE
clauses. By the end, you’ll understand how to retrieve specific data from a table and how to filter it using a variety of conditions.
Learn SQL online
Getting Started with a Simple Table
Let’s begin with a sample dataset so that we have something concrete to work with. Imagine you’re working at a company and have access to a table named employees
. This table contains basic information about each employee, including their ID, first and last name, department, salary, and hire date.
Here’s what the data might look like:
employee_id | first_name | last_name | department | salary | hire_date |
---|---|---|---|---|---|
1 | John | Smith | HR | 50000 | 2015-03-10 |
2 | Jane | Doe | Engineering | 80000 | 2018-07-01 |
3 | Sam | Brown | Engineering | 75000 | 2020-01-15 |
4 | Emily | Davis | Marketing | 60000 | 2016-09-23 |
5 | Michael | Wilson | HR | 52000 | 2019-11-11 |
With this table as our starting point, let’s walk through how you can begin querying this data using SQL.
Writing Your First SQL Query
At the heart of SQL is the SELECT
statement. This is what you use when you want to ask the database for specific information. When you write a query, you’re essentially giving the database a question, and it will return a set of results, known as a result set.
A basic SQL query includes three key parts: the SELECT
clause, the FROM
clause, and optionally, the WHERE
clause.
Here’s a simple query that retrieves all the data from the employees
table:
SELECT *
FROM employees;
The asterisk (*
) tells SQL to return all columns. But usually, you’ll want to be more selective. For instance, if you’re only interested in seeing the first names and departments of all employees, you can write:
SELECT first_name, department
FROM employees;
This is more efficient and readable, especially when dealing with tables that have dozens of columns.
Filtering Data with the WHERE Clause
So far, we’ve just been retrieving everything or specific columns from the table. But often, you’ll want to retrieve only certain rows—those that meet specific criteria. This is where the WHERE
clause comes into play.
Let’s say you only want to see the employees who work in the HR department. You can filter the results like this:
SELECT *
FROM employees
WHERE department = 'HR';
The WHERE
clause allows you to specify conditions. In this case, we’re saying, “only return rows where the department is equal to ‘HR’.”
The WHERE
clause supports a variety of comparison operators. For example, you might want to retrieve all employees who earn more than $60,000. That query would look like this:
SELECT *
FROM employees
WHERE salary > 60000;
You can also use less than (<
), less than or equal to (<=
), greater than or equal to (>=
), and not equal to (!=
or <>
), depending on the kind of comparison you’re making.
Combining Multiple Conditions
Sometimes you’ll need to filter data using more than one condition. SQL allows you to do this using the AND
and OR
keywords. Suppose you’re interested in seeing only those employees who are in the Engineering department and earn more than $75,000. You can write:
SELECT *
FROM employees
WHERE department = 'Engineering' AND salary > 75000;
On the other hand, if you’re looking for employees who work in either HR or Marketing, you can use the OR
keyword:
SELECT *
FROM employees
WHERE department = 'HR' OR department = 'Marketing';
hese logical operators make your queries more expressive and flexible.
More Advanced Filtering with IN, BETWEEN, and LIKE
SQL also offers some handy operators for filtering rows based on sets or patterns. One such operator is IN
, which allows you to check if a value appears in a list of possibilities. If you want all employees who are in either HR or Marketing, you could write the query this way instead:
SELECT *
FROM employees
WHERE department IN ('HR', 'Marketing');
Another useful operator is BETWEEN
, which is great for checking if a value falls within a certain range. For instance, if you’re interested in employees who were hired between 2016 and 2019, you might write:
SELECT *
FROM employees
WHERE hire_date BETWEEN '2016-01-01' AND '2019-12-31';
And when you’re dealing with textual data, the LIKE
operator comes in handy for pattern matching. If you want to find employees whose last names start with the letter “D”, you could use:
SELECT *
FROM employees
WHERE last_name LIKE 'D%';
The %
symbol is a wildcard that matches any number of characters, so 'D%'
matches “Davis”, “Dunn”, “Douglas”, and so on.
Handling Missing Data
Sometimes data can be missing in a table—this is represented in SQL with the keyword NULL
. To check for missing values, you don’t use the usual comparison operators. Instead, SQL provides IS NULL
and IS NOT NULL
.
If you want to find employees whose department value is not missing, you would write:
SELECT *
FROM employees
WHERE department IS NOT NULL;
Sort By
Once you’ve filtered your data, a common next step is to sort the results. This is where the ORDER BY
clause comes in. It allows you to specify one or more columns by which to sort the returned rows. By default, SQL sorts in ascending order. Suppose you want to view employees ordered by their salary, from lowest to highest. You would write:
SELECT *
FROM employees
ORDER BY salary;
If instead you want to see the highest earners first, you can sort in descending order by adding the DESC
keyword:
SELECT *
FROM employees
ORDER BY salary DESC;
You can also sort by multiple columns. For example, you might want to sort employees first by department, and then within each department by hire date:
SELECT *
FROM employees
ORDER BY department, hire_date;
Putting It All Together
Let’s finish with a slightly more complex example that combines everything we’ve learned. Suppose you want to retrieve the first and last names of employees in the Engineering department who were hired after January 1, 2018, and who earn more than $70,000. Your query might look like this:
SELECT first_name, last_name
FROM employees
WHERE department = 'Engineering'
AND hire_date > '2018-01-01'
AND salary > 70000;
ORDER BY first_name
This query gives you a highly targeted result set by filtering based on multiple conditions.
Please see the other SQL video learning articles below as part of this learning SQL online course video.
If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY.