1. Home
  2. Explore
  3. Computer Science
  4. SQL Course Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL Course Introduction

Transcript

Hi, my name is Mosh Hamedani, and I'm super excited to be your instructor. In this three hour course, you're going to learn everything you need to get started with SQL or SQUEL. First I'm going to give you a three minute introduction to SQL, then we're going to install the necessary tools and write our first SQL query. This course is ideal for anyone who wants to learn SQL from scratch, but also anyone with some basic knowledge who wants to fill in the missing gaps. By the end of this course, you'll be able to retrieve insert, update, and delete data in databases. We'll talk about tables, relationships, different types of joints, sub-queries regular expressions and much, much, more. These are the essential concepts every software developer or data scientist must know. This SQL course is packed with tons of exercises that help you both learn and remember the SQL syntax. Also, there is a table of content below this video so you can quickly jump to specific tutorials, now let's jump in and get started.

Let's start the course with a quick overview of databases, what they are and how we use them. A database is a collection of data stored in a format that can be easily accessed. In order to manage our databases, we use a software application called database management system, or, DBMS. We connect to a DBMS and give it instructions for querying or modifying data. The DMBS will execute our instructions and send results back. Now we have several database management systems out there, and these are classified into two categories, relational and non relational, also called NoSQL. In relational databases, we store data in tables that are linked to each other using relationships. That's why we call these databases relational databases, each table stores data about a specific type of object, like customer product, order and so on. SQL or SQUEL is the language that we use to work with these relational database

management systems. It looks like this. We use SQL to query or modify our data. In this course you're going to learn everything there is to know about this powerful language. Now, there are many different relational database management systems, out there, some of the most popular ones are MySQL, SQL Server my Microsoft, and Oracle. But of course, there are plenty more. Each database management system has a different flavor of SQL. But all these implementations are very similar and are based on the standard SQL specification. So most of the SQL code that you will learn in this course will work with any database management system. In this course, we'll be using MySQL, which is the most popular open source database in the world. Okay, now back to this diagram, what about non relational databases? In non relational databases, we don't have tables or relationships, these databases are

different from relational databases, but that's a topic for an entirely different course. What you need to know is non relational database systems don't understand SQL. They have their own query language. So, we use SQL to work with relational database management systems. Now, before we jump in and install MySql, let me clarify something quickly, as you talk to different people, you will hear two different pronunciations of SQL. SQUEL, or SQL. What is the correct way? Well, it depends on who you ask, and of course everybody thinks their way of pronouncing this word is the right way. But here's a history about this language. SQL was originally developed at IBM in the 70s and back then it was initially called SQUEL short for structured English query language. But they changed the acronym to SQL, because SQUEL was the trademark of an airplane company. So to this day, there has been an argument about what is the right

generally speaking, people in non-English speaking countries call it SQL. I'm used to calling it SQUEL because it's shorter and sweeter than SQL. But if you prefer to call it SQL, that's totally fine with me, I'm not going to get mad at you. So that's the history behind this language. But what about MySQL as a software product? Developers of this product prefer to call it MySQL rather than mySQL. But they don't mind if they call it MySQL. In this course, I'll be teaching you SQL with MySQL.

Hey guys, Mosh here. I just wanted to let you know that you really don't have to memorize anything in this course, because I've created a complete cheat sheet with summary notes for you. You can find it below this video in the description box. So I have done my best to create the best possible, most comprehensive SQL course for you, and I would really appreciate it if you would support my hard work by liking and sharing this video. Thank you so much, now, let's continue. In this tutorial, I'm going to show you how to install MySQL on your computer. Now I'm using a Mac, so first I will show you how to install MySQL on a Mac computer, and then I will show you how to install it on Windows. So if you're a Windows user, feel free to skip this tutorial. Now, open up your browser and head over to MySQL.com. Then, go to the downloads page, and scroll down to the bottom. Here you should see MySQL community edition, this is absolutely

