# SQL and Databases

# Welcome to week 4; SQL and Databases

<div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-in-the-world-of-secu"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from In the world of security, diversity is important." class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In the world of security, diversity is important. </span></div><div aria-label="toggle video from Diverse perspectives are often" class="rc-Phrase css-ugczj4" data-cue="2" data-cue-index="1" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Diverse perspectives are often </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">needed to find effective solutions. </span></div><div aria-label="toggle video from This is also true of the tools we use." class="rc-Phrase css-ugczj4" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">This is also true of the tools we use. </span></div><div aria-label="toggle video from Your job will often require" class="rc-Phrase css-ugczj4" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Your job will often require </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">you to use a lot of diverse tools. </span></div><div aria-label="toggle video from In the last section," class="rc-Phrase css-ugczj4" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In the last section, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we studied the Linux command line and learned how </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">this tool can help you search and filter through data, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">navigate through the Linux file system, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and authenticate users. </span></div><div aria-label="toggle video from Now, we'll learn about another tool." class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Now, we'll learn about another tool. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-in-this-section%2C-we%27"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from In this section, we'll explore SQL and how it allows" class="rc-Phrase css-ugczj4" data-cue="13" data-cue-index="12" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In this section, we'll explore SQL and how it allows </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">you to analyze data in a way </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">needed for your role as a security analyst. </span></div><div aria-label="toggle video from We're going to start off by learning about" class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We're going to start off by learning about </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">relational databases and how they're structured. </span></div><div aria-label="toggle video from From there, we're going to introduce" class="rc-Phrase css-ugczj4" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">From there, we're going to introduce </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">SQL queries and how to use </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">them to access data from databases. </span></div><div aria-label="toggle video from We then move on to SQL filters," class="rc-Phrase css-ugczj4" data-cue="21" data-cue-index="20" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We then move on to SQL filters, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">which help us refine our queries </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">to get the exact information we need. </span></div><div aria-label="toggle video from Lastly, we'll explore SQL joins," class="rc-Phrase css-ugczj4" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Lastly, we'll explore SQL joins, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">which allow you to combine tables together. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--1"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-when-i%27m-presented-w"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from When I'm presented with a problem or a project at work," class="rc-Phrase css-ugczj4" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">When I'm presented with a problem or a project at work, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I often have to sift through a large amount of data. </span></div><div aria-label="toggle video from When I use SQL," class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">When I use SQL, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I'm able to review data quickly and provide results with </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">confidence since the queries are </span></div><div aria-label="toggle video from consistent and easily executed." class="rc-Phrase css-ugczj4" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">consistent and easily executed. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--2"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-sql-is-a-very-powerf"><div aria-label="toggle video from SQL is a very powerful and flexible tool." class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">SQL is a very powerful and flexible tool. </span></div><div aria-label="toggle video from Throughout this section, you'll" class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Throughout this section, you'll </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">learn how to use the parts of it you </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">need as a security analyst and gain hands-on experience. </span></div><div aria-label="toggle video from Good luck, and I'll join you for the rest of the course!" class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Good luck, and I'll join you for the rest of the course! </span></div><div aria-label="toggle video from Good luck, and I'll join you for the rest of the course!" class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0">---

</div></div>

# New Page



# Introduction to databases

<div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-our-modern-world-is-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Our modern world is filled with data and" class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Our modern world is filled with data and </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">that data almost always guides us in making important decisions. </span></div><div aria-label="toggle video from When working with large amounts of data, we need to know how to store it, so" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">When working with large amounts of data, we need to know how to store it, so </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">it's organized and quick to access and process. </span></div><div aria-label="toggle video from The solution to this is through databases, and" class="rc-Phrase css-ugczj4" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The solution to this is through databases, and </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">that's what we're exploring in this video! </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-to-start-us-off%2C%C2%A0we-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from To start us off," class="rc-Phrase css-ugczj4" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">To start us off, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we can define a database as an organized collection of information or data. </span></div><div aria-label="toggle video from Databases are often compared to spreadsheets." class="rc-Phrase css-ugczj4" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Databases are often compared to spreadsheets. </span></div><div aria-label="toggle video from Some of you may have used Google Sheets or" class="rc-Phrase css-ugczj4" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Some of you may have used Google Sheets or </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">another common spreadsheet program in the past. </span></div><div aria-label="toggle video from While these programs are convenient ways to store data, spreadsheets" class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">While these programs are convenient ways to store data, spreadsheets </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">are often designed for a single user or a small team to store less data. </span></div><div aria-label="toggle video from In contrast, databases can be accessed by multiple people simultaneously and" class="rc-Phrase css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In contrast, databases can be accessed by multiple people simultaneously and </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">can store massive amounts of data. </span></div><div aria-label="toggle video from Databases can also perform complex tasks while accessing data." class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Databases can also perform complex tasks while accessing data. </span></div><div aria-label="toggle video from As a security analyst," class="rc-Phrase css-ugczj4" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">As a security analyst, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">you'll often need to access databases containing useful information. </span></div><div aria-label="toggle video from current lecture segment: For example, these could be databases containing information on login attempts," class="rc-Phrase active css-ugczj4" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">For example, these could be databases containing information on login attempts, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">software and updates, or machines and their owners. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--1"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-now-that-we-know-how"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Now that we know how important databases are for us," class="rc-Phrase css-ugczj4" data-cue="21" data-cue-index="20" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Now that we know how important databases are for us, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">let's talk about how they're organized and how we can interact with them. </span></div><div aria-label="toggle video from Using databases allow us to store large amounts of data while keeping it quick and" class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Using databases allow us to store large amounts of data while keeping it quick and </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">easy to access. </span></div><div aria-label="toggle video from There are lots of different ways we can structure a database, but in this course," class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">There are lots of different ways we can structure a database, but in this course, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we'll be working with relational databases. </span></div><div aria-label="toggle video from A relational database is a structured database containing tables that" class="rc-Phrase css-ugczj4" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">A relational database is a structured database containing tables that </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">are related to each other. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--2"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-let%27s-learn-more-abo"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's learn more about what makes a relational database." class="rc-Phrase css-ugczj4" data-cue="29" data-cue-index="28" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Let's learn more about what makes a relational database. </span></div><div aria-label="toggle video from We'll start by examining an individual table in" class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We'll start by examining an individual table in </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">a larger database of organizational information. </span></div><div aria-label="toggle video from Each table contains fields of information." class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Each table contains fields of information. </span></div><div aria-label="toggle video from For example, in this table on employees," class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">For example, in this table on employees, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">these would include fields like employee\_id, device\_id, and username. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--3"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-these-are-the-column"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from These are the columns of the tables." class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">These are the columns of the tables. </span></div><div aria-label="toggle video from In addition, tables contain rows also called records." class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In addition, tables contain rows also called records. </span></div><div aria-label="toggle video from Rows are filled with specific data related to the columns in the table." class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Rows are filled with specific data related to the columns in the table. </span></div><div aria-label="toggle video from For example, our first row is a record for an employee whose id is 1,000 and" class="rc-Phrase css-ugczj4" data-cue="38" data-cue-index="37" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">For example, our first row is a record for an employee whose id is 1,000 and </span></div><div aria-label="toggle video from who works in the marketing department." class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">who works in the marketing department. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--4"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-relational-databases"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Relational databases often have multiple tables." class="rc-Phrase css-ugczj4" data-cue="40" data-cue-index="39" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Relational databases often have multiple tables. </span></div><div aria-label="toggle video from Consider an example where we have two tables from a larger database, one with" class="rc-Phrase css-ugczj4" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Consider an example where we have two tables from a larger database, one with </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">employees of the company and another with machines given to those employees. </span></div><div aria-label="toggle video from We can connect two tables if they share a common column." class="rc-Phrase css-ugczj4" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We can connect two tables if they share a common column. </span></div><div aria-label="toggle video from In this example," class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In this example, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we establish a relationship between them with a common employee\_id column. </span></div><div aria-label="toggle video from The columns that relate two tables to each other are called keys." class="rc-Phrase css-ugczj4" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The columns that relate two tables to each other are called keys. </span></div><div aria-label="toggle video from There are two types of keys." class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">There are two types of keys. </span></div><div aria-label="toggle video from There are two types of keys." class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The first is called a primary key. </span></div><div aria-label="toggle video from The primary key refers to a column where every row has a unique entry." class="rc-Phrase css-ugczj4" data-cue="49" data-cue-index="48" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The primary key refers to a column where every row has a unique entry. </span></div><div aria-label="toggle video from The primary key must not have any duplicate values, or any null or" class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The primary key must not have any duplicate values, or any null or </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">empty values. </span></div><div aria-label="toggle video from The primary key allows us to uniquely identify every row in our table." class="rc-Phrase css-ugczj4" data-cue="52" data-cue-index="51" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The primary key allows us to uniquely identify every row in our table. </span></div><div aria-label="toggle video from For the table of employees, employee_id is a primary key." class="rc-Phrase css-ugczj4" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">For the table of employees, employee\_id is a primary key. </span></div><div aria-label="toggle video from Every employee_id is unique and there are no employee_ids that are duplicate or" class="rc-Phrase css-ugczj4" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Every employee\_id is unique and there are no employee\_ids that are duplicate or </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">empty. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--5"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-the-second-type-of-k"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from The second type of key is a foreign key." class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The second type of key is a foreign key. </span></div><div aria-label="toggle video from The foreign key is a column in a table that is a primary key in another table." class="rc-Phrase css-ugczj4" data-cue="57" data-cue-index="56" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The foreign key is a column in a table that is a primary key in another table. </span></div><div aria-label="toggle video from Foreign keys, unlike primary keys, can have empty values and duplicates." class="rc-Phrase css-ugczj4" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Foreign keys, unlike primary keys, can have empty values and duplicates. </span></div><div aria-label="toggle video from The foreign key allows us to connect two tables together." class="rc-Phrase css-ugczj4" data-cue="59" data-cue-index="58" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The foreign key allows us to connect two tables together. </span></div><div aria-label="toggle video from In our example, we can look at the employee_id column in the machines table." class="rc-Phrase css-ugczj4" data-cue="60" data-cue-index="59" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In our example, we can look at the employee\_id column in the machines table. </span></div><div aria-label="toggle video from We previously identified this as a primary key in the employees table, so" class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We previously identified this as a primary key in the employees table, so </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we can use this to connect every machine to their corresponding employee. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--6"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-it%27s-also-important-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from It's also important to know that a table can only have one primary key, but" class="rc-Phrase css-ugczj4" data-cue="63" data-cue-index="62" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">It's also important to know that a table can only have one primary key, but </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">multiple foreign keys. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--7"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-with-this-informatio"><div aria-label="toggle video from With this information, we're ready to move on to the basics of SQL," class="rc-Phrase css-ugczj4" data-cue="65" data-cue-index="64" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">With this information, we're ready to move on to the basics of SQL, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">the language that lets us work with databases. </span></div><div aria-label="toggle video from Throughout this section," class="rc-Phrase css-ugczj4" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Throughout this section, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we'll gain hands-on experience working with the concepts we just covered! </span></div></div>

# SQL filtering versus Linux filtering

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 <var>sqlite3</var> 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.

# Adedayo: SQL in cybersecurity

<div class="phrases" id="bkmrk-my-name-is-adedayo%2C%C2%A0"><div aria-label="toggle video from My name is Adedayo," class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">My name is Adedayo, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and I'm a Security Engineer at Google. </span></div><div aria-label="toggle video from A lot of people think you need to have a degree in" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">A lot of people think you need to have a degree in </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">computer science, right to be able to get </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">into cybersecurity. I don't think that's true. </span></div><div aria-label="toggle video from Take me for an example," class="rc-Phrase css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Take me for an example, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I started learning IT from Lagos, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Nigeria where I was born and raised, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and then I'm all the way here now </span></div><div aria-label="toggle video from in Silicon Valley, working for Google." class="rc-Phrase css-ugczj4" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">in Silicon Valley, working for Google. </span></div><div aria-label="toggle video from I think that's just amazing and a dream come true." class="rc-Phrase css-ugczj4" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I think that's just amazing and a dream come true. </span></div><div aria-label="toggle video from You taking this certificate is a first step to you" class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">You taking this certificate is a first step to you </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">making a commitment to switching </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">your career to cybersecurity. Kudos to you on that. </span></div><div aria-label="toggle video from SQL is one of" class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">SQL is one of </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">the skillset you need to have in your toolbox as a </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">cybersecurity professional because you can </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">very quickly make decisions, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">not just off the bat, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">but make decisions with data backing you, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and be able to communicate with your team, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">with stakeholders about why you made </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">a decision because it's one thing to be able to say, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we need to do this, it's </span></div><div aria-label="toggle video from another thing to say we need to do this" class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">another thing to say we need to do this </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and here's the data that I wrote my SQL statements about. </span></div><div aria-label="toggle video from I learned SQL by, first," class="rc-Phrase css-ugczj4" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I learned SQL by, first, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">as a coursework in school, that was really great, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">but I think I forgot everything about that after school. </span></div><div aria-label="toggle video from The next step that I took was taking online courses," class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The next step that I took was taking online courses, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">such as the one you're taking right now to learn </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">SQL and the fundamentals </span></div><div aria-label="toggle video from about it and how to really use it." class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">about it and how to really use it. </span></div><div aria-label="toggle video from Then the first time I used SQL practically was at Google." class="rc-Phrase css-ugczj4" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Then the first time I used SQL practically was at Google. </span></div><div aria-label="toggle video from You really need to practice." class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">You really need to practice. </span></div><div aria-label="toggle video from I think with anything else, practice makes perfect." class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I think with anything else, practice makes perfect. </span></div><div aria-label="toggle video from Being able to, even if it's just a few hours a week," class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Being able to, even if it's just a few hours a week, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">put aside time to practice writing SQL statement. </span></div><div aria-label="toggle video from Having that skill is something that will" class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Having that skill is something that will </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">be very applicable to your first job, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">and you can use that to make data-driven decisions. </span></div><div aria-label="toggle video from I feel very fulfilled working in cybersecurity." class="rc-Phrase css-ugczj4" data-cue="42" data-cue-index="41" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I feel very fulfilled working in cybersecurity. </span></div><div aria-label="toggle video from I feel very energized," class="rc-Phrase css-ugczj4" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">I feel very energized, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">come into work every day. </span></div><div aria-label="toggle video from Not only because I get to work on" class="rc-Phrase css-ugczj4" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Not only because I get to work on </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">really complex problems and </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">try to figure out solutions for them, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">but I also have great teammates that we </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">all come together and tackle the problem. </span></div><div aria-label="toggle video from Being able to go to bed at night," class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Being able to go to bed at night, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">knowing that the work that I do is for the better of </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Google users and Google employees </span></div><div aria-label="toggle video from is a very rewarding feeling for me." class="rc-Phrase css-ugczj4" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">is a very rewarding feeling for me. </span></div></div>

