Comprehensive SQL Learning Journey
This detailed course offers a step-by-step approach to mastering SQL, from basic query writing to advanced data analytics and optimization.
Course Roadmap
- Basics: SQL syntax, queries (SELECT, WHERE, JOINs)
- Intermediate: Filtering data with operators, joins, functions (string, numeric, date/time), data manipulation
- Advanced: Complex queries, subqueries, Common Table Expressions (CTE), views, stored procedures, triggers, performance tuning, indexing, partitions
- AI Integration: Leveraging AI tools like ChatGPT and GitHub Copilot for coding assistance
- Real Projects: Data warehousing, exploratory data analysis (EDA), and advanced analytics
Core Learning Areas
Data Warehousing
- Understand data warehouse concepts
- ETL/ELT processes to extract, transform, and load data
- Architecture design using medallion approach (bronze, silver, gold layers)
- Data modeling: star schema for facts and dimensions
- Data lineage diagrams and documentation
Data Exploration and Analysis
- Distinguish between dimensions and measures
- Exploring data uniqueness using DISTINCT
- Analyzing date ranges with MIN, MAX, and DATE functions
- Aggregations by categories: SUM, AVG, COUNT grouped by dimension
- Ranking and segmentation using window functions and CASE statements
Advanced Analytics
- Change over time analysis using window functions
- Cumulative and rolling totals
- Performance analysis comparing current metrics with averages and previous periods using window functions like LAG
- Data segmentation using CASE statements
SQL Optimization and Performance
- Execution plans: estimated, actual, live query
- Indexing strategies: clustered, non-clustered, columnstore, unique, filtered indices
- Index maintenance: fragmentation, statistics updating
- SQL best practices: avoid SELECT *, minimize unnecessary DISTINCT/ORDER BY, limit rows for exploration
- Query writing tips: avoid functions on indexed columns, use IN instead of multiple ORs
- Using SQL hints to influence execution plans
Stored Procedures and Programmability
- Create and execute stored procedures
- Use parameters and variables for flexible, reusable code
- Implement control flow with IF...ELSE
- Incorporate error handling using TRY...CATCH
Triggers
- Automate actions on INSERT, UPDATE, DELETE via database triggers
- Maintain audit logs for data changes
Working with Views and Temporary Tables
- Create views to encapsulate complex logic for reuse and simplify querying
- Understand differences between views (virtual, no data stored) and tables (physical data storage)
- Create tables from query results (CTAS) for performance
- Use temporary tables for intermediate results within database sessions
AI-Assisted SQL Development
- Use ChatGPT for idea generation, planning, learning, and code optimization
- Use GitHub Copilot for inline coding assistance, refactoring, and comment insertion
- Prepare for interviews and exams through interactive AI sessions
Practical Insights
- Avoid over-indexing to balance read/write performance
- Regularly monitor index usage and fragmentation
- Use partitioning to enhance query performance on large data
- Design modular, reusable queries with CTEs and subqueries
- Maintain clear naming conventions and project documentation
- Build complex reports incrementally, using views for business-ready data
Project Workflows
- Data Warehouse Construction: from source data ingestion (bronze), cleaning and standardization (silver), to business-ready models (gold)
- Exploratory Data Analysis: dimensions, measures, trend analysis, segmentation, and ranking , see also Master Excel for Data Analysis: From Basics to Interactive Dashboards for complementary techniques in Excel
- Advanced Analytics: time series analysis, cumulative metrics, part-to-whole comparisons
This course prepares you to implement industry-level SQL projects confidently and efficiently, mastering both the technical and practical aspects of modern data engineering and analytics.
For a broader data processing framework complementing SQL skills, consider The Ultimate Guide to Apache Spark: Concepts, Techniques, and Best Practices for 2025.
Support the channel if you found this valuable and stay tuned for more advanced content.
Hello and welcome to this unique course to master SQL. My name is Barzalini and I lead big data projects at
Mercedes-Benz over a decade of experience in SQL data engineering, building data warehouses and data
analytics. Now, of course, the first question is what makes this course so special. Well, not only you will learn
how to write SQL codes, but more important than that, you will learn how exactly SQL works behind the scenes. So
I'm going to break complex concept in SQL using hundreds of animated visuals. This makes it really easier to
understand SQL and as well it is more fun than just sharing my screen and I just show you code. Right. The second
reason is this course is taught by me. I have industrial experience and I will be sharing with you everything that I know
about SQL and how I use it in my real projects. So I will be sharing with you hundreds of best practices, tips and
tricks and I'm going to show you my decision-m process in SQL. So by the end of this course, you will be ready to
solve any complex task like I do using SQL. So now I designed this course to cover the basics like writing your first
SQL query and then we're going to keep progressing in the course by covering advanced techniques in SQL like the
window functions, stored procedures, indexes and even at the end we're going to build a data warehouse using SQL. And
this course is suitable for anyone data engineers, data analyst, data scientist and even for students. And by the way
the good news everything is for free from the start until the ends I will be sharing with you as well a lot of
materials code presentations and animations and there are no hidden costs. So you don't have to pay for
anything. But my friends in return I really appreciate it if you support the channel in order to grow. All right my
friends I'm really excited about it. I don't know about you. If you are motivated join me learning SQL. This is
going to be amazing. So let's go. All right. Now I'm going to show you the road map in order to learn
everything about SQL starting from very basics and then advance step by step until we have very advanced topics. So
now at the start we have to understand few stuff like what is SQL, why to learn it, what are databases and the types of
databases and after the theory we're going to prepare your PC with data and the softwares. Now once we have
everything then we can go to the next chapter. This is the basics how to query data using SQL and here we're going to
cover the basic components in each SQL query like select from where those basics. Now once you understand how to
query the data, how to get the data out of the database the next step we're going to go and learn how to define the
structure of the database. How to create a new table add a new column remove column and as well how to drop a table.
So with that you are defining new stuff in the database and then the next chapter you have to learn about the data
manipulation. This time we're going to go inside the table and we're going to learn how to insert a new data, how to
update the data and as well delete few rows from our database. So with that you have the basics how to query data, how
to define the structure of your tables and how to manipulate your data. And I can say with that you cover the basics
about SQL. Now after that we start with the intermediate phase where we're going to deep dive into topics like how to
filter your data. Here we're going to learn about the comparison operators, logical operators, between and like. So
all the operators that you can use in order to build a condition in order to filter your data. Then after that it's
going to be very interesting topic. You have to learn how to combine them. And here we have two mechanism either using
the join or using the set operators. And oh my god joining data. It's going to be very interesting topic. Here we're going
to cover like a lot of stuff like we're going to start with the basic joins and then we go to advanced and then you have
to learn how to choose the right join and after that you have to learn about the set operators and here you have like
four methods union union all except intersects. So with that you learn how to combine multiple tables by combining
the columns or the rows of your tables. So this is very important. Now moving on in our course. Now using SQL you can do
a lot of stuff cleaning up the data a lot of data preparations and at the end you can do a lot of analytics and
aggregations. So there are like two families of functions. The first one is the role level functions and here we
have a lot of stuff you can transform your string values the numbers date and time and how to handle the nulls in SQL
and at the end the amazing case statements. So all those stuffs are transformation for only one single
value. We call it role level functions. And after you learn how to do data transformations, then you have to learn
about how to do data analytics and aggregations using SQL functions. So we're going to start with very basics
like the aggregate functions. And then we're going to deep dive into the window functions, analytical functions. And
here we have like aggregates, ranking and value functions. Those are very important tool for any data analyst or
data scientist doing analytics task in SQL. So I can say the rowle functions is for data engineers and the analytical
functions are for data analysts. So at the chapter 8 we can say you have covered now the intermediate level and
the last four chapters they will be the advanced stuff in SQL. So here there are a lot of techniques that you have to
learn about SQL. So the first one is the subquery query inside another query and the very famous CTE common table
expression. A lot of developers like this one and then you will learn about how to create views in the database.
This technique if you learn it you're going to be really professional in SQL. Then we're going to learn how to create
tables using select the temporal tables and then we're going to learn about the third procedures how to write a program
in SQL and after that of course comes the triggers. So those are the advanced techniques that you have to learn in SQL
in order to do advanced projects using SQL. So now once you learn all those concepts and you start writing a lot of
SQL codes you will notice that some queries going to be really slow and for that you have to learn how to optimize
the performance of your queries and here there are a lot of techniques. The most famous one is to create an index in the
database or create a partition and at the end I will be sharing with you the top 10 best practices that I have
learned in my projects on how to optimize the performance of your queries. So this is very important and
then we're going to move to very interesting one. I will be sharing with you how I use AI like shy GBT or copilot
as I'm using SQL in my projects. So here you have to learn how to write correct prompts to get assistance from AI as you
are using SQL. And finally and my favorite one it will be about SQL projects. So my friends here you have to
bring everything that you have learned about SQL in handon projects. With real projects you will get challenges and
struggle and here going to happen the magic and the real learning and here there are three types of projects. The
first one is data warehousing project. This is very data engineering focused project where you're going to learn how
to build real data warehouse where you're going to take the data from the raw formats and then process it in
different layers. Once you build it then you jump to another project. Here you're going to start exploring the data and
start getting the first insights about the business. And the last project that you can do is the advanced data
analytics project. So this is very important section where you do SQL projects. So my friends this is the road
map on how to learn SQL. So as you can see it takes you step by step from basics to intermediate and you will end
up having advanced topics and with that I can tell you you will learn everything about SQL. Okay. So now let's start with
the first chapter the introduction to SQL and here we're going to cover few topics. So we have to understand first
what is exactly SQL? Why we have to learn it? what are databases and the different SQL commands that we have in
SQL. So it is the basics the theory about SQL. So what is exactly SQL? Let's go. So what is exactly SQL? Everything
generate data and data is everywhere. Your first name is data your mobile and everything inside the mobile is data.
Car is as well generating a lot of data. Bank, your finance statements, everything is data. And now of course
the question is where do we store our data? Personally we store a lot of our data in like excels, spreadsheets in a
text file. So you store a lot of your data in different files. Now how about companies? They have a lot of things
that generate a lot of data that the products that they produce their customers as well generating a lot of
data and sales informations and a lot of things. So companies generate massive amount of data. So now the big question
is how they handle the data how they store it. Of course, they cannot go unused like simple files. They need
something bigger, stronger and smarter. And here where the database comes in. So think about the database. It's like a
container for storing data. But instead of just dumping files into folders, the database organized the data. So it is
easy to access, to manage and to search. So a database simply it is a container that stores data. So now you might ask
why we are using database. Can't we just use files like I do it personally? Well, let me tell you why we use databases.
Imagine that someone asks the following question. Go and find the total spending in your data. So now, in order for Mike
to find the total spending and the costs, he will be opening each of those files one by one, searching for the
costs trying to combine the data and it's going to be very long and messy process. But now in the other side, if
your data in database and you want to ask a question, it's going to be very easy. So all what you have to do is to
talk to the database to ask a question and the database can answer your question with a result. And now comes of
course the question how do we talk to a database? Well we use SQL. SQL is the language that you use in order to talk
to the database. It stands for structured query language SQL. And here you have people that call it SQL like me
and others that call it SQL. There is no right and wrong but if you follow me through the course I think you will
start saying SQL. So by using SQL you can ask the database you can ask your data and the database going to answer
your question by sending you a result. So this process is very easy simple and fast and this is way better than having
your data stored in different files. Another reason why we use databases is that they can handle really huge amount
of data. So sometimes we have like millions of data inside our database but in the other side if you are storing
your data inside spreadsheets and you have like massive amount of data what can happen your spreadsheets going to
just break they simply can't handle big data and another reason why we use databases is that it is just secure. It
is safer to store important and critical data inside the database than just storing it in spreadsheets and files. So
the databases are secure and you can control who is accessing what. So it is just more professional to store the data
inside a database. All right my friends so far what we have learned most of the companies stores their data inside a
container called a database and for you in order to ask questions and to talk to your database you have to speak the
language of SQL. Now I'm going to show you how it looks like usually in companies. So we
have our data inside the database and then you will have multiple people with multiple roles that are just writing
different SQLs in order to talk to the data. But now not only employees and people interact with the database. You
could build a website or an application that as well interacts with the database by sending different SQLs. And of
course, depend on how many people are interacting with the application and the website, it might generate really
massive amount of SQLs that sent to the database. And not only that, you might has as well tools in order to do data
visualizations where you have like a dashboard or reports maybe created using PowerBI or Tableau and it is used by
stakeholders and managers in order to make decisions and as well those tools will be connected to the database and
creating SQLs. So now as you can see we have a lot of interactions with the database from people applications tools
a lot of things are generating SQLs and interacting with the database but the database is just a container and storage
right so we need something a software that manage all those requests and that's why we have something called
database management system DPMS so it is a software that going to manage all those different requests to our database
and it going to make the priority which SQL must be executed First, this software can as well manage the security
whether the SQL is allowed to be executed in the first place. So my friends, the DPMS is the software that
going to manage the database. And now we are not done yet. There is something missing. So we have our data, we have
the software. What is missing here is the hardware. So in real companies, we cannot run that on our PC because first
our PC is weak and as well it goes offline. That's why we need a server. server it is like very powerful PC and
as well it lives 24/7 so it is always available and here we can decide whether we're going to have a server inside the
company or we can use cloud services in order to run our database so my friends so far what we have learned the database
it is container to store the data the SQL it is the language in order to talk to the database the DPMS it is the
manager it manage the database and the server it is the physical machine where the database lives so this is how it
looks Like and now my friends there are different types of databases. So let's
see what do we have. The first and the most famous one it is the relational database. It is very simple. It is like
spreadsheets call them table where we have columns and rows and then there is like a relationship between those tables
to describe how they relate to each other and that's why we call it relational database. So if people hear a
database they're going to think about this one. Now we have another type of databases called key value. This time
the data is organized completely different where you have pairs of keys and values. Think about it. It's like a
big dictionary where you have a word like the key and the definition of the word this is the value. And now moving
on to the next one. This is as well important column based. So now instead of grouping the data by the rows this
type of databases group the data into columns. That's why it's called column paste. And this is very advanced
database in order to handle huge amount of data where the main purpose is to search for data. Moving on to another
database called graph database. The main focus here is the relationship between objects. So the main idea here is how to
connect my data points. And now finally we have the document database. The data is stored as entire documents where the
structure of the data is not that important. What is more important is to fit everything in one page in one
document. And now if you look to those five types, we can group the document, graph, column based, key value, all
those databases called NoSQL databases and the relational database, SQL database. And in this course, we will be
focusing of course on the relational database. And I'm sure you have heard about like the Microsoft SQL server, the
MySQL, the possesses they are SQL relational database. And for the key value you have
the radius the Amazon Dynamo DB and we have for the column paste we have the Cassandra and the red shift. For the
graph database we have the Neo 4G and the very famous database the MongoDB as a document database. Now my friends for
this course we're going to be focusing on the SQL relational databases because it is the most famous one and the most
used one in companies and I will be focusing on the Microsoft SQL server. So those are the different types of
databases. Now the databases are very structured and organized. It has the following
hierarchy. The starting point is the server as we learned it is powerful PC and it is where the database lives and
inside it we can have multiple databases. So maybe you have a database for the sales and another one for the
HR. So the server can host multiple databases and as we learned a database is a container of your data. Now moving
on to the next level. In each database we can have multiple schemas. A schema it is like category or you can call it a
logical container that we can use it in order to group up related objects like let's say you have hundred of tables. So
you can split all the tables that has to do with the orders in one schema and then another group of tables with the
schema customers and so on. So it help you to organize your tables and your objects in the database. And now if you
go inside schema you can have multiple objects like tables. So now of course the question is what is a table? It is
like spreadsheet. It organize your data into columns. The column define the data that you store inside it. So you have
one column about the customer ID. Another column about the names, the scores, the birthday. So each column is
about one type of data and sometimes we call the columns as fields. Now the other thing that we have in tables is
the rows or sometimes we call it records. It is where actually the data is stored. Now in this example each
record represent one customer one person. So we have one record for Maria, John and Peter. Those we call them rows.
Now in each table there is like one very important column called the primary key. It is always very important to have like
one unique identifier for each customer for each row and we use it for different purposes in order to combine it with
another table in order to identify quickly one customer. So it is unique. It's like fingerprint and there is no
two customers having the same ID. Now the overlapping between the columns and the rows we have a single value a cell
and each value each column stores specific data type. A data type it is like what kind of data we are storing
like an integer 1 2 30 or a decimal where you have a decimal point 3.14. Now if you want to store characters we have
different data types for that like you want to store the name or the description. So here we can use the char
or the vchar. So you store inside them like the first name Maria or something. Now you might ask what is a char or
vchar. So the char always a fixed one. So if you define it like five characters always it's going to go and reserve five
characters from the space. But if you want things more dynamic then you go with the vchar. And now moving on we
have another data types called the date and time. So if you want to store a date like the birth dates and if you want to
store the time information you can use the time data type. So we call those stuff int, decimal, char, date, time.
They are data types. So my friends, as you can see, SQL databases are very organized and
structured. Okay. So now let's focus more about the SQL itself. We have in SQL different type of commands. So let's
say that we have a database and this database is empty. So we have nothing inside it. Now, of course, the first
thing that you have to do is to write an SQL with the command create in order to create brand new table in the database.
So, once you executed the database going to go and build one, but this table is empty. So, we have nothing inside it. So
now what you have done here is you have defined something new, right? And we call this type of commands the data
definition language, the DDL. We have create to create something new, alter in order to edit something that already
exists and drop in order to delete something. to drop for example a table. So this is the first family of commands.
Now if you look at our table, it is empty. What do we need? We need data. So let's say that we have a website or an
application. Now this application is generating a lot of data. Now in order for this application to move the data
inside our new table, it must use the SQL command insert. So if you execute insert, you can add a new data inside
your table. This type of commands we call it data manipulation language. And here we have three commands. insert in
order to insert a new data, update in order to update an already existing data and delete in order to go and delete
data from your table and that's why we call it data manipulation language because you are manipulating your data.
So what do we have now? We have table, we have data inside the table. Now what we can do we can start asking questions.
So let's say that you have analytical question about your data. Now all what you have to do is to write something
called SQL query and inside it you use the command select but the whole thing we call it a query. So you send a query
to the database, you have a question and the database can return for you the result, the data answering your query,
your question and we call this type of activities using SQL, the data query language. And here we have only one and
it is very famous. We have the select. We can use it in order to query our data. So those are the three different
commands in SQL. And of course, we're going to learn all of them, but we will spend most of our time learning how to
write the correct query for the correct answer. And now you might ask me, Barra, why we have to learn SQL? And if the
time goes back, are you going to learn SQL again? Well, for sure, of course. And here are the top three reasons that
I have. The first one, you have to learn it in order to talk to the data. You know, most of the companies stores their
data in databases, and this is a standard way. This is how they do it. And if you want to work on the company
in the data field and you want to talk to their data, then you have to use SQL. It's like you move to another country
where they speak another language and you want to live there for a long time, you have to speak their language. The
same thing here. If you want to work with data, you have to learn the language in order to speak to the
database, the SQL. So this is for me the most important reason why we have to learn SQL and SQL it is in high demand.
If you go now and check the job description of the software developer, data analyst, data engineer, data
scientist, I promise you you will find there that they going to demand for SQL. So you will find they going to ask for
SQL skills almost in each job description. So if you check for any data related jobs, you will find that
they going to ask for SQL skills. Now another reason that I have is it is industry standard. So if you go and
check multiple modern data platforms and tools like PowerBI, Tableau, Kafka, Spark, Synaps, you will understand that
there will be always a section where you have to enter SQL code. So most of those vendors adopt SQL because it is the
standard. It is widely used. It is like selling points that their tools are easy. So those are my top three reasons
why SQL is still relevant and why you have to learn it. Okay, my friends. So with that we have now clear
understanding what is an SQL why we need it what are databases and their different types why do we have DBMS
servers and as well now you have understanding how things are very organized and structured inside the
databases so that's all this is SQL all right so with that we have covered the basics about what is SQL and databases
now in the next step we're going to go and set up our environments so that means we're going to prepare your PC
with the data with the databases and all the tools that you need in order to learn
SQL. Okay. So now go to the link in the description and you will land here in my newsletter website and you can subscribe
if you want to get weekly news about my content. I make as well post about data and many other projects. So once you do
that what we're going to do now we're going to go to the downloads over here and you will find here all the materials
of different courses and the one that we want is SQL ultimate course. Let's go over here. Now once you do that you will
land to this page where I have listed all the important links. So the first one and the most important one is to go
and download the course materials. Here you can find everything code the slides the presentations the whole course or if
you don't want that you can go to my get repository and there you will find exactly the same materials. So let's go
and download everything. Okay. So now go and put the downloaded folder somewhere safe and let's go inside it. And here
you can find three things. The first one is the data sets. Here if you go inside it you will find the data for the course
the databases that we will be using in order to practice SQL. So everything is available here. Now the second folder
you can find all the documentations. So that means all the visuals the presentation slides everything that I
present during the course. It is available here as a documentation notes for you. Now moving on to the third one
we have the scripts. So during the course we will be writing a lot of SQL codes and all those codes are here
available. So that means those are all the codes that is used in the course. Okay. So with that you have now all the
course materials. All right. So now the next step is that we have to go and download the SQL Server Express and you
can find the link as well over here. So let's go there SQL Server Express. And now we're going to land on the Microsoft
page where we can see the different offering from Microsoft where it's called server. So either we have it on
the Azure or we can download it on the on premises. But we don't want those stuff. Just scroll down to see those two
options. So the first option on the left side we have the developer edition. You will get all the features and services
that Microsoft offers with the SQL server. It is as well free but the installation here is little bit
complicated. But in the second option on the right side we have the express edition. Installation here going to be
really fast and very easy. You will get as well all the stuff that you need for practicing SQL and learn SQL. So both of
the options are free. It's just a matter of the installation. We will go now for the express edition. So go and click
download now and it's very small file. So let's go and start it. And now the installation going to start. So we have
basic, custom and download media. So download media means download now and later we're going to do the
installation. Custom means we have more control on how to download and install the stuff. The basic is the easiest one
and the quickest one. So let's go with the basics and click on that. And let's go and accept all those stuff. And now
let's click on install. So now it's going to install the applications, drivers and so on. It may take a little
bit time. So in order to do that, let's go and click on install SS SMS. So let's
click on that and as well we can find the link over here. So let's go to SQL Server Management Studio. So let's click
on that. You can find of course this link as well with the other links that I have collected. So now we are again at
Microsoft page. Let's go scroll down and now we will see the following link free download for SQL Server Management
Studio SS SMS. So let's go and click on that and then it's going to go and download it. Let's go and start it. So
the first thing that we have to define the location. I will go with the default stuff. So let's click on
install. Okay. Setup completed. We just installed SM SS SMS. So let's go and close it. So now let's go and start it.
If you go to your menu over here, search for SQL Server and you will find it here. SQL Server Management Studio.
Let's go and start it. Okay, so now we're going to get this window in order to connect to our server. So again, what
is our server? It is the one we have installed at the first step, SQL Server Express. And that's why you're going to
see in the server name, your PC name, of course, like it's not going to be my PC name. But here we have something called
SQL Express. This is the server we just installed. So in the first option, we have database engines. We have reporting
services. Those are different stuff from Microsoft. We're going to leave it as a database engine. And it should be like
this. SQL Express. Now, how to access this database? We have the following stuff. We can do that using the window
authentications or SQL server authentications. I'm going to say that let's stick with the window
authentication. And the username going to be the PC name and as well the window user. If you don't have it for some
reason those informations, you can go to your search search for cmd and then here you can say who am I?
And with that you will get the PC name and as well the user that you are currently logged in. And this is exactly
what I'm seeing over here. One more thing if you're having issue connecting to your database make sure to check the
encryption. It should be mandatory and to click on the trust server certificates. So once you do that you
will be able to connect. Okay. So with that we have the server we have the client. And now the last step we have to
go and create the database. We want to insert our data. So now if you look to the object explorer and open the
databases you can see that we don't have any database. So now let's do something about it. Go back to the course
materials inside the data sets you will find the following. You will find we have here three folders MySQL postcress
and SQL server. So if you want to follow with this course using different database like MySQL and Postgress you
can find the exact same data for the database that you are using. But now in this course we are using the SQL server.
So if you follow me with that go inside the SQL server folder and here you will find four files with different
extensions. So what is going on here? Now for this course we have two databases. One that is very simple
called my database and second one that has more tables called sales DB. And now in SQL server there are multiple ways on
how to create databases. I will show you now two methods on how to create the database. Now the first option we want
to create the database from a script. And if you look to those files, we have here two files with the extension SQL.
Those are files with SQL code. So let's start with the first one, the init SQL server my
database.SQL. Go inside it. And now here we have the SQL code. Copy everything. And now let's go back to our studio and
then go to the menu and click on new query. And here in the middle you can paste the code. So now we have the code
for the first database. And all what you have to do is to go and execute it. So once we executed you will see we will
not get any error. And now on the left side we don't see yet our database because we have to refresh. So right
click on the databases and click refresh. And now you can see it my database. So now let's see the content.
Go extend it and then go extend the tables. And now you see here our two tables customers and orders. Inside
those tables we can find our data. In order to see the data right click for example of the customers and let's go
with the option select top 1,000 rows. Once you do that you can see now in the results we have here five customers.
This is our data inside the table customers. So here again about the interface on the left side we have the
object explorer where you can see the whole structure of the database from server to databases to tables. So you
can see the whole structure on the top we have a menu with a lot of icons and then in the middle this place here we
call it the SQL editor. We're going to go and write their SQL codes and then once you execute it at the bottom you
will get the result and messages and below the SQL editor we have the output. So here you can see for example the data
the results or different messages from the database. So the interface is very simple. Now we have to go and get our
second database. So if you go back to our files you can find a second SQL file the initql server sales db.sql. Open
that and let's go and copy everything here and let's go back to our studio. Same thing you have to go and create a
new query then paste the whole code and this database is about the sales DB. So let's go and execute it and with that we
will not get any errors and now we go to the left side and we do the same thing refresh and we can see the second
database sales DB. Now we can go and explore it. So extend it go to the tables and here you can see five tables
customers employees orders products. So here this is the intermediate database for our course. So now let's go and
check our data. For example, let's go to the orders, right click on it and select top 10,00. And those are the orders of
our database. Perfect. So everything is working. So those are the main two databases that we will be working
through the whole course. And of course if you want to go and practice using another database, it's totally fine. For
example, in Microsoft, there are a database called Adventure Works. It is really amazing. And I'm going to show
you now how to import it. We can go over here the adventure works. So let's click on this link. So now we are again in
Microsoft page. If you scroll down you can see here three different types of databases. The OLTB, data warehouse and
lightweights. So they are like different databases. The OLTP is the most like complicated one. A lot of tables and
transactions and so on. The data warehouse it is like really nice one in order to do data analyzes and stuff. The
lightweight it is the simplest one. So let's go for example and get the data warehouse. So click on that and now as
you can see the extension of this file isbak and now I'm going to show you the second way on how to create databases in
SQL server. So now all what you have to do is to go to the following path. It really depends where you have installed
the SQL server. So for me I have installed it in the program files Microsoft SQL Server MSSQL SQL Express
then MSSQL backup. You have to go there. So here what you can do you can place all the files with the extension bak.
For example, the adventure works that we just installed. This is a backup file for the database and we want to go and
restore it and with that you are creating like a database. So this is the second method on how to create databases
in SQL server by restoring the database. If for some reason the script didn't work for you. Now let me show you
quickly how we can do that. Let's go back to our studio. Right click on the database and then here we have an option
called restore database. Click on that. And now here we have two options under the source database and device. The
default going to be database but we have to switch to a device because we want to import it from files. And then we go to
these three dots. Click on that. And now we have to go to the option add. And now it's going to take you to the place
where the SQL server creates backups. So here we can find our files and what we want you to create is the adventure
works. Select that. Then okay, one more okay and one final okay. So now the database will be restored and it is
successfully. So now on the left side we can see our third database. If you don't see it go and refresh of course and here
you will find a lot of tables in the adventure works. And as usual we can go and explore the data by selecting top
thousand rows. So my friends now you have three databases but of course our focus is only the first two that we have
done my database and sales DB. And with that you have learned two ways on how to import databases into SQL server. So
with that my friends we have prepared everything. We have the SQL Server Express running on your local PC. We
have the studio the clients where we're going to use it in order to interact with the database and we have created
our two databases that we will be using in order to practice SQL. So we are ready. All right my friends. So with
that we are done with the first chapter. We have our introduction to SQL and now we're going to start learning the first
thing in SQL and that is how to query our data. So let's go and start with that.
Okay, so now we can understand exactly what is an SQL query. Now normally your data is inside the table and your table
is inside the database and now you might have a question from the business like what is the total sales? What is the
total number of customers? So any question that you have in your mind and you want to go and ask your data you
want to go and retrieve data from the database and in order to do that you have to talk to the database using its
language the SQL. So in order to do that you're going to go and write a query where you write inside the query
something called select statement and with that you are asking the database for data. So once you execute your query
the database going to go and fetch your data and then it prepares a result to be sent back to you. So with that you are
asking the database a question by writing a query and the database going to process your query and answer your
question by sending back data and with that we are like reading our data from the database and the queries will not
modify anything will not change the data inside your tables or even change the structure of the database. So you use
select statement only in order to read something from the database. You just want to retrieve data from the database.
So this is what we mean with a query. And now my friends, each SQL query has usually different sections,
different components. We call them clauses. And this is amazing because you're going to have enough tools to
write a query that matches any question that you have about your data. So what we're going to do, we're going to cover
all those clauses step by step in order to write any query that you need. So now we're going to start with two clauses
that makes the simplest query in SQL. the select and from. So let's start with that. All right. So now it's really
important for me that you understand how SQL works with the code with the queries. So now what I'm going to do,
I'm going to show you on the right side the syntax of the query in SQL and then on the left side I'm going to show you
exactly step by step how SQL going to go and execute your query. So now we have the table customers inside our database
and we will start with the easiest form where we're going to select everything. Select the star. So the select star is
going to go and retrieve all the columns from your table. So everything and the from clause it's going to tell SQL where
to find your data. So with the select we select the columns that we want and the from you specify the table where your
data come from. So the syntax going to be very simple. In each query we start always with the select. And now since we
want all the columns we're going to write star and with that SQL going to understand I want to see everything. And
then after that comes the keyword from. And now we want to tell SQL where the data come from. So we have to specify
the table name. And that's it. This is all what you need to do. So once you execute it what's going to happen? SQL
going to go and execute first the from clause. So it's going to go and retrieve all the data from the database to the
results. And then in the next step going to go and check the select statement. So which columns we have to keep in the
result since you are saying star then the SQL going to keep everything all the columns and with that you will see in
the result everything all the columns and all the rows. So that's it. This is how it works. Now let's go back to scale
in order to select few data from our database. Okay. So back to our studio. Let's go and start a new query and let's
go and find our database just to expand it and our tables. Now it is very important to make sure that you are
connected to the correct database. So go to the top left in the menu over here and make sure to select your database.
So my database like this or we have a command for that called use and then just write the database name like this.
So I'm telling SQL just use my database like this and with that SQL going to switch to your database. Now if you are
learning any new programming language, it is very important to understand about the comments. So comments are like notes
that you add to your code in order to understand what is going on. And of course the engine, the database will not
go and execute it. it's going to go and ignore everything inside it. And there is like two ways on how to do that.
Either you make inline comments by typing two dashes like this and then you write anything this is a comment. So now
in SQL if you see it is green that means it is a comments. Now the other type you can have multiple line comments and in
order to do that what you can do you can write slash and then start and then you can write anything this and then start a
new line is a comment. So as you can see all the lines after the slash star it is getting green that means it is a comment
and now let's say that you are at the end. So in order to close it you write again star and then slash and that you
are telling SQL I'm done with my comments. So those are the two types of writing comments in SQL. Now back to our
query. Let's say that we have the following task says retrieve all customer data. So I would like to see in
the results all the data of my customers everything all the rows and all the columns. So currently our data is stored
inside the table called customer and I need to see all the data in the output. In order to do that we're going to write
a query and all our query start always with a select and since I need everything all the columns we write star
and then a new line. Let's go and specify for SQL from where it's going to go and get the data. So it's going to be
from and then we going to write the name of the table. It must be exactly like it is in the database. So it's called
customers and you have to have it here as a customers. So that's it. Let's go and execute it. And now if you look to
the results, you can see we have four columns and five rows. So with that you are seeing everything inside the table
customers. You can see we have five customers and you can see all the columns about the customers. So this is
very simple. We have ask question for the database using SQL query and the database should answer our question by
returning our data in the results. All right. So now let's move to another task. I'm going to go and create a new
query and this time we're going to retrieve all the order data. So that means I would like to see all the data
inside the orders. So let's go and write a very simple query. We start as usual with select and since we want
everything. So it is select star from our table orders. So that's it. Let's go and execute. And with that you can see
in the output we have again four columns but this time we have only four rows. So that means in this table we have four
orders and we can see all the data inside this table. So with that we can understand we have five customers inside
our database and these customers did generate four orders. So as you can see we are now talking to our database and
this is the simplest form of query in SQL. All right. So now let's move to the next step in our query where you say you
know what I don't want to see all the columns from the database. I want to be more specific. So I would like to select
exactly the columns that I need. So now we want to select few columns from the database where we select only the
columns that we need instead of everything. Now about the syntax we're going to go and change a little thing.
So instead of using star we're going to go and make a list of columns that we want to see in the output. So we're
going to select column one column two and we're going to separate them using a comma. So we are just writing a list of
columns exactly after the select. And for the from it's going to stay as it is. So from a table. Now if you execute
this what going to happen as usual SQL going to start with the from. So it's going to go and get the data from the
database and then the next step is going to go and check the select. So what going to happen? SQL going to go and
keep only two columns like for example the name and the country and all the columns that are not mentioned in the
select statements will be excluded. So SQL going to go and remove it from the results and keeps only the columns that
we mentioned in our query. So this time instead of having four columns in the output we can have only two. So with
that you are like filtering the columns and you are selecting exactly what you need. So now let's go back to SQL in
order to practice this. All right. So now we have the following task and it says retrieve each customer's name,
country and score. So that means I don't want to see everything from the table customers. I need only to see the three
columns. So let's see how we can do that. As usual we start with select and I'm going to go with a star in order to
see the whole table first from the table customers. So it's exactly like before. Let's go and execute it. And now I can
see everything inside the table customers. But the task says I need only three columns. So now what we're going
to do instead of the star, we're going to make a list of columns. So we start a new line and then we write the name of
the first column. So the first name and a new line for the second column for the country and then again a comma and then
we write a score. So with that we have the three columns. Now what I usually do, I go and select them and give it
then a push using a tab. This just looks nicer and easier to read. So with that we have now between the select and from
list of columns. Now there is like mistake that happens a lot where we go and type a comma after the last column.
So if you do that and execute it you will get an error because SQL going to expect from you a column after the comma
and since there is no column and immediately you have a from you will get an error. So there is no need for a
comma after the last column. Now let's remove it and execute. And now that you can see in the output we don't have four
columns we have only three. the first name, the country and the score. And by the way, they are ordered exactly like
you selected in your query. So first we have the first name and then the country and then the last one the score. So that
means if I go and now change the order. So let's get the country at the end and execute. You will see the country at the
end. I'm going to go and put it back in between to match exactly like the task and remove the last comma. So execute
again. And with that we have selected few columns from our table. So we are more specific to what we need. Okay. So
that we have covered the two select and from next we're going to talk about the wear clause that you can use in order to
filter your data. So let's go. So what is exactly where? We use where in order to filter our data based
on a condition and any data that fulfill the condition going to stay in the output in the result and the data that
don't meet the condition will be filtered out of the results. Condition could be anything like for example we
say the score must be higher than 500 or you can say the country must be equal to Germany. So any condition that you have
in your question. Now let's see the syntax in SQL. As usual we start with a select. We select the columns that we
need. Then we write from where the data come from and then after the from we're going to write the where and exactly
after that you specify your condition. So now let's see how SQL going to execute this. First SQL start as usual
from the from. So it's going to go and get your data from the database and after that SQL going to go and execute
the wear clause. So let's say that the condition should be higher than 500. And now what going to happen? SQL going to
check each row whether it meets this condition or not. So for example for Maria she doesn't fulfill the condition
because her score the 350 is not higher than 500. So she doesn't fulfill the condition and SQL going to go and remove
completely this row this record from the results. Now SQL going to go to the second record. So Joan is fulfilling the
condition. So he going to stay in the result. The same thing for George. Now moving on to the fourth one Martin. So
this customer is not fulfilling the condition and SQL going to go and remove it from the results. The same things
happen for the last customer. The score is zero and not fulfilling the condition. So that means if we apply
this filter, SQL going to return only two customers out of five. So with that we are filtering the rows based on
condition using the work clause. Now as you can see in the result we are getting all the columns but if you specify in
the query like for example only two columns like the name and the country then SQL going to start removing as well
the columns of the results. And this means in the output we will get only two columns and two rows. So with that you
are filtering the columns and the rows of your results. So now let's go back to scale in order to practice this. All
right. So let's have the following task and it says retrieve customers with a score not equal to zero. So now if you
are looking to our task you see we have like here a condition. The condition says the score must not be equal to
zero. So I don't want to see all the customers. I want to see only the customers thus fulfill this condition.
So it's like we have to filter the data. So let's go and solve the task. Let's start as usual. Select star. There's no
specifications about the columns from our table customers. Okay. So I'm going to start with this. Let's go and execute
it. Now if you look at the result, you can see like almost all the customers are fulfilling the condition. Their
scores are not equal to zero. Only one. The last customer his score is zero. So this customer does not fulfill our
condition. Now let's go and build filter for that. So we're going to say where. And now there will be a section that is
only focusing on how to build conditions and filtering in SQL. So don't worry a lot about the syntax of the conditions.
We're going to cover that later of course but it is very simple. Now for the condition we need a column. So in
which column is our condition based on it's going to be on the score. So we're going to write here score and since we
are saying not equal there is like an operator in SQL called not equal and then we have to write a value after
that. It's going to be a zero. So again the condition is like this. The score must not be equal to zero. It's very
simple, right? And with that we have our condition and we are using the where in order to filter the data. So let's go
and execute it. And now as you can see SQL did remove the last customer because he is not fulfilling this condition. And
we have now only the rows that fulfill our condition. So as you can see it is very simple how to filter the data. All
what you have to do is to write where clause after the from and then write a condition after that. Now let's have
another task like for example it says retrieve customers from Germany. So I don't want to see all customers from
different countries. I just want to see the customers that come from Germany. So that means we have a condition here.
Country of the customer must be equal to Germany. So let's go and remove the current condition. It is not the one
that we need and execute. If you are looking to the results, we have two customers that come from Germany and we
are interested only to show those two customers. So let's go and make a filter for that. We're going to write where
clause and after that we need a column. The column going to be the country. So we're going to write here country and
this time the country must be equal to Germany. So we're going to write an equal operator. So we're going to write
Germany like this exactly like the value inside our data. But now as you can see we are getting like an error here. And
that's because in SQL if you want to write a value that contains characters then you have to put it between two
single quotes. So at the start you put a single quote and as well at the end. And now as you can see the red line is away
and the value now is red and that's because it is a string value. It is a value that contains characters and with
that you will not get an error. So if your columns contains only numbers you can write it without single quotes. But
if your values contains characters then you have to write it between two single quotes. Okay. So now back to our
condition the country must be equal to Germany. Let's go and execute it. And it is working. So as you can see now we are
seeing in the output only the customers does fulfill my condition where the country is equal to Germany. So this is
exactly how we work with the wear clause in order to filter our data. So my friends this is how you filter your
rows. And now let's say that I would like to filter the rows together with the columns. So I just want to keep the
first name and the country and not interested to see the scores and the ids. So in order to do that we're going
to go to the select and list the columns that we want to see. So the first name and after that a comma then the country
and that's it. So let's go and give it a push and execute it. So we have two rows and two columns. So guys as you can see
SQL is very simple. All right. So with that you have learned how to filter your data using the wear clause. Next we're
going to talk about how to sort your data using the order by. So let's go. Okay. So what is exactly order by?
You can use this type of clouds in order to sort your data. And of course, in order to sort your data, you have to
decide on two mechanism. Either you want to sort your data ascending from the lowest value to the highest value or
exactly the opposite way using descending from the highest value to the lowest. And the syntax kind of looks
like this. So as usual, we start with the select and then from and after the from you can specify order by and with
that you are telling SQL we have to sort the data and you have to specify two things. First you have to specify for
SQL the column that should be used in order to sort the results. So for example you can say score and after the
column name you have to specify the mechanism. So for example you say ascending from the lowest to the
highest. And in SQL if you don't specify the mechanism the default going to be ascending. So you will not get an error
if you don't specify anything after the column name. But my advice here is always to specify something after the
column easier because it's just straightforward and easier to understand and if someone reads it can understand
immediately it's going to be ascending because maybe not everyone knows what is the default in SQL. So always specify a
value even if it's like easier to skip it and if you want to store the data from the highest to the lowest then you
can specify descending. So as usual SQL going to go and start from the from it's going to go and grab your data from
database. Then the second step is SQL going to go and sort the result. So the order by going to be executed and SQL
going to see okay I'm going to sort it by the score and using the sending mechanism and still going to go and
start like moving around your rows where the first row going to be the customer with the highest score and in this
example John has the highest score the 900. So John going to appear as a first row at the result and that's because his
score and after that the second highest is going to be George with 750 and SQL going to go and keep sorting the data
and then we have 500 then 350 and the last row going to be the customer with the lowest score the zero. So this is
how SQL executes your order by. Now let's go back to scale in order to practice. All right. So now we have the
firming task and it says retrieve all customers and sort the result by the highest score first. So now by looking
at the task we need all the customers. So there is like no conditions or anything to filter but we have to sort
the results. So let's go and do that. We're going to start as usual by selecting all the columns from the table
customers. So now if you go and execute it you will get all your customers and you are now seeing the data exactly like
stored in the database. And you can see the result is not sorted by the scores. So we have here a low score then high
score then low and so on. Now the task says we have to sort the results. So we have to go and use the order by and now
you have to understand from which column and we can get that from the task. So it says it should be sorted by the score.
So we're going to go and define the score here. And the final thing that you have to define is the mechanism
descending or ascending. And you can get it as well from the task. So we have to sort the data by the highest score
first. So the highest first and then the lowest. So that means we're going to go and use the descending. So that's all.
Let's go and execute it. Now as you can see in the results, the first customer has the highest score. Then we have the
second one with the second highest until the last one with the lowest score. That's it. This is how you sort your
data. And with that we have solved the task. Now let's do exactly the opposite. So we want to sort the results by the
lowest score first. So that means we want to see first the customers with the lowest score like here in this example
we should see the ID number five as the first because he has the lowest score the zero. Now in order to do that all
what you have to do is to switch the mechanism instead of descending when you can use ascending. Let's go and execute
it. And that's it. As you can see now we have the lowest score then the second lowest score until the last row. It's
going to be the customer with the highest score. So the lowest score comes first. So it is very simple. This is how
you sort your data using SQL. And now I'm going to show you one more thing that you can do with the
order by. You can sort your data using multiple columns. And we call it nested sorting. So now let's take this very
simple example where you want to sort your data using country. So we are saying order by the column country and
the mechanism going to be ascending. So from the lowest to the highest. Now if you do that going to go and sort the
data this time based on the country. So we're going to have like the first two customers from Germany. It is sorting it
alphabetically. Then we have the UK and the last two going to be from USA. Now if you are checking the final results
you might say you know what there is like something wrong. The data is not completely sorted correctly. So if you
are looking to the first two customers that come from country Germany. You can see the scores are sorted in ascending
way from the lowest to the highest. So first we have 350 then 500. Then UK it's fine because we have only one customer.
Now if you look to the customers from USA you see that it is like sorted the way around. It is sorted descending from
the highest to the lowest. So first we have the score 900 then zero. So there is like no clean way on how the data is
sorted and the result is not really clean and this issue happens usually if you are sorting your data based in a
column that has repetition like here the country we have twice Germany and twice USA. So now in order to refine the
sorting and make it more correct, we can include in the sorting another column in this scenario for example the score. So
we can make a list of columns in the order by and we can separate them using the comma. And of course you can have
different mechanism for each column like for the country we are saying it is ascending but for the score we say you
know what let's make it descending. It will not be only one for all columns. So now what can happen is we're going to
start sorting the data for each section. So for the two customers from Germany the sorting going to be from the highest
to the lowest. So it's going to go and switch the two customers. So Martin going to be first because he has higher
score than Maria. And with that we are refining the scores based on the same value of course the country. Now for the
UK nothing going to happen because we have only one value and for the USA as well nothing going to happen because it
is already sorted in the correct way from the highest to the lowest. So as you can see if you are including a
second column you are refining your sorting and as well my friends the order is very important. So this is how you
can do nested sorting in SQL. Let's go back to our SQL and start practicing. All right so now we have the following
task and it says retrieve all customers and sort the results by the country and then by the highest score. So again we
need all customers. So select everything from customers table. And now the task says we have to sort the result by the
country. So we're going to start with the order by and since it says by the country. We're going to go with the
country and we're going to sort it alphabetically. So it's going to be ascending. So let's go execute it. Now
you can see the data is sorted completely differently by the country. So we have first Germany, UK and then
USA. But that's not all and says then by the highest score. So we have to go and include another column in the sorting
and we can go and add that by adding a comma and then mention another column the score and now we have to specify the
mechanism. It says by the highest score. So the highest must come first and with that we are using descending. Now what
is the current situation in that? If you look to the results for example for those two customers we have 350 and then
500. So that means the scores are sorted ascending right the same thing for USA. So from the lowest to the highest. Now
if you go and do it like this what going to happen it's going to go and switch it. So you can see over here now for
Germany first comes the highest the 500 and then the 350 and for USA as well they switched. So we have the highest
and then the lowest and with that we have solved the task. Now again the order of those columns are very
important. So since the scores comes after the country we will not get the highest scores first at the results. So
we will not get the 900 as a first row. And that's because the scores must be sorted after the country. So the country
has more priority. Now if you go and flip that. So let's go over here and says sort first the score and then the
country. So let's go and execute it. It's called has first to sort the scores. So with that you will get the
900 first, right? And then the countries. And since there is like no duplicates in the scores, this makes no
sense at all. So you can go and skip it. So nested sorting only makes sense if you have repetition in your results and
you can use the help of a second column in order to make the sorting perfect. So that's it and with that of course we
have solved the task. All right. So with that you have learned how to sort your data using order by. Now in the next
step we're going to talk about how to aggregate and group up your data using group by and we're going to put it
between the where and the order by because in the order of the query the group by comes between the where and the
order by. So let's go. Okay. So what is exactly group by? It's going to go and combine the rows
with the same value. So it's going to go and combine and smash press your rows to make it aggregated and more combined. So
all what group by does it aggregates a column by another column. Like for example, if you want to find the total
score by country. So you aggregate all the scores value for one country. If you have this kind of tasks, then you can
use the group I. Let's see the syntax of that. We will start as usual with the select. And now what we want to see in
the result is two columns. So we have to specify like a category like the country. This is the value that you want
to group the data by. and another one where you are doing the aggregations. So for example you are saying I would like
to see the total score. So we use the function sum in order to summarize the values of the score. After that as usual
we use the from in order to select the data from specific table. And now comes the magic we use after the from group
by. And now understands okay I have now to combine the data. I have to group up the data by something. And this time we
are saying you have to group up the data by the country. So that means each value of the country must be presented in the
output only once and for each country we want to see the aggregation and that is the total score. So let's see how is
going to execute it. So it's going to first start with the from it's going to go and get the data from the database
and then it's still going to execute the group by and now scale understand okay I have to group up now the data by the
country and it understands it has to aggregate the scores for that. So it's going to go and identify the rows that
are sharing the same value. Like for example here we have two rows for Germany and it's going to bring it to
the results. So now we have two rows for the same country but since we are saying group by country SQL going to try and
combine them smash them together in only one row. So each value of the country must exist at maximum once. We cannot
leave it like this. So now what we going to do with the scores? We have two scores. Now SQL going to check the
aggregate function. It is the summarization. So, and it's going to go and add those values 350 + 500. And with
that, we're going to get the total score of 850. And with that, as you can see, scale is combining those two rows into
one. So, in the output, Germany will exist only one. And about the scores, we will get the total score. And the same
thing going to happen for the next value. In the country, we have the USA. We have it twice. So, we're going to get
two rows. And scale going to combine those two rows in one because USA must exist only once. And with the scores we
will have the total scores. So 900 plus zero we will get 900. And with that it's still converted those two rows into one.
And for the last value in the countries we have the UK. It's going to stay as it is. There is no need to smash and
combine anything because it's already one value. So my friends if you are looking to the output you can see we
grouped the original data by the country. And that means we're going to get one row for each value inside the
country column. So my friends the original data you have five rows in the output if you are using group by like
this you will get only three rows. So this is exactly how the group by works. Let's go back to scale and practice.
Okay. So we have the following task and it says find the total score for each country. So from reading this you can
understand we have to do aggregations and we have to combine the data by a column. So now usually I start like
this. I start selecting the columns that I need in order to solve this task. So what do we need? We need the country and
score from our table customers. So let's start like this. Now you can see we have the countries and the scores. And the
task says we have to group up the data by the country. So that means this is the column where we're going to do the
group by and the total scores will be aggregated. So what we have to do? We're going to use the group by since it says
for each country. We're going to use it over here. Group by country. And now we have to go and aggregate the scores. We
cannot leave it like this. So we're going to say the sum of the score. So let's go and execute it. And with that,
as you can see, we are getting the total scores for each country. So now instead of having five customers, we have only
three rows now. And that's because the countries has three rows. And now if you check the result, you can see something
weird. It says no column name. And that's because we have changed the scores. It's not anymore the original
score. It is it is the total scores. We have summarized those values. So SQL don't know how we going to call it. So
those values doesn't come directly from the database. It is manipulation that you have done here. Now in order to give
a nice name for that we can go and add aliases. An alias it is only like a name that lives inside your query. So we can
do it like this as and you can specify any name you want like for example total score. And now scale can understand okay
this is the name for this column and if you go and execute it you will see the new name in the results. But you have to
understand this name exists only in this query. You are not renaming anything inside your database and you cannot use
it in any other queries. It is just something that is known inside this query and only for your results. And of
course you can rename anything any column like for example here you can say this is the customer country and if you
execute it you are just renaming the column in the output. So this is really nice in SQL. Okay. So now there is like
one more thing about the group I the non-aggregated columns that you are adding in the select must be as well
mentioned in the group I. So now for example let's say that okay I'm seeing now the countries the total scores I
would like to see as well the first name. So you go over here and say you know what let's get the first name. So
country first name the total scores and execute. You will get an error because it's going to tell you I need only the
columns that you want to group the data by or should be aggregated. So now the first name it is not aggregated and as
well not used for the group I. So it is just here to confuse SQL and it will not work. So if you bring a column either it
should be in the aggregation or it should be part of the group I. So in order to fix this and you really want to
see the first name you can go over here and say you know what let's add it to the group I and execute. This time it
going to work because all the columns that are mentioned here is as well part of the group I. So now as you can see we
have the countries the first name and the total scores and you can see again we have five rows we don't have three
rows and that's because now you are combining the data by the country and as well the first name and now you can see
in the output we are getting five rows we are not getting anymore the three rows the three countries and that's
because SQL now grouping the data by two columns the combination of the country and the first name and those two columns
gives five combinations and that means you will get five rows so that means you have to be really careful what you are
defining in the group I and the number of the unique values that those columns are generating going to define the
output the results. So if you go and remove the first name and from here as well you are grouping by only one column
and this column has only three values and that's why you are getting three rows and with that of course we have
solved the task and now let's extend the task and say find the total score and total number of customers for each
country. So that means we need two aggregations. We have the total score and as well we need the total number of
customers. So from reading this you can understand we still want to group up the data by the country but this time we
need two type of aggregations. We need the total number of customers and the total scores. So we have almost
everything but what is missing is the second aggregation. Now what you can do you can go over here and add another
aggregate function called the count. And what we want to count is the number of customers. So we can go and add the ID
over here and call it total customers. So now of course SQL going to So now if you go and execute it, you will get as
well the total customers by the country. And now as you can see SSQL has no problem with the ID and that's because
you are aggregating the ID. So SQL know what to do with it and how to combine it. So that means you don't have to
mention the ID in the country because you are aggregating it. So that's all with that we have solved as well the
task. All right. Right. So with this you have learned how to group up your data using the group eye. Next we're going to
talk about another technique on how to filter your data but this time using the having clause. So let's
go. All right. So what is exactly having? You can use it in order to filter your data but after the
aggregation. So that means we can use the having only after using the group I. So let's see the syntax of that. So
again like the previous example we are finding the total score by country. So we have our select from group I and now
you say you know what I would like to filter the end results and in order to do that we use the having after the
group I and now like the wear clause you have to specify a condition. So we have the following condition where we want to
see in the results only the countries if their total score is higher than 800. So this going to be our condition. So now
you might noticing something with the group by we are using the country the column where we are grouping the data by
its value but with the having we are using the aggregated column the sum of the score. So this is how the syntax
works and now let's see how is going to execute it. So as usual SQL start with the from we are getting our data and
then the second step is going to go and aggregate the data by the country. So it's like before going to group the rows
with the same value of the country. So we're going to have one row for each country and this is what going to happen
if you use group I and with that we have now aggregated values right and after the group IQL going to go and execute
the having. So having it is like a filter. Now we have a nice condition the total sale must be higher than 800 and
SQL going to go and check the new results after the aggregation. So in Germany we have the total sales of 850.
So it meets the condition and it going to stay in the results. The same thing for USA it is higher as well than 900s
but for UK it is not meeting the condition 750 it is not higher than 800 and SQL going to go and filter out this
row so that means after applying the having we will get only two countries because they have values that is
fulfilling the condition and that's it is what can happen if you are using having it is simply filtering the data
but now you might be confused you say you know what we have used the wear clouds to filter the data so why we have
in SQL another cloud how to filter my data. Can't we just use the where? Well, in SQL there are like different ways on
how to filter your data based on the scenario. So now let's go and add both of the filters in my query. We are
already using the having after the group I and now let's go and add the wear. Usually the wear comes between the from
and the group I so directly after the from. And here we are saying the score must be higher than 400. So now we are
filtering based on the scores twice, right? Once we are saying the score higher than 400 and by having we are
saying the sum of score must be higher than 800. So what is the big difference? It is when the filter is happening. If
you want to filter the data before the aggregation you want to filter the original data then you can go and use
the wear clause. But if you want to filter the data after the aggregations after the group by then you can go and
use having. So it's really all about when the filter is happening. So let's see how is still going to execute this.
So as usual first the from going to be executed to get the data. Then after that the second step the wear going to
be executed. This is our first filter. So SQL going to filter the data using where before doing any aggregations and
based on our condition the first customer will be filtered out because score is less than 400 and the same
thing for the last customer. Now after the applying the wear clouds we will get only three rows only three customers.
And now next SQL going to go and execute the group by. So it's still going to go and group the data by the country. So
now we have fewer data to be combined. So the values will not be summarized because we have only one row for each
country. Now after the data is aggregated by the group by then SQL going to activate the second filter
having. So the next step is going to execute the having and here SQL going to filter the new results based on the
total scores and still going to check one by one. So, USA is meeting the condition. UK going to be filtered out
because it is not higher than 800. And this time Germany as well will be filtered out because this time it is not
fulfilling the condition. In the previous example without the wear, we had more scores for Germany. That's why
it passed the test. But this time since we filtered a lot of customers using the wear, Germany will not have enough
scores pass the second filter. So with that in the output we will get only one row and that's because we are filtering
a lot of data. So it is very simple where going to be executed before the group by before the aggregations having
going to be executed after the group by after the aggregations. So now let's go back to scale in order to practice.
Okay. So now we have very interesting task find the average score for each country considering only customers with
a score not equal to zero. So it sounds like condition and return only those countries with an average score greater
than 430. So this is again another condition. So I know there is a lot of things that's going on. Let's do it step
by step. Usually I start by doing a very simple select statement with the columns and data that I need. So let's start
with a simple select. So what do we need over here? We need a score. We need a country. Again we need a score country.
So all what we need is two columns. Now I'm going to go and select the ID just to see the customer ID. Then let's go
and get the country score from our table customers. So let's go and query that. So now as you can see I start with the
basics. Query the data and then build up on top of it the second step. Now what do we have in the task? We have to find
the average score for each country. That means we have to do some aggregations. And here we have two conditions. The
first condition says we need only the customers with a score not equal to zero. And the second one we need only
the countries with an average score greater than 430. Now you have to decide for each condition whether you're going
to use the where or having. Now for the first one we want to filter based on the scores. So that means we want to filter
before the aggregations. It's not saying the average score. It's saying the score itself. So that means we can use for
this a wear condition. Now about the second one it says countries with an average score greater than 430. That
means we want to filter the data after aggregating the score. So that means for this condition we have to use the
having. Now what I would like to do is to implement the first condition. It's very simple. We're going to say where
after the from the score is not equal to zero. So let's go and execute it. And with that we don't have any customers
where the scores is not equal to zero. So that we have solved this part. But now for the second condition first we
have to do the aggregations. So we're going to start with the average score. We're going to go over here and say
average and we're going to call it average score. Now we don't want to see only the average score. We want to see
the average score for each country. So that means we have to aggregate by the country and for that we use the group I
group by comes always after the wear clause. So group by and which column? It's going to be the country. So
country. Now there is like an issue here. You cannot execute it like this. We have to go and get rid of the ID. We
don't need it at all. So let's go and execute it. So with that we have the average score for each country and we
have solved the first part. So that means the first and the second part they are completed. Now we're going to talk
about the last part. The average score must be higher than 430. And for that we're going to use the having and having
comes after the group by. Now we need to specify the condition. It must be the aggregated column. So we're going to
take the average score from here and put it after the having and it should be greater than 430. So that's it. With
that we have the last part as well. Let's go and execute it now. And with that my friends we have filtered the
data after the aggregation. So this is how I decide between the where and having. It is very simple. All right. So
with that you have learned how to filter the aggregated data using the having. And now next we're going to go back to
the top where we can use there the keyword distinct exactly after the select. So let's go now and learn about
the distinct. Okay. So what is exactly distinct? If you use it in SQL, it's
going to go and remove duplicates in your data. Duplicates are like repeated values in your data and it's going to
make sure that each value appears only once in the results. So it sounds very simple and as well the syntax is easy.
So as usual we start always with a select but directly after the select we use the keyword distinct. So there is
nothing between them and then the normal stuff we specify the columns and then the from in order to get the data from
table. Let's say that I would like to get a list of unique values of the country. So the first thing that SQL
going to do of course is to get the data from the database using the from. And now the second step is the select. So
SQL going to execute it and going to select only one column the country. All other columns going to be excluded and
removed from the results. And now SQL going to go to the third step. It's going to go and apply the distincts on
the country values. So it acts like a filter where it going to make sure each value happens only once. So it's going
to start with the first value Germany. Now it's going to look to the results. Do we have Germany? Well, we don't have
anything yet. So that's why it's going to include it in the results. Then the next value is going to be USA. The same
thing. We don't have USA in the results. So it's going to go and include it. And this happens as well for the UK. We
don't have UK in the final results. That's why it's going to go as well included. Now comes Germany again. Now
it's going to say wait, we have it already. So it will not go and add it again in the output because it must
appear only once. So we will not have Germany twice. And as well for the last value the USA we have it already in the
results that's why it will not appear again and with that we have removed the duplicates or the repetition inside our
data. So each value is unique. Now let's go back to SQL. Okay that task is very simple. It says return unique list of
all countries. So let's go and do that. It's going to be funny. So select and now let's get the column country from
our table customers like this. Now you can see we have a list of all countries but the task says we need a unique list.
So that means I cannot have here repetitions inside it. And with that we're going to use the very nice
distinct. So if you do it like this let's go and execute. You will see there will be no duplicates in your results
and all the values in the result going to be unique. So with that we have solved the task. It's it's very simple.
Now there is like one thing about the distinct that I see a lot of people using it a lot in cases that it's not
really necessary. So for example, let's go and get the ID. Now if you go and execute it, you can see here we have a
list of all ids and there are no duplicates. But now if I go and remove the distinct and executed, we will get
the same results because the ids are usually unique. So it really makes no sense to go and say distinct because as
you can see the database has to go and make sure each value happens only once. So there's like extra work for the SQL
and it is usually an expensive operation. So if your data is already unique, don't go and apply distincts.
Only if you see repetitions and duplicates and you don't want to see that only in this scenario, go and apply
the distinct. Don't go blindly for each query applying distinct just in case there is duplicates. This is usually bad
practices. Okay. So that's all for distinct. Okay my friends. So with that you have learned how to remove the
duplicates using the distinct. In the next step we're going to talk about another keyword that you can use
together with the select. You can use top in order to limit your data. So now let's go and understand what this
means. Okay. So what is exactly top or in other databases we call it limit. So it is again some kind of filtering in
SQL. If you use it, it's going to go and restrict the number of rows returned in the results. So you have a control on
how many rows you want to see in the results. The syntax is very simple as well. Directly after the selects you're
going to use the keyword top and then you specify the number of rows you want to see in the results. So for example
three and then only after that you specify the columns that you want and then from which table. Now let's see how
going to execute it. So as usual the from going to be executed we will get our data and then the second step is
going to go and select the columns. In this case all the columns going to stay and then after that it's going to
execute that top. So how it works? It's very simple. For each row in database, we have a row number. It has nothing to
do with your data with the ids. For example, here like in the current result, we have row number 1 2 3 4 5.
Those numbers are not your actual data. It is something technical from the database. So it is not equal to the ids.
For example, the ids is actually your content your data. So here we are not filtering based on the data based on the
row numbers. So since here we have defined three SQL going to count. Okay. row number one 2 three and that's it. So
it's going to make a cut and all the rows after number three they will be excluded from the results and you will
get only the three rows at the results. So now as you can see this type of filtering is not based on a condition or
something it's just based on the row numbers. So whatever results you have in your data it will go and make a cut at
specific row. So let's go to scale and practice that. Okay. So now we have a very simple task. It says retrieve only
three customers. So let's go and do that. We're going to go and select star from our table customers and execute it.
Now as you can see in the output we have five customers. But the task says we want only three. And there is no
specifications at all about any condition. So I don't have to go and make a work clause where we write a
condition based on our data. We just want three customers. So we can do that very simply by just adding top exactly
after the select and then specify the number of rows you want to see from the output. So select top three and then the
star. Let's go and execute it. And with that we are getting three customers. That's it. It's very simple. All right.
Now moving on to another task. It says retrieve the top three customers with the highest scores. Now of course this
is like a mix between ordering the data and filtering the data. Right? So we usually sort the data by the scores from
the highest to the lowest. But now it's like we are doing both together. So let's do it again step by step. I will
just back to the select star from customers. Now what we can do we can go and sort the data by the score from the
highest to the lowest using the order by so order by score and then descending. So let's go and execute it. And now you
can see the first customer is with the highest score and then the second highest and so on. Now I think you
already got it in order to get the top three customers with the highest scores. What you have to do is to just go over
here and say top three and execute it. And with that you have now a really nice analyzis on your data. It's like a
reports where we are finding the top customers with the highest score. So this is really amazing and very easy. So
as you can see mixing the top with the sorting the data you can make top end analyzes or bottom end analyzers. So
let's have this task retrieve the lowest two customers based on the score. So now we want to get the lowest scores in our
table. And in order to do that is very simple. What we're going to do we're going to flip that. So we're going to
sort our data based on the scores ascending from the lowest to the highest. And since we want only the
lowest two customers, we're going to replace the three with a two and execute it. And with that, we're going to get at
the lowest two customers. It is Peter and Maria. They have the lowest scores. Again, it's very easy. Okay, this is
fun. Let's go to the next one. Get the two most recent orders. Well, this time we are speaking about another table.
Let's go and select everything from the table orders like this. So now, as you can see, we have here four orders and we
want the two most recent orders. So most recent means we have to deal with the order dates and we can build that by
sorting the data by the order dates. So order by order dates and since we are saying the most recent orders so from
the highest date to the lowest that means descending right let's go and execute it and as you can see based on
our data and now we can look to our result this is the last order in our business based on the order age and this
one is one of the earliest orders. So with that we have sorted the data and since we want the two most recent orders
we go over here and say we go exactly after the select and say top two and execute and with that we have now the
last two orders in our business. So as you can see combining the top with the order by you can do amazing analyszis.
All right so this is how you limit your data using top and with that you have learned the basics everything that you
can learn and with that you have learned all the clauses the sections that you can use in any query in SQL. Now next
what we're going to do we're going to put everything together in one query in order to learn how SQL going to go and
deal with all those clauses and how SQL going to go and execute it. So let's go and do
that. Okay. So now I'm going to show you the coding order of a query compared to the execution order that happens in the
database. So the coding order of a query starts always with a select and then exactly after that you can put a
distinct and then after the distinct you can put a top. So this is the order of all those keywords and then you can go
and select like few columns and after you specify the columns separated with a comma you tell SQL from which table your
data come from using the from clause. Now after that if you want to filter the data before the aggregation you can use
the where clause and this always comes directly after the from. And if you want to group the data then you have to do it
after the wear clause using the group by and after the group buys comes the having if you want to filter the data.
And the last thing that you can specify in query it is always the order by. So this is the order of all those
components of the query. And if you don't follow this order you will get an error from the database. Now if you look
to this query there are a lot of things that's going to filter your data. So let's check them one by one. The first
thing that you can do is to filter the columns. If you don't want to see all the columns, you want to see only
specific columns, you use the select and of course you must use it. So the columns that you specify will be shown
in the results. So it's like filtering the columns. Now there is another type of filter where you filter out the
duplicates if you want to see unique results and that's using the distinct. So this is another type of filter.
Moving on, we can filter the result based on the row numbers. So we can limit the result using the top. But this
type of filter doesn't need any conditions. It's purely based on the row number in the results. Now moving on, if
you want to filter your data based on conditions based on your data, you can filter the rows before the aggregation
using the wear clause. And the last type of filtering, you can filter your rows after the aggregation using the having.
So as you can see, we have like five different types and how to filter the results in SQL. So now let's see the
execution order. As we learned the first thing that's going to happen is that SQL going to execute the from clause. So SQL
going to go and find your data in the database where all the next steps going to be paste on this data. Now the next
step that is going to do is that it's going to go and filter the data using the wear clause. This has to be happen
before anything else. So before any aggregations and so on we have to make scope of the data. So once SQL apply it
maybe some of the rows going to be removed and once the data is filtered the third step SQL going to execute the
group I so going to take the results and start combining the similar values in one row and start aggregating the data
based on the aggregate function that you have specified. So now after the group by after aggregating the data what is
going to do now it's going to go and apply the second type of filter the having. So based on the condition the
SQL going to go and start removing few aggregated data away and keep the rest. Now moving on to the step number five.
Finally it's going to go and execute the select distinct. So SQL going to go and start selecting the columns that we need
to see in the results and remove the other stuff. And once the columns are selected SQL going to go and execute the
order by. So SQL going to start sorting the data based on the column that you have specified and the mechanism as
well. So the data will be sorted differently. And my friends the last step that going to happen in your query
will be always the top statements. So based on the final final results SQL going to go and execute the top. So here
we are saying top two that means we want to keep only the first two rows without any conditions. So SQL going to count
okay row number one two and after that it's going to make cuts and remove anything after that. So this is the last
filter that's going to happen and as well the last step. So now if you sit back and look at this the coding order
is completely different than the execution order in the coding we have first to specify the select actually the
select going to be executed just almost at the end. So at the step number five and once you understand how SQL execute
your query you can understand how to build correct queries. So now the first thing that we
have learned that we can go and have like one query right something like this select star from customers. Now this is
one query and in the output we have one results but did you know that in SQL we can have like multiple queries and
multiple results in one go. So we can do everything together like for example let's say I'm selecting as well the data
from orders. So that means we have two queries and now if you go and execute what can happens you will get two result
grids. The first result grid is for the first query and the second one is for the second query. So with that you can
do multiple queries in the same window and with that the results can be splitted into multiple window depend how
many queries you have and usually in SQL you might find that by the end of each query there is a semicolon like this. So
at the end of the first query we have semicolon and for the second query we have as well at the end another
semicolon. For the SQL server it is not a must but for other databases if you have multiple queries in one execution
you must separate them with a semicolon and with that the database can understand okay this is the end of the
first query and this is the end of the second query. So you have like separations between
queries. Okay. Now moving on to another cool thing in SQL. Now what if we don't want to query the data inside our
tables, we would like to show a static value from us from the one that is writing the query. And this is very
practical. If you are like practicing and you want to check something using a value from you, not from the tables. So
how we can do that? It is very simple. We're going to write select and then now after that instead of having a column
name you can go and add any value like 1 2 3. So it is just a number and we do not specify after that any table. So we
leave it like this. Select 1 2 3 and we don't need to use the from close. So now if you go and execute it you will get 1
2 3. So this is a static value. And of course you can go and rename the column like static number. So execute it again.
So with that we have a static value. And you can go and add anything like string as well. So let's say hello as static
for example string. So let's go and execute. Now we have two queries. The second one you can see our static value.
Hello. So in queries we can add values from us. Not only selecting data from the queries but of course you can go and
mix stuff. So we can have like in one query data from the database and static data from us. So let me show you what I
mean. Let's go over here and say select and let's go and get for example the ID the first name from the table customers
like this. So with that we can see we are getting data from the database. But now I can go and add something from me
new customer and we can call it customer type. So now what is going on here? Two columns from the database and one column
from us. It is the static one. So if you go and execute it, you can see for the ID and the first name those data comes
from the database. But for each record we are always getting the same static value new customer, new customer and so
on. So this piece of information comes from the query. It is not stored inside the database and those two informations
come from the stored data inside the database. So this is really cool thing. You can add few informations from you
and you can get the data from the database. This is the static values. Okay. One more cool thing that I
want to show you that if you have a query like this you are selecting from table and filtering the data and now you
would like not to execute the whole thing. You would like to execute only a part of this query. So now sometimes as
you are writing a query, you don't want to execute the whole thing. You want to execute only part of the query. Like for
example, I would like to see all the customers again in this query without this filter. So instead of removing it
and then query and then again adding it, what you can do, you can highlight what you want without now the filter and
execute. So without the database going to execute exactly what you highlighted. And now as you can see I'm getting all
the customers without the filter. And if you don't highlight anything and execute, what's going to happen? It's
still going to execute the whole thing inside the editor. And this is really nice if you want to query another table
quickly in the same editor. Like we want to select everything from the orders just quickly. So you can highlight only
this query and execute. And with that SQL is ignoring everything else and only executing what I'm highlighting. And
this is really nice. It gives us like speed and dynamic. And you're going to find me doing that a lot in the course.
So this is really nice. Okay. My friends. So with that we have learned the basics about SQL query. the basic
components of the select statements and with that you can talk to our database in order to get data. Now in the next
chapter we're going to learn how to define the structure of our database. So we're going to learn the data definition
language DDL. So let's go. Okay. So usually if you have like an empty database what you want to do is to
go and define the structure of your data. So one of the first things that we usually do is we go and create a new
tables. So here we have a command called create and if you use it you can create a new object inside the database like
for example a table. So once you execute it you're going to get brand new table and usually the table going to be empty
without any data. So it is very simple. This is what the create command does. And now let's go to SQL in order to
create a new table. So my friends we have the following task. Create a new table called persons with columns ID
person name birth date and phone. Okay. So this time we will not start by select we will start with the command create
table. So we are telling SQL to create a table and after that we have to define the name of the table. So in this task
we have to call it persons. Now we have to go and open two parenthesis like this and in between we have to define the
columns. So what do we need? First we need an ID. So this is the first column name. And next we have to define which
data type for this column. It's going to be an int. So it is a number does not contain any characters. And now next we
can define some constraints and we cannot have a person without an ID. So it should not be in null. So not null.
This is the first column. So we have defined the name of the column, the data type and the constraint. Okay. So let's
go to the second column and here we're going to have a comma and the next one name going to be person name. So this is
the column name and the person name we can have. And now the data type for this column it going to be a varchar because
the person name contains characters. So vchar. And now we have to define the length. So I'm going to go with 50
characters. And now I would say this is a must. So each person should has a name. So we're going to say not null as
well. So that we have the name, the type and the constraint. Now let's move to the third column. It's going to be birth
date. Now which type of informations we have inside the birth date? So it's going to be a date, not a number, not
characters. So we're going to go with the data date. And now about the constraint well depends. I would say in
our application it is an optional because this is very personal information and maybe some persons will
not provide their birth dates. So this is an optional and I will not say it is not null. So nulls are allowed. Now
let's move on to the next one. It's going to be the phone. So now what is the data type of a phone? Well we have
some types numbers we have characters special characters. So we could have anything. So that's why I'm going to go
with the farchar. And here you can specify the length that you think it's okay. I'm going to go with 15. Now of
course depend on the system that you are building. I would say the phones are very important in order to validate
whether this is a real person. So we're going to say not null. So we are not allowing nulls in this field. Perfect.
So with that we have covered all the columns that are required. We have defined the data types and as well the
constraints. Now the last thing in each database table we should has a primary key in order to make sure this table has
an integrity and maybe as well connectable to other tables. So now what we're going to do, we're going to go and
add the primary key constraint, comma, for the last column. And then we're going to say constraint. Now we have to
give a primary key name. This is only going to be visible for the database. So I'm going to call it PK for primary key.
And here persons and then after that we're going to say primary key. And between two parentheses, we're going to
go and pick which one is the primary key. And of course, it's going to be the ID. So we're going to go over here and
say ID. So again, we are saying there is a new constraint. This is the name of it. It's only internal for the database.
And then we are saying this one is a primary key on the field ID. So that's it with that. We have defined a primary
key for our table. Let's go and execute it. So as you can see it is successful. Let's go and check our database for our
new table. So if you don't see it already, you have to right click on the database and then go and refresh. So
let's go to tables and now we have a brand new table called persons. So with that we have created our new table. Now
of course for the DDL commands you will not get results or data. All what you're getting is a message from the database
and the message says here the command completed successfully and then we have a date when this is completed. So that
means the DDL command will never return data. It is changing the structure of your database. It's not about retrieving
any data and so on. So this command did change something in our database and in this scenario it created a new table and
that's why we call this data definition language DDL because we are defining the database. Now of course if you go and
say select star from our new table persons. So let's go highlight it and then execute it. You will see we are
getting of course the columns. So the ID, the person name, birth date, the phone but we don't have any rows that
means our table is empty. Now what is very important to that you go and save those informations in an SQL script
because maybe later you have to redefine this table but let's say that you have created different queries and you have
lost the script and now I would like to see again the create statements for this table well there is trick for that if
you go to the left side you see the persons right here right click on it and then you have here script table as and
now we have here different options that you can run on the table and the first one says create two Then let's go to new
query editor. So now what happened? The database did read the metadata
informations about the person and created your DDL query with many extra stuff that we haven't done. But this is
the template that the database uses. So now we can see a lot of stuff. But what is interesting is this create table. So
we can see create table the schema DBU the default one then the persons and then we have our columns the data type
and as well the constraints. So with that you got back your DDL statements and many other stuff about the table
which is now not interesting. But now what I really need is to see the create statements about this table. So this is
how you can get back your DL command. But of course what I recommend you is always put your code inside a get
repository and always keep it up to date. So that always you can check your work and extend
it. Okay. So now what else you can do with the structure of your database? If you have already a table, what you can
do, you can go and edit and change the definition of the table. So for example, let's say I would like to add a new
column. In order to do that, we can use the command alter. Alter means you want to edit the definition of your table and
you want to change it like adding new column or maybe changing the data type and anything in the definition of the
table. So the alter command, you can use it in order to change the definition of your table. And now let's go back to
scale and try to change something. All right. Now the task says add a new column called email to the person's
table. So it is very simple what you can do. We can use the alter table command. So we are not creating new table. We
want to edit already existing table. So which table we want to modify it's going to be the persons. So we are telling SQL
we want to change something in the table persons. And of course we have to tell SQL what we want to change. Are we
removing a column? Are we adding column? In this scenario we want to add new column. So let's go and add the email
information. So this is the column name and as you are creating a table you have to define column name the data type and
the constraint. So now for the emails we're going to have like characters, numbers, special characters. So we're
going to go with the varchar and about the length it's going to be let's say 50 and I'm going to say each person has to
has an email. So it's going to be not null. So with that we are adding completely a new column. So that's it.
Let's go and execute it. Now again this is not a query. This is a DDL command and in the output we will not get data.
We will get a message whether everything went correctly. So it says command completed successfully and the time when
this is completed. Now we can go and do a simple query just to have a check to the table. So and now you can see we
have our columns and at the end we have a new column called emails. This is very important. If you are adding new column
it's going to be always at the end of the table. But now you might say you know what I would like to have the email
like something in the middle maybe after the person name. Well, in order to do that, you have completely to delete and
drop the table and create it from the scratch using create command which is might be bad if you have data inside the
table. So if you are fine by adding your new column at the end, you can use the alter table. But if you say I would like
it in the middle, then sadly you have to go and drop everything and start from the scratch. Okay. So now let's have
another task and it says remove the column phone from the person's table. So now we're going to do exactly the
opposite. We're going to go remove it completely with its data from the table. So we're going to still saying alter
table persons. We are saying we want to edit the definition of the table persons. And now instead of adding we
will be dropping a column. And then after that we have to specify as well the column name. It's going to be the
phone. But we don't have to mention again the data type and the constraint. And that's because the database already
knows those informations. So we need those informations if we are creating something new. That's why we can get rid
of that. We just need the column name and the database is going to do the rest. So let's go and do that. Now you
can see successful. And now let's go and check our table. And now as you can see we have the ID, person name, birth date,
email, and we don't have the column phone. Be careful. If you are deleting column, you will be losing as well all
the data inside this column. So as you can see, this is very simple. This is how we can edit the definition of our
table by adding and removing columns. Okay, now moving on to the last one in this group of commands. So now so far
what we have done, we have created something new in the database. We have changed the definition of something
inside our database. And now the last one, you can go and drop something from the database. Let's say we have another
table and we don't need it anymore. So we can go and use the drop command in order to remove the table completely
from the database. And this means as well removing everything the table and the data inside it. So now let's go to
SQL and let's drop something from our database. Okay. So now our task says delete the table persons from the
database. This is the simplest form of command in SQL but yet the most risky one. So what we need? We have to delete
and drop the whole table persons. We don't need it anymore. We're going to say drop table and then all what we have
to do is to give the name of the table persons. So three words. You don't have to specify anything. Just destroy the
table persons. Let's go and execute it. It is successful. So as you can see it is very simple. Now on the left side to
your database go refresh and go to the tables and you will not see the table persons. So the drop command it is very
simple but yet very risky. So if you compare now create table with a drop table you can see destroying things is
way easier than building it. Those are the commands create alter drop. those commands we use in order to define the
structure of our database the DDL commands that was very simple all right so that's all about the data definition
language DDL and with that you have learned how to define new stuff in your database now moving on to the next one
we're going to learn about the data manipulation language and here we're going to learn how to manipulate our
data inside the database let's go all right so now what we're going to do we're going to go and modify and
manipulate your data inside the database. So now sometimes what happens you have a table inside your database
and the table is empty. You don't have any rows any data inside the table. Now in order to add your data to the table
what you can do you can use the command insert. So insert going to go and add new rows to your table and of course not
always the table must be empty to add your data. You can add new rows to already existing data and SQL going to
go and append it at the end of the table. Now my friends in order to insert new data to the target table there are
two methods. The first and the classical way in order to insert new data we can use the insert command and manually
specifying the values that should be inserted to the table. So you're going to start specifying in the script the
values and then they're going to be inserted as a new rows to the target table. So in this process you are
manually inserting new values to the table using like an SQL scripts. So now we're going to focus on this scenario on
how to insert data. All right. Now let's check quickly the syntax of the insert command. It start with the keyword
insert into and after that we have to specify the table name. So where we want to insert and then we make a list of all
columns that we want to insert. And then we specify list of columns where we're going to insert values into them. And
after that we say values. And finally we're going to go now and specify the data that should be inserted to the
table. and we make it as well as a list like we have done for the columns. Now in the insert statements specifying
those columns it is totally optional. So if you don't specify the columns of the table then SQL going to expect you to
insert values into each column because sometimes of course we don't want to insert value for each column. You can
skip few columns of course but if you want to insert a value for each column either you go and specify them as a list
or you can skip it. Now for the insert statements there is very important rule. The number of columns and values must
match. So if you specify here three columns then you must insert as well exactly three values. So this must be
matching. And one last thing about the syntax you can insert multiple values in one go. So for each row you can specify
a list of values that must be inserted. So that's all about the syntax. Let's go back to SQL in order to practice insert
command. Okay. So now let's go and insert a new customers. So it's very simple. It start with insert into. So we
are saying we want to insert data into. So we have to go and specify the table name customers. Now after that we have
to specify list of columns where we want to insert data into it. And what we can do we can go and check which columns do
we have inside our table. So we can see we have ID, first name, country, score. And we can go and make a list of that.
So we can say ID, first name, country and score. So we just have a list of all columns inside our table customers. Now
what we need? We need the values. So which data should be inserted. So we can go and open two parenthesis. And now we
have to specify an ID. We know the last customer was five. So we're going to go with the customer six. Now we have to
give the name of the customer. Let's go for Anna. And then a country. Let's go for USA. And this customer has no
scores. So what we can do? We can say null. So we don't know the score of this customer. nulls means nothing we don't
know. So with that you can go and insert one row. But now let's say that I would like to go and insert like a second row
one more customer. What we can do we can separate this with a comma and then we can go and repeat the whole thing again.
So the ID is seven. The next one let's call this customer Sam and we don't know the country of this customer. So we're
going to say it's null. But the score we know it already. It is 100. So as you can see we are adding a value for each
of those columns. And if you don't know the answer then make it null. if the database allows it to be null. Some
columns they are not allowed to be null like the primary key. So if you go and say over here null the database will not
allow it. Well actually we can go and test it. Let's execute. And you can see you cannot insert the value null into
the column ID. So this is not allowed. Going to have a seven. But for the other columns it is allowed. You can go and
check the definition of the table. Now we go and execute. Now the output of the modifications command is going to always
indicate what happens to the data. So it says two rows affected. Affected might be inserted, updated, deleted. So you're
going to get a general statement from the database. But you are getting how many record is affected. So we got two
because we have inserted two records. So now as you can see it's not like the query. We are not getting any data in
the output. We are just getting a message. So this is a big difference between querying the data using the
selects and modifying the data using inserts. We are doing now direct modifications to the data inside our
database. Of course, if you want to see the data in the customers, what we can do, we can go and query the data, right?
So, let's go and do that. Select star from customers. I would like to see the whole table. So, market and execute it.
Now, you can see we have seven customers. So, we just manipulated our data. We have here Anna and Sam. This is
how you can insert data to the database. Now, there's like few rules you have to be careful as you are inserting new data
to your tables. You have to pay attention that the order of the columns that you have defined. insert is
matching the values that you are inserting over here. Let's have an example. I'm going to go and remove this
over here and let's say that we are inserting a new one number eight and now in the first name instead of the name of
the customers we have inserted the country like USA and in the country we have inserted the name is just mistake
and we are all human right? So let's have a name like this max. Now if you go and execute it the database can accept
it because it is really hard for the database to understand that you have made here an error. Both of them are var
and the database doesn't care about the content of the data as long as you are following the rules of the data type. So
now if you go and select the data from the customers you can see now we have a customer called USA from the country
max. So the SQL going to do it blindly like you insert the data as long as you are following the data type rules and
the constraints. So for example, if you made this error over here and you say the id is max and let's say the first
name is let's say nine and you execute it here the database is smart enough to say you know what there is something
wrong the ID should not be strange so the database going to reject your inserts be careful of the order of your
columns now let's go and query again our table now if you are in the insert commands defining all the columns
exactly like the table so as you can see we have here complete match ID first name country score we have all the
columns and as well the correct order there is like lazy way you can go and remove the whole thing over here and
with that the database can understand okay we are inserting values to all of the columns so going to understand you
are inserting something to each columns in the correct direction so let's go and do that correctly nine and here let's
say we have from Germany so if you go and execute it it will be working even
though we didn't define the columns and that's because the values that we are inserting as exactly the same number of
columns of the table and following as well the rules. Now moving on to the next one, you can go and add only two
columns in the definition. If you know already always the country and the score is null. We know only two informations,
the ID and the name. Then you don't have always to go and say null null null and so on. We can go and skip that. Okay. So
now let me show you what I mean. We're going to go after the table name and we're going to define only two columns,
the ID and the first name. So that means we are telling SQL we want to insert only two columns. And now you have to be
careful. If you define here two columns then the values should be as well two columns. So we're going to remove the
country and the score. And we can go and add only two informations. So 10. And we can go and add here for example Sara. So
if you go and execute it, it will be working. And now what is skill is doing with the other two columns. It's going
to be nulls. So let's go and select again from our table. You can see here Sara has null in the country and as well
in the score because we didn't define those informations. But be careful, you cannot here skip a column that is not
allowed to be null. So you have always have in your list all the columns that are not null. So for example, I cannot
go and insert only the first name. I will get an error because the database can try to insert a null in the ID and
this is not allowed. So you can skip only nullable columns. All right, my friends. So that
was the first method on how to insert data to your target table as you saw by typing manually the values inside an
insert command using values. And now let's move to another methods. We're going to insert data but this time not
manually. We're going to insert data using another table. So imagine we have the following scenario. We have an
already existing table with data and this going to be the source table, the source of your data and we have another
table. This table is empty and we want to insert a new data to this target table. Now what we can do, we can take
the data from the source table and insert it into the target table without manually writing the script for the
values. So we are moving the data from one table to another. Now in order to do that we need to do two steps. The first
step we have to write an SQL query using select from and so on in order to select the data that we need from the source
table. And once you do that you will get a results. So this is like you are doing a normal query. You right select and you
will get an answer with the results. And now what we can do in the next step we can take this results and use an insert
command in order to insert this results into the target table. And with that we have moved the data from the source
table to the target table. So first write the query on the source table. And the second step use an insert to move
this results to the target table. So let's go back to the scale in order to do that. So now we have the following
task and it says insert data from the table customers into the table persons. So that means the source table is the
customers and the target table is persons. Now how I usually do it that I keep my eye on the target table to
understand the structure of this table and I start writing the query from the source table. If you go to the left
side, we can see okay, we have here an ID. We have here person name, birth date and phone. And you can see only the
birth date except nulls and the rest we have always to provide informations. So with that I have now understanding about
the table persons. Now next I'm going to go and start writing the query from the source. So we start like this. Select
star from our table customers just to have an overview of our table. Now the next step we're going to go and design a
perfect result from this query that is matching the target table. So in the output we need ID and we have it from
the customer from the original table. We're going to go and select ID. Okay. So now next we need a person name and
here we have from the original table something called first name. So this is a perfect match. So we're going to go
and select this table as a second column. So we have covered the first two. Then the third one is going to be
the birth date. Well, my friends, we don't have birth dates, but the database can accept it as a null. So, I'm going
to go and write a null because I don't have such information from the source table. And now the next one going to be
the phone as well. We don't have phone informations. But we cannot have it as a null because it says here not null. So,
what we're going to do, we're going to go and add a static value, a default value. So, we're going to have two
single quotes and in between we're going to say unknown. Since it is var, it can accept this word. So, now let's go and
just query. So we have the ID, we have the first name, the birth date is empty, and the phones is unknown. Now you might
say, but the column name is not matching with the column name of the persons. Well, the database does not care about
that. As long as the result of the data is matching the table, it can go and insert it. So the database will never
compare the column names together. But if you like and go and add here like the aliases exactly like the target table it
will not hurt but it has no effect on the results. All right. Okay. So now we have like query select and we have a
results but this is not an insert. So how we going to insert the result of this into the table persons. Well for
that we need the insert into command. So insert into and now we have to specify the target table going to be the
persons. And of course you can go and list all the column names but if you have like exact match you can skip it
but for me I would like always to add it just to make sure that we don't have any issue. So the ID, person name, birth
date and the phone. So that's it. Let's go and execute. So it is working now. We can
see 10 rows affected. Well that means 10 rows are inserted from the table customers into the target persons. And
now what we can do we can go and query the table persons just to check that everything is working perfectly. Select
star from persons and let's go and execute. And with that you can see our 10 persons that we have added from the
customers. So with that we have moved the data from one table and inserted into another table. And as you can see
it was very simple. First you have to write a query from the source table in order to collect the data that you need.
and then you go and insert it into the target table. So this is really nice and easy and this is another way on how to
insert data into your database. Okay, so with that we have learned how to insert data to our
tables. Now let's say that I don't have something new. I don't have any rows to be added to my table but I have an
update. I would like to go and change the content of the already existing rows. So what you can do? We can use the
command updates in order to change the content of already existing rows. So again my friends insert going to go and
insert completely new rows but update going to go and change the data of already existing row. Now let's have a
look quickly to the syntax of the updates. It start with the keyword updates and then we have to specify the
table name and after that we're going to use sit in order to specify what are the new values for the columns. So you have
to write down for each column that you want to update a new value and you separate the columns of course using a
comma. Now after that we have to specify as well a wear condition. So it's like the queries you say where and then you
write a condition and if you don't do that and you don't use the wear clause what going to happen you will be end up
updating all the rows inside your table. So that's why we need always the wear clause. All right. So that's all about
the syntax. Let's go back to SQL in order to update our data. Okay. So let's have the following task and it says
change the score of customer 6 to zero. So that means we have to go and modify the data of the customer ID equal to
six. So now first I would like to go and have a look to our data. So select star from customers and now the task is
targeting this customer over here and we would like to replace the null to zero. Now how we can go and update this
information inside the table? We can use the update command. So what we going to do? We're going to start writing update
and after that we have to specify the table name. So what we are updating? We are updating the customers and then
we're going to tell the database to set the value of the score to a zero. So we would like to update and change the
value from null to a zero. And now here comes something very risky. Don't execute this query yet. If you do that,
what's going to happen? The database going to go to the table customers and replace all those values of all
customers to zero. So it's going to go and update the whole table and this is of course very risky. That's why in the
update command we have to give a wear condition a filter in order to target only specific row or the rows that you
want really to modify. In this case we want to change only one row. So what we have to do is to go and specify the work
condition like we have done in the select query. Nothing new, right? So we're going to say where the customer ID
is equal to six. And with that SQL will not go and update everything. First it's going to filter the data and then
updates. And now before I execute just to make sure I go and check which data going to be affected. So it's very
simple you go and select star from table customers and then I go and take the exact where and put it in my query and
then I select the whole thing and execute. And now if this query gives me the data that should be modified then
I'm doing the update command correctly. And in this case we are targeting only one customer. This is the customer
number six. And with that I feel really confident with my update. So what we can do since I'm going to use this later I'm
going to put the whole thing in a comment and if I execute now only the update going to be executed. So let's go
and do that. Now very important to check the message you can see one row is affected which is really good because if
I see here 10 rows is affected that means everything is updated. Now let's go and check the data. I'm going to go
and remove the wear here and check the whole table. Now you can see we still have the old scores only Anna has now
score zero instead of null. So this is how I usually update the data. You have to do it very carefully. Now let's move
to another task. It's going to say change the score of the customer number 10 to zero and update the country to UK.
So now this time we are targeting the user number 10. As you can see she doesn't have the country and score. And
the task wants us to change the score to a zero and the country to UK. So now how we going to do it? We're going to use
the exact same command but with different condition. So the ID this times is equal to 10 and the score is to
zero. But now we have to change as well the country. Now if you want to do multiple updates, you're going to have
here a comma after the score and the new line and let's say country equal and then we're going to add UK. So select
the whole thing and let's go and execute. So again it is affecting only one row. This is really good. And if you
go and check the table search for Sara, you can see in one update we have updated two columns the country and as
well the score. So with that we have solved the task. It's very simple. Now moving on to the second task. It says
update all customers with a null score by setting their score to a zero. So this time we are not speaking about one
specific customer. We are talking about updating the data for a subset of customers. So now imagine you have like
hundreds of customers and you are making one update command for each customer. It's going to be really wasting of time.
Now instead of that we can specify a condition that targets multiple customers and we're going to do the
update for those customers in one go. So now let's see how we're going to do it. We are talking only about replacing the
nulls with a zero. So we don't need the country. So set score equal to zero. But now we will not be specific for the ids.
Now we have to make a new condition. It's going to say like this where score is null. Now of course in the course we
have a full dedicated chapter about the nulls and here all what we are doing is we are searching for scores that is
equal to null. But we cannot write an equal we have to write it like this is null. Of course before we update
anything we have to go and test it in a query. So select star from customers where score is null. Let's go and
execute. Now as you can see we have two customers where the score is null. So that means this condition is targeting a
subset of customers and we're going to do now the updates for multiple rows for this subset. So that means we can run
this query. Let's go and execute it. Now you can see two rows are affected. So that means multiple rows got affected
got updated. So now if you go and query our table customers you can see we don't have any nulls inside the scores and we
have replaced all the nulls with a zero. And of course you can do the same thing. you can go and make an update command in
order to replace all the nulls in the country to maybe something unknown or any default value that you want. So this
is how you can update multiple rows in one go. All right my friends. So with that
we have learned how to insert new rows to our tables and as well how to update the content of already existing row. Now
the last thing or command that we can do to the data inside the table that we can go and remove rows from our table and we
can do that using the command delete. So if you use delete SQL going to go and start removing already existing rows
inside your table. All right. Now for the syntax of the delete it's going to be very simple. We're going to say
delete from and then we're going to write the table name. And here comes something very important. We have to add
a wear condition. And it's like the update. If you don't do that, if you don't include where condition, what
going to happen? You will end up deleting all the rows inside the table. So the syntax is very simple. Let's go
back to scale in order to delete some data. Okay. So now we have the following task. Delete all customers with an ID
greater than five. So now we have to go and delete all the customers that we recently added. So how we going to do
it? It's very simple. We're going to say delete from. So that means I want to delete something from a table. And we
have to specify the table name. It's going to be the customers. So the syntax is very simple. Now my friends, this is
more risky than updates because if you execute it like this, don't do that yet. Wait, what's going to happen? All the
data of the customers going to be deleted. So you will get an empty table and we will not do that. So now we're
going to do exactly like the update command. We're going to specify the work clause. So it says the ID should be
greater than five. So that means ID higher than five. So with that we are defining a subset of the data that
should be deleted, not everything. And if we check in the updates, we have here to do a double check before deleting
anything. So again what we do, we select star from table customers and we're going to go and copy the work condition
in order to test what going to be deleted. So it's going to be all the customers that is higher than five. And
with that I'm making sure that my delete command is correct which is from what I see here is correct. So those five
customers should be deleted. So now let's go and delete those customers. And now very important to read the message.
It says five rows affected. So that means five customers got deleted. And this is better than 10 of course. So
let's go and check what customers left. So we have 1 2 3 4 5. Those are the original customers. And everything else
got deleted. And with that we have solved the task. And this is how we can delete data from tables. Be very
careful. Always test before doing the delete command. Okay. So now we have the following task. And it says delete all
data from table persons. So that means we have to go and drop everything from the table persons. But we don't want to
delete the table. We just want to delete the data inside the table now. So now what we're going to do, we're going to
write delete from. And now we have to specify the table persons. And if you execute it, what's going to happen? SQL
going to go and drop all the data in the persons. But in SQL, we have more interesting command. If you want to
delete everything from the table persons, we have that truncate. Truncate. It is exactly like delete from
persons. It's going to go and make the whole table empty. But why I like to use truncate because it is way faster than
deletes. If you have large tables, the delete command going to be really slow because with the delete there is like a
lot of things happening behind the scenes. There is like logs and protocols. But if you are using trunk,
the database going to skip all those extra stuff and it's going to be very fast. So if you want to delete all the
data from table, you can do it like this if it's like small table. But what I usually do, I go and write truncate and
then table. we're going to get the same effect and with that I'm saying reset everything make the table empty. So
let's go and execute it and now with that you will not get the number of deleted rows and that's why it's
truncate it's way faster. It is not protocoling anything it's not logging anything it just go and delete all the
data without any extra steps. So this is how we can delete all the data from a table but the table still exists. Okay
my friends, so with that you have learned the basics on how to manipulate your data inside the database the data
manipulation language DML and with that I can tell you we have covered the basics of SQL. So with that we have
covered the beginner level. Now in the next chapters we will be in the intermediate level and the first thing
that you're going to learn in the intermediate level you will learn how to filter your data and we're going to
cover many operators that you can use inside the workclass. So let's go. All right. So now let's have an overview
about all different operators in SQL. So the first group of operators we have the comparison operators. They are the
easiest one where all what we have to do is to compare two values and we have like six different variants and how to
do that. Now to the next one we have the logical operators. We use it in order to combine multiple operators. And moving
on to the next one we have the range operator. Here we have only one, the between. We're going to use it in order
to check whether a value falls within a specific range. Now moving on to the next one, we have the membership
operator. And here we have two things. We have the in operator or not in. Here all what you have to do is to check
whether a value is in a list or not. And the last category that we have is the search operator. And here as well we
have only one operator that like we use it in order to search for a specific thing in a text. So my friends, we're
going to go through all those operators one by one. Okay. So now let's go and deep dive into the first category the
comparison operators and we're going to cover all those stuff. So what is exactly comparison
operator? Okay. So what is exactly comparison operators? It is very simple. We want to compare two things and there
is a lot of things that we can compare in SQL. But the formula for that going to be always like this. So we have the
first expression and then operator and then we have another expression and this going to form something called
condition. So here we have a lot of variance. We can compare one column to another column. So for example, you can
go and compare the first name with the last name. So both of the expressions are columns here. Another scenario, you
want to compare a column with a value, a static value. Like for example, you say the first name must be equal to a value
like John. So now we are comparing a column with a value. It's not anymore two columns. Now we have another
scenario where we want to apply a function to a column and then compare the results to maybe a value. So for
example, we apply the upper function to the first name and then this must be equal to a value like John with all the
letters in the uppercase. And one more thing that you can compare you can write an expression in one of the sides like
for example you can say if we multiply price with the quantity it must be equal to 1,000 for example. So here we have an
expression. We have multiple columns included in one sides and the output of this expression must be equal to 1,000.
And now the last one is going to be a little bit more advanced and we're going to cover that of course in other
chapter. We can include a whole query the complete query to one of the sides and we call this a subquery. So in one
of the sides you're going to write a whole query select from where whatever you want and you go and compare the
result of this query to for example a value or a column. So as you can see in a scale we can compare a lot of things
together. Either comparing the columns together or a column with a value or we use a function or an expression or even
a whole query. So this is how we build conditions in SQL. Okay my friends. So let's see how the conditions works in
SQL. So we have our data the name the country the score and let's say that we have built a condition where it says the
country must be equal to the USA. So this is very simple comparison operator and this is the condition that we are
using inside the work clause. So once you apply this filter to your data what going to happen? SQL going to go row by
row evaluating whether it is meeting the condition. If it's not fulfilling the condition then SQL going to remove it
from the results. But if it is fulfilling the condition it's going to keep it. So now we are comparing the
values of column together with a static value the USA. So we're going to compare whatever value we get from the country
together with the USA. So now let's see how is going to apply this filter to our data for the first customer Maria. Now
you can see the value inside the country is Germany. So Isql now going to go and compare Germany to USA since it is not
equal. Then is going to understand okay Maria is not fulfilling the condition. So it is false and is going to go and
remove this customer from the results. So she is not fulfilling the condition. Moving on to the next one to Joan. Now S
is going to take the value inside the country the USA it is equal to USA. So that means John is fulfilling the
condition and Isl going to be happy about it. So it is true and this means is going to keep Joan in the final
results. Now moving on to George the value is UK not equal to USA. He is not fulfilling the condition. Is going to go
and remove him from the final result. Same thing for Martin. Germany is not equal to USA. Is going to remove this
customer as well. And to the last one bit better you can see the value is USA. So USA equal USA. The condition is
fulfilled. SQL is happy about it and going to leave the customer in the output. So now if you go and apply this
condition using the comparison operator to your data only two customers going to be left in the output. This is exactly
how the conditions and the comparison operators works in SQL. Okay. So now let's start with the first operator.
It's very simple. We have the equal. It's going to checks if the two values are equal. That's very simple. Let's
have an example. Okay. So now we have this task. It says retrieve all customers from Germany. So this is very
basic. We're going to go and select and we're going to select all the columns since we don't have any specifications
from the table customers. And if you go and execute it, you will get all the customers. But we don't need that only
the customers that comes from Germany. So we have to go and apply a condition using the wear clause country equal to
the value Germany. So make sure you are writing it exactly like in the database otherwise it will not work. So let's go
and execute and with that we are getting only the customers from Germany. So it is very simple and this is why we use
the equal operator. Okay. So now moving on to the next one again very simple. If you want to check if two values are not
equal we can use the not equal operator. So let's have an example. Okay. So now we let's have the opposite task. It says
retrieve all customers who are not from Germany. So this is very simple. We are saying here who are not they are not
equal to Germany. So we can use the not equal operator in order to get these customers. So with that as you can see
after executing we are getting all the customers country is not equal to Germany and there's like another way on
how to do the not equal doing it like this we'll get the same results. All right my friends moving on to the next
one. We can check if a value is greater than another value. So we use the greater operator. Let's have an example.
Okay. So now the next task it says retrieve all customers with a score greater than 500. Now we want to filter
the data based on the score. So we're going to say where score and now the task says greater than 500. We're going
to use the operator greater than 500. It's very simple. So with that we will get only the customers where the score
is higher than 500. So for example Maria it's not fulfilling the condition. The same thing for the Peter and as well for
Martin it must be greater than 500. So if you go executed you will get only those two customers because they are
greater than 500. Okay, moving on to the next one. This time we're going to check if a value is greater than or equal to
another value. So it is like mix between the greater than and the equal. If one of them is fulfilled then the value
going to meet the condition. So let's have an example for that. Now, if the task says retrieve all customers with a
score of 500 or more, this time we're going to go and include the customers where their score is equal as well to
500 or higher. So, we're going to have a similar condition based on the score and the 500's value, but this time we're
going to say greater or equal to 500. So, if you go now and execute it, this time we're going to see the customer
Martin with the score of 500. So, in this scenario, we're going to use greater or equal. All right. Right. So
now let's keep moving. The next one is as well very simple. We're going to check this time if a value is less than
another value. So we're going to use the less operator. Let's have an example. Now moving on to another simple task.
Retrieve all customers with a score less than 500. So this time we want all the customers with a lower score. And we're
going to use exactly the opposite. It's going to be the score is less than 500. And again here it is not equal, right?
So if you go and execute, you will get all the customers with a low scores. he will not get to Martin because Martin is
equal to 500. So with that we have solved the task. We have all the customers with the score less than 500.
Okay my friends, now moving on to the last one. I think you already got it. So we're going to check whether a value is
less than or equal to another value. So you can go and combine the less operator together with the equal and if one of
them is fulfilled then the value going to meet the condition. So let's have an example for that. This time we are
retrieving all customers with a score of 500 or less. So the query going to be very similar but we are saying it is
less or equal to 500. So we are including the value in our condition. And with that as you can see we still
have our two customers where we have the score less than 500 but we have now as well Martin with a score of 500. Okay my
friends. So with that we have covered the first group the comparison operators. Now we're going to move on to
the next group. We're going to speak about the logical operators and here we have three and or not. So let's start
with the first one. What is exactly and operator. Okay. So now what is the definition of the and it says all
conditions must be true. So all the conditions that you have in the wear clause must be true in order to keep the
row in the results. So let's understand what this means. things going to get more complicated where you can have not
only one condition but you might have multiple conditions in your query. So here we're going to add a second
condition where we're going to say not only the country must be equal to USA but also the score must be higher than
500. So now you have two conditions and you have to put them in the wear clause. Now you have to combine those conditions
using the logical operator and here we have two options two operators the and operator and the or operator. In this
scenario, if you say and then SQL is very restrictive. Both of the conditions must be true in order to keep the row in
the results. So now let's see how this going to work. Now for the first row and for the first condition you can see the
country is Germany and it is not fulfilling the first condition. So this going to be false. And as well if you
check the second condition for the first row you can see the score is 350. So that means this customer is as well not
fulfilling even the second condition. So both of the conditions is false and it's going to go I remove this customer from
the results. Now to the next one John you can see John is fulfilling the first condition because the country is equal
to USA and as well fulfilling the second condition. His score is 900 and this is higher than 500. So now SQL going to be
very happy about it because both of them is true and this is the only way in order to keep the row in the output
because we are using the operator and so John going to stay in the output. Now moving on to George. He is not
fulfilling the first condition. But now the second condition is fulfilled. His score is 750 and this is higher than
500. So now it's like 50/50 right. In one side it's false but the other side is true. But this is not enough for the
ant operator. Both of them should be true in order to keep the result in the output. That's why SQL going to remove
this row. Now moving on to Martin. He is not fulfilling both of the conditions. So SQL going to go I remove it from the
results. And now for the last one. Peter is fulfilling the first condition. the country is equal to USA but the second
condition is sadly not fulfilled so we have the score zero not higher than 500 again we have the same scenario it's
50/50 and this is not enough for the ant operator that's why SQL going to go I remove it so as you can see if you use
an and operator a lot of rows going to be removed if one of the condition is not met so the ant operator is very
restrictive both of the conditions must be fulfilled to keep the row in the results so this is exactly how the and
operator works. Okay. So now we have the following task. Retrieve all customers who are from USA and have a score
greater than 500. So here we are like combining multiple conditions and let's go and do it step by step. So the first
thing that we have to go and select the data from the correct table. So select star from customers and with that we are
getting all the customers from the table. Now the first condition we need the customers that come from USA. So we
need only those two customers and in order to do that as we learned we can go and use the wear clause and the
condition going to be country equal to USA. So if you go and execute we will get those two customers. Nothing is new.
We have used the compression operator equal. But we are not done yet. We have another condition from those two
customers. We need only the customers where their score is higher than 500. So now by looking to those two customers
you can see we see that the bitter here does not have a score higher than 500 and we don't want to see that in the
results. So now what we have to do we have to go and write a condition for this one over here. So this is based
this time on the scores not on the country. So the score should be greater than 500. Now as you can see we have the
first condition for the first one here and the second condition for the second requirement. Now the question how to
connect those two conditions. So here we have two options and or and to be honest this is very simple the task says it
customer should fulfill both of the conditions should be from USA and as well at the same time greater than 500.
So it is very simple real and so with that we have connected both of those conditions and if you go and query it
you will get only one customer that is fulfilling our conditions. So from all customers we have only one customer
that's fulfilled this condition that comes from USA and at the same time the score of this customer is higher than
500. So this is how we use the ant operator in order to connect two conditions. Okay my friends. So that's
all for the ant operator. Let's speak now about the or operator. All right. Now the or operator
it says at least one condition must be true. So it is less restrictive than the and it is enough to have one condition
true in order to keep the row in the results. Let's understand exactly what this means. Okay. So now we have the
same scenario. We have two conditions and in SQL you have to connect them either using the and operator or the or
operator. In this scenario we're going to talk about the or operator. And as we said at least one of the conditions must
be fulfilled in order to leave the record in the results. So let's see what's going to happen here. Now the
first customer Maria she is not fulfilling the first condition and as well the second condition. So both of
them is false and this is the only scenario where SQL going to remove the record from the results because it is
not fulfilling the minimum at least one of them should be true. Both of them is false then SQL going to go and remove
this row. Now moving on to the next one to John. John is from USA and has higher score than 500. Both of the conditions
is green. So both of them is true and this is more than enough to keep the row in the output. That's why we will see
John in the outputs. Now moving on to the third one, George. George is not fulfilling the first condition because
UK is not equal to USA. But John this time is fulfilling the second condition. So we have here true and since we have
at least one true, this is good enough to keep the record in the output. So you will see George in the results. Now
moving on to Martin. He is not fulfilling the first condition as well not fulfilling the second condition.
Both of them is false and this is not enough to keep the result in the output. So that's why it's still going to go and
remove it. Now moving on to the last one. Peter he is fulfilling the first condition but not the second condition
but still everything is fine because he is fulfilling at least one condition. So we have the minimum and it's still going
to leave it in the output. So as you can see the or operator is not restrictive like the and operator. It's enough to
have one true in order to keep the data in the output. And this is exactly how the or operator works. Now let's see the
second task. Retrieve all customers who are either from USA or have a score greater than 500. So it is a very
similar task. We have two conditions. So we need the customers that are either from USA. So it is based on this country
equal to USA. And the second condition is the score is greater than 500. But this time we are very relaxed. either
this condition is fulfilled or the second one. So instead of having and we will be using the operator or. So it is
enough to fulfill one of those conditions. And if you go and execute now as you can see we are getting more
results because it is easier to fulfill the conditions. So we can see those three customers either fulfilling the
first condition or the second one. All right my friends. So that's all for the or operator and we're going to move to
the last one in this group the not. So what do we mean with the not operator? Okay. So now what is this operator not?
It is a reverse operator. It's going to go and exclude the matching values. So what this exactly means? Let's have a
very simple example. All right. So now the net operator is not like the or and the ands. This operator will not go and
combine two conditions. So you can use it with only one condition. And let's say that our current condition is like
this. The country must be equal to USA. So this is like a comparison operator. And if you apply it to your data, as we
learned, it's going to leave only two customers, John and Peter, because they fulfill the conditions and all other
customers will be removed because they don't fulfill the condition. So nothing crazy so far. But now if you go and
apply the not operator to the condition, what going to happen? You're going to reverse the whole truth. So you are
saying if this condition is fulfilled, it must be removed from the final results. So it is switching everything.
We want to see the customers that is not fulfilling the condition. So now let's see what can happen if you apply the not
operator together with the condition. We can see that the first customer is not fulfilling the condition which is great
thing. This is exactly what we want. We want the customer that is not fulfilling the condition. That's why going to be
happy about it and SQL going to make it true and leave it in the output. So Maria is fulfilling the whole thing. She
is not meeting the condition. So SQL going to leave it at the output. Now for the next one. So this customer is
fulfilling the condition and that is not a good thing. So SQL going to go and this time remove John from the results
because he is fulfilling the condition. And moving on to George. So George is not fulfilling the condition which is
amazing. So that's why SQL going to keep this time George in the output. The same thing for Martin. Martin is not
fulfilling the condition. So Isl going to keep the customer and better he is fulfilling the condition. So SQL going
to go and remove this customer from the output. So as you can see we have reversed everything right. The not
operator going to make the true false and the false true. Okay. So this is how it works. Now let's go back to SQL in
order to practice. Okay. The next task it says retrieve all customers with a score not less than 500. So this sounds
really funny. As usual we're going to go and select star from customers. And now we have to filter the data based on this
condition. So the score is not less than 500. Well, you can go and say well the score is higher, greater or equal to
500, right? And with that it is not less than 500. So if you go and execute it, we just solve the task, right? We get
all the customers that are not less than 500. Or you can go and use the not operator to make things more funnier. So
you go over here and say it is not and then you switch it. So you make like this. So the score is less than 500. But
as we use here not then we twisted everything. So we are saying the score is not less than 500. And if you execute
it you will get the exact same results. Convert the truth. If you remove it and execute you will get everything that is
less than 500. But if you put the nut you will convert the whole logic. So if you go and execute you are not getting
the scores that are less than 500. So this is really nice. This is how you use the nut operator. Okay my friends. So
with that we have covered everything about the logical operators. Now we're going to move to the third group. We're
going to talk about the range operator. And here we have only one the between. So what is exactly between
operator? Okay. So what is between? It's going to go and check if a value falls within a specific range. So you have a
range and you are checking whether your value is in the range or outside the range. So let's understand exactly what
this means. Okay. So now in order to build a range you need two things. You need the lower boundary for the range
and you need as well the upper boundary. Once you have two boundaries then you have a range and everything between
those two boundaries going to be true and everything outside those boundaries going to be false. So now for example
let's say that we have the lower boundary 100 and the upper boundary 500. And there is one thing that you have to
understand about the between the boundaries are inclusive. So that means if a value is exactly 100 or exactly 500
then it's going to considered as a true. So it is considered to be inside the range. Now if you apply this filter to
our data where we say the score must be between 100 and 500 going to go and do the following. So for the first customer
Maria is going to go and check whether her score is inside the boundaries. So as you can see 300 is between 100 and
500. So she is in the green area and that's why Isque going to be happy about it and leave the customer in the
outputs. Now moving on to John. John has 900. As you can see 900 is greater than 500. So this value is going to be
outside the boundaries on the right side and this means the score of John is not in the range. That's why he is not
fulfilling the condition and SQL going to go and remove this customer from the results. Now moving on to George 750.
The same thing outside the range. SQL will not accept it and remove this customer from the final results. Now
moving on to Martin his score is 500 and this is exactly at the boundary. So if it's like 5001 it's going to be outside.
So since between is inclusive then SQL going to accept it and Martin considered to be in the range and fulfilling the
condition. So SQL going to keep him in the final result. Now here are speaking about better he has zero score and this
is less than 100. So in the left side not in the range. So not fulfilling the condition and SQL going to go and remove
him. This is exactly how between works in SQL. It's very simple. Okay. So now we have the following task and it says
retrieve all customers whose score falls in range between 100 and 500. So let's start as usual by selecting all data
from customers and execute it. Now the task says everything. We need all customers in a range. So we have a lower
value and a higher value. So in order to do that as usual we're going to use the where and then we're going to specify
the column that we want to filter on. So it's going to be the score and since we have like two boundaries we can go and
use the function between and we start with the first boundary the lowest boundary. So it is the 100 and 500 the
high boundary the upper boundary. So between 100 and 500. So now let's go and execute it. And with that we get only
those two customers because they are between this window. Now there is another way in how to solve this task by
not using between. We can go and use the comparison operator together with a logical operator and. So let me show you
how we can do that. I'm going to go and copy the whole thing. And now we're going to write two conditions. So first
the score should be higher or equal to 100 because the boundaries is inclusive and the other one the score is less or
equal to 500. So this is the upper boundary. So with that we have the two conditions and we can go and connect
them using the and operator. So it's like very similar to the between we have an and between the upper and the lower
boundaries but we are using the comparison operators. So it is higher or equal to 100 and lower or equal to 500.
If you go and run this query you will get exactly same results. Now if you ask me which method is my favorite I'm going
to go with this method and I will skip the between because each time to be honest for me I forget about the between
whether the boundaries are inclusive or exclusive. But if I read the script I am going to see exactly that those
boundaries are inclusive because we have here the equals. So I really prefer using the compressor operator together
with the and then using between. So it's up to you if you memorize it then go with the between. But for me I'm going
to go with the compression operators. Okay my friends. So that's all about the between and the range operator. Now
let's move to another group. We have the membership operator. So here we have like two. We have the in and the not in.
So let's understand what this exactly means. Okay. So what is in operator? It's going to go and check if a value
exist in a list. So you have a list of values and you are checking whether your value is a member of your list. So let's
have very simple example in order to understand what this means. Okay. So now how this works exactly what you have to
do is to go and make a list of values. So let's say that I have a list and there I have specified two values
Germany and USA. So those two are the members of this list. Now if you use the n operator it's going to go and check
the value of countries whether it is in the list or not. So let's do it one by one. For the first customer Maria her
country is Germany and Germany is member of the list. So it's going to be happy and going to leave Maria in the final
results. Now moving on to John. John comes from USA. USA is member of the list. So he is fulfilling as well the
condition and you're going to see John in the final results. Now we come to George. George comes from UK and UK is
not member of our list. And SQL going to go and remove this customer from the final results not fulfilling the
condition. Now for the last two, Martin and Peter, their country is a member of the list and SQL going to go and leave
those customers in the final results. So as you can see it's very simple. Or what you have to do is to define the members
of a list and use the n operator and if the value is a member of this list it's going to be true otherwise it's going to
be false. Now of course the other operator going to be exactly the opposite where we say not in the list.
So we are searching for values that are not in this list. So as we are using not it's going to go and reverse completely
the truth. And if you apply this you will get in the result only one customer. you will get George and the
result because the country is UK and UK is not a member of the list. So if you use not together with the in operator
you will get exactly the opposite effect. So this is how the in and the not in operator works in SQL. Let's go
back to scale in order to practice that. Okay. So now we have this task and it says retrieve all customers from either
Germany or USA. Okay. So let's try to solve this task. This going to be a little bit tricky. So select star from
customers as usual and execute it. So now we need in the results only customer that comes either from Germany or USA.
So that means this customer over here should be excluded from the result because he come from UK. So how we going
to write it? It's going to be like this maybe. So the first one going to be the country is equal to Germany or the
country is equal to USA right something like this. So if you go and execute it, you will get in the output only the
customers that are either from Germany or USA. And with that we have solved the task, right? Well, there is another way
in order to solve this task which is more clear and shorter using the n operator. So now how we going to do it?
Let's go and get the whole thing in another query. And now instead of having equals and ors and so on, we're going to
use the in operator and then we're going to have like two parentheses and then inside it we're going to have a list of
values. So it's going to be the Germany and then the second value going to be USA like this. So we are saying country
should be in this list Germany or USA and if it is like one of those values then the condition is fulfilled. So now
if you go and execute this one over here you will get the exact same results. So my friends, if you notice that you are
repeating yourself in the wear condition and you are just changing the value of the condition, it is based on the same
column and you are connecting them using the or then there is something wrong and always think on this scenario to use the
in operator because this can be really ugly once you have a lot of values. So imagine in our database we have a lot of
countries and your query going to be like something like this. So you are keep repeating country equal or country
equal and so on. Instead of that you're going to have a really nice list of countries in one go. So this is as you
can see here it is easier to extend and as well has better performance. So as you can see we are repeating the same
thing but we are just changing the value and we are connecting all those conditions using the or in this scenario
go and use the in operator. All right my friends. So that's all for the membership operators. Now we're going to
speak about the last one the search operator. And here we have only one the like. And each time we're going to say
like, I'm going to remind you to like this course. So let's go. Okay. So now what is like operator?
You can use it in order to search for a pattern in your text. So if you have like a text or characters and you are
searching for a specific pattern inside the text. So let's have an example in order to understand exactly what this
means. Okay. So now if you don't have yet cafe, go grab one because you have to focus for this one. Now what we have
to do is to define a pattern in is scale. In order to build a pattern we have like two special characters. If you
use a percentage you are saying anything. So I'm going to accept anything. So it could be no characters
at all or only one character or many characters. So I'm saying anything. Now if you use an underscore you are
expecting to have exactly one thing like one character or one number. So it is exactly one. I know this sounds
complicated but with an example you can understand this. And I can tell you the percentage is way more famous than the
underscore. I rarely really use the underscore. So now let's say that I build the pattern like this. I say the
first character must be M and then percentage. So here I'm saying in my text the first character must be an M
and after the first character I really don't care. It could be any character, any number whatever. So this is the
pattern and now let's have few values in order to say whether it's true or false. So now if you have the value Mariam. So
now you can see the first character is an M which is perfect. This is exactly our pattern. The first character must be
an M. And then after the M we got like four characters. So whatever it is totally fine. We can say Maria is
fulfilling our pattern. And this is exactly what we are searching for. This value is fulfilling the condition. Okay.
Now moving on to the next value we have m a. So here again the first character is an M which is perfect. And after that
we have only one character a. Well we have say percentage. So it could be anything one character multiple
characters a number or whatever. So that's why this value can match our pattern and we will see it in the
outputs. Now moving on to the next value we have only one m which is as well totally fine because we are saying the
first character must be an M and then followed with anything. Now moving on to the last scenario we have Emma. Now this
is a problematic because the first character is an E and in our pattern we say it must start with M. So we don't
have that in this word. The first character is an E. That's why this value is not fulfilling our pattern and SQL
going to remove this value from the final results. So this is exactly what going to happen if you have this pattern
and those values. Now let's have another scenario where you say you know what it could start with anything but for me it
is very important the last two characters it must be an I and N. So we could start with anything but the last
two must be an I and N. So let's take this value Martin going to go and check immediately the last two characters. So
you can see we have an I and N and the first part marks it is fine. It could be anything. So this value is fulfilling
the condition because the last two characters is an I and N. Now moving on to the next one we have vin. So v i n
the last two characters is as well exactly what we are searching for. It is fulfilling the condition and we have
before it like only v. So we say anything with a percentage. Right? Now one more we have in. So it is as well
fulfilling the condition because before it we don't have anything. So en is fulfilling as well the condition. The
percentage is always saying anything. Now moving on to the last scenario we have Jasmine. They are not the last two
characters. The last two characters is an N and E and this is not matching our pattern and this why this value is not
fulfilling our pattern and you will not see it in the results. So with that you can understand how we can search for
something in a text using the like operator. Let's keep going. Now let's say that I have a percentage at the
start and percentage at the end and in between I have only one character an R. If you define it like this you are
saying if there is an R anywhere it is good enough whether it's beginning or at the end or in between then the condition
is fulfilled. So if you have Maria you can see we have an R in the middle. So in the left side we have two characters
on the right side we have two characters doesn't matter the main thing we have an R somewhere. So this going to be
fulfilling the condition. Now moving on to better we have an R at the end and that is totally fine cuz we say at the
right side it could be anything. So we have an R somewhere that's why it's going to fulfill the condition. Now we
have another case where we say Ryan we have an R at the start. So we don't have anything before and we have after that
like three characters which is totally fine. So we don't really care about the position of the R. It is totally
acceptable to have an R anywhere. And if you have only an R that is as well good enough. You don't have anything before.
you don't have anything after and that's okay. But if you have a word like Alice, we don't have any R inside it. So that's
why this is the only case where you say we don't have here an R and it's going to remove this value from the results.
And this way of searching of something is very famous. You don't care about the words before this word and after the
word, right? So if you are searching for any word, you're going to say percentage before and percentage after. Now I know
that we want to practice with the underscore. So let's say that I have two underscores and then the character B and
then a percentage. So here what I'm saying there should be something in the first position. There should be as well
something in the second position. Then the third position should be the character B must be exactly at this
position and after that it could be anything. So we really don't care. I know this is a little bit complicated.
Let's have an example. So we have the value alert. Now we can see the first position we have something the A. Then
the second position we have as well something the L. So so far we are good at the pattern and then the third
position we have B. So we have complete match and the rest the ERT whatever. So with that Albert is matching our
pattern. Moving on to the next one rope. You can see the first character we have something which is good. We have the R.
Then the second character we have an O. So it's not empty. We have something. And then the third one we have exactly
B. And after that we don't have anything which is fine. So again this value going to fulfill the condition. So moving on
to the next one. So it start with an A. So we have something in the first position. The second position we have as
well something the B. But now the third character it is a problem. It is not P. We have an E. So that's why it is not
following our pattern. And is going to go and remove it. Now moving on to last example we have an A and an N. So in the
first position we have something. The second one as well. But the third one we don't have anything. We don't have a B.
So that's why it's going to be removed. So my friends I know that was a lot. This is exactly how you build a pattern
for the like operator using the percentage and the underscore. But the percentage is more famous. So this is
exactly how it works. Let's go back to scale in order to have some examples. All right, let's start with this task.
Find all customers whose first name starts with a capital M. So let's go and start searching for those informations.
We're going to start as usual. Select star from customers. And now we have to go and build the filter logic. So we're
going to say where. Now we are searching something in the first name. So we're going to say first name. So that means
it is very important to start with an M and then the rest it doesn't matter. So we're going to use the like operator in
order to search. And we're going to have our single quotes and we're going to start with the M. And it doesn't matter
what comes after that. So for us it is very important that the first character is an M. Let's go and execute it. And
with that we got our two customers Maria and Martin. And both of them starts with an M. So with that we have solved the
task. It is very simple. Now we have the following task. Find all customers whose first name ends with an N. So let's go
first and select all the customers here. And we need all those customers where they are having an N at the end. So we
have John and as well Martin. So how we going to do it? The same thing where first name like since we are searching
but here we're going to change the expression. So it must ends with an N as a last character. So before that it
doesn't matter whether it is the first character. So it could be anything but the last character of the word should be
an N. So that's it. Let's go and execute. And with that we got John and Martin because the last character is an
N. It is very simple, right? It is all about where we're going to place this percentage. Okay. So now we have the
next task. Find all customers whose first name contains an R. So here we don't have like specifications whether
it is at the start or at the end. Somewhere there should be an R. So if you go and execute first without any
wear condition you can see here for example Maria we have in the middle somewhere an R George George as well
Martin and Peter at the end. So we have a lot of names with an R. So how we can search for that? We're going to stick
with the where first name like and here our character going to be an R and we're going to put before it and after it a
percentage. So it doesn't matter what is before it or after it somewhere there should be an R. So let's go and execute
it. And with that we got all our customers where somewhere we have an R. As you can see it is very simple. If you
put it before and after then you are open for more results. And this is usually used a lot in order to search
for a value inside your database. All right. Now we're going to move to a funny one. It kind of says find all
customers whose first name has an R in the third position for some reason. I don't know why. So let's go and execute
our customers here without any filter. So it is for us very important to find the customers where in the third
position we have an R like here for example Maria the third character is an R which is okay but with Peter over here
it is not the third character so it is not fulfilling the condition. So how we going to write that? It going to say
like this where the first name like but we have to write it now from the start. So the first position going to be an
underscore the second position going to be as well an underscore and now in the third position going to have an R. So
with that we make sure the third position and an R and before it we have two positions and now afterward it
doesn't matter what comes after that it could be nothing or characters. So if you go and execute it like this we will
get Maria and Martin and we will not get Peter because the R is not in the third position. So now if you don't do it
correctly with the underscores let's go and remove one of them and execute. You will get nothing because we don't have
any first name where the second position is an R. So you have to be very careful with this. All right my friends. So this
is how you search inside your values. And with that we have covered all different groups of operators that you
can use inside a wear clause. So with that you have learned how to filter your data using multiple operators that you
can use inside the wear clause. So you can filter anything now in SQL. Now we will move to very interesting topic. You
will learn how to combine your data from multiple tables. And here we have two main methods. The first one is SQL joins
and the second set operators. And they are really big topics. So we're going to first focus on the SQL joins. And here
we have a lot of things to cover. So now we are talking about the core of SQL. So let's
go. All right. So now we have two tables, table A and table B. And the big question here is how to combine those
two tables. What do we want exactly? Do you want to combine the rows or the columns? And now if you say I would like
to combine the columns then we are talking about joining tables. So we're going to use joins in SQL. So now let's
say that we are joining the table A with the table B and we start from the table A. So SQL going to take the columns and
the rows of the table A and SQL going to call it the left table because we started from there and then we join it
with the table B and SQL going to call the second table as the right table. And here what's going to happen? and SQL
going to take the columns and the rows from the right table and put it side by side with the columns and rows of the
table A. So we are like combining the columns we are putting them side by side. And now if you say you know what I
don't want to do that I would like to combine the rows both of the tables having the same columns. I just want to
stack them. So we are now talking about another methods. It is called the set operators. So here there is like no left
and right. So since we started with the table A, the SQL going to take the columns and the rows of the table A and
put it in the results. And then it's going to go to the second table, table B and it's going to take only the rows and
put it below the rows of the the table A. So we are putting the rows beneath each others. We are doing like
appending. So that means as we are using the set operators, we are combining the rows. Our table going to be longer but
with the joins we are combining the columns side by side and we are getting wider table. But now for each methods
there are different types. So now for example in order to do the joints we have four very famous types. We can do
an inner join, full join, left join, right join. But of course there are more than that but those are the basics. And
for the set methods we have as well types. We have the union, union all except and intersect. And for each
methods there are like different rules. In order to join the tables we have to define the key columns between the two
tables. Don't worry we're going to learn about that later. This is the requirement in order to join tables and
the requirement of combining tables using the set operators the tables in your query should has the exact same
number of columns but here you don't need any like key in order to combine the tables. So guys if you look at this
in order to combine two tables first you have to decide do I want to combine the columns or the rows. So first you have
to decide in the methods and after that you have different types on how exactly you're going to go and combine the data
and of course there are rules that you have to follow. Now, of course, we're going to go and cover everything in the
course, but now in this section, we're going to learn how we're going to combine the tables using the SQL joins.
So, we're going to go and dive into this word. All right. So, now what is exactly SQL joins? Now, let's say that we have
two tables. On the left table, we have the customer name. So, we have four customers. And on the right table, we
have the country informations about the customer. And now we would like to query both of those informations the names and
the countries. Now in order to query those two tables in one query first we have to connect them. And in order to
connect those two tables we need a key a column that exist on the left and on the right sides. And by looking to this the
common column here is the ID of the customer. Now once we connect those ids together we will be able to query those
tables together and SQL going to start matching those ids. So for the ID number one, we will get the name Maria and the
country Germany. And the ID2 is connecting John to USA. And now you can see the ID3 is not connectable. So we
cannot connect it to the right side. But for the ID4, we can use it in order to connect Martin to Germany. So this is
exactly what happens if you join two tables. You connect those two tables using a common column, a key like the
ID. And once we have matching value, we can connect the two rows together. So this is what we mean with SQL
joins. Now you might ask why do we need actually joins? Well, the first and very important reason is to recombine your
data. So now usually in databases the data about something like the customers could be spreaded into multiple tables.
Like we could have table called customers, another one where we have the customer addresses and a third table
where you can find the orders of the customers and maybe another one where you can find the reviews of the
customers. So as you can see the data of the customers is spreaded into like four tables. Now how about I would like to
see all the data about the customers in one results. So I would like to see the complete big picture about our
customers. What we can do, we can go and connect those four tables using the SQL joins. And once we do that in one query,
I will be able to combine all those tables in one big results. And this is the most important reason why we use SQL
joins in order to combine all the data about specific topic in order to see the big picture. Now, another reason why we
use SQL joins is to do data enrichment. It is where I want to get an extra data and extra information. So let's say that
you are querying the table customers and this is your main table the master table. So you are able to see all the
data that you need but sometimes what happens you would like to get an extra information from another table like for
example the zip codes of the countries. So you would like the help of another table we call it a reference table or
sometimes lookup table where there is like one extra information that you would like to add it to your master
table to the primary source of your data. So now what we can do we can join those two tables in order to enhance our
table. So we are getting one extra relevant informations for the customers and this process we call it data
enrichments. I'm getting an extra data for my main table. So this is another reason why we use joins. All right. So
now so far we have used joins in order to get the data from two tables. But now there is another use case for the SQL
joins. We use it in order to check the existence of your data in another table or maybe as well the not existence. So
let's say that I have a table called customers and I'm working with this table and doing queries. But now I would
like to check something. I would like to check whether our customers did order something. Now in order to check that I
need the help of another table for example the table orders. So that means I'm using the table orders only for my
check. So I don't want to get any extra data from the orders in my final results. I'm just using the table orders
and we call in this table a lookup. So now what we can do we can connect those two tables together. And now based on
the existence of the customers inside the second table the orders either the customer going to stay in the final
results or going to be removed. So that means I'm filtering the data based on the join. And of course I can check as
well the net existence. I would like to see in the final results all the customers that didn't order anything. So
it is the same scenario. So my friends, those are the main three reasons why you use SQL joins. First, if you want to
combine the data from multiple tables in one big picture. So I use join in order to get the data from different tables.
The second use case, you are working with one table but you would like to get an extra information from another table.
So you are doing it like something called data enrichments. And in the third scenario, we don't want to combine
the data. We want just to join it with another table in order to do a check to check the existence of your records in
another table. So this is why we need joins in SQL. Now there is like a lot of
different possibilities on how to join tables, how to join the data. Now in order to make it easy to understand,
we're going to visuals as like two circles. So we have the table A and a table B. The table A is on the left
side. We call it the left table. And the table B going to be on the right side and we call it the right table. The side
of the tables is very important. Now if you combine those two circles, you will get three different possibilities. The
circles going to overlap. And here exactly where we can have the matching data between the two tables. So the data
is available on the left and on the right. Or another possibility you want to get all the data from one of the
tables. So you can get all the rows from one circle. And the third possibility you want to get only the unmatching data
from one table. So if something exists in one table but not in the other table then we call it unmatching data. So
those are the three scenarios that you have to ask yourself once you are combining tables and this can generate a
lot of join types. So here we have like basic SQL joins those are the classical one and here depends on the scenario
whether you want only matching all or all the rows from either left or right and we have advanced SQL joins where we
focus on the unmatching data. Now we're going to go and cover all those types one by one. So we're going to start
first with the basics and the first option that you have is to get all the data without joining tables. So let's
see what this means. So what do we mean with no join? Well, we want to returns the data from two
tables without combining them. So actually this is not a joint type because we are not combining anything.
We just want to query the data from two tables. So that means from the table A we want to see all the rows everything
and from the table B we want to see everything as well all the rows. So that means we want to see two results and
there is no need to combine them. So let's see the syntax of that. So all what you have to do is very simple.
Select star from table A and then semicolon and then start another query. Select star from table B. So that's it.
And of course since we are not combining the data there will be no join in the syntax. So that's it. Let's go to SQL in
order to do that. Okay. So now we have the following task. It says retrieve all data from customers and orders in two
different results. So that sounds that we don't have to go and combine the tables together. And all what we can do
is the following. We can go and select the data from the first table like this and then we make another query for the
second table the orders and we don't have to go and combine them in one big query. We just use a very simple select
statements in order to retrieve the data. So if you go and execute it since you have two separate queries you will
get two results and with that in one result you will get all the customers and in the other result you will get all
the orders and the data is not combined at all. So this is how you query two tables without combining them. So with
that we are getting all the data without joining the tables. Now we're going to start talking about the first type of
join the inner join where we start combining the data from two tables. So let's
go. Okay. So now what is exactly an inner join? So this type going to return only the matching rows from both tables.
So that means we will see in the output only matching rows. So now what do we need from the left table? We want only
the matching data. So we will not get the whole circle of A. We will get only where we have an overlapping with the
table B. So we want to see the data from A only if it exists in the table B. And now what do we need from the table B?
Exactly the same thing only the matching data. So that means I don't want to see all the data from B. I want to see only
the data in B that has a match from the table A from the left side. And with that you will get only the matching data
from both tables. Now let's see how we can write that in SQL. So it is a usual query and always we start with a select.
So we select for example all the columns from and here we specify the table name. So it's going to be a. So so far nothing
new. But now we want to add as well the table B in the same query. In order to do that we use the keyword join and then
we say table B the name of the table. And since we have like different types of joins in SQL, you can specify the
type of the join before the keyword join. And if you don't specify anything, the default type is inner join. But my
friends, the best practices is always mention the type. I don't like to skip the defaults because in projects maybe
not everyone is aware of the defaults. So don't skip that. Always specify the type. So now what we're going to do,
we're going to put the keyword inner before the join. And with that SQL going to know how to deal with the rows
between two tables. But still we are not done there. We have to tell SQL how to combine the tables. And with that we use
the keyword on. And after that you specify the join condition. And as we learned in order to join two tables we
have to find out a common column in order to match the data. Right? And usually in scale they are the keys or
ids. So the condition can be like this. the key from the table A must be equal to the key from the table B. So this is
the join condition and using this join SQL can go and start matching the data from the left table and the right table.
And there is one thing that is very important while you are joining the tables you have to understand about the
order of the tables in your query. Now in the inner join the order of the tables doesn't really matter. So whether
you start from A or you start from B it doesn't matter because you will get the same results. Both of the tables has the
same priority and it doesn't matter where we start whether we say from A join B or we say from B join A we will
get the exact same results. So in the inner join you don't have to worry about the order of the tables. So that's all
about the inner join. Now let's go back to scale in order to practice. Okay. So now we have the following task and it
says all customers along with their orders but only for customers who have placed an order. So my friends that
means we need the data from the customers and from the orders from two tables and we have to put everything in
one results. That means we have to join two tables. Now let's go and do it step by step. So we're going to go and say
select star from customers and then we have to go and join it with the orders. We're going to say join orders. Now you
have to go and specify the join type. Is it inner, left, full and so on. Well that's depend on the task. It says we
want all customers but only for customers who have placed an order. So there is like condition right here. We
don't want to see everything from the customer. We just want to see only the matching data only if the customers has
an order in the orders table. And for that we can go and use the inner join. Of course if you can leave it like this
you will get the same effects but I'm going to go and specify it like this inner join just to make it clear. We are
speaking about the inner join. And after that we have to go and specify the join condition. So we have to go and find a
common column between the customers and the orders. So how I usually do it I go and explore both of the tables. So I'm
going to go and select everything from customers and as well everything from the orders. So let's go
and execute. Now we're going to start searching where do we have a common column between those two tables. So we
have the from the first table first name, country score and you don't find any of those informations in the second
table. The only one is the ID. So the ID of the customer and the ID of the customer you can find it in the orders
the second column here. So this is the common column between those two tables. And usually in databases we create ids
exactly for this in order to connect tables. So it's really rarely that we're going to use like a country or score or
first name in order to join tables. We usually use the ids. So let's go back to our query and use those two columns. So
it's going to be the ID from the customers equal to the customer ID. So that's it. With that we have the
condition we have decided on the type and we can go and execute it. Now you can see we are getting only three
customers. Right? If you don't apply the inner join we can see that we have five customers. So that means actually we
have two customers without any orders any matching data from the other table. And as well you can see very nicely we
have now not only the columns from the customers but as well all the columns from the orders side by side. So with
that we have combined the data and as well with that we have solved the task but we will not leave our query like
this because it is not really good practices. What we have to do is to go and select only the columns that really
make sense in our query because in many cases in your tables you will have a lot of columns that is not needed like for
example if you check here you see we have the customer ID here and as well the customer ID over here. So it's like
repetition and it's enough to see it only once. So what you have to do is to go and pick few columns that we want.
For example, I'm going to start with the ID maybe the first name and that's all from the first table. Let's go and get
the order ID and I don't want the customer ID again. So from the second table I'll get add the sales. So let's
go and execute it. And with that you can see very nicely the customer's name and their orders with the sales. And now
comes something very important. Sometimes if you have two tables you might have columns that having the same
names. Like imagine the order ID in the table orders it's called ID. So that means we have the same name in both
tables and this kind of makes SQL very confused. And here you will get an error tells you I really don't know what do
you mean with the ID. Is it from the table customers or from the orders? So we have to tell SQL exactly from which
table did this column come from. So in SQL in order to do that what we do before the column name you write again
the table name the customers and then you make a dot and now we are telling SQL this column the ID it comes from the
table customers and SQL will not be confused about it and it's going to go and get the ID from the customers. And
for the second id you can go over here and as well before it you say orders do id so that knows okay this ID come from
the orders and the other one comes from the customers and it is always good practice especially if you are joining
tables to always assign for each column a table because after a while if you open your query and you see okay the
sales does the sales come from the customers or the orders and if you have a long list of columns it's going to be
really confusing so that's why we consider it best practices if you always assign for each column the table name
especially if you are doing joins. So it's going to be like this. But of course if you have like only one table
it's clear that all the columns in the select comes from this table. But since here we are dealing with multiple tables
it is good to show it like this. And of course here we don't have the ID. We have the order ID and the same thing for
the join condition. So the ID from here comes from the customers and the customer ID come from the orders. So now
it is clear for everyone which column come from which table. But now you might say you know what each time I have to
write the customers this is very long name and sometimes in real projects you're going to see tables that has
really long name and it's going to be really annoying to add it each time before each column right so instead of
that we can go and assign aliases for the tables but only for the columns so usually we go over here and say as and
maybe you can go and use only one character like the first character C. And now instead of saying customers you
can go over here and say C. The same thing for the second column and as well over here. And you can use now the C in
everywhere in your query. The same thing for the orders. You can go over here and say has O. And now instead of orders you
say O on here. And now it is very easily to see those two columns comes from the C
that means the customers and those two columns comes from the O the orders. Those are the best practices as you are
joining tables together in SQL. And of course with that we have solved the task. And about the order of the tables,
it doesn't matter where do you start. So for example, if you take the orders here and put it in the join and get the
orders in the from. So I just switch the tables and execute it, you will get the exact same results. So if you are doing
inner join between two tables, don't worry about the order of the tables. Okay. So now let's go and instant
exactly how executed the inner join. Okay. So now again here we have our query. Then we have the two tables
customers and orders. And here we have the ID where we are joining the data. So this is the ID from the table customers
and this is the customer ID that we have in the orders. Now let's see how SQL can execute this. So we are saying I would
like to see the ID and the first name. So we will get the ID, the first name from the table customers and we would
like to get the order ID and as well the sales from the table orders. So our result going to focus on those four
columns. Now the data should be joined between those two tables using the inner join and SQL going to start from the
left table from the customers because we say from customers. So it's going to start matching the ID from the left
table with the right table. So it's going to say okay is there a match from the first record from the first order?
Well yes it is the same ID and then SQL going to say okay that condition is fulfilled and we are allowed to see the
data. So the data will be presented in the output. So we're going to have the ID Maria and the order ID from Maria and
the sales of this order. So there is a match. Then SQL going to go to the second record. Well, we don't have a
match. The third we don't have match. And so on for the last one. So we have only one match for this ID. Then SQL
going to go again to the customers and pick the second one and start matching again with the first order. Do we have a
match? Well, no. Then it's going to go to the second. Well, now we have a match. So SQL going to be happy. the
condition is fulfilled and we will see the results. So we're going to see the first name and as well the order
information for this customer in the output. It's going to keep searching. So we don't have a match as well here. So
that's it. Now for the third customer as well from the start there match no to the second to the third and here we have
a match. So it's going to go and show this informations since there is a match. So the customer three George with
the order from this customer order ID and the sales as well in the output. Now it's going to go and keep continuing the
search. Well, we don't have any match. Then it's still going to go to the fourth customer and start matching. Do
we have here an ID? Do we have here a match? Well, no. Then the second, third, and fourth. We don't have any order for
this ID. There is no match at all. And since we are saying inner join then SQL will not allow to show the data of this
customer in the results. There is no match and SQL going to totally ignore this customer. Then we're going to go to
the last one and start as well matching this ID with the orders. Well, there is no match as well. SQL going to go and
exclude this user from the results. So this is exactly how the inner join works. it start from the left side and
start matching the data on the right side and only if there is match the result going to be presented in the
output and this is exactly why we are getting this results and how the inner join works. So now if you look again to
the reasons why we are joining tables we can say we can use the inner join in order to recombine the multiple tables
into one big picture. So the first use case and as well we can use the inner join in order to filter the data. So
since we are saying only the matching data that means we are filtering the data we are checking the existence of
the records in another table. So you can use inner join either to combine data from multiple tables or you can use it
as well only for filtering purposes only to check the existence of your rows. So this is usually the two use cases of
inner. All right. So that's all about the first type the inner join. Next we're going to talk about the left join.
So we're going to focus on the left side. So let's go. Okay. So now what is exactly left join?
This type going to returns all the rows from the left table and only the matching from the right table. So now if
you look again to our two circles A and B. What do we need from the left table? We want to see everything all the rows
all the data. So that means we will get a full circle. And now from the right table we want to get only the matching
data. So that means we don't want to see everything from the table B. We want to see only the records that has match to
the table A. So that means my friends the left table has here more priority. This is the primary source of your data.
The main source we cannot miss anything. This is very important. We want to see all the data. But from the table B, it
is a secondary source of data and we are joining it only to get an additional data. So I don't want everything. I want
only the data that has matched to the lift table. So this is what we mean with a lift join. Now if you look to the
syntax it's going to be very similar to the inner join. So we start from the left table the A. Then we say left join
the right table B and then the same condition using keys. So here we just switch the type. Instead of inner we
have now left. But now here with the syntax we need to be very careful. The order of the tables now is very
important. You have to start from the correct table. So you have to mention the left table exactly in the from
clause and then you join it with the right table. So in the join you have to specify the right table. If you don't do
it like this then you will not get all the data from a and you will not get the results that you are expecting. So this
is what we mean with the left join. Let's go back to scale in order to practice. All right. So now we have the
following task. It says get all customers along with their orders including those without orders. So again
here we need the data from two tables the customers and orders and we want everything in one result. So that means
we have to go and join the data. And now the task says includes those without orders. So that means I want to see
everything the matching data and the unmatching data from the table customers. And by looking to our query
this is not working because we are not getting everything right. We are getting only the customers that has match in the
table orders. And this is not of course fulfilling the task. So now if you read the task you can understand the main
table here is the customers. We are not speaking about to see all the orders and not missing any order and the orders
here is only for additional informations. So now in order to not lose any data for the customers we make
sure we start from the table customers. So that means now the customers on the left side and now after that instead of
inner join this is not good thing for this task. We're going to say left join and with that we guarantee we will get
all the data from the customers. Now we say left join orders and of course the condition going to stay like this. This
is how we are connecting the two tables. So actually that's it. Let's go and execute it. And now by looking to the
result you can see that we have now five customers even the customers that didn't place any orders. So you can see Martin
and Peter they don't have any order ID. So that means they didn't order anything. And as you can see is showing
us nulls when there is no match. So with that we have solved the task. Now my friends one more thing as I told you the
order of the tables is very important because the customer is now the left table because you start from it and the
second table the orders is the right table. Now if you go and switch them like this. So we start from the orders
and then join it with the customers and you go execute it you will not get all the customers and of course the task is
now not solved. So as you can see you are getting now completely different result if you go and switch the tables.
So be careful where you start and how you join the tables in order to get the effects that you want. All right. So now
I'm going to put everything back like before. Now let's go and understand how is exactly executed this query. Okay. So
now again we have the data from customers and orders and this time we are doing the lift join. So now let's
see how is going to do it. So going to say okay we need the ID and the first name and we will get that as well in the
results and from the right table we need only those two informations the order ID and the sales in the output. So those
are the columns that we need. So now SQL in the left join going to do it a little bit differently. It's going to start as
well from the lift table from the customers. But this time going to go and immediately put the result in the output
without like trying to match anything and to check whether the data exist or not because it doesn't matter not doing
any validation whether the customer exist in the orders. Since it's lift join is still going to show all the data
from the lift table. So there will be like no check. But now as a next step in order to get the order ID and the sales
SQL will start searching. So SQL going to go over here and start searching where do we have a customer with this
ID? Well, it's going to be the first order. We're going to get the order ID and as well the sales informations and
we will see that in the output. So that's it for the first one. Now it's going to go to the second row and the
same thing going to happen immediately. The SQL going to go and put the result in the output without checking anything.
And then in order to get the order data, it will start searching for this ID. So we have it here in the second row. We
have the order ID and the sales. And it's still going to put those results to the output. So the search for the third
one immediately going to put everything in the output. And then start searching for orders with this ID. We have it over
here. So this order belongs to the user ID number three. So far we are getting the same result as the inner joint. But
we are not done yet. Now exactly count the difference this guy going to go and get Martin and put it immediately in the
output and start searching for an order with this ID. So do we have any order with the ID number four? Well, we don't
have anything this time. SQL of course will not go and exclude the ID number four. It's going to leave it. But in SQL
if there is no match, we still have to have something in the output. So SQL going to go and say the output going to
be null like this. We don't know it is unknown. And the same thing for the sales. So in the lift join if there is
no match you will see nulls. The same thing for the next customer for better. So SQL will go and put the result
immediately in the output and then start searching the orders. So do we have anything for the ID number five? We
don't have anything. That's why SQL going to go and present nulls as well in the output. And that's why you saw nulls
in the output because those customers don't have any orders. So this is exactly the effect of the lift join. you
will get everything from the lift table and only the matching stuff on the right side and if there is something not
matching you will get nulls. So that's it is this is how scale execute the left join okay so now back to this use cases
of joins if I think about lift join I can use it in order to recombine data in order to build this big picture and as
well in the second use case where we use it in order to get an extra information from another table. So we have a main
table and secondary table. So we use it for both use cases and as well in the third use case only with a twist that
we're going to learn later. So that's all about the left join. Now we have another type that is exactly the
opposite of the lift join. We have the right join. So now let's understand what this
means. Okay. So now what is exactly right join? This is the total opposite of the left join. So this tag going to
returns all the rows from the right table and only the matching from the left table. So here the main table the
main focus is the right table. So SQL going to get you all the rows everything from the table B the right table but
from the left side we will get only the matching data. So that means in the left sides you will get only the data that
has a match on the right side and with that the right table going to be the primary the main source of your data. So
it is very important table but the lift table is not that important. You are just joining it in order to get
additional data. So again about the syntax it's not that crazy. All what you have to do is to change the join type.
So instead of left you say right join and again here the order of the tables is very important because the side here
makes a difference. So we start from the left table A and then right join it to the table B. So it sounds very similar
to the left join. We are just switching things. Now let's go back to scale. in order to practice. Okay my friends, so
now we have the following task and it says get all customers along with their orders including orders without matching
customers. So again we have the customers and the orders and we are doing the join but here the condition is
different. We want to see all the orders even if they don't have a matching customer. So that means I would like to
see everything from the table orders and the customers table here is only like supporting and helping. So the main
table that we are focusing on is in the orders. We want to see everything and from the customers only the matching and
if you are looking currently to the results you can see we are seeing only three orders right but in the original
table if you go back over here you can see that we have four orders. So we are currently using this query not seeing
all the orders. So now how we going to solve it? If you start from the table customers you can say you know what
instead of left join we're going to say right join. And with that you're going to guarantee you will get everything
from the table orders. But now the left table the customers is not that important and you will see the data of
the customers only if there is a match. So doing the right join like this guaranteed to see everything whether
there is match or no match. Now if you go and execute it you can see on the right side the order ID and the sales
and we can see now all the orders and on the left side the ID and the first name. We are seeing only the customers if they
did order something. And for the orders without a known customer, we are getting nulls. So with us, you have solved the
task using the right join. So now my friends, you have to go and solve this task to get the exact same results. But
you are allowed to use only the left join. So you are not allowed to use the right join. So now go pause the video,
solve the task and meet you [Music] soon. Now my friends, in SQL there is
always alternatives on how to solve a task. So now if you want to get all the data from B and only the matching from
A, you can do it like we have done using the right join. But if you go and switch the sides and you make the table B as a
left table and the table A as a right table, you can do that of course in SQL. But you have to switch the join type. So
instead of right, we have to use left now since the B table now on the left side and as well you have to switch the
order. So you start from the B table and then you say left join the A table. and of course the same join condition. And
if you do that, you will get the exact same result as the left query. So if you just switch the tables and as well
switch the join type, you can get the same results. And to be honest, my friends, I don't like the right join.
It's just in the last 10 years, I always tend to start from a table and then use a left join. And from my point of view,
the left join is way more famous than the right join. And I think I never used a query where I'm using a right join. So
my advice for you always try to skip the right join and stick with the left join just get the order of the tables in the
query correct and you will get the same results. So with that you know an alternative for the right join. Now all
what you have to do is to go and switch the right to left. Uh this is not enough because if I go and execute it. So now
all what I have to do is to go and switch the tables like this. So we start from the table orders because I want to
see everything from the orders and then lift join it with the customers. And of course we don't have to change anything
here. It doesn't matter the order because we have an equal operator here. What is very important here is where you
start from which table and what is the table that you are joining with. So if you go and execute it, you will get the
exact same results. So now I'm seeing all the orders. I'm not missing anything and only the matching customers. And I
prefer this way solving this task instead of using the right join. All right. So that's all about the right
join. Next we're going to combine everything. We're going to talk about the full join. So let's
go. Okay. So now what is exactly a full join? If you use it, SQL returns everything all the rows from both
tables. So now if you check again our circles from the left table, we want to get everything all the rows. So you will
get the whole circle and as well from the right table you want to get everything all the rows the whole
circle. So that you want to get everything the matching the unmatching all the data from left and right. Now
let's check the syntax. It's going to be very simple. The joint type here going to be a full join. And the full join it
is very similar to the inner join. You remember the order of the tables is not important at all. So there is here no
main table and secondary table. Both of the tables are important and it doesn't matter in your query where you start.
You can start from A full join B or you can start from B then full join A. you will get the exact same results. It
sounds simple. Let's go to SQL and practice the full join. All right. So now we have the following task and it
says get all customers and all orders even if there is no match. So now again we need the data from customers and
orders. But now of course which type we're going to use? It says even if there is no match but it didn't say no
match from orders or customers. So you can understand from this task we are not focusing only on the orders or the
customers. Both of them are equally important and we need all the data. So that means we need all the data from
left, all the data from right and we can go and use the full join. So now we have this query over here. We are starting
from customers and then joining to orders. But now instead of having left, we're going to say full join. So now
let's go and just execute it. Now if you are looking to the left side, you can see we are getting all the customers,
right? So we have our five customers and if you are looking to the right, you can see all our orders. So with that we have
everything from left and everything from right and the matching data is just side by side in the results and if there is
no match we are getting nulls. So actually with that we have solved the task and again it doesn't matter how you
start. You can start from the orders and then join it to the customers and you will get the exact same results. So you
are getting exactly the same data. Now let's go and understand exactly how is executed the full join. Okay again we
have the data of the customers and the orders and our full join. So now we're still going to identify those columns
that we want to see in the results. So the ID and the first name, the order ID and the sales informations to the
output. Now it's still going to start from the left table since it is started with the customers. It's still going to
take simply everything from the left table and present it in the output. Since it is full join, we want to see
all the data from the left side. And now start searching for matches from the right table. So let's start with the
first customer. And as usual, we will get the order from the customer number one. And the same thing for the second
customer, we have as well here match. So we will get as well. It's like that lift join. And for the third one, we have as
well a match. And we're going to have it like this. And since we don't have orders for those two customers, we will
get as well nulls in the outputs. So scale going to mark it with null. The same thing over here. And as well for
the last customer. So we will get nulls for those two customers. And now of course SQL will not stop here otherwise
we will get a left join effect. Now SQL going to start looking at the right side to find any order that is not in the
output. So SQL going to see okay the first order is in the output. The second one is as well in the output. The third
but the fourth one is not in the results. So SQL going to take this result and put it in the output. So this
order has no match at all from the left side. And with that if you are looking to the right side you can see SQL going
to be happy because we have all the orders from the right table. And of course SQL will not leave it like this.
Instead of that SQL going to show nulls on the left side. So there is no ID and there is no first name. So this is
exactly why we got this results. And this is how SQL executed the full join. Okay. Okay. So now if you are looking to
the use cases I can say you can use the full join in order as well to recombine the data from multiple tables if you
don't want to miss anything from all four tables all data the matching and unmatching data but I don't use it
usually for data enrichment for the second use case and where we can use the full join is in the last use case as
well but with a little twist that we're going to learn later. So this is mainly where we can use the full join. All
right. So with that we have covered the basic types of joins inner, left, right and full join. Those are the classical
joins on how to combine two tables. Now we're going to start talking about the advanced SQL joins. And now
we're going to cover the first part the lift anti- join. So let's see what this means. Okay. So now what is exactly a
lift anti- join? Now in this mechanism we want to return rows from the left side the left table that has no match in
the right table. So now by looking to our two circles from the left table we want to see only the unmatching rows. So
only rows that exist in table A but it don't exist in the table B. So if there is like matching data we don't want to
see it. And now from the right table we don't want anything. We don't want any data. So that means the only source of
your data going to be the left table. And from the right table we don't need any data. We are just joining the tables
to do a check to filter the data. So now for the syntax this can be interesting. We don't have a special type called left
anti- join. At least in the SQL server we still can create this effect. Since we are saying left we can use the type
left join and then as usual the join condition with the keys. But now if you leave it like this you will get the
effect of the lift join. And we don't want that because with the lift join you will get the complete circle from the
lift table. But now in order to remove the matching data this overlapping in the middle what we can do we can use a
filter and in order to filter the data we use the wear clause. So now in order to get rid of the matching data we can
take the key from the right table and we say the key must be null. So if the key is null so that means there is no match
on the right side. And if you do it like this you will get the effect of the left anti-join only the data in the left that
has no match on the right. So now let's go in scale and create this effect. Okay. So now we have the following task
and it says get all customers who haven't placed any order. So now by looking to this query clearly we are
focusing on the table customers but we want to see the customers that didn't order anything. So they are in our
database but the customers are inactive. Now there are like different ways on how to solve this task but we're going to
solve it using the joins. Now let's go and start by just writing a very simple query where we are selecting everything
from the table customers. Now you can see this is our five customers. And now I want to check which of those customers
didn't order anything yet. Now since we are talking about the orders, we can go and join it with the table orders. So
we're going to say lift join the table orders as all and then we're going to go and connect the tables using the ids
with the customer ID. So now if you go and execute it now we are still seeing all the customers because we are using
the lift join and now we can see the orders informations of each customer and you can see immediately those two
customers didn't order anything because we are seeing here nulls right so they are empty there is no orders now we can
use this information in order to filter the data I just want to see Martin and Peter so what you can do we can go and
say where and all what you have to do is to take the key that we are using in order to join in the tables this is this
one over here and say this must be null so is null so if you see it like this that means you want to see the data if
the customer ID is null so let's go and execute it perfect now you are getting the customers who haven't order anything
and this is exactly the effect that we wanted the left anti-join we are getting the data from the left side where there
are no match on the right side so you have always to do it in two steps first join the data as you normally do using
the classical joins the lift join and then the second step you go and use a filter using the wear clause if you do
it like this you can check for not existence and with that we are getting the effect of the left anti-join so
that's it okay so now if you are looking to this picture I think you already know where we use the lift anti- join we're
going to use it only in the last use case where we are checking the existence so if you use the lift join together
with the where you can check for the notexistence of your data in another table so This is exactly for this
scenario. All right. So that's all about the left anti- join. Now we're going to speak about the exact opposite of that.
We will cover the right anti- join. So it's going to be very similar but we are just switching sides. So let's
go. Okay. So now what is exactly the right anti- join? Well, it is the opposite of the left anti- join. So we
want to return the rows from the right table that has no match in the left table. So again if you are looking to
our two circles. Now what is important is the right table. We want to see only the unmatching rows from the right
table. So only the rows that exist in B but not in A. And from the left table we don't need anything. So no data is
needed and that means the only source of data comes from the right table and you are using the left table as a filter as
a lookup just in order to check the existence. So now the syntax of that going to be very similar to the left
anti- join. So we don't have a special type called right anti-join. We have to use the classical one the right join.
But if you do that you will get everything from the right table. And now in order to get rid of the matching data
in the middle we use a filter. We use the wear clause where we say we are interested only on the unmatching data.
So we take the key from the left table and we say the key from left is null. And if you do that you will get rid of
any matching data. Is null means there is no match. And again here the same thing the order of the tables is very
important since here we are talking about sides and you have to do it correctly. Okay. So now the task says
get all orders without matching customers. So now it is exactly the opposite. We want to see all the orders
that don't have a valid customer. So this is really bad scenario. You have in your business orders without a valid
customers. So let's see how we can discover that using SQL joins. Now as you can see we are focusing completely
on the orders. It's not the customers anymore. And we want to see only the orders where there is no match with the
customers. So now again here we have two steps. The first step we're going to go and do the normal join. So using either
the left or the right join. Now by looking to this query you can leave it like this where you can start from the
customers. But if you want to fully focus on the orders you have to switch this from left to right. And with that
you will get all the orders and only the matching customers. And let's go and remove this workloads from here first.
So I'm just adding comments. And with that SQL going to totally ignore this line of code. So let's go and execute
it. Now you can see we are getting all the orders right and data from customers only if there is a match. And now of
course this is not the task. We don't want to see all the orders. We want to see only the orders where we don't have
a match from the customers. So if you look to this those three orders they are okay. They are totally fine. We are
finding customers for them. So they have valid customers. But this order here is really bad. So there is no valid
customer for this order and now our task to show only this type of orders in the result. Now what we have to do we have
to use the workclass in order to get exactly the effects. So this time we're going to say if the ID of the customer
here. So here we're going to say the ID of the customer from the table customers must be null. So we're going to remove
this here and take the key join from the customer and we are saying this ID must be null. So let's go and execute it.
Perfect. With us we have solved the task and we are getting the effect of the right anti- join and we are getting now
those orders that don't have any customers. So we have solved the task. Now my friends you have to go and solve
this task without using the right join but still you have to get the same effects. You want to get exactly those
orders without customers. So pause the video and go solve the task. [Music]
Now again as you know me I don't like the right joins. We can create the same effects if you switch the sides of the
table. So if you say the B table now on the left side and the A on the right side then we will get the same effect if
you go and switch the type of join from right to left and you go just switch the tables. So you start from the B table
since it's on the left side and then join it with the A. And we still say of course in our work condition where the
data from A is null. So there is no match. So if you do this you will get the exact same results like the lift
query by using the lift join and just switching the tables. So you will get the same results and with that you know
that in scale we have always alternatives. I hope that you are done. So it's very simple what you're going to
do. We're going to go and switch the joins and since the orders is the main table we're going to start first from
the table orders. So we are putting it on the left side and then the right table going to be the customers. And of
course the condition going to stay as it is. We want to see the orders where there is no customer. So we don't have
to switch anything here or in the join key. So let's go and execute it. With that you are getting the same exact
results. Since we are using here the star, it's always starts from the left table and show the data from the right
table. But still the result is valid. We are getting this type of orders without matching customers. And I prefer this
way. All right. So now with that we have the left, the right and now of course what is next? We will get the full. So
let's speak about now the full anti-join in SQL. Let's go. Okay. So now what is exactly a full
anti- join? Well, this time we don't have sides. We want to return only the rows that don't match in either tables.
So what this means? If you are looking to the left circle, we want only the unmatching rows. So we don't want the
whole circle. We want only the data that exist in A but it don't exist in B on the right table. Sounds like the left
ant join but since we are saying full then you have to do the same thing on the right side as well. So on the right
table we want only the unmatching rows. So we want to see in the result the data that is in B but don't have a match from
A. So it's exactly the opposite. And if you look to this then that means we want to see only the unmatching data and this
is exactly the opposite effect of the inner join. In the inner join we were interested only on the matching data
only when there is like overlapping. But now with the full anti-join it is exactly the opposite. We don't want to
see the matching data. We want to see everything else the unmatching data. So how we going to write this query? Again
here we don't have a special type called full anti-join. We will use the help of the classical full join. So the basic
one. So you start from a full join b and then the same key. But now what is interesting is about the where
condition. Now we have like two conditions right? So now in order to get all data from A that has no match in B,
you have to make a filter where you say the key from the B table must be null. And now since we want the exact same
thing from the right table, we want all the data in B that has no match in A. You have to say as well the key from the
A table must be null. So now we have here like two conditions. And in SQL if you have like two conditions in the work
clause, you have here two options either use and operator or the over operator. So now the one that we're going to use
here is the or operator. So either the key from right is empty or the key from left is empty. If you do it like this,
you will get the effect of the full anti- join. And of course since here both sides are equal then the order of
the tables as well here is not that important. So you can say from A full join B or from B full join A. It doesn't
matter. So now let's go back to scale in order to create this effect. Okay. Instead we have the following task and
it says find customers without orders and orders without customers. So if you are looking to this this means we want
to see only the unmatching data from customers and as well from orders. There is no main table and secondary table.
Both of them are equally important. So now since we are talking about the unmatching data and the anti-join we
have to do it in two steps. The first step we're going to do the classical join and then we focus on the wear
clause. So let me remove the wear clause to make it as a comment. Now since we want the data from left and right, we're
going to go and use the full join. So let's go and execute it. Now you can see we are getting the effect of the full
join. We are getting all the orders and as well all the customers. But now we are interested only on the strange cases
where they are like orders without customers like this one here and as well customers without orders. So that means
the first three rows they are not really interesting for us because it is boring. We have here matching data and this is
totally fine but we are not focusing on that now. We are focusing only if there is like missing data from left or from
right. As you notice I'm saying or and this is very important because we're going to use the or operator. So now
let's focus on getting this scenario over here. We want to get an order without a customer. So that means the
customer ID must be null. And we have it already here. So we are saying where the ID of the customer is null. So if I go
and execute it, I will get only one records only this one over here. But as well I want to get the opposite
scenario. So in this scenario, the customer ID must be null. So we're going to say or the customer
ID in the orders is null or we can do it like side by side like this. Either the right side is null or the left side is
null. So if you go and execute it, you will get the effect of the full anti-join. And with that we are finding
the customers without orders and orders without customers. I think this is really fun and as well really easy. So
this is how we do the full anti- join. All right. So now if you are looking to the use cases we use the full anti- join
again exactly for the last use case in order to check the existence. So if you combine the full with the where you can
check the existence or the notexistence of your data in another table. So this is exactly the scenario for that.
Okay, my friends, now we have a bonus section where I'm going to challenge you to solve the following task without
using an inner join. So, it says, "Get all customers along with their orders, but only for customers who have placed
an order, but without using an inner join." So, pause the video now and go and solve this
[Music] task. Okay, so now let's see how we're going to solve this. We want the
customers, the orders, blah blah blah. But we want only the customers who have placed an order. Previously, we have
used the inner join in order to solve this task. But this time, we are not allowed to use it. So, let's go and
solve it. This is how I'm going to do it. Select star from table customers. Can't give it the alias. So, now I'm
getting all the customers, but I am interested only the customers who have placed an order. So, as we know before
there's like two customers didn't order anything, and we don't want to see them in the final results. Now how we will
get that? Well, we can use the help of the table orders in order to check the existence of our customers there. And of
course, I'm not allowed to use the inner join. So I'm going to go and use a left join with a table orders and then
combine them as usual. Nothing new with the customer ID. So now let's go and execute it. As you can see, we are doing
it step by step. You don't have to rush everything in one go. So you start simple, check the results and decide on
the next step. So now by looking at these results I want to get those three customers because they have ordered
something and we are seeing data about their orders and I don't want to get in the result the last two. So again we
still can use the customer ID from the right table in order to decide which data going to stay in the result and
which data should be filtered. We're going to go and use the wear clause and then the key from the orders and this
time we're going to say is not null. I know we didn't learn yet about the not and the logical operators but using the
not null it means there should be data inside the column it must not be null if you do it like this and execute you will
get the exact effect as the inner join. So as you can see as you are joining the tables using the left join you can
control what you want to see using the wear clouds using the filter and this is how you can solve this task without
using an inner join. Okay, so with that we have covered all those three scenarios in order to find the
unmatching data. Left, right, full and joints. Now we can speak about one crazy join. We call it the cross join. This
one is totally different from all other types that we have learned. So let's understand exactly what is the cross
join. Let's go. So now what is exactly a cross join? Now in some scenarios we want to combine
every row from the left, every row from the right. So that means I want to see all the possible combinations from both
tables. So we are doing something called like cartesian join. So now if you look at our two circles, we want everything
from A and as well everything from B. So that means I want to see everything from A combined with everything with B. So in
this example, we have two rows in A and three rows in B. If you do a cross join, you will get six possible combinations
by just multiplying the number of rows between A and B. So be careful using the cross join. If you use it, you will get
like crazy number of rows in the results and you're going to make the database really busy finding out the result for
you. So now about the syntax, it's going to be the easiest. So you start as usual from one of those tables, the A for
example, and then you say cross join B. So now my friends, if you look at this, you can see it's not like the previous
joins that we have done. We have always before talked about unmatching rows, matching rows and so on. But here we
don't care at all about whether the data is matching or not. I just want to see all the possible combinations
everything. So since we don't care about matching the two tables, we don't have to specify any condition. So there is no
need to use the keyword on because we don't need any condition. So that's it. You just say cross join B and the magic
can happen. So this is a cross join. Let's go to SQL to try that. Okay. So now we have the following task. It says
generate all possible combinations of customers and orders. So that means we want everything with everything using
the cross join and this going to be very simple. So we're going to start with select star from whatever table. So you
can start from the customers and then you say cross join orders. That's it. Very simple. Let's go and execute it. So
now as you know we have five customers and four orders. And if you multiply them you will get in the results 20
rows. So now we are getting everything with everything. even if the data is not matching at all. So you can see for
example the orders here. So this is one order that belongs only to one customer the customer ID one. So it is an order
from actually Maria but still we are seeing this same order with the other customers since we want to combine
everything with everything. So there are no rules. The same thing for the next set. So this is the second order
actually belongs to John but we are seeing this order with all customers. So that's it. This is how the cross join
works. And now you might ask me why we have this. It makes no sense, right? Well, my friends, I rarely use it. But
sometimes if I want to generate like test data or maybe if you have like for example table called colors and table
called products and you would like to see all the combinations between the products and the colors. So in some
scenarios it makes really sense to see all your products together with all the colors without any matching conditions
or whatever. So there are like few scenarios for the cross join if you are like doing simulations or testing. So
this is how we do the cross join. Okay. So that's all about the cross join. And with that we have covered the four
advanced types of joins. Now if you look at this you might ask okay how I'm going to choose between all those types. So
you might ask me okay bar how you do it? Well I'm going to show you now my decision tree that I usually follow in
order to choose the correct type. So now if I'm combining two tables and I want to see in the results only the
matching data between two tables then I go and use the inner join. We don't have any other type for that. So that's
simple but now if I want to see everything all the data I don't want to miss anything after joining two tables
then I take different path and here I ask myself is there like one side more important than the other am I interested
in all data from one table from one side like here we have like a main table or a master table then I go and use the lift
join but if I want to see all the data from all tables in my query everything so there is no one table more important
than other then I go with the full join So this is another path and now the third path if I'm interested to see only
the unmatching data. So I'm doing some kind of checkups and so on. And here again the same thing do I want to see
the unmatching data from only one side. There is like one table that is important then I go and use the lift
anti- join. So I want to see the unmatching data from one table and I'm using the other table only for the
check. But in my query if both of the tables are important there is no main table and secondary table both are
important then I go and use the full anti- join. So actually that's it. This is the decision tree that I follow
usually as I'm writing a query. And you might ask me how about the right join. Well as you know me I don't have it at
all in my decision tree. So I don't use it at all. Now by looking to this I can tell you if I check most of the queries
that I write very often I use the left join. So I can tell you this is my favorite way on how to join tables. So
let me show you exactly why. Usually I write queries in order to do data analyzes. So in data analytics
you have always like starting points. You have like a topic that you are analyzing like the customer. So you have
always like a master table. So I always start with the main table of my analysis. So in my query I start from
this table from table A the main table. And then what happens? The data is not enough in this table. I need some extra
data that comes from another table like the table B. So the table B is only here like an additional data to the master
table. So I go and use the lift join in order to connect the table B and then I find another interesting information in
another table in table C. So same things happens. I go and join the tables using the lift join and so on. So I keep
connecting multiple tables to this main table in the middle. And my query going to look like this. always doing lift
joins with multiple tables. Now, of course, you might say, "Yeah, but sometimes you would like to see only the
matching data and so on. So, it makes sense only to use the inner join." Well, in order to do that, I can control
everything that I want to see in the final results using the wear clause. So, in the wear clause, I define exactly
what I want to see in the final result. So, with that, I get like more flexibility on whether I want to see the
matching, unmatching data and so on like we done in the lift and join, right? So as I'm analyzing data I tend very
frequently having this setup where I start from the main table and I lift join all other tables and with the word
conditions I control the final results. So this is how I connect multiple tables together. So now if I want to visual
this in like circles it's going to look like this. We have the circle A. So this is the master table the starting point.
I want to see all the data from table A and I live join it then with another table B and from table B I want to see
only the matching data. So it's like the lift join. Now what going to happen? I'm going to go and add another table. So
another circle the circle C. And from the circle C, we want to see only the matching data. And of course you can
keep adding circles to this. But it's going to be always the same thing. And in your circle going to has only the
matching data. So now as we learned we can use joins in order to combine multiple tables to get a complete big
picture about topic like the customers. I would like to see everything about the customers in the final results. So
either you're going to do it like me where you start from the main table and then go and lift join all other tables
or maybe you say you know what there is no main table about the customer's data all the tables are equally important
then you can go and join all those tables using the inner join if you are interested only on the match data so
what can happen if you have again those circles from the A you need only the matching data from B you need as well
only matching data and as well from the third circle so you are interested only on the overlapping between all all three
tables. So you will get only this section where you have overlapping between all three tables. So this is of
course another way on how to join multiple tables. Okay. So now my friends let's go back to scale in order to
practice how to join multiple tables. Okay. So now let's have a task. This going to be a little bit challenging. We
will be doing multi- joins using the sales DB. Retrieve a list of all orders along with the related customer product
and employee details. And for each order display the following. We want to see the order ID, the customer name, the
product name, sales price, salesperson name. So there is a lot of things that is going on. And the first thing that
you're going to notice it does now we are using different database. We will be not using the my database, we're going
to go and use the sales DB. So this is the first thing that we have to do. So instead of using my database, so we say
use sales DB and then execute it. We are now connected to the sales DB. So this is the first thing. So now if you are
reading this task there are a lot of tables that are involved. We need the orders, we need the customers, products
and employees. So there are like four tables needed in this task and we need different stuff from each table. So now
how I think about it well it is mainly focusing on the table orders right? So we need all the orders we cannot miss
any order here. So this sounds for me this is the main table and then it says along with that we need other
informations. So that means the other tables are not that important like the orders. So this gives me feeling about
what is the main table and this going to be my starting points. So let's start from that from the table orders. So
select star from and here you have to pay attention that this database has always a schema. It's called if you look
to the left side sales dot the table name. So we have to write that now in our query. So we're going to write it
over here sales dot and then the table name orders. Let's go and execute it. Now I know this is the first time that
you are querying this table. We have a lot of informations here and as well we have a lot of ids. Those ids going to
help us of course on joining our data with the other tables. So what do we need from here? We need the order ID. So
we have it over here. We're going to get the order ID. This time the naming convention is different. We don't have
like underscores and comm. We have different type of namings. So be careful with that. So what else do we need? We
need the sales. So if you go to the right side over here, we have column gold sales and we're going to go and
include it to the results. Now all the other informations are actually not needed, but I need those ids in order to
join it with the other tables. So now what I'm going to do, I'm going to go and give it an alias and all. So now I'm
going to go and assign it for each column. This comes from the orders and as well the same thing for the sales. So
that's it for now. And if I go and execute it, I will get the orders and the sales. All right, so that's all for
the first table. Let's go now and see what do we need. We need the customer's name. Well, actually we don't have this
piece of information in the orders. So all what you have to do is to go and explore in the other tables in order to
find this column. So how I usually do I go and explore the tables like this. So I write a symbol select from each
tables. So the customers. So now I go and repeat this for each table inside the database. So we have the customers,
employees, we have an orders, the orders archive and as well the products. So now I start exploring the table. So if I go
to the customers over here, we can see we have here five customers and we can see the names of the customers. So we
see the first name and the last name and this is exactly what I need for my query. Now of course we have to go and
connect this table with the orders. So we need a common column. Usually it's going to be the ID. So here we have the
customer ID and if you go and query the orders you can find here as well the customer ID. Now if you are working in
big projects you're going to have a lot of tables and exploring each one of them going to be really hard. So now of
course if you have like in the project hundreds of tables it's going to be really hard to explore each table. So
instead of that a good project a good database usually has an entity relationship model er model like the one
that we have for the course. And here you can find easily the tables that you have inside your database and as well