free, and we're going to use that throughout this course. So let's go ahead and look at that, now on this page, click on MySQL, community server, and then on this page, you should see available releases for Mac OS. So, in this list, download the first item which is a dmg archive. Alright, now, on the next page, click on no thanks, just start my download. Okay, as you can see, we are getting a dmg file, which is basically a setup wizard.

Alright, now the DMG is downloaded, so let's open it, and then double click on this package, this will launch an installation wizard which is pretty easy to use, so simply click on continue and again, and again, agree with the license agreement, and install MySQL, it's going to ask for your password, this is the password that you used to log into your computer, so let's put that here, alright, now here we need to set a password to the root for the admin user. So click next, and

in this box, type a complex password, alright, now, let's finish the installation, and enter your computer's password one more time, and we are done! That was super easy and sweet. Alright, we installed MySQL community server, now we need the graphical tool to connect with this server and manage our databases. So, back to the downloads page, one more time, scroll to the bottom, and go to MySQL community addition, and on this page, somewhere

you should see MySQL workbench. This is a graphical tool that we use to connect to our database server. So let's go ahead and download this as well. Now once again on this page, we need to download a dmg archive, so, download, and, again we have to say no we don't want to login or sign up, so let's go ahead and download the dmg, and then open it, alright, you're going to see something like this, so drag this MySQL workbench and drop it onto the applications folder.

So, let's go ahead with that, now it's going to copy this into the applications folder, beautiful, so we're done with the installation, that was super easy, now press command and space, and search for MySQL, work bench, there you go, let's open it, now the first time we get this message, because this is an application we downloaded from the internet. So, we need to tell Mac that we trust this, let's go ahead with that, so this is MySQL workbench, now by default

you see a connection here, if you don't see that, you need to create it. Let me show you how to do that. So for this demo I'm going to right click this and delete this connection, alright, now let's create a connection from scratch. So click on this plus icon, on this page, give this connection a name, let's say local instance, now the connection method we're going to use is tcip, which is set by default, the host name is 127.0.0.1, which

is the address of the local machine, and the port is 33 06, this is the default port for MySQL server, that is the user name of the admin, now we need to enter the password. This is the password that we set during the installation. So, click on store in keychain, and in this box, type the password for the MySQL server. Alright and finally, let's test the connection, okay, we successfully connected to MySQL server on the local machine, beautiful, let's click okay, and here we have a connection on the homepage of MySQL workbench, every time we open MySQL workbench, we use this connection to connect to our local server. Alright, we're done with the installation of MySQL on a Mac, next I will talk about MySQL on a Windows computer, so feel free to skip that tutorial.

In this tutorial, I'm going to show you how to install MySQL on Windows. So open up your browser and head over to MySQL.com, then, go to the downloads page now here, scroll down to the bottom, we're going to use MySQL community edition which is absolutely free. So, let's go with this, now, select MySQL community server, and then scroll down so here you should see something like this, MySQL installer for Windows. This is the recommended method for installing MySQL on Windows. So,

click on this, alright, on the next page, scroll down, and download the first installer here. On the next page scroll downs nd click on no thanks just start my download. Otherwise you have to create an account and log, which is unnecessary for knowing this course, so, let's go with this, and save this file to our computer, and then run it. Alright, we're going to use this setup wizard to install MySQL, on our computer. This is very easy, all you have to do is click next,

but there are a couple places where you need to enter a password, let me show you. So, on the first page, for the setup type we're going to use the developer default setup. Go to the next page. Now here we're getting a little warning, because this installation wizard wants to install the connector for python 3.7, but I don't have Python on this machine, so that's why I'm getting this warning, now on your machine, you might or you might not get this error, just click next, and one more time, so here are the products that I want to get installed the first one is MySQL server, the second one is MySQL workbench, this is the graphical tool that we use to connect to our database server, and manage our database. You're going to see that soon. So click on execute, now this is going to take about 5-10 minutes, so, I'm going to pause the recording. Alright, all the products are installed, beautiful, let's go to the next page,

and again, here our the group replication page, also click on next, and the next page which is about networking, leave all the default settings, so, let's go to the next page, now we should set a password for the root for the admin user. So, click on next, and in this box, type a password for the admin user. Alright, and then let's go to the next page, once again leave all the default settings, and click on next, and execute one more time, alright, and now