# Basic queries

<div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-in-this-video%2C-we%27re"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from In this video, we're going to be" class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In this video, we're going to be </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">running our very first SQL query! </span></div><div aria-label="toggle video from This query will be based on" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">This query will be based on </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">a common work task that you might </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">encounter as a security analyst. </span></div><div aria-label="toggle video from We're going to determine" class="rc-Phrase css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We're going to determine </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">which computer has been assigned to a certain employee. </span></div><div aria-label="toggle video from Let's say we have access to the employees table." class="rc-Phrase css-ugczj4" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Let's say we have access to the employees table. </span></div><div aria-label="toggle video from The employees table has five columns." class="rc-Phrase css-ugczj4" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The employees table has five columns. </span></div><div aria-label="toggle video from Two of them, employee_id and device_id," class="rc-Phrase css-ugczj4" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Two of them, employee\_id and device\_id, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">contain the information that we need. </span></div><div aria-label="toggle video from We'll write a query to this table that" class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We'll write a query to this table that </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">returns only those two columns from the table. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-the-two-sql-keywords"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from The two SQL keywords we need for" class="rc-Phrase css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The two SQL keywords we need for </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">basic SQL queries are SELECT and FROM. </span></div><div aria-label="toggle video from SELECT indicates which columns to return." class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">SELECT indicates which columns to return. </span></div><div aria-label="toggle video from FROM indicates which table to query." class="rc-Phrase css-ugczj4" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">FROM indicates which table to query. </span></div><div aria-label="toggle video from The use of these keywords in SQL is very similar" class="rc-Phrase css-ugczj4" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The use of these keywords in SQL is very similar </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">to how we would use these words in everyday language. </span></div><div aria-label="toggle video from For example, we can ask a friend to select" class="rc-Phrase css-ugczj4" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">For example, we can ask a friend to select </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">apples and bananas from </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">the big box when going out to buy fruit. </span></div><div aria-label="toggle video from This is already very similar to SQL." class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">This is already very similar to SQL. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--1"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-so-let%27s-go-ahead-an"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from So let's go ahead and use SELECT and FROM in SQL to" class="rc-Phrase css-ugczj4" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">So let's go ahead and use SELECT and FROM in SQL to </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">return the information we need on </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">employees and the computers they use. </span></div><div aria-label="toggle video from We start off by typing in the SQL statement." class="rc-Phrase css-ugczj4" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We start off by typing in the SQL statement. </span></div><div aria-label="toggle video from After FROM, we've identified" class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">After FROM, we've identified </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">that the information will be </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">pulled from the employees table. </span></div><div aria-label="toggle video from And after SELECT, employee_id and device_id" class="rc-Phrase css-ugczj4" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">And after SELECT, employee\_id and device\_id </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">indicate the two columns we </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">want to return from this table. </span></div><div aria-label="toggle video from Notice how a comma separates" class="rc-Phrase css-ugczj4" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Notice how a comma separates </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">the two columns that we want to return. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--2"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-it%27s-also-worth-ment"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from It's also worth mentioning a couple of" class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">It's also worth mentioning a couple of </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">key aspects related to the syntax of SQL here. </span></div><div aria-label="toggle video from Syntax refers to the rules that determine" class="rc-Phrase css-ugczj4" data-cue="38" data-cue-index="37" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Syntax refers to the rules that determine </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">what is correctly structured in a computing language. </span></div><div aria-label="toggle video from In SQL, keywords are not case-sensitive, so" class="rc-Phrase css-ugczj4" data-cue="40" data-cue-index="39" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In SQL, keywords are not case-sensitive, so </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">you could also write select and from in lowercase, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">but we're placing them in capital letters because </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">it makes the query easier to understand. </span></div><div aria-label="toggle video from Another aspect of this syntax" class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Another aspect of this syntax </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">is that semicolons are </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">placed at the end of the statement. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--3"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-and-now%2C-we%27ll-run-t"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from And now, we'll run the query by pressing Enter." class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">And now, we'll run the query by pressing Enter. </span></div><div aria-label="toggle video from The output gives us the information we" class="rc-Phrase css-ugczj4" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">The output gives us the information we </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">need to match employees to their computers. </span></div><div aria-label="toggle video from We just ran our very first SQL query!" class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We just ran our very first SQL query! </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--4"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk-suppose-you-wanted-t"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Suppose you wanted to know" class="rc-Phrase css-ugczj4" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Suppose you wanted to know </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">what department the employee using </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">the computer is from, or their </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">username, or the office they work in. </span></div><div aria-label="toggle video from To do that, we can use SQL to make" class="rc-Phrase css-ugczj4" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">To do that, we can use SQL to make </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">another statement that prints out </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">all of the columns from the table. </span></div><div aria-label="toggle video from We can do this by placing an asterisk after SELECT." class="rc-Phrase css-ugczj4" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">We can do this by placing an asterisk after SELECT. </span></div><div aria-label="toggle video from This is commonly referred to as select all." class="rc-Phrase css-ugczj4" data-cue="59" data-cue-index="58" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">This is commonly referred to as select all. </span></div><div aria-label="toggle video from Now, let's run this query to the employees table in SQL." class="rc-Phrase css-ugczj4" data-cue="60" data-cue-index="59" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">Now, let's run this query to the employees table in SQL. </span></div><div aria-label="toggle video from And now we have the full table in the output." class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">And now we have the full table in the output. </span></div></div></div></div><div class="cds-185 css-xl5mb3 cds-186" id="bkmrk--5"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item">  
</div></div><div class="cds-185 css-arowdh cds-187 cds-grid-item cds-232 cds-257" id="bkmrk-you-just-made-it-thr"><div><div class="rc-TranscriptHighlighter css-79elbk"><div class="rc-Transcript css-9li235" data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="interactive_transcript" data-track-page="item_layout" role="presentation"><div class="cds-185 css-xl5mb3 cds-186"><div class="cds-185 rc-Paragraph css-1lz62pp cds-187 cds-grid-item"><div class="phrases"><div aria-label="toggle video from You just made it through a basic query" class="rc-Phrase css-ugczj4" data-cue="62" data-cue-index="61" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">You just made it through a basic query </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">in SQL, congratulations! </span></div><div aria-label="toggle video from In the next video," class="rc-Phrase css-ugczj4" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">In the next video, </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">we'll learn how to add filters to </span><span aria-hidden="true" class="cds-321 css-80vnnb cds-323">our queries, so I'll meet you there! </span></div></div></div></div></div></div></div></div><div class="cds-185 css-hcqebr cds-187 cds-grid-item cds-232 cds-250" id="bkmrk--6"><div class="cds-819 cds-formControl-root css-4p0j7c cds-822"><div class="css-1whdyhf" data-testid="visually-hidden">  
</div><label class="cds-823 cds-formLabel-root cds-formLabel-onLight css-wamxw1 cds-828 cds-829" for="cds-react-aria-482" id="bkmrk--7"></label></div></div>

# Query a database

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 <var>Chinook</var> database, which this course uses for queries in readings and quizzes.

## Basic SQL query

There are two essential keywords in any SQL query: <var>SELECT</var> and <var>FROM</var>. 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:

<var>SELECT employee\_id, device\_id</var>

<var>FROM employees;</var>

In readings and quizzes, this course uses a sample database called the <var>Chinook</var> database to run queries. The <var>Chinook</var> 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 <var>employees</var> table, a <var>customers</var> table, and an <var>invoices</var> table. These tables include data such as names and addresses.

As an example, you can run this query to return data from the <var>customers</var> table of the <var>Chinook</var> database:

SELECT customerid, city, country  
FROM customers;

```
+------------+---------------------+----------------+
| CustomerId | City                | Country        |
+------------+---------------------+----------------+
|          1 | São José dos Campos | Brazil         |
|          2 | Stuttgart           | Germany        |
|          3 | Montréal            | Canada         |
|          4 | Oslo                | Norway         |
|          5 | Prague              | Czech Republic |
|          6 | Prague              | Czech Republic |
|          7 | Vienne              | Austria        |
|          8 | Brussels            | Belgium        |
|          9 | Copenhagen          | Denmark        |
|         10 | São Paulo           | Brazil         |
|         11 | São Paulo           | Brazil         |
|         12 | Rio de Janeiro      | Brazil         |
|         13 | Brasília            | Brazil         |
|         14 | Edmonton            | Canada         |
|         15 | Vancouver           | Canada         |
|         16 | Mountain View       | USA            |
|         17 | Redmond             | USA            |
|         18 | New York            | USA            |
|         19 | Cupertino           | USA            |
|         20 | Mountain View       | USA            |
|         21 | Reno                | USA            |
|         22 | Orlando             | USA            |
|         23 | Boston              | USA            |
|         24 | Chicago             | USA            |
|         25 | Madison             | USA            |
+------------+---------------------+----------------+
(Output limit exceeded, 25 of 59 total rows shown)
```

<div class="monaco-scrollable-element editor-scrollable vs" data-mprt="5" id="bkmrk--2" role="presentation" style="position: absolute; overflow: hidden; left: 83px; width: 705px; height: 114px;"><div aria-hidden="true" class="invisible scrollbar vertical" role="presentation" style="position: absolute; width: 14px; height: 114px; right: 0px; top: 0px;">  
</div></div><div aria-hidden="true" class="minimap slider-mouseover" data-mprt="8" id="bkmrk--5" role="presentation" style="position: absolute; left: 0px; width: 0px; height: 114px;"></div><div class="cml-code-evaluator" id="bkmrk--7"></div>The <var>SELECT</var> keyword indicates which columns to return. For example, you can return the <var>customerid</var> column from the <var>Chinook</var> database with

<var>SELECT customerid</var>

You can also select multiple columns by separating them with a comma. For example, if you want to return both the <var>customerid</var> and <var>city</var> columns, you should write <var>SELECT customerid, city</var>.

If you want to return all columns in a table, you can follow the <var>SELECT</var> keyword with an asterisk (<var>\*</var>). The first line in the query will be <var>SELECT \*</var>.

**Note:** Although the tables you're querying in this course are relatively small, using <var>SELECT \*</var> 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 <var>SELECT</var> keyword always comes with the <var>FROM</var> keyword. <var>FROM</var> indicates which table to query. To use the <var>FROM</var> keyword, you should write it after the <var>SELECT</var> 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 <var>customers</var> table, you can write:

<var>SELECT \*</var>

<var>FROM customers;</var>

When you want to end the query here, you put a semicolon (<var>;</var>) 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

<var>SELECT \* FROM customers;  
and here would be the databases answer for  
SELECT \* FROM customers ORDER BY country, city;  
</var>

