You will practice using SQL to communicate with databases. You’ll learn how to query a database and filter the results. You’ll also learn how SQL can join multiple tables together in a query.
Learning Objectives
- Discuss how SQL is used within the security profession.
- Describe how a relational database is organized.
- Use SQL to retrieve information from a database.
- Apply filters to SQL queries.
- Use SQL joins to combine multiple tables into a query.
Introduction to SQL and Databases
Video: Welcome to module 4
- Diversity of perspectives and tools is crucial in the field of security.
- SQL is a powerful and flexible tool that allows security analysts to analyze data effectively.
- The course will cover relational databases, SQL queries, SQL filters, and SQL joins.
- Hands-on experience will be provided to gain proficiency in using SQL.
In the world of security,
diversity is important. Diverse perspectives are often needed to find
effective solutions. This is also true of
the tools we use. Your job will often require you to use a lot
of diverse tools. In the last section, we studied the Linux command
line and learned how this tool can help you search
and filter through data, navigate through the
Linux file system, and authenticate users. Now, we’ll learn
about another tool. In this section, we’ll
explore SQL and how it allows you to analyze data in a way needed for your role
as a security analyst. We’re going to start
off by learning about relational databases and
how they’re structured. From there, we’re
going to introduce SQL queries and how to use them to access data
from databases. We then move on to SQL filters, which help us refine our queries to get the exact
information we need. Lastly, we’ll explore SQL joins, which allow you to
combine tables together. When I’m presented with a
problem or a project at work, I often have to sift through
a large amount of data. When I use SQL, I’m able to review data quickly
and provide results with confidence since the queries are consistent and easily executed. SQL is a very powerful
and flexible tool. Throughout this section, you’ll learn how to use
the parts of it you need as a security analyst
and gain hands-on experience. Good luck, and I’ll join you
for the rest of the course!
Video: Introduction to databases
- Our modern world is data-driven, and databases are essential for storing and managing large amounts of data.
- Databases are organized collections of information, similar to spreadsheets, but they can handle more data and support multiple users simultaneously.
- Relational databases are a common type of database that stores data in tables with rows and columns.
- Each table contains fields of information, also called columns, which represent different attributes of the data.
- Rows, also called records, contain specific data related to the columns in the table.
- Relational databases can have multiple tables connected through common columns called keys.
- The primary key is a unique identifier for each row in a table, while the foreign key connects two tables by referencing the primary key of another table.
- SQL is the language used to interact with databases, and we will gain hands-on experience working with it throughout this section.
Our modern world is filled with data and that data almost always guides us
in making important decisions. When working with large amounts of data,
we need to know how to store it, so it’s organized and
quick to access and process. The solution to this is through databases,
and that’s what we’re exploring in this video! To start us off, we can define a database as an organized
collection of information or data. Databases are often
compared to spreadsheets. Some of you may have used Google Sheets or another common spreadsheet
program in the past. While these programs are convenient
ways to store data, spreadsheets are often designed for a single user or
a small team to store less data. In contrast, databases can be accessed
by multiple people simultaneously and can store massive amounts of data. Databases can also perform complex
tasks while accessing data. As a security analyst, you’ll often need to access databases
containing useful information. For example, these could be databases
containing information on login attempts, software and updates, or
machines and their owners. Now that we know how important
databases are for us, let’s talk about how they’re organized and
how we can interact with them. Using databases allow us to store large
amounts of data while keeping it quick and easy to access. There are lots of different ways we can
structure a database, but in this course, we’ll be working with
relational databases. A relational database is a structured
database containing tables that are related to each other. Let’s learn more about what
makes a relational database. We’ll start by examining
an individual table in a larger database of
organizational information. Each table contains fields of information. For example, in this table on employees, these would include fields like
employee_id, device_id, and username. These are the columns of the tables. In addition,
tables contain rows also called records. Rows are filled with specific data
related to the columns in the table. For example, our first row is a record for
an employee whose id is 1,000 and who works in the marketing department. Relational databases often
have multiple tables. Consider an example where we have two
tables from a larger database, one with employees of the company and another
with machines given to those employees. We can connect two tables if
they share a common column. In this example, we establish a relationship between
them with a common employee_id column. The columns that relate two tables
to each other are called keys. There are two types of keys. The first is called a primary key. The primary key refers to a column
where every row has a unique entry. The primary key must not have any
duplicate values, or any null or empty values. The primary key allows us to uniquely
identify every row in our table. For the table of employees,
employee_id is a primary key. Every employee_id is unique and there
are no employee_ids that are duplicate or empty. The second type of key is a foreign key. The foreign key is a column in a table
that is a primary key in another table. Foreign keys, unlike primary keys,
can have empty values and duplicates. The foreign key allows us to
connect two tables together. In our example, we can look at the
employee_id column in the machines table. We previously identified this as
a primary key in the employees table, so we can use this to connect every machine
to their corresponding employee. It’s also important to know that a table
can only have one primary key, but multiple foreign keys. With this information, we’re ready
to move on to the basics of SQL, the language that lets
us work with databases. Throughout this section, we’ll gain hands-on experience working
with the concepts we just covered!
Video: Query databases with SQL
- SQL is an essential tool for security analysts as it allows them to interact with and retrieve information from databases.
- SQL queries are used to request specific data from database tables.
- SQL can be used to retrieve logs, which are records of events that occur within an organization’s systems.
- Security logs are often very large and difficult to process manually, but SQL can be used to quickly extract relevant rows of data.
- SQL can also be used for basic data analytics, which is another valuable skill for security analysts.
- In the next video, we will learn how to make basic SQL queries to a sample database.
As a security analyst, you’ll need to
be familiar both with databases and the tools used to access them. Now that we know the basics of databases, let’s focus on an important tool
used to work with them, SQL, and learn more about how analysts
like yourself might utilize it. SQL, or as it’s also pronounced, S-Q-L,
stands for Structured Query Language. SQL is a programming language used
to create, interact with, and request information from a database. Before learning more about SQL,
we need to define what query means. A query is a request for data from a
database table or a combination of tables. Nearly all relational databases rely
on some version of SQL to query data. The different versions of SQL only have
slight differences in their structure, like where to place quotation marks. Whatever variety of SQL you use, you’ll find it to be a very important
tool in your work as a security analyst. First, let’s discuss how SQL
can help you retrieve logs. A log is a record of events that occur
within an organization’s systems. As a security analyst, you might be tasked
with reviewing logs for various reasons. For example, some logs might contain
details on machines used in a company, and as an analyst, you would need to find those machines
that weren’t configured properly. Other logs might describe the visitors
to your website or web app and the tasks they perform. In that case, you might be looking for unusual patterns
that may point to malicious activity. Security logs are often very large and
hard to process. There are millions of data points, and it’s very time consuming
to find what you need. But this is where SQL comes in! It can
search through millions of data points to extract relevant rows of data using
one query that takes seconds to run. That’s pretty useful, right? SQL is also a very common language
used for basic data analytics, another set of skills that will set
you apart as a security analyst. As a security analyst, you can use
SQL’s filtering to find data to support security-related decisions and
analyze when things may go wrong. For instance, you can identify what machines
haven’t received the latest patch. This is important because patches are
updates that help secure against attacks. As another example,
you can use SQL to determine the best time to update a machine based
on when it’s least used. Now that we know why
SQL is important to us, we’re going to start making basic
queries to a sample database! This is definitely exciting, and
I’ll meet you in the next video.
How do security analysts commonly use SQL in their work?
To find relevant information to support cybersecurity-related decisions
Security analysts commonly use SQL to find relevant information to support cybersecurity-related decisions.
Reading: SQL filtering versus Linux filtering
Reading
Previously, you explored the Linux commands that allow you to filter for specific information contained within files or directories. And, more recently, you examined how SQL helps you efficiently filter for the information you need. In this reading, you’ll explore differences between the two tools as they relate to filtering. You’ll also learn that one way to access SQL is through the Linux command line.
Accessing SQL
There are many interfaces for accessing SQL and many different versions of SQL. One way to access SQL is through the Linux command line.
To access SQL from Linux, you need to type in a command for the version of SQL that you want to use. For example, if you want to access SQLite, you can enter the command sqlite3 in the command line.
After this, any commands typed in the command line will be directed to SQL instead of Linux commands.
Differences between Linux and SQL filtering
Although both Linux and SQL allow you to filter through data, there are some differences that affect which one you should choose.
Structure
SQL offers a lot more structure than Linux, which is more free-form and not as tidy.
For example, if you wanted to access a log of employee log-in attempts, SQL would have each record separated into columns. Linux would print the data as a line of text without this organization. As a result, selecting a specific column to analyze would be easier and more efficient in SQL.
In terms of structure, SQL provides results that are more easily readable and that can be adjusted more quickly than when using Linux.
Joining tables
Some security-related decisions require information from different tables. SQL allows the analyst to join multiple tables together when returning data. Linux doesn’t have that same functionality; it doesn’t allow data to be connected to other information on your computer. This is more restrictive for an analyst going through security logs.
Best uses
As a security analyst, it’s important to understand when you can use which tool. Although SQL has a more organized structure and allows you to join tables, this doesn’t mean that there aren’t situations that would require you to filter data in Linux.
A lot of data used in cybersecurity will be stored in a database format that works with SQL. However, other logs might be in a format that is not compatible with SQL. For instance, if the data is stored in a text file, you cannot search through it with SQL. In those cases, it is useful to know how to filter in Linux.
Key takeaways
To work with SQL, you can access it from multiple different interfaces, such as the Linux command line. Both SQL and Linux allow you to filter for specific data, but SQL offers the advantages of structuring the data and allowing you to join data from multiple tables.
Video: Adedayo: SQL in cybersecurity
Sure, here is a summary of the text:
Adedayo is a Security Engineer at Google who believes that anyone can learn cybersecurity, regardless of their background. He shares his own experience learning SQL, a crucial skill for cybersecurity professionals. Adedayo recommends taking online courses and practicing regularly to improve your SQL skills. He emphasizes the importance of data-driven decision-making in cybersecurity and the fulfillment that comes from working on impactful projects.
My name is Adedayo, and I’m a Security
Engineer at Google. A lot of people think you
need to have a degree in computer science, right
to be able to get into cybersecurity. I
don’t think that’s true. Take me for an example, I started learning
IT from Lagos, Nigeria where I was
born and raised, and then I’m all
the way here now in Silicon Valley,
working for Google. I think that’s just amazing
and a dream come true. You taking this certificate
is a first step to you making a commitment to switching your career to cybersecurity.
Kudos to you on that. SQL is one of the skillset you need to
have in your toolbox as a cybersecurity professional
because you can very quickly make decisions, not just off the bat, but make decisions
with data backing you, and be able to communicate
with your team, with stakeholders
about why you made a decision because it’s one
thing to be able to say, we need to do this, it’s another thing to say
we need to do this and here’s the data that I
wrote my SQL statements about. I learned SQL by, first, as a coursework in school,
that was really great, but I think I forgot everything
about that after school. The next step that I took
was taking online courses, such as the one you’re
taking right now to learn SQL and the fundamentals about it and how
to really use it. Then the first time I used SQL
practically was at Google. You really need to practice. I think with anything else,
practice makes perfect. Being able to, even if it’s
just a few hours a week, put aside time to practice
writing SQL statement. Having that skill is
something that will be very applicable
to your first job, and you can use that to
make data-driven decisions. I feel very fulfilled
working in cybersecurity. I feel very energized, come into work every day. Not only because
I get to work on really complex problems and try to figure out
solutions for them, but I also have great
teammates that we all come together and
tackle the problem. Being able to go
to bed at night, knowing that the work that
I do is for the better of Google users and
Google employees is a very rewarding
feeling for me.
Practice Quiz: Test your knowledge: SQL and databases
Which statement accurately describes the organization of a relational database?
Relational databases contain tables that are related to each other through primary and foreign keys.
A relational database is a structured database containing tables that are related to each other through primary and foreign keys.
What is SQL used for? Select two answers.
- Creating, interacting with, and requesting information from a database
- Finding data to support security-related decisions and analysis
SQL is a programming language used to create, interact with, and request information from a database. SQL’s filtering can be used to find data to support security-related decisions.
A record of attempts to connect to an organization’s network is one example of a log.
True
A record of attempts to connect to an organization’s network is one example of a log. Logs are records of events that occur within an organization’s systems.
Fill in the blank: A request for data from a database table or a combination of tables is called a _____.
query
A request for data from a database table or a combination of tables is called a query.
SQL queries
Video: Basic queries
This video explains how to run a basic SQL query to determine which computer has been assigned to a certain employee. The query uses the SELECT and FROM keywords to specify which columns to return and which table to query. The syntax of SQL is similar to everyday language, and keywords are not case-sensitive. Semicolons are placed at the end of the statement. To return all columns from a table, use an asterisk after SELECT. Congratulations on completing your first basic query in SQL!
In this video, we’re going to be running our very
first SQL query! This query will be based on a common work task
that you might encounter as a security analyst. We’re going to determine which computer has been
assigned to a certain employee. Let’s say we have access
to the employees table. The employees table
has five columns. Two of them, employee_id
and device_id, contain the
information that we need. We’ll write a query
to this table that returns only those two
columns from the table. The two SQL keywords we need for basic SQL queries
are SELECT and FROM. SELECT indicates which
columns to return. FROM indicates which
table to query. The use of these keywords
in SQL is very similar to how we would use these
words in everyday language. For example, we can
ask a friend to select apples and bananas from the big box when going
out to buy fruit. This is already very
similar to SQL. So let’s go ahead and use
SELECT and FROM in SQL to return the information
we need on employees and the
computers they use. We start off by typing
in the SQL statement. After FROM, we’ve identified that the information will be pulled from the employees table. And after SELECT, employee_id
and device_id indicate the two columns we want to return from this table. Notice how a comma separates the two columns that
we want to return. It’s also worth
mentioning a couple of key aspects related to
the syntax of SQL here. Syntax refers to the
rules that determine what is correctly structured
in a computing language. In SQL, keywords are
not case-sensitive, so you could also write select
and from in lowercase, but we’re placing them in
capital letters because it makes the query
easier to understand. Another aspect of this syntax is that semicolons are placed at the end
of the statement. And now, we’ll run the query
by pressing Enter. The output gives us
the information we need to match employees
to their computers. We just ran our very
first SQL query! Suppose you wanted to know what department
the employee using the computer is from, or their username, or the
office they work in. To do that, we can
use SQL to make another statement
that prints out all of the columns
from the table. We can do this by placing
an asterisk after SELECT. This is commonly referred
to as select all. Now, let’s run this query to
the employees table in SQL. And now we have the full
table in the output. You just made it
through a basic query in SQL, congratulations! In the next video, we’ll learn how
to add filters to our queries, so I’ll
meet you there!
Which columns will SELECT * return?
All columns from the specified table
SELECT * instructs SQL to return all columns from the specified table.
Reading: Query a database
Reading
Previously, you explored how SQL is an important tool in the world of cybersecurity and is essential when querying databases. You examined a few basic SQL queries and keywords used to extract needed information from a database. In this reading, you’ll review those basic SQL queries and learn a new keyword that will help you organize your output. You’ll also learn about the Chinook database, which this course uses for queries in readings and quizzes.
Basic SQL query
There are two essential keywords in any SQL query: SELECT and FROM. You will use these keywords every time you want to query a SQL database. Using them together helps SQL identify what data you need from a database and the table you are returning it from.
The video demonstrated this SQL query:
SELECT employee_id, device_id
FROM employees;
In readings and quizzes, this course uses a sample database called the Chinook database to run queries. The Chinook database includes data that might be created at a digital media company. A security analyst employed by this company might need to query this data. For example, the database contains eleven tables, including an employees table, a customers table, and an invoices table. These tables include data such as names and addresses.
As an example, you can run this query to return data from the customers table of the Chinook database:
SELECT customerid, city, country
FROM customers;
SELECT
The SELECT keyword indicates which columns to return. For example, you can return the customerid column from the Chinook database with
SELECT customerid
You can also select multiple columns by separating them with a comma. For example, if you want to return both the customerid and city columns, you should write SELECT customerid, city.
If you want to return all columns in a table, you can follow the SELECT keyword with an asterisk (*). The first line in the query will be SELECT *.
Note: Although the tables you’re querying in this course are relatively small, using SELECT * may not be advisable when working with large databases and tables; in those cases, the final output may be difficult to understand and might be slow to run.
FROM
The SELECT keyword always comes with the FROM keyword. FROM indicates which table to query. To use the FROM keyword, you should write it after the SELECT keyword, often on a new line, and follow it with the name of the table you’re querying. If you want to return all columns from the customers table, you can write:
SELECT *
FROM customers;
When you want to end the query here, you put a semicolon (;) at the end to tell SQL that this is the entire query.
Note: Line breaks are not necessary in SQL queries, but are often used to make the query easier to understand. If you prefer, you can also write the previous query on one line as
SELECT * FROM customers;
ORDER BY
Database tables are often very complicated, and this is where other SQL keywords come in handy. ORDER BY is an important keyword for organizing the data you extract from a table.
ORDER BY sequences the records returned by a query based on a specified column or columns. This can be in either ascending or descending order.
Sorting in ascending order
To use the ORDER BY keyword, write it at the end of the query and specify a column to base the sort on. In this example, SQL will return the customerid, city, and country columns from the customers table, and the records will be sequenced by the city column:
SELECT customerid, city, country
FROM customers
ORDER BY city;
The ORDER BY keyword sorts the records based on the column specified after this keyword. By default, as shown in this example, the sequence will be in ascending order. This means
- if you choose a column containing numeric data, it sorts the output from the smallest to largest. For example, if sorting on customerid, the ID numbers are sorted from smallest to largest.
- if the column contains alphabetic characters, such as in the example with the city column, it orders the records from the beginning of the alphabet to the end.
Sorting in descending order
You can also use the ORDER BY with the DESC keyword to sort in descending order. The DESC keyword is short for “descending” and tells SQL to sort numbers from largest to smallest, or alphabetically from Z to A. This can be done by following ORDER BY with the DESC keyword. For example, you can run this query to examine how the results differ when DESC is applied:
SELECT customerid, city, country
FROM customers
ORDER BY city DESC;
Now, cities at the end of the alphabet are listed first.
Sorting based on multiple columns
You can also choose multiple columns to order by. For example, you might first choose the country and then the city column. SQL then sorts the output by country, and for rows with the same country, it sorts them based on city. You can run this to explore how SQL displays this:
SELECT customerid, city, country
FROM customers
ORDER BY country, city;
Key takeaways
SELECT and FROM are important keywords in SQL queries. You use SELECT to indicate which columns to return and FROM to indicate which table to query. You can also include ORDER BY in your query to organize the output. These foundational SQL skills will support you as you move into more advanced queries.
Video: Basic filters on SQL queries
Filtering is a powerful feature of SQL that allows you to select specific pieces of data from a database. This is done by adding a WHERE clause to your SELECT statement. The WHERE clause specifies the condition for the filter, which can be based on an exact value or a pattern. For example, you could filter a log-in attempts table to find all attempts from a specific country or all offices in the East building.
There are two main types of operators used in SQL filters: the equal to operator (=) and the LIKE operator. The equal to operator returns records that are equal to a particular value, while the LIKE operator returns records that match a pattern. For example, the following query would return all records in the log-in attempts table where the country column is equal to ‘USA’:
SQL
SELECT * FROM log_in_attempts WHERE country = 'USA';
The following query would return all records in the employees table where the office column starts with the word ‘East’:
SQL
SELECT * FROM employees WHERE office LIKE 'East%';
By using filters, you can make your SQL queries more selective and get the exact data you need.
Introduction to SQL Filters
SQL, or Structured Query Language, is a powerful tool for interacting with databases and retrieving specific data. Filtering is a fundamental aspect of SQL queries, allowing you to narrow down the results based on specific criteria. By applying filters, you can ensure that your queries return only the data that is relevant to your analysis or task.
Understanding the WHERE Clause
The WHERE clause is the primary tool for applying filters in SQL queries. It specifies the conditions that must be met for a record to be included in the query’s results. The syntax of the WHERE clause is straightforward:
SQL
SELECT * FROM table_name WHERE condition;
In this syntax, table_name
represents the table from which you want to retrieve data, and condition
is the expression that defines the filtering criteria.
Common Filter Operators
SQL provides a variety of operators for constructing filtering conditions. Here are some of the most commonly used operators:
- Equality (==): Checks if a column value is equal to a specified value.
- Inequality (!=): Checks if a column value is not equal to a specified value.
- Greater than (>): Checks if a column value is greater than a specified value.
- Less than (<): Checks if a column value is less than a specified value.
- Greater than or equal to (>=): Checks if a column value is greater than or equal to a specified value.
- Less than or equal to (<=): Checks if a column value is less than or equal to a specified value.
Filtering Examples
Let’s consider some examples of how to apply filters in SQL queries:
- Filtering by a specific value:
SQL
SELECT * FROM customers WHERE city = 'New York';
This query selects all records from the customers
table where the city
column is equal to ‘New York’.
- Filtering by a range of values:
SQL
SELECT * FROM products WHERE price BETWEEN 100 AND 200;
This query selects all records from the products
table where the price
column is between 100 and 200.
- Filtering using multiple criteria:
SQL
SELECT * FROM orders WHERE status = 'Shipped' AND order_date > '2023-10-01';
This query selects all records from the orders
table where the status
column is equal to ‘Shipped’ and the order_date
column is greater than ‘2023-10-01’.
Combining Filters with LIKE Operator
The LIKE operator is particularly useful for filtering data based on patterns or partial matches. It allows you to specify a pattern using wildcards, such as the percentage sign (%) to represent any number of characters.
- Filtering using LIKE operator:
SQL
SELECT * FROM employees WHERE name LIKE '%Smith';
This query selects all records from the employees
table where the name
column contains the string ‘Smith’ anywhere in the value.
Conclusion
SQL filters are essential for retrieving specific and relevant data from databases. By utilizing the WHERE clause and various filter operators, you can refine your queries to extract the information you need for your analysis or tasks. Whether you’re filtering based on exact values, ranges, or patterns, SQL filters provide the flexibility to narrow down your results and gain valuable insights from your data.
One of the most powerful features
of SQL is its ability to filter. In this video, we’re going to learn how
this helps us make better queries and select more specific pieces
of data from a database. Filtering is selecting data
that match a certain condition. Think of filtering as a way of
only choosing the data we want. Let’s say we wanted to select
apples from a fruit cart. Filtering allows us to specify what
kind of apples we want to choose. When we go buy apples, we might explicitly
say, “Choose only apples that are fresh.” This removes apples that aren’t
fresh from the selection. This is a filter! As a security analyst, you might filter a
log-in attempts table to find all attempts from a specific country. This could be done by applying
a filter on the country column. For example, you could filter to just
return records containing Canada. Before we get started, we need to focus on an
important part of the syntax of SQL. Let’s learn about operators. An operator is a symbol or
keyword that represents an operation. An example of an operator would
be the equal to operator. For example,
if we wanted to find all records that have USA in the country
column, we use country = ‘USA’ To filter a query in SQL, we simply
add an extra line to the SELECT and FROM statement we used before. This extra line will use
a WHERE clause. In SQL, WHERE indicates the condition for a filter. After the keyword WHERE, the specific
condition is listed using operators. So if we wanted to find all of the login
attempts made in the United States, we would create this filter. In this particular condition, we’re
indicating to return all records that have a value in the country
column that is equal to USA. Let’s try putting it
all together in SQL. We’re going to start with
selecting all the columns from the log_in_attempts table. And
then add the WHERE filter. Don’t forget the semicolon! This tells us we finished
the SQL statement. Now, let’s run this query!
Because of our filter, only the rows where the country of the log-in
attempt was USA are returned. In the previous example, the condition for
our filter was based simply on returning records that are equal
to a particular value. We can also make our conditions
more complex by searching for a pattern instead of an exact word. For example, in the employees table,
we have a column for office. We could search for records in this
column that match a certain pattern. Perhaps we might want all
offices in the East building. To search for a pattern, we used the
percentage sign to act as a wildcard for unspecified characters. If we ran a filter for ‘East%’, this would
return all records that start with East — for example, the offices East-120,
East-290, and East-435. When searching for
patterns with the percentage sign, we cannot use the equals operator. Instead, we use another operator, LIKE. LIKE is an operator used with WHERE to
search for a pattern in a column. Since LIKE is an operator,
similar to the equal sign, we use it instead of the equal sign. So, when our goal is to return all values
in the office column that start with the word East, LIKE would
appear in a WHERE clause. Let’s go back to the example in
which we wanted to filter for log-in attempts made in the United States. Imagine that we realize that our database
contains inconsistencies with how the United States is represented. Some entries use US while others use USA. Let’s get into SQL and
apply this new type of filter with LIKE. We’re going to start with
the same first two lines of code because we want to select all
columns from the log-in attempts table. And we’re going to add a filter with
LIKE so that records will be returned if they contain a value in the country
column beginning with the characters US. This includes both US and USA. Let’s run this query to check if
the output changes. This returns all the entries where the user location
was in the United States. And now we can use the LIKE clause to
filter columns based on a pattern! Wow, we’ve already learned how to get
very precise with our database and get exactly the data we
need with one single query. I’m excited for what’s next!
Which WHERE clause contains the correct syntax to return all records that contain a value in the username column that starts with the character 'a'?
WHERE username LIKE ‘a%’;
WHERE username LIKE ‘a%’; contains the correct syntax to return all records that contain a value in the username column that starts with the character ‘a’. The LIKE operator is used with WHERE to search for a pattern in a column. The % wildcard substitutes for any number of other characters.
Reading: The WHERE clause and basic operators
Reading
Previously, you focused on how to refine your SQL queries by using the WHERE clause to filter results. In this reading, you’ll further explore how to use the WHERE clause, the LIKE operator and the percentage sign (%) wildcard. You’ll also be introduced to the underscore (_), another wildcard that can help you filter queries.
How filtering helps
As a security analyst, you’ll often be responsible for working with very large and complicated security logs. To find the information you need, you’ll often need to use SQL to filter the logs.
In a cybersecurity context, you might use filters to find the login attempts of a specific user or all login attempts made at the time of a security issue. As another example, you might filter to find the devices that are running a specific version of an application.
WHERE
To create a filter in SQL, you need to use the keyword WHERE. WHERE indicates the condition for a filter.
If you needed to email employees with a title of IT Staff, you might use a query like the one in the following example. You can run this example to examine what it returns:
SELECT firstname, lastname, title, email
FROM employees
WHERE title = 'IT Staff';
Rather than returning all records in the employees table, this WHERE clause instructs SQL to return only those that contain ‘IT Staff’ in the title column. It uses the equals sign (=) operator to set this condition.
Note: You should place the semicolon (;) where the query ends. When you add a filter to a basic query, the semicolon is after the filter.
Filtering for patterns
You can also filter based on a pattern. For example, you can identify entries that start or end with a certain character or characters. Filtering for a pattern requires incorporating two more elements into your WHERE clause:
- a wildcard
- the LIKE operator
Wildcards
A wildcard is a special character that can be substituted with any other character. Two of the most useful wildcards are the percentage sign (%) and the underscore (_):
- The percentage sign substitutes for any number of other characters.
- The underscore symbol only substitutes for one other character.
These wildcards can be placed after a string, before a string, or in both locations depending on the pattern you’re filtering for.
The following table includes these wildcards applied to the string ‘a’ and examples of what each pattern would return.
Pattern | Results that could be returned |
---|---|
‘a%’ | apple123, art, a |
‘a_’ | as, an, a7 |
‘a__’ | ant, add, a1c |
‘%a’ | pizza, Z6ra, a |
‘_a’ | ma, 1a, Ha |
‘%a%’ | Again, back, a |
‘_a_’ | Car, ban, ea7 |
LIKE
To apply wildcards to the filter, you need to use the LIKE operator instead of an equals sign (=). LIKE is used with WHERE to search for a pattern in a column.
For instance, if you want to email employees with a title of either ‘IT Staff’ or ‘IT Manager’, you can use LIKE operator combined with the % wildcard:
SELECT lastname, firstname, title, email
FROM employees
WHERE title LIKE 'IT%';
This query returns all records with values in the title column that start with the pattern of ‘IT’. This means both ‘IT Staff’ and ‘IT Manager’ are returned.
As another example, if you want to search through the invoices table to find all customers located in states with an abbreviation of ‘NY’, ‘NV’, ‘NS’ or ‘NT’, you can use the ‘N_’ pattern on the state column:
SELECT firstname,lastname, state, country
FROM customers
WHERE state LIKE 'N_';
This returns all the records with state abbreviations that follow this pattern.
Key takeaways
Filters are important when refining what your query returns. WHERE is an essential keyword for adding a filter to your query. You can also filter for patterns by combining the LIKE operator with the percentage sign (%) and the underscore (_) wildcards.
Practice Quiz: Test your knowledge: SQL querie
What is filtering in SQL?
Selecting data that match a certain condition
Filtering in SQL is selecting data that match a certain condition. Analysts use filters in SQL to return the data they need.
You are working with the Chinook database and want to return the firstname, lastname, and phone of all employees. Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT
firstname, lastname, phone
FROM employees;
What is Andrew Adams’ phone number?
+1 (780) 428-9482
Andrew Adams’ phone number is +1 (780) 428-9482. By replacing –??? with SELECT firstname, lastname, phone, you can complete the query and return this result.
A security analyst wants to filter the log_in_attempts table for records where the value in the country column is ‘Canada’. What is a valid query for this?
SELECT *
FROM log_in_attempts
WHERE country = 'Canada';
The security analyst can use the following query to filter the log_in_attempts table for records where the value in the country column is ‘Canada’:
SELECT *
FROM log_in_attempts
WHERE country = ‘Canada’;
Which pattern matches with any string that starts with the character ‘A’?
‘A%’
The percentage sign (%) is a wildcard that substitutes for any number of other characters. The pattern ‘A%’ matches with any string that starts with the character ‘A’.
More SQL filters
Video: Filter dates and numbers
In this video, the focus is on applying SQL queries and filters to new data types, specifically string, numeric, and date and time data. The tutorial introduces these data types and highlights their characteristics. It emphasizes the importance of working with numeric and date and time data in the context of security analysis.
Operators for filtering numeric and date and time data are discussed, including equals, greater than, less than, not equal to, greater than or equal to, and less than or equal to. The video illustrates how to construct SQL queries for filtering log-in attempts after 6 pm and finding patches installed within a specific date range using the BETWEEN operator.
The tutorial emphasizes the use of quotation marks for strings, dates, and times in filters but not for numbers. The viewer is prepared to apply these filtering techniques to various scenarios involving numeric and date data in SQL queries. The next video is teased to cover the expansion of filtering capabilities by using multiple conditions in a single query.
Filtering Dates
Filtering dates in SQL is a common task that is used to extract data from a database for a specific date or date range. There are two main operators that are used to filter dates in SQL:
- The BETWEEN operator: The BETWEEN operator is used to select records where a date column is between two specified dates. For example, the following query selects all records from the
orders
table where theorder_date
column is between ‘2023-01-01’ and ‘2023-12-31’:
SQL
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
- The LIKE operator: The LIKE operator is used to select records where a date column matches a pattern. For example, the following query selects all records from the
orders
table where theorder_date
column starts with the word ‘2023’:
SQL
SELECT * FROM orders WHERE order_date LIKE '2023%';
Filtering Numbers
Filtering numbers in SQL is also a common task that is used to extract data from a database for a specific number or range of numbers. There are several operators that are used to filter numbers in SQL:
- The = operator: The = operator is used to select records where a number column is equal to a specified number. For example, the following query selects all records from the
products
table where theprice
column is equal to 100:
SQL
SELECT * FROM products WHERE price = 100;
- The < operator: The < operator is used to select records where a number column is less than a specified number. For example, the following query selects all records from the
products
table where theprice
column is less than 50:
SQL
SELECT * FROM products WHERE price < 50;
- The > operator: The > operator is used to select records where a number column is greater than a specified number. For example, the following query selects all records from the
products
table where theprice
column is greater than 150:
SQL
SELECT * FROM products WHERE price > 150;
- The <= operator: The <= operator is used to select records where a number column is less than or equal to a specified number. For example, the following query selects all records from the
products
table where theprice
column is less than or equal to 100:
SQL
SELECT * FROM products WHERE price <= 100;
- The >= operator: The >= operator is used to select records where a number column is greater than or equal to a specified number. For example, the following query selects all records from the
products
table where theprice
column is greater than or equal to 150:
SQL
SELECT * FROM products WHERE price >= 150;
Filtering Dates and Numbers Together
You can also use the AND and OR operators to filter dates and numbers together. For example, the following query selects all records from the orders
table where the order_date
column is between ‘2023-01-01’ and ‘2023-12-31’ and the price
column is greater than or equal to 100:
SQL
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31' AND price >= 100;
I hope this tutorial has been helpful. Please let me know if you have any questions.
In this video, we’re going to continue
using SQL queries and filters, but now we’re going to apply
them to new data types. First, let’s explore the three
common data types that you will find in databases: string, numeric,
and date and time. String data is data consisting of
an ordered sequence of characters. These characters could be numbers,
letters, or symbols. For example, you’ll encounter
string data in user names, such as a user name: analyst10. Numeric data is data
consisting of numbers, such as a count of log-in attempts. Unlike strings, mathematical
operations can be used on numeric data, like multiplication or addition. Date and time data refers to data
representing a date and/or time. Previously, we applied filters
using string data, but now let’s work with numeric and
date and time data. As a security analyst, you’ll often
need to query numbers and dates. For example, we could filter patch dates
to find machines that need an update, or we could filter log-in
attempts to return only those made in a certain period of time. We learned about operators
in the last video, and we’re going to use them again for
numbers and dates. Common operators for
working with numeric or date and time data types include: equals,
greater than, less than, not equal to, greater than or
equal to, and less than or equal to. Let’s say you want to find the
log-in attempts made after 6 pm. Because this is past
normal business hours, you want to look for suspicious patterns. You can identify these attempts by using
the greater than operator in your filter. We’ll start writing our query in SQL. We begin by indicating that
we want to select all columns FROM the log_in_attempts table. Then we’ll add our filter with WHERE. Our condition indicates that the value in
the time column must be greater than, or for dates and times, later than ’18:00′,
which is how 6 pm is written in SQL. Let’s run this and examine the output. Perfect! Now we have a list of
log-in attempts made after 6 pm. We can also filter for numbers and
dates by using the BETWEEN operator. BETWEEN is an operator that filters for
numbers or dates within a range. An example of this would
be when looking for all patches installed
within a certain range. Let’s do this! Let’s find all
the patches installed between March 1st, 2021 and September 1st, 2021. In our query, we start with selecting
all records FROM the machines table. And we add the BETWEEN operator
in the WHERE statement. Let’s break down the statement. First, after WHERE, we indicate
which column to filter, in our case, OS_patch_date. Next, comes our operator BETWEEN. We then add the beginning of our range,
type AND, then finish by adding the end
of our range and a semicolon. Now, let’s run this and
explore the output. And now we have a list of all machines
patched between those two dates! Before we wrap up, an important thing to
note is that when we filter for strings, dates, and times, we use quotation marks
to specify what we’re looking for. However, for
numbers, we don’t use quotation marks. With this new knowledge, you’re now ready to work on all sorts of
interesting filters for numbers and dates. In the next video, we’ll be able to
expand our filtering even further by using multiple conditions in one query.
Which WHERE clause has the correct syntax to return all records that have a value of 5, 6, 7, or 8 in the event_id column?
WHERE event_id BETWEEN 5 AND 8;
WHERE event_id BETWEEN 5 AND 8; returns all records that have a value of 5, 6, 7, or 8 in the event_id column. The BETWEEN operator filters for values within a range. The BETWEEN operator is placed before the first value to be included in the range. This is followed by the AND operator and the last value to be included in the range.
Reading: Operators for filtering dates and numbers
Reading
Previously, you examined operators like less than (<) or greater than (>) and explored how they can be used in filtering numeric and date and time data types. This reading summarizes what you learned and provides new examples of using operators in filters.
Numbers, dates, and times in cybersecurity
Security analysts work with more than just string data, or data consisting of an ordered sequence of characters.
They also frequently work with numeric data, or data consisting of numbers. A few examples of numeric data that you might encounter in your work as a security analyst include:
- the number of login attempts
- the count of a specific type of log entry
- the volume of data being sent from a source
- the volume of data being sent to a destination
You’ll also encounter date and time data, or data representing a date and/or time. As a first example, logs will generally timestamp every record. Other time and date data might include:
- login dates
- login times
- dates for patches
- the duration of a connection
Comparison operators
In SQL, filtering numeric and date and time data often involves operators. You can use the following operators in your filters to make sure you return only the rows you need:
operator | use |
---|---|
< | less than |
> | greater than |
= | equal to |
<= | less than or equal to |
>= | greater than or equal to |
<> | not equal to |
Note: You can also use != as an alternative operator for not equal to.
Incorporating operators into filters
These comparison operators are used in the WHERE clause at the end of a query. The following query uses the > operator to filter the birthdate column. You can run this query to explore its output:
SELECT firstname, lastname, birthdate
FROM employees
WHERE birthdate > '1970-01-01';
+-----------+----------+---------------------+
| FirstName | LastName | BirthDate |
+-----------+----------+---------------------+
| Jane | Peacock | 1973-08-29 00:00:00 |
| Michael | Mitchell | 1973-07-01 00:00:00 |
| Robert | King | 1970-05-29 00:00:00 |
+-----------+----------+---------------------+
This query returns the first and last names of employees born after, but not on, ‘1970-01-01’ (or January 1, 1970). If you were to use the >= operator instead, the results would also include results on exactly ‘1970-01-01’.
In other words, the > operator is exclusive and the >= operator is inclusive. An exclusive operator is an operator that does not include the value of comparison. An inclusive operator is an operator that includes the value of comparison.
BETWEEN
Another operator used for numeric data as well as date and time data is the BETWEEN operator. BETWEEN filters for numbers or dates within a range. For example, if you want to find the first and last names of all employees hired between January 1, 2002 and January 1, 2003, you can use the BETWEEN operator as follows:
SELECT firstname, lastname, hiredate
FROM employees
WHERE hiredate BETWEEN '2002-01-01' AND '2003-01-01';
+-----------+----------+---------------------+
| FirstName | LastName | HireDate |
+-----------+----------+---------------------+
| Andrew | Adams | 2002-08-14 00:00:00 |
| Nancy | Edwards | 2002-05-01 00:00:00 |
| Jane | Peacock | 2002-04-01 00:00:00 |
+-----------+----------+---------------------+
Note: The BETWEEN operator is inclusive. This means records with a hiredate of January 1, 2002 or January 1, 2003 are included in the results of the previous query.
Key takeaways
Operators are important when filtering numeric and date and time data. These include exclusive operators such as < and inclusive operators such as <=. The BETWEEN operator, another inclusive operator, helps you return the data you need within a range.
Video: Filters with AND, OR, and NOT
This lesson delves into the utilization of the AND, OR, and NOT operators in SQL to meticulously filter queries. The AND operator enforces the requirement that both specified conditions must be simultaneously fulfilled. In contrast, the OR operator mandates that either one of the stipulated conditions be met. Conversely, the NOT operator effectively negates the specified condition, excluding any records that satisfy it.
To illustrate the application of the AND operator, consider a scenario where you seek to identify apples from a vast container solely comprising large and fresh apples. Employing the AND operator ensures that the retrieved results encompass exclusively large and fresh apples, eliminating any small apples, regardless of their freshness, or any rotten apples, irrespective of their size.
Moving on to the OR operator, imagine a situation where you aim to locate machines running either Operating System 1 or Operating System 3, as both require an urgent patch. Utilizing the OR operator ensures that the query results include machines equipped with either Operating System 1 or Operating System 3, encompassing all machines that meet either condition.
Finally, the NOT operator proves invaluable when you need to exclude specific records from a query. For instance, suppose you intend to update all devices in your company except those running Operating System 3. Leveraging the NOT operator effectively eliminates machines running Operating System 3 from the update process.
In essence, these operators empower you to refine queries with precision, enabling you to extract the precise information you seek from a vast dataset. As you continue your journey to becoming a proficient SQL analyst, mastering these operators will prove instrumental in crafting insightful and impactful queries.
Introduction
SQL filters are used to narrow down the results of a query based on specific criteria. In this tutorial, we will discuss three common SQL filters: AND, OR, and NOT.
The AND Operator
The AND operator is used to combine two or more conditions, and all of the conditions must be true for the record to be included in the results. For example, the following query selects all customers who live in California and have a credit score of over 700:
SQL
SELECT * FROM customers
WHERE state = 'CA' AND credit_score > 700;
The OR Operator
The OR operator is used to combine two or more conditions, and only one of the conditions needs to be true for the record to be included in the results. For example, the following query selects all customers who live in California or have a credit score of over 700:
SQL
SELECT * FROM customers
WHERE state = 'CA' OR credit_score > 700;
The NOT Operator
The NOT operator is used to negate a condition. For example, the following query selects all customers who do not live in California:
SQL
SELECT * FROM customers
WHERE state != 'CA';
Combining Filters
You can combine filters using parentheses to group conditions. For example, the following query selects all customers who live in California and either have a credit score of over 700 or are over the age of 50:
SQL
SELECT * FROM customers
WHERE state = 'CA' AND (credit_score > 700 OR age > 50);
Conclusion
Filters are a powerful tool for narrowing down the results of your SQL queries. By understanding the AND, OR, and NOT operators, you can write more complex and effective queries.
Examples
Here are some additional examples of how to use filters in SQL:
- Select all products that are in stock and cost less than $50:
SQL
SELECT * FROM products
WHERE in_stock = 1 AND price < 50;
- Select all orders that were placed in the year 2023:
SQL
SELECT * FROM orders
WHERE order_date >= '2023-01-01' AND order_date < '2024-01-01';
- Select all employees who are not managers:
SQL
SELECT * FROM employees
WHERE is_manager = 0;
I hope this tutorial helps you learn how to use filters in SQL!
Additional Notes
- You can also use the IN operator to check if a value is in a list of values. For example, the following query selects all customers who live in one of the following states: CA, NY, TX:
SQL
SELECT * FROM customers
WHERE state IN ('CA', 'NY', 'TX');
- You can also use the BETWEEN operator to check if a value is within a range of values. For example, the following query selects all products that cost between $50 and $100:
SQL
SELECT * FROM products
WHERE price BETWEEN 50 AND 100;
In the previous lesson, we learned about even more
ways to filter queries in SQL to work with some typical
security analyst tasks. However, when working with
real security questions, we often have to filter
for multiple conditions. Vulnerabilities, for instance, might depend on more
than one factor. For example, a security vulnerability
might be related to machines using a
specific email client on a specific operating system. So, to find the possible
vulnerabilities, we need to find machines using both the email client and
the operating system. To make a query with multiple conditions
that must be met, we use the AND operator between
two separate conditions. AND is an operator
that specifies that both conditions must
be met simultaneously. Bringing this back to our
fruit and vegetable analogy, this is the same as asking
someone to select apples from the big box where the
apples are large and fresh. This means our results won’t
include any small apples even if they’re fresh, or any rotten apples even
if they’re large. They’ll only include
large fresh apples. The apples must meet
both conditions. Going back to our database, the machines table lists all operating systems
and email clients. We want a list of machines
running Operating System 1 and a list of machines
using Email Client 1. We’ll use the left
and right circles in the Venn diagram to
represent these groups. We need SQL to select
the machines that have both OS 1 and
Email Client 1. The filled-in area at
the intersection of these circles represents
this condition. Let’s take this and
implement it in SQL. First, we’re going to start by building the first
lines of the query, telling SQL to SELECT* all columns FROM the
machines table. Then, we’ll add the WHERE clause. Let’s examine this more closely. First, we indicate the first condition
that it must meet, that the operating system
column has a value of ‘0S 1’ Then, we use AND to join
this to another condition. And finally, we enter
the other condition, in this case that the email client column should have a value of ‘Email Client 1’ And this is how you use the
AND operator in SQL! Let’s run this to get
the query results. Perfect! All the results
match both our conditions! Let’s keep going and explore
more ways to combine different conditions by
working with the OR operator. The OR operator is
an operator that specifies that either
condition can be met. In a Venn diagram, let’s say each circle
represents a condition. When they are joined with OR, SQL would select all rows that satisfy one of the conditions. And it’s also ok if it
meets both conditions. Let’s run another query
and use the OR operator. Let’s say that we wanted the filter to identify
machines that have either OS 1 or OS 3 because
both types need a patch. We’ll type in these conditions. Let’s examine this more closely. After WHERE, our first
condition indicates we want to filter, so that the query
selects machines with ‘OS 1’ We use the OR operator
because we also want to find records that
match another condition. This additional condition
is placed after OR and indicates to also select
machines running ‘OS 3’ Executing the query, our results now include
records that have a value of either OS 1 or OS 3 in the
operating system column. Good job, we’re running
some complex queries. The last operator
we’re going to go into is the NOT operator. NOT negates a condition. In a diagram, we can show this by selecting every entry that does
not match our condition. The condition is
represented by the circle. The filled-in portion outside the circle represents
what gets returned. This is all data that does
not match the condition. For example, when
picking out fruit, you can be looking for any
fruit that is not an apple. That is a lot more efficient than telling your
friend you want a banana or an orange
or a lime, and so on. Suppose you wanted to update all of the devices in your company except for
the ones using OS 3. Bringing this into SQL, we can write this query. We place NOT after WHERE and before the
condition of the filter. Executing these queries
gives us the list of all the machines that
aren’t running OS 3, and now we know which
machines to update. That was a lot of new content
that we just looked into, but you’re learning more
and more SQL that you can use on your journey
to become an analyst! In the next video, we’ll be learning
how to combine and join two tables together to expand the kinds of queries we can
run. I’ll meet you there!
Why might a security analyst use the OR operator?
To find the ID numbers of all employees working in either the U.S. or Canada
A security analyst might use the OR operator to find the ID numbers of all employees working in either the U.S. or Canada. The OR operator specifies that either condition can be met.
Reading: More on filters with AND, OR, and NOT
Reading
Previously, you explored how to add filters containing the AND, OR, and NOT operators to your SQL queries. In this reading, you’ll continue to explore how these operators can help you refine your queries.
Logical operators
AND, OR, and NOT allow you to filter your queries to return the specific information that will help you in your work as a security analyst. They are all considered logical operators.
AND
First, AND is used to filter on two conditions. AND specifies that both conditions must be met simultaneously.
As an example, a cybersecurity concern might affect only those customer accounts that meet both the condition of being handled by a support representative with an ID of 5 and the condition of being located in the USA. To find the names and emails of those specific customers, you should place the two conditions on either side of the AND operator in the WHERE clause:
SELECT firstname, lastname, email, country, supportrepid
FROM customers
WHERE supportrepid = 5 AND country = 'USA';
+-----------+----------+-------------------------+---------+--------------+
| FirstName | LastName | Email | Country | SupportRepId |
+-----------+----------+-------------------------+---------+--------------+
| Jack | Smith | jacksmith@microsoft.com | USA | 5 |
| Kathy | Chase | kachase@hotmail.com | USA | 5 |
| Victor | Stevens | vstevens@yahoo.com | USA | 5 |
| Julia | Barnett | jubarnett@gmail.com | USA | 5 |
+-----------+----------+-------------------------+---------+--------------+
Running this query returns four rows of information about the customers. You can use this information to contact them about the security concern.
OR
The OR operator also connects two conditions, but OR specifies that either condition can be met. It returns results where the first condition, the second condition, or both are met.
For example, if you are responsible for finding all customers who are either in the USA or Canada so that you can communicate information about a security update, you can use an OR operator to find all the needed records. As the following query demonstrates, you should place the two conditions on either side of the OR operator in the WHERE clause:
SELECT firstname, lastname, email, country
FROM customers
WHERE country = 'Canada' OR country = 'USA';
+-----------+------------+--------------------------+---------+
| FirstName | LastName | Email | Country |
+-----------+------------+--------------------------+---------+
| François | Tremblay | ftremblay@gmail.com | Canada |
| Mark | Philips | mphilips12@shaw.ca | Canada |
| Jennifer | Peterson | jenniferp@rogers.ca | Canada |
| Frank | Harris | fharris@google.com | USA |
| Jack | Smith | jacksmith@microsoft.com | USA |
| Michelle | Brooks | michelleb@aol.com | USA |
| Tim | Goyer | tgoyer@apple.com | USA |
| Dan | Miller | dmiller@comcast.com | USA |
| Kathy | Chase | kachase@hotmail.com | USA |
| Heather | Leacock | hleacock@gmail.com | USA |
| John | Gordon | johngordon22@yahoo.com | USA |
| Frank | Ralston | fralston@gmail.com | USA |
| Victor | Stevens | vstevens@yahoo.com | USA |
| Richard | Cunningham | ricunningham@hotmail.com | USA |
| Patrick | Gray | patrick.gray@aol.com | USA |
| Julia | Barnett | jubarnett@gmail.com | USA |
| Robert | Brown | robbrown@shaw.ca | Canada |
| Edward | Francis | edfrancis@yachoo.ca | Canada |
| Martha | Silk | marthasilk@gmail.com | Canada |
| Aaron | Mitchell | aaronmitchell@yahoo.ca | Canada |
| Ellie | Sullivan | ellie.sullivan@shaw.ca | Canada |
+-----------+------------+--------------------------+---------+
The query returns all customers in either the US or Canada.
Note: Even if both conditions are based on the same column, you need to write out both full conditions. For instance, the query in the previous example contains the filter WHERE country = ‘Canada’ OR country = ‘USA’.
NOT
Unlike the previous two operators, the NOT operator only works on a single condition, and not on multiple ones. The NOT operator negates a condition. This means that SQL returns all records that don’t match the condition specified in the query.
For example, if a cybersecurity issue doesn’t affect customers in the USA but might affect those in other countries, you can return all customers who are not in the USA. This would be more efficient than creating individual conditions for all of the other countries. To use the NOT operator for this task, write the following query and place NOT directly after WHERE:
SELECT firstname, lastname, email, country
FROM customers
WHERE NOT country = 'USA';
+-----------+-------------+-------------------------------+----------------+
| FirstName | LastName | Email | Country |
+-----------+-------------+-------------------------------+----------------+
| Luís | Gonçalves | luisg@embraer.com.br | Brazil |
| Leonie | Köhler | leonekohler@surfeu.de | Germany |
| François | Tremblay | ftremblay@gmail.com | Canada |
| Bjørn | Hansen | bjorn.hansen@yahoo.no | Norway |
| František | Wichterlová | frantisekw@jetbrains.com | Czech Republic |
| Helena | Holý | hholy@gmail.com | Czech Republic |
| Astrid | Gruber | astrid.gruber@apple.at | Austria |
| Daan | Peeters | daan_peeters@apple.be | Belgium |
| Kara | Nielsen | kara.nielsen@jubii.dk | Denmark |
| Eduardo | Martins | eduardo@woodstock.com.br | Brazil |
| Alexandre | Rocha | alero@uol.com.br | Brazil |
| Roberto | Almeida | roberto.almeida@riotur.gov.br | Brazil |
| Fernanda | Ramos | fernadaramos4@uol.com.br | Brazil |
| Mark | Philips | mphilips12@shaw.ca | Canada |
| Jennifer | Peterson | jenniferp@rogers.ca | Canada |
| Robert | Brown | robbrown@shaw.ca | Canada |
| Edward | Francis | edfrancis@yachoo.ca | Canada |
| Martha | Silk | marthasilk@gmail.com | Canada |
| Aaron | Mitchell | aaronmitchell@yahoo.ca | Canada |
| Ellie | Sullivan | ellie.sullivan@shaw.ca | Canada |
| João | Fernandes | jfernandes@yahoo.pt | Portugal |
| Madalena | Sampaio | masampaio@sapo.pt | Portugal |
| Hannah | Schneider | hannah.schneider@yahoo.de | Germany |
| Fynn | Zimmermann | fzimmermann@yahoo.de | Germany |
| Niklas | Schröder | nschroder@surfeu.de | Germany |
+-----------+-------------+-------------------------------+----------------+
(Output limit exceeded, 25 of 46 total rows shown)
SQL returns every entry where the customers are not from the USA.
Pro tip: Another way of finding values that are not equal to a certain value is by using the <> operator or the != operator. For example, WHERE country <> ‘USA’ and WHERE country != ‘USA’ are the same filters as WHERE NOT country = ‘USA’.
Combining logical operators
Logical operators can be combined in filters. For example, if you know that both the USA and Canada are not affected by a cybersecurity issue, you can combine operators to return customers in all countries besides these two. In the following query, NOT is placed before the first condition, it’s joined to a second condition with AND, and then NOT is also placed before that second condition. You can run it to explore what it returns:
SELECT firstname, lastname, email, country
FROM customers
WHERE NOT country = 'Canada' AND NOT country = 'USA';
+-----------+-------------+-------------------------------+----------------+
| FirstName | LastName | Email | Country |
+-----------+-------------+-------------------------------+----------------+
| Luís | Gonçalves | luisg@embraer.com.br | Brazil |
| Leonie | Köhler | leonekohler@surfeu.de | Germany |
| Bjørn | Hansen | bjorn.hansen@yahoo.no | Norway |
| František | Wichterlová | frantisekw@jetbrains.com | Czech Republic |
| Helena | Holý | hholy@gmail.com | Czech Republic |
| Astrid | Gruber | astrid.gruber@apple.at | Austria |
| Daan | Peeters | daan_peeters@apple.be | Belgium |
| Kara | Nielsen | kara.nielsen@jubii.dk | Denmark |
| Eduardo | Martins | eduardo@woodstock.com.br | Brazil |
| Alexandre | Rocha | alero@uol.com.br | Brazil |
| Roberto | Almeida | roberto.almeida@riotur.gov.br | Brazil |
| Fernanda | Ramos | fernadaramos4@uol.com.br | Brazil |
| João | Fernandes | jfernandes@yahoo.pt | Portugal |
| Madalena | Sampaio | masampaio@sapo.pt | Portugal |
| Hannah | Schneider | hannah.schneider@yahoo.de | Germany |
| Fynn | Zimmermann | fzimmermann@yahoo.de | Germany |
| Niklas | Schröder | nschroder@surfeu.de | Germany |
| Camille | Bernard | camille.bernard@yahoo.fr | France |
| Dominique | Lefebvre | dominiquelefebvre@gmail.com | France |
| Marc | Dubois | marc.dubois@hotmail.com | France |
| Wyatt | Girard | wyatt.girard@yahoo.fr | France |
| Isabelle | Mercier | isabelle_mercier@apple.fr | France |
| Terhi | Hämäläinen | terhi.hamalainen@apple.fi | Finland |
| Ladislav | Kovács | ladislav_kovacs@apple.hu | Hungary |
| Hugh | O'Reilly | hughoreilly@apple.ie | Ireland |
+-----------+-------------+-------------------------------+----------------+
(Output limit exceeded, 25 of 38 total rows shown)
Key takeaways
Logical operators allow you to create more specific filters that target the security-related information you need. The AND operator requires two conditions to be true simultaneously, the OR operator requires either one or both conditions to be true, and the NOT operator negates a condition. Logical operators can be combined together to create even more specific queries.
Quiz: Portfolio Activity: Apply filters to SQL queries
Reading
Activity Overview
In this activity, you will create a new portfolio document to demonstrate your experience using SQL. You can add this document to your cybersecurity portfolio, which you can share with prospective employers or recruiters. To review the importance of building a professional portfolio and options for creating your portfolio, read Create a cybersecurity portfolio.
To create your portfolio document, you will review a scenario and follow a series of steps. This scenario is connected to the lab you have just completed about using the AND, OR, and NOT operators in SQL to filter for information. You will explain the queries you performed in that lab, and this will help you prepare for future job interviews and other steps in the hiring process.
Be sure to complete this activity and answer the questions that follow before moving on. The next course item will provide you with a completed exemplar to compare to your own work.
Scenario
Review the scenario below. Then complete the step-by-step instructions.
You are a security professional at a large organization. Part of your job is to investigate security issues to help keep the system secure. You recently discovered some potential security issues that involve login attempts and employee machines.
Your task is to examine the organization’s data in their employees and log_in_attempts tables. You’ll need to use SQL filters to retrieve records from different datasets and investigate the potential security issues.
Note: This scenario involves the same queries as the ones the Filter with AND, OR, and NOT lab. You can revisit the lab to get screenshots to include in your portfolio document. If you choose, it’s also possible to complete this activity without revisiting the lab by typing your queries in the template.
Step-By-Step Instruction
Follow the instructions to complete each step of the activity. Then, answer the 8 questions at the end of the activity before going to the next course item to compare your work to a completed exemplar.
Step 1: Access the template
To use the template for this course item, click the following link and select Use Template. (In this step, you will just open the template. More instructions for how to use the template will be included in later steps.)
Link to template: Apply filters to SQL queries
Step 2: Access supporting materials
The following supporting materials will help you complete this activity. Keep them open as you proceed to the next steps.
To use the supporting materials for this course item, click the links.
The Instructions for including SQL queries document provides instructions and best practices for including samples of SQL queries in your portfolio activity.
Link to supporting materials: Instructions for including SQL queries
The Table formats document describes how the tables used for this portfolio activity are organized.
Link to supporting material: Table formats
Note: It is recommended that you use the Filter with AND, OR, and NOT lab to complete this portfolio activity. If you’re revisiting the lab, the Table formats document is optional. You will also be able to view the tables in the lab.
Step 3: Retrieve after hours failed login attempts
You recently discovered a potential security incident that occurred after business hours. To investigate this, you need to query the log_in_attempts table and review after hours login activity. Use filters in SQL to create a query that identifies all failed login attempts that occurred after 18:00. (The time of the login attempt is found in the login_time column. The success column contains a value of 0 when a login attempt failed; you can use either a value of 0 or FALSE in your query to identify failed login attempts.)
Describe your query and how it works in the Retrieve after hours failed login attempts section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 4: Retrieve login attempts on specific dates
A suspicious event occurred on 2022-05-09. To investigate this event, you want to review all login attempts which occurred on this day and the day before. Use filters in SQL to create a query that identifies all login attempts that occurred on 2022-05-09 or 2022-05-08. (The date of the login attempt is found in the login_date column.)
Describe your query and how it works in the Retrieve login attempts on specific dates section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 5: Retrieve login attempts outside of Mexico
There’s been suspicious activity with login attempts, but the team has determined that this activity didn’t originate in Mexico. Now, you need to investigate login attempts that occurred outside of Mexico. Use filters in SQL to create a query that identifies all login attempts that occurred outside of Mexico. (When referring to Mexico, the country column contains values of both MEX and MEXICO, and you need to use the LIKE keyword with % to make sure your query reflects this.)
Describe your query and how it works in the Retrieve login attempts outside of Mexico section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 6: Retrieve employees in Marketing
Your team wants to perform security updates on specific employee machines in the Marketing department. You’re responsible for getting information on these employee machines and will need to query the employees table. Use filters in SQL to create a query that identifies all employees in the Marketing department for all offices in the East building.
(The department of the employee is found in the department column, which contains values that include Marketing. The office is found in the office column. Some examples of values in this column are East-170, East-320, and North-434. You’ll need to use the LIKE keyword with % to filter for the East building.)
Describe your query and how it works in the Retrieve employees in Marketing section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 7: Retrieve employees in Finance or Sales
Your team now needs to perform a different security update on machines for employees in the Sales and Finance departments. Use filters in SQL to create a query that identifies all employees in the Sales or Finance departments. (The department of the employee is found in the department column, which contains values that include Sales and Finance.)
Describe your query and how it works in the Retrieve employees in Finance or Sales section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 8: Retrieve all employees not in IT
Your team needs to make one more update to employee machines. The employees who are in the Information Technology department already had this update, but employees in all other departments need it. Use filters in SQL to create a query which identifies all employees not in the IT department. (The department of the employee is found in the department column, which contains values that include Information Technology.)
Describe your query and how it works in the Retrieve all employees not in IT section of the Apply filters to SQL queries template.
In the Filter with AND, OR, and NOT lab, take a screenshot of the SQL query you used and copy it into the template. Or, type this query directly into the template.
Step 9: Finalize your document
To finalize the document and make its purpose clear to potential employers, be sure to complete the Project description and Summary sections of the Apply filters to SQL queries template.
In the Project description section, give a general overview of the scenario and what you accomplish through SQL. Write two to four sentences.
In the Summary section, provide a short summary of the previous tasks and connect them to the scenario. Write approximately two to four sentences.
What to Include in Your Response
Be sure to include the following in your completed activity:
- Screenshots of your queries or typed versions of the queries
- Explanations of your queries
- A project description at the beginning
- A summary at the end
- Details on using LIKE to search for a pattern
- Details on filtering for dates and times
- Details on using AND and OR to filter on multiple conditions
- Details on using NOT in filters
Reading: Portfolio Activity Exemplar: Apply filters to SQL queries
Reading
Completed Exemplar
To review the exemplar for this course item, click the following link and select Use Template.
Link to exemplar: Apply filters to SQL queries
Assessment of Exemplar
Compare the exemplar to your completed activity. Review your work using each of the criteria in the exemplar. What did you do well? Where can you improve? Use your answers to these questions to revise your project as needed and guide you as you continue to progress through the certificate program.
Note: The exemplar represents one possible way to complete the Apply filters to SQL queries portfolio activity. Yours will likely differ in certain ways. What’s important is that you understand how to use SQL queries to apply filters.
The exemplar uses details from the given scenario and includes the following:
- Screenshots of queries or typed versions of the queries
- Explanations of queries
- A project description at the beginning
- A summary at the end
- Details on using LIKE to search for a pattern
- Details on filtering for dates and times
- Details on using AND and OR to filter on multiple conditions
- Details on using NOT in filters
Practice Quiz: Test your knowledge: More SQL filters
Which filter outputs all records with values in the date column between ’01-01-2015′ (January 1, 2015) and ’01-04-2015′ (April 1, 2015)?
WHERE date BETWEEN ’01-01-2015′ AND ’01-04-2015′;
The filter WHERE date BETWEEN ’01-01-2015′ AND ’01-04-2015′; outputs all records with values in the date column between ’01-01-2015′ and ’01-04-2015′.
Which operator is most efficient at returning all records with a status other than ‘successful’?
NOT
NOT is most efficient at returning all records with a status other than ‘successful’. The NOT operator negates a condition. In this case, it can be used in a filter of WHERE NOT status = ‘successful’;.
You are working with the Chinook database. You want to find the first and last names of customers who have a value in the country column of either ‘Brazil’ or ‘Argentina’. Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT firstname, lastname, country
FROM customers
WHERE country = 'Brazil' OR country = 'Argentina';
How many customers are from Brazil or Argentina?
6
While working as an analyst, you encounter a query that includes the following filter:
SELECT *
FROM customers
WHERE country = ‘USA’ AND state = ‘NV’;
What will this query return?
Information about customers who have a value of ‘USA’ in the country column and a value of ‘NV’ in the state column.
The query returns information about customers who have a value of ‘USA’ in the country column and a value of ‘NV’ in the state column. The AND operator specifies that both conditions must be met simultaneously.
SQL joins
Video: Join tables in SQL
Joining tables is a useful technique for combining information from two or more tables in a database. The syntax for joining tables is as follows:
SQL
SELECT column1, column2, ...
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
The INNER JOIN
keyword tells SQL to only return rows that have a matching value in both tables. The ON
clause specifies the column that the join should be based on.
In the example given in the text, the employee_id
column is used to join the employees
and machines
tables. The resulting query returns a list of users and their office location, along with the operating system they use on their machines.
Other types of joins, such as LEFT JOIN
and RIGHT JOIN
, will return rows even if there is no matching value in one of the tables. We will discuss these joins in the next video.
Combining Data from Multiple Tables: Mastering Joins in SQL
SQL, or Structured Query Language, serves as the foundation for interacting with relational databases. It enables users to manipulate, retrieve, and manage data stored within these databases. Among its capabilities, SQL excels at combining data from multiple tables using a powerful technique known as joining.
Understanding the Need for Joins
In relational databases, data is organized into tables, each containing specific information about a particular entity or aspect of a system. For instance, a database might have separate tables for employees, customers, orders, and products.
Often, queries require combining data from multiple tables to gain insights or perform comprehensive analyses. This is where joins come into play. Joins allow us to connect related data from different tables, providing a unified view of the information.
Types of Joins: Connecting Tables Effectively
SQL offers various types of joins, each serving a specific purpose in data retrieval and manipulation. Let’s explore the most common join types:
- INNER JOIN: This is the most fundamental join type, returning rows that have matching values in both tables. It ensures that only records with corresponding data are included in the results.
- LEFT JOIN: Unlike INNER JOIN, LEFT JOIN returns all rows from the left table, regardless of whether there’s a matching value in the right table. This means that even if no matching data exists, the left table’s rows will still be included in the result set.
- RIGHT JOIN: In contrast to LEFT JOIN, RIGHT JOIN returns all rows from the right table, regardless of whether there’s a matching value in the left table. This ensures that the right table’s rows are always included, even if there’s no corresponding data in the left table.
- FULL OUTER JOIN: This join combines the functionality of LEFT and RIGHT JOINs, returning all rows from both tables, regardless of whether there’s a matching value in the other table. It ensures that no rows are excluded, even if there’s no corresponding data in either table.
Implementing Joins in SQL: Practical Examples
To effectively utilize joins in SQL queries, understanding the syntax and application of each join type is crucial. Let’s consider some practical examples:
INNER JOIN: Suppose we have two tables, employees
and departments
, with corresponding columns employee_id
and department_id
. To retrieve employee names and their respective department names, we can use an INNER JOIN:
SQL
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
LEFT JOIN: Consider a scenario where we want to list all products, even if they have no associated orders. Using a LEFT JOIN, we can achieve this:
SQL
SELECT products.product_name, orders.order_id
FROM products
LEFT JOIN orders ON products.product_id = orders.product_id;
RIGHT JOIN: Imagine we need to display all customers, even if they haven’t placed any orders yet. A RIGHT JOIN can fulfill this requirement:
SQL
SELECT customers.name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
FULL OUTER JOIN: Suppose we want to list all employees and their corresponding managers, even if some employees don’t have a manager assigned. A FULL OUTER JOIN can handle this task:
SQL
SELECT employees.name, managers.name AS manager_name
FROM employees
FULL OUTER JOIN managers ON employees.manager_id = managers.manager_id;
Mastering Joins: Key Takeaways
- Joins are essential for combining data from multiple tables in SQL.
- Understand the purpose and syntax of different join types: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
- Practice using joins in SQL queries to retrieve and analyze data effectively.
If you run the following query, what will it return? Select all that apply.
SELECT *
FROM log_in_attempts
INNER JOIN employees ON log_in_attempts.username = employees.username;
- All rows in the log_in_attempts and employees tables that match on username
- All columns in the log_in_attempts and employees tables
This query will return all rows in the log_in_attempts and employees tables that match on username and all columns in the log_in_attempts and employees tables. INNER JOIN returns rows matching on a specified column that exists in more than one table. It returns all columns that are indicated following the SELECT keyword. In this case, SELECT * indicates to return all columns.
You’ve already
learned a lot about SQL queries and
filters. Nice work! The last concept
we’re introducing in this section is joining tables
when querying a database. This is helpful when you need information from two different
tables in a database. Let’s say we have two tables: one that tells us about
security vulnerabilities of different operating
systems, and one about different machines
in our company, including their
operating systems. Having the ability
to combine them gives us a list of
vulnerable machines. That’s pretty cool, right? First, let’s start talking about
the syntax of joins. Since we’re working
with two tables now, we need a way to tell SQL what table we’re
picking columns from. In our example database, we have an employee_id column in both the employees table
and the machines table. In SQL statements that
contain two columns, SQL needs to know which
column we’re referring to. The way to resolve this is by writing the name of
the table first, then a period, and then
the name of a column. So, we would have employees
followed by a period, followed by the column name. This is the employee_id column
for the employees table. Similarly, this is the
employee_id column for the machines table. Now that we understand
this syntax, let’s apply it to a join! Imagine that we want to get a deeper understanding of the employees accessing the
machines in our company. By joining the employees and the machines tables,
we can do this! We first need to identify the shared column that we’ll use to connect the two tables. In this case, we’ll
use a primary key and one table to connect to another table where
it’s a foreign key. The primary key of the
employees table is employee_id, which is a foreign key
in the machines table. employee_id is a primary key in the employees
table because it has a unique value for every row in the employees table,
and no empty values. We don’t have a guarantee that
the employee_id column in the machines table follows the same criteria since it’s a foreign key and
not a primary key. Next, we’ll use a type of
join called an INNER JOIN. An INNER JOIN returns
rows matching on a specified column that exists
in more than one table. Tables usually contain
many more rows, but to further explain what
we mean by INNER JOIN, let’s focus on just
four rows from the employees table and four
rows from the machines table. We’ll also look at just a few columns of each
table for this example. Let’s say we choose employee_id in both tables
to perform an INNER JOIN. Let’s look at the two rows
where there is a match. Both tables have 1188 and 1189 in their respective
employee_id columns, so they are considered a match. The results of the join is
the two rows that have 1188 and 1189 and all columns
from both tables. Before we move on
to the queries, we have to talk about the
NULL values in the tables. In SQL, NULL represents a missing
value due to any reason. In this case, this might be machines that are not
assigned to any employee. Now, let’s bring this into SQL and do an INNER JOIN
on the full tables. Let’s imagine we want to join these tables in order to
get a list of users and their office location
that also shows what operating system they
use on their machines. employee_id is a common
column between these tables, and we can use this to join them. But we won’t need to show
this column in the results. First, let’s start
with a basic query that indicates we want
to select the username, office, and
operating_system columns. We want employees to be
our first or left table, so we’ll use that in our
FROM statement. Now, we write the part of
the query that tells SQL to join the machines table
with the employees table. Let’s break down this query. INNER JOIN tells SQL to
perform the INNER JOIN. Then, we name the second table we want to combine
with the first. This is called the right table. In this case, we want
to join machines with the employees table that was already identified after FROM. Lastly, we tell SQL what
column to base the join on. In our case, we’re using
the employee_id column. Since we’re using two tables, we have to identify the table and follow that with
the column name. So, we have employees.employee_id.
And machines.employee_id. Let’s review the output. Perfect! We have now
joined two tables. The results of our
query displays the records that match on
the employee_id column. Notice that these records contain columns
from both tables, but only the ones we’ve indicated through our
SELECT statement. There are other types
of joins that don’t require a match to
join two tables, and we’re going to discuss those in the next video. I’ll
meet you there!
Video: Types of joins
Outer Joins in SQL
Outer joins are a type of SQL join that returns all records from one or both tables, regardless of whether there is a matching value in the specified columns. There are three types of outer joins:
- LEFT JOIN: Returns all records from the left table, and only matching records from the right table.
- RIGHT JOIN: Returns all records from the right table, and only matching records from the left table.
- FULL OUTER JOIN: Returns all records from both tables, regardless of whether there is a match.
Implementing Outer Joins in SQL
The syntax for implementing outer joins in SQL is the same as for INNER JOINs, but with the following keywords:
- LEFT JOIN:
LEFT JOIN
- RIGHT JOIN:
RIGHT JOIN
- FULL OUTER JOIN:
FULL OUTER JOIN
Benefits of Using Outer Joins
Outer joins can be useful for a variety of reasons, including:
- Returning all records from one or both tables: This can be useful for getting a complete picture of the data, even if there are missing values.
- Identifying records that do not have matching values: This can be useful for identifying potential problems or anomalies in the data.
Conclusion
Outer joins are an important tool for security analysts who use SQL. By understanding the different types of outer joins and how to implement them, security analysts can effectively query their data and gain valuable insights.
SQL Joins: Conquering the Art of Data Combination
In the realm of relational databases, data often resides across multiple tables. Joining, a fundamental SQL operation, enables us to seamlessly combine data from these tables, providing a unified view of the information.
Understanding the Need for Joins
Imagine a scenario where you need to retrieve customer information along with their respective orders. Customer data might be stored in a “customers” table, while order details reside in an “orders” table. To retrieve this combined information, we need to join these tables using the customer ID, which is a common attribute in both tables.
Types of Joins: Each with a Purpose
SQL offers various join types, each serving a specific purpose in data retrieval and manipulation. Let’s explore the most common join types:
INNER JOIN: The most fundamental join type, INNER JOIN returns rows that have matching values in both tables. It ensures that only records with corresponding data are included in the results.
SQL
SELECT customers.name, orders.order_id
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;
LEFT JOIN: Unlike INNER JOIN, LEFT JOIN returns all rows from the left table, regardless of whether there’s a matching value in the right table. This means that even if no matching data exists, the left table’s rows will still be included in the result set.
SQL
SELECT customers.name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
RIGHT JOIN: In contrast to LEFT JOIN, RIGHT JOIN returns all rows from the right table, regardless of whether there’s a matching value in the left table. This ensures that the right table’s rows are always included, even if there’s no corresponding data in the left table.
SQL
SELECT customers.name, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
FULL OUTER JOIN: This join combines the functionality of LEFT and RIGHT JOINs, returning all rows from both tables, regardless of whether there’s a matching value in the other table. It ensures that no rows are excluded, even if there’s no corresponding data in either table.
SQL
SELECT customers.name, orders.order_id
FROM customers
FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;
Mastering Joins: Enhancing Data Analysis
Joins play a crucial role in data analysis, enabling us to combine data from multiple sources, uncover hidden patterns, and gain valuable insights. By understanding the different join types and their applications, we can effectively extract meaningful information from relational databases.
Conclusion: Joins as Pillars of SQL Expertise
Joins stand as fundamental building blocks of SQL proficiency, empowering users to connect and combine data from various tables. By mastering the different join types, we can transform raw data into actionable insights, driving informed decision-making.
What is the difference between an inner join and an outer join?
Inner joins only return rows that match on a specified column, but outer joins also return rows that don’t match on the specified column.
Inner joins only return rows that match on a specified column, but outer joins also return rows that don’t match on the specified column.
Welcome back. I hope you
enjoyed working on inner joins. In the previous video and exercises, we saw how
inner joins can be useful by only returning records that share a value
in specify columns. However, in some
situations, we might need all of the entries from
one or both of our tables. This is where we need
to use outer joins. There are three types of
outer joins: LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. Similar to inner joins, outer joins combine
two tables together; however, they don’t
necessarily need a match between columns
to return a row. Which rows are returned
depends on the type of join. LEFT JOIN returns all of the
records of the first table, but only returns rows of the second table that match
on a specified column. Like we did in the
previous video, let’s examine this type of join by looking at just four rows of two tables with a small
number of columns. Employees is the left
table, or the first table, and machines is the right
table, or the second table. Let’s join on employee_id. There’s a matching value in this column for two
of the four records. When we execute the join, SQL returns these rows
with the matching value, all other rows from the left table, and all
columns from both tables. Records from the
employees table that didn’t match but were returned
through the LEFT JOIN contain NULL values in columns that came
from the machines table. Next, let’s talk
about right joins. RIGHT JOIN returns all of the records of
the second table but only returns rows from the first table that match
on a specified column. With a RIGHT JOIN on
the previous example, the full result returns
matching rows from both, all the rows from the second table, and all
the columns in both tables. For the values that don’t
exist in either table, we are left with a NULL value. Last, we’ll discuss
full outer joins. FULL OUTER JOIN
returns all records from both tables. Using
our same example, a FULL OUTER JOIN returns
all columns from all tables. If a row doesn’t
have a value for a particular column,
it returns NULL. For example, the machines table do not have any rows
with employee_id 1190, so the values
for that row and the columns that came from the
machines table is NULL. To implement left
joins, right joins, and full outer joins
in SQL, you use the same syntax structure
as the INNER JOIN but use these keywords: LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. As a security analyst, you’re not required to know
all of these from memory. Once you understand the
type of join you need, you can quickly search and find all the information you need
to execute these queries. With this information on joins, we’ve now covered some
very important information you’ll need as a security
analyst using SQL. Thank you for joining
me in this video.
Reading: Compare types of joins
Reading
Previously, you explored SQL joins and how to use them to join data from multiple tables when these tables share a common column. You also examined how there are different types of joins, and each of them returns different rows from the tables being joined. In this reading, you’ll review these concepts and more closely analyze the syntax needed for each type of join.
Inner joins
The first type of join that you might perform is an inner join. INNER JOIN returns rows matching on a specified column that exists in more than one table.
It only returns the rows where there is a match, but like other types of joins, it returns all specified columns from all joined tables. For example, if the query joins two tables with SELECT *, all columns in both of the tables are returned.
Note: If a column exists in both of the tables, it is returned twice when SELECT * is used.
The syntax of an inner join
To write a query using INNER JOIN, you can use the following syntax:
SELECT *
FROM employees
INNER JOIN machines ON employees.device_id = machines.device_id;
You must specify the two tables to join by including the first or left table after FROM and the second or right table after INNER JOIN.
After the name of the right table, use the ON keyword and the = operator to indicate the column you are joining the tables on. It’s important that you specify both the table and column names in this portion of the join by placing a period (.) between the table and the column.
In addition to selecting all columns, you can select only certain columns. For example, if you only want the join to return the username, operating_system and device_id columns, you can write this query:
SELECT username, operating_system, employees.device_id
FROM employees
INNER JOIN machines ON employees.device_id = machines.device_id;
Note: In the example query, username and operating_system only appear in one of the two tables, so they are written with just the column name. On the other hand, because device_id appears in both tables, it’s necessary to indicate which one to return by specifying both the table and column name (employees.device_id).
Outer joins
Outer joins expand what is returned from a join. Each type of outer join returns all rows from either one table or both tables.
Left joins
When joining two tables, LEFT JOIN returns all the records of the first table, but only returns rows of the second table that match on a specified column.
The syntax for using LEFT JOIN is demonstrated in the following query:
SELECT *
FROM employees
LEFT JOIN machines ON employees.device_id = machines.device_id;
As with all joins, you should specify the first or left table as the table that comes after FROM and the second or right table as the table that comes after LEFT JOIN. In the example query, because employees is the left table, all of its records are returned. Only records that match on the device_id column are returned from the right table, machines.
Right joins
When joining two tables, RIGHT JOIN returns all of the records of the second table, but only returns rows from the first table that match on a specified column.
The following query demonstrates the syntax for RIGHT JOIN:
SELECT *
FROM employees
RIGHT JOIN machines ON employees.device_id = machines.device_id;
RIGHT JOIN has the same syntax as LEFT JOIN, with the only difference being the keyword RIGHT JOIN instructs SQL to produce different output. The query returns all records from machines, which is the second or right table. Only matching records are returned from employees, which is the first or left table.
Note: You can use LEFT JOIN and RIGHT JOIN and return the exact same results if you use the tables in reverse order. The following RIGHT JOIN query returns the exact same result as the LEFT JOIN query demonstrated in the previous section:
SELECT *
FROM machines
RIGHT JOIN employees ON employees.device_id = machines.device_id;
All that you have to do is switch the order of the tables that appear before and after the keyword used for the join, and you will have swapped the left and right tables.
Full outer joins
FULL OUTER JOIN returns all records from both tables. You can think of it as a way of completely merging two tables.
You can review the syntax for using FULL OUTER JOIN in the following query:
SELECT *
FROM employees
FULL OUTER JOIN machines ON employees.device_id = machines.device_id;
The results of a FULL OUTER JOIN query include all records from both tables. Similar to INNER JOIN, the order of tables does not change the results of the query.
Key takeaways
When working in SQL, there are multiple ways to join tables. All joins return the records that match on a specified column. INNER JOIN will return only these records. Outer joins also return all other records from one or both of the tables. LEFT JOIN returns all records from the first or left table, RIGHT JOIN returns all records from the second or right table, and FULL OUTER JOIN returns all records from both tables.
Practice Quiz: Test your knowledge: SQL joins
Which join types return all rows from only one of the tables being joined? Select all that apply.
RIGHT JOIN, LEFT JOIN
LEFT JOIN and RIGHT JOIN return all rows from only one of the tables being joined. LEFT JOIN returns all the records of the first table, but only returns rows of the second table that match on a specified column. RIGHT JOIN returns all of the records of the second table, but only returns rows from the first table that match on a specified column.
You are performing an INNER JOIN on two tables on the employee_id column. The left table is employees, and the right table is machines. Which of the following queries has the correct INNER JOIN syntax?
SELECT *
FROM employees
INNER JOIN machines ON employees.employee_id = machines.employee_id;
In the following query, which join returns all records from the employees table, but only records that match on employee_id from the machines table?
SELECT *
FROM employees
_____ machines ON employees.employee_id = machines.employee_id;
LEFT JOIN
LEFT JOIN returns all records from the employees table, but only records that match on employee_id from the machines table. Because it is located after FROM, the employees table is the left table.
As a security analyst, you are responsible for performing an INNER JOIN on the invoices and invoice_items tables of the Chinook database. These tables can be connected through the invoiceid column. Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT customerid, trackid
FROM invoices
INNER JOIN invoice_items on invoices.invoiceid = invoice_items.invoiceid;
What is the value in the trackid column of the first row that is returned from this query?
2
2 is the value in the trackid column of the first row returned from this query. By replacing –??? with INNER JOIN invoice_items ON invoices.invoiceid = invoice_items.invoiceid;, you can complete the query and return this result.
Reading: Continuous learning in SQL
Reading
You’ve explored a lot about SQL, including applying filters to SQL queries and joining multiple tables together in a query. There’s still more that you can do with SQL. This reading will explore an example of something new you can add to your SQL toolbox: aggregate functions. You’ll then focus on how you can continue learning about this and other SQL topics on your own.
Aggregate functions
In SQL, aggregate functions are functions that perform a calculation over multiple data points and return the result of the calculation. The actual data is not returned.
There are various aggregate functions that perform different calculations:
- COUNT returns a single number that represents the number of rows returned from your query.
- AVG returns a single number that represents the average of the numerical data in a column.
- SUM returns a single number that represents the sum of the numerical data in a column.
Aggregate function syntax
To use an aggregate function, place the keyword for it after the SELECT keyword, and then in parentheses, indicate the column you want to perform the calculation on.
For example, when working with the customers table, you can use aggregate functions to summarize important information about the table. If you want to find out how many customers there are in total, you can use the COUNT function on any column, and SQL will return the total number of records, excluding NULL values. You can run this query and explore its output:
SELECT COUNT(firstname)
FROM customers;
+------------------+
| COUNT(firstname) |
+------------------+
| 59 |
+------------------+
The result is a table with one column titled COUNT(firstname) and one row that indicates the count.
If you want to find the number of customers from a specific country, you can add a filter to your query:
SELECT COUNT(firstname)
FROM customers
WHERE country = 'USA';
+------------------+
| COUNT(firstname) |
+------------------+
| 13 |
+------------------+
With this filter, the count is lower because it only includes the records where the country column contains a value of ‘USA’.
There are a lot of other aggregate functions in SQL. The syntax of placing them after SELECT is exactly the same as the COUNT function.
Continuing to learn SQL
SQL is a widely used querying language, with many more keywords and applications. You can continue to learn more about aggregate functions and other aspects of using SQL on your own.
Most importantly, approach new tasks with curiosity and a willingness to find new ways to apply SQL to your work as a security analyst. Identify the data results that you need and try to use SQL to obtain these results.
Fortunately, SQL is one of the most important tools for working with databases and analyzing data, so you’ll find a lot of support in trying to learn SQL online. First, try searching for the concepts you’ve already learned and practiced to find resources that have accurate easy-to-follow explanations. When you identify these resources, you can use them to extend your knowledge.
Continuing your practical experience with SQL is also important. You can also search for new databases that allow you to perform SQL queries using what you’ve learned.
Key takeaways
Aggregate functions like COUNT, SUM, and AVG allow you to work with SQL in new ways. There are many other additional aspects of SQL that could be useful to you as an analyst. By continuing to explore SQL on your own, you can expand the ways you can apply SQL in a cybersecurity context.
Review: Databases and SQL
Video: Wrap-up
This section focused on SQL, a tool that will help you on your journey as a security analyst. You learned about the structure of relational databases and how to access them using SQL. You also learned how to write SQL queries to bring up information you might need on the job. Additionally, you learned about SQL filters and how to use them to join multiple tables.
Congratulations! We’ve made it together through the end
of our focus on SQL. You’ve put in a lot of work and learned an
important tool that will help you on your journey
as a security analyst. Let’s take a moment
to go through all of the topics you
learned in this section. We started by learning about the structure of
relational databases and how we can access them by using the query language SQL. We then got hands-on practice with writing our
own SQL queries. We used SQL to bring up
information you might need on the job when
working as an analyst. We then focused on SQL filters. We started with simple
conditions with strings, and by the end, we
learned how to use multiple filters in one query. We concluded the unit with SQL joins and learned how to
join multiple tables, giving us even more
information at once. By completing this course, you just took a very big step in your future career as
a security analyst. You have been introduced to a powerful tool that can
help you in your work. Whenever you need to, I encourage you to revisit
the materials in this course. Learning a querying language
like SQL takes time. Thank you again for joining
me in this journey. I hope you’ll enjoy using
SQL as much as I do.
Reading: Reference guide: SQL
Reading
The SQL reference guide contains keywords for SQL queries. Security analysts can use these keywords to query databases and find data to support security-related decisions. The reference guide is divided into four different categories of SQL keywords for security-related tasks:
- Query a database
- Apply filters to SQL queries
- Join tables
- Perform calculations
Within each category, commands are organized alphabetically.
Access and save the guide
You can save a copy of this guide for future reference. You can use it as a resource for additional practice or in your future professional projects.
To access a downloadable version of this course item, click the following link and select Use Template.
Reading: Glossary terms from module 4
Terms and definitions from Course 4, Module 4
Database: An organized collection of information or data
Date and time data: Data representing a date and/or time
Exclusive operator: An operator that does not include the value of comparison
Filtering: Selecting data that match a certain condition
Foreign key: A column in a table that is a primary key in another table
Inclusive operator: An operator that includes the value of comparison
Log: A record of events that occur within an organization’s systems
Numeric data: Data consisting of numbers
Operator: A symbol or keyword that represents an operation
Primary key: A column where every row has a unique entry
Query: A request for data from a database table or a combination of tables
Relational database: A structured database containing tables that are related to each other
String data: Data consisting of an ordered sequence of characters
SQL (Structured Query Language): A programming language used to create, interact with, and request information from a database
Syntax: The rules that determine what is correctly structured in a computing language
Wildcard: A special character that can be substituted with any other character
Quiz: Module 4 challenge
Why might a security analyst use SQL?
To efficiently find needed data in security logs
What is true about the values in the primary key column? Select all that apply.
- Each row must have a unique value.
- They cannot be null (or empty).
Which of these SQL statements queries the machines table? Select all that apply.
SELECT *
FROM machines;
SELECT device_id, operating_system
FROM machines
WHERE operating_system = 'OS 2';
Both an employees table and a machines table contain an employee_id column, and you want to return only the records that share a value in this column. Which keyword should be part of your query?
INNER JOIN
What does WHERE department = ‘Sales’ indicate in the following SQL query?
SELECT *
FROM employees
WHERE department = 'Sales';
To only return rows that match the filter
You work with a table that has one column for name. Some of these names have prefixes. You want to identify all of the doctors. Which query will return every name that starts with the prefix ‘Dr.’?
WHERE name LIKE ‘Dr.%’;
You need to perform a SQL join. You want to return all the columns with records matching on the device_id column between the employees and machines tables. You also want to return all records from the employees table. Which of the following queries would you use?
SELECT *
FROM employees
LEFT JOIN machines ON employees.device_id = machines.device_id;
You are working with the Chinook database. You want to return the lastname and title columns from the employees table. Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT lastname, title
FROM employees;
What is the title of the employee with the last name of Callahan?
IT Staff
You are working with the Chinook database and are responsible for filtering for the employees with a birthdate that is on or after ‘1973-01-01’ (January 1, 1973). Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT firstname,lastname, birthdate
FROM employees
WHERE birthdate >= '1971-01-01';
How many employees were born on or after January 1, 1973?
2
You are working with the Chinook database and are responsible for filtering for the customers that have a value of ‘USA’ in the country column and have a value of ‘Frank’ in the firstname column. Replace –??? with the missing information to complete the query. (If you want to undo your changes to the query, you can click the Reset button.)
SELECT firstname, lastname, country
FROM customers
WHERE country = 'USA' AND firstname = 'Frank';
How many customers live in the USA and have the name Frank?
2
Congratulations on completing Course 4!
Video: Course wrap-up
This course provided an introduction to computing basics for aspiring security analysts. It covered operating systems, Linux operating system, and SQL.
You made it to the
end of this course! Congratulations—you did it! I hope you are proud
of all you learned. The focus of this course
was computing basics. Understanding the basics of computing is a valuable skill as you transition into your
career as a security analyst. Let’s recap what you
learned in this course. We first focused on
operating systems and how they relate to
applications and hardware. Understanding how the
system you’re protecting works is essential for
doing your job effectively. That brings us to the
Linux operating system. When working in the
security profession, familiarity with
Linux is important. We first discussed
this architecture and various distributions. Then, we used a Linux
command line to carry out tasks you might encounter
as a security analyst. Finally, we looked at another useful tool and used
SQL to query databases. After this course, I hope you have a better
understanding of how these foundations
of computing support a security analyst
in their daily work. I also hope you continue
your path with this program. There are a lot of other useful and exciting topics ahead. Once again, congratulations. You’ve finished another course. Building skills is something
you should be proud of. Keep it up as you progress
through this program.
Reading: Course 4 glossary
Reading
Reading: Get started on the next course
Reading
Congratulations on completing Course 4 of the Google Cybersecurity Certificate: Tools of the Trade: Linux and SQL! In this part of the program, you learned about computing skills that will support your work as an analyst. First, you learned about operating systems. Then, you communicated with the Linux operating system through the Bash shell in order to complete security-related tasks. Finally, you performed SQL queries that returned information to support security-related decisions.
The Google Cybersecurity Certificate has eight courses:
- Foundations of Cybersecurity — Explore the cybersecurity profession, including significant events that led to the development of the cybersecurity field and its continued importance to organizational operations. Learn about entry-level cybersecurity roles and responsibilities.
- Play It Safe: Manage Security Risks — Identify how cybersecurity professionals use frameworks and controls to protect business operations, and explore common cybersecurity tools.
- Connect and Protect: Networks and Network Security — Gain an understanding of network-level vulnerabilities and how to secure networks.
- Tools of the Trade: Linux and SQL — Explore foundational computing skills, including communicating with the Linux operating system through the command line and querying databases with SQL. (This is the course you just completed. Well done!)
- Assets, Threats, and Vulnerabilities — Learn about the importance of security controls and developing a threat actor mindset to protect and defend an organization’s assets from various threats, risks, and vulnerabilities.
- Sound the Alarm: Detection and Response — Understand the incident response lifecycle and practice using tools to detect and respond to cybersecurity incidents.
- Automate Cybersecurity Tasks with Python — Explore the Python programming language and write code to automate cybersecurity tasks.
- Put It to Work: Prepare for Cybersecurity Jobs — Learn about incident classification, escalation, and ways to communicate with stakeholders. This course closes out the program with tips on how to engage with the cybersecurity community and prepare for your job search.
Now that you have completed this course, you’re ready to move on to the next course: Assets, Threats, and Vulnerabilities