let's finish our installation, once again we have to click on next, and then finish. One more time, there are so many steps. Now here's the page where you need to enter the admin password, so the page is called connect to server, you can see the username is root, which represents the admin user, so in this box enter the password that you said earlier, then click on check, okay, connection was successful beautiful, let's go to the next page, and click on execute, and finally finish.

There you go, we have one more step, next the finally after all these steps the installation is complete. Now, this is going to start, MySQL workbench, which is the graphical tool we use to manage our database and run SQL queries. So, click on finish, so now we have a command prompt window, where we can type instructions to talk to our MySQL server, we don't really need this, so close it, and here's MySQL workbench, now the first time you open this page,

by default you should see a connection here, if you don't see it, click on this plus icon, on this page give this connection a name, let's say local instance, now leave all their settings to their default value, but here for the password click on store involved. And in this box, type the password that you used for the admin user. So, I'm going to put that here, okay, now click on test connection, alright, we successfully connected to the MySQL server on this

computer, alright, then, click on okay, now we click on this connection to connect to our database server. Alright, so here's the interface you'll be using throughout this course, on the left hand side we have the navigator panel, in the middle we have the query editor which we can resize this is where we're going to write our SQL queries, and on the right side we have additions. So we're done with MySQL on our computer, next, I'm going to show you how to create our databases for this query.

In this tutorial, I'm going to show you how to create the databases for this course. So here I've got MySQL workbench open, let me quickly give you an overview of this interface because the first time you open it it might appear a little bit intimidating. But actually it's not that difficult. So here on the top we have this tool bar with these buttons for creating a new tab for writing SQL code as well as opening a SQL file. And next to that we've got a bunch of buttons for creating a database, creating new tables and so on. On the left side, we've got the navigator panel with two tabs administration and schemas. We use the administration tab to do administrative work, such as starting or stopping our server. Importing or exporting data and so on. The schemas tab shows the databases that we have in the current database server. So currently we only have one database, that is sis, and this is

the database that MySQL uses internally to do it's work. Now, in the middle we've got this query editor window, this is where we write our SQL code, so we'll be spending most of our time in this course, in this window, and on the right side we've got another panel with two tabs, contacts held, and snippets, now chances are this interface might look slightly different on Windows, but what I'm showing you here is almost identical to what we have on Windows. So don't get hung up if it looks slightly different on your machine. It doesn't really matter. Now up here, we've got these buttons for showing or hiding these panels, so to clean this interface, I'm going to hide this panel on the right side. As well as the panel on the bottom. That is better. Now, to create the databases for this course, download the zip file attached, below this video. When you extract the (?) you're going to see a bunch of SQL files like this. So the main one you'll be using in this tutorial

is called create databases.SQL. So this file contains all the SQL code to create all the databases that we need in this course, now we also have individual files for creating individual databases, I've just added these files in case you need to recreate one of these databases in the future. But for now don't worry about them. Now, back to MySQL work bench, let's open the main SQL file. That is create databases. So this is an example of SQL code.

Now this may look complex at the beginning, but trust me, as you go through the course you're going to understand exactly how everything works here. You're going to be able to write SQL code like this. So, you want to execute this, to create all the databases for this course. To do that, we click on this icon, this yellow thunder icon that we have on this tool bar here. This will execute either the selection or the entire code if there is nothing selected for example, if I select this line here, and click on this icon, this will execute only this line. In this case we want to execute the entire code, so we shouldn't select anything, and now let's execute this, beautiful, now here down on the bottom, we have this panel called the output window that shows all the operations perform in our database server, so we can see all the operations completed successfully, or something went wrong.

As you can see you've got these green ticks next to each operation. Beautiful, so I'm going to close this panel, that's better. Now on the left side in this schemas tab you don't see this new database so we'll have to refresh this view, beautiful. So we've got all these databases that are prefixed with SQL or SQL, I decided to prefix them with SQL so we know that these are the databases for this course, they don't accidentally clash with a database on the same name of your database server. Now, at the time of recording this video, there are only 4 databases here, but as we go through the course, I'm going to update the script for creating the databases, so, when you watch this course, chances are you're going to see more databases here. Don't worry worry about the difference. Now as an example let's explore these databases. And by the way we don't need this tab anymore so let's close it.