```
+------------+-----------+-------------+--------------------------------------------------+--------------------------------------+---------------------+-------+----------------+------------+---------------------+--------------------+-------------------------------+--------------+
| CustomerId | FirstName | LastName    |                                          Company | Address                              | City                | State | Country        | PostalCode | Phone               |                Fax | Email                         | SupportRepId |
+------------+-----------+-------------+--------------------------------------------------+--------------------------------------+---------------------+-------+----------------+------------+---------------------+--------------------+-------------------------------+--------------+
|         56 | Diego     | Gutiérrez   |                                             None | 307 Macacha Güemes                   | Buenos Aires        |  None | Argentina      | 1106       | +54 (0)11 4311 4333 |               None | diego.gutierrez@yahoo.ar      |            4 |
|         55 | Mark      | Taylor      |                                             None | 421 Bourke Street                    | Sidney              |   NSW | Australia      | 2010       | +61 (02) 9332 3633  |               None | mark.taylor@yahoo.au          |            4 |
|          7 | Astrid    | Gruber      |                                             None | Rotenturmstraße 4, 1010 Innere Stadt | Vienne              |  None | Austria        | 1010       | +43 01 5134505      |               None | astrid.gruber@apple.at        |            5 |
|          8 | Daan      | Peeters     |                                             None | Grétrystraat 63                      | Brussels            |  None | Belgium        | 1000       | +32 02 219 03 03    |               None | daan_peeters@apple.be         |            4 |
|         13 | Fernanda  | Ramos       |                                             None | Qe 7 Bloco G                         | Brasília            |    DF | Brazil         | 71020-677  | +55 (61) 3363-5547  | +55 (61) 3363-7855 | fernadaramos4@uol.com.br      |            4 |
|         12 | Roberto   | Almeida     |                                           Riotur | Praça Pio X, 119                     | Rio de Janeiro      |    RJ | Brazil         | 20040-020  | +55 (21) 2271-7000  | +55 (21) 2271-7070 | roberto.almeida@riotur.gov.br |            3 |
|          1 | Luís      | Gonçalves   | Embraer - Empresa Brasileira de Aeronáutica S.A. | Av. Brigadeiro Faria Lima, 2170      | São José dos Campos |    SP | Brazil         | 12227-000  | +55 (12) 3923-5555  | +55 (12) 3923-5566 | luisg@embraer.com.br          |            3 |
|         10 | Eduardo   | Martins     |                                 Woodstock Discos | Rua Dr. Falcão Filho, 155            | São Paulo           |    SP | Brazil         | 01007-010  | +55 (11) 3033-5446  | +55 (11) 3033-4564 | eduardo@woodstock.com.br      |            4 |
|         11 | Alexandre | Rocha       |                             Banco do Brasil S.A. | Av. Paulista, 2022                   | São Paulo           |    SP | Brazil         | 01310-200  | +55 (11) 3055-3278  | +55 (11) 3055-8131 | alero@uol.com.br              |            5 |
|         14 | Mark      | Philips     |                                            Telus | 8210 111 ST NW                       | Edmonton            |    AB | Canada         | T6G 2C7    | +1 (780) 434-4554   |  +1 (780) 434-5565 | mphilips12@shaw.ca            |            5 |
|         31 | Martha    | Silk        |                                             None | 194A Chain Lake Drive                | Halifax             |    NS | Canada         | B3S 1C5    | +1 (902) 450-0450   |               None | marthasilk@gmail.com          |            5 |
|          3 | François  | Tremblay    |                                             None | 1498 rue Bélanger                    | Montréal            |    QC | Canada         | H2G 1A7    | +1 (514) 721-4711   |               None | ftremblay@gmail.com           |            3 |
|         30 | Edward    | Francis     |                                             None | 230 Elgin Street                     | Ottawa              |    ON | Canada         | K2P 1L7    | +1 (613) 234-3322   |               None | edfrancis@yachoo.ca           |            3 |
|         29 | Robert    | Brown       |                                             None | 796 Dundas Street West               | Toronto             |    ON | Canada         | M6J 1V1    | +1 (416) 363-8888   |               None | robbrown@shaw.ca              |            3 |
|         15 | Jennifer  | Peterson    |                                    Rogers Canada | 700 W Pender Street                  | Vancouver           |    BC | Canada         | V6C 1G8    | +1 (604) 688-2255   |  +1 (604) 688-8756 | jenniferp@rogers.ca           |            3 |
|         32 | Aaron     | Mitchell    |                                             None | 696 Osborne Street                   | Winnipeg            |    MB | Canada         | R3L 2B9    | +1 (204) 452-6452   |               None | aaronmitchell@yahoo.ca        |            4 |
|         33 | Ellie     | Sullivan    |                                             None | 5112 48 Street                       | Yellowknife         |    NT | Canada         | X1A 1N6    | +1 (867) 920-2233   |               None | ellie.sullivan@shaw.ca        |            3 |
|         57 | Luis      | Rojas       |                                             None | Calle Lira, 198                      | Santiago            |  None | Chile          | None       | +56 (0)2 635 4444   |               None | luisrojas@yahoo.cl            |            5 |
|          5 | František | Wichterlová |                                 JetBrains s.r.o. | Klanova 9/506                        | Prague              |  None | Czech Republic | 14700      | +420 2 4172 5555    |   +420 2 4172 5555 | frantisekw@jetbrains.com      |            4 |
|          6 | Helena    | Holý        |                                             None | Rilská 3174/6                        | Prague              |  None | Czech Republic | 14300      | +420 2 4177 0449    |               None | hholy@gmail.com               |            5 |
|          9 | Kara      | Nielsen     |                                             None | Sønder Boulevard 51                  | Copenhagen          |  None | Denmark        | 1720       | +453 3331 9991      |               None | kara.nielsen@jubii.dk         |            4 |
|         44 | Terhi     | Hämäläinen  |                                             None | Porthaninkatu 9                      | Helsinki            |  None | Finland        | 00530      | +358 09 870 2000    |               None | terhi.hamalainen@apple.fi     |            3 |
|         42 | Wyatt     | Girard      |                                             None | 9, Place Louis Barthou               | Bordeaux            |  None | France         | 33000      | +33 05 56 96 96 96  |               None | wyatt.girard@yahoo.fr         |            3 |
|         43 | Isabelle  | Mercier     |                                             None | 68, Rue Jouvence                     | Dijon               |  None | France         | 21000      | +33 03 80 73 66 99  |               None | isabelle_mercier@apple.fr     |            3 |
|         41 | Marc      | Dubois      |                                             None | 11, Place Bellecour                  | Lyon                |  None | France         | 69002      | +33 04 78 30 30 30  |               None | marc.dubois@hotmail.com       |            5 |
+------------+-----------+-------------+--------------------------------------------------+--------------------------------------+---------------------+-------+----------------+------------+---------------------+--------------------+-------------------------------+--------------+
(Output limit exceeded, 25 of 59 total rows shown)
```

## ORDER BY

Database tables are often very complicated, and this is where other SQL keywords come in handy. <var>ORDER BY</var> is an important keyword for organizing the data you extract from a table.

<var>ORDER BY</var> 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 <var>ORDER BY</var> 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 <var>customerid</var>, <var>city</var>, and <var>country</var> columns from the <var>customers</var> table, and the records will be sequenced by the <var>city</var> column:

SELECT customerid, city, country  
FROM customers  
ORDER BY city;

```
+------------+--------------+----------------+
| CustomerId | City         | Country        |
+------------+--------------+----------------+
|         48 | Amsterdam    | Netherlands    |
|         59 | Bangalore    | India          |
|         36 | Berlin       | Germany        |
|         38 | Berlin       | Germany        |
|         42 | Bordeaux     | France         |
|         23 | Boston       | USA            |
|         13 | Brasília     | Brazil         |
|          8 | Brussels     | Belgium        |
|         45 | Budapest     | Hungary        |
|         56 | Buenos Aires | Argentina      |
|         24 | Chicago      | USA            |
|          9 | Copenhagen   | Denmark        |
|         19 | Cupertino    | USA            |
|         58 | Delhi        | India          |
|         43 | Dijon        | France         |
|         46 | Dublin       | Ireland        |
|         54 | Edinburgh    | United Kingdom |
|         14 | Edmonton     | Canada         |
|         26 | Fort Worth   | USA            |
|         37 | Frankfurt    | Germany        |
|         31 | Halifax      | Canada         |
|         44 | Helsinki     | Finland        |
|         34 | Lisbon       | Portugal       |
|         52 | London       | United Kingdom |
|         53 | London       | United Kingdom |
+------------+--------------+----------------+
(Output limit exceeded, 25 of 59 total rows shown)
```

The <var>ORDER BY</var> 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 <var>customerid</var>, the ID numbers are sorted from smallest to largest.
- if the column contains alphabetic characters, such as in the example with the <var>city</var> column, it orders the records from the beginning of the alphabet to the end.

### **Sorting in descending order**

You can also use the <var>ORDER BY</var> with the <var>DESC</var> keyword to sort in descending order. The <var>DESC</var> 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 <var>ORDER BY</var> with the <var>DESC</var> keyword. For example, you can run this query to examine how the results differ when <var>DESC</var> is applied:

SELECT customerid, city, country  
FROM customers  
ORDER BY city DESC;

```
+------------+---------------------+----------------+
| CustomerId | City                | Country        |
+------------+---------------------+----------------+
|         33 | Yellowknife         | Canada         |
|         32 | Winnipeg            | Canada         |
|         49 | Warsaw              | Poland         |
|          7 | Vienne              | Austria        |
|         15 | Vancouver           | Canada         |
|         27 | Tucson              | USA            |
|         29 | Toronto             | Canada         |
|         10 | São Paulo           | Brazil         |
|         11 | São Paulo           | Brazil         |
|          1 | São José dos Campos | Brazil         |
|          2 | Stuttgart           | Germany        |
|         51 | Stockholm           | Sweden         |
|         55 | Sidney              | Australia      |
|         57 | Santiago            | Chile          |
|         28 | Salt Lake City      | USA            |
|         47 | Rome                | Italy          |
|         12 | Rio de Janeiro      | Brazil         |
|         21 | Reno                | USA            |
|         17 | Redmond             | USA            |
|          5 | Prague              | Czech Republic |
|          6 | Prague              | Czech Republic |
|         35 | Porto               | Portugal       |
|         39 | Paris               | France         |
|         40 | Paris               | France         |
|         30 | Ottawa              | Canada         |
+------------+---------------------+----------------+
(Output limit exceeded, 25 of 59 total rows shown)
```

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 <var>country</var> and then the <var>city</var> column. SQL then sorts the output by <var>country</var>, and for rows with the same <var>country</var>, it sorts them based on <var>city</var>. You can run this to explore how SQL displays this:

SELECT customerid, city, country  
FROM customers  
ORDER BY country, city;

```
+------------+---------------------+----------------+
| CustomerId | City                | Country        |
+------------+---------------------+----------------+
|         56 | Buenos Aires        | Argentina      |
|         55 | Sidney              | Australia      |
|          7 | Vienne              | Austria        |
|          8 | Brussels            | Belgium        |
|         13 | Brasília            | Brazil         |
|         12 | Rio de Janeiro      | Brazil         |
|          1 | São José dos Campos | Brazil         |
|         10 | São Paulo           | Brazil         |
|         11 | São Paulo           | Brazil         |
|         14 | Edmonton            | Canada         |
|         31 | Halifax             | Canada         |
|          3 | Montréal            | Canada         |
|         30 | Ottawa              | Canada         |
|         29 | Toronto             | Canada         |
|         15 | Vancouver           | Canada         |
|         32 | Winnipeg            | Canada         |
|         33 | Yellowknife         | Canada         |
|         57 | Santiago            | Chile          |
|          5 | Prague              | Czech Republic |
|          6 | Prague              | Czech Republic |
|          9 | Copenhagen          | Denmark        |
|         44 | Helsinki            | Finland        |
|         42 | Bordeaux            | France         |
|         43 | Dijon               | France         |
|         41 | Lyon                | France         |
+------------+---------------------+----------------+
(Output limit exceeded, 25 of 59 total rows shown)
```

## Key takeaways

<var>SELECT</var> and <var>FROM</var> are important keywords in SQL queries. You use <var>SELECT</var> to indicate which columns to return and <var>FROM</var> to indicate which table to query. You can also include <var>ORDER BY</var> in your query to organize the output. These foundational SQL skills will support you as you move into more advanced queries.

# find table name and comumns definintion for SQL and variences

<div class="flex flex-grow flex-col gap-3" id="bkmrk-standard-sql%3A-for-da"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">  
</div><div class="empty:hidden">**Standard SQL:**</div><div class="empty:hidden">For databases that support the ANSI SQL standard and have the `INFORMATION_SCHEMA` views available, you can use the following query:</div></div></div>```sql
SELECT table_name, column_name
FROM information_schema.columns;
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-you-can-append-if-yo"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">you can append if you want to specify where  
</div></div></div>```sql
WHERE table_schema = 'your_database_name'; 
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-if-you-want-database"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">if you want **Database-specific Queries:** If you are working with a specific database system and the standard SQL approach doesn't work, you can try the following methods:</div></div></div>**MySQL/MariaDB:**

```mysql
SELECT table_name, column_name
FROM information_schema.columns;
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-or"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">**or**</div></div></div>```mysql
SHOW TABLES;
DESCRIBE table_name;
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-postgresql%3A"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">**PostgreSQL:**</div></div></div>```sql
 SELECT table_name, column_name
FROM information_schema.columns;
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-sqlite%3A"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden">**SQLite:**</div><div class="empty:hidden"><div class="empty:hidden">  
</div><div class="empty:hidden">  
</div></div></div></div>```sqlite
SELECT name AS table_name, sql AS column_definition
FROM sqlite_master
WHERE type = 'table';
```

<div class="flex flex-grow flex-col gap-3" id="bkmrk-you-would-run-this-s"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="empty:hidden"><div class="empty:hidden">  
</div></div><div class="empty:hidden">  
</div><div class="empty:hidden">You would run this SQLite command when you want to list all the tables in your SQLite database along with their SQL schema.</div></div></div>SQLite keeps a system table, `sqlite_master`, where it stores metadata about the database. Each row of `sqlite_master` represents an object (table, index, etc.) in the database.

The columns are:

<div class="flex flex-grow flex-col gap-3" id="bkmrk-type%3A-the-type-of-th"><div class="min-h-[20px] flex items-start overflow-x-auto whitespace-pre-wrap break-words flex-col gap-4"><div class="markdown prose w-full break-words dark:prose-invert light">- type: the type of the database object, such as 'table' or 'index'.
- name: the name of the object.
- tbl\_name: the name of the table to which the object is associated. For a table, it's the same as `name`.
- rootpage: the page number in the database file where the root B-tree page for the object is stored.
- sql: the SQL statement that created the object.

</div></div></div>This command specifies `type = 'table'` in the `WHERE` clause, so it only selects tables, not other types of objects like indices. For each table, it selects the name (renamed as `table_name` for clarity) and the SQL statement that created the table (as `column_definition`).

So this command is useful when you need to know the structure of all tables in your SQLite database, such as the table names and their corresponding column definitions. It's a handy tool for exploring a database when you don't have the schema in front of you or when you've inherited a database and need to understand its structure.

# Basic filters on SQL queries

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-one-of-the-most-powe"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from One of the most powerful features of SQL is its ability to filter." class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">One of the most powerful features of SQL is its ability to filter. </span></div><div aria-label="toggle video from In this video, we're going to learn how this helps us make better queries and" class="rc-Phrase css-ugczj4" data-cue="2" data-cue-index="1" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this video, we're going to learn how this helps us make better queries and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">select more specific pieces of data from a database. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-filtering-is-selecti"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Filtering is selecting data that match a certain condition." class="rc-Phrase css-ugczj4" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Filtering is selecting data that match a certain condition. </span></div><div aria-label="toggle video from Think of filtering as a way of only choosing the data we want." class="rc-Phrase css-ugczj4" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Think of filtering as a way of only choosing the data we want. </span></div><div aria-label="toggle video from Let's say we wanted to select apples from a fruit cart." class="rc-Phrase css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's say we wanted to select apples from a fruit cart. </span></div><div aria-label="toggle video from Filtering allows us to specify what kind of apples we want to choose." class="rc-Phrase css-ugczj4" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Filtering allows us to specify what kind of apples we want to choose. </span></div><div aria-label="toggle video from When we go buy apples, we might explicitly say, "Choose only apples that are fresh."" class="rc-Phrase css-ugczj4" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">When we go buy apples, we might explicitly say, "Choose only apples that are fresh." </span></div><div aria-label="toggle video from This removes apples that aren't fresh from the selection." class="rc-Phrase css-ugczj4" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This removes apples that aren't fresh from the selection. </span></div><div aria-label="toggle video from This is a filter!" class="rc-Phrase css-ugczj4" data-cue="10" data-cue-index="9" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is a filter! </span></div><div aria-label="toggle video from As a security analyst, you might filter a log-in attempts table to find all attempts" class="rc-Phrase css-ugczj4" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">As a security analyst, you might filter a log-in attempts table to find all attempts </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">from a specific country. </span></div><div aria-label="toggle video from This could be done by applying a filter on the country column." class="rc-Phrase css-ugczj4" data-cue="13" data-cue-index="12" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This could be done by applying a filter on the country column. </span></div><div aria-label="toggle video from current lecture segment: For example, you could filter to just return records containing Canada." class="rc-Phrase active css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, you could filter to just return records containing Canada. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-before-we-get-starte"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Before we get started, we need to focus on an important part of the syntax of SQL." class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Before we get started, we need to focus on an important part of the syntax of SQL. </span></div><div aria-label="toggle video from Let's learn about operators." class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's learn about operators. </span></div><div aria-label="toggle video from An operator is a symbol or keyword that represents an operation." class="rc-Phrase css-ugczj4" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">An operator is a symbol or keyword that represents an operation. </span></div><div aria-label="toggle video from An example of an operator would be the equal to operator." class="rc-Phrase css-ugczj4" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">An example of an operator would be the equal to operator. </span></div><div aria-label="toggle video from For example, if we wanted to find all records that" class="rc-Phrase css-ugczj4" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, if we wanted to find all records that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">have USA in the country column, we use country = 'USA' </span></div><div aria-label="toggle video from For example, if we wanted to find all records that" class="rc-Phrase css-ugczj4" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">To filter a query in SQL, we simply add an extra line to the SELECT and FROM </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">statement we used before. </span></div><div aria-label="toggle video from This extra line will use a WHERE clause." class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This extra line will use a WHERE clause. </span></div><div aria-label="toggle video from In SQL, WHERE indicates the condition for a filter." class="rc-Phrase css-ugczj4" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In SQL, WHERE indicates the condition for a filter. </span></div><div aria-label="toggle video from After the keyword WHERE, the specific condition is listed using operators." class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">After the keyword WHERE, the specific condition is listed using operators. </span></div><div aria-label="toggle video from So if we wanted to find all of the login attempts made in the United States," class="rc-Phrase css-ugczj4" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">So if we wanted to find all of the login attempts made in the United States, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we would create this filter. </span></div><div aria-label="toggle video from In this particular condition, we're indicating to return all records that" class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this particular condition, we're indicating to return all records that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">have a value in the country column that is equal to USA. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--2"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-try-putting-it"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's try putting it all together in SQL." class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's try putting it all together in SQL. </span></div><div aria-label="toggle video from We're going to start with selecting all the columns from the" class="rc-Phrase css-ugczj4" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We're going to start with selecting all the columns from the </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">log\_in\_attempts table. And then add the WHERE filter. </span></div><div aria-label="toggle video from Don't forget the semicolon!" class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Don't forget the semicolon! </span></div><div aria-label="toggle video from This tells us we finished the SQL statement." class="rc-Phrase css-ugczj4" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This tells us we finished the SQL statement. </span></div><div aria-label="toggle video from Now, let's run this query! Because of our filter, only the rows" class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Now, let's run this query! Because of our filter, only the rows </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">where the country of the log-in attempt was USA are returned. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--3"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-in-the-previous-exam"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from In the previous example, the condition for our filter was based simply on returning" class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In the previous example, the condition for our filter was based simply on returning </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">records that are equal to a particular value. </span></div><div aria-label="toggle video from We can also make our conditions more complex by searching for" class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We can also make our conditions more complex by searching for </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a pattern instead of an exact word. </span></div><div aria-label="toggle video from For example, in the employees table, we have a column for office." class="rc-Phrase css-ugczj4" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, in the employees table, we have a column for office. </span></div><div aria-label="toggle video from We could search for records in this column that match a certain pattern." class="rc-Phrase css-ugczj4" data-cue="42" data-cue-index="41" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We could search for records in this column that match a certain pattern. </span></div><div aria-label="toggle video from Perhaps we might want all offices in the East building." class="rc-Phrase css-ugczj4" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Perhaps we might want all offices in the East building. </span></div><div aria-label="toggle video from To search for a pattern, we used the percentage sign to act as a wildcard for" class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">To search for a pattern, we used the percentage sign to act as a wildcard for </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">unspecified characters. </span></div><div aria-label="toggle video from If we ran a filter for 'East%', this would return all records that start with East --" class="rc-Phrase css-ugczj4" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">If we ran a filter for 'East%', this would return all records that start with East -- </span></div><div aria-label="toggle video from for example, the offices East-120, East-290, and East-435." class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">for example, the offices East-120, East-290, and East-435. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--4"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-when-searching-for-p"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from When searching for patterns with the percentage sign," class="rc-Phrase css-ugczj4" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">When searching for patterns with the percentage sign, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we cannot use the equals operator. </span></div><div aria-label="toggle video from Instead, we use another operator, LIKE." class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Instead, we use another operator, LIKE. </span></div><div aria-label="toggle video from LIKE is an operator used with WHERE to search for a pattern in a column." class="rc-Phrase css-ugczj4" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">LIKE is an operator used with WHERE to search for a pattern in a column. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--5"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-since-like-is-an-ope"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Since LIKE is an operator, similar to the equal sign," class="rc-Phrase css-ugczj4" data-cue="52" data-cue-index="51" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Since LIKE is an operator, similar to the equal sign, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we use it instead of the equal sign. </span></div><div aria-label="toggle video from So, when our goal is to return all values in the office column that start with the word" class="rc-Phrase css-ugczj4" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">So, when our goal is to return all values in the office column that start with the word </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">East, LIKE would appear in a WHERE clause. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--6"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-go-back-to-the"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's go back to the example in which we wanted to filter for" class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's go back to the example in which we wanted to filter for </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">log-in attempts made in the United States. </span></div><div aria-label="toggle video from Imagine that we realize that our database contains inconsistencies with how" class="rc-Phrase css-ugczj4" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Imagine that we realize that our database contains inconsistencies with how </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the United States is represented. </span></div><div aria-label="toggle video from Some entries use US while others use USA." class="rc-Phrase css-ugczj4" data-cue="60" data-cue-index="59" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Some entries use US while others use USA. </span></div><div aria-label="toggle video from Let's get into SQL and apply this new type of filter with LIKE." class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's get into SQL and apply this new type of filter with LIKE. </span></div><div aria-label="toggle video from We're going to start with the same first two lines of" class="rc-Phrase css-ugczj4" data-cue="62" data-cue-index="61" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We're going to start with the same first two lines of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">code because we want to select all columns from the log-in attempts table. </span></div><div aria-label="toggle video from And we're going to add a filter with LIKE so that records will be returned if" class="rc-Phrase css-ugczj4" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And we're going to add a filter with LIKE so that records will be returned if </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">they contain a value in the country column beginning with the characters US. </span></div><div aria-label="toggle video from This includes both US and USA." class="rc-Phrase css-ugczj4" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This includes both US and USA. </span></div><div aria-label="toggle video from Let's run this query to check if the output changes. This returns all" class="rc-Phrase css-ugczj4" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's run this query to check if the output changes. This returns all </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the entries where the user location was in the United States. </span></div><div aria-label="toggle video from And now we can use the LIKE clause to filter columns based on a pattern!" class="rc-Phrase css-ugczj4" data-cue="69" data-cue-index="68" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And now we can use the LIKE clause to filter columns based on a pattern! </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--7"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-wow%2C-we%27ve-already-l"><div aria-label="toggle video from Wow, we've already learned how to get very precise with our database and" class="rc-Phrase css-ugczj4" data-cue="70" data-cue-index="69" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Wow, we've already learned how to get very precise with our database and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">get exactly the data we need with one single query. </span></div><div aria-label="toggle video from I'm excited for what's next!" class="rc-Phrase css-ugczj4" data-cue="72" data-cue-index="71" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">I'm excited for what's next! </span></div></div>

# The WHERE clause and basic operators

Previously, you focused on how to refine your SQL queries by using the <var>WHERE</var> clause to filter results. In this reading, you’ll further explore how to use the <var>WHERE</var> clause, the <var>LIKE</var> operator and the percentage sign (<var>%</var>) wildcard. You’ll also be introduced to the underscore (<var>\_</var>), 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 <var>WHERE</var>. <var>WHERE</var> 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:

---

```sql
SELECT firstname, lastname, title, email
FROM employees
WHERE title = 'IT Staff';
```

---

```
+-----------+----------+----------+------------------------+
| FirstName | LastName | Title    | Email                  |
+-----------+----------+----------+------------------------+
| Robert    | King     | IT Staff | robert@chinookcorp.com |
| Laura     | Callahan | IT Staff | laura@chinookcorp.com  |
+-----------+----------+----------+------------------------+
```

---

Rather than returning all records in the <var>employees</var> table, this <var>WHERE</var> clause instructs SQL to return only those that contain <var>'IT Staff'</var> in the <var>title</var> column. It uses the equals sign (<var>=</var>) operator to set this condition.

**Note:** You should place the semicolon (<var>;</var>) 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 <var>WHERE</var> clause:

- a wildcard
- the <var>LIKE</var> 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 (<var>%</var>) and the underscore (<var>\_</var>):

- 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 <var>'a'</var> and examples of what each pattern would return.

<div class="css-1yr0py9" id="bkmrk-pattern-results-that"><table><thead><tr><th scope="col">**Pattern**

</th><th scope="col">**Results that could be returned**

</th></tr></thead><tbody><tr><td><var>'a%'</var>

</td><td><var>apple123, art, a</var>

</td></tr><tr><td><var>'a\_'</var>

</td><td><var>as, an, a7</var>

</td></tr><tr><td><var>'a\_\_' </var>

</td><td><var>ant, add, a1c</var>

</td></tr><tr><td><var>'%a'</var>

</td><td><var>pizza, Z6ra, a</var>

</td></tr><tr><td><var>'\_a'</var>

</td><td><var>ma, 1a, Ha</var>

</td></tr><tr><td><var>'%a%'</var>

</td><td><var>Again, back, a</var>

</td></tr><tr><td><var>'\_a\_'</var>

</td><td><var>Car, ban, ea7</var>

</td></tr></tbody></table>

</div>### **LIKE**

To apply wildcards to the filter, you need to use the <var>LIKE</var> operator instead of an equals sign (<var>=</var>). <var>LIKE</var> is used with <var>WHERE</var> to search for a pattern in a column.

For instance, if you want to email employees with a title of either <var>'IT Staff'</var> or <var>'IT Manager'</var>, you can use <var>LIKE</var> operator combined with the <var>%</var> wildcard:

---

```sql
SELECT lastname, firstname, title, email
FROM employees
WHERE title LIKE 'IT%';
```

---

```
+----------+-----------+------------+-------------------------+
| LastName | FirstName | Title      | Email                   |
+----------+-----------+------------+-------------------------+
| Mitchell | Michael   | IT Manager | michael@chinookcorp.com |
| King     | Robert    | IT Staff   | robert@chinookcorp.com  |
| Callahan | Laura     | IT Staff   | laura@chinookcorp.com   |
+----------+-----------+------------+-------------------------+
```

---

This query returns all records with values in the <var>title</var> column that start with the pattern of <var>'IT'</var>. This means both <var>'IT Staff'</var> and <var>'IT Manager'</var> 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 <var>'NY'</var>, <var>'NV'</var>, <var>'NS'</var> or <var>'NT'</var>, you can use the <var>'N\_'</var> pattern on the <var>state</var> column:

---

```sql
SELECT firstname,lastname, state, country
FROM customers
WHERE state LIKE 'N_';
```

---

```
+-----------+----------+-------+---------+
| FirstName | LastName | State | Country |
+-----------+----------+-------+---------+
| Michelle  | Brooks   | NY    | USA     |
| Kathy     | Chase    | NV    | USA     |
| Martha    | Silk     | NS    | Canada  |
| Ellie     | Sullivan | NT    | Canada  |
+-----------+----------+-------+---------+
```

---

This returns all the records with state abbreviations that follow this pattern.

## Key takeaways

Filters are important when refining what your query returns. <var>WHERE</var> is an essential keyword for adding a filter to your query. You can also filter for patterns by combining the <var>LIKE</var> operator with the percentage sign (<var>%</var>) and the underscore (<var>\_</var>) wildcards.

# Filter dates and numbers

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-in-this-video%2C-we%27re"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from current lecture segment: In this video, we're going to continue using SQL queries and filters," class="rc-Phrase active css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this video, we're going to continue using SQL queries and filters, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but now we're going to apply them to new data types. </span></div><div aria-label="toggle video from First, let's explore the three common data types that you will find in databases:" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, let's explore the three common data types that you will find in databases: </span></div><div aria-label="toggle video from string, numeric, and date and time." class="rc-Phrase css-ugczj4" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">string, numeric, and date and time. </span></div><div aria-label="toggle video from String data is data consisting of an ordered sequence of characters." class="rc-Phrase css-ugczj4" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">String data is data consisting of an ordered sequence of characters. </span></div><div aria-label="toggle video from These characters could be numbers, letters, or symbols." class="rc-Phrase css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">These characters could be numbers, letters, or symbols. </span></div><div aria-label="toggle video from For example, you'll encounter string data in user names," class="rc-Phrase css-ugczj4" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, you'll encounter string data in user names, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">such as a user name: analyst10. </span></div><div aria-label="toggle video from Numeric data is data consisting of numbers," class="rc-Phrase css-ugczj4" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Numeric data is data consisting of numbers, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">such as a count of log-in attempts. </span></div><div aria-label="toggle video from Unlike strings, mathematical operations can be used on numeric data," class="rc-Phrase css-ugczj4" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Unlike strings, mathematical operations can be used on numeric data, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">like multiplication or addition. </span></div><div aria-label="toggle video from Date and time data refers to data representing a date and/or time." class="rc-Phrase css-ugczj4" data-cue="13" data-cue-index="12" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Date and time data refers to data representing a date and/or time. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-previously%2C-we-appli"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Previously, we applied filters using string data, but" class="rc-Phrase css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Previously, we applied filters using string data, but </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">now let's work with numeric and date and time data. </span></div><div aria-label="toggle video from As a security analyst, you'll often need to query numbers and dates." class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">As a security analyst, you'll often need to query numbers and dates. </span></div><div aria-label="toggle video from For example, we could filter patch dates to find machines that need an update," class="rc-Phrase css-ugczj4" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, we could filter patch dates to find machines that need an update, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">or we could filter log-in attempts to return only </span></div><div aria-label="toggle video from those made in a certain period of time." class="rc-Phrase css-ugczj4" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">those made in a certain period of time. </span></div><div aria-label="toggle video from We learned about operators in the last video, and" class="rc-Phrase css-ugczj4" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We learned about operators in the last video, and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we're going to use them again for numbers and dates. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-common-operators-for"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Common operators for working with numeric or date and" class="rc-Phrase css-ugczj4" data-cue="22" data-cue-index="21" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Common operators for working with numeric or date and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">time data types include: equals, greater than, less than, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">not equal to, greater than or equal to, and less than or equal to. </span></div><div aria-label="toggle video from Let's say you want to find the log-in attempts made after 6 pm." class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's say you want to find the log-in attempts made after 6 pm. </span></div><div aria-label="toggle video from Because this is past normal business hours," class="rc-Phrase css-ugczj4" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Because this is past normal business hours, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you want to look for suspicious patterns. </span></div><div aria-label="toggle video from You can identify these attempts by using the greater than operator in your filter." class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">You can identify these attempts by using the greater than operator in your filter. </span></div><div aria-label="toggle video from We'll start writing our query in SQL." class="rc-Phrase css-ugczj4" data-cue="29" data-cue-index="28" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We'll start writing our query in SQL. </span></div><div aria-label="toggle video from We begin by indicating that we want to select all columns FROM the" class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We begin by indicating that we want to select all columns FROM the </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">log\_in\_attempts table. </span></div><div aria-label="toggle video from Then we'll add our filter with WHERE." class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Then we'll add our filter with WHERE. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--2"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-our-condition-indica"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Our condition indicates that the value in the time column must be greater than, or" class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Our condition indicates that the value in the time column must be greater than, or </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">for dates and times, later than '18:00', which is how 6 pm is written in SQL. </span></div><div aria-label="toggle video from Let's run this and examine the output." class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's run this and examine the output. </span></div><div aria-label="toggle video from Perfect! Now we have a list of log-in attempts made after 6 pm." class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Perfect! Now we have a list of log-in attempts made after 6 pm. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--3"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-we-can-also-filter-f"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from We can also filter for numbers and dates by using the BETWEEN operator." class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We can also filter for numbers and dates by using the BETWEEN operator. </span></div><div aria-label="toggle video from BETWEEN is an operator that filters for numbers or dates within a range." class="rc-Phrase css-ugczj4" data-cue="38" data-cue-index="37" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">BETWEEN is an operator that filters for numbers or dates within a range. </span></div><div aria-label="toggle video from An example of this would be when looking for" class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">An example of this would be when looking for </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all patches installed within a certain range. </span></div><div aria-label="toggle video from Let's do this! Let's find all the patches installed between March 1st," class="rc-Phrase css-ugczj4" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's do this! Let's find all the patches installed between March 1st, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">2021 and September 1st, 2021. </span></div><div aria-label="toggle video from In our query, we start with selecting all records FROM the machines table." class="rc-Phrase css-ugczj4" data-cue="43" data-cue-index="42" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In our query, we start with selecting all records FROM the machines table. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--4"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-and-we-add-the-betwe"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from And we add the BETWEEN operator in the WHERE statement." class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And we add the BETWEEN operator in the WHERE statement. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--5"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-break-down-the"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's break down the statement." class="rc-Phrase css-ugczj4" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's break down the statement. </span></div><div aria-label="toggle video from First, after WHERE, we indicate which column to filter," class="rc-Phrase css-ugczj4" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, after WHERE, we indicate which column to filter, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">in our case, OS\_patch\_date. </span></div><div aria-label="toggle video from Next, comes our operator BETWEEN." class="rc-Phrase css-ugczj4" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Next, comes our operator BETWEEN. </span></div><div aria-label="toggle video from We then add the beginning of our range, type AND," class="rc-Phrase css-ugczj4" data-cue="49" data-cue-index="48" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We then add the beginning of our range, type AND, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">then finish by adding the end of our range and a semicolon. </span></div><div aria-label="toggle video from Now, let's run this and explore the output." class="rc-Phrase css-ugczj4" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Now, let's run this and explore the output. </span></div><div aria-label="toggle video from And now we have a list of all machines patched between those two dates!" class="rc-Phrase css-ugczj4" data-cue="52" data-cue-index="51" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And now we have a list of all machines patched between those two dates! </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--6"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-before-we-wrap-up%2C-a"><div aria-label="toggle video from Before we wrap up, an important thing to note is that when we filter for strings," class="rc-Phrase css-ugczj4" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Before we wrap up, an important thing to note is that when we filter for strings, </span></div><div aria-label="toggle video from dates, and times, we use quotation marks to specify what we're looking for." class="rc-Phrase css-ugczj4" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">dates, and times, we use quotation marks to specify what we're looking for. </span></div><div aria-label="toggle video from However, for numbers, we don't use quotation marks." class="rc-Phrase css-ugczj4" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">However, for numbers, we don't use quotation marks. </span></div><div aria-label="toggle video from With this new knowledge," class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">With this new knowledge, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you're now ready to work on all sorts of interesting filters for numbers and dates. </span></div><div aria-label="toggle video from In the next video, we'll be able to expand our filtering even further by" class="rc-Phrase css-ugczj4" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In the next video, we'll be able to expand our filtering even further by </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">using multiple conditions in one query. </span></div></div>

# Operators for filtering dates and numbers

Previously, you examined operators like less than (<var>&lt;</var>) or greater than (<var>&gt;</var>) 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:

<div class="rc-CML" dir="auto" id="bkmrk-the-number-of-login-"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer">- 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

</div></div></div></div></div>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:

<div class="rc-CML" dir="auto" id="bkmrk-login-dates-login-ti"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer">- login dates
- login times
- dates for patches
- the duration of a connection

</div></div></div></div></div>## 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:

<div class="rc-CML" dir="auto" id="bkmrk-operator-use-%3C-less-"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer"><div class="css-1yr0py9"><table><thead><tr><th scope="col">**operator**

</th><th scope="col">**use**

</th></tr></thead><tbody><tr><td><var>&lt;</var>

</td><td>less than

</td></tr><tr><td><var>&gt;</var>

</td><td>greater than

</td></tr><tr><td><var>=</var>

</td><td>equal to

</td></tr><tr><td><var>&lt;=</var>

</td><td>less than or equal to

</td></tr><tr><td><var>&gt;=</var>

</td><td>greater than or equal to

</td></tr><tr><td><var>&lt;&gt;</var>

</td><td>not equal to

</td></tr></tbody></table>

</div></div></div></div></div></div>**Note:** You can also use <var>!=</var> as an alternative operator for not equal to.

### Incorporating operators into filters

These comparison operators are used in the <var>WHERE</var> clause at the end of a query. The following query uses the <var>&gt;</var> operator to filter the <var>birthdate</var> column. You can run this query to explore its output:

---

```sqlite
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, <var>'1970-01-01'</var> (or January 1, 1970). If you were to use the <var>&gt;=</var> operator instead, the results would also include results on exactly <var>'1970-01-01'</var>.