That's better. Let's expand the SQL store database, now in every database we have these objects, we have tables, this is where we store our data, we have views, which are kind of like virtual cables, so we can combine data from multiple tables, and put them in a view. And this is especially powerful for creating reports, you're going to learn about them in the future. You also have store procedures and functions, and these are little programs that we stored inside of our database for querying data. For example, you can have a store procedure for getting all the customers in a given city. So we call that procedure and we say hey, give me all the customers in San Francisco And this will return all the customers in San Francisco. Okay, now, let's expand the tables, so here are the tables in this database. We have customers, we have orders, products, shippers and so on. Now, select this customers table whenever you hover your mouse

over this item and see these three items on the right side. Click on the right most icon, that looks like a table with a thunder. With this we can see all the data, in this table. So this is our customers table. In this table, we have these columns like customer id which we use to uniquely identify customers. We all have first name, last name, birth date, phone, address and so on. So these are the columns in this table and every row is called a

record. So every row represents one customer and these are the pieces of information we know for each customer. Now let's look at another table, let's open the orders table, in this table we have these columns like order ID, customer ID, order date, status, and so on. What is this customer ID here. We use this column to identify who has placed each order. Now what is interesting here is you have referred to this customer using their customer ID which uniquely identifies that.

In other words, if Jon Smith has placed an order, we don't store John Smith here, we only store John's customer ID. Why is that? Here is the reason. It is possible John Smith might have placed multiple orders in our system. Now every time John places his order, we need to look up his address and phone to ship his order. Now it is possible that some of this information might change in the future. John might move to a new place or change his phone number, he might even change his name, if you repeat all that information next to each order, then we'll have to come back and make changes in multiple places. In contrast, with this design we only store the ID of john here, so anytime we want to change any information about John, instead of changing that here. We go back to our customers table, so let's look at customer with ID 6, that is actually called (?) So here is all the information about Elka. This is her phone number, this is

her address, and by the way this is all dummy data that I created using a tool. So, if any information about elka changes in the future, this is the only place that we need to modify. So this is how these databases work. We refer to these databases as relational databases. That basically means in these kind of databases we have multiple tables that are related to each other using a relationship. So internally there is a relationship between the customers table, and the orders table. So the customer ID column in the customers table is related or linked or associated with the customer ID column. In the orders table. Here's the orders table, and here we have the customer ID column. So this was a brief introduction to relational databases, you learned about databases, tables, columns, rows and relationships. In the next section I'm going to show you how to retrieve data from a single table in this database.

But, before going any further as an exercise I want you to explore the invoicing database. Look at the all the tables, look at all the data to get an id of the kind of data that we have in this database. You're going to use this database a lot in the future, so let's spend a couple minutes to explore this database. In this tutorial I'm going to show you how to retrieve data from a single panel. Now look at the navigator panel. Currently none of our databases is displayed in bold, and that

means none of these databases are selected for query. So the first step to rate a query to get data from a database, is to select a database. The query that we'll write will be executed against that database. In this demo, we're going to use the SEQUEL store database. So we type our use SQL_ store. Now use is a keyword in the SQL language, and that's displayed in blue. Now SQL is not a case sensitive language, and that means we can use upper case or lower case characters, it doesn't really

matter, but as a best practice, we should capitalize the SQL keywords, and use lower case characters for everything else. So now, let's go ahead and execute this query. Alright, Look, the SQL store database is now displayed in bold. Now in MySQL workbench, we can also select a database by double clicking that, so now I double click SQL invoice and it's the current database. Now if we run this query again, the SQL store database becomes selected.