In other words, the <var>&gt;</var> operator is exclusive and the <var>&gt;=</var> 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 <var>BETWEEN</var> operator. <var>BETWEEN</var> 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 <var>BETWEEN</var> 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 <var>BETWEEN</var> operator is inclusive. This means records with a <var>hiredate</var> 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 <var>&lt;</var> and inclusive operators such as <var>&lt;=</var>. The <var>BETWEEN</var> operator, another inclusive operator, helps you return the data you need within a range.

# Filters with AND, OR, and NOT

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-in-the-previous-less"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from In the previous lesson," class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In the previous lesson, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we learned about even more ways to filter queries in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">SQL to work with some typical security analyst tasks. </span></div><div aria-label="toggle video from However, when working with real security questions," class="rc-Phrase css-ugczj4" data-cue="4" data-cue-index="3" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">However, when working with real security questions, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we often have to filter for multiple conditions. </span></div><div aria-label="toggle video from current lecture segment: Vulnerabilities, for instance," class="rc-Phrase active css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Vulnerabilities, for instance, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">might depend on more than one factor. </span></div><div aria-label="toggle video from For example, a" class="rc-Phrase css-ugczj4" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, a </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">security vulnerability might be related to </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">machines using a specific email client </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">on a specific operating system. </span></div><div aria-label="toggle video from So, to find the possible vulnerabilities," class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">So, to find the possible vulnerabilities, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we need to find machines using </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">both the email client and the operating system. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-to-make-a-query-with"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from To make a query with" class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">To make a query with </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">multiple conditions that must be met, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we use the AND operator between two separate conditions. </span></div><div aria-label="toggle video from AND is an operator that specifies that" class="rc-Phrase css-ugczj4" data-cue="18" data-cue-index="17" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">AND is an operator that specifies that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">both conditions must be met simultaneously. </span></div><div aria-label="toggle video from Bringing this back to our fruit and vegetable analogy," class="rc-Phrase css-ugczj4" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Bringing this back to our fruit and vegetable analogy, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">this is the same as asking someone to select apples from </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the big box where the apples are large and fresh. </span></div><div aria-label="toggle video from This means our results won't include any small apples" class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This means our results won't include any small apples </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">even if they're fresh, or </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">any rotten apples even if they're large. </span></div><div aria-label="toggle video from They'll only include large fresh apples." class="rc-Phrase css-ugczj4" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">They'll only include large fresh apples. </span></div><div aria-label="toggle video from The apples must meet both conditions." class="rc-Phrase css-ugczj4" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The apples must meet both conditions. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-going-back-to-our-da"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Going back to our database," class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Going back to our database, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the machines table lists </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all operating systems and email clients. </span></div><div aria-label="toggle video from We want a list of machines running Operating System" class="rc-Phrase css-ugczj4" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We want a list of machines running Operating System </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">1 and a list of machines using Email Client 1. </span></div><div aria-label="toggle video from We'll use the left and right circles in" class="rc-Phrase css-ugczj4" data-cue="33" data-cue-index="32" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We'll use the left and right circles in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the Venn diagram to represent these groups. </span></div><div aria-label="toggle video from We need SQL to select the machines that" class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We need SQL to select the machines that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">have both OS 1 and Email Client 1. </span></div><div aria-label="toggle video from The filled-in area at the intersection of" class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The filled-in area at the intersection of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">these circles represents this condition. </span></div><div aria-label="toggle video from Let's take this and implement it in SQL." class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's take this and implement it in SQL. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--2"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-first%2C-we%27re-going-t"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from First, we're going to start by" class="rc-Phrase css-ugczj4" data-cue="40" data-cue-index="39" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, we're going to start by </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">building the first lines of the query, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">telling SQL to SELECT\* </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all columns FROM the machines table. </span></div><div aria-label="toggle video from Then, we'll add the WHERE clause." class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Then, we'll add the WHERE clause.   
</span></div><div aria-label="toggle video from Then, we'll add the WHERE clause." class="rc-Phrase css-ugczj4" data-cue="44" data-cue-index="43" role="button" tabindex="0"></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-examine-this-m"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's examine this more closely." class="rc-Phrase css-ugczj4" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's examine this more closely. </span></div><div aria-label="toggle video from First, we indicate" class="rc-Phrase css-ugczj4" data-cue="46" data-cue-index="45" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, we indicate </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the first condition that it must meet, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">that the operating system column has a value of '0S 1' </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--3"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-then%2C-we-use-and-to-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Then, we use AND to join this to another condition." class="rc-Phrase css-ugczj4" data-cue="49" data-cue-index="48" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Then, we use AND to join this to another condition. </span></div><div aria-label="toggle video from And finally, we enter the other condition," class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And finally, we enter the other condition, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">in this case that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the email client column should </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">have a value of 'Email Client 1' </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-and-this-is-how-you-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from And this is how you use the AND operator in SQL!" class="rc-Phrase css-ugczj4" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And this is how you use the AND operator in SQL! </span></div><div aria-label="toggle video from Let's run this to get the query results." class="rc-Phrase css-ugczj4" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's run this to get the query results. </span></div><div aria-label="toggle video from Perfect! All the results match both our conditions!" class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Perfect! All the results match both our conditions! </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--4"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-keep-going-and"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's keep going and explore more ways to combine" class="rc-Phrase css-ugczj4" data-cue="57" data-cue-index="56" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's keep going and explore more ways to combine </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">different conditions by working with the OR operator. </span></div><div aria-label="toggle video from The OR operator is an operator that" class="rc-Phrase css-ugczj4" data-cue="59" data-cue-index="58" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The OR operator is an operator that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">specifies that either condition can be met. </span></div><div aria-label="toggle video from In a Venn diagram," class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In a Venn diagram, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">let's say each circle represents a condition. </span></div><div aria-label="toggle video from When they are joined with OR," class="rc-Phrase css-ugczj4" data-cue="63" data-cue-index="62" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">When they are joined with OR, </span></div><div aria-label="toggle video from SQL would select all rows that" class="rc-Phrase css-ugczj4" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">SQL would select all rows that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">satisfy one of the conditions. </span></div><div aria-label="toggle video from And it's also ok if it meets both conditions." class="rc-Phrase css-ugczj4" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">And it's also ok if it meets both conditions. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--5"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-run-another-qu"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's run another query and use the OR operator." class="rc-Phrase css-ugczj4" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's run another query and use the OR operator. </span></div><div aria-label="toggle video from Let's say that we wanted the" class="rc-Phrase css-ugczj4" data-cue="68" data-cue-index="67" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's say that we wanted the </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">filter to identify machines that have </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">either OS 1 or OS 3 because both types need a patch. </span></div><div aria-label="toggle video from We'll type in these conditions." class="rc-Phrase css-ugczj4" data-cue="71" data-cue-index="70" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We'll type in these conditions. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--6"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-examine-this-m-1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's examine this more closely." class="rc-Phrase css-ugczj4" data-cue="72" data-cue-index="71" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's examine this more closely. </span></div><div aria-label="toggle video from After WHERE, our first condition indicates we want to" class="rc-Phrase css-ugczj4" data-cue="73" data-cue-index="72" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">After WHERE, our first condition indicates we want to </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">filter, so that the query selects machines with 'OS 1' </span></div><div aria-label="toggle video from We use the OR operator because we also want" class="rc-Phrase css-ugczj4" data-cue="75" data-cue-index="74" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We use the OR operator because we also want </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">to find records that match another condition. </span></div><div aria-label="toggle video from This additional condition is placed after OR and" class="rc-Phrase css-ugczj4" data-cue="77" data-cue-index="76" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This additional condition is placed after OR and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">indicates to also select machines running 'OS 3' </span></div><div aria-label="toggle video from Executing the query," class="rc-Phrase css-ugczj4" data-cue="79" data-cue-index="78" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Executing the query, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">our results now include records that have a value of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">either OS 1 or OS 3 in the operating system column. </span></div><div aria-label="toggle video from Good job, we're running some complex queries." class="rc-Phrase css-ugczj4" data-cue="82" data-cue-index="81" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Good job, we're running some complex queries. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--7"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-the-last-operator-we"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from The last operator we're going to go" class="rc-Phrase css-ugczj4" data-cue="83" data-cue-index="82" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The last operator we're going to go </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">into is the NOT operator. </span></div><div aria-label="toggle video from NOT negates a condition." class="rc-Phrase css-ugczj4" data-cue="85" data-cue-index="84" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">NOT negates a condition. </span></div><div aria-label="toggle video from In a diagram," class="rc-Phrase css-ugczj4" data-cue="86" data-cue-index="85" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In a diagram, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we can show this by selecting </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">every entry that does not match our condition. </span></div><div aria-label="toggle video from The condition is represented by the circle." class="rc-Phrase css-ugczj4" data-cue="89" data-cue-index="88" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The condition is represented by the circle. </span></div><div aria-label="toggle video from The filled-in portion outside" class="rc-Phrase css-ugczj4" data-cue="90" data-cue-index="89" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The filled-in portion outside </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the circle represents what gets returned. </span></div><div aria-label="toggle video from This is all data that does not match the condition." class="rc-Phrase css-ugczj4" data-cue="92" data-cue-index="91" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is all data that does not match the condition. </span></div><div aria-label="toggle video from For example, when picking out fruit," class="rc-Phrase css-ugczj4" data-cue="93" data-cue-index="92" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, when picking out fruit, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you can be looking for any fruit that is not an apple. </span></div><div aria-label="toggle video from That is a lot more efficient" class="rc-Phrase css-ugczj4" data-cue="95" data-cue-index="94" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">That is a lot more efficient </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">than telling your friend you want </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a banana or an orange or a lime, and so on. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--8"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-suppose-you-wanted-t"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Suppose you wanted to update" class="rc-Phrase css-ugczj4" data-cue="98" data-cue-index="97" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Suppose you wanted to update </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all of the devices in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">your company except for the ones using OS 3. </span></div><div aria-label="toggle video from Bringing this into SQL," class="rc-Phrase css-ugczj4" data-cue="101" data-cue-index="100" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Bringing this into SQL, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we can write this query. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--9"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-we-place-not-after%C2%A0w"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from We place NOT after" class="rc-Phrase css-ugczj4" data-cue="103" data-cue-index="102" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We place NOT after </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">WHERE and before the condition of the filter. </span></div><div aria-label="toggle video from Executing these queries gives us the list" class="rc-Phrase css-ugczj4" data-cue="105" data-cue-index="104" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Executing these queries gives us the list </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">of all the machines that aren't running OS 3, and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">now we know which machines to update. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--10"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-that-was-a-lot-of-ne"><div aria-label="toggle video from That was a lot of new content that we just looked into," class="rc-Phrase css-ugczj4" data-cue="108" data-cue-index="107" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">That was a lot of new content that we just looked into, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but you're learning more and more SQL that you </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">can use on your journey to become an analyst! </span></div><div aria-label="toggle video from In the next video," class="rc-Phrase css-ugczj4" data-cue="111" data-cue-index="110" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In the next video, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we'll be learning how to combine and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">join two tables together to </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">expand the kinds of queries we can run. I'll meet you there! </span></div></div>