Alright, now let's write our first query to retrieve all the customers in this database. So after the you statement we're going to use select statement. Here is the basic syntax or basic structure of the select statement, we type out select in front of that we specify the columns that we want to retrieve, for example we can retrieve the customer ID column as well as the first name column or we can retrieve all columns using an asterisk. Now after that we use the from clause and this is where we specify the table that we want to query, in this case the customer's table. So this is the simplest query to select all the customers in a given table, now whenever you have multiple SQL statements you need to terminate each statement using a semicolon. So, look, we have a red underline here that indicates an error, if you hover your mouse over here, you can see this tool tip saying select is not valid at this position.

Because we didn't terminate the first statement with a semi colon. Okay, now let's execute this query one more time, once again, we can click on this button here, or we can use a short cut, so look at the query menu on the top, the first line is execute. Now here's the shortcut for this command, on Mac, it's shift command and enter. On Windows its going to be different honestly I'm not sure. So whatever it is, use that. So I'm going to press shift command enter, and here are

all the customers in this table. So this next statement has two clauses, the select clause and the from clause. But there are other clauses we can use to filter and sort data. For example, you can use the wear clause to filter the result and get the customer with ID one. So we can write an expression like this. Where _ID = 1. Now when we execute this query, we'll only get the customer with ID 1. So this is the where clause. We can also sort the data so after wear we use the order by

clause and here we specify the columns that we're going to sort the results from on. Let's say we want to sort these customers by their first name so we type out first_name. That is the name of one of the columns in this table, right? Now if you execute this query this order by doesn't really have an impact, because we only get one record in the result so let me temporarily take out the where clause, to do that we can put two hyphens in front of this line, now this line, is treated as a comment, which means the SQL engine is not going to execute this, okay, so let's execute this query one more time, now all the customers that we get our sorted based on their first name. So that's the basic idea. Now over the next few tutorials your'e going to learn more about these clauses in detail. But what you need to take away in this tutorial is that is that these 3 clauses from where an order by are

optional. As you can see in this example, I'm not using the where clause, we can also comment out the order by clause, we can also comment out the from clause, so instead of selecting all the columns, in a given table, we can select some values like one and two. Now, if you execute this query one more time, in the result, we get something like this. Two columns called one and two, and in these columns we have these values. So all these clauses are optional but in the real world we quite often use all of them. Now what you need to understand here is that the order of these clauses matter, so we always have select first, then we have from, then where, and finally order by. We cannot change the order of these clauses, otherwise we get a syntax error. Which basically means the syntax, or the grammar or the structure of our simple statement is incorrect. So it cannot be executed. And one last thing before we finish this tutorial, you can

see I've listed all these clauses on a new line, now technically you don't have to do this, because line breaks, white spaces and tabs are ignored when executing SQL statements. So we could come back here and put from in front of select, so select store from customers all in one line, and that's perfectly acceptable for simple queries, but as your queries get more complex, it's better to put each clause on a new line. So that's all for this tutorial. In the next tutorial, we'll explore the select clause in detail.

In this tutorial, we're going to look at the select clause in detail. So, since our current database is SQL store, to clean things up I'm going to remove the first statement, you don't really need it now, also, I'm going to delete these two comments, we just want to focus on the select clause. Alright, so what can we do with this select clause? Well in the last tutorial we learned that if you use an asterisk, this will return all the columns. Alternatively we can specify the columns that we want and this is helpful in situations where you have a big table with so many columns and perhaps millions of record. If you want to bring back all that data, that's going to put a lot of pressure on the database server, in this case MySQL, as well as the network. So, that's when we explicitly specify the columns that we want to get. Let's say we want to get the first name and last name columns. Execute the query, as you can see, we only get these two columns.

And they are in the same order we specified here. So if we change the order and put the last name first and execute the query again, now we can see the last name column comes first. Now let's add a new column, at the end, let's get the points for each customer as well, run the query, so these are the points for each customer which are calculated based on their shopping. Now let's say we want to get these points and put them in a mathematical formula to calculate the discount that we can give to each customer. So here we can use an arithmetic expression, that's points plus ten. This is an arithmetic expression. So now we can execute this query for the first record you can see that their points will end up being 2283. Let's run the query one more time there you go. Now we can put the original points column here for clarity. So points, points plus 10. Let's run the query one more time, now you can see the original points, and next

to that you can see the value that we're going to use to calculate the discount. Now here we're using the plus operator which is for addition, we also have multiplication, division, subtraction, and module which is the remainder of the division. So let's change this to something more complex. Let's say we want to get the points, multiply by 10, and then add 100 to it. Now we can immediately see that this line one is getting too long and it doesn't fit on the screen, in situations like this, you can break up the select clause by placing each column on a new line, so, select last name, then first name points finally points times 10, plus 100. So let's execute this query one more time. So this is our new column with the new calculated value. Now one thing you need to understand in this arithmetic expression is the order of operators, and this is based on the order of operators in math.

So in math, the multiplication and division operators have nighter precedence then addition and subtraction, so in this expression, points is multiplied by 10 and then the result is added to 100. If this is not what you want you can always change the order by using parenthesis, as an example, let's change this multiplication to addition and then put that multiplication here. In this expression, first 10 is multiplied by 100, and then the result is added to the points.

Now let's say this is not what we want, so we can change the order by using parenthesis here. With these parenthesis, first we get the points add 10 to them and then multiply the result by 100. So this parenthesis are useful for changing the order of operations as well as adding clarity to our code. So someone else reading this code can easily understand. the order of these operations. Now let's execute this query one more time, alright, now look at the name of this column here,

its set to the expression that we have on line 5. That doesn't quite make sense you want to have a clear descriptive name, so we can give this column an alias using the as keyword. So, as and then we give it a name like discount discount _factor. Let's run the query again. now the name of this column has changed, so this is the benefit of using an alias, we can give descriptive names to the columns and the results sets. Now if you want to have a space in the

column name, you need to surround it with quotes, either single, or double quotes. So we put quotes here and then we can add a space in between these two words. Let me execute the query one more time. Now we have discount factor. So let's quickly recap everything you learned about the select clause. We cause an asterisk to return the columns, or we can explicitly specify the columns that you want to return. We can also use, arithmetic expressions here, and optionally, we can even gibe an alias to each column in the results set. Now there is one more thing you need to know about the select clause. So let's delete this query and select the state column, of the customers table. Take a look. These are the states in which our customers our located. Now currently in the sample data we don't have any duplicates, in other words we don't have multiple customer in any of these states. But for this demo I want to change the

state of the first customer to Virginia, so we end up with duplicates in the result set. So let's open up navigator panel here's our customers table, let's look at all the data, and here's our first customer, here a you can see, it's located in the state of Massachusetts now I want to change this to Virginia. So double click VA for Virginia, enter, now on the bottom right corner of the screen, you should see two buttons, apply and revert. Unfortunately I cannot show you this button

because the recording window is a bit smaller then MySQL But look down in the bottom right hand corner, click and apply. You're going to see a dial up box like this asking you to review the changes, so go ahead and click that button one more time alright now let's go back to our first query window, and execute this query one more time. As you can see, the first two customers are located in Virginia. What if you wanted to get a unique list of

states in the results set, that's when we use a distinct keyword. So select distinct set. With his query, we'll retrieve a unique list of states from the customers table. So with the distinct keyword we can remove let's execute the query one more time, now you can see Virginia is not duplicated. Alright here's an exercise for you. I want you to write a SQL query to return all the products in our database in the result set. I want to see three columns, name, unit price, and a new column called new price which is based on this expression, unit price times 1.1. So let's say you want to increase the price of each product by 10%. With this query you want to get all the products the original price and the new price. So pause the video and spend one or two minutes on this exercise, when you are done come back and see my solution.

Alright this is pretty easy, so we start with select now what columns do we want to select? Name, unit _price and then here we're going to use an arithmetic expression to calculate the new price. So we type out unit price times 1.1 and then give it a alias, o as new_price or we can put this in quotes and put a space between new and price. Now where do we want to select these columns from? From the products table, so from products. Note that I've used upper case characters for all the SQL keywords and lowercase characters for everything else. So, let's go ahead and execute tis query, this is what we get so these are all the products, you can see their original price as well as the new price which is 10 % more expensive.