# More on filters with AND, OR, and NOT

Previously, you explored how to add filters containing the <var>AND</var>, <var>OR</var>, and <var>NOT</var> operators to your SQL queries. In this reading, you'll continue to explore how these operators can help you refine your queries.

## Logical operators

<var>AND</var>, <var>OR</var>, and <var>NOT</var> 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, <var>AND</var> is used to filter on two conditions. <var>AND</var> 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 <var>AND</var> operator in the <var>WHERE</var> clause:

---

```sqlite
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 <var>OR</var> operator also connects two conditions, but <var>OR</var> 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 <var>OR</var> operator to find all the needed records. As the following query demonstrates, you should place the two conditions on either side of the <var>OR</var> operator in the <var>WHERE</var> clause:

---

```mysql
SELECT firstname, lastname, email, country
FROM customers
WHERE country = 'Canada' OR 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 |
+-----------+----------+-------------------------+---------+--------------+
```

---

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 <var>WHERE country = 'Canada' OR country = 'USA'</var>.

### NOT

Unlike the previous two operators, the <var>NOT</var> operator only works on a single condition, and not on multiple ones. The <var>NOT</var> 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 <var>NOT</var> operator for this task, write the following query and place <var>NOT</var> directly after <var>WHERE</var>:

---

```sqlite
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 <var>&lt;&gt;</var> operator or the <var>!=</var> operator. For example, <var>WHERE country &lt;&gt; 'USA'</var> and <var>WHERE country != 'USA'</var> are the same filters as <var>WHERE NOT country = 'USA'</var>.

## 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, <var>NOT</var> is placed before the first condition, it's joined to a second condition with <var>AND</var>, and then <var>NOT</var> is also placed before that second condition. You can run it to explore what it returns:

---

```sqlite
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 <var>AND</var> operator requires two conditions to be true simultaneously, the <var>OR</var> operator requires either one or both conditions to be true, and the <var>NOT</var> operator negates a condition. Logical operators can be combined together to create even more specific queries.

# Join tables in SQL

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-you%27ve-already-learn"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from You've already learned a lot about" class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">You've already learned a lot about </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">SQL queries and filters. Nice work! </span></div><div aria-label="toggle video from The last concept we're introducing in" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The last concept we're introducing in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">this section is joining tables when querying a database. </span></div><div aria-label="toggle video from This is helpful when you need" class="rc-Phrase css-ugczj4" data-cue="5" data-cue-index="4" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is helpful when you need </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">information from two different tables in a database. </span></div><div aria-label="toggle video from Let's say we have two tables:" class="rc-Phrase css-ugczj4" data-cue="7" data-cue-index="6" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's say we have two tables: </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">one that tells us about security vulnerabilities of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">different operating systems, and one </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">about different machines in our company, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">including their operating systems. </span></div><div aria-label="toggle video from Having the ability to combine them" class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Having the ability to combine them </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">gives us a list of vulnerable machines. </span></div><div aria-label="toggle video from That's pretty cool, right?" class="rc-Phrase css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">That's pretty cool, right? </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-first%2C-let%27s-start-t"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from First, let's start talking about the syntax of joins." class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, let's start talking about the syntax of joins. </span></div><div aria-label="toggle video from Since we're working with two tables now," class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Since we're working with two tables now, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we need a way to tell SQL </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">what table we're picking columns from. </span></div><div aria-label="toggle video from Since we're working with two tables now," class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In our example database, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we have an employee\_id column </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">in both the employees table and the machines table. </span></div><div aria-label="toggle video from In SQL statements that contain two columns," class="rc-Phrase css-ugczj4" data-cue="22" data-cue-index="21" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In SQL statements that contain two columns, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">SQL needs to know which column we're referring to. </span></div><div aria-label="toggle video from The way to resolve this is by" class="rc-Phrase css-ugczj4" data-cue="24" data-cue-index="23" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The way to resolve this is by </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">writing the name of the table first, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">then a period, and then the name of a column. </span></div><div aria-label="toggle video from So, we would have employees followed by a period," class="rc-Phrase css-ugczj4" data-cue="27" data-cue-index="26" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">So, we would have employees followed by a period, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">followed by the column name. </span></div><div aria-label="toggle video from This is the employee_id column for the employees table." class="rc-Phrase css-ugczj4" data-cue="29" data-cue-index="28" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is the employee\_id column for the employees table. </span></div><div aria-label="toggle video from Similarly, this is the employee_id column" class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Similarly, this is the employee\_id column </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">for the machines table. </span></div><div aria-label="toggle video from Now that we understand this syntax," class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Now that we understand this syntax, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">let's apply it to a join! </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-imagine-that-we-want"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Imagine that we want to get" class="rc-Phrase css-ugczj4" data-cue="34" data-cue-index="33" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Imagine that we want to get </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a deeper understanding of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the employees accessing the machines in our company. </span></div><div aria-label="toggle video from By joining the employees and" class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">By joining the employees and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the machines tables, we can do this! </span></div><div aria-label="toggle video from We first need to identify" class="rc-Phrase css-ugczj4" data-cue="39" data-cue-index="38" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We first need to identify </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the shared column that we'll </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">use to connect the two tables. </span></div><div aria-label="toggle video from In this case, we'll use a primary key and" class="rc-Phrase css-ugczj4" data-cue="42" data-cue-index="41" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this case, we'll use a primary key and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">one table to connect to </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">another table where it's a foreign key. </span></div><div aria-label="toggle video from The primary key of the employees table is employee_id," class="rc-Phrase css-ugczj4" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The primary key of the employees table is employee\_id, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">which is a foreign key in the machines table. </span></div><div aria-label="toggle video from employee_id is a primary key" class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">employee\_id is a primary key </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">in the employees table because it has </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a unique value for every row in </span></div><div aria-label="toggle video from the employees table, and no empty values." class="rc-Phrase css-ugczj4" data-cue="50" data-cue-index="49" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the employees table, and no empty values. </span></div><div aria-label="toggle video from We don't have a guarantee that the employee_id column in" class="rc-Phrase css-ugczj4" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We don't have a guarantee that the employee\_id column in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the machines table follows </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the same criteria since it's </span></div><div aria-label="toggle video from a foreign key and not a primary key." class="rc-Phrase css-ugczj4" data-cue="54" data-cue-index="53" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a foreign key and not a primary key. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--2"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-next%2C-we%27ll-use-a-ty"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Next, we'll use a type of join called an INNER JOIN." class="rc-Phrase css-ugczj4" data-cue="55" data-cue-index="54" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Next, we'll use a type of join called an INNER JOIN. </span></div><div aria-label="toggle video from An INNER JOIN returns rows matching on" class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">An INNER JOIN returns rows matching on </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a specified column that exists in more than one table. </span></div><div aria-label="toggle video from Tables usually contain many more rows," class="rc-Phrase css-ugczj4" data-cue="58" data-cue-index="57" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Tables usually contain many more rows, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but to further explain what we mean by INNER JOIN, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">let's focus on just four rows from </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the employees table and four rows from the machines table. </span></div><div aria-label="toggle video from We'll also look at" class="rc-Phrase css-ugczj4" data-cue="62" data-cue-index="61" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We'll also look at </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">just a few columns of each table for this example. </span></div><div aria-label="toggle video from Let's say we choose" class="rc-Phrase css-ugczj4" data-cue="64" data-cue-index="63" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's say we choose </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">employee\_id in both tables to perform an INNER JOIN. </span></div><div aria-label="toggle video from Let's look at the two rows where there is a match." class="rc-Phrase css-ugczj4" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's look at the two rows where there is a match. </span></div><div aria-label="toggle video from Both tables have 1188 and" class="rc-Phrase css-ugczj4" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Both tables have 1188 and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">1189 in their respective employee\_id columns, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">so they are considered a match. </span></div><div aria-label="toggle video from The results of the join is the two rows that have 1188" class="rc-Phrase css-ugczj4" data-cue="70" data-cue-index="69" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The results of the join is the two rows that have 1188 </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and 1189 and all columns from both tables. </span></div><div aria-label="toggle video from The results of the join is the two rows that have 1188" class="rc-Phrase css-ugczj4" data-cue="70" data-cue-index="69" role="button" tabindex="0"></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-before-we-move-on-to"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Before we move on to the queries," class="rc-Phrase css-ugczj4" data-cue="72" data-cue-index="71" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Before we move on to the queries, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we have to talk about the NULL values in the tables. </span></div><div aria-label="toggle video from In SQL, NULL represents a missing value due to any reason." class="rc-Phrase css-ugczj4" data-cue="74" data-cue-index="73" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In SQL, NULL represents a missing value due to any reason. </span></div><div aria-label="toggle video from In this case, this might be" class="rc-Phrase css-ugczj4" data-cue="75" data-cue-index="74" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this case, this might be </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">machines that are not assigned to any employee. </span></div><div aria-label="toggle video from Now, let's bring this into SQL" class="rc-Phrase css-ugczj4" data-cue="77" data-cue-index="76" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Now, let's bring this into SQL </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and do an INNER JOIN on the full tables. </span></div><div aria-label="toggle video from Let's imagine we want to join" class="rc-Phrase css-ugczj4" data-cue="79" data-cue-index="78" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's imagine we want to join </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">these tables in order to get a list of users and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">their office location that also shows </span></div><div aria-label="toggle video from what operating system they use on their machines." class="rc-Phrase css-ugczj4" data-cue="82" data-cue-index="81" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">what operating system they use on their machines. </span></div><div aria-label="toggle video from employee_id is a common column between these tables, and" class="rc-Phrase css-ugczj4" data-cue="83" data-cue-index="82" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">employee\_id is a common column between these tables, and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we can use this to join them. </span></div><div aria-label="toggle video from But we won't need to show this column in the results." class="rc-Phrase css-ugczj4" data-cue="85" data-cue-index="84" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">But we won't need to show this column in the results. </span></div><div aria-label="toggle video from First, let's start with a basic query" class="rc-Phrase css-ugczj4" data-cue="86" data-cue-index="85" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">First, let's start with a basic query </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">that indicates we want to select the username, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">office, and operating\_system columns. </span></div><div aria-label="toggle video from We want employees to be our first or left table, so" class="rc-Phrase css-ugczj4" data-cue="89" data-cue-index="88" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We want employees to be our first or left table, so </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we'll use that in our FROM statement. </span></div><div aria-label="toggle video from Now, we write the part of the query that tells SQL" class="rc-Phrase css-ugczj4" data-cue="91" data-cue-index="90" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Now, we write the part of the query that tells SQL </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">to join the machines table with the employees table. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--3"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-let%27s-break-down-thi"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Let's break down this query." class="rc-Phrase css-ugczj4" data-cue="93" data-cue-index="92" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's break down this query. </span></div><div aria-label="toggle video from INNER JOIN tells SQL to perform the INNER JOIN." class="rc-Phrase css-ugczj4" data-cue="94" data-cue-index="93" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">INNER JOIN tells SQL to perform the INNER JOIN. </span></div><div aria-label="toggle video from Then, we name the second table" class="rc-Phrase css-ugczj4" data-cue="95" data-cue-index="94" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Then, we name the second table </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we want to combine with the first. </span></div><div aria-label="toggle video from This is called the right table." class="rc-Phrase css-ugczj4" data-cue="97" data-cue-index="96" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is called the right table. </span></div><div aria-label="toggle video from In this case, we want to join machines with" class="rc-Phrase css-ugczj4" data-cue="98" data-cue-index="97" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In this case, we want to join machines with </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the employees table that was </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">already identified after FROM. </span></div><div aria-label="toggle video from Lastly, we tell SQL what column to base the join on." class="rc-Phrase css-ugczj4" data-cue="101" data-cue-index="100" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Lastly, we tell SQL what column to base the join on. </span></div><div aria-label="toggle video from In our case, we're using the employee_id column." class="rc-Phrase css-ugczj4" data-cue="102" data-cue-index="101" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In our case, we're using the employee\_id column. </span></div><div aria-label="toggle video from Since we're using two tables," class="rc-Phrase css-ugczj4" data-cue="103" data-cue-index="102" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Since we're using two tables, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we have to identify the table </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and follow that with the column name. </span></div><div aria-label="toggle video from So, we have employees.employee_id. And machines.employee_id." class="rc-Phrase css-ugczj4" data-cue="106" data-cue-index="105" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">So, we have employees.employee\_id. And machines.employee\_id. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--4"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-let%27s-review-the-out"><div aria-label="toggle video from Let's review the output." class="rc-Phrase css-ugczj4" data-cue="107" data-cue-index="106" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's review the output. </span></div><div aria-label="toggle video from Perfect! We have now joined two tables." class="rc-Phrase css-ugczj4" data-cue="108" data-cue-index="107" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Perfect! We have now joined two tables. </span></div><div aria-label="toggle video from The results of our query displays" class="rc-Phrase css-ugczj4" data-cue="109" data-cue-index="108" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">The results of our query displays </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the records that match on the employee\_id column. </span></div><div aria-label="toggle video from Notice that these records" class="rc-Phrase css-ugczj4" data-cue="111" data-cue-index="110" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Notice that these records </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">contain columns from both tables, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but only the ones we've </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">indicated through our SELECT statement. </span></div><div aria-label="toggle video from There are other types of joins that don't" class="rc-Phrase css-ugczj4" data-cue="115" data-cue-index="114" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">There are other types of joins that don't </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">require a match to join two tables, and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we're going to discuss those in </span></div><div aria-label="toggle video from the next video. I'll meet you there!" class="rc-Phrase css-ugczj4" data-cue="118" data-cue-index="117" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the next video. I'll meet you there! </span></div></div>

# Types of joins

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-welcome-back.-i-hope"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Welcome back. I hope you enjoyed working on inner joins." class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Welcome back. I hope you enjoyed working on inner joins. </span></div><div aria-label="toggle video from In the previous video and" class="rc-Phrase css-ugczj4" data-cue="2" data-cue-index="1" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">In the previous video and </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">exercises, we saw how inner joins can be </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">useful by only returning records </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">that share a value in specify columns. </span></div><div aria-label="toggle video from However, in some situations, we might need" class="rc-Phrase css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">However, in some situations, we might need </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all of the entries from one or both of our tables. </span></div><div aria-label="toggle video from This is where we need to use outer joins." class="rc-Phrase css-ugczj4" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">This is where we need to use outer joins. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-there-are-three-type"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from There are three types of outer joins: LEFT JOIN," class="rc-Phrase css-ugczj4" data-cue="9" data-cue-index="8" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">There are three types of outer joins: LEFT JOIN, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">RIGHT JOIN, and FULL OUTER JOIN. </span></div><div aria-label="toggle video from Similar to inner joins," class="rc-Phrase css-ugczj4" data-cue="11" data-cue-index="10" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Similar to inner joins, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">outer joins combine two tables together; </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">however, they don't necessarily need </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a match between columns to return a row. </span></div><div aria-label="toggle video from Which rows are returned depends on the type of join." class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Which rows are returned depends on the type of join. </span></div><div aria-label="toggle video from Which rows are returned depends on the type of join." class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"></div><div aria-label="toggle video from Which rows are returned depends on the type of join." class="rc-Phrase css-ugczj4" data-cue="15" data-cue-index="14" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">LEFT JOIN returns all of the records of the first table, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but only returns rows of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the second table that match on a specified column. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-like-we-did-in-the-p"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Like we did in the previous video, let's" class="rc-Phrase css-ugczj4" data-cue="19" data-cue-index="18" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Like we did in the previous video, let's </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">examine this type of join by </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">looking at just four rows of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">two tables with a small number of columns. </span></div><div aria-label="toggle video from Employees is the left table, or the first table," class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Employees is the left table, or the first table, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and machines is the right table, or the second table. </span></div><div aria-label="toggle video from Let's join on employee_id." class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's join on employee\_id. </span></div><div aria-label="toggle video from Let's join on employee_id." class="rc-Phrase css-ugczj4" data-cue="25" data-cue-index="24" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">There's a matching value in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">this column for two of the four records. </span></div><div aria-label="toggle video from When we execute the join," class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">When we execute the join, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">SQL returns these rows with the matching value, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all other rows from </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the left table, and all columns from both tables. </span></div><div aria-label="toggle video from Records from the employees table that" class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Records from the employees table that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">didn't match but were returned through the LEFT </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">JOIN contain NULL values </span></div><div aria-label="toggle video from in columns that came from the machines table." class="rc-Phrase css-ugczj4" data-cue="35" data-cue-index="34" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">in columns that came from the machines table. </span></div><div aria-label="toggle video from Next, let's talk about right joins." class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Next, let's talk about right joins. </span></div><div aria-label="toggle video from Next, let's talk about right joins." class="rc-Phrase css-ugczj4" data-cue="36" data-cue-index="35" role="button" tabindex="0"></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-right-join-returns-a"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from RIGHT JOIN returns all" class="rc-Phrase css-ugczj4" data-cue="37" data-cue-index="36" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">RIGHT JOIN returns all </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">of the records of the second table </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but only returns rows from </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the first table that match on a specified column. </span></div><div aria-label="toggle video from With a RIGHT JOIN on the previous example," class="rc-Phrase css-ugczj4" data-cue="41" data-cue-index="40" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">With a RIGHT JOIN on the previous example, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the full result returns matching rows from both, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all the rows from </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the second table, and all the columns in both tables. </span></div><div aria-label="toggle video from For the values that don't exist in either table," class="rc-Phrase css-ugczj4" data-cue="45" data-cue-index="44" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For the values that don't exist in either table, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we are left with a NULL value. </span></div><div aria-label="toggle video from Last, we'll discuss full outer joins." class="rc-Phrase css-ugczj4" data-cue="47" data-cue-index="46" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Last, we'll discuss full outer joins. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-full-outer-join-retu"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from FULL OUTER JOIN returns all records" class="rc-Phrase css-ugczj4" data-cue="48" data-cue-index="47" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">FULL OUTER JOIN returns all records </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">from both tables. Using our same example, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a FULL OUTER JOIN returns all columns from all tables. </span></div><div aria-label="toggle video from If a row doesn't have a value for" class="rc-Phrase css-ugczj4" data-cue="51" data-cue-index="50" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">If a row doesn't have a value for </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a particular column, it returns NULL. </span></div><div aria-label="toggle video from For example, the machines table" class="rc-Phrase css-ugczj4" data-cue="53" data-cue-index="52" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">For example, the machines table </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">do not have any rows with employee\_id </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">1190, so the values for that row and the </span></div><div aria-label="toggle video from columns that came from the machines table is NULL." class="rc-Phrase css-ugczj4" data-cue="56" data-cue-index="55" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">columns that came from the machines table is NULL. </span></div><div aria-label="toggle video from To implement left joins, right joins," class="rc-Phrase css-ugczj4" data-cue="57" data-cue-index="56" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">To implement left joins, right joins, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and full outer joins in SQL, you use </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the same syntax structure as the INNER JOIN </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">but use these keywords: </span></div><div aria-label="toggle video from LEFT JOIN, RIGHT JOIN," class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">LEFT JOIN, RIGHT JOIN, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and FULL OUTER JOIN. </span></div><div aria-label="toggle video from LEFT JOIN, RIGHT JOIN," class="rc-Phrase css-ugczj4" data-cue="61" data-cue-index="60" role="button" tabindex="0"></div></div></div></div><div class="phrases" id="bkmrk-as-a-security-analys"><div aria-label="toggle video from As a security analyst," class="rc-Phrase css-ugczj4" data-cue="63" data-cue-index="62" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">As a security analyst, </span></div><div aria-label="toggle video from you're not required to know all of these from memory." class="rc-Phrase css-ugczj4" data-cue="64" data-cue-index="63" role="button"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you're not required to know all of these from memory. </span></div><div aria-label="toggle video from Once you understand the type of join you need," class="rc-Phrase css-ugczj4" data-cue="65" data-cue-index="64" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Once you understand the type of join you need, </span></div><div aria-label="toggle video from you can quickly search and find" class="rc-Phrase css-ugczj4" data-cue="66" data-cue-index="65" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you can quickly search and find </span></div><div aria-label="toggle video from all the information you need to execute these queries." class="rc-Phrase css-ugczj4" data-cue="67" data-cue-index="66" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">all the information you need to execute these queries. </span></div><div aria-label="toggle video from With this information on joins," class="rc-Phrase css-ugczj4" data-cue="68" data-cue-index="67" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">With this information on joins, </span></div><div aria-label="toggle video from we've now covered some very important information" class="rc-Phrase css-ugczj4" data-cue="69" data-cue-index="68" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">we've now covered some very important information </span></div><div aria-label="toggle video from you'll need as a security analyst using SQL." class="rc-Phrase css-ugczj4" data-cue="70" data-cue-index="69" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you'll need as a security analyst using SQL. </span></div><div aria-label="toggle video from Thank you for joining me in this video." class="rc-Phrase css-ugczj4" data-cue="71" data-cue-index="70" role="button"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Thank you for joining me in this video. </span></div></div>

# Compare types of joins

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. <var>INNER JOIN</var> returns rows matching on a specified column that exists in more than one table.

<div class="rc-CML" dir="auto" id="bkmrk-"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer"><div><figure contenteditable="false" role="figure">![Venn diagram with two circles labeled "left table" and "right table". The intersection is highlighted.](https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/9y5ZKSySQTuS5RQ-MJLXrA_6b756cb30b9442c8ae576607a6ab3ff1_CS_R-080_Inner-joins.png?expiry=1689120000000&hmac=ztPJ29SOfpwVlt4cKmV6BcvIflzMNjIGbHMDxy37yOg)</figure></div></div></div></div></div></div>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 <var>SELECT \*</var>, all columns in both of the tables are returned.

**Note:** If a column exists in both of the tables, it is returned twice when <var>SELECT \*</var> is used.

### The syntax of an inner join

To write a query using <var>INNER JOIN</var>, you can use the following syntax:

<var>SELECT \*</var>

<var>FROM employees</var>

<var>INNER JOIN machines ON employees.device\_id = machines.device\_id;  
  
SELECT thing1, thing2, thingX FROM table1 inner join table2 ON table1.commun\_colomn = table2.common\_colomn;  
</var>

You must specify the two tables to join by including the first or left table after <var>FROM</var> and the second or right table after <var>INNER JOIN</var>.

After the name of the right table, use the <var>ON</var> keyword and the <var>=</var> 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 (<var>.</var>) 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 <var>username</var>, <var>operating\_system</var> and <var>device\_id</var> columns, you can write this query:

<var>SELECT username, operating\_system, employees.device\_id</var>

<var>FROM employees</var>

<var>INNER JOIN machines ON employees.device\_id = machines.device\_id;  
</var>

<var>it makes more sense for it to be all in one row for me so heres an explanation of each part how it works  
</var><var>SELECT thing\_1, thing\_2, thing\_X FROM table1 INNER JOIN table2 ON table1.common\_column = table2.common\_column;  
</var>

**Note**: In the example query, <var>username</var> and <var>operating\_system</var> only appear in one of the two tables, so they are written with just the column name. On the other hand, because <var>device\_id</var> appears in both tables, it's necessary to indicate which one to return by specifying both the table and column name (<var>employees.device\_id</var>).

## 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, <var>LEFT JOIN</var> returns all the records of the first table, but only returns rows of the second table that match on a specified column.

<div class="rc-CML" dir="auto" id="bkmrk--1"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer"><div><figure contenteditable="false" role="figure">![Venn diagram with two circles labeled "left table" and "right table". The left circle and the intersection are highlighted.](https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/GsYCwSiOSMmymUqPUAQJ5w_5beed7e470c546fca088a83dfd9465f1_CS_R-080_Left-joins.png?expiry=1689120000000&hmac=HHSM7i9uwK-2Oq05zEf06ori3MPZ8G-R5W8fyug1QZU)</figure></div></div></div></div></div></div>The syntax for using <var>LEFT JOIN</var> is demonstrated in the following query:

<var>SELECT \*</var>

<var>FROM employees</var>

<var>LEFT JOIN machines ON employees.device\_id = machines.device\_id;</var>

As with all joins, you should specify the first or left table as the table that comes after <var>FROM</var> and the second or right table as the table that comes after <var>LEFT JOIN</var>. In the example query, because <var>employees</var> is the left table, all of its records are returned. Only records that match on the <var>device\_id</var> column are returned from the right table, <var>machines</var>.

### Right joins

When joining two tables, <var>RIGHT JOIN</var> returns all of the records of the second table, but only returns rows from the first table that match on a specified column.

<div class="rc-CML" dir="auto" id="bkmrk--2"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer"><div><figure contenteditable="false" role="figure">![Venn diagram with two circles labeled "left table" and "right table". The right circle and the intersection are highlighted.](https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/YHXRMOLiQheppUjthmM5yQ_cfb18a8315e34357bd1299f7eefafcf1_CS_R-080_Right-joins.png?expiry=1689120000000&hmac=vl4SnuhTha_RePhUQ-sb5pfEVYjfYrnCmJpUc14hKDY)</figure></div></div></div></div></div></div>The following query demonstrates the syntax for <var>RIGHT JOIN</var>:

<var>SELECT \*</var>

<var>FROM employees</var>

<var>RIGHT JOIN machines ON employees.device\_id = machines.device\_id;</var>

<var>RIGHT JOIN</var> has the same syntax as <var>LEFT JOIN</var>, with the only difference being the keyword <var>RIGHT JOIN</var> instructs SQL to produce different output. The query returns all records from <var>machines</var>, which is the second or right table. Only matching records are returned from <var>employees</var>, which is the first or left table.

**Note:** You can use <var>LEFT JOIN</var> and <var>RIGHT JOIN</var> and return the exact same results if you use the tables in reverse order. The following <var>RIGHT JOIN</var> query returns the exact same result as the <var>LEFT JOIN</var> query demonstrated in the previous section:

<var>SELECT \*</var>

<var>FROM machines</var>

<var>RIGHT JOIN employees ON employees.device\_id = machines.device\_id;</var>

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 

<var>FULL OUTER JOIN</var> returns all records from both tables. You can think of it as a way of completely merging two tables.

<div class="rc-CML" dir="auto" id="bkmrk--3"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer"><div><figure contenteditable="false" role="figure">![Venn diagram with two circles labeled "left table" and "right table". Both circles are highlighted.](https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/oRzF__GaTqSGMmUqXKbSrQ_92db9841a00244c2aa214e60bb07f1f1_CS_R-080_FULL-OUTER-JOIN.png?expiry=1689120000000&hmac=_mQrCfMdNvPXplXuBFDsTIGUpqmxB-HicarJwCeLsUw)</figure></div></div></div></div></div></div>You can review the syntax for using <var>FULL OUTER JOIN</var> in the following query:

<var>SELECT \*</var>

<var>FROM employees</var>

<var>FULL OUTER JOIN machines ON employees.device\_id = machines.device\_id;</var>

The results of a <var>FULL OUTER JOIN</var> query include all records from both tables. Similar to <var>INNER JOIN</var>, 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. <var>INNER JOIN</var> will return only these records. Outer joins also return all other records from one or both of the tables. <var>LEFT JOIN</var> returns all records from the first or left table, <var>RIGHT JOIN</var> returns all records from the second or right table, and <var>FULL OUTER JOIN</var> returns all records from both tables.

# Continuous learning in SQL

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:

<div class="rc-CML" dir="auto" id="bkmrk-count-returns-a-sing"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1k5v0wb" data-testid="cml-viewer">- <var>COUNT</var> returns a single number that represents the number of rows returned from your query.
- <var>AVG</var> returns a single number that represents the average of the numerical data in a column.
- <var>SUM</var> returns a single number that represents the sum of the numerical data in a column.

</div></div></div></div></div>### **Aggregate function syntax**

To use an aggregate function, place the keyword for it after the <var>SELECT</var> keyword, and then in parentheses, indicate the column you want to perform the calculation on.

For example, when working with the <var>customers</var> 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 <var>COUNT</var> function on any column, and SQL will return the total number of records, excluding <var>NULL</var> 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 <var>COUNT(firstname)</var> 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 <var>country</var> column contains a value of <var>'USA'</var>.

There are a lot of other aggregate functions in SQL. The syntax of placing them after <var>SELECT</var> is exactly the same as the <var>COUNT</var> 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 <var>COUNT</var>, <var>SUM</var>, and <var>AVG</var> 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.

# Wrap-up; Glossary terms from week 4

<div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-congratulations%21-we%27"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from Congratulations! We've made it" class="rc-Phrase css-ugczj4" data-cue="1" data-cue-index="0" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Congratulations! We've made it </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">together through the end of our focus on SQL. </span></div><div aria-label="toggle video from You've put in a lot of" class="rc-Phrase css-ugczj4" data-cue="3" data-cue-index="2" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">You've put in a lot of </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">work and learned an important tool that </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">will help you on your journey as a security analyst. </span></div><div aria-label="toggle video from current lecture segment: Let's take a moment to go through all" class="rc-Phrase active css-ugczj4" data-cue="6" data-cue-index="5" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Let's take a moment to go through all </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">of the topics you learned in this section. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk-we-started-by-learni"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item"><div class="phrases"><div aria-label="toggle video from We started by learning about" class="rc-Phrase css-ugczj4" data-cue="8" data-cue-index="7" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We started by learning about </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">the structure of relational databases </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and how we can access them by </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">using the query language SQL. </span></div><div aria-label="toggle video from We then got hands-on practice" class="rc-Phrase css-ugczj4" data-cue="12" data-cue-index="11" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We then got hands-on practice </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">with writing our own SQL queries. </span></div><div aria-label="toggle video from We used SQL to bring up information you might" class="rc-Phrase css-ugczj4" data-cue="14" data-cue-index="13" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We used SQL to bring up information you might </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">need on the job when working as an analyst. </span></div><div aria-label="toggle video from We then focused on SQL filters." class="rc-Phrase css-ugczj4" data-cue="16" data-cue-index="15" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We then focused on SQL filters. </span></div><div aria-label="toggle video from We started with simple conditions with strings," class="rc-Phrase css-ugczj4" data-cue="17" data-cue-index="16" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We started with simple conditions with strings, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">and by the end, we learned how to use </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">multiple filters in one query. </span></div><div aria-label="toggle video from We concluded the unit with SQL" class="rc-Phrase css-ugczj4" data-cue="20" data-cue-index="19" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">We concluded the unit with SQL </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">joins and learned how to join multiple tables, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">giving us even more information at once. </span></div></div></div></div><div class="cds-1 css-xl5mb3 cds-2" id="bkmrk--1"><div class="cds-1 rc-Paragraph css-1lz62pp cds-3 cds-grid-item">  
</div></div><div class="phrases" id="bkmrk-by-completing-this-c"><div aria-label="toggle video from By completing this course," class="rc-Phrase css-ugczj4" data-cue="23" data-cue-index="22" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">By completing this course, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">you just took a very big step in </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">your future career as a security analyst. </span></div><div aria-label="toggle video from You have been introduced to" class="rc-Phrase css-ugczj4" data-cue="26" data-cue-index="25" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">You have been introduced to </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">a powerful tool that can help you in your work. </span></div><div aria-label="toggle video from Whenever you need to," class="rc-Phrase css-ugczj4" data-cue="28" data-cue-index="27" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Whenever you need to, </span><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">I encourage you to revisit the materials in this course. </span></div><div aria-label="toggle video from Learning a querying language like SQL takes time." class="rc-Phrase css-ugczj4" data-cue="30" data-cue-index="29" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Learning a querying language like SQL takes time. </span></div><div aria-label="toggle video from Thank you again for joining me in this journey." class="rc-Phrase css-ugczj4" data-cue="31" data-cue-index="30" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">Thank you again for joining me in this journey. </span></div><div aria-label="toggle video from I hope you'll enjoy using SQL as much as I do." class="rc-Phrase css-ugczj4" data-cue="32" data-cue-index="31" role="button" tabindex="0"><span aria-hidden="true" class="cds-137 css-80vnnb cds-139">I hope you'll enjoy using SQL as much as I do. </span></div></div>### **Terms and definitions from Course 4, Week 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