In this tutorial, we're going to look at the where clause in SQL. So earlier I told you that we use the where clause to filter data. For example, let's say we only want to get the customers with points greater than 3000. So here in the where clause you can type out the condition like this. Points, greater than 3000. When we execute this query, the query execution engine in MySQL is going to iterate over all the customers in the customers table. For each customer it's going to evaluate this condition if this condition is true, it will return that customer in the result set. So let's go ahead and execute this, and here's the result, as you can see you only have two customers with points greater than 3000. So, thesis what we call the greater than operator which is one of the comparison operators in SQL. Let me show you the completeness of comparison operators, so, we have

greater than, greater than, or equal to, we have less then, less than or equal to, here's the equality operator, and for not equality, we can use an exclamation followed by an equal sign, or something like this. So both of these are not equal operators. Let me show you examples of these operators. So I'm going to delete all these and bring back the previous query, let's say we want to get only the customers in the state of Virginia. So we can change our condition to something like this.

Where state equals Virginia. Note that I've put Virginia in quotes, because this is what we call a string. A string is a sequence of characters. So whenever you're dealing with a sequence of characters, or basically textural data, we need to enclose your values with either single or double quotes. But quite often, by convention, we use single quotes so let's execute this query and here's the result, you can see we only have these two customers with ID 1

and 2, who are located in Virginia. And it doesn't matter if you use upper case or lower case characters, so if you type out the a in lower case and execute the query you get the exact same result. Now what if you want to get all the customers outside of the state of Virginia, you can use the not equal operator. So, we can either prefix this with an exclamation or use this other notation. Either way we get the same result. So these are

the customers that are not located in Virginia. Now we can use these comparison operators for date values as well. For example, let's say you want to get only the customers born after January 1st, 1990. So we change out condition to first date, greater then, once again we use quotes, for representing date values even though dates are actually not strings. But in the sequel language, we should enclose dates with quotes, so, here we type out 1990, 01 for January, -01 for

date. So this is the standard or default format for representing dates in MySQL. 4 digits for the year, two digits for the month, and 2 digits for the day. So let's go ahead and execute this query. I actually made a mistake here, so we don't see the result, instead we see the action output, or the output window. If you scroll to the bottom, you can see the details of the error. So here I used the wrong name for the column, which should separate these two words with an underscore. That is the name of our column. So, let's execute the query one more time, so we only have 3 customers born after January 1st 1990. So these are examples of comparison operators in SQL. In the next tutorial I'm going to show you how to combine multiple conditions when filtering data.

Alright, here's your exercise. I want you to write a query to get the orders that are placed this year, so look at the orders table, see what columns to do we have there, and based on that write query with a where clause. So here's the orders table, in this table we have this column, order date. We can use this problem to see the orders that are placed this year so here's our query, select, start from order where order_date, is greater than or equal to 2019, assuming this is the current yer, so 2019, 0101. Now since currently we are in the year 2019, this query will return all the orders placed this year. But next year this query

is not going to give us the right result. But don't worry about it, later in the course I will show you how to write a query to get the orders placed in the current year, so for the purpose of the new exercise, this is a valid solution. Now let's execute this query and see what we get. So, we have only one order, order ID 1, that is placed in the current year. In this tutorial I'm going to show you how to combine multiple search conditions when filtering data. So,

let's say you want to get all the customers that were born after January 1st, 1990, who also happen to have more then 1000 points. So this is where we use the and operator. So we type out and, and after we type out another condition. Like points, greater than 1000. Now when we execute this query, we only get customers who have both these conditions. Let's take a look, so execute we only have 2 customers and if you look both these people are born after 1990, and they have more then

1000 points. So this is the and operator. When we use this operator, both these conditions should be true. In contrast to the and operator, we have the or operator. So with or if at least one of these conditions is true, that row will be returned in the result set. Let's take a look. Now we execute this query again, 2 records we have quite a few records. So for example, we have this person who's not born after 1990 but if you look at their points they have more then 1000 points.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent