# SQL Full Course for Beginners (30 Hours) – From Zero to Hero

https://www.youtube.com/watch?v=SSKVgrwhzus

[00:00] Hello and welcome to this unique course to master SQL.
[00:01] 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.
[00:12] Now, of course, the first question is what makes this course so special.
[00:15] 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.
[00:24] So I'm going to break complex concept in SQL using hundreds of animated visuals.
[00:29] 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.
[00:35] Right.
[00:37] The second reason is this course is taught by me.
[00:39] 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.
[00:46] 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.
[00:51] So by the end of this course, you will be ready to solve any complex task like I do using SQL.
[00:58] So now I designed this course to
[01:01] 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.
[01:16] And this course is suitable for anyone: data engineers, data analyst, data scientist and even for students.
[01:22] And by the way the good news: everything is for free from the start until the ends.
[01:26] I will be sharing with you as well a lot of materials, code, presentations and animations and there are no hidden costs.
[01:32] So you don't have to pay for anything.
[01:34] But my friends in return I really appreciate it if you support the channel in order to grow.
[01:39] All right my friends I'm really excited about it.
[01:41] I don't know about you.
[01:42] If you are motivated join me learning SQL.
[01:45] This is going to be amazing.
[01:45] So let's go.
[01:50] All right.
[01:52] 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.
[02:00] So now at the start we have to understand.
[02:02] Few stuff like what is SQL, why to learn it, what are databases and the types of databases.
[02:07] And after the theory we're going to prepare your PC with data and the softwares.
[02:11] Now once we have everything then we can go to the next chapter.
[02:14] 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.
[02:24] 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.
[02:32] How to create a new table, add a new column, remove column and as well how to drop a table.
[02:39] So with that you are defining new stuff in the database and then the next chapter you have to learn about the data manipulation.
[02:45] 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.
[02:53] 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.
[02:59] And I can say with that you cover the basics.
[03:02] About SQL.
[03:05] Now after that we start with the intermediate phase where we're going to deep dive into topics like how to filter your data.
[03:10] Here we're going to learn about the comparison operators, logical operators, between and like.
[03:15] So all the operators that you can use in order to build a condition in order to filter your data.
[03:21] Then after that it's going to be very interesting topic.
[03:23] You have to learn how to combine them.
[03:24] And here we have two mechanism either using the join or using the set operators.
[03:29] And oh my god joining data.
[03:33] 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.
[03:41] And after that you have to learn about the set operators and here you have like four methods union union all except intersects.
[03:46] So with that you learn how to combine multiple tables by combining the columns or the rows of your tables.
[03:54] So this is very important.
[03:56] Now moving on in our course.
[03:56] Now using SQL you can do a lot of stuff cleaning up the data a lot of data preparations and at the end.
[04:03] You can do a lot of analytics and aggregations.
[04:05] So there are like two families of functions.
[04:07] 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.
[04:19] So all those stuffs are transformation for only one single value.
[04:23] We call it role level functions.
[04:25] 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.
[04:31] So we're going to start with very basics like the aggregate functions.
[04:34] And then we're going to deep dive into the window functions, analytical functions.
[04:39] And here we have like aggregates, ranking and value functions.
[04:42] Those are very important tool for any data analyst or data scientist doing analytics task in SQL.
[04:49] So I can say the rowle functions is for data engineers and the analytical functions are for data analysts.
[04:54] 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.
[05:03] So here there are.
[05:05] A lot of techniques that you have to learn about SQL.
[05:09] So the first one is the subquery, query inside another query, and the very famous CTE, common table expression.
[05:14] A lot of developers like this one, and then you will learn about how to create views in the database.
[05:20] This technique, if you learn it, you're going to be really professional in SQL.
[05:23] Then we're going to learn how to create tables using select, the temporal tables, and then we're going to learn about the stored procedures, how to write a program in SQL, and after that, of course, comes the triggers.
[05:33] So those are the advanced techniques that you have to learn in SQL in order to do advanced projects using SQL.
[05:40] 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.
[05:46] And for that, you have to learn how to optimize the performance of your queries, and here there are a lot of techniques.
[05:52] The most famous one is to create an index in the database or create a partition.
[05:58] 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.
[06:05] Queries.
[06:08] So this is very important and then we're going to move to very interesting one.
[06:09] I will be sharing with you how I use AI like shy GBT or copilot as I'm using SQL in my projects.
[06:17] So here you have to learn how to write correct prompts to get assistance from AI as you are using SQL.
[06:22] And finally and my favorite one it will be about SQL projects.
[06:27] So my friends here you have to bring everything that you have learned about SQL in handon projects.
[06:30] 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.
[06:39] The first one is data warehousing project.
[06:41] 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.
[06:51] Once you build it then you jump to another project.
[06:53] Here you're going to start exploring the data and start getting the first insights about the business.
[06:59] And the last project that you can do is the advanced data analytics project.
[07:03] So this is very important section where you do SQL.
[07:07] Projects.
[07:07] So my friends, this is the roadmap on how to learn SQL.
[07:12] 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.
[07:19] Okay.
[07:20] So now let's start with the first chapter, the introduction to SQL, and here we're going to cover few topics.
[07:25] So we have to understand first, what is exactly SQL?
[07:26] Why we have to learn it?
[07:28] What are databases and the different SQL commands that we have in SQL?
[07:33] So it is the basics, the theory about SQL.
[07:35] So what is exactly SQL?
[07:42] Let's go.
[07:45] Everything generate data and data is everywhere.
[07:47] Your first name is data, your mobile and everything inside the mobile is data.
[07:51] Car is as well generating a lot of data.
[07:54] Bank, your finance statements, everything is data.
[07:57] And now of course the question is, where do we store our data?
[07:59] Personally, we store a lot of our data in like excels, spreadsheets, in a text file.
[08:03] So you store a lot of your data in different files.
[08:05] Now how about?
[08:08] Companies?
[08:09] 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.
[08:17] So companies generate massive amount of data.
[08:20] So now the big question is how they handle the data how they store it.
[08:23] Of course, they cannot go unused like simple files.
[08:25] They need something bigger, stronger and smarter.
[08:27] And here where the database comes in.
[08:29] So think about the database.
[08:31] It's like a container for storing data.
[08:33] But instead of just dumping files into folders, the database organized the data.
[08:38] So it is easy to access, to manage and to search.
[08:40] So a database simply it is a container that stores data.
[08:43] So now you might ask why we are using database.
[08:45] Can't we just use files like I do it personally?
[08:50] Well, let me tell you why we use databases.
[08:52] Imagine that someone asks the following question.
[08:54] Go and find the total spending in your data.
[08:55] 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.
[09:09] It's going to be very long and messy process.
[09:11] 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.
[09:18] 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.
[09:22] And now comes of course the question how do we talk to a database?
[09:26] Well we use SQL.
[09:28] SQL is the language that you use in order to talk to the database.
[09:33] It stands for structured query language SQL.
[09:35] And here you have people that call it SQL like me and others that call it SQL.
[09:41] There is no right and wrong but if you follow me through the course I think you will start saying SQL.
[09:47] 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.
[09:56] So this process is very easy simple and fast and this is way better than having your data stored in different files.
[10:02] Another reason why we use databases is that they can handle really huge amount of data.
[10:07] So sometimes we have like millions of data inside our database but
[10:11] In the other side, if you are storing your data inside spreadsheets and you have like massive amount of data, what can happen?
[10:16] Your spreadsheets going to just break.
[10:18] They simply can't handle big data.
[10:20] And another reason why we use databases is that it is just secure.
[10:25] It is safer to store important and critical data inside the database than just storing it in spreadsheets and files.
[10:31] So the databases are secure and you can control who is accessing what.
[10:36] So it is just more professional to store the data inside a database.
[10:38] All right my friends, so far what we have learned, most of the companies stores their data inside a container called a database.
[10:47] And for you, in order to ask questions and to talk to your database, you have to speak the language of SQL.
[10:55] Now I'm going to show you how it looks like usually in companies.
[10:59] 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.
[11:08] But now not only employees and people interact with the database.
[11:09] You.
[11:11] Could build a website or an application that as well interacts with the database by sending different SQLs.
[11:18] 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.
[11:26] 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.
[11:44] 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.
[12:07] So it is a software that going to manage all those different requests to our database and it going to make the priority which.
[12:13] SQL must be executed first.
[12:15] This software can as well manage the security whether the SQL is allowed to be executed in the first place.
[12:22] So my friends, the DPMS is the software that going to manage the database.
[12:26] And now we are not done yet.
[12:28] There is something missing.
[12:29] So we have our data, we have the software.
[12:30] What is missing here is the hardware.
[12:32] So in real companies, we cannot run that on our PC because first our PC is weak and as well it goes offline.
[12:38] That's why we need a server.
[12:40] 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.
[12:53] So my friends, so far what we have learned: the database it is container to store the data.
[12:58] The SQL it is the language in order to talk to the database.
[13:01] The DPMS it is the manager; it manage the database.
[13:06] And the server it is the physical machine where the database lives.
[13:08] So this is how it looks.
[13:13] Like and now my friends there are different types of databases.
[13:18] So let's see what do we have.
[13:19] The first and the most famous one it is the relational database.
[13:22] It is very simple.
[13:24] 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.
[13:33] So if people hear a database they're going to think about this one.
[13:37] Now we have another type of databases called key value.
[13:41] This time the data is organized completely different where you have pairs of keys and values.
[13:45] Think about it.
[13:47] It's like a big dictionary where you have a word like the key and the definition of the word this is the value.
[13:53] And now moving on to the next one.
[13:54] This is as well important column based.
[13:57] So now instead of grouping the data by the rows this type of databases group the data into columns.
[14:01] That's why it's called column paste.
[14:03] And this is very advanced database in order to handle huge amount of data where the main purpose is to search for data.
[14:09] Moving on to another database called graph database.
[14:11] The main focus here is the relationship between.
[14:15] Objects.
[14:15] So the main idea here is how to connect my data points.
[14:20] And now finally we have the document database.
[14:23] The data is stored as entire documents where the structure of the data is not that important.
[14:27] What is more important is to fit everything in one page in one document.
[14:30] 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.
[14:42] And in this course, we will be focusing of course on the relational database.
[14:45] And I'm sure you have heard about like the Microsoft SQL server, the MySQL, the possesses they are SQL relational database.
[14:55] 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.
[15:03] For the graph database we have the Neo 4G and the very famous database the MongoDB as a document database.
[15:08] 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.
[15:18] Used one in companies and I will be focusing on the Microsoft SQL server.
[15:22] So those are the different types of databases.
[15:28] Now the databases are very structured and organized.
[15:30] It has the following hierarchy.
[15:34] 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.
[15:40] So maybe you have a database for the sales and another one for the HR.
[15:45] So the server can host multiple databases and as we learned a database is a container of your data.
[15:51] Now moving on to the next level.
[15:53] In each database we can have multiple schemas.
[15:56] 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.
[16:04] 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.
[16:10] So it help you to organize your tables and your objects in the database.
[16:16] And now if you go inside schema you can have multiple.
[16:18] Objects like tables.
[16:20] So now of course the question is what is a table?
[16:23] It is like spreadsheet.
[16:23] It organize your data into columns.
[16:25] The column define the data that you store inside it.
[16:27] So you have one column about the customer ID.
[16:31] Another column about the names, the scores, the birthday.
[16:33] So each column is about one type of data and sometimes we call the columns as fields.
[16:38] Now the other thing that we have in tables is the rows or sometimes we call it records.
[16:43] It is where actually the data is stored.
[16:45] Now in this example each record represent one customer one person.
[16:50] So we have one record for Maria, John and Peter.
[16:52] Those we call them rows.
[16:55] Now in each table there is like one very important column called the primary key.
[16:59] 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.
[17:10] So it is unique.
[17:13] It's like fingerprint and there is no two customers having the same ID.
[17:14] Now the overlapping between the columns and
[17:18] The rows we have a single value a cell.
[17:21] And each value each column stores specific data type.
[17:24] 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.
[17:34] Now if you want to store characters we have different data types for that like you want to store the name or the description.
[17:39] So here we can use the char or the vchar.
[17:42] So you store inside them like the first name Maria or something.
[17:46] Now you might ask what is a char or vchar.
[17:48] So the char always a fixed one.
[17:50] So if you define it like five characters always it's going to go and reserve five characters from the space.
[17:54] But if you want things more dynamic then you go with the vchar.
[17:58] And now moving on we have another data types called the date and time.
[18:02] 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.
[18:07] So we call those stuff int, decimal, char, date, time.
[18:12] They are data types.
[18:14] So my friends, as you can see, SQL databases are very organized and.
[18:20] Structured.
[18:20] Okay.
[18:20] So now let's focus more about the SQL itself.
[18:25] We have in SQL different type of commands.
[18:27] So let's say that we have a database and this database is empty.
[18:29] So we have nothing inside it.
[18:30] 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.
[18:39] So, once you executed the database going to go and build one, but this table is empty.
[18:43] So, we have nothing inside it.
[18:45] So now what you have done here is you have defined something new, right?
[18:49] And we call this type of commands the data definition language, the DDL.
[18:53] We have create to create something new, alter in order to edit something that already exists and drop in order to delete something.
[19:00] To drop for example a table.
[19:02] So this is the first family of commands.
[19:04] Now if you look at our table, it is empty.
[19:05] What do we need?
[19:05] We need data.
[19:07] So let's say that we have a website or an application.
[19:09] Now this application is generating a lot of data.
[19:13] Now in order for this application to move the data inside our new table, it must use the SQL command insert.
[19:18] So if you execute insert, you can add a new data inside.
[19:23] Your table.
[19:25] This type of commands we call it data manipulation language.
[19:27] And here we have three commands.
[19:29] 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.
[19:40] So what do we have now?
[19:42] We have table, we have data inside the table.
[19:44] Now what we can do we can start asking questions.
[19:46] So let's say that you have analytical question about your data.
[19:50] 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.
[19:56] 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.
[20:10] And here we have only one and it is very famous.
[20:12] We have the select.
[20:15] We can use it in order to query our data.
[20:17] So those are the three different commands in SQL.
[20:19] And of course, we're going to learn all of them, but we will spend most of our time learning how to.
[20:25] write the correct query for the correct
[20:31] answer. And now you might ask me, Barra,
[20:33] why we have to learn SQL? And if the
[20:35] time goes back, are you going to learn
[20:37] SQL again? Well, for sure, of course.
[20:40] And here are the top three reasons that
[20:42] I have. The first one, you have to learn
[20:43] it in order to talk to the data. You
[20:45] know, most of the companies stores their
[20:47] data in databases, and this is a
[20:49] standard way. This is how they do it.
[20:50] And if you want to work on the company
[20:52] in the data field and you want to talk
[20:54] to their data, then you have to use SQL.
[20:56] It's like you move to another country
[20:58] where they speak another language and
[21:00] you want to live there for a long time,
[21:01] you have to speak their language. The
[21:03] same thing here. If you want to work
[21:04] with data, you have to learn the
[21:06] language in order to speak to the
[21:08] database, the SQL. So this is for me the
[21:10] most important reason why we have to
[21:11] learn SQL and SQL it is in high demand.
[21:14] If you go now and check the job
[21:15] description of the software developer,
[21:17] data analyst, data engineer, data
[21:19] scientist, I promise you you will find
[21:21] there that they going to demand for SQL.
[21:23] So you will find they going to ask for
[21:25] SQL skills almost in each job
[21:27] description. So if you check for any
[21:29] data related jobs, you will find that
[21:31] they going to ask for SQL skills. Now
[21:33] another reason that I have is it is
[21:35] industry standard. So if you go and
[21:37] check multiple modern data platforms and
[21:40] tools like PowerBI, Tableau, Kafka,
[21:42] Spark, Synaps, you will understand that
[21:45] there will be always a section where you
[21:47] have to enter SQL code. So most of those
[21:49] vendors adopt SQL because it is the
[21:52] standard. It is widely used. It is like
[21:54] selling points that their tools are
[21:56] easy. So those are my top three reasons
[21:58] why SQL is still relevant and why you
[22:01] have to learn it. Okay, my friends. So
[22:02] with that we have now clear
[22:03] understanding what is an SQL why we need
[22:06] it what are databases and their
[22:08] different types why do we have DBMS
[22:10] servers and as well now you have
[22:12] understanding how things are very
[22:14] organized and structured inside the
[22:16] databases so that's all this is SQL all
[22:19] right so with that we have covered the
[22:20] basics about what is SQL and databases
[22:23] now in the next step we're going to go
[22:25] and set up our environments so that
[22:27] means we're going to prepare your PC
[22:28] with the data with the databases and all
[22:30] the tools that you need in order to
[22:32] learn
[22:36] SQL. Okay. So now go to the link in the
[22:38] description and you will land here in my
[22:40] newsletter website and you can subscribe
[22:42] if you want to get weekly news about my
[22:45] content. I make as well post about data
[22:48] and many other projects. So once you do
[22:50] that what we're going to do now we're
[22:51] going to go to the downloads over here
[22:53] and you will find here all the materials
[22:55] of different courses and the one that we
[22:56] want is SQL ultimate course. Let's go
[22:58] over here. Now once you do that you will
[23:00] land to this page where I have listed
[23:02] all the important links. So the first
[23:04] one and the most important one is to go
[23:06] and download the course materials. Here
[23:08] you can find everything code the slides
[23:10] the presentations the whole course or if
[23:12] you don't want that you can go to my get
[23:14] repository and there you will find
[23:15] exactly the same materials. So let's go
[23:17] and download everything. Okay. So now go
[23:19] and put the downloaded folder somewhere
[23:21] safe and let's go inside it. And here
[23:24] you can find three things. The first one
[23:26] is the data sets. Here if you go inside
[23:28] it you will find the data for the course
[23:30] the databases that we will be using in
[23:32] order to practice SQL. So everything is
[23:35] available here. Now the second folder
[23:37] you can find all the documentations. So
[23:39] that means all the visuals the
[23:40] presentation slides everything that I
[23:43] present during the course. It is
[23:44] available here as a documentation notes
[23:46] for you. Now moving on to the third one
[23:48] we have the scripts. So during the
[23:50] course we will be writing a lot of SQL
[23:52] codes and all those codes are here
[23:54] available. So that means those are all
[23:56] the codes that is used in the course.
[23:58] Okay. So with that you have now all the
[24:00] course materials. All right. So now the
[24:02] next step is that we have to go and
[24:03] download the SQL Server Express and you
[24:05] can find the link as well over here. So
[24:07] let's go there SQL Server Express. And
[24:09] now we're going to land on the Microsoft
[24:11] page where we can see the different
[24:13] offering from Microsoft where it's
[24:14] called server. So either we have it on
[24:16] the Azure or we can download it on the
[24:18] on premises. But we don't want those
[24:20] stuff. Just scroll down to see those two
[24:22] options. So the first option on the left
[24:24] side we have the developer edition. You
[24:26] will get all the features and services
[24:28] that Microsoft offers with the SQL
[24:31] server. It is as well free but the
[24:33] installation here is little bit
[24:34] complicated. But in the second option on
[24:36] the right side we have the express
[24:38] edition. Installation here going to be
[24:40] really fast and very easy. You will get
[24:42] as well all the stuff that you need for
[24:44] practicing SQL and learn SQL. So both of
[24:46] the options are free. It's just a matter
[24:48] of the installation. We will go now for
[24:50] the express edition. So go and click
[24:53] download now and it's very small file.
[24:55] So let's go and start it. And now the
[24:57] installation going to start. So we have
[24:59] basic, custom and download media. So
[25:00] download media means download now and
[25:02] later we're going to do the
[25:03] installation. Custom means we have more
[25:05] control on how to download and install
[25:07] the stuff. The basic is the easiest one
[25:09] and the quickest one. So let's go with
[25:11] the basics and click on that. And let's
[25:13] go and accept all those stuff. And now
[25:16] let's click on install. So now it's
[25:18] going to install the applications,
[25:19] drivers and so on. It may take a little
[25:22] bit
[25:24] time. So in order to do that, let's go
[25:27] and click on install SS SMS. So let's
[25:30] click on that and as well we can find
[25:32] the link over here. So let's go to SQL
[25:34] Server Management Studio. So let's click
[25:36] on that. You can find of course this
[25:38] link as well with the other links that I
[25:40] have collected. So now we are again at
[25:41] Microsoft page. Let's go scroll down and
[25:44] now we will see the following link free
[25:46] download for SQL Server Management
[25:48] Studio SS SMS. So let's go and click on
[25:51] that and then it's going to go and
[25:52] download it. Let's go and start it. So
[25:55] the first thing that we have to define
[25:56] the location. I will go with the default
[25:58] stuff. So let's click on
[26:02] install. Okay. Setup completed. We just
[26:05] installed SM SS SMS. So let's go and
[26:09] close it. So now let's go and start it.
[26:11] If you go to your menu over here, search
[26:13] for SQL Server and you will find it
[26:15] here. SQL Server Management Studio.
[26:17] Let's go and start it. Okay, so now
[26:20] we're going to get this window in order
[26:21] to connect to our server. So again, what
[26:23] is our server? It is the one we have
[26:25] installed at the first step, SQL Server
[26:28] Express. And that's why you're going to
[26:29] see in the server name, your PC name, of
[26:32] course, like it's not going to be my PC
[26:34] name. But here we have something called
[26:36] SQL Express. This is the server we just
[26:38] installed. So in the first option, we
[26:40] have database engines. We have reporting
[26:42] services. Those are different stuff from
[26:44] Microsoft. We're going to leave it as a
[26:46] database engine. And it should be like
[26:48] this. SQL Express. Now, how to access
[26:51] this database? We have the following
[26:53] stuff. We can do that using the window
[26:55] authentications or SQL server
[26:56] authentications. I'm going to say that
[26:58] let's stick with the window
[26:59] authentication. And the username going
[27:01] to be the PC name and as well the window
[27:04] user. If you don't have it for some
[27:06] reason those informations, you can go to
[27:08] your search search for
[27:10] cmd and then here you can say who am I?
[27:15] And with that you will get the PC name
[27:17] and as well the user that you are
[27:19] currently logged in. And this is exactly
[27:21] what I'm seeing over here. One more
[27:22] thing if you're having issue connecting
[27:24] to your database make sure to check the
[27:26] encryption. It should be mandatory and
[27:28] to click on the trust server
[27:30] certificates. So once you do that you
[27:31] will be able to connect. Okay. So with
[27:33] that we have the server we have the
[27:35] client. And now the last step we have to
[27:37] go and create the database. We want to
[27:39] insert our data. So now if you look to
[27:41] the object explorer and open the
[27:43] databases you can see that we don't have
[27:44] any database. So now let's do something
[27:46] about it. Go back to the course
[27:48] materials inside the data sets you will
[27:50] find the following. You will find we
[27:52] have here three folders MySQL postcress
[27:55] and SQL server. So if you want to follow
[27:57] with this course using different
[27:59] database like MySQL and Postgress you
[28:01] can find the exact same data for the
[28:03] database that you are using. But now in
[28:05] this course we are using the SQL server.
[28:07] So if you follow me with that go inside
[28:10] the SQL server folder and here you will
[28:12] find four files with different
[28:13] extensions. So what is going on here?
[28:15] Now for this course we have two
[28:16] databases. One that is very simple
[28:19] called my database and second one that
[28:21] has more tables called sales DB. And now
[28:24] in SQL server there are multiple ways on
[28:26] how to create databases. I will show you
[28:28] now two methods on how to create the
[28:30] database. Now the first option we want
[28:32] to create the database from a script.
[28:34] And if you look to those files, we have
[28:36] here two files with the extension SQL.
[28:39] Those are files with SQL code. So let's
[28:41] start with the first one, the init SQL
[28:43] server my
[28:45] database.SQL. Go inside it. And now here
[28:47] we have the SQL code. Copy everything.
[28:49] And now let's go back to our studio and
[28:51] then go to the menu and click on new
[28:53] query. And here in the middle you can
[28:55] paste the code. So now we have the code
[28:57] for the first database. And all what you
[28:59] have to do is to go and execute it. So
[29:01] once we executed you will see we will
[29:03] not get any error. And now on the left
[29:05] side we don't see yet our database
[29:07] because we have to refresh. So right
[29:09] click on the databases and click
[29:10] refresh. And now you can see it my
[29:12] database. So now let's see the content.
[29:14] Go extend it and then go extend the
[29:16] tables. And now you see here our two
[29:18] tables customers and orders. Inside
[29:20] those tables we can find our data. In
[29:22] order to see the data right click for
[29:24] example of the customers and let's go
[29:26] with the option select top 1,000 rows.
[29:28] Once you do that you can see now in the
[29:30] results we have here five customers.
[29:33] This is our data inside the table
[29:34] customers. So here again about the
[29:36] interface on the left side we have the
[29:38] object explorer where you can see the
[29:40] whole structure of the database from
[29:41] server to databases to tables. So you
[29:44] can see the whole structure on the top
[29:46] we have a menu with a lot of icons and
[29:48] then in the middle this place here we
[29:50] call it the SQL editor. We're going to
[29:52] go and write their SQL codes and then
[29:55] once you execute it at the bottom you
[29:57] will get the result and messages and
[29:59] below the SQL editor we have the output.
[30:01] So here you can see for example the data
[30:03] the results or different messages from
[30:05] the database. So the interface is very
[30:07] simple. Now we have to go and get our
[30:09] second database. So if you go back to
[30:11] our files you can find a second SQL file
[30:14] the initql server sales db.sql. Open
[30:17] that and let's go and copy everything
[30:19] here and let's go back to our studio.
[30:22] Same thing you have to go and create a
[30:23] new query then paste the whole code and
[30:26] this database is about the sales DB. So
[30:29] let's go and execute it and with that we
[30:31] will not get any errors and now we go to
[30:33] the left side and we do the same thing
[30:35] refresh and we can see the second
[30:37] database sales DB. Now we can go and
[30:39] explore it. So extend it go to the
[30:41] tables and here you can see five tables
[30:43] customers employees orders products. So
[30:46] here this is the intermediate database
[30:48] for our course. So now let's go and
[30:49] check our data. For example, let's go to
[30:51] the orders, right click on it and select
[30:53] top 10,00. And those are the orders of
[30:56] our database. Perfect. So everything is
[30:58] working. So those are the main two
[31:00] databases that we will be working
[31:02] through the whole course. And of course
[31:03] if you want to go and practice using
[31:05] another database, it's totally fine. For
[31:07] example, in Microsoft, there are a
[31:09] database called Adventure Works. It is
[31:11] really amazing. And I'm going to show
[31:13] you now how to import it. We can go over
[31:14] here the adventure works. So let's click
[31:17] on this link. So now we are again in
[31:18] Microsoft page. If you scroll down you
[31:21] can see here three different types of
[31:22] databases. The OLTB, data warehouse and
[31:25] lightweights. So they are like different
[31:27] databases. The OLTP is the most like
[31:30] complicated one. A lot of tables and
[31:32] transactions and so on. The data
[31:34] warehouse it is like really nice one in
[31:36] order to do data analyzes and stuff. The
[31:38] lightweight it is the simplest one. So
[31:40] let's go for example and get the data
[31:42] warehouse. So click on that and now as
[31:44] you can see the extension of this file
[31:47] isbak and now I'm going to show you the
[31:49] second way on how to create databases in
[31:51] SQL server. So now all what you have to
[31:53] do is to go to the following path. It
[31:55] really depends where you have installed
[31:57] the SQL server. So for me I have
[31:59] installed it in the program files
[32:00] Microsoft SQL Server MSSQL SQL Express
[32:04] then MSSQL backup. You have to go there.
[32:07] So here what you can do you can place
[32:09] all the files with the extension bak.
[32:12] For example, the adventure works that we
[32:13] just installed. This is a backup file
[32:16] for the database and we want to go and
[32:18] restore it and with that you are
[32:20] creating like a database. So this is the
[32:22] second method on how to create databases
[32:24] in SQL server by restoring the database.
[32:27] If for some reason the script didn't
[32:29] work for you. Now let me show you
[32:30] quickly how we can do that. Let's go
[32:32] back to our studio. Right click on the
[32:34] database and then here we have an option
[32:36] called restore database. Click on that.
[32:38] And now here we have two options under
[32:40] the source database and device. The
[32:42] default going to be database but we have
[32:44] to switch to a device because we want to
[32:46] import it from files. And then we go to
[32:49] these three dots. Click on that. And now
[32:51] we have to go to the option add. And now
[32:53] it's going to take you to the place
[32:54] where the SQL server creates backups. So
[32:57] here we can find our files and what we
[32:59] want you to create is the adventure
[33:00] works. Select that. Then okay, one more
[33:03] okay and one final okay. So now the
[33:06] database will be restored and it is
[33:08] successfully. So now on the left side we
[33:10] can see our third database. If you don't
[33:12] see it go and refresh of course and here
[33:14] you will find a lot of tables in the
[33:16] adventure works. And as usual we can go
[33:18] and explore the data by selecting top
[33:20] thousand rows. So my friends now you
[33:22] have three databases but of course our
[33:24] focus is only the first two that we have
[33:25] done my database and sales DB. And with
[33:28] that you have learned two ways on how to
[33:30] import databases into SQL server. So
[33:33] with that my friends we have prepared
[33:34] everything. We have the SQL Server
[33:36] Express running on your local PC. We
[33:38] have the studio the clients where we're
[33:40] going to use it in order to interact
[33:42] with the database and we have created
[33:44] our two databases that we will be using
[33:47] in order to practice SQL. So we are
[33:49] ready. All right my friends. So with
[33:50] that we are done with the first chapter.
[33:52] We have our introduction to SQL and now
[33:54] we're going to start learning the first
[33:56] thing in SQL and that is how to query
[33:58] our data. So let's go and start with
[34:00] that.
[34:05] Okay, so now we can understand exactly
[34:07] what is an SQL query. Now normally your
[34:09] data is inside the table and your table
[34:11] is inside the database and now you might
[34:13] have a question from the business like
[34:15] what is the total sales? What is the
[34:17] total number of customers? So any
[34:19] question that you have in your mind and
[34:21] you want to go and ask your data you
[34:23] want to go and retrieve data from the
[34:25] database and in order to do that you
[34:27] have to talk to the database using its
[34:29] language the SQL. So in order to do that
[34:32] you're going to go and write a query
[34:34] where you write inside the query
[34:36] something called select statement and
[34:38] with that you are asking the database
[34:40] for data. So once you execute your query
[34:42] the database going to go and fetch your
[34:44] data and then it prepares a result to be
[34:47] sent back to you. So with that you are
[34:49] asking the database a question by
[34:51] writing a query and the database going
[34:53] to process your query and answer your
[34:55] question by sending back data and with
[34:57] that we are like reading our data from
[35:00] the database and the queries will not
[35:02] modify anything will not change the data
[35:04] inside your tables or even change the
[35:06] structure of the database. So you use
[35:08] select statement only in order to read
[35:11] something from the database. You just
[35:13] want to retrieve data from the database.
[35:16] So this is what we mean with a
[35:20] query. And now my friends, each SQL
[35:23] query has usually different sections,
[35:25] different components. We call them
[35:27] clauses. And this is amazing because
[35:29] you're going to have enough tools to
[35:31] write a query that matches any question
[35:33] that you have about your data. So what
[35:35] we're going to do, we're going to cover
[35:36] all those clauses step by step in order
[35:39] to write any query that you need. So now
[35:41] we're going to start with two clauses
[35:43] that makes the simplest query in SQL.
[35:45] the select and from. So let's start with
[35:51] that. All right. So now it's really
[35:53] important for me that you understand how
[35:55] SQL works with the code with the
[35:57] queries. So now what I'm going to do,
[35:58] I'm going to show you on the right side
[36:00] the syntax of the query in SQL and then
[36:03] on the left side I'm going to show you
[36:04] exactly step by step how SQL going to go
[36:07] and execute your query. So now we have
[36:09] the table customers inside our database
[36:11] and we will start with the easiest form
[36:13] where we're going to select everything.
[36:15] Select the star. So the select star is
[36:17] going to go and retrieve all the columns
[36:20] from your table. So everything and the
[36:22] from clause it's going to tell SQL where
[36:24] to find your data. So with the select we
[36:27] select the columns that we want and the
[36:29] from you specify the table where your
[36:31] data come from. So the syntax going to
[36:33] be very simple. In each query we start
[36:35] always with the select. And now since we
[36:37] want all the columns we're going to
[36:39] write star and with that SQL going to
[36:41] understand I want to see everything. And
[36:43] then after that comes the keyword from.
[36:45] And now we want to tell SQL where the
[36:46] data come from. So we have to specify
[36:48] the table name. And that's it. This is
[36:50] all what you need to do. So once you
[36:52] execute it what's going to happen? SQL
[36:54] going to go and execute first the from
[36:56] clause. So it's going to go and retrieve
[36:58] all the data from the database to the
[37:00] results. And then in the next step going
[37:02] to go and check the select statement. So
[37:04] which columns we have to keep in the
[37:06] result since you are saying star then
[37:08] the SQL going to keep everything all the
[37:10] columns and with that you will see in
[37:11] the result everything all the columns
[37:13] and all the rows. So that's it. This is
[37:16] how it works. Now let's go back to scale
[37:18] in order to select few data from our
[37:19] database. Okay. So back to our studio.
[37:21] Let's go and start a new query and let's
[37:24] go and find our database just to expand
[37:26] it and our tables. Now it is very
[37:28] important to make sure that you are
[37:30] connected to the correct database. So go
[37:32] to the top left in the menu over here
[37:34] and make sure to select your database.
[37:36] So my database like this or we have a
[37:38] command for that called use and then
[37:40] just write the database name like this.
[37:42] So I'm telling SQL just use my database
[37:45] like this and with that SQL going to
[37:47] switch to your database. Now if you are
[37:49] learning any new programming language,
[37:50] it is very important to understand about
[37:52] the comments. So comments are like notes
[37:54] that you add to your code in order to
[37:56] understand what is going on. And of
[37:58] course the engine, the database will not
[38:00] go and execute it. it's going to go and
[38:02] ignore everything inside it. And there
[38:04] is like two ways on how to do that.
[38:05] Either you make inline comments by
[38:07] typing two dashes like this and then you
[38:09] write anything this is a comment. So now
[38:13] in SQL if you see it is green that means
[38:15] it is a comments. Now the other type you
[38:18] can have multiple line comments and in
[38:20] order to do that what you can do you can
[38:21] write slash and then start and then you
[38:24] can write anything this and then start a
[38:26] new line is a comment. So as you can see
[38:29] all the lines after the slash star it is
[38:32] getting green that means it is a comment
[38:34] and now let's say that you are at the
[38:35] end. So in order to close it you write
[38:37] again star and then slash and that you
[38:40] are telling SQL I'm done with my
[38:41] comments. So those are the two types of
[38:43] writing comments in SQL. Now back to our
[38:45] query. Let's say that we have the
[38:47] following task says retrieve all
[38:49] customer data. So I would like to see in
[38:51] the results all the data of my customers
[38:53] everything all the rows and all the
[38:55] columns. So currently our data is stored
[38:57] inside the table called customer and I
[38:59] need to see all the data in the output.
[39:01] In order to do that we're going to write
[39:03] a query and all our query start always
[39:05] with a select and since I need
[39:07] everything all the columns we write star
[39:09] and then a new line. Let's go and
[39:10] specify for SQL from where it's going to
[39:13] go and get the data. So it's going to be
[39:15] from and then we going to write the name
[39:17] of the table. It must be exactly like it
[39:19] is in the database. So it's called
[39:21] customers and you have to have it here
[39:23] as a customers. So that's it. Let's go
[39:25] and execute it. And now if you look to
[39:27] the results, you can see we have four
[39:29] columns and five rows. So with that you
[39:32] are seeing everything inside the table
[39:34] customers. You can see we have five
[39:35] customers and you can see all the
[39:37] columns about the customers. So this is
[39:39] very simple. We have ask question for
[39:41] the database using SQL query and the
[39:44] database should answer our question by
[39:46] returning our data in the results. All
[39:48] right. So now let's move to another
[39:49] task. I'm going to go and create a new
[39:51] query and this time we're going to
[39:52] retrieve all the order data. So that
[39:54] means I would like to see all the data
[39:55] inside the orders. So let's go and write
[39:57] a very simple query. We start as usual
[40:00] with select and since we want
[40:01] everything. So it is select star from
[40:04] our table orders. So that's it. Let's go
[40:07] and execute. And with that you can see
[40:08] in the output we have again four columns
[40:11] but this time we have only four rows. So
[40:13] that means in this table we have four
[40:15] orders and we can see all the data
[40:17] inside this table. So with that we can
[40:19] understand we have five customers inside
[40:22] our database and these customers did
[40:24] generate four orders. So as you can see
[40:26] we are now talking to our database and
[40:28] this is the simplest form of query in
[40:34] SQL. All right. So now let's move to the
[40:36] next step in our query where you say you
[40:39] know what I don't want to see all the
[40:40] columns from the database. I want to be
[40:42] more specific. So I would like to select
[40:44] exactly the columns that I need. So now
[40:47] we want to select few columns from the
[40:49] database where we select only the
[40:51] columns that we need instead of
[40:53] everything. Now about the syntax we're
[40:54] going to go and change a little thing.
[40:56] So instead of using star we're going to
[40:58] go and make a list of columns that we
[41:00] want to see in the output. So we're
[41:02] going to select column one column two
[41:04] and we're going to separate them using a
[41:06] comma. So we are just writing a list of
[41:08] columns exactly after the select. And
[41:10] for the from it's going to stay as it
[41:12] is. So from a table. Now if you execute
[41:14] this what going to happen as usual SQL
[41:16] going to start with the from. So it's
[41:18] going to go and get the data from the
[41:19] database and then the next step is going
[41:21] to go and check the select. So what
[41:23] going to happen? SQL going to go and
[41:24] keep only two columns like for example
[41:27] the name and the country and all the
[41:29] columns that are not mentioned in the
[41:31] select statements will be excluded. So
[41:33] SQL going to go and remove it from the
[41:35] results and keeps only the columns that
[41:37] we mentioned in our query. So this time
[41:40] instead of having four columns in the
[41:41] output we can have only two. So with
[41:43] that you are like filtering the columns
[41:45] and you are selecting exactly what you
[41:47] need. So now let's go back to SQL in
[41:49] order to practice this. All right. So
[41:51] now we have the following task and it
[41:53] says retrieve each customer's name,
[41:55] country and score. So that means I don't
[41:58] want to see everything from the table
[42:00] customers. I need only to see the three
[42:02] columns. So let's see how we can do
[42:04] that. As usual we start with select and
[42:06] I'm going to go with a star in order to
[42:08] see the whole table first from the table
[42:09] customers. So it's exactly like before.
[42:12] Let's go and execute it. And now I can
[42:13] see everything inside the table
[42:15] customers. But the task says I need only
[42:17] three columns. So now what we're going
[42:18] to do instead of the star, we're going
[42:20] to make a list of columns. So we start a
[42:23] new line and then we write the name of
[42:25] the first column. So the first name and
[42:27] a new line for the second column for the
[42:30] country and then again a comma and then
[42:32] we write a score. So with that we have
[42:34] the three columns. Now what I usually
[42:36] do, I go and select them and give it
[42:38] then a push using a tab. This just looks
[42:40] nicer and easier to read. So with that
[42:42] we have now between the select and from
[42:44] list of columns. Now there is like
[42:46] mistake that happens a lot where we go
[42:48] and type a comma after the last column.
[42:50] So if you do that and execute it you
[42:52] will get an error because SQL going to
[42:54] expect from you a column after the comma
[42:56] and since there is no column and
[42:58] immediately you have a from you will get
[43:00] an error. So there is no need for a
[43:01] comma after the last column. Now let's
[43:03] remove it and execute. And now that you
[43:05] can see in the output we don't have four
[43:07] columns we have only three. the first
[43:09] name, the country and the score. And by
[43:11] the way, they are ordered exactly like
[43:14] you selected in your query. So first we
[43:16] have the first name and then the country
[43:19] and then the last one the score. So that
[43:20] means if I go and now change the order.
[43:23] So let's get the country at the end and
[43:25] execute. You will see the country at the
[43:28] end. I'm going to go and put it back in
[43:30] between to match exactly like the task
[43:32] and remove the last comma. So execute
[43:35] again. And with that we have selected
[43:37] few columns from our table. So we are
[43:39] more specific to what we need. Okay. So
[43:41] that we have covered the two select and
[43:43] from next we're going to talk about the
[43:45] wear clause that you can use in order to
[43:48] filter your data. So let's
[43:53] go. So what is exactly where? We use
[43:56] where in order to filter our data based
[43:58] on a condition and any data that fulfill
[44:01] the condition going to stay in the
[44:03] output in the result and the data that
[44:06] don't meet the condition will be
[44:08] filtered out of the results. Condition
[44:09] could be anything like for example we
[44:11] say the score must be higher than 500 or
[44:14] you can say the country must be equal to
[44:16] Germany. So any condition that you have
[44:18] in your question. Now let's see the
[44:19] syntax in SQL. As usual we start with a
[44:22] select. We select the columns that we
[44:24] need. Then we write from where the data
[44:26] come from and then after the from we're
[44:28] going to write the where and exactly
[44:30] after that you specify your condition.
[44:32] So now let's see how SQL going to
[44:33] execute this. First SQL start as usual
[44:36] from the from. So it's going to go and
[44:37] get your data from the database and
[44:39] after that SQL going to go and execute
[44:42] the wear clause. So let's say that the
[44:44] condition should be higher than 500. And
[44:46] now what going to happen? SQL going to
[44:48] check each row whether it meets this
[44:51] condition or not. So for example for
[44:52] Maria she doesn't fulfill the condition
[44:54] because her score the 350 is not higher
[44:58] than 500. So she doesn't fulfill the
[45:00] condition and SQL going to go and remove
[45:03] completely this row this record from the
[45:05] results. Now SQL going to go to the
[45:07] second record. So Joan is fulfilling the
[45:10] condition. So he going to stay in the
[45:11] result. The same thing for George. Now
[45:13] moving on to the fourth one Martin. So
[45:16] this customer is not fulfilling the
[45:18] condition and SQL going to go and remove
[45:20] it from the results. The same things
[45:22] happen for the last customer. The score
[45:23] is zero and not fulfilling the
[45:25] condition. So that means if we apply
[45:27] this filter, SQL going to return only
[45:29] two customers out of five. So with that
[45:32] we are filtering the rows based on
[45:34] condition using the work clause. Now as
[45:36] you can see in the result we are getting
[45:38] all the columns but if you specify in
[45:40] the query like for example only two
[45:42] columns like the name and the country
[45:44] then SQL going to start removing as well
[45:47] the columns of the results. And this
[45:49] means in the output we will get only two
[45:51] columns and two rows. So with that you
[45:53] are filtering the columns and the rows
[45:56] of your results. So now let's go back to
[45:58] scale in order to practice this. All
[45:59] right. So let's have the following task
[46:01] and it says retrieve customers with a
[46:04] score not equal to zero. So now if you
[46:06] are looking to our task you see we have
[46:08] like here a condition. The condition
[46:09] says the score must not be equal to
[46:12] zero. So I don't want to see all the
[46:13] customers. I want to see only the
[46:15] customers thus fulfill this condition.
[46:18] So it's like we have to filter the data.
[46:19] So let's go and solve the task. Let's
[46:21] start as usual. Select star. There's no
[46:23] specifications about the columns from
[46:25] our table customers. Okay. So I'm going
[46:28] to start with this. Let's go and execute
[46:30] it. Now if you look at the result, you
[46:31] can see like almost all the customers
[46:33] are fulfilling the condition. Their
[46:35] scores are not equal to zero. Only one.
[46:38] The last customer his score is zero. So
[46:40] this customer does not fulfill our
[46:42] condition. Now let's go and build filter
[46:44] for that. So we're going to say where.
[46:46] And now there will be a section that is
[46:48] only focusing on how to build conditions
[46:50] and filtering in SQL. So don't worry a
[46:52] lot about the syntax of the conditions.
[46:54] We're going to cover that later of
[46:55] course but it is very simple. Now for
[46:57] the condition we need a column. So in
[46:58] which column is our condition based on
[47:00] it's going to be on the score. So we're
[47:02] going to write here score and since we
[47:04] are saying not equal there is like an
[47:06] operator in SQL called not equal and
[47:08] then we have to write a value after
[47:10] that. It's going to be a zero. So again
[47:11] the condition is like this. The score
[47:13] must not be equal to zero. It's very
[47:16] simple, right? And with that we have our
[47:18] condition and we are using the where in
[47:20] order to filter the data. So let's go
[47:22] and execute it. And now as you can see
[47:23] SQL did remove the last customer because
[47:26] he is not fulfilling this condition. And
[47:28] we have now only the rows that fulfill
[47:31] our condition. So as you can see it is
[47:33] very simple how to filter the data. All
[47:35] what you have to do is to write where
[47:36] clause after the from and then write a
[47:39] condition after that. Now let's have
[47:40] another task like for example it says
[47:42] retrieve customers from Germany. So I
[47:45] don't want to see all customers from
[47:46] different countries. I just want to see
[47:48] the customers that come from Germany. So
[47:50] that means we have a condition here.
[47:52] Country of the customer must be equal to
[47:55] Germany. So let's go and remove the
[47:56] current condition. It is not the one
[47:58] that we need and execute. If you are
[48:00] looking to the results, we have two
[48:02] customers that come from Germany and we
[48:04] are interested only to show those two
[48:06] customers. So let's go and make a filter
[48:08] for that. We're going to write where
[48:10] clause and after that we need a column.
[48:12] The column going to be the country. So
[48:14] we're going to write here country and
[48:15] this time the country must be equal to
[48:18] Germany. So we're going to write an
[48:20] equal operator. So we're going to write
[48:22] Germany like this exactly like the value
[48:24] inside our data. But now as you can see
[48:26] we are getting like an error here. And
[48:28] that's because in SQL if you want to
[48:30] write a value that contains characters
[48:33] then you have to put it between two
[48:35] single quotes. So at the start you put a
[48:37] single quote and as well at the end. And
[48:39] now as you can see the red line is away
[48:41] and the value now is red and that's
[48:43] because it is a string value. It is a
[48:45] value that contains characters and with
[48:47] that you will not get an error. So if
[48:49] your columns contains only numbers you
[48:51] can write it without single quotes. But
[48:53] if your values contains characters then
[48:55] you have to write it between two single
[48:57] quotes. Okay. So now back to our
[48:58] condition the country must be equal to
[49:00] Germany. Let's go and execute it. And it
[49:03] is working. So as you can see now we are
[49:04] seeing in the output only the customers
[49:07] does fulfill my condition where the
[49:09] country is equal to Germany. So this is
[49:11] exactly how we work with the wear clause
[49:13] in order to filter our data. So my
[49:15] friends this is how you filter your
[49:17] rows. And now let's say that I would
[49:18] like to filter the rows together with
[49:20] the columns. So I just want to keep the
[49:22] first name and the country and not
[49:24] interested to see the scores and the
[49:25] ids. So in order to do that we're going
[49:27] to go to the select and list the columns
[49:29] that we want to see. So the first name
[49:32] and after that a comma then the country
[49:34] and that's it. So let's go and give it a
[49:37] push and execute it. So we have two rows
[49:39] and two columns. So guys as you can see
[49:42] SQL is very simple. All right. So with
[49:44] that you have learned how to filter your
[49:46] data using the wear clause. Next we're
[49:48] going to talk about how to sort your
[49:50] data using the order by. So let's
[49:56] go. Okay. So what is exactly order by?
[49:59] You can use this type of clouds in order
[50:01] to sort your data. And of course, in
[50:03] order to sort your data, you have to
[50:04] decide on two mechanism. Either you want
[50:06] to sort your data ascending from the
[50:09] lowest value to the highest value or
[50:11] exactly the opposite way using
[50:13] descending from the highest value to the
[50:15] lowest. And the syntax kind of looks
[50:17] like this. So as usual, we start with
[50:18] the select and then from and after the
[50:21] from you can specify order by and with
[50:23] that you are telling SQL we have to sort
[50:25] the data and you have to specify two
[50:27] things. First you have to specify for
[50:29] SQL the column that should be used in
[50:31] order to sort the results. So for
[50:33] example you can say score and after the
[50:36] column name you have to specify the
[50:37] mechanism. So for example you say
[50:39] ascending from the lowest to the
[50:41] highest. And in SQL if you don't specify
[50:43] the mechanism the default going to be
[50:45] ascending. So you will not get an error
[50:47] if you don't specify anything after the
[50:49] column name. But my advice here is
[50:51] always to specify something after the
[50:54] column easier because it's just
[50:56] straightforward and easier to understand
[50:58] and if someone reads it can understand
[51:00] immediately it's going to be ascending
[51:02] because maybe not everyone knows what is
[51:04] the default in SQL. So always specify a
[51:07] value even if it's like easier to skip
[51:09] it and if you want to store the data
[51:10] from the highest to the lowest then you
[51:12] can specify descending. So as usual SQL
[51:15] going to go and start from the from it's
[51:16] going to go and grab your data from
[51:18] database. Then the second step is SQL
[51:20] going to go and sort the result. So the
[51:22] order by going to be executed and SQL
[51:24] going to see okay I'm going to sort it
[51:25] by the score and using the sending
[51:28] mechanism and still going to go and
[51:29] start like moving around your rows where
[51:32] the first row going to be the customer
[51:34] with the highest score and in this
[51:36] example John has the highest score the
[51:38] 900. So John going to appear as a first
[51:41] row at the result and that's because his
[51:43] score and after that the second highest
[51:45] is going to be George with 750 and SQL
[51:48] going to go and keep sorting the data
[51:50] and then we have 500 then 350 and the
[51:53] last row going to be the customer with
[51:55] the lowest score the zero. So this is
[51:57] how SQL executes your order by. Now
[52:00] let's go back to scale in order to
[52:01] practice. All right. So now we have the
[52:03] firming task and it says retrieve all
[52:05] customers and sort the result by the
[52:08] highest score first. So now by looking
[52:10] at the task we need all the customers.
[52:12] So there is like no conditions or
[52:14] anything to filter but we have to sort
[52:16] the results. So let's go and do that.
[52:18] We're going to start as usual by
[52:19] selecting all the columns from the table
[52:22] customers. So now if you go and execute
[52:24] it you will get all your customers and
[52:26] you are now seeing the data exactly like
[52:28] stored in the database. And you can see
[52:30] the result is not sorted by the scores.
[52:32] So we have here a low score then high
[52:34] score then low and so on. Now the task
[52:37] says we have to sort the results. So we
[52:39] have to go and use the order by and now
[52:42] you have to understand from which column
[52:43] and we can get that from the task. So it
[52:45] says it should be sorted by the score.
[52:48] So we're going to go and define the
[52:49] score here. And the final thing that you
[52:51] have to define is the mechanism
[52:52] descending or ascending. And you can get
[52:54] it as well from the task. So we have to
[52:56] sort the data by the highest score
[52:58] first. So the highest first and then the
[53:00] lowest. So that means we're going to go
[53:02] and use the descending. So that's all.
[53:04] Let's go and execute it. Now as you can
[53:06] see in the results, the first customer
[53:08] has the highest score. Then we have the
[53:10] second one with the second highest until
[53:12] the last one with the lowest score.
[53:14] That's it. This is how you sort your
[53:16] data. And with that we have solved the
[53:17] task. Now let's do exactly the opposite.
[53:20] So we want to sort the results by the
[53:22] lowest score first. So that means we
[53:24] want to see first the customers with the
[53:26] lowest score like here in this example
[53:28] we should see the ID number five as the
[53:30] first because he has the lowest score
[53:32] the zero. Now in order to do that all
[53:34] what you have to do is to switch the
[53:35] mechanism instead of descending when you
[53:37] can use ascending. Let's go and execute
[53:39] it. And that's it. As you can see now we
[53:41] have the lowest score then the second
[53:43] lowest score until the last row. It's
[53:46] going to be the customer with the
[53:47] highest score. So the lowest score comes
[53:50] first. So it is very simple. This is how
[53:52] you sort your data using
[53:56] SQL. And now I'm going to show you one
[53:58] more thing that you can do with the
[54:00] order by. You can sort your data using
[54:02] multiple columns. And we call it nested
[54:04] sorting. So now let's take this very
[54:06] simple example where you want to sort
[54:08] your data using country. So we are
[54:10] saying order by the column country and
[54:13] the mechanism going to be ascending. So
[54:15] from the lowest to the highest. Now if
[54:16] you do that going to go and sort the
[54:18] data this time based on the country. So
[54:20] we're going to have like the first two
[54:22] customers from Germany. It is sorting it
[54:24] alphabetically. Then we have the UK and
[54:26] the last two going to be from USA. Now
[54:28] if you are checking the final results
[54:29] you might say you know what there is
[54:31] like something wrong. The data is not
[54:34] completely sorted correctly. So if you
[54:36] are looking to the first two customers
[54:37] that come from country Germany. You can
[54:39] see the scores are sorted in ascending
[54:42] way from the lowest to the highest. So
[54:44] first we have 350 then 500. Then UK it's
[54:47] fine because we have only one customer.
[54:49] Now if you look to the customers from
[54:51] USA you see that it is like sorted the
[54:53] way around. It is sorted descending from
[54:56] the highest to the lowest. So first we
[54:58] have the score 900 then zero. So there
[55:00] is like no clean way on how the data is
[55:02] sorted and the result is not really
[55:04] clean and this issue happens usually if
[55:06] you are sorting your data based in a
[55:08] column that has repetition like here the
[55:10] country we have twice Germany and twice
[55:12] USA. So now in order to refine the
[55:15] sorting and make it more correct, we can
[55:17] include in the sorting another column in
[55:20] this scenario for example the score. So
[55:22] we can make a list of columns in the
[55:24] order by and we can separate them using
[55:26] the comma. And of course you can have
[55:27] different mechanism for each column like
[55:29] for the country we are saying it is
[55:31] ascending but for the score we say you
[55:33] know what let's make it descending. It
[55:34] will not be only one for all columns. So
[55:37] now what can happen is we're going to
[55:38] start sorting the data for each section.
[55:41] So for the two customers from Germany
[55:43] the sorting going to be from the highest
[55:44] to the lowest. So it's going to go and
[55:46] switch the two customers. So Martin
[55:48] going to be first because he has higher
[55:50] score than Maria. And with that we are
[55:52] refining the scores based on the same
[55:54] value of course the country. Now for the
[55:56] UK nothing going to happen because we
[55:58] have only one value and for the USA as
[56:00] well nothing going to happen because it
[56:02] is already sorted in the correct way
[56:04] from the highest to the lowest. So as
[56:06] you can see if you are including a
[56:07] second column you are refining your
[56:10] sorting and as well my friends the order
[56:12] is very important. So this is how you
[56:14] can do nested sorting in SQL. Let's go
[56:17] back to our SQL and start practicing.
[56:19] All right so now we have the following
[56:20] task and it says retrieve all customers
[56:23] and sort the results by the country and
[56:26] then by the highest score. So again we
[56:29] need all customers. So select everything
[56:31] from customers table. And now the task
[56:33] says we have to sort the result by the
[56:36] country. So we're going to start with
[56:37] the order by and since it says by the
[56:39] country. We're going to go with the
[56:40] country and we're going to sort it
[56:42] alphabetically. So it's going to be
[56:43] ascending. So let's go execute it. Now
[56:46] you can see the data is sorted
[56:47] completely differently by the country.
[56:49] So we have first Germany, UK and then
[56:51] USA. But that's not all and says then by
[56:55] the highest score. So we have to go and
[56:57] include another column in the sorting
[56:59] and we can go and add that by adding a
[57:01] comma and then mention another column
[57:03] the score and now we have to specify the
[57:05] mechanism. It says by the highest score.
[57:07] So the highest must come first and with
[57:09] that we are using descending. Now what
[57:11] is the current situation in that? If you
[57:13] look to the results for example for
[57:15] those two customers we have 350 and then
[57:18] 500. So that means the scores are sorted
[57:21] ascending right the same thing for USA.
[57:23] So from the lowest to the highest. Now
[57:25] if you go and do it like this what going
[57:27] to happen it's going to go and switch
[57:29] it. So you can see over here now for
[57:31] Germany first comes the highest the 500
[57:33] and then the 350 and for USA as well
[57:36] they switched. So we have the highest
[57:38] and then the lowest and with that we
[57:40] have solved the task. Now again the
[57:41] order of those columns are very
[57:43] important. So since the scores comes
[57:45] after the country we will not get the
[57:47] highest scores first at the results. So
[57:49] we will not get the 900 as a first row.
[57:52] And that's because the scores must be
[57:54] sorted after the country. So the country
[57:56] has more priority. Now if you go and
[57:58] flip that. So let's go over here and
[58:01] says sort first the score and then the
[58:03] country. So let's go and execute it.
[58:05] It's called has first to sort the
[58:07] scores. So with that you will get the
[58:09] 900 first, right? And then the
[58:11] countries. And since there is like no
[58:13] duplicates in the scores, this makes no
[58:15] sense at all. So you can go and skip it.
[58:17] So nested sorting only makes sense if
[58:19] you have repetition in your results and
[58:22] you can use the help of a second column
[58:25] in order to make the sorting perfect. So
[58:28] that's it and with that of course we
[58:29] have solved the task. All right. So with
[58:31] that you have learned how to sort your
[58:32] data using order by. Now in the next
[58:35] step we're going to talk about how to
[58:37] aggregate and group up your data using
[58:39] group by and we're going to put it
[58:41] between the where and the order by
[58:43] because in the order of the query the
[58:45] group by comes between the where and the
[58:47] order by. So let's
[58:52] go. Okay. So what is exactly group by?
[58:54] It's going to go and combine the rows
[58:56] with the same value. So it's going to go
[58:58] and combine and smash press your rows to
[59:01] make it aggregated and more combined. So
[59:03] all what group by does it aggregates a
[59:06] column by another column. Like for
[59:08] example, if you want to find the total
[59:10] score by country. So you aggregate all
[59:13] the scores value for one country. If you
[59:16] have this kind of tasks, then you can
[59:17] use the group I. Let's see the syntax of
[59:20] that. We will start as usual with the
[59:21] select. And now what we want to see in
[59:23] the result is two columns. So we have to
[59:25] specify like a category like the
[59:27] country. This is the value that you want
[59:29] to group the data by. and another one
[59:31] where you are doing the aggregations. So
[59:33] for example you are saying I would like
[59:35] to see the total score. So we use the
[59:37] function sum in order to summarize the
[59:39] values of the score. After that as usual
[59:41] we use the from in order to select the
[59:43] data from specific table. And now comes
[59:45] the magic we use after the from group
[59:47] by. And now understands okay I have now
[59:50] to combine the data. I have to group up
[59:52] the data by something. And this time we
[59:54] are saying you have to group up the data
[59:56] by the country. So that means each value
[59:58] of the country must be presented in the
[01:00:00] output only once and for each country we
[01:00:03] want to see the aggregation and that is
[01:00:06] the total score. So let's see how is
[01:00:08] going to execute it. So it's going to
[01:00:09] first start with the from it's going to
[01:00:10] go and get the data from the database
[01:00:12] and then it's still going to execute the
[01:00:13] group by and now scale understand okay I
[01:00:16] have to group up now the data by the
[01:00:17] country and it understands it has to
[01:00:20] aggregate the scores for that. So it's
[01:00:22] going to go and identify the rows that
[01:00:24] are sharing the same value. Like for
[01:00:26] example here we have two rows for
[01:00:28] Germany and it's going to bring it to
[01:00:29] the results. So now we have two rows for
[01:00:31] the same country but since we are saying
[01:00:34] group by country SQL going to try and
[01:00:36] combine them smash them together in only
[01:00:38] one row. So each value of the country
[01:00:40] must exist at maximum once. We cannot
[01:00:43] leave it like this. So now what we going
[01:00:45] to do with the scores? We have two
[01:00:46] scores. Now SQL going to check the
[01:00:48] aggregate function. It is the
[01:00:49] summarization. So, and it's going to go
[01:00:51] and add those values 350 + 500. And with
[01:00:54] that, we're going to get the total score
[01:00:56] of 850. And with that, as you can see,
[01:00:59] scale is combining those two rows into
[01:01:01] one. So, in the output, Germany will
[01:01:03] exist only one. And about the scores, we
[01:01:05] will get the total score. And the same
[01:01:07] thing going to happen for the next
[01:01:09] value. In the country, we have the USA.
[01:01:11] We have it twice. So, we're going to get
[01:01:13] two rows. And scale going to combine
[01:01:15] those two rows in one because USA must
[01:01:17] exist only once. And with the scores we
[01:01:20] will have the total scores. So 900 plus
[01:01:22] zero we will get 900. And with that it's
[01:01:25] still converted those two rows into one.
[01:01:27] And for the last value in the countries
[01:01:29] we have the UK. It's going to stay as it
[01:01:31] is. There is no need to smash and
[01:01:33] combine anything because it's already
[01:01:35] one value. So my friends if you are
[01:01:36] looking to the output you can see we
[01:01:38] grouped the original data by the
[01:01:41] country. And that means we're going to
[01:01:42] get one row for each value inside the
[01:01:45] country column. So my friends the
[01:01:47] original data you have five rows in the
[01:01:49] output if you are using group by like
[01:01:51] this you will get only three rows. So
[01:01:53] this is exactly how the group by works.
[01:01:56] Let's go back to scale and practice.
[01:01:58] Okay. So we have the following task and
[01:01:59] it says find the total score for each
[01:02:02] country. So from reading this you can
[01:02:04] understand we have to do aggregations
[01:02:06] and we have to combine the data by a
[01:02:08] column. So now usually I start like
[01:02:09] this. I start selecting the columns that
[01:02:12] I need in order to solve this task. So
[01:02:14] what do we need? We need the country and
[01:02:17] score from our table customers. So let's
[01:02:20] start like this. Now you can see we have
[01:02:21] the countries and the scores. And the
[01:02:23] task says we have to group up the data
[01:02:25] by the country. So that means this is
[01:02:27] the column where we're going to do the
[01:02:28] group by and the total scores will be
[01:02:31] aggregated. So what we have to do? We're
[01:02:32] going to use the group by since it says
[01:02:34] for each country. We're going to use it
[01:02:36] over here. Group by country. And now we
[01:02:38] have to go and aggregate the scores. We
[01:02:41] cannot leave it like this. So we're
[01:02:42] going to say the sum of the score. So
[01:02:44] let's go and execute it. And with that,
[01:02:47] as you can see, we are getting the total
[01:02:49] scores for each country. So now instead
[01:02:51] of having five customers, we have only
[01:02:54] three rows now. And that's because the
[01:02:56] countries has three rows. And now if you
[01:02:58] check the result, you can see something
[01:03:00] weird. It says no column name. And
[01:03:02] that's because we have changed the
[01:03:04] scores. It's not anymore the original
[01:03:06] score. It is it is the total scores. We
[01:03:09] have summarized those values. So SQL
[01:03:12] don't know how we going to call it. So
[01:03:14] those values doesn't come directly from
[01:03:16] the database. It is manipulation that
[01:03:18] you have done here. Now in order to give
[01:03:20] a nice name for that we can go and add
[01:03:22] aliases. An alias it is only like a name
[01:03:25] that lives inside your query. So we can
[01:03:27] do it like this as and you can specify
[01:03:29] any name you want like for example total
[01:03:32] score. And now scale can understand okay
[01:03:34] this is the name for this column and if
[01:03:36] you go and execute it you will see the
[01:03:38] new name in the results. But you have to
[01:03:40] understand this name exists only in this
[01:03:42] query. You are not renaming anything
[01:03:44] inside your database and you cannot use
[01:03:47] it in any other queries. It is just
[01:03:49] something that is known inside this
[01:03:51] query and only for your results. And of
[01:03:53] course you can rename anything any
[01:03:54] column like for example here you can say
[01:03:57] this is the customer country and if you
[01:04:00] execute it you are just renaming the
[01:04:02] column in the output. So this is really
[01:04:04] nice in SQL. Okay. So now there is like
[01:04:06] one more thing about the group I the
[01:04:08] non-aggregated columns that you are
[01:04:10] adding in the select must be as well
[01:04:12] mentioned in the group I. So now for
[01:04:14] example let's say that okay I'm seeing
[01:04:16] now the countries the total scores I
[01:04:18] would like to see as well the first
[01:04:20] name. So you go over here and say you
[01:04:22] know what let's get the first name. So
[01:04:24] country first name the total scores and
[01:04:26] execute. You will get an error because
[01:04:28] it's going to tell you I need only the
[01:04:30] columns that you want to group the data
[01:04:32] by or should be aggregated. So now the
[01:04:35] first name it is not aggregated and as
[01:04:37] well not used for the group I. So it is
[01:04:40] just here to confuse SQL and it will not
[01:04:42] work. So if you bring a column either it
[01:04:45] should be in the aggregation or it
[01:04:47] should be part of the group I. So in
[01:04:49] order to fix this and you really want to
[01:04:50] see the first name you can go over here
[01:04:52] and say you know what let's add it to
[01:04:54] the group I and execute. This time it
[01:04:57] going to work because all the columns
[01:04:59] that are mentioned here is as well part
[01:05:01] of the group I. So now as you can see we
[01:05:03] have the countries the first name and
[01:05:05] the total scores and you can see again
[01:05:07] we have five rows we don't have three
[01:05:09] rows and that's because now you are
[01:05:11] combining the data by the country and as
[01:05:14] well the first name and now you can see
[01:05:15] in the output we are getting five rows
[01:05:18] we are not getting anymore the three
[01:05:19] rows the three countries and that's
[01:05:21] because SQL now grouping the data by two
[01:05:24] columns the combination of the country
[01:05:26] and the first name and those two columns
[01:05:28] gives five combinations and that means
[01:05:30] you will get five rows so that means you
[01:05:32] have to be really careful what you are
[01:05:34] defining in the group I and the number
[01:05:36] of the unique values that those columns
[01:05:38] are generating going to define the
[01:05:41] output the results. So if you go and
[01:05:43] remove the first name and from here as
[01:05:45] well you are grouping by only one column
[01:05:47] and this column has only three values
[01:05:49] and that's why you are getting three
[01:05:51] rows and with that of course we have
[01:05:52] solved the task and now let's extend the
[01:05:54] task and say find the total score and
[01:05:57] total number of customers for each
[01:05:59] country. So that means we need two
[01:06:01] aggregations. We have the total score
[01:06:03] and as well we need the total number of
[01:06:05] customers. So from reading this you can
[01:06:07] understand we still want to group up the
[01:06:09] data by the country but this time we
[01:06:11] need two type of aggregations. We need
[01:06:14] the total number of customers and the
[01:06:16] total scores. So we have almost
[01:06:17] everything but what is missing is the
[01:06:19] second aggregation. Now what you can do
[01:06:21] you can go over here and add another
[01:06:23] aggregate function called the count. And
[01:06:26] what we want to count is the number of
[01:06:28] customers. So we can go and add the ID
[01:06:30] over here and call it total customers.
[01:06:33] So now of course SQL going to So now if
[01:06:34] you go and execute it, you will get as
[01:06:37] well the total customers by the country.
[01:06:39] And now as you can see SSQL has no
[01:06:41] problem with the ID and that's because
[01:06:42] you are aggregating the ID. So SQL know
[01:06:45] what to do with it and how to combine
[01:06:47] it. So that means you don't have to
[01:06:48] mention the ID in the country because
[01:06:51] you are aggregating it. So that's all
[01:06:53] with that we have solved as well the
[01:06:54] task. All right. Right. So with this you
[01:06:56] have learned how to group up your data
[01:06:58] using the group eye. Next we're going to
[01:07:00] talk about another technique on how to
[01:07:01] filter your data but this time using the
[01:07:04] having clause. So let's
[01:07:09] go. All right. So what is exactly
[01:07:11] having? You can use it in order to
[01:07:13] filter your data but after the
[01:07:15] aggregation. So that means we can use
[01:07:18] the having only after using the group I.
[01:07:21] So let's see the syntax of that. So
[01:07:23] again like the previous example we are
[01:07:25] finding the total score by country. So
[01:07:27] we have our select from group I and now
[01:07:30] you say you know what I would like to
[01:07:31] filter the end results and in order to
[01:07:33] do that we use the having after the
[01:07:36] group I and now like the wear clause you
[01:07:38] have to specify a condition. So we have
[01:07:40] the following condition where we want to
[01:07:42] see in the results only the countries if
[01:07:45] their total score is higher than 800. So
[01:07:48] this going to be our condition. So now
[01:07:50] you might noticing something with the
[01:07:51] group by we are using the country the
[01:07:54] column where we are grouping the data by
[01:07:55] its value but with the having we are
[01:07:58] using the aggregated column the sum of
[01:08:01] the score. So this is how the syntax
[01:08:03] works and now let's see how is going to
[01:08:05] execute it. So as usual SQL start with
[01:08:07] the from we are getting our data and
[01:08:09] then the second step is going to go and
[01:08:11] aggregate the data by the country. So
[01:08:13] it's like before going to group the rows
[01:08:16] with the same value of the country. So
[01:08:18] we're going to have one row for each
[01:08:20] country and this is what going to happen
[01:08:21] if you use group I and with that we have
[01:08:23] now aggregated values right and after
[01:08:26] the group IQL going to go and execute
[01:08:28] the having. So having it is like a
[01:08:30] filter. Now we have a nice condition the
[01:08:32] total sale must be higher than 800 and
[01:08:35] SQL going to go and check the new
[01:08:36] results after the aggregation. So in
[01:08:39] Germany we have the total sales of 850.
[01:08:41] So it meets the condition and it going
[01:08:43] to stay in the results. The same thing
[01:08:44] for USA it is higher as well than 900s
[01:08:47] but for UK it is not meeting the
[01:08:49] condition 750 it is not higher than 800
[01:08:53] and SQL going to go and filter out this
[01:08:55] row so that means after applying the
[01:08:57] having we will get only two countries
[01:08:59] because they have values that is
[01:09:01] fulfilling the condition and that's it
[01:09:03] is what can happen if you are using
[01:09:05] having it is simply filtering the data
[01:09:07] but now you might be confused you say
[01:09:09] you know what we have used the wear
[01:09:10] clouds to filter the data so why we have
[01:09:12] in SQL another cloud how to filter my
[01:09:15] data. Can't we just use the where? Well,
[01:09:17] in SQL there are like different ways on
[01:09:19] how to filter your data based on the
[01:09:21] scenario. So now let's go and add both
[01:09:23] of the filters in my query. We are
[01:09:25] already using the having after the group
[01:09:27] I and now let's go and add the wear.
[01:09:29] Usually the wear comes between the from
[01:09:31] and the group I so directly after the
[01:09:33] from. And here we are saying the score
[01:09:35] must be higher than 400. So now we are
[01:09:37] filtering based on the scores twice,
[01:09:39] right? Once we are saying the score
[01:09:41] higher than 400 and by having we are
[01:09:43] saying the sum of score must be higher
[01:09:45] than 800. So what is the big difference?
[01:09:48] It is when the filter is happening. If
[01:09:50] you want to filter the data before the
[01:09:52] aggregation you want to filter the
[01:09:54] original data then you can go and use
[01:09:57] the wear clause. But if you want to
[01:09:59] filter the data after the aggregations
[01:10:01] after the group by then you can go and
[01:10:03] use having. So it's really all about
[01:10:05] when the filter is happening. So let's
[01:10:07] see how is still going to execute this.
[01:10:09] So as usual first the from going to be
[01:10:11] executed to get the data. Then after
[01:10:13] that the second step the wear going to
[01:10:15] be executed. This is our first filter.
[01:10:17] So SQL going to filter the data using
[01:10:19] where before doing any aggregations and
[01:10:22] based on our condition the first
[01:10:24] customer will be filtered out because
[01:10:26] score is less than 400 and the same
[01:10:28] thing for the last customer. Now after
[01:10:30] the applying the wear clouds we will get
[01:10:32] only three rows only three customers.
[01:10:35] And now next SQL going to go and execute
[01:10:36] the group by. So it's still going to go
[01:10:38] and group the data by the country. So
[01:10:40] now we have fewer data to be combined.
[01:10:42] So the values will not be summarized
[01:10:44] because we have only one row for each
[01:10:46] country. Now after the data is
[01:10:47] aggregated by the group by then SQL
[01:10:50] going to activate the second filter
[01:10:52] having. So the next step is going to
[01:10:54] execute the having and here SQL going to
[01:10:56] filter the new results based on the
[01:10:58] total scores and still going to check
[01:11:00] one by one. So, USA is meeting the
[01:11:02] condition. UK going to be filtered out
[01:11:04] because it is not higher than 800. And
[01:11:06] this time Germany as well will be
[01:11:08] filtered out because this time it is not
[01:11:10] fulfilling the condition. In the
[01:11:12] previous example without the wear, we
[01:11:14] had more scores for Germany. That's why
[01:11:16] it passed the test. But this time since
[01:11:18] we filtered a lot of customers using the
[01:11:20] wear, Germany will not have enough
[01:11:22] scores pass the second filter. So with
[01:11:24] that in the output we will get only one
[01:11:26] row and that's because we are filtering
[01:11:28] a lot of data. So it is very simple
[01:11:30] where going to be executed before the
[01:11:32] group by before the aggregations having
[01:11:34] going to be executed after the group by
[01:11:36] after the aggregations. So now let's go
[01:11:38] back to scale in order to practice.
[01:11:40] Okay. So now we have very interesting
[01:11:42] task find the average score for each
[01:11:44] country considering only customers with
[01:11:46] a score not equal to zero. So it sounds
[01:11:49] like condition and return only those
[01:11:51] countries with an average score greater
[01:11:53] than 430. So this is again another
[01:11:56] condition. So I know there is a lot of
[01:11:58] things that's going on. Let's do it step
[01:12:00] by step. Usually I start by doing a very
[01:12:02] simple select statement with the columns
[01:12:04] and data that I need. So let's start
[01:12:06] with a simple select. So what do we need
[01:12:08] over here? We need a score. We need a
[01:12:10] country. Again we need a score country.
[01:12:13] So all what we need is two columns. Now
[01:12:15] I'm going to go and select the ID just
[01:12:17] to see the customer ID. Then let's go
[01:12:19] and get the country score from our table
[01:12:22] customers. So let's go and query that.
[01:12:23] So now as you can see I start with the
[01:12:25] basics. Query the data and then build up
[01:12:27] on top of it the second step. Now what
[01:12:29] do we have in the task? We have to find
[01:12:30] the average score for each country. That
[01:12:33] means we have to do some aggregations.
[01:12:35] And here we have two conditions. The
[01:12:36] first condition says we need only the
[01:12:39] customers with a score not equal to
[01:12:40] zero. And the second one we need only
[01:12:43] the countries with an average score
[01:12:44] greater than 430. Now you have to decide
[01:12:47] for each condition whether you're going
[01:12:49] to use the where or having. Now for the
[01:12:52] first one we want to filter based on the
[01:12:54] scores. So that means we want to filter
[01:12:56] before the aggregations. It's not saying
[01:12:59] the average score. It's saying the score
[01:13:00] itself. So that means we can use for
[01:13:03] this a wear condition. Now about the
[01:13:05] second one it says countries with an
[01:13:07] average score greater than 430. That
[01:13:09] means we want to filter the data after
[01:13:12] aggregating the score. So that means for
[01:13:14] this condition we have to use the
[01:13:16] having. Now what I would like to do is
[01:13:18] to implement the first condition. It's
[01:13:20] very simple. We're going to say where
[01:13:22] after the from the score is not equal to
[01:13:25] zero. So let's go and execute it. And
[01:13:27] with that we don't have any customers
[01:13:29] where the scores is not equal to zero.
[01:13:32] So that we have solved this part. But
[01:13:34] now for the second condition first we
[01:13:36] have to do the aggregations. So we're
[01:13:37] going to start with the average score.
[01:13:40] We're going to go over here and say
[01:13:41] average and we're going to call it
[01:13:43] average score. Now we don't want to see
[01:13:45] only the average score. We want to see
[01:13:47] the average score for each country. So
[01:13:49] that means we have to aggregate by the
[01:13:51] country and for that we use the group I
[01:13:54] group by comes always after the wear
[01:13:56] clause. So group by and which column?
[01:13:58] It's going to be the country. So
[01:14:00] country. Now there is like an issue
[01:14:02] here. You cannot execute it like this.
[01:14:04] We have to go and get rid of the ID. We
[01:14:06] don't need it at all. So let's go and
[01:14:08] execute it. So with that we have the
[01:14:10] average score for each country and we
[01:14:12] have solved the first part. So that
[01:14:14] means the first and the second part they
[01:14:17] are completed. Now we're going to talk
[01:14:18] about the last part. The average score
[01:14:20] must be higher than 430. And for that
[01:14:23] we're going to use the having and having
[01:14:25] comes after the group by. Now we need to
[01:14:27] specify the condition. It must be the
[01:14:29] aggregated column. So we're going to
[01:14:31] take the average score from here and put
[01:14:33] it after the having and it should be
[01:14:35] greater than 430. So that's it. With
[01:14:38] that we have the last part as well.
[01:14:40] Let's go and execute it now. And with
[01:14:42] that my friends we have filtered the
[01:14:44] data after the aggregation. So this is
[01:14:47] how I decide between the where and
[01:14:49] having. It is very simple. All right. So
[01:14:51] with that you have learned how to filter
[01:14:53] the aggregated data using the having.
[01:14:55] And now next we're going to go back to
[01:14:56] the top where we can use there the
[01:14:58] keyword distinct exactly after the
[01:15:00] select. So let's go now and learn about
[01:15:03] the
[01:15:06] distinct. Okay. So what is exactly
[01:15:09] distinct? If you use it in SQL, it's
[01:15:11] going to go and remove duplicates in
[01:15:12] your data. Duplicates are like repeated
[01:15:15] values in your data and it's going to
[01:15:16] make sure that each value appears only
[01:15:19] once in the results. So it sounds very
[01:15:21] simple and as well the syntax is easy.
[01:15:23] So as usual we start always with a
[01:15:25] select but directly after the select we
[01:15:28] use the keyword distinct. So there is
[01:15:30] nothing between them and then the normal
[01:15:32] stuff we specify the columns and then
[01:15:34] the from in order to get the data from
[01:15:36] table. Let's say that I would like to
[01:15:37] get a list of unique values of the
[01:15:39] country. So the first thing that SQL
[01:15:41] going to do of course is to get the data
[01:15:42] from the database using the from. And
[01:15:44] now the second step is the select. So
[01:15:47] SQL going to execute it and going to
[01:15:48] select only one column the country. All
[01:15:51] other columns going to be excluded and
[01:15:53] removed from the results. And now SQL
[01:15:55] going to go to the third step. It's
[01:15:56] going to go and apply the distincts on
[01:15:58] the country values. So it acts like a
[01:16:00] filter where it going to make sure each
[01:16:02] value happens only once. So it's going
[01:16:04] to start with the first value Germany.
[01:16:06] Now it's going to look to the results.
[01:16:07] Do we have Germany? Well, we don't have
[01:16:09] anything yet. So that's why it's going
[01:16:11] to include it in the results. Then the
[01:16:12] next value is going to be USA. The same
[01:16:14] thing. We don't have USA in the results.
[01:16:16] So it's going to go and include it. And
[01:16:18] this happens as well for the UK. We
[01:16:20] don't have UK in the final results.
[01:16:22] That's why it's going to go as well
[01:16:23] included. Now comes Germany again. Now
[01:16:26] it's going to say wait, we have it
[01:16:28] already. So it will not go and add it
[01:16:30] again in the output because it must
[01:16:32] appear only once. So we will not have
[01:16:34] Germany twice. And as well for the last
[01:16:36] value the USA we have it already in the
[01:16:38] results that's why it will not appear
[01:16:40] again and with that we have removed the
[01:16:42] duplicates or the repetition inside our
[01:16:45] data. So each value is unique. Now let's
[01:16:47] go back to SQL. Okay that task is very
[01:16:49] simple. It says return unique list of
[01:16:52] all countries. So let's go and do that.
[01:16:54] It's going to be funny. So select and
[01:16:56] now let's get the column country from
[01:16:58] our table customers like this. Now you
[01:17:01] can see we have a list of all countries
[01:17:02] but the task says we need a unique list.
[01:17:05] So that means I cannot have here
[01:17:06] repetitions inside it. And with that
[01:17:08] we're going to use the very nice
[01:17:09] distinct. So if you do it like this
[01:17:13] let's go and execute. You will see there
[01:17:15] will be no duplicates in your results
[01:17:16] and all the values in the result going
[01:17:18] to be unique. So with that we have
[01:17:19] solved the task. It's it's very simple.
[01:17:21] Now there is like one thing about the
[01:17:22] distinct that I see a lot of people
[01:17:24] using it a lot in cases that it's not
[01:17:27] really necessary. So for example, let's
[01:17:29] go and get the ID. Now if you go and
[01:17:31] execute it, you can see here we have a
[01:17:33] list of all ids and there are no
[01:17:35] duplicates. But now if I go and remove
[01:17:37] the distinct and executed, we will get
[01:17:39] the same results because the ids are
[01:17:42] usually unique. So it really makes no
[01:17:44] sense to go and say distinct because as
[01:17:46] you can see the database has to go and
[01:17:48] make sure each value happens only once.
[01:17:50] So there's like extra work for the SQL
[01:17:52] and it is usually an expensive
[01:17:54] operation. So if your data is already
[01:17:56] unique, don't go and apply distincts.
[01:17:58] Only if you see repetitions and
[01:18:00] duplicates and you don't want to see
[01:18:01] that only in this scenario, go and apply
[01:18:03] the distinct. Don't go blindly for each
[01:18:06] query applying distinct just in case
[01:18:08] there is duplicates. This is usually bad
[01:18:10] practices. Okay. So that's all for
[01:18:12] distinct. Okay my friends. So with that
[01:18:14] you have learned how to remove the
[01:18:15] duplicates using the distinct. In the
[01:18:17] next step we're going to talk about
[01:18:19] another keyword that you can use
[01:18:20] together with the select. You can use
[01:18:22] top in order to limit your data. So now
[01:18:24] let's go and understand what this
[01:18:29] means. Okay. So what is exactly top or
[01:18:32] in other databases we call it limit. So
[01:18:34] it is again some kind of filtering in
[01:18:36] SQL. If you use it, it's going to go and
[01:18:38] restrict the number of rows returned in
[01:18:41] the results. So you have a control on
[01:18:43] how many rows you want to see in the
[01:18:45] results. The syntax is very simple as
[01:18:47] well. Directly after the selects you're
[01:18:49] going to use the keyword top and then
[01:18:51] you specify the number of rows you want
[01:18:53] to see in the results. So for example
[01:18:55] three and then only after that you
[01:18:56] specify the columns that you want and
[01:18:58] then from which table. Now let's see how
[01:19:00] going to execute it. So as usual the
[01:19:02] from going to be executed we will get
[01:19:04] our data and then the second step is
[01:19:06] going to go and select the columns. In
[01:19:08] this case all the columns going to stay
[01:19:09] and then after that it's going to
[01:19:11] execute that top. So how it works? It's
[01:19:13] very simple. For each row in database,
[01:19:16] we have a row number. It has nothing to
[01:19:18] do with your data with the ids. For
[01:19:20] example, here like in the current
[01:19:21] result, we have row number 1 2 3 4 5.
[01:19:24] Those numbers are not your actual data.
[01:19:27] It is something technical from the
[01:19:28] database. So it is not equal to the ids.
[01:19:31] For example, the ids is actually your
[01:19:33] content your data. So here we are not
[01:19:35] filtering based on the data based on the
[01:19:37] row numbers. So since here we have
[01:19:39] defined three SQL going to count. Okay.
[01:19:41] row number one 2 three and that's it. So
[01:19:44] it's going to make a cut and all the
[01:19:46] rows after number three they will be
[01:19:48] excluded from the results and you will
[01:19:50] get only the three rows at the results.
[01:19:52] So now as you can see this type of
[01:19:54] filtering is not based on a condition or
[01:19:56] something it's just based on the row
[01:19:58] numbers. So whatever results you have in
[01:20:00] your data it will go and make a cut at
[01:20:03] specific row. So let's go to scale and
[01:20:05] practice that. Okay. So now we have a
[01:20:07] very simple task. It says retrieve only
[01:20:09] three customers. So let's go and do
[01:20:11] that. We're going to go and select star
[01:20:13] from our table customers and execute it.
[01:20:16] Now as you can see in the output we have
[01:20:17] five customers. But the task says we
[01:20:19] want only three. And there is no
[01:20:21] specifications at all about any
[01:20:23] condition. So I don't have to go and
[01:20:25] make a work clause where we write a
[01:20:26] condition based on our data. We just
[01:20:29] want three customers. So we can do that
[01:20:31] very simply by just adding top exactly
[01:20:34] after the select and then specify the
[01:20:36] number of rows you want to see from the
[01:20:38] output. So select top three and then the
[01:20:40] star. Let's go and execute it. And with
[01:20:42] that we are getting three customers.
[01:20:44] That's it. It's very simple. All right.
[01:20:46] Now moving on to another task. It says
[01:20:48] retrieve the top three customers with
[01:20:51] the highest scores. Now of course this
[01:20:53] is like a mix between ordering the data
[01:20:55] and filtering the data. Right? So we
[01:20:57] usually sort the data by the scores from
[01:20:59] the highest to the lowest. But now it's
[01:21:01] like we are doing both together. So
[01:21:03] let's do it again step by step. I will
[01:21:05] just back to the select star from
[01:21:07] customers. Now what we can do we can go
[01:21:09] and sort the data by the score from the
[01:21:11] highest to the lowest using the order by
[01:21:14] so order by score and then descending.
[01:21:16] So let's go and execute it. And now you
[01:21:18] can see the first customer is with the
[01:21:19] highest score and then the second
[01:21:21] highest and so on. Now I think you
[01:21:23] already got it in order to get the top
[01:21:26] three customers with the highest scores.
[01:21:28] What you have to do is to just go over
[01:21:30] here and say top three and execute it.
[01:21:33] And with that you have now a really nice
[01:21:35] analyzis on your data. It's like a
[01:21:37] reports where we are finding the top
[01:21:39] customers with the highest score. So
[01:21:41] this is really amazing and very easy. So
[01:21:43] as you can see mixing the top with the
[01:21:45] sorting the data you can make top end
[01:21:48] analyzes or bottom end analyzers. So
[01:21:50] let's have this task retrieve the lowest
[01:21:52] two customers based on the score. So now
[01:21:54] we want to get the lowest scores in our
[01:21:56] table. And in order to do that is very
[01:21:58] simple. What we're going to do we're
[01:22:00] going to flip that. So we're going to
[01:22:01] sort our data based on the scores
[01:22:02] ascending from the lowest to the
[01:22:04] highest. And since we want only the
[01:22:06] lowest two customers, we're going to
[01:22:07] replace the three with a two and execute
[01:22:09] it. And with that, we're going to get at
[01:22:10] the lowest two customers. It is Peter
[01:22:12] and Maria. They have the lowest scores.
[01:22:14] Again, it's very easy. Okay, this is
[01:22:16] fun. Let's go to the next one. Get the
[01:22:18] two most recent orders. Well, this time
[01:22:21] we are speaking about another table.
[01:22:23] Let's go and select everything from the
[01:22:24] table orders like this. So now, as you
[01:22:28] can see, we have here four orders and we
[01:22:30] want the two most recent orders. So most
[01:22:33] recent means we have to deal with the
[01:22:35] order dates and we can build that by
[01:22:37] sorting the data by the order dates. So
[01:22:40] order by order dates and since we are
[01:22:43] saying the most recent orders so from
[01:22:45] the highest date to the lowest that
[01:22:47] means descending right let's go and
[01:22:49] execute it and as you can see based on
[01:22:51] our data and now we can look to our
[01:22:52] result this is the last order in our
[01:22:54] business based on the order age and this
[01:22:57] one is one of the earliest orders. So
[01:22:59] with that we have sorted the data and
[01:23:01] since we want the two most recent orders
[01:23:03] we go over here and say we go exactly
[01:23:05] after the select and say top two and
[01:23:08] execute and with that we have now the
[01:23:10] last two orders in our business. So as
[01:23:13] you can see combining the top with the
[01:23:14] order by you can do amazing analyszis.
[01:23:17] All right so this is how you limit your
[01:23:19] data using top and with that you have
[01:23:20] learned the basics everything that you
[01:23:22] can learn and with that you have learned
[01:23:23] all the clauses the sections that you
[01:23:26] can use in any query in SQL. Now next
[01:23:29] what we're going to do we're going to
[01:23:30] put everything together in one query in
[01:23:33] order to learn how SQL going to go and
[01:23:35] deal with all those clauses and how SQL
[01:23:37] going to go and execute it. So let's go
[01:23:39] and do
[01:23:42] that. Okay. So now I'm going to show you
[01:23:45] the coding order of a query compared to
[01:23:48] the execution order that happens in the
[01:23:50] database. So the coding order of a query
[01:23:52] starts always with a select and then
[01:23:54] exactly after that you can put a
[01:23:56] distinct and then after the distinct you
[01:23:58] can put a top. So this is the order of
[01:24:00] all those keywords and then you can go
[01:24:02] and select like few columns and after
[01:24:04] you specify the columns separated with a
[01:24:06] comma you tell SQL from which table your
[01:24:08] data come from using the from clause.
[01:24:11] Now after that if you want to filter the
[01:24:13] data before the aggregation you can use
[01:24:14] the where clause and this always comes
[01:24:17] directly after the from. And if you want
[01:24:19] to group the data then you have to do it
[01:24:21] after the wear clause using the group by
[01:24:23] and after the group buys comes the
[01:24:25] having if you want to filter the data.
[01:24:27] And the last thing that you can specify
[01:24:29] in query it is always the order by. So
[01:24:32] this is the order of all those
[01:24:33] components of the query. And if you
[01:24:35] don't follow this order you will get an
[01:24:37] error from the database. Now if you look
[01:24:39] to this query there are a lot of things
[01:24:41] that's going to filter your data. So
[01:24:43] let's check them one by one. The first
[01:24:44] thing that you can do is to filter the
[01:24:46] columns. If you don't want to see all
[01:24:47] the columns, you want to see only
[01:24:49] specific columns, you use the select and
[01:24:51] of course you must use it. So the
[01:24:53] columns that you specify will be shown
[01:24:55] in the results. So it's like filtering
[01:24:56] the columns. Now there is another type
[01:24:58] of filter where you filter out the
[01:25:00] duplicates if you want to see unique
[01:25:02] results and that's using the distinct.
[01:25:04] So this is another type of filter.
[01:25:06] Moving on, we can filter the result
[01:25:08] based on the row numbers. So we can
[01:25:10] limit the result using the top. But this
[01:25:12] type of filter doesn't need any
[01:25:14] conditions. It's purely based on the row
[01:25:17] number in the results. Now moving on, if
[01:25:19] you want to filter your data based on
[01:25:20] conditions based on your data, you can
[01:25:22] filter the rows before the aggregation
[01:25:25] using the wear clause. And the last type
[01:25:28] of filtering, you can filter your rows
[01:25:30] after the aggregation using the having.
[01:25:32] So as you can see, we have like five
[01:25:34] different types and how to filter the
[01:25:36] results in SQL. So now let's see the
[01:25:38] execution order. As we learned the first
[01:25:41] thing that's going to happen is that SQL
[01:25:43] going to execute the from clause. So SQL
[01:25:45] going to go and find your data in the
[01:25:47] database where all the next steps going
[01:25:49] to be paste on this data. Now the next
[01:25:51] step that is going to do is that it's
[01:25:53] going to go and filter the data using
[01:25:55] the wear clause. This has to be happen
[01:25:57] before anything else. So before any
[01:25:59] aggregations and so on we have to make
[01:26:01] scope of the data. So once SQL apply it
[01:26:03] maybe some of the rows going to be
[01:26:05] removed and once the data is filtered
[01:26:07] the third step SQL going to execute the
[01:26:10] group I so going to take the results and
[01:26:12] start combining the similar values in
[01:26:15] one row and start aggregating the data
[01:26:17] based on the aggregate function that you
[01:26:18] have specified. So now after the group
[01:26:20] by after aggregating the data what is
[01:26:23] going to do now it's going to go and
[01:26:24] apply the second type of filter the
[01:26:26] having. So based on the condition the
[01:26:27] SQL going to go and start removing few
[01:26:30] aggregated data away and keep the rest.
[01:26:32] Now moving on to the step number five.
[01:26:34] Finally it's going to go and execute the
[01:26:37] select distinct. So SQL going to go and
[01:26:39] start selecting the columns that we need
[01:26:41] to see in the results and remove the
[01:26:42] other stuff. And once the columns are
[01:26:44] selected SQL going to go and execute the
[01:26:47] order by. So SQL going to start sorting
[01:26:49] the data based on the column that you
[01:26:51] have specified and the mechanism as
[01:26:53] well. So the data will be sorted
[01:26:55] differently. And my friends the last
[01:26:57] step that going to happen in your query
[01:26:59] will be always the top statements. So
[01:27:02] based on the final final results SQL
[01:27:04] going to go and execute the top. So here
[01:27:06] we are saying top two that means we want
[01:27:08] to keep only the first two rows without
[01:27:10] any conditions. So SQL going to count
[01:27:12] okay row number one two and after that
[01:27:14] it's going to make cuts and remove
[01:27:16] anything after that. So this is the last
[01:27:19] filter that's going to happen and as
[01:27:20] well the last step. So now if you sit
[01:27:22] back and look at this the coding order
[01:27:24] is completely different than the
[01:27:26] execution order in the coding we have
[01:27:27] first to specify the select actually the
[01:27:30] select going to be executed just almost
[01:27:32] at the end. So at the step number five
[01:27:34] and once you understand how SQL execute
[01:27:36] your query you can understand how to
[01:27:39] build correct
[01:27:43] queries. So now the first thing that we
[01:27:45] have learned that we can go and have
[01:27:47] like one query right something like this
[01:27:50] select star from customers. Now this is
[01:27:52] one query and in the output we have one
[01:27:54] results but did you know that in SQL we
[01:27:57] can have like multiple queries and
[01:27:59] multiple results in one go. So we can do
[01:28:02] everything together like for example
[01:28:03] let's say I'm selecting as well the data
[01:28:05] from orders. So that means we have two
[01:28:08] queries and now if you go and execute
[01:28:11] what can happens you will get two result
[01:28:13] grids. The first result grid is for the
[01:28:16] first query and the second one is for
[01:28:18] the second query. So with that you can
[01:28:20] do multiple queries in the same window
[01:28:23] and with that the results can be
[01:28:25] splitted into multiple window depend how
[01:28:27] many queries you have and usually in SQL
[01:28:29] you might find that by the end of each
[01:28:31] query there is a semicolon like this. So
[01:28:34] at the end of the first query we have
[01:28:35] semicolon and for the second query we
[01:28:37] have as well at the end another
[01:28:39] semicolon. For the SQL server it is not
[01:28:41] a must but for other databases if you
[01:28:43] have multiple queries in one execution
[01:28:46] you must separate them with a semicolon
[01:28:48] and with that the database can
[01:28:49] understand okay this is the end of the
[01:28:52] first query and this is the end of the
[01:28:54] second query. So you have like
[01:28:56] separations between
[01:29:01] queries. Okay. Now moving on to another
[01:29:03] cool thing in SQL. Now what if we don't
[01:29:06] want to query the data inside our
[01:29:08] tables, we would like to show a static
[01:29:10] value from us from the one that is
[01:29:12] writing the query. And this is very
[01:29:13] practical. If you are like practicing
[01:29:15] and you want to check something using a
[01:29:17] value from you, not from the tables. So
[01:29:20] how we can do that? It is very simple.
[01:29:22] We're going to write select and then now
[01:29:24] after that instead of having a column
[01:29:26] name you can go and add any value like 1
[01:29:29] 2 3. So it is just a number and we do
[01:29:32] not specify after that any table. So we
[01:29:34] leave it like this. Select 1 2 3 and we
[01:29:36] don't need to use the from close. So now
[01:29:39] if you go and execute it you will get 1
[01:29:41] 2 3. So this is a static value. And of
[01:29:43] course you can go and rename the column
[01:29:45] like static number. So execute it again.
[01:29:49] So with that we have a static value. And
[01:29:51] you can go and add anything like string
[01:29:53] as well. So let's say hello as static
[01:29:56] for example string. So let's go and
[01:29:58] execute. Now we have two queries. The
[01:30:00] second one you can see our static value.
[01:30:02] Hello. So in queries we can add values
[01:30:04] from us. Not only selecting data from
[01:30:07] the queries but of course you can go and
[01:30:08] mix stuff. So we can have like in one
[01:30:10] query data from the database and static
[01:30:13] data from us. So let me show you what I
[01:30:15] mean. Let's go over here and say select
[01:30:17] and let's go and get for example the ID
[01:30:20] the first name from the table customers
[01:30:23] like this. So with that we can see we
[01:30:24] are getting data from the database. But
[01:30:27] now I can go and add something from me
[01:30:29] new customer and we can call it customer
[01:30:32] type. So now what is going on here? Two
[01:30:34] columns from the database and one column
[01:30:37] from us. It is the static one. So if you
[01:30:39] go and execute it, you can see for the
[01:30:41] ID and the first name those data comes
[01:30:43] from the database. But for each record
[01:30:45] we are always getting the same static
[01:30:47] value new customer, new customer and so
[01:30:48] on. So this piece of information comes
[01:30:51] from the query. It is not stored inside
[01:30:53] the database and those two informations
[01:30:55] come from the stored data inside the
[01:30:57] database. So this is really cool thing.
[01:30:59] You can add few informations from you
[01:31:02] and you can get the data from the
[01:31:03] database. This is the static
[01:31:08] values. Okay. One more cool thing that I
[01:31:11] want to show you that if you have a
[01:31:12] query like this you are selecting from
[01:31:14] table and filtering the data and now you
[01:31:16] would like not to execute the whole
[01:31:17] thing. You would like to execute only a
[01:31:19] part of this query. So now sometimes as
[01:31:21] you are writing a query, you don't want
[01:31:23] to execute the whole thing. You want to
[01:31:25] execute only part of the query. Like for
[01:31:27] example, I would like to see all the
[01:31:28] customers again in this query without
[01:31:30] this filter. So instead of removing it
[01:31:33] and then query and then again adding it,
[01:31:35] what you can do, you can highlight what
[01:31:37] you want without now the filter and
[01:31:39] execute. So without the database going
[01:31:41] to execute exactly what you highlighted.
[01:31:43] And now as you can see I'm getting all
[01:31:44] the customers without the filter. And if
[01:31:46] you don't highlight anything and
[01:31:48] execute, what's going to happen? It's
[01:31:49] still going to execute the whole thing
[01:31:51] inside the editor. And this is really
[01:31:53] nice if you want to query another table
[01:31:55] quickly in the same editor. Like we want
[01:31:56] to select everything from the orders
[01:31:58] just quickly. So you can highlight only
[01:32:00] this query and execute. And with that
[01:32:02] SQL is ignoring everything else and only
[01:32:05] executing what I'm highlighting. And
[01:32:07] this is really nice. It gives us like
[01:32:08] speed and dynamic. And you're going to
[01:32:10] find me doing that a lot in the course.
[01:32:12] So this is really nice. Okay. My
[01:32:14] friends. So with that we have learned
[01:32:15] the basics about SQL query. the basic
[01:32:18] components of the select statements and
[01:32:20] with that you can talk to our database
[01:32:22] in order to get data. Now in the next
[01:32:24] chapter we're going to learn how to
[01:32:25] define the structure of our database. So
[01:32:27] we're going to learn the data definition
[01:32:29] language DDL. So let's
[01:32:34] go. Okay. So usually if you have like an
[01:32:37] empty database what you want to do is to
[01:32:39] go and define the structure of your
[01:32:41] data. So one of the first things that we
[01:32:43] usually do is we go and create a new
[01:32:45] tables. So here we have a command called
[01:32:47] create and if you use it you can create
[01:32:49] a new object inside the database like
[01:32:52] for example a table. So once you execute
[01:32:54] it you're going to get brand new table
[01:32:56] and usually the table going to be empty
[01:32:57] without any data. So it is very simple.
[01:33:00] This is what the create command does.
[01:33:02] And now let's go to SQL in order to
[01:33:04] create a new table. So my friends we
[01:33:06] have the following task. Create a new
[01:33:07] table called persons with columns ID
[01:33:11] person name birth date and phone. Okay.
[01:33:13] So this time we will not start by select
[01:33:16] we will start with the command create
[01:33:18] table. So we are telling SQL to create a
[01:33:20] table and after that we have to define
[01:33:22] the name of the table. So in this task
[01:33:24] we have to call it persons. Now we have
[01:33:26] to go and open two parenthesis like this
[01:33:28] and in between we have to define the
[01:33:31] columns. So what do we need? First we
[01:33:33] need an ID. So this is the first column
[01:33:35] name. And next we have to define which
[01:33:37] data type for this column. It's going to
[01:33:38] be an int. So it is a number does not
[01:33:41] contain any characters. And now next we
[01:33:43] can define some constraints and we
[01:33:44] cannot have a person without an ID. So
[01:33:46] it should not be in null. So not null.
[01:33:48] This is the first column. So we have
[01:33:50] defined the name of the column, the data
[01:33:52] type and the constraint. Okay. So let's
[01:33:54] go to the second column and here we're
[01:33:55] going to have a comma and the next one
[01:33:57] name going to be person name. So this is
[01:34:00] the column name and the person name we
[01:34:02] can have. And now the data type for this
[01:34:04] column it going to be a varchar because
[01:34:06] the person name contains characters. So
[01:34:08] vchar. And now we have to define the
[01:34:10] length. So I'm going to go with 50
[01:34:12] characters. And now I would say this is
[01:34:14] a must. So each person should has a
[01:34:16] name. So we're going to say not null as
[01:34:18] well. So that we have the name, the type
[01:34:21] and the constraint. Now let's move to
[01:34:22] the third column. It's going to be birth
[01:34:25] date. Now which type of informations we
[01:34:27] have inside the birth date? So it's
[01:34:29] going to be a date, not a number, not
[01:34:31] characters. So we're going to go with
[01:34:32] the data date. And now about the
[01:34:34] constraint well depends. I would say in
[01:34:37] our application it is an optional
[01:34:39] because this is very personal
[01:34:40] information and maybe some persons will
[01:34:42] not provide their birth dates. So this
[01:34:44] is an optional and I will not say it is
[01:34:46] not null. So nulls are allowed. Now
[01:34:48] let's move on to the next one. It's
[01:34:49] going to be the phone. So now what is
[01:34:51] the data type of a phone? Well we have
[01:34:53] some types numbers we have characters
[01:34:56] special characters. So we could have
[01:34:57] anything. So that's why I'm going to go
[01:34:59] with the farchar. And here you can
[01:35:00] specify the length that you think it's
[01:35:02] okay. I'm going to go with 15. Now of
[01:35:04] course depend on the system that you are
[01:35:06] building. I would say the phones are
[01:35:07] very important in order to validate
[01:35:09] whether this is a real person. So we're
[01:35:12] going to say not null. So we are not
[01:35:15] allowing nulls in this field. Perfect.
[01:35:17] So with that we have covered all the
[01:35:18] columns that are required. We have
[01:35:20] defined the data types and as well the
[01:35:22] constraints. Now the last thing in each
[01:35:24] database table we should has a primary
[01:35:27] key in order to make sure this table has
[01:35:29] an integrity and maybe as well
[01:35:31] connectable to other tables. So now what
[01:35:33] we're going to do, we're going to go and
[01:35:34] add the primary key constraint, comma,
[01:35:36] for the last column. And then we're
[01:35:38] going to say constraint. Now we have to
[01:35:40] give a primary key name. This is only
[01:35:42] going to be visible for the database. So
[01:35:44] I'm going to call it PK for primary key.
[01:35:46] And here persons and then after that
[01:35:48] we're going to say primary key. And
[01:35:51] between two parentheses, we're going to
[01:35:53] go and pick which one is the primary
[01:35:54] key. And of course, it's going to be the
[01:35:56] ID. So we're going to go over here and
[01:35:58] say ID. So again, we are saying there is
[01:36:00] a new constraint. This is the name of
[01:36:02] it. It's only internal for the database.
[01:36:04] And then we are saying this one is a
[01:36:06] primary key on the field ID. So that's
[01:36:08] it with that. We have defined a primary
[01:36:10] key for our table. Let's go and execute
[01:36:13] it. So as you can see it is successful.
[01:36:14] Let's go and check our database for our
[01:36:16] new table. So if you don't see it
[01:36:18] already, you have to right click on the
[01:36:20] database and then go and refresh. So
[01:36:22] let's go to tables and now we have a
[01:36:24] brand new table called persons. So with
[01:36:26] that we have created our new table. Now
[01:36:28] of course for the DDL commands you will
[01:36:30] not get results or data. All what you're
[01:36:33] getting is a message from the database
[01:36:35] and the message says here the command
[01:36:37] completed successfully and then we have
[01:36:39] a date when this is completed. So that
[01:36:41] means the DDL command will never return
[01:36:43] data. It is changing the structure of
[01:36:45] your database. It's not about retrieving
[01:36:48] any data and so on. So this command did
[01:36:50] change something in our database and in
[01:36:52] this scenario it created a new table and
[01:36:54] that's why we call this data definition
[01:36:57] language DDL because we are defining the
[01:37:00] database. Now of course if you go and
[01:37:01] say select star from our new table
[01:37:04] persons. So let's go highlight it and
[01:37:07] then execute it. You will see we are
[01:37:09] getting of course the columns. So the
[01:37:11] ID, the person name, birth date, the
[01:37:13] phone but we don't have any rows that
[01:37:16] means our table is empty. Now what is
[01:37:18] very important to that you go and save
[01:37:20] those informations in an SQL script
[01:37:23] because maybe later you have to redefine
[01:37:25] this table but let's say that you have
[01:37:27] created different queries and you have
[01:37:28] lost the script and now I would like to
[01:37:30] see again the create statements for this
[01:37:33] table well there is trick for that if
[01:37:35] you go to the left side you see the
[01:37:37] persons right here right click on it and
[01:37:39] then you have here script table as and
[01:37:41] now we have here different options that
[01:37:43] you can run on the table and the first
[01:37:45] one says create two Then let's go to new
[01:37:48] query
[01:37:49] editor. So now what happened? The
[01:37:51] database did read the metadata
[01:37:53] informations about the person and
[01:37:55] created your DDL query with many extra
[01:37:58] stuff that we haven't done. But this is
[01:37:59] the template that the database uses. So
[01:38:01] now we can see a lot of stuff. But what
[01:38:03] is interesting is this create table. So
[01:38:06] we can see create table the schema DBU
[01:38:09] the default one then the persons and
[01:38:11] then we have our columns the data type
[01:38:13] and as well the constraints. So with
[01:38:15] that you got back your DDL statements
[01:38:17] and many other stuff about the table
[01:38:19] which is now not interesting. But now
[01:38:21] what I really need is to see the create
[01:38:23] statements about this table. So this is
[01:38:25] how you can get back your DL command.
[01:38:27] But of course what I recommend you is
[01:38:29] always put your code inside a get
[01:38:31] repository and always keep it up to
[01:38:33] date. So that always you can check your
[01:38:35] work and extend
[01:38:39] it. Okay. So now what else you can do
[01:38:41] with the structure of your database? If
[01:38:43] you have already a table, what you can
[01:38:45] do, you can go and edit and change the
[01:38:48] definition of the table. So for example,
[01:38:50] let's say I would like to add a new
[01:38:51] column. In order to do that, we can use
[01:38:54] the command alter. Alter means you want
[01:38:56] to edit the definition of your table and
[01:38:59] you want to change it like adding new
[01:39:01] column or maybe changing the data type
[01:39:03] and anything in the definition of the
[01:39:05] table. So the alter command, you can use
[01:39:07] it in order to change the definition of
[01:39:08] your table. And now let's go back to
[01:39:10] scale and try to change something. All
[01:39:12] right. Now the task says add a new
[01:39:14] column called email to the person's
[01:39:16] table. So it is very simple what you can
[01:39:18] do. We can use the alter table command.
[01:39:20] So we are not creating new table. We
[01:39:23] want to edit already existing table. So
[01:39:25] which table we want to modify it's going
[01:39:27] to be the persons. So we are telling SQL
[01:39:29] we want to change something in the table
[01:39:31] persons. And of course we have to tell
[01:39:32] SQL what we want to change. Are we
[01:39:34] removing a column? Are we adding column?
[01:39:36] In this scenario we want to add new
[01:39:38] column. So let's go and add the email
[01:39:41] information. So this is the column name
[01:39:43] and as you are creating a table you have
[01:39:45] to define column name the data type and
[01:39:47] the constraint. So now for the emails
[01:39:49] we're going to have like characters,
[01:39:50] numbers, special characters. So we're
[01:39:52] going to go with the varchar and about
[01:39:54] the length it's going to be let's say 50
[01:39:56] and I'm going to say each person has to
[01:39:58] has an email. So it's going to be not
[01:40:00] null. So with that we are adding
[01:40:02] completely a new column. So that's it.
[01:40:04] Let's go and execute it. Now again this
[01:40:07] is not a query. This is a DDL command
[01:40:09] and in the output we will not get data.
[01:40:11] We will get a message whether everything
[01:40:13] went correctly. So it says command
[01:40:15] completed successfully and the time when
[01:40:17] this is completed. Now we can go and do
[01:40:19] a simple query just to have a check to
[01:40:21] the table. So and now you can see we
[01:40:23] have our columns and at the end we have
[01:40:25] a new column called emails. This is very
[01:40:27] important. If you are adding new column
[01:40:29] it's going to be always at the end of
[01:40:31] the table. But now you might say you
[01:40:33] know what I would like to have the email
[01:40:34] like something in the middle maybe after
[01:40:36] the person name. Well, in order to do
[01:40:38] that, you have completely to delete and
[01:40:40] drop the table and create it from the
[01:40:42] scratch using create command which is
[01:40:45] might be bad if you have data inside the
[01:40:47] table. So if you are fine by adding your
[01:40:49] new column at the end, you can use the
[01:40:50] alter table. But if you say I would like
[01:40:52] it in the middle, then sadly you have to
[01:40:54] go and drop everything and start from
[01:40:56] the scratch. Okay. So now let's have
[01:40:58] another task and it says remove the
[01:40:59] column phone from the person's table. So
[01:41:02] now we're going to do exactly the
[01:41:03] opposite. We're going to go remove it
[01:41:04] completely with its data from the table.
[01:41:07] So we're going to still saying alter
[01:41:09] table persons. We are saying we want to
[01:41:11] edit the definition of the table
[01:41:12] persons. And now instead of adding we
[01:41:15] will be dropping a column. And then
[01:41:18] after that we have to specify as well
[01:41:19] the column name. It's going to be the
[01:41:21] phone. But we don't have to mention
[01:41:23] again the data type and the constraint.
[01:41:26] And that's because the database already
[01:41:27] knows those informations. So we need
[01:41:29] those informations if we are creating
[01:41:31] something new. That's why we can get rid
[01:41:33] of that. We just need the column name
[01:41:35] and the database is going to do the
[01:41:36] rest. So let's go and do that. Now you
[01:41:39] can see successful. And now let's go and
[01:41:41] check our table. And now as you can see
[01:41:43] we have the ID, person name, birth date,
[01:41:45] email, and we don't have the column
[01:41:47] phone. Be careful. If you are deleting
[01:41:48] column, you will be losing as well all
[01:41:50] the data inside this column. So as you
[01:41:52] can see, this is very simple. This is
[01:41:54] how we can edit the definition of our
[01:41:56] table by adding and removing columns.
[01:42:03] Okay, now moving on to the last one in
[01:42:05] this group of commands. So now so far
[01:42:07] what we have done, we have created
[01:42:08] something new in the database. We have
[01:42:10] changed the definition of something
[01:42:12] inside our database. And now the last
[01:42:14] one, you can go and drop something from
[01:42:17] the database. Let's say we have another
[01:42:18] table and we don't need it anymore. So
[01:42:21] we can go and use the drop command in
[01:42:23] order to remove the table completely
[01:42:26] from the database. And this means as
[01:42:28] well removing everything the table and
[01:42:30] the data inside it. So now let's go to
[01:42:32] SQL and let's drop something from our
[01:42:34] database. Okay. So now our task says
[01:42:36] delete the table persons from the
[01:42:38] database. This is the simplest form of
[01:42:41] command in SQL but yet the most risky
[01:42:43] one. So what we need? We have to delete
[01:42:46] and drop the whole table persons. We
[01:42:48] don't need it anymore. We're going to
[01:42:49] say drop table and then all what we have
[01:42:52] to do is to give the name of the table
[01:42:54] persons. So three words. You don't have
[01:42:57] to specify anything. Just destroy the
[01:42:59] table persons. Let's go and execute it.
[01:43:01] It is successful. So as you can see it
[01:43:03] is very simple. Now on the left side to
[01:43:05] your database go refresh and go to the
[01:43:07] tables and you will not see the table
[01:43:09] persons. So the drop command it is very
[01:43:11] simple but yet very risky. So if you
[01:43:14] compare now create table with a drop
[01:43:16] table you can see destroying things is
[01:43:18] way easier than building it. Those are
[01:43:20] the commands create alter drop. those
[01:43:23] commands we use in order to define the
[01:43:26] structure of our database the DDL
[01:43:28] commands that was very simple all right
[01:43:30] so that's all about the data definition
[01:43:32] language DDL and with that you have
[01:43:34] learned how to define new stuff in your
[01:43:36] database now moving on to the next one
[01:43:38] we're going to learn about the data
[01:43:39] manipulation language and here we're
[01:43:41] going to learn how to manipulate our
[01:43:42] data inside the database let's
[01:43:48] go all right so now what we're going to
[01:43:50] do we're going to go and modify and
[01:43:51] manipulate your data inside the
[01:43:54] database. So now sometimes what happens
[01:43:56] you have a table inside your database
[01:43:58] and the table is empty. You don't have
[01:44:00] any rows any data inside the table. Now
[01:44:02] in order to add your data to the table
[01:44:04] what you can do you can use the command
[01:44:06] insert. So insert going to go and add
[01:44:09] new rows to your table and of course not
[01:44:12] always the table must be empty to add
[01:44:13] your data. You can add new rows to
[01:44:16] already existing data and SQL going to
[01:44:18] go and append it at the end of the
[01:44:20] table. Now my friends in order to insert
[01:44:22] new data to the target table there are
[01:44:24] two methods. The first and the classical
[01:44:26] way in order to insert new data we can
[01:44:29] use the insert command and manually
[01:44:31] specifying the values that should be
[01:44:33] inserted to the table. So you're going
[01:44:35] to start specifying in the script the
[01:44:37] values and then they're going to be
[01:44:38] inserted as a new rows to the target
[01:44:40] table. So in this process you are
[01:44:42] manually inserting new values to the
[01:44:44] table using like an SQL scripts. So now
[01:44:47] we're going to focus on this scenario on
[01:44:49] how to insert data. All right. Now let's
[01:44:50] check quickly the syntax of the insert
[01:44:52] command. It start with the keyword
[01:44:54] insert into and after that we have to
[01:44:56] specify the table name. So where we want
[01:44:58] to insert and then we make a list of all
[01:45:01] columns that we want to insert. And then
[01:45:03] we specify list of columns where we're
[01:45:05] going to insert values into them. And
[01:45:07] after that we say values. And finally
[01:45:09] we're going to go now and specify the
[01:45:11] data that should be inserted to the
[01:45:13] table. and we make it as well as a list
[01:45:15] like we have done for the columns. Now
[01:45:17] in the insert statements specifying
[01:45:19] those columns it is totally optional. So
[01:45:22] if you don't specify the columns of the
[01:45:23] table then SQL going to expect you to
[01:45:26] insert values into each column because
[01:45:29] sometimes of course we don't want to
[01:45:30] insert value for each column. You can
[01:45:32] skip few columns of course but if you
[01:45:34] want to insert a value for each column
[01:45:37] either you go and specify them as a list
[01:45:39] or you can skip it. Now for the insert
[01:45:41] statements there is very important rule.
[01:45:43] The number of columns and values must
[01:45:46] match. So if you specify here three
[01:45:48] columns then you must insert as well
[01:45:50] exactly three values. So this must be
[01:45:53] matching. And one last thing about the
[01:45:55] syntax you can insert multiple values in
[01:45:57] one go. So for each row you can specify
[01:46:00] a list of values that must be inserted.
[01:46:02] So that's all about the syntax. Let's go
[01:46:04] back to SQL in order to practice insert
[01:46:07] command. Okay. So now let's go and
[01:46:08] insert a new customers. So it's very
[01:46:10] simple. It start with insert into. So we
[01:46:13] are saying we want to insert data into.
[01:46:15] So we have to go and specify the table
[01:46:17] name customers. Now after that we have
[01:46:19] to specify list of columns where we want
[01:46:22] to insert data into it. And what we can
[01:46:24] do we can go and check which columns do
[01:46:25] we have inside our table. So we can see
[01:46:27] we have ID, first name, country, score.
[01:46:29] And we can go and make a list of that.
[01:46:31] So we can say ID, first name, country
[01:46:35] and score. So we just have a list of all
[01:46:38] columns inside our table customers. Now
[01:46:41] what we need? We need the values. So
[01:46:43] which data should be inserted. So we can
[01:46:45] go and open two parenthesis. And now we
[01:46:47] have to specify an ID. We know the last
[01:46:49] customer was five. So we're going to go
[01:46:51] with the customer six. Now we have to
[01:46:53] give the name of the customer. Let's go
[01:46:55] for Anna. And then a country. Let's go
[01:46:57] for USA. And this customer has no
[01:47:00] scores. So what we can do? We can say
[01:47:02] null. So we don't know the score of this
[01:47:05] customer. nulls means nothing we don't
[01:47:07] know. So with that you can go and insert
[01:47:09] one row. But now let's say that I would
[01:47:11] like to go and insert like a second row
[01:47:13] one more customer. What we can do we can
[01:47:15] separate this with a comma and then we
[01:47:18] can go and repeat the whole thing again.
[01:47:20] So the ID is seven. The next one let's
[01:47:22] call this customer Sam and we don't know
[01:47:25] the country of this customer. So we're
[01:47:27] going to say it's null. But the score we
[01:47:28] know it already. It is 100. So as you
[01:47:30] can see we are adding a value for each
[01:47:32] of those columns. And if you don't know
[01:47:34] the answer then make it null. if the
[01:47:36] database allows it to be null. Some
[01:47:38] columns they are not allowed to be null
[01:47:40] like the primary key. So if you go and
[01:47:41] say over here null the database will not
[01:47:43] allow it. Well actually we can go and
[01:47:45] test it. Let's execute. And you can see
[01:47:47] you cannot insert the value null into
[01:47:50] the column ID. So this is not allowed.
[01:47:52] Going to have a seven. But for the other
[01:47:53] columns it is allowed. You can go and
[01:47:55] check the definition of the table. Now
[01:47:57] we go and execute. Now the output of the
[01:48:00] modifications command is going to always
[01:48:02] indicate what happens to the data. So it
[01:48:04] says two rows affected. Affected might
[01:48:07] be inserted, updated, deleted. So you're
[01:48:09] going to get a general statement from
[01:48:11] the database. But you are getting how
[01:48:13] many record is affected. So we got two
[01:48:15] because we have inserted two records. So
[01:48:17] now as you can see it's not like the
[01:48:18] query. We are not getting any data in
[01:48:21] the output. We are just getting a
[01:48:22] message. So this is a big difference
[01:48:24] between querying the data using the
[01:48:26] selects and modifying the data using
[01:48:28] inserts. We are doing now direct
[01:48:30] modifications to the data inside our
[01:48:32] database. Of course, if you want to see
[01:48:34] the data in the customers, what we can
[01:48:36] do, we can go and query the data, right?
[01:48:39] So, let's go and do that. Select star
[01:48:40] from customers. I would like to see the
[01:48:42] whole table. So, market and execute it.
[01:48:45] Now, you can see we have seven
[01:48:47] customers. So, we just manipulated our
[01:48:49] data. We have here Anna and Sam. This is
[01:48:51] how you can insert data to the database.
[01:48:53] Now, there's like few rules you have to
[01:48:55] be careful as you are inserting new data
[01:48:57] to your tables. You have to pay
[01:48:58] attention that the order of the columns
[01:49:00] that you have defined. insert is
[01:49:03] matching the values that you are
[01:49:04] inserting over here. Let's have an
[01:49:06] example. I'm going to go and remove this
[01:49:08] over here and let's say that we are
[01:49:10] inserting a new one number eight and now
[01:49:12] in the first name instead of the name of
[01:49:14] the customers we have inserted the
[01:49:16] country like USA and in the country we
[01:49:19] have inserted the name is just mistake
[01:49:21] and we are all human right? So let's
[01:49:23] have a name like this max. Now if you go
[01:49:24] and execute it the database can accept
[01:49:27] it because it is really hard for the
[01:49:28] database to understand that you have
[01:49:30] made here an error. Both of them are var
[01:49:32] and the database doesn't care about the
[01:49:34] content of the data as long as you are
[01:49:36] following the rules of the data type. So
[01:49:37] now if you go and select the data from
[01:49:39] the customers you can see now we have a
[01:49:41] customer called USA from the country
[01:49:44] max. So the SQL going to do it blindly
[01:49:46] like you insert the data as long as you
[01:49:48] are following the data type rules and
[01:49:50] the constraints. So for example, if you
[01:49:52] made this error over here and you say
[01:49:54] the id is max and let's say the first
[01:49:57] name is let's say nine and you execute
[01:50:00] it here the database is smart enough to
[01:50:02] say you know what there is something
[01:50:04] wrong the ID should not be strange so
[01:50:06] the database going to reject your
[01:50:07] inserts be careful of the order of your
[01:50:09] columns now let's go and query again our
[01:50:11] table now if you are in the insert
[01:50:13] commands defining all the columns
[01:50:15] exactly like the table so as you can see
[01:50:17] we have here complete match ID first
[01:50:19] name country score we have all the
[01:50:21] columns and as well the correct order
[01:50:22] there is like lazy way you can go and
[01:50:24] remove the whole thing over here and
[01:50:26] with that the database can understand
[01:50:28] okay we are inserting values to all of
[01:50:30] the columns so going to understand you
[01:50:32] are inserting something to each columns
[01:50:34] in the correct direction so let's go and
[01:50:36] do that correctly nine and here let's
[01:50:39] say we
[01:50:41] have from Germany so if you go and
[01:50:44] execute it it will be working even
[01:50:46] though we didn't define the columns and
[01:50:48] that's because the values that we are
[01:50:50] inserting as exactly the same number of
[01:50:52] columns of the table and following as
[01:50:54] well the rules. Now moving on to the
[01:50:56] next one, you can go and add only two
[01:50:58] columns in the definition. If you know
[01:51:00] already always the country and the score
[01:51:02] is null. We know only two informations,
[01:51:05] the ID and the name. Then you don't have
[01:51:07] always to go and say null null null and
[01:51:08] so on. We can go and skip that. Okay. So
[01:51:11] now let me show you what I mean. We're
[01:51:12] going to go after the table name and
[01:51:14] we're going to define only two columns,
[01:51:15] the ID and the first name. So that means
[01:51:18] we are telling SQL we want to insert
[01:51:20] only two columns. And now you have to be
[01:51:22] careful. If you define here two columns
[01:51:24] then the values should be as well two
[01:51:26] columns. So we're going to remove the
[01:51:28] country and the score. And we can go and
[01:51:30] add only two informations. So 10. And we
[01:51:33] can go and add here for example Sara. So
[01:51:35] if you go and execute it, it will be
[01:51:37] working. And now what is skill is doing
[01:51:39] with the other two columns. It's going
[01:51:40] to be nulls. So let's go and select
[01:51:42] again from our table. You can see here
[01:51:44] Sara has null in the country and as well
[01:51:47] in the score because we didn't define
[01:51:48] those informations. But be careful, you
[01:51:50] cannot here skip a column that is not
[01:51:52] allowed to be null. So you have always
[01:51:54] have in your list all the columns that
[01:51:57] are not null. So for example, I cannot
[01:51:58] go and insert only the first name. I
[01:52:00] will get an error because the database
[01:52:02] can try to insert a null in the ID and
[01:52:04] this is not allowed. So you can skip
[01:52:06] only nullable
[01:52:11] columns. All right, my friends. So that
[01:52:13] was the first method on how to insert
[01:52:15] data to your target table as you saw by
[01:52:18] typing manually the values inside an
[01:52:20] insert command using values. And now
[01:52:23] let's move to another methods. We're
[01:52:25] going to insert data but this time not
[01:52:27] manually. We're going to insert data
[01:52:29] using another table. So imagine we have
[01:52:31] the following scenario. We have an
[01:52:32] already existing table with data and
[01:52:35] this going to be the source table, the
[01:52:36] source of your data and we have another
[01:52:38] table. This table is empty and we want
[01:52:41] to insert a new data to this target
[01:52:43] table. Now what we can do, we can take
[01:52:45] the data from the source table and
[01:52:47] insert it into the target table without
[01:52:49] manually writing the script for the
[01:52:52] values. So we are moving the data from
[01:52:54] one table to another. Now in order to do
[01:52:56] that we need to do two steps. The first
[01:52:58] step we have to write an SQL query using
[01:53:01] select from and so on in order to select
[01:53:04] the data that we need from the source
[01:53:06] table. And once you do that you will get
[01:53:09] a results. So this is like you are doing
[01:53:11] a normal query. You right select and you
[01:53:13] will get an answer with the results. And
[01:53:15] now what we can do in the next step we
[01:53:17] can take this results and use an insert
[01:53:20] command in order to insert this results
[01:53:23] into the target table. And with that we
[01:53:26] have moved the data from the source
[01:53:27] table to the target table. So first
[01:53:29] write the query on the source table. And
[01:53:32] the second step use an insert to move
[01:53:34] this results to the target table. So
[01:53:36] let's go back to the scale in order to
[01:53:37] do that. So now we have the following
[01:53:39] task and it says insert data from the
[01:53:41] table customers into the table persons.
[01:53:43] So that means the source table is the
[01:53:46] customers and the target table is
[01:53:48] persons. Now how I usually do it that I
[01:53:51] keep my eye on the target table to
[01:53:53] understand the structure of this table
[01:53:55] and I start writing the query from the
[01:53:58] source table. If you go to the left
[01:54:00] side, we can see okay, we have here an
[01:54:01] ID. We have here person name, birth date
[01:54:04] and phone. And you can see only the
[01:54:05] birth date except nulls and the rest we
[01:54:08] have always to provide informations. So
[01:54:10] with that I have now understanding about
[01:54:12] the table persons. Now next I'm going to
[01:54:14] go and start writing the query from the
[01:54:16] source. So we start like this. Select
[01:54:18] star from our table customers just to
[01:54:21] have an overview of our table. Now the
[01:54:23] next step we're going to go and design a
[01:54:25] perfect result from this query that is
[01:54:28] matching the target table. So in the
[01:54:30] output we need ID and we have it from
[01:54:32] the customer from the original table.
[01:54:34] We're going to go and select ID. Okay.
[01:54:36] So now next we need a person name and
[01:54:39] here we have from the original table
[01:54:40] something called first name. So this is
[01:54:42] a perfect match. So we're going to go
[01:54:44] and select this table as a second
[01:54:46] column. So we have covered the first
[01:54:48] two. Then the third one is going to be
[01:54:49] the birth date. Well, my friends, we
[01:54:51] don't have birth dates, but the database
[01:54:53] can accept it as a null. So, I'm going
[01:54:55] to go and write a null because I don't
[01:54:57] have such information from the source
[01:54:59] table. And now the next one going to be
[01:55:00] the phone as well. We don't have phone
[01:55:02] informations. But we cannot have it as a
[01:55:04] null because it says here not null. So,
[01:55:06] what we're going to do, we're going to
[01:55:07] go and add a static value, a default
[01:55:10] value. So, we're going to have two
[01:55:12] single quotes and in between we're going
[01:55:13] to say unknown. Since it is var, it can
[01:55:16] accept this word. So, now let's go and
[01:55:19] just query. So we have the ID, we have
[01:55:21] the first name, the birth date is empty,
[01:55:24] and the phones is unknown. Now you might
[01:55:26] say, but the column name is not matching
[01:55:29] with the column name of the persons.
[01:55:31] Well, the database does not care about
[01:55:32] that. As long as the result of the data
[01:55:35] is matching the table, it can go and
[01:55:37] insert it. So the database will never
[01:55:40] compare the column names together. But
[01:55:41] if you like and go and add here like the
[01:55:43] aliases exactly like the target table it
[01:55:46] will not hurt but it has no effect on
[01:55:49] the results. All right. Okay. So now we
[01:55:50] have like query select and we have a
[01:55:52] results but this is not an insert. So
[01:55:55] how we going to insert the result of
[01:55:57] this into the table persons. Well for
[01:55:59] that we need the insert into command. So
[01:56:03] insert into and now we have to specify
[01:56:05] the target table going to be the
[01:56:07] persons. And of course you can go and
[01:56:09] list all the column names but if you
[01:56:11] have like exact match you can skip it
[01:56:13] but for me I would like always to add it
[01:56:15] just to make sure that we don't have any
[01:56:18] issue. So the ID, person name, birth
[01:56:20] date and the
[01:56:22] phone. So that's it. Let's go and
[01:56:26] execute. So it is working now. We can
[01:56:28] see 10 rows affected. Well that means 10
[01:56:31] rows are inserted from the table
[01:56:33] customers into the target persons. And
[01:56:36] now what we can do we can go and query
[01:56:38] the table persons just to check that
[01:56:40] everything is working perfectly. Select
[01:56:42] star from persons and let's go and
[01:56:45] execute. And with that you can see our
[01:56:47] 10 persons that we have added from the
[01:56:49] customers. So with that we have moved
[01:56:51] the data from one table and inserted
[01:56:54] into another table. And as you can see
[01:56:55] it was very simple. First you have to
[01:56:57] write a query from the source table in
[01:57:00] order to collect the data that you need.
[01:57:02] and then you go and insert it into the
[01:57:05] target table. So this is really nice and
[01:57:07] easy and this is another way on how to
[01:57:09] insert data into your
[01:57:14] database. Okay, so with that we have
[01:57:16] learned how to insert data to our
[01:57:18] tables. Now let's say that I don't have
[01:57:20] something new. I don't have any rows to
[01:57:22] be added to my table but I have an
[01:57:24] update. I would like to go and change
[01:57:26] the content of the already existing
[01:57:29] rows. So what you can do? We can use the
[01:57:31] command updates in order to change the
[01:57:34] content of already existing rows. So
[01:57:37] again my friends insert going to go and
[01:57:39] insert completely new rows but update
[01:57:42] going to go and change the data of
[01:57:44] already existing row. Now let's have a
[01:57:46] look quickly to the syntax of the
[01:57:48] updates. It start with the keyword
[01:57:49] updates and then we have to specify the
[01:57:51] table name and after that we're going to
[01:57:53] use sit in order to specify what are the
[01:57:56] new values for the columns. So you have
[01:57:58] to write down for each column that you
[01:57:59] want to update a new value and you
[01:58:01] separate the columns of course using a
[01:58:03] comma. Now after that we have to specify
[01:58:05] as well a wear condition. So it's like
[01:58:07] the queries you say where and then you
[01:58:09] write a condition and if you don't do
[01:58:11] that and you don't use the wear clause
[01:58:14] what going to happen you will be end up
[01:58:16] updating all the rows inside your table.
[01:58:19] So that's why we need always the wear
[01:58:20] clause. All right. So that's all about
[01:58:22] the syntax. Let's go back to SQL in
[01:58:24] order to update our data. Okay. So let's
[01:58:26] have the following task and it says
[01:58:28] change the score of customer 6 to zero.
[01:58:30] So that means we have to go and modify
[01:58:32] the data of the customer ID equal to
[01:58:34] six. So now first I would like to go and
[01:58:36] have a look to our data. So select star
[01:58:38] from customers and now the task is
[01:58:41] targeting this customer over here and we
[01:58:43] would like to replace the null to zero.
[01:58:46] Now how we can go and update this
[01:58:48] information inside the table? We can use
[01:58:50] the update command. So what we going to
[01:58:52] do? We're going to start writing update
[01:58:54] and after that we have to specify the
[01:58:56] table name. So what we are updating? We
[01:58:58] are updating the customers and then
[01:59:00] we're going to tell the database to set
[01:59:02] the value of the score to a zero. So we
[01:59:05] would like to update and change the
[01:59:06] value from null to a zero. And now here
[01:59:09] comes something very risky. Don't
[01:59:10] execute this query yet. If you do that,
[01:59:12] what's going to happen? The database
[01:59:14] going to go to the table customers and
[01:59:16] replace all those values of all
[01:59:18] customers to zero. So it's going to go
[01:59:20] and update the whole table and this is
[01:59:22] of course very risky. That's why in the
[01:59:24] update command we have to give a wear
[01:59:26] condition a filter in order to target
[01:59:29] only specific row or the rows that you
[01:59:31] want really to modify. In this case we
[01:59:33] want to change only one row. So what we
[01:59:36] have to do is to go and specify the work
[01:59:38] condition like we have done in the
[01:59:40] select query. Nothing new, right? So
[01:59:42] we're going to say where the customer ID
[01:59:45] is equal to six. And with that SQL will
[01:59:47] not go and update everything. First it's
[01:59:49] going to filter the data and then
[01:59:51] updates. And now before I execute just
[01:59:53] to make sure I go and check which data
[01:59:55] going to be affected. So it's very
[01:59:56] simple you go and select star from table
[01:59:59] customers and then I go and take the
[02:00:01] exact where and put it in my query and
[02:00:04] then I select the whole thing and
[02:00:06] execute. And now if this query gives me
[02:00:08] the data that should be modified then
[02:00:11] I'm doing the update command correctly.
[02:00:13] And in this case we are targeting only
[02:00:15] one customer. This is the customer
[02:00:17] number six. And with that I feel really
[02:00:19] confident with my update. So what we can
[02:00:21] do since I'm going to use this later I'm
[02:00:23] going to put the whole thing in a
[02:00:24] comment and if I execute now only the
[02:00:26] update going to be executed. So let's go
[02:00:29] and do that. Now very important to check
[02:00:31] the message you can see one row is
[02:00:33] affected which is really good because if
[02:00:35] I see here 10 rows is affected that
[02:00:38] means everything is updated. Now let's
[02:00:40] go and check the data. I'm going to go
[02:00:43] and remove the wear here and check the
[02:00:44] whole table. Now you can see we still
[02:00:46] have the old scores only Anna has now
[02:00:49] score zero instead of null. So this is
[02:00:51] how I usually update the data. You have
[02:00:53] to do it very carefully. Now let's move
[02:00:55] to another task. It's going to say
[02:00:57] change the score of the customer number
[02:00:59] 10 to zero and update the country to UK.
[02:01:03] So now this time we are targeting the
[02:01:04] user number 10. As you can see she
[02:01:06] doesn't have the country and score. And
[02:01:08] the task wants us to change the score to
[02:01:11] a zero and the country to UK. So now how
[02:01:13] we going to do it? We're going to use
[02:01:14] the exact same command but with
[02:01:16] different condition. So the ID this
[02:01:18] times is equal to 10 and the score is to
[02:01:21] zero. But now we have to change as well
[02:01:23] the country. Now if you want to do
[02:01:25] multiple updates, you're going to have
[02:01:27] here a comma after the score and the new
[02:01:29] line and let's say country equal and
[02:01:32] then we're going to add UK. So select
[02:01:34] the whole thing and let's go and
[02:01:36] execute. So again it is affecting only
[02:01:39] one row. This is really good. And if you
[02:01:41] go and check the table search for Sara,
[02:01:43] you can see in one update we have
[02:01:45] updated two columns the country and as
[02:01:48] well the score. So with that we have
[02:01:50] solved the task. It's very simple. Now
[02:01:51] moving on to the second task. It says
[02:01:53] update all customers with a null score
[02:01:56] by setting their score to a zero. So
[02:01:59] this time we are not speaking about one
[02:02:01] specific customer. We are talking about
[02:02:03] updating the data for a subset of
[02:02:05] customers. So now imagine you have like
[02:02:07] hundreds of customers and you are making
[02:02:09] one update command for each customer.
[02:02:11] It's going to be really wasting of time.
[02:02:12] Now instead of that we can specify a
[02:02:15] condition that targets multiple
[02:02:17] customers and we're going to do the
[02:02:19] update for those customers in one go. So
[02:02:21] now let's see how we're going to do it.
[02:02:22] We are talking only about replacing the
[02:02:24] nulls with a zero. So we don't need the
[02:02:27] country. So set score equal to zero. But
[02:02:30] now we will not be specific for the ids.
[02:02:32] Now we have to make a new condition.
[02:02:34] It's going to say like this where score
[02:02:36] is null. Now of course in the course we
[02:02:38] have a full dedicated chapter about the
[02:02:40] nulls and here all what we are doing is
[02:02:42] we are searching for scores that is
[02:02:44] equal to null. But we cannot write an
[02:02:46] equal we have to write it like this is
[02:02:48] null. Of course before we update
[02:02:50] anything we have to go and test it in a
[02:02:52] query. So select star from customers
[02:02:55] where score is null. Let's go and
[02:02:56] execute. Now as you can see we have two
[02:02:59] customers where the score is null. So
[02:03:01] that means this condition is targeting a
[02:03:04] subset of customers and we're going to
[02:03:06] do now the updates for multiple rows for
[02:03:08] this subset. So that means we can run
[02:03:11] this query. Let's go and execute it. Now
[02:03:13] you can see two rows are affected. So
[02:03:16] that means multiple rows got affected
[02:03:19] got updated. So now if you go and query
[02:03:21] our table customers you can see we don't
[02:03:24] have any nulls inside the scores and we
[02:03:26] have replaced all the nulls with a zero.
[02:03:28] And of course you can do the same thing.
[02:03:30] you can go and make an update command in
[02:03:33] order to replace all the nulls in the
[02:03:35] country to maybe something unknown or
[02:03:37] any default value that you want. So this
[02:03:39] is how you can update multiple rows in
[02:03:41] one
[02:03:45] go. All right my friends. So with that
[02:03:47] we have learned how to insert new rows
[02:03:50] to our tables and as well how to update
[02:03:52] the content of already existing row. Now
[02:03:54] the last thing or command that we can do
[02:03:57] to the data inside the table that we can
[02:03:59] go and remove rows from our table and we
[02:04:02] can do that using the command delete. So
[02:04:04] if you use delete SQL going to go and
[02:04:07] start removing already existing rows
[02:04:09] inside your table. All right. Now for
[02:04:11] the syntax of the delete it's going to
[02:04:13] be very simple. We're going to say
[02:04:14] delete from and then we're going to
[02:04:16] write the table name. And here comes
[02:04:18] something very important. We have to add
[02:04:20] a wear condition. And it's like the
[02:04:22] update. If you don't do that, if you
[02:04:23] don't include where condition, what
[02:04:25] going to happen? You will end up
[02:04:27] deleting all the rows inside the table.
[02:04:29] So the syntax is very simple. Let's go
[02:04:31] back to scale in order to delete some
[02:04:33] data. Okay. So now we have the following
[02:04:35] task. Delete all customers with an ID
[02:04:38] greater than five. So now we have to go
[02:04:40] and delete all the customers that we
[02:04:41] recently added. So how we going to do
[02:04:43] it? It's very simple. We're going to say
[02:04:44] delete from. So that means I want to
[02:04:47] delete something from a table. And we
[02:04:49] have to specify the table name. It's
[02:04:51] going to be the customers. So the syntax
[02:04:52] is very simple. Now my friends, this is
[02:04:54] more risky than updates because if you
[02:04:57] execute it like this, don't do that yet.
[02:04:59] Wait, what's going to happen? All the
[02:05:01] data of the customers going to be
[02:05:02] deleted. So you will get an empty table
[02:05:05] and we will not do that. So now we're
[02:05:07] going to do exactly like the update
[02:05:08] command. We're going to specify the work
[02:05:10] clause. So it says the ID should be
[02:05:12] greater than five. So that means ID
[02:05:15] higher than five. So with that we are
[02:05:17] defining a subset of the data that
[02:05:19] should be deleted, not everything. And
[02:05:21] if we check in the updates, we have here
[02:05:22] to do a double check before deleting
[02:05:24] anything. So again what we do, we select
[02:05:27] star from table customers and we're
[02:05:30] going to go and copy the work condition
[02:05:32] in order to test what going to be
[02:05:34] deleted. So it's going to be all the
[02:05:35] customers that is higher than five. And
[02:05:37] with that I'm making sure that my delete
[02:05:40] command is correct which is from what I
[02:05:41] see here is correct. So those five
[02:05:44] customers should be deleted. So now
[02:05:45] let's go and delete those customers. And
[02:05:48] now very important to read the message.
[02:05:50] It says five rows affected. So that
[02:05:52] means five customers got deleted. And
[02:05:54] this is better than 10 of course. So
[02:05:56] let's go and check what customers left.
[02:05:59] So we have 1 2 3 4 5. Those are the
[02:06:01] original customers. And everything else
[02:06:03] got deleted. And with that we have
[02:06:05] solved the task. And this is how we can
[02:06:06] delete data from tables. Be very
[02:06:09] careful. Always test before doing the
[02:06:11] delete command. Okay. So now we have the
[02:06:13] following task. And it says delete all
[02:06:15] data from table persons. So that means
[02:06:18] we have to go and drop everything from
[02:06:20] the table persons. But we don't want to
[02:06:22] delete the table. We just want to delete
[02:06:24] the data inside the table now. So now
[02:06:26] what we're going to do, we're going to
[02:06:27] write delete from. And now we have to
[02:06:29] specify the table persons. And if you
[02:06:31] execute it, what's going to happen? SQL
[02:06:33] going to go and drop all the data in the
[02:06:35] persons. But in SQL, we have more
[02:06:37] interesting command. If you want to
[02:06:39] delete everything from the table
[02:06:40] persons, we have that truncate.
[02:06:42] Truncate. It is exactly like delete from
[02:06:44] persons. It's going to go and make the
[02:06:46] whole table empty. But why I like to use
[02:06:48] truncate because it is way faster than
[02:06:51] deletes. If you have large tables, the
[02:06:52] delete command going to be really slow
[02:06:54] because with the delete there is like a
[02:06:56] lot of things happening behind the
[02:06:57] scenes. There is like logs and
[02:06:59] protocols. But if you are using trunk,
[02:07:01] the database going to skip all those
[02:07:03] extra stuff and it's going to be very
[02:07:05] fast. So if you want to delete all the
[02:07:07] data from table, you can do it like this
[02:07:09] if it's like small table. But what I
[02:07:10] usually do, I go and write truncate and
[02:07:14] then table. we're going to get the same
[02:07:16] effect and with that I'm saying reset
[02:07:18] everything make the table empty. So
[02:07:20] let's go and execute it and now with
[02:07:21] that you will not get the number of
[02:07:23] deleted rows and that's why it's
[02:07:25] truncate it's way faster. It is not
[02:07:27] protocoling anything it's not logging
[02:07:29] anything it just go and delete all the
[02:07:31] data without any extra steps. So this is
[02:07:34] how we can delete all the data from a
[02:07:36] table but the table still exists. Okay
[02:07:38] my friends, so with that you have
[02:07:39] learned the basics on how to manipulate
[02:07:41] your data inside the database the data
[02:07:43] manipulation language DML and with that
[02:07:45] I can tell you we have covered the
[02:07:47] basics of SQL. So with that we have
[02:07:50] covered the beginner level. Now in the
[02:07:52] next chapters we will be in the
[02:07:53] intermediate level and the first thing
[02:07:55] that you're going to learn in the
[02:07:56] intermediate level you will learn how to
[02:07:58] filter your data and we're going to
[02:07:59] cover many operators that you can use
[02:08:01] inside the workclass. So let's go.
[02:08:08] All right. So now let's have an overview
[02:08:09] about all different operators in SQL. So
[02:08:12] the first group of operators we have the
[02:08:13] comparison operators. They are the
[02:08:15] easiest one where all what we have to do
[02:08:17] is to compare two values and we have
[02:08:19] like six different variants and how to
[02:08:20] do that. Now to the next one we have the
[02:08:23] logical operators. We use it in order to
[02:08:25] combine multiple operators. And moving
[02:08:27] on to the next one we have the range
[02:08:29] operator. Here we have only one, the
[02:08:31] between. We're going to use it in order
[02:08:32] to check whether a value falls within a
[02:08:35] specific range. Now moving on to the
[02:08:37] next one, we have the membership
[02:08:38] operator. And here we have two things.
[02:08:40] We have the in operator or not in. Here
[02:08:42] all what you have to do is to check
[02:08:44] whether a value is in a list or not. And
[02:08:46] the last category that we have is the
[02:08:48] search operator. And here as well we
[02:08:50] have only one operator that like we use
[02:08:52] it in order to search for a specific
[02:08:54] thing in a text. So my friends, we're
[02:08:56] going to go through all those operators
[02:08:58] one by one. Okay. So now let's go and
[02:09:00] deep dive into the first category the
[02:09:02] comparison operators and we're going to
[02:09:04] cover all those stuff. So what is
[02:09:05] exactly comparison
[02:09:10] operator? Okay. So what is exactly
[02:09:12] comparison operators? It is very simple.
[02:09:14] We want to compare two things and there
[02:09:16] is a lot of things that we can compare
[02:09:18] in SQL. But the formula for that going
[02:09:20] to be always like this. So we have the
[02:09:22] first expression and then operator and
[02:09:24] then we have another expression and this
[02:09:27] going to form something called
[02:09:28] condition. So here we have a lot of
[02:09:30] variance. We can compare one column to
[02:09:32] another column. So for example, you can
[02:09:34] go and compare the first name with the
[02:09:36] last name. So both of the expressions
[02:09:38] are columns here. Another scenario, you
[02:09:41] want to compare a column with a value, a
[02:09:43] static value. Like for example, you say
[02:09:46] the first name must be equal to a value
[02:09:49] like John. So now we are comparing a
[02:09:51] column with a value. It's not anymore
[02:09:53] two columns. Now we have another
[02:09:55] scenario where we want to apply a
[02:09:57] function to a column and then compare
[02:09:59] the results to maybe a value. So for
[02:10:01] example, we apply the upper function to
[02:10:03] the first name and then this must be
[02:10:06] equal to a value like John with all the
[02:10:08] letters in the uppercase. And one more
[02:10:10] thing that you can compare you can write
[02:10:12] an expression in one of the sides like
[02:10:15] for example you can say if we multiply
[02:10:17] price with the quantity it must be equal
[02:10:20] to 1,000 for example. So here we have an
[02:10:23] expression. We have multiple columns
[02:10:25] included in one sides and the output of
[02:10:27] this expression must be equal to 1,000.
[02:10:29] And now the last one is going to be a
[02:10:31] little bit more advanced and we're going
[02:10:32] to cover that of course in other
[02:10:33] chapter. We can include a whole query
[02:10:36] the complete query to one of the sides
[02:10:38] and we call this a subquery. So in one
[02:10:40] of the sides you're going to write a
[02:10:42] whole query select from where whatever
[02:10:45] you want and you go and compare the
[02:10:47] result of this query to for example a
[02:10:49] value or a column. So as you can see in
[02:10:51] a scale we can compare a lot of things
[02:10:53] together. Either comparing the columns
[02:10:55] together or a column with a value or we
[02:10:57] use a function or an expression or even
[02:11:00] a whole query. So this is how we build
[02:11:02] conditions in SQL. Okay my friends. So
[02:11:05] let's see how the conditions works in
[02:11:06] SQL. So we have our data the name the
[02:11:09] country the score and let's say that we
[02:11:11] have built a condition where it says the
[02:11:13] country must be equal to the USA. So
[02:11:15] this is very simple comparison operator
[02:11:17] and this is the condition that we are
[02:11:20] using inside the work clause. So once
[02:11:22] you apply this filter to your data what
[02:11:24] going to happen? SQL going to go row by
[02:11:26] row evaluating whether it is meeting the
[02:11:28] condition. If it's not fulfilling the
[02:11:30] condition then SQL going to remove it
[02:11:32] from the results. But if it is
[02:11:33] fulfilling the condition it's going to
[02:11:35] keep it. So now we are comparing the
[02:11:37] values of column together with a static
[02:11:39] value the USA. So we're going to compare
[02:11:41] whatever value we get from the country
[02:11:43] together with the USA. So now let's see
[02:11:45] how is going to apply this filter to our
[02:11:47] data for the first customer Maria. Now
[02:11:50] you can see the value inside the country
[02:11:51] is Germany. So Isql now going to go and
[02:11:54] compare Germany to USA since it is not
[02:11:57] equal. Then is going to understand okay
[02:11:59] Maria is not fulfilling the condition.
[02:12:02] So it is false and is going to go and
[02:12:04] remove this customer from the results.
[02:12:06] So she is not fulfilling the condition.
[02:12:08] Moving on to the next one to Joan. Now S
[02:12:10] is going to take the value inside the
[02:12:12] country the USA it is equal to USA. So
[02:12:15] that means John is fulfilling the
[02:12:17] condition and Isl going to be happy
[02:12:18] about it. So it is true and this means
[02:12:21] is going to keep Joan in the final
[02:12:23] results. Now moving on to George the
[02:12:25] value is UK not equal to USA. He is not
[02:12:27] fulfilling the condition. Is going to go
[02:12:29] and remove him from the final result.
[02:12:31] Same thing for Martin. Germany is not
[02:12:34] equal to USA. Is going to remove this
[02:12:36] customer as well. And to the last one
[02:12:38] bit better you can see the value is USA.
[02:12:40] So USA equal USA. The condition is
[02:12:42] fulfilled. SQL is happy about it and
[02:12:44] going to leave the customer in the
[02:12:45] output. So now if you go and apply this
[02:12:47] condition using the comparison operator
[02:12:50] to your data only two customers going to
[02:12:52] be left in the output. This is exactly
[02:12:54] how the conditions and the comparison
[02:12:56] operators works in SQL. Okay. So now
[02:12:58] let's start with the first operator.
[02:13:00] It's very simple. We have the equal.
[02:13:02] It's going to checks if the two values
[02:13:04] are equal. That's very simple. Let's
[02:13:06] have an example. Okay. So now we have
[02:13:07] this task. It says retrieve all
[02:13:09] customers from Germany. So this is very
[02:13:11] basic. We're going to go and select and
[02:13:12] we're going to select all the columns
[02:13:14] since we don't have any specifications
[02:13:15] from the table customers. And if you go
[02:13:17] and execute it, you will get all the
[02:13:19] customers. But we don't need that only
[02:13:21] the customers that comes from Germany.
[02:13:23] So we have to go and apply a condition
[02:13:25] using the wear clause country equal to
[02:13:28] the value Germany. So make sure you are
[02:13:31] writing it exactly like in the database
[02:13:33] otherwise it will not work. So let's go
[02:13:35] and execute and with that we are getting
[02:13:37] only the customers from Germany. So it
[02:13:39] is very simple and this is why we use
[02:13:41] the equal operator. Okay. So now moving
[02:13:43] on to the next one again very simple. If
[02:13:45] you want to check if two values are not
[02:13:48] equal we can use the not equal operator.
[02:13:50] So let's have an example. Okay. So now
[02:13:52] we let's have the opposite task. It says
[02:13:54] retrieve all customers who are not from
[02:13:57] Germany. So this is very simple. We are
[02:13:59] saying here who are not they are not
[02:14:02] equal to Germany. So we can use the not
[02:14:05] equal operator in order to get these
[02:14:07] customers. So with that as you can see
[02:14:09] after executing we are getting all the
[02:14:12] customers country is not equal to
[02:14:14] Germany and there's like another way on
[02:14:16] how to do the not equal doing it like
[02:14:18] this we'll get the same results. All
[02:14:19] right my friends moving on to the next
[02:14:21] one. We can check if a value is greater
[02:14:24] than another value. So we use the
[02:14:26] greater operator. Let's have an example.
[02:14:28] Okay. So now the next task it says
[02:14:30] retrieve all customers with a score
[02:14:33] greater than 500. Now we want to filter
[02:14:36] the data based on the score. So we're
[02:14:38] going to say where score and now the
[02:14:41] task says greater than 500. We're going
[02:14:44] to use the operator greater than 500.
[02:14:46] It's very simple. So with that we will
[02:14:48] get only the customers where the score
[02:14:51] is higher than 500. So for example Maria
[02:14:54] it's not fulfilling the condition. The
[02:14:56] same thing for the Peter and as well for
[02:14:58] Martin it must be greater than 500. So
[02:15:01] if you go executed you will get only
[02:15:03] those two customers because they are
[02:15:05] greater than 500. Okay, moving on to the
[02:15:09] next one. This time we're going to check
[02:15:10] if a value is greater than or equal to
[02:15:13] another value. So it is like mix between
[02:15:15] the greater than and the equal. If one
[02:15:17] of them is fulfilled then the value
[02:15:19] going to meet the condition. So let's
[02:15:21] have an example for that. Now, if the
[02:15:22] task says retrieve all customers with a
[02:15:25] score of 500 or more, this time we're
[02:15:28] going to go and include the customers
[02:15:30] where their score is equal as well to
[02:15:32] 500 or higher. So, we're going to have a
[02:15:34] similar condition based on the score and
[02:15:36] the 500's value, but this time we're
[02:15:38] going to say greater or equal to 500.
[02:15:41] So, if you go now and execute it, this
[02:15:44] time we're going to see the customer
[02:15:45] Martin with the score of 500. So, in
[02:15:48] this scenario, we're going to use
[02:15:49] greater or equal. All right. Right. So
[02:15:51] now let's keep moving. The next one is
[02:15:53] as well very simple. We're going to
[02:15:54] check this time if a value is less than
[02:15:57] another value. So we're going to use the
[02:15:59] less operator. Let's have an example.
[02:16:01] Now moving on to another simple task.
[02:16:03] Retrieve all customers with a score less
[02:16:06] than 500. So this time we want all the
[02:16:08] customers with a lower score. And we're
[02:16:10] going to use exactly the opposite. It's
[02:16:12] going to be the score is less than 500.
[02:16:14] And again here it is not equal, right?
[02:16:16] So if you go and execute, you will get
[02:16:18] all the customers with a low scores. he
[02:16:21] will not get to Martin because Martin is
[02:16:23] equal to 500. So with that we have
[02:16:25] solved the task. We have all the
[02:16:26] customers with the score less than 500.
[02:16:29] Okay my friends, now moving on to the
[02:16:31] last one. I think you already got it. So
[02:16:33] we're going to check whether a value is
[02:16:35] less than or equal to another value. So
[02:16:38] you can go and combine the less operator
[02:16:40] together with the equal and if one of
[02:16:42] them is fulfilled then the value going
[02:16:44] to meet the condition. So let's have an
[02:16:46] example for that. This time we are
[02:16:48] retrieving all customers with a score of
[02:16:50] 500 or less. So the query going to be
[02:16:52] very similar but we are saying it is
[02:16:55] less or equal to 500. So we are
[02:16:57] including the value in our condition.
[02:17:00] And with that as you can see we still
[02:17:01] have our two customers where we have the
[02:17:04] score less than 500 but we have now as
[02:17:06] well Martin with a score of 500. Okay my
[02:17:09] friends. So with that we have covered
[02:17:11] the first group the comparison
[02:17:13] operators. Now we're going to move on to
[02:17:14] the next group. We're going to speak
[02:17:16] about the logical operators and here we
[02:17:17] have three and or not. So let's start
[02:17:20] with the first one. What is exactly and
[02:17:26] operator. Okay. So now what is the
[02:17:28] definition of the and it says all
[02:17:31] conditions must be true. So all the
[02:17:33] conditions that you have in the wear
[02:17:34] clause must be true in order to keep the
[02:17:37] row in the results. So let's understand
[02:17:39] what this means. things going to get
[02:17:40] more complicated where you can have not
[02:17:43] only one condition but you might have
[02:17:45] multiple conditions in your query. So
[02:17:47] here we're going to add a second
[02:17:48] condition where we're going to say not
[02:17:50] only the country must be equal to USA
[02:17:53] but also the score must be higher than
[02:17:55] 500. So now you have two conditions and
[02:17:58] you have to put them in the wear clause.
[02:18:00] Now you have to combine those conditions
[02:18:02] using the logical operator and here we
[02:18:04] have two options two operators the and
[02:18:06] operator and the or operator. In this
[02:18:09] scenario, if you say and then SQL is
[02:18:12] very restrictive. Both of the conditions
[02:18:14] must be true in order to keep the row in
[02:18:17] the results. So now let's see how this
[02:18:18] going to work. Now for the first row and
[02:18:20] for the first condition you can see the
[02:18:22] country is Germany and it is not
[02:18:24] fulfilling the first condition. So this
[02:18:27] going to be false. And as well if you
[02:18:28] check the second condition for the first
[02:18:30] row you can see the score is 350. So
[02:18:32] that means this customer is as well not
[02:18:34] fulfilling even the second condition. So
[02:18:37] both of the conditions is false and it's
[02:18:39] going to go I remove this customer from
[02:18:41] the results. Now to the next one John
[02:18:43] you can see John is fulfilling the first
[02:18:45] condition because the country is equal
[02:18:46] to USA and as well fulfilling the second
[02:18:49] condition. His score is 900 and this is
[02:18:52] higher than 500. So now SQL going to be
[02:18:54] very happy about it because both of them
[02:18:56] is true and this is the only way in
[02:18:58] order to keep the row in the output
[02:19:00] because we are using the operator and so
[02:19:03] John going to stay in the output. Now
[02:19:04] moving on to George. He is not
[02:19:06] fulfilling the first condition. But now
[02:19:08] the second condition is fulfilled. His
[02:19:10] score is 750 and this is higher than
[02:19:12] 500. So now it's like 50/50 right. In
[02:19:15] one side it's false but the other side
[02:19:17] is true. But this is not enough for the
[02:19:19] ant operator. Both of them should be
[02:19:21] true in order to keep the result in the
[02:19:22] output. That's why SQL going to remove
[02:19:24] this row. Now moving on to Martin. He is
[02:19:26] not fulfilling both of the conditions.
[02:19:28] So SQL going to go I remove it from the
[02:19:30] results. And now for the last one. Peter
[02:19:33] is fulfilling the first condition. the
[02:19:35] country is equal to USA but the second
[02:19:37] condition is sadly not fulfilled so we
[02:19:39] have the score zero not higher than 500
[02:19:42] again we have the same scenario it's
[02:19:43] 50/50 and this is not enough for the ant
[02:19:46] operator that's why SQL going to go I
[02:19:48] remove it so as you can see if you use
[02:19:50] an and operator a lot of rows going to
[02:19:52] be removed if one of the condition is
[02:19:55] not met so the ant operator is very
[02:19:57] restrictive both of the conditions must
[02:19:59] be fulfilled to keep the row in the
[02:20:01] results so this is exactly how the and
[02:20:04] operator works. Okay. So now we have the
[02:20:05] following task. Retrieve all customers
[02:20:07] who are from USA and have a score
[02:20:10] greater than 500. So here we are like
[02:20:12] combining multiple conditions and let's
[02:20:15] go and do it step by step. So the first
[02:20:16] thing that we have to go and select the
[02:20:18] data from the correct table. So select
[02:20:20] star from customers and with that we are
[02:20:22] getting all the customers from the
[02:20:24] table. Now the first condition we need
[02:20:26] the customers that come from USA. So we
[02:20:28] need only those two customers and in
[02:20:31] order to do that as we learned we can go
[02:20:32] and use the wear clause and the
[02:20:34] condition going to be country equal to
[02:20:37] USA. So if you go and execute we will
[02:20:39] get those two customers. Nothing is new.
[02:20:42] We have used the compression operator
[02:20:44] equal. But we are not done yet. We have
[02:20:46] another condition from those two
[02:20:48] customers. We need only the customers
[02:20:50] where their score is higher than 500. So
[02:20:53] now by looking to those two customers
[02:20:54] you can see we see that the bitter here
[02:20:57] does not have a score higher than 500
[02:20:59] and we don't want to see that in the
[02:21:01] results. So now what we have to do we
[02:21:02] have to go and write a condition for
[02:21:04] this one over here. So this is based
[02:21:06] this time on the scores not on the
[02:21:08] country. So the score should be greater
[02:21:11] than 500. Now as you can see we have the
[02:21:14] first condition for the first one here
[02:21:16] and the second condition for the second
[02:21:18] requirement. Now the question how to
[02:21:20] connect those two conditions. So here we
[02:21:22] have two options and or and to be honest
[02:21:25] this is very simple the task says it
[02:21:27] customer should fulfill both of the
[02:21:29] conditions should be from USA and as
[02:21:31] well at the same time greater than 500.
[02:21:34] So it is very simple real and so with
[02:21:37] that we have connected both of those
[02:21:39] conditions and if you go and query it
[02:21:41] you will get only one customer that is
[02:21:43] fulfilling our conditions. So from all
[02:21:46] customers we have only one customer
[02:21:48] that's fulfilled this condition that
[02:21:50] comes from USA and at the same time the
[02:21:52] score of this customer is higher than
[02:21:55] 500. So this is how we use the ant
[02:21:57] operator in order to connect two
[02:21:59] conditions. Okay my friends. So that's
[02:22:01] all for the ant operator. Let's speak
[02:22:03] now about the or
[02:22:08] operator. All right. Now the or operator
[02:22:11] it says at least one condition must be
[02:22:13] true. So it is less restrictive than the
[02:22:16] and it is enough to have one condition
[02:22:18] true in order to keep the row in the
[02:22:20] results. Let's understand exactly what
[02:22:22] this means. Okay. So now we have the
[02:22:23] same scenario. We have two conditions
[02:22:25] and in SQL you have to connect them
[02:22:27] either using the and operator or the or
[02:22:30] operator. In this scenario we're going
[02:22:32] to talk about the or operator. And as we
[02:22:34] said at least one of the conditions must
[02:22:36] be fulfilled in order to leave the
[02:22:38] record in the results. So let's see
[02:22:40] what's going to happen here. Now the
[02:22:41] first customer Maria she is not
[02:22:44] fulfilling the first condition and as
[02:22:46] well the second condition. So both of
[02:22:48] them is false and this is the only
[02:22:50] scenario where SQL going to remove the
[02:22:52] record from the results because it is
[02:22:54] not fulfilling the minimum at least one
[02:22:57] of them should be true. Both of them is
[02:22:58] false then SQL going to go and remove
[02:23:00] this row. Now moving on to the next one
[02:23:02] to John. John is from USA and has higher
[02:23:05] score than 500. Both of the conditions
[02:23:08] is green. So both of them is true and
[02:23:10] this is more than enough to keep the row
[02:23:12] in the output. That's why we will see
[02:23:14] John in the outputs. Now moving on to
[02:23:16] the third one, George. George is not
[02:23:18] fulfilling the first condition because
[02:23:20] UK is not equal to USA. But John this
[02:23:22] time is fulfilling the second condition.
[02:23:25] So we have here true and since we have
[02:23:27] at least one true, this is good enough
[02:23:29] to keep the record in the output. So you
[02:23:31] will see George in the results. Now
[02:23:33] moving on to Martin. He is not
[02:23:35] fulfilling the first condition as well
[02:23:37] not fulfilling the second condition.
[02:23:39] Both of them is false and this is not
[02:23:41] enough to keep the result in the output.
[02:23:43] So that's why it's still going to go and
[02:23:45] remove it. Now moving on to the last
[02:23:46] one. Peter he is fulfilling the first
[02:23:48] condition but not the second condition
[02:23:50] but still everything is fine because he
[02:23:52] is fulfilling at least one condition. So
[02:23:55] we have the minimum and it's still going
[02:23:56] to leave it in the output. So as you can
[02:23:58] see the or operator is not restrictive
[02:24:00] like the and operator. It's enough to
[02:24:02] have one true in order to keep the data
[02:24:04] in the output. And this is exactly how
[02:24:06] the or operator works. Now let's see the
[02:24:08] second task. Retrieve all customers who
[02:24:11] are either from USA or have a score
[02:24:14] greater than 500. So it is a very
[02:24:16] similar task. We have two conditions. So
[02:24:18] we need the customers that are either
[02:24:20] from USA. So it is based on this country
[02:24:23] equal to USA. And the second condition
[02:24:25] is the score is greater than 500. But
[02:24:28] this time we are very relaxed. either
[02:24:30] this condition is fulfilled or the
[02:24:32] second one. So instead of having and we
[02:24:35] will be using the operator or. So it is
[02:24:37] enough to fulfill one of those
[02:24:38] conditions. And if you go and execute
[02:24:40] now as you can see we are getting more
[02:24:42] results because it is easier to fulfill
[02:24:44] the conditions. So we can see those
[02:24:46] three customers either fulfilling the
[02:24:48] first condition or the second one. All
[02:24:51] right my friends. So that's all for the
[02:24:52] or operator and we're going to move to
[02:24:54] the last one in this group the not. So
[02:24:56] what do we mean with the not operator?
[02:25:02] Okay. So now what is this operator not?
[02:25:04] It is a reverse operator. It's going to
[02:25:07] go and exclude the matching values. So
[02:25:10] what this exactly means? Let's have a
[02:25:11] very simple example. All right. So now
[02:25:13] the net operator is not like the or and
[02:25:16] the ands. This operator will not go and
[02:25:18] combine two conditions. So you can use
[02:25:21] it with only one condition. And let's
[02:25:22] say that our current condition is like
[02:25:24] this. The country must be equal to USA.
[02:25:26] So this is like a comparison operator.
[02:25:28] And if you apply it to your data, as we
[02:25:30] learned, it's going to leave only two
[02:25:31] customers, John and Peter, because they
[02:25:34] fulfill the conditions and all other
[02:25:35] customers will be removed because they
[02:25:38] don't fulfill the condition. So nothing
[02:25:39] crazy so far. But now if you go and
[02:25:41] apply the not operator to the condition,
[02:25:44] what going to happen? You're going to
[02:25:45] reverse the whole truth. So you are
[02:25:47] saying if this condition is fulfilled,
[02:25:50] it must be removed from the final
[02:25:52] results. So it is switching everything.
[02:25:54] We want to see the customers that is not
[02:25:57] fulfilling the condition. So now let's
[02:25:59] see what can happen if you apply the not
[02:26:01] operator together with the condition. We
[02:26:02] can see that the first customer is not
[02:26:04] fulfilling the condition which is great
[02:26:06] thing. This is exactly what we want. We
[02:26:08] want the customer that is not fulfilling
[02:26:10] the condition. That's why going to be
[02:26:12] happy about it and SQL going to make it
[02:26:14] true and leave it in the output. So
[02:26:17] Maria is fulfilling the whole thing. She
[02:26:19] is not meeting the condition. So SQL
[02:26:22] going to leave it at the output. Now for
[02:26:23] the next one. So this customer is
[02:26:25] fulfilling the condition and that is not
[02:26:27] a good thing. So SQL going to go and
[02:26:29] this time remove John from the results
[02:26:31] because he is fulfilling the condition.
[02:26:33] And moving on to George. So George is
[02:26:36] not fulfilling the condition which is
[02:26:38] amazing. So that's why SQL going to keep
[02:26:40] this time George in the output. The same
[02:26:42] thing for Martin. Martin is not
[02:26:44] fulfilling the condition. So Isl going
[02:26:46] to keep the customer and better he is
[02:26:48] fulfilling the condition. So SQL going
[02:26:50] to go and remove this customer from the
[02:26:52] output. So as you can see we have
[02:26:54] reversed everything right. The not
[02:26:56] operator going to make the true false
[02:26:58] and the false true. Okay. So this is how
[02:27:00] it works. Now let's go back to SQL in
[02:27:02] order to practice. Okay. The next task
[02:27:05] it says retrieve all customers with a
[02:27:07] score not less than 500. So this sounds
[02:27:11] really funny. As usual we're going to go
[02:27:12] and select star from customers. And now
[02:27:16] we have to filter the data based on this
[02:27:18] condition. So the score is not less than
[02:27:21] 500. Well, you can go and say well the
[02:27:23] score is higher, greater or equal to
[02:27:27] 500, right? And with that it is not less
[02:27:30] than 500. So if you go and execute it,
[02:27:32] we just solve the task, right? We get
[02:27:34] all the customers that are not less than
[02:27:36] 500. Or you can go and use the not
[02:27:38] operator to make things more funnier. So
[02:27:41] you go over here and say it is not and
[02:27:44] then you switch it. So you make like
[02:27:46] this. So the score is less than 500. But
[02:27:49] as we use here not then we twisted
[02:27:51] everything. So we are saying the score
[02:27:54] is not less than 500. And if you execute
[02:27:56] it you will get the exact same results.
[02:27:59] Convert the truth. If you remove it and
[02:28:01] execute you will get everything that is
[02:28:03] less than 500. But if you put the nut
[02:28:06] you will convert the whole logic. So if
[02:28:08] you go and execute you are not getting
[02:28:10] the scores that are less than 500. So
[02:28:12] this is really nice. This is how you use
[02:28:14] the nut operator. Okay my friends. So
[02:28:16] with that we have covered everything
[02:28:17] about the logical operators. Now we're
[02:28:19] going to move to the third group. We're
[02:28:21] going to talk about the range operator.
[02:28:23] And here we have only one the between.
[02:28:25] So what is exactly between
[02:28:30] operator? Okay. So what is between? It's
[02:28:32] going to go and check if a value falls
[02:28:35] within a specific range. So you have a
[02:28:37] range and you are checking whether your
[02:28:39] value is in the range or outside the
[02:28:41] range. So let's understand exactly what
[02:28:43] this means. Okay. So now in order to
[02:28:45] build a range you need two things. You
[02:28:47] need the lower boundary for the range
[02:28:49] and you need as well the upper boundary.
[02:28:52] Once you have two boundaries then you
[02:28:54] have a range and everything between
[02:28:56] those two boundaries going to be true
[02:28:59] and everything outside those boundaries
[02:29:01] going to be false. So now for example
[02:29:03] let's say that we have the lower
[02:29:04] boundary 100 and the upper boundary 500.
[02:29:07] And there is one thing that you have to
[02:29:08] understand about the between the
[02:29:10] boundaries are inclusive. So that means
[02:29:12] if a value is exactly 100 or exactly 500
[02:29:16] then it's going to considered as a true.
[02:29:18] So it is considered to be inside the
[02:29:20] range. Now if you apply this filter to
[02:29:22] our data where we say the score must be
[02:29:25] between 100 and 500 going to go and do
[02:29:28] the following. So for the first customer
[02:29:30] Maria is going to go and check whether
[02:29:32] her score is inside the boundaries. So
[02:29:35] as you can see 300 is between 100 and
[02:29:37] 500. So she is in the green area and
[02:29:40] that's why Isque going to be happy about
[02:29:41] it and leave the customer in the
[02:29:43] outputs. Now moving on to John. John has
[02:29:45] 900. As you can see 900 is greater than
[02:29:48] 500. So this value is going to be
[02:29:50] outside the boundaries on the right side
[02:29:52] and this means the score of John is not
[02:29:54] in the range. That's why he is not
[02:29:56] fulfilling the condition and SQL going
[02:29:58] to go and remove this customer from the
[02:30:00] results. Now moving on to George 750.
[02:30:03] The same thing outside the range. SQL
[02:30:05] will not accept it and remove this
[02:30:07] customer from the final results. Now
[02:30:09] moving on to Martin his score is 500 and
[02:30:12] this is exactly at the boundary. So if
[02:30:14] it's like 5001 it's going to be outside.
[02:30:17] So since between is inclusive then SQL
[02:30:20] going to accept it and Martin considered
[02:30:22] to be in the range and fulfilling the
[02:30:24] condition. So SQL going to keep him in
[02:30:25] the final result. Now here are speaking
[02:30:27] about better he has zero score and this
[02:30:30] is less than 100. So in the left side
[02:30:32] not in the range. So not fulfilling the
[02:30:34] condition and SQL going to go and remove
[02:30:36] him. This is exactly how between works
[02:30:38] in SQL. It's very simple. Okay. So now
[02:30:40] we have the following task and it says
[02:30:42] retrieve all customers whose score falls
[02:30:45] in range between 100 and 500. So let's
[02:30:48] start as usual by selecting all data
[02:30:51] from customers and execute it. Now the
[02:30:54] task says everything. We need all
[02:30:55] customers in a range. So we have a lower
[02:30:58] value and a higher value. So in order to
[02:31:00] do that as usual we're going to use the
[02:31:02] where and then we're going to specify
[02:31:04] the column that we want to filter on. So
[02:31:06] it's going to be the score and since we
[02:31:07] have like two boundaries we can go and
[02:31:09] use the function between and we start
[02:31:11] with the first boundary the lowest
[02:31:12] boundary. So it is the 100 and 500 the
[02:31:17] high boundary the upper boundary. So
[02:31:19] between 100 and 500. So now let's go and
[02:31:23] execute it. And with that we get only
[02:31:25] those two customers because they are
[02:31:27] between this window. Now there is
[02:31:29] another way in how to solve this task by
[02:31:31] not using between. We can go and use the
[02:31:34] comparison operator together with a
[02:31:36] logical operator and. So let me show you
[02:31:38] how we can do that. I'm going to go and
[02:31:40] copy the whole thing. And now we're
[02:31:41] going to write two conditions. So first
[02:31:43] the score should be higher or equal to
[02:31:46] 100 because the boundaries is inclusive
[02:31:49] and the other one the score is less or
[02:31:52] equal to 500. So this is the upper
[02:31:55] boundary. So with that we have the two
[02:31:56] conditions and we can go and connect
[02:31:58] them using the and operator. So it's
[02:32:00] like very similar to the between we have
[02:32:02] an and between the upper and the lower
[02:32:04] boundaries but we are using the
[02:32:06] comparison operators. So it is higher or
[02:32:09] equal to 100 and lower or equal to 500.
[02:32:12] If you go and run this query you will
[02:32:14] get exactly same results. Now if you ask
[02:32:16] me which method is my favorite I'm going
[02:32:18] to go with this method and I will skip
[02:32:20] the between because each time to be
[02:32:22] honest for me I forget about the between
[02:32:24] whether the boundaries are inclusive or
[02:32:26] exclusive. But if I read the script I am
[02:32:29] going to see exactly that those
[02:32:30] boundaries are inclusive because we have
[02:32:32] here the equals. So I really prefer
[02:32:34] using the compressor operator together
[02:32:36] with the and then using between. So it's
[02:32:38] up to you if you memorize it then go
[02:32:40] with the between. But for me I'm going
[02:32:42] to go with the compression operators.
[02:32:44] Okay my friends. So that's all about the
[02:32:45] between and the range operator. Now
[02:32:47] let's move to another group. We have the
[02:32:49] membership operator. So here we have
[02:32:51] like two. We have the in and the not in.
[02:32:53] So let's understand what this exactly
[02:32:59] means. Okay. So what is in operator?
[02:33:01] It's going to go and check if a value
[02:33:03] exist in a list. So you have a list of
[02:33:06] values and you are checking whether your
[02:33:07] value is a member of your list. So let's
[02:33:10] have very simple example in order to
[02:33:11] understand what this means. Okay. So now
[02:33:13] how this works exactly what you have to
[02:33:15] do is to go and make a list of values.
[02:33:18] So let's say that I have a list and
[02:33:19] there I have specified two values
[02:33:21] Germany and USA. So those two are the
[02:33:24] members of this list. Now if you use the
[02:33:26] n operator it's going to go and check
[02:33:28] the value of countries whether it is in
[02:33:30] the list or not. So let's do it one by
[02:33:32] one. For the first customer Maria her
[02:33:34] country is Germany and Germany is member
[02:33:37] of the list. So it's going to be happy
[02:33:39] and going to leave Maria in the final
[02:33:41] results. Now moving on to John. John
[02:33:43] comes from USA. USA is member of the
[02:33:45] list. So he is fulfilling as well the
[02:33:47] condition and you're going to see John
[02:33:49] in the final results. Now we come to
[02:33:50] George. George comes from UK and UK is
[02:33:53] not member of our list. And SQL going to
[02:33:56] go and remove this customer from the
[02:33:58] final results not fulfilling the
[02:33:59] condition. Now for the last two, Martin
[02:34:01] and Peter, their country is a member of
[02:34:03] the list and SQL going to go and leave
[02:34:05] those customers in the final results. So
[02:34:07] as you can see it's very simple. Or what
[02:34:09] you have to do is to define the members
[02:34:10] of a list and use the n operator and if
[02:34:13] the value is a member of this list it's
[02:34:15] going to be true otherwise it's going to
[02:34:17] be false. Now of course the other
[02:34:18] operator going to be exactly the
[02:34:20] opposite where we say not in the list.
[02:34:23] So we are searching for values that are
[02:34:25] not in this list. So as we are using not
[02:34:27] it's going to go and reverse completely
[02:34:29] the truth. And if you apply this you
[02:34:31] will get in the result only one
[02:34:33] customer. you will get George and the
[02:34:35] result because the country is UK and UK
[02:34:38] is not a member of the list. So if you
[02:34:40] use not together with the in operator
[02:34:42] you will get exactly the opposite
[02:34:44] effect. So this is how the in and the
[02:34:46] not in operator works in SQL. Let's go
[02:34:48] back to scale in order to practice that.
[02:34:50] Okay. So now we have this task and it
[02:34:52] says retrieve all customers from either
[02:34:54] Germany or USA. Okay. So let's try to
[02:34:57] solve this task. This going to be a
[02:34:59] little bit tricky. So select star from
[02:35:02] customers as usual and execute it. So
[02:35:04] now we need in the results only customer
[02:35:06] that comes either from Germany or USA.
[02:35:09] So that means this customer over here
[02:35:11] should be excluded from the result
[02:35:12] because he come from UK. So how we going
[02:35:14] to write it? It's going to be like this
[02:35:16] maybe. So the first one going to be the
[02:35:19] country is equal to Germany or the
[02:35:22] country is equal to USA right something
[02:35:25] like this. So if you go and execute it,
[02:35:27] you will get in the output only the
[02:35:28] customers that are either from Germany
[02:35:30] or USA. And with that we have solved the
[02:35:32] task, right? Well, there is another way
[02:35:34] in order to solve this task which is
[02:35:36] more clear and shorter using the n
[02:35:38] operator. So now how we going to do it?
[02:35:40] Let's go and get the whole thing in
[02:35:43] another query. And now instead of having
[02:35:45] equals and ors and so on, we're going to
[02:35:48] use the in operator and then we're going
[02:35:50] to have like two parentheses and then
[02:35:51] inside it we're going to have a list of
[02:35:53] values. So it's going to be the Germany
[02:35:55] and then the second value going to be
[02:35:57] USA like this. So we are saying country
[02:36:00] should be in this list Germany or USA
[02:36:03] and if it is like one of those values
[02:36:05] then the condition is fulfilled. So now
[02:36:07] if you go and execute this one over here
[02:36:09] you will get the exact same results. So
[02:36:12] my friends, if you notice that you are
[02:36:13] repeating yourself in the wear condition
[02:36:15] and you are just changing the value of
[02:36:17] the condition, it is based on the same
[02:36:19] column and you are connecting them using
[02:36:21] the or then there is something wrong and
[02:36:23] always think on this scenario to use the
[02:36:25] in operator because this can be really
[02:36:28] ugly once you have a lot of values. So
[02:36:30] imagine in our database we have a lot of
[02:36:32] countries and your query going to be
[02:36:34] like something like this. So you are
[02:36:36] keep repeating country equal or country
[02:36:38] equal and so on. Instead of that you're
[02:36:40] going to have a really nice list of
[02:36:42] countries in one go. So this is as you
[02:36:44] can see here it is easier to extend and
[02:36:47] as well has better performance. So as
[02:36:49] you can see we are repeating the same
[02:36:51] thing but we are just changing the value
[02:36:53] and we are connecting all those
[02:36:54] conditions using the or in this scenario
[02:36:56] go and use the in operator. All right my
[02:36:59] friends. So that's all for the
[02:37:00] membership operators. Now we're going to
[02:37:02] speak about the last one the search
[02:37:04] operator. And here we have only one the
[02:37:06] like. And each time we're going to say
[02:37:07] like, I'm going to remind you to like
[02:37:09] this course. So let's
[02:37:13] go. Okay. So now what is like operator?
[02:37:16] You can use it in order to search for a
[02:37:19] pattern in your text. So if you have
[02:37:21] like a text or characters and you are
[02:37:23] searching for a specific pattern inside
[02:37:25] the text. So let's have an example in
[02:37:27] order to understand exactly what this
[02:37:28] means. Okay. So now if you don't have
[02:37:30] yet cafe, go grab one because you have
[02:37:33] to focus for this one. Now what we have
[02:37:34] to do is to define a pattern in is
[02:37:36] scale. In order to build a pattern we
[02:37:38] have like two special characters. If you
[02:37:40] use a percentage you are saying
[02:37:42] anything. So I'm going to accept
[02:37:44] anything. So it could be no characters
[02:37:46] at all or only one character or many
[02:37:48] characters. So I'm saying anything. Now
[02:37:50] if you use an underscore you are
[02:37:52] expecting to have exactly one thing like
[02:37:55] one character or one number. So it is
[02:37:57] exactly one. I know this sounds
[02:37:59] complicated but with an example you can
[02:38:00] understand this. And I can tell you the
[02:38:02] percentage is way more famous than the
[02:38:05] underscore. I rarely really use the
[02:38:07] underscore. So now let's say that I
[02:38:09] build the pattern like this. I say the
[02:38:11] first character must be M and then
[02:38:13] percentage. So here I'm saying in my
[02:38:15] text the first character must be an M
[02:38:18] and after the first character I really
[02:38:20] don't care. It could be any character,
[02:38:22] any number whatever. So this is the
[02:38:24] pattern and now let's have few values in
[02:38:26] order to say whether it's true or false.
[02:38:27] So now if you have the value Mariam. So
[02:38:29] now you can see the first character is
[02:38:31] an M which is perfect. This is exactly
[02:38:34] our pattern. The first character must be
[02:38:36] an M. And then after the M we got like
[02:38:38] four characters. So whatever it is
[02:38:40] totally fine. We can say Maria is
[02:38:42] fulfilling our pattern. And this is
[02:38:44] exactly what we are searching for. This
[02:38:47] value is fulfilling the condition. Okay.
[02:38:48] Now moving on to the next value we have
[02:38:50] m a. So here again the first character
[02:38:53] is an M which is perfect. And after that
[02:38:55] we have only one character a. Well we
[02:38:57] have say percentage. So it could be
[02:38:59] anything one character multiple
[02:39:00] characters a number or whatever. So
[02:39:02] that's why this value can match our
[02:39:04] pattern and we will see it in the
[02:39:06] outputs. Now moving on to the next value
[02:39:08] we have only one m which is as well
[02:39:10] totally fine because we are saying the
[02:39:12] first character must be an M and then
[02:39:14] followed with anything. Now moving on to
[02:39:16] the last scenario we have Emma. Now this
[02:39:18] is a problematic because the first
[02:39:20] character is an E and in our pattern we
[02:39:23] say it must start with M. So we don't
[02:39:25] have that in this word. The first
[02:39:27] character is an E. That's why this value
[02:39:29] is not fulfilling our pattern and SQL
[02:39:32] going to remove this value from the
[02:39:33] final results. So this is exactly what
[02:39:35] going to happen if you have this pattern
[02:39:37] and those values. Now let's have another
[02:39:39] scenario where you say you know what it
[02:39:41] could start with anything but for me it
[02:39:43] is very important the last two
[02:39:45] characters it must be an I and N. So we
[02:39:48] could start with anything but the last
[02:39:50] two must be an I and N. So let's take
[02:39:52] this value Martin going to go and check
[02:39:54] immediately the last two characters. So
[02:39:56] you can see we have an I and N and the
[02:39:59] first part marks it is fine. It could be
[02:40:01] anything. So this value is fulfilling
[02:40:03] the condition because the last two
[02:40:05] characters is an I and N. Now moving on
[02:40:07] to the next one we have vin. So v i n
[02:40:10] the last two characters is as well
[02:40:12] exactly what we are searching for. It is
[02:40:14] fulfilling the condition and we have
[02:40:15] before it like only v. So we say
[02:40:17] anything with a percentage. Right? Now
[02:40:19] one more we have in. So it is as well
[02:40:22] fulfilling the condition because before
[02:40:23] it we don't have anything. So en is
[02:40:26] fulfilling as well the condition. The
[02:40:28] percentage is always saying anything.
[02:40:30] Now moving on to the last scenario we
[02:40:31] have Jasmine. They are not the last two
[02:40:33] characters. The last two characters is
[02:40:36] an N and E and this is not matching our
[02:40:39] pattern and this why this value is not
[02:40:41] fulfilling our pattern and you will not
[02:40:43] see it in the results. So with that you
[02:40:44] can understand how we can search for
[02:40:46] something in a text using the like
[02:40:48] operator. Let's keep going. Now let's
[02:40:50] say that I have a percentage at the
[02:40:51] start and percentage at the end and in
[02:40:54] between I have only one character an R.
[02:40:56] If you define it like this you are
[02:40:58] saying if there is an R anywhere it is
[02:41:01] good enough whether it's beginning or at
[02:41:03] the end or in between then the condition
[02:41:06] is fulfilled. So if you have Maria you
[02:41:07] can see we have an R in the middle. So
[02:41:10] in the left side we have two characters
[02:41:11] on the right side we have two characters
[02:41:13] doesn't matter the main thing we have an
[02:41:15] R somewhere. So this going to be
[02:41:16] fulfilling the condition. Now moving on
[02:41:18] to better we have an R at the end and
[02:41:20] that is totally fine cuz we say at the
[02:41:22] right side it could be anything. So we
[02:41:24] have an R somewhere that's why it's
[02:41:26] going to fulfill the condition. Now we
[02:41:28] have another case where we say Ryan we
[02:41:30] have an R at the start. So we don't have
[02:41:32] anything before and we have after that
[02:41:34] like three characters which is totally
[02:41:36] fine. So we don't really care about the
[02:41:38] position of the R. It is totally
[02:41:40] acceptable to have an R anywhere. And if
[02:41:42] you have only an R that is as well good
[02:41:44] enough. You don't have anything before.
[02:41:46] you don't have anything after and that's
[02:41:48] okay. But if you have a word like Alice,
[02:41:50] we don't have any R inside it. So that's
[02:41:52] why this is the only case where you say
[02:41:54] we don't have here an R and it's going
[02:41:57] to remove this value from the results.
[02:41:59] And this way of searching of something
[02:42:00] is very famous. You don't care about the
[02:42:02] words before this word and after the
[02:42:04] word, right? So if you are searching for
[02:42:06] any word, you're going to say percentage
[02:42:08] before and percentage after. Now I know
[02:42:11] that we want to practice with the
[02:42:12] underscore. So let's say that I have two
[02:42:14] underscores and then the character B and
[02:42:16] then a percentage. So here what I'm
[02:42:18] saying there should be something in the
[02:42:20] first position. There should be as well
[02:42:22] something in the second position. Then
[02:42:24] the third position should be the
[02:42:26] character B must be exactly at this
[02:42:28] position and after that it could be
[02:42:30] anything. So we really don't care. I
[02:42:31] know this is a little bit complicated.
[02:42:33] Let's have an example. So we have the
[02:42:34] value alert. Now we can see the first
[02:42:36] position we have something the A. Then
[02:42:39] the second position we have as well
[02:42:40] something the L. So so far we are good
[02:42:42] at the pattern and then the third
[02:42:44] position we have B. So we have complete
[02:42:46] match and the rest the ERT whatever. So
[02:42:49] with that Albert is matching our
[02:42:51] pattern. Moving on to the next one rope.
[02:42:53] You can see the first character we have
[02:42:54] something which is good. We have the R.
[02:42:57] Then the second character we have an O.
[02:42:59] So it's not empty. We have something.
[02:43:01] And then the third one we have exactly
[02:43:03] B. And after that we don't have anything
[02:43:05] which is fine. So again this value going
[02:43:07] to fulfill the condition. So moving on
[02:43:08] to the next one. So it start with an A.
[02:43:11] So we have something in the first
[02:43:12] position. The second position we have as
[02:43:14] well something the B. But now the third
[02:43:16] character it is a problem. It is not P.
[02:43:18] We have an E. So that's why it is not
[02:43:20] following our pattern. And is going to
[02:43:22] go and remove it. Now moving on to last
[02:43:24] example we have an A and an N. So in the
[02:43:26] first position we have something. The
[02:43:27] second one as well. But the third one we
[02:43:30] don't have anything. We don't have a B.
[02:43:32] So that's why it's going to be removed.
[02:43:33] So my friends I know that was a lot.
[02:43:35] This is exactly how you build a pattern
[02:43:37] for the like operator using the
[02:43:39] percentage and the underscore. But the
[02:43:41] percentage is more famous. So this is
[02:43:43] exactly how it works. Let's go back to
[02:43:45] scale in order to have some examples.
[02:43:47] All right, let's start with this task.
[02:43:48] Find all customers whose first name
[02:43:51] starts with a capital M. So let's go and
[02:43:53] start searching for those informations.
[02:43:55] We're going to start as usual. Select
[02:43:56] star from customers. And now we have to
[02:43:58] go and build the filter logic. So we're
[02:44:01] going to say where. Now we are searching
[02:44:03] something in the first name. So we're
[02:44:05] going to say first name. So that means
[02:44:07] it is very important to start with an M
[02:44:09] and then the rest it doesn't matter. So
[02:44:11] we're going to use the like operator in
[02:44:13] order to search. And we're going to have
[02:44:15] our single quotes and we're going to
[02:44:16] start with the M. And it doesn't matter
[02:44:19] what comes after that. So for us it is
[02:44:21] very important that the first character
[02:44:23] is an M. Let's go and execute it. And
[02:44:26] with that we got our two customers Maria
[02:44:28] and Martin. And both of them starts with
[02:44:30] an M. So with that we have solved the
[02:44:32] task. It is very simple. Now we have the
[02:44:34] following task. Find all customers whose
[02:44:37] first name ends with an N. So let's go
[02:44:40] first and select all the customers here.
[02:44:42] And we need all those customers where
[02:44:44] they are having an N at the end. So we
[02:44:46] have John and as well Martin. So how we
[02:44:49] going to do it? The same thing where
[02:44:50] first name like since we are searching
[02:44:53] but here we're going to change the
[02:44:54] expression. So it must ends with an N as
[02:44:57] a last character. So before that it
[02:44:59] doesn't matter whether it is the first
[02:45:01] character. So it could be anything but
[02:45:03] the last character of the word should be
[02:45:05] an N. So that's it. Let's go and
[02:45:07] execute. And with that we got John and
[02:45:10] Martin because the last character is an
[02:45:12] N. It is very simple, right? It is all
[02:45:14] about where we're going to place this
[02:45:16] percentage. Okay. So now we have the
[02:45:17] next task. Find all customers whose
[02:45:20] first name contains an R. So here we
[02:45:22] don't have like specifications whether
[02:45:24] it is at the start or at the end.
[02:45:26] Somewhere there should be an R. So if
[02:45:28] you go and execute first without any
[02:45:30] wear condition you can see here for
[02:45:31] example Maria we have in the middle
[02:45:33] somewhere an R George George as well
[02:45:36] Martin and Peter at the end. So we have
[02:45:38] a lot of names with an R. So how we can
[02:45:40] search for that? We're going to stick
[02:45:41] with the where first name like and here
[02:45:44] our character going to be an R and we're
[02:45:46] going to put before it and after it a
[02:45:48] percentage. So it doesn't matter what is
[02:45:50] before it or after it somewhere there
[02:45:53] should be an R. So let's go and execute
[02:45:55] it. And with that we got all our
[02:45:57] customers where somewhere we have an R.
[02:46:00] As you can see it is very simple. If you
[02:46:01] put it before and after then you are
[02:46:03] open for more results. And this is
[02:46:05] usually used a lot in order to search
[02:46:07] for a value inside your database. All
[02:46:10] right. Now we're going to move to a
[02:46:11] funny one. It kind of says find all
[02:46:13] customers whose first name has an R in
[02:46:15] the third position for some reason. I
[02:46:17] don't know why. So let's go and execute
[02:46:19] our customers here without any filter.
[02:46:21] So it is for us very important to find
[02:46:23] the customers where in the third
[02:46:25] position we have an R like here for
[02:46:27] example Maria the third character is an
[02:46:29] R which is okay but with Peter over here
[02:46:31] it is not the third character so it is
[02:46:34] not fulfilling the condition. So how we
[02:46:35] going to write that? It going to say
[02:46:37] like this where the first name like but
[02:46:40] we have to write it now from the start.
[02:46:41] So the first position going to be an
[02:46:43] underscore the second position going to
[02:46:45] be as well an underscore and now in the
[02:46:47] third position going to have an R. So
[02:46:49] with that we make sure the third
[02:46:51] position and an R and before it we have
[02:46:53] two positions and now afterward it
[02:46:55] doesn't matter what comes after that it
[02:46:57] could be nothing or characters. So if
[02:46:59] you go and execute it like this we will
[02:47:01] get Maria and Martin and we will not get
[02:47:04] Peter because the R is not in the third
[02:47:06] position. So now if you don't do it
[02:47:08] correctly with the underscores let's go
[02:47:09] and remove one of them and execute. You
[02:47:12] will get nothing because we don't have
[02:47:14] any first name where the second position
[02:47:16] is an R. So you have to be very careful
[02:47:18] with this. All right my friends. So this
[02:47:20] is how you search inside your values.
[02:47:22] And with that we have covered all
[02:47:24] different groups of operators that you
[02:47:26] can use inside a wear clause. So with
[02:47:28] that you have learned how to filter your
[02:47:30] data using multiple operators that you
[02:47:32] can use inside the wear clause. So you
[02:47:35] can filter anything now in SQL. Now we
[02:47:37] will move to very interesting topic. You
[02:47:39] will learn how to combine your data from
[02:47:41] multiple tables. And here we have two
[02:47:43] main methods. The first one is SQL joins
[02:47:45] and the second set operators. And they
[02:47:48] are really big topics. So we're going to
[02:47:49] first focus on the SQL joins. And here
[02:47:52] we have a lot of things to cover. So now
[02:47:54] we are talking about the core of SQL. So
[02:47:56] let's
[02:48:00] go. All right. So now we have two
[02:48:02] tables, table A and table B. And the big
[02:48:05] question here is how to combine those
[02:48:08] two tables. What do we want exactly? Do
[02:48:10] you want to combine the rows or the
[02:48:12] columns? And now if you say I would like
[02:48:14] to combine the columns then we are
[02:48:16] talking about joining tables. So we're
[02:48:19] going to use joins in SQL. So now let's
[02:48:21] say that we are joining the table A with
[02:48:23] the table B and we start from the table
[02:48:25] A. So SQL going to take the columns and
[02:48:27] the rows of the table A and SQL going to
[02:48:30] call it the left table because we
[02:48:32] started from there and then we join it
[02:48:34] with the table B and SQL going to call
[02:48:36] the second table as the right table. And
[02:48:38] here what's going to happen? and SQL
[02:48:39] going to take the columns and the rows
[02:48:41] from the right table and put it side by
[02:48:43] side with the columns and rows of the
[02:48:46] table A. So we are like combining the
[02:48:48] columns we are putting them side by
[02:48:50] side. And now if you say you know what I
[02:48:52] don't want to do that I would like to
[02:48:53] combine the rows both of the tables
[02:48:55] having the same columns. I just want to
[02:48:57] stack them. So we are now talking about
[02:49:00] another methods. It is called the set
[02:49:02] operators. So here there is like no left
[02:49:04] and right. So since we started with the
[02:49:06] table A, the SQL going to take the
[02:49:08] columns and the rows of the table A and
[02:49:10] put it in the results. And then it's
[02:49:11] going to go to the second table, table B
[02:49:13] and it's going to take only the rows and
[02:49:15] put it below the rows of the the table
[02:49:17] A. So we are putting the rows beneath
[02:49:20] each others. We are doing like
[02:49:21] appending. So that means as we are using
[02:49:24] the set operators, we are combining the
[02:49:26] rows. Our table going to be longer but
[02:49:28] with the joins we are combining the
[02:49:30] columns side by side and we are getting
[02:49:32] wider table. But now for each methods
[02:49:34] there are different types. So now for
[02:49:36] example in order to do the joints we
[02:49:38] have four very famous types. We can do
[02:49:40] an inner join, full join, left join,
[02:49:42] right join. But of course there are more
[02:49:44] than that but those are the basics. And
[02:49:46] for the set methods we have as well
[02:49:48] types. We have the union, union all
[02:49:50] except and intersect. And for each
[02:49:53] methods there are like different rules.
[02:49:55] In order to join the tables we have to
[02:49:57] define the key columns between the two
[02:49:59] tables. Don't worry we're going to learn
[02:50:00] about that later. This is the
[02:50:02] requirement in order to join tables and
[02:50:04] the requirement of combining tables
[02:50:06] using the set operators the tables in
[02:50:08] your query should has the exact same
[02:50:10] number of columns but here you don't
[02:50:12] need any like key in order to combine
[02:50:14] the tables. So guys if you look at this
[02:50:16] in order to combine two tables first you
[02:50:19] have to decide do I want to combine the
[02:50:21] columns or the rows. So first you have
[02:50:22] to decide in the methods and after that
[02:50:25] you have different types on how exactly
[02:50:27] you're going to go and combine the data
[02:50:28] and of course there are rules that you
[02:50:30] have to follow. Now, of course, we're
[02:50:31] going to go and cover everything in the
[02:50:32] course, but now in this section, we're
[02:50:34] going to learn how we're going to
[02:50:35] combine the tables using the SQL joins.
[02:50:38] So, we're going to go and dive into this
[02:50:44] word. All right. So, now what is exactly
[02:50:46] SQL joins? Now, let's say that we have
[02:50:48] two tables. On the left table, we have
[02:50:50] the customer name. So, we have four
[02:50:52] customers. And on the right table, we
[02:50:53] have the country informations about the
[02:50:55] customer. And now we would like to query
[02:50:57] both of those informations the names and
[02:51:00] the countries. Now in order to query
[02:51:02] those two tables in one query first we
[02:51:05] have to connect them. And in order to
[02:51:06] connect those two tables we need a key a
[02:51:09] column that exist on the left and on the
[02:51:11] right sides. And by looking to this the
[02:51:14] common column here is the ID of the
[02:51:16] customer. Now once we connect those ids
[02:51:18] together we will be able to query those
[02:51:21] tables together and SQL going to start
[02:51:23] matching those ids. So for the ID number
[02:51:25] one, we will get the name Maria and the
[02:51:28] country Germany. And the ID2 is
[02:51:30] connecting John to USA. And now you can
[02:51:33] see the ID3 is not connectable. So we
[02:51:35] cannot connect it to the right side. But
[02:51:37] for the ID4, we can use it in order to
[02:51:40] connect Martin to Germany. So this is
[02:51:42] exactly what happens if you join two
[02:51:44] tables. You connect those two tables
[02:51:46] using a common column, a key like the
[02:51:48] ID. And once we have matching value, we
[02:51:51] can connect the two rows together. So
[02:51:53] this is what we mean with SQL
[02:51:58] joins. Now you might ask why do we need
[02:52:01] actually joins? Well, the first and very
[02:52:03] important reason is to recombine your
[02:52:05] data. So now usually in databases the
[02:52:07] data about something like the customers
[02:52:10] could be spreaded into multiple tables.
[02:52:12] Like we could have table called
[02:52:14] customers, another one where we have the
[02:52:16] customer addresses and a third table
[02:52:18] where you can find the orders of the
[02:52:20] customers and maybe another one where
[02:52:22] you can find the reviews of the
[02:52:24] customers. So as you can see the data of
[02:52:26] the customers is spreaded into like four
[02:52:28] tables. Now how about I would like to
[02:52:30] see all the data about the customers in
[02:52:32] one results. So I would like to see the
[02:52:34] complete big picture about our
[02:52:36] customers. What we can do, we can go and
[02:52:39] connect those four tables using the SQL
[02:52:42] joins. And once we do that in one query,
[02:52:45] I will be able to combine all those
[02:52:47] tables in one big results. And this is
[02:52:49] the most important reason why we use SQL
[02:52:52] joins in order to combine all the data
[02:52:54] about specific topic in order to see the
[02:52:57] big picture. Now, another reason why we
[02:52:59] use SQL joins is to do data enrichment.
[02:53:02] It is where I want to get an extra data
[02:53:04] and extra information. So let's say that
[02:53:06] you are querying the table customers and
[02:53:08] this is your main table the master
[02:53:10] table. So you are able to see all the
[02:53:12] data that you need but sometimes what
[02:53:14] happens you would like to get an extra
[02:53:16] information from another table like for
[02:53:18] example the zip codes of the countries.
[02:53:20] So you would like the help of another
[02:53:22] table we call it a reference table or
[02:53:24] sometimes lookup table where there is
[02:53:26] like one extra information that you
[02:53:28] would like to add it to your master
[02:53:30] table to the primary source of your
[02:53:32] data. So now what we can do we can join
[02:53:33] those two tables in order to enhance our
[02:53:36] table. So we are getting one extra
[02:53:38] relevant informations for the customers
[02:53:40] and this process we call it data
[02:53:42] enrichments. I'm getting an extra data
[02:53:44] for my main table. So this is another
[02:53:47] reason why we use joins. All right. So
[02:53:49] now so far we have used joins in order
[02:53:51] to get the data from two tables. But now
[02:53:54] there is another use case for the SQL
[02:53:55] joins. We use it in order to check the
[02:53:58] existence of your data in another table
[02:54:01] or maybe as well the not existence. So
[02:54:03] let's say that I have a table called
[02:54:04] customers and I'm working with this
[02:54:06] table and doing queries. But now I would
[02:54:08] like to check something. I would like to
[02:54:10] check whether our customers did order
[02:54:12] something. Now in order to check that I
[02:54:15] need the help of another table for
[02:54:17] example the table orders. So that means
[02:54:19] I'm using the table orders only for my
[02:54:22] check. So I don't want to get any extra
[02:54:24] data from the orders in my final
[02:54:26] results. I'm just using the table orders
[02:54:28] and we call in this table a lookup. So
[02:54:30] now what we can do we can connect those
[02:54:32] two tables together. And now based on
[02:54:34] the existence of the customers inside
[02:54:36] the second table the orders either the
[02:54:39] customer going to stay in the final
[02:54:40] results or going to be removed. So that
[02:54:42] means I'm filtering the data based on
[02:54:44] the join. And of course I can check as
[02:54:46] well the net existence. I would like to
[02:54:48] see in the final results all the
[02:54:50] customers that didn't order anything. So
[02:54:53] it is the same scenario. So my friends,
[02:54:55] those are the main three reasons why you
[02:54:57] use SQL joins. First, if you want to
[02:55:00] combine the data from multiple tables in
[02:55:02] one big picture. So I use join in order
[02:55:04] to get the data from different tables.
[02:55:06] The second use case, you are working
[02:55:08] with one table but you would like to get
[02:55:10] an extra information from another table.
[02:55:12] So you are doing it like something
[02:55:14] called data enrichments. And in the
[02:55:16] third scenario, we don't want to combine
[02:55:18] the data. We want just to join it with
[02:55:20] another table in order to do a check to
[02:55:22] check the existence of your records in
[02:55:24] another table. So this is why we need
[02:55:26] joins in
[02:55:31] SQL. Now there is like a lot of
[02:55:33] different possibilities on how to join
[02:55:35] tables, how to join the data. Now in
[02:55:37] order to make it easy to understand,
[02:55:39] we're going to visuals as like two
[02:55:41] circles. So we have the table A and a
[02:55:43] table B. The table A is on the left
[02:55:45] side. We call it the left table. And the
[02:55:48] table B going to be on the right side
[02:55:49] and we call it the right table. The side
[02:55:51] of the tables is very important. Now if
[02:55:53] you combine those two circles, you will
[02:55:55] get three different possibilities. The
[02:55:57] circles going to overlap. And here
[02:55:59] exactly where we can have the matching
[02:56:01] data between the two tables. So the data
[02:56:04] is available on the left and on the
[02:56:05] right. Or another possibility you want
[02:56:07] to get all the data from one of the
[02:56:09] tables. So you can get all the rows from
[02:56:12] one circle. And the third possibility
[02:56:14] you want to get only the unmatching data
[02:56:16] from one table. So if something exists
[02:56:19] in one table but not in the other table
[02:56:21] then we call it unmatching data. So
[02:56:23] those are the three scenarios that you
[02:56:25] have to ask yourself once you are
[02:56:27] combining tables and this can generate a
[02:56:29] lot of join types. So here we have like
[02:56:31] basic SQL joins those are the classical
[02:56:34] one and here depends on the scenario
[02:56:36] whether you want only matching all or
[02:56:38] all the rows from either left or right
[02:56:40] and we have advanced SQL joins where we
[02:56:43] focus on the unmatching data. Now we're
[02:56:45] going to go and cover all those types
[02:56:46] one by one. So we're going to start
[02:56:48] first with the basics and the first
[02:56:49] option that you have is to get all the
[02:56:52] data without joining tables. So let's
[02:56:54] see what this means.
[02:56:58] So what do we mean with no join? Well,
[02:57:01] we want to returns the data from two
[02:57:03] tables without combining them. So
[02:57:05] actually this is not a joint type
[02:57:07] because we are not combining anything.
[02:57:09] We just want to query the data from two
[02:57:11] tables. So that means from the table A
[02:57:13] we want to see all the rows everything
[02:57:15] and from the table B we want to see
[02:57:17] everything as well all the rows. So that
[02:57:19] means we want to see two results and
[02:57:21] there is no need to combine them. So
[02:57:23] let's see the syntax of that. So all
[02:57:25] what you have to do is very simple.
[02:57:26] Select star from table A and then
[02:57:29] semicolon and then start another query.
[02:57:31] Select star from table B. So that's it.
[02:57:34] And of course since we are not combining
[02:57:35] the data there will be no join in the
[02:57:38] syntax. So that's it. Let's go to SQL in
[02:57:40] order to do that. Okay. So now we have
[02:57:42] the following task. It says retrieve all
[02:57:44] data from customers and orders in two
[02:57:46] different results. So that sounds that
[02:57:49] we don't have to go and combine the
[02:57:50] tables together. And all what we can do
[02:57:52] is the following. We can go and select
[02:57:54] the data from the first table like this
[02:57:57] and then we make another query for the
[02:58:00] second table the orders and we don't
[02:58:03] have to go and combine them in one big
[02:58:05] query. We just use a very simple select
[02:58:07] statements in order to retrieve the
[02:58:09] data. So if you go and execute it since
[02:58:11] you have two separate queries you will
[02:58:12] get two results and with that in one
[02:58:14] result you will get all the customers
[02:58:16] and in the other result you will get all
[02:58:18] the orders and the data is not combined
[02:58:21] at all. So this is how you query two
[02:58:23] tables without combining them. So with
[02:58:25] that we are getting all the data without
[02:58:27] joining the tables. Now we're going to
[02:58:28] start talking about the first type of
[02:58:30] join the inner join where we start
[02:58:32] combining the data from two tables. So
[02:58:34] let's
[02:58:38] go. Okay. So now what is exactly an
[02:58:40] inner join? So this type going to return
[02:58:43] only the matching rows from both tables.
[02:58:46] So that means we will see in the output
[02:58:48] only matching rows. So now what do we
[02:58:50] need from the left table? We want only
[02:58:52] the matching data. So we will not get
[02:58:54] the whole circle of A. We will get only
[02:58:57] where we have an overlapping with the
[02:58:59] table B. So we want to see the data from
[02:59:01] A only if it exists in the table B. And
[02:59:04] now what do we need from the table B?
[02:59:06] Exactly the same thing only the matching
[02:59:08] data. So that means I don't want to see
[02:59:10] all the data from B. I want to see only
[02:59:12] the data in B that has a match from the
[02:59:15] table A from the left side. And with
[02:59:17] that you will get only the matching data
[02:59:19] from both tables. Now let's see how we
[02:59:21] can write that in SQL. So it is a usual
[02:59:24] query and always we start with a select.
[02:59:26] So we select for example all the columns
[02:59:28] from and here we specify the table name.
[02:59:30] So it's going to be a. So so far nothing
[02:59:32] new. But now we want to add as well the
[02:59:34] table B in the same query. In order to
[02:59:36] do that we use the keyword join and then
[02:59:39] we say table B the name of the table.
[02:59:42] And since we have like different types
[02:59:43] of joins in SQL, you can specify the
[02:59:46] type of the join before the keyword
[02:59:48] join. And if you don't specify anything,
[02:59:50] the default type is inner join. But my
[02:59:53] friends, the best practices is always
[02:59:55] mention the type. I don't like to skip
[02:59:57] the defaults because in projects maybe
[02:59:59] not everyone is aware of the defaults.
[03:00:02] So don't skip that. Always specify the
[03:00:04] type. So now what we're going to do,
[03:00:05] we're going to put the keyword inner
[03:00:07] before the join. And with that SQL going
[03:00:09] to know how to deal with the rows
[03:00:11] between two tables. But still we are not
[03:00:13] done there. We have to tell SQL how to
[03:00:16] combine the tables. And with that we use
[03:00:18] the keyword on. And after that you
[03:00:20] specify the join condition. And as we
[03:00:22] learned in order to join two tables we
[03:00:25] have to find out a common column in
[03:00:27] order to match the data. Right? And
[03:00:29] usually in scale they are the keys or
[03:00:31] ids. So the condition can be like this.
[03:00:33] the key from the table A must be equal
[03:00:36] to the key from the table B. So this is
[03:00:38] the join condition and using this join
[03:00:41] SQL can go and start matching the data
[03:00:43] from the left table and the right table.
[03:00:45] And there is one thing that is very
[03:00:47] important while you are joining the
[03:00:49] tables you have to understand about the
[03:00:51] order of the tables in your query. Now
[03:00:53] in the inner join the order of the
[03:00:55] tables doesn't really matter. So whether
[03:00:57] you start from A or you start from B it
[03:00:59] doesn't matter because you will get the
[03:01:01] same results. Both of the tables has the
[03:01:03] same priority and it doesn't matter
[03:01:05] where we start whether we say from A
[03:01:07] join B or we say from B join A we will
[03:01:10] get the exact same results. So in the
[03:01:12] inner join you don't have to worry about
[03:01:14] the order of the tables. So that's all
[03:01:16] about the inner join. Now let's go back
[03:01:18] to scale in order to practice. Okay. So
[03:01:20] now we have the following task and it
[03:01:22] says all customers along with their
[03:01:24] orders but only for customers who have
[03:01:26] placed an order. So my friends that
[03:01:28] means we need the data from the
[03:01:30] customers and from the orders from two
[03:01:32] tables and we have to put everything in
[03:01:34] one results. That means we have to join
[03:01:36] two tables. Now let's go and do it step
[03:01:38] by step. So we're going to go and say
[03:01:40] select star from customers and then we
[03:01:43] have to go and join it with the orders.
[03:01:45] We're going to say join orders. Now you
[03:01:47] have to go and specify the join type. Is
[03:01:50] it inner, left, full and so on. Well
[03:01:52] that's depend on the task. It says we
[03:01:55] want all customers but only for
[03:01:57] customers who have placed an order. So
[03:01:59] there is like condition right here. We
[03:02:01] don't want to see everything from the
[03:02:03] customer. We just want to see only the
[03:02:05] matching data only if the customers has
[03:02:08] an order in the orders table. And for
[03:02:10] that we can go and use the inner join.
[03:02:13] Of course if you can leave it like this
[03:02:14] you will get the same effects but I'm
[03:02:16] going to go and specify it like this
[03:02:17] inner join just to make it clear. We are
[03:02:19] speaking about the inner join. And after
[03:02:21] that we have to go and specify the join
[03:02:23] condition. So we have to go and find a
[03:02:25] common column between the customers and
[03:02:27] the orders. So how I usually do it I go
[03:02:29] and explore both of the tables. So I'm
[03:02:32] going to go and select everything from
[03:02:33] customers and as well
[03:02:35] everything from the orders. So let's go
[03:02:39] and execute. Now we're going to start
[03:02:40] searching where do we have a common
[03:02:42] column between those two tables. So we
[03:02:44] have the from the first table first
[03:02:46] name, country score and you don't find
[03:02:48] any of those informations in the second
[03:02:50] table. The only one is the ID. So the ID
[03:02:53] of the customer and the ID of the
[03:02:55] customer you can find it in the orders
[03:02:57] the second column here. So this is the
[03:02:59] common column between those two tables.
[03:03:01] And usually in databases we create ids
[03:03:04] exactly for this in order to connect
[03:03:05] tables. So it's really rarely that we're
[03:03:08] going to use like a country or score or
[03:03:10] first name in order to join tables. We
[03:03:12] usually use the ids. So let's go back to
[03:03:14] our query and use those two columns. So
[03:03:17] it's going to be the ID from the
[03:03:19] customers equal to the customer ID. So
[03:03:22] that's it. With that we have the
[03:03:23] condition we have decided on the type
[03:03:25] and we can go and execute it. Now you
[03:03:27] can see we are getting only three
[03:03:29] customers. Right? If you don't apply the
[03:03:31] inner join we can see that we have five
[03:03:34] customers. So that means actually we
[03:03:36] have two customers without any orders
[03:03:39] any matching data from the other table.
[03:03:41] And as well you can see very nicely we
[03:03:43] have now not only the columns from the
[03:03:45] customers but as well all the columns
[03:03:48] from the orders side by side. So with
[03:03:50] that we have combined the data and as
[03:03:52] well with that we have solved the task
[03:03:53] but we will not leave our query like
[03:03:55] this because it is not really good
[03:03:56] practices. What we have to do is to go
[03:03:58] and select only the columns that really
[03:04:00] make sense in our query because in many
[03:04:02] cases in your tables you will have a lot
[03:04:04] of columns that is not needed like for
[03:04:06] example if you check here you see we
[03:04:08] have the customer ID here and as well
[03:04:10] the customer ID over here. So it's like
[03:04:12] repetition and it's enough to see it
[03:04:14] only once. So what you have to do is to
[03:04:15] go and pick few columns that we want.
[03:04:18] For example, I'm going to start with the
[03:04:19] ID maybe the first name and that's all
[03:04:22] from the first table. Let's go and get
[03:04:24] the order ID and I don't want the
[03:04:26] customer ID again. So from the second
[03:04:28] table I'll get add the sales. So let's
[03:04:30] go and execute it. And with that you can
[03:04:32] see very nicely the customer's name and
[03:04:34] their orders with the sales. And now
[03:04:36] comes something very important.
[03:04:37] Sometimes if you have two tables you
[03:04:39] might have columns that having the same
[03:04:42] names. Like imagine the order ID in the
[03:04:44] table orders it's called ID. So that
[03:04:46] means we have the same name in both
[03:04:48] tables and this kind of makes SQL very
[03:04:50] confused. And here you will get an error
[03:04:52] tells you I really don't know what do
[03:04:54] you mean with the ID. Is it from the
[03:04:56] table customers or from the orders? So
[03:04:58] we have to tell SQL exactly from which
[03:05:00] table did this column come from. So in
[03:05:02] SQL in order to do that what we do
[03:05:04] before the column name you write again
[03:05:07] the table name the customers and then
[03:05:09] you make a dot and now we are telling
[03:05:11] SQL this column the ID it comes from the
[03:05:14] table customers and SQL will not be
[03:05:16] confused about it and it's going to go
[03:05:18] and get the ID from the customers. And
[03:05:20] for the second id you can go over here
[03:05:22] and as well before it you say orders do
[03:05:25] id so that knows okay this ID come from
[03:05:28] the orders and the other one comes from
[03:05:30] the customers and it is always good
[03:05:32] practice especially if you are joining
[03:05:34] tables to always assign for each column
[03:05:36] a table because after a while if you
[03:05:38] open your query and you see okay the
[03:05:40] sales does the sales come from the
[03:05:42] customers or the orders and if you have
[03:05:44] a long list of columns it's going to be
[03:05:46] really confusing so that's why we
[03:05:47] consider it best practices if you always
[03:05:50] assign for each column the table name
[03:05:52] especially if you are doing joins. So
[03:05:54] it's going to be like this. But of
[03:05:55] course if you have like only one table
[03:05:57] it's clear that all the columns in the
[03:05:59] select comes from this table. But since
[03:06:01] here we are dealing with multiple tables
[03:06:03] it is good to show it like this. And of
[03:06:04] course here we don't have the ID. We
[03:06:05] have the order ID and the same thing for
[03:06:08] the join condition. So the ID from here
[03:06:10] comes from the customers and the
[03:06:12] customer ID come from the orders. So now
[03:06:15] it is clear for everyone which column
[03:06:17] come from which table. But now you might
[03:06:19] say you know what each time I have to
[03:06:21] write the customers this is very long
[03:06:23] name and sometimes in real projects
[03:06:25] you're going to see tables that has
[03:06:26] really long name and it's going to be
[03:06:28] really annoying to add it each time
[03:06:30] before each column right so instead of
[03:06:32] that we can go and assign aliases for
[03:06:35] the tables but only for the columns so
[03:06:37] usually we go over here and say as and
[03:06:39] maybe you can go and use only one
[03:06:41] character like the first character C.
[03:06:43] And now instead of saying customers you
[03:06:45] can go over here and say C. The same
[03:06:46] thing for the second column and as well
[03:06:49] over here. And you can use now the C in
[03:06:51] everywhere in your query. The same thing
[03:06:53] for the orders. You can go over here and
[03:06:54] say has O. And now instead of orders you
[03:06:57] say
[03:06:59] O on here. And now it is very easily to
[03:07:02] see those two columns comes from the C
[03:07:05] that means the customers and those two
[03:07:06] columns comes from the O the orders.
[03:07:09] Those are the best practices as you are
[03:07:10] joining tables together in SQL. And of
[03:07:13] course with that we have solved the
[03:07:14] task. And about the order of the tables,
[03:07:15] it doesn't matter where do you start. So
[03:07:17] for example, if you take the orders here
[03:07:19] and put it in the join and get the
[03:07:22] orders in the from. So I just switch the
[03:07:24] tables and execute it, you will get the
[03:07:26] exact same results. So if you are doing
[03:07:28] inner join between two tables, don't
[03:07:30] worry about the order of the tables.
[03:07:32] Okay. So now let's go and instant
[03:07:33] exactly how executed the inner join.
[03:07:36] Okay. So now again here we have our
[03:07:38] query. Then we have the two tables
[03:07:40] customers and orders. And here we have
[03:07:42] the ID where we are joining the data. So
[03:07:44] this is the ID from the table customers
[03:07:46] and this is the customer ID that we have
[03:07:48] in the orders. Now let's see how SQL can
[03:07:50] execute this. So we are saying I would
[03:07:52] like to see the ID and the first name.
[03:07:54] So we will get the ID, the first name
[03:07:56] from the table customers and we would
[03:07:58] like to get the order ID and as well the
[03:08:02] sales from the table orders. So our
[03:08:04] result going to focus on those four
[03:08:06] columns. Now the data should be joined
[03:08:08] between those two tables using the inner
[03:08:09] join and SQL going to start from the
[03:08:11] left table from the customers because we
[03:08:13] say from customers. So it's going to
[03:08:16] start matching the ID from the left
[03:08:18] table with the right table. So it's
[03:08:19] going to say okay is there a match from
[03:08:21] the first record from the first order?
[03:08:24] Well yes it is the same ID and then SQL
[03:08:27] going to say okay that condition is
[03:08:28] fulfilled and we are allowed to see the
[03:08:30] data. So the data will be presented in
[03:08:33] the output. So we're going to have the
[03:08:35] ID Maria and the order ID from Maria and
[03:08:39] the sales of this order. So there is a
[03:08:41] match. Then SQL going to go to the
[03:08:43] second record. Well, we don't have a
[03:08:45] match. The third we don't have match.
[03:08:47] And so on for the last one. So we have
[03:08:49] only one match for this ID. Then SQL
[03:08:51] going to go again to the customers and
[03:08:53] pick the second one and start matching
[03:08:55] again with the first order. Do we have a
[03:08:57] match? Well, no. Then it's going to go
[03:08:59] to the second. Well, now we have a
[03:09:01] match. So SQL going to be happy. the
[03:09:03] condition is fulfilled and we will see
[03:09:05] the results. So we're going to see the
[03:09:07] first name and as well the order
[03:09:08] information for this customer in the
[03:09:10] output. It's going to keep searching. So
[03:09:13] we don't have a match as well here. So
[03:09:15] that's it. Now for the third customer as
[03:09:17] well from the start there match no to
[03:09:20] the second to the third and here we have
[03:09:21] a match. So it's going to go and show
[03:09:23] this informations since there is a
[03:09:26] match. So the customer three George with
[03:09:28] the order from this customer order ID
[03:09:31] and the sales as well in the output. Now
[03:09:33] it's going to go and keep continuing the
[03:09:35] search. Well, we don't have any match.
[03:09:37] Then it's still going to go to the
[03:09:39] fourth customer and start matching. Do
[03:09:41] we have here an ID? Do we have here a
[03:09:43] match? Well, no. Then the second, third,
[03:09:45] and fourth. We don't have any order for
[03:09:48] this ID. There is no match at all. And
[03:09:50] since we are saying inner join then SQL
[03:09:52] will not allow to show the data of this
[03:09:55] customer in the results. There is no
[03:09:57] match and SQL going to totally ignore
[03:09:59] this customer. Then we're going to go to
[03:10:01] the last one and start as well matching
[03:10:03] this ID with the orders. Well, there is
[03:10:05] no match as well. SQL going to go and
[03:10:07] exclude this user from the results. So
[03:10:10] this is exactly how the inner join
[03:10:12] works. it start from the left side and
[03:10:14] start matching the data on the right
[03:10:16] side and only if there is match the
[03:10:18] result going to be presented in the
[03:10:19] output and this is exactly why we are
[03:10:21] getting this results and how the inner
[03:10:23] join works. So now if you look again to
[03:10:25] the reasons why we are joining tables we
[03:10:27] can say we can use the inner join in
[03:10:29] order to recombine the multiple tables
[03:10:32] into one big picture. So the first use
[03:10:34] case and as well we can use the inner
[03:10:36] join in order to filter the data. So
[03:10:38] since we are saying only the matching
[03:10:40] data that means we are filtering the
[03:10:42] data we are checking the existence of
[03:10:44] the records in another table. So you can
[03:10:46] use inner join either to combine data
[03:10:49] from multiple tables or you can use it
[03:10:51] as well only for filtering purposes only
[03:10:53] to check the existence of your rows. So
[03:10:55] this is usually the two use cases of
[03:10:57] inner. All right. So that's all about
[03:10:59] the first type the inner join. Next
[03:11:01] we're going to talk about the left join.
[03:11:03] So we're going to focus on the left
[03:11:04] side. So let's go.
[03:11:09] Okay. So now what is exactly left join?
[03:11:11] This type going to returns all the rows
[03:11:14] from the left table and only the
[03:11:16] matching from the right table. So now if
[03:11:18] you look again to our two circles A and
[03:11:20] B. What do we need from the left table?
[03:11:22] We want to see everything all the rows
[03:11:25] all the data. So that means we will get
[03:11:27] a full circle. And now from the right
[03:11:29] table we want to get only the matching
[03:11:32] data. So that means we don't want to see
[03:11:34] everything from the table B. We want to
[03:11:36] see only the records that has match to
[03:11:38] the table A. So that means my friends
[03:11:41] the left table has here more priority.
[03:11:43] This is the primary source of your data.
[03:11:46] The main source we cannot miss anything.
[03:11:48] This is very important. We want to see
[03:11:49] all the data. But from the table B, it
[03:11:51] is a secondary source of data and we are
[03:11:54] joining it only to get an additional
[03:11:56] data. So I don't want everything. I want
[03:11:58] only the data that has matched to the
[03:12:00] lift table. So this is what we mean with
[03:12:02] a lift join. Now if you look to the
[03:12:04] syntax it's going to be very similar to
[03:12:05] the inner join. So we start from the
[03:12:07] left table the A. Then we say left join
[03:12:10] the right table B and then the same
[03:12:12] condition using keys. So here we just
[03:12:14] switch the type. Instead of inner we
[03:12:16] have now left. But now here with the
[03:12:18] syntax we need to be very careful. The
[03:12:20] order of the tables now is very
[03:12:22] important. You have to start from the
[03:12:24] correct table. So you have to mention
[03:12:26] the left table exactly in the from
[03:12:28] clause and then you join it with the
[03:12:31] right table. So in the join you have to
[03:12:33] specify the right table. If you don't do
[03:12:35] it like this then you will not get all
[03:12:37] the data from a and you will not get the
[03:12:39] results that you are expecting. So this
[03:12:41] is what we mean with the left join.
[03:12:42] Let's go back to scale in order to
[03:12:44] practice. All right. So now we have the
[03:12:46] following task. It says get all
[03:12:48] customers along with their orders
[03:12:50] including those without orders. So again
[03:12:53] here we need the data from two tables
[03:12:55] the customers and orders and we want
[03:12:57] everything in one result. So that means
[03:12:58] we have to go and join the data. And now
[03:13:00] the task says includes those without
[03:13:03] orders. So that means I want to see
[03:13:05] everything the matching data and the
[03:13:07] unmatching data from the table
[03:13:09] customers. And by looking to our query
[03:13:11] this is not working because we are not
[03:13:13] getting everything right. We are getting
[03:13:14] only the customers that has match in the
[03:13:17] table orders. And this is not of course
[03:13:19] fulfilling the task. So now if you read
[03:13:21] the task you can understand the main
[03:13:23] table here is the customers. We are not
[03:13:25] speaking about to see all the orders and
[03:13:28] not missing any order and the orders
[03:13:29] here is only for additional
[03:13:31] informations. So now in order to not
[03:13:32] lose any data for the customers we make
[03:13:35] sure we start from the table customers.
[03:13:37] So that means now the customers on the
[03:13:39] left side and now after that instead of
[03:13:41] inner join this is not good thing for
[03:13:43] this task. We're going to say left join
[03:13:45] and with that we guarantee we will get
[03:13:47] all the data from the customers. Now we
[03:13:49] say left join orders and of course the
[03:13:52] condition going to stay like this. This
[03:13:54] is how we are connecting the two tables.
[03:13:56] So actually that's it. Let's go and
[03:13:58] execute it. And now by looking to the
[03:14:00] result you can see that we have now five
[03:14:02] customers even the customers that didn't
[03:14:04] place any orders. So you can see Martin
[03:14:06] and Peter they don't have any order ID.
[03:14:09] So that means they didn't order
[03:14:11] anything. And as you can see is showing
[03:14:13] us nulls when there is no match. So with
[03:14:16] that we have solved the task. Now my
[03:14:17] friends one more thing as I told you the
[03:14:19] order of the tables is very important
[03:14:21] because the customer is now the left
[03:14:24] table because you start from it and the
[03:14:26] second table the orders is the right
[03:14:28] table. Now if you go and switch them
[03:14:30] like this. So we start from the orders
[03:14:32] and then join it with the customers and
[03:14:35] you go execute it you will not get all
[03:14:37] the customers and of course the task is
[03:14:39] now not solved. So as you can see you
[03:14:40] are getting now completely different
[03:14:42] result if you go and switch the tables.
[03:14:44] So be careful where you start and how
[03:14:46] you join the tables in order to get the
[03:14:48] effects that you want. All right. So now
[03:14:50] I'm going to put everything back like
[03:14:52] before. Now let's go and understand how
[03:14:54] is exactly executed this query. Okay. So
[03:14:57] now again we have the data from
[03:14:58] customers and orders and this time we
[03:15:00] are doing the lift join. So now let's
[03:15:02] see how is going to do it. So going to
[03:15:04] say okay we need the ID and the first
[03:15:05] name and we will get that as well in the
[03:15:08] results and from the right table we need
[03:15:09] only those two informations the order ID
[03:15:12] and the sales in the output. So those
[03:15:14] are the columns that we need. So now SQL
[03:15:16] in the left join going to do it a little
[03:15:18] bit differently. It's going to start as
[03:15:19] well from the lift table from the
[03:15:21] customers. But this time going to go and
[03:15:24] immediately put the result in the output
[03:15:27] without like trying to match anything
[03:15:29] and to check whether the data exist or
[03:15:30] not because it doesn't matter not doing
[03:15:33] any validation whether the customer
[03:15:35] exist in the orders. Since it's lift
[03:15:37] join is still going to show all the data
[03:15:38] from the lift table. So there will be
[03:15:41] like no check. But now as a next step in
[03:15:43] order to get the order ID and the sales
[03:15:45] SQL will start searching. So SQL going
[03:15:47] to go over here and start searching
[03:15:49] where do we have a customer with this
[03:15:51] ID? Well, it's going to be the first
[03:15:52] order. We're going to get the order ID
[03:15:54] and as well the sales informations and
[03:15:57] we will see that in the output. So
[03:15:59] that's it for the first one. Now it's
[03:16:01] going to go to the second row and the
[03:16:02] same thing going to happen immediately.
[03:16:04] The SQL going to go and put the result
[03:16:06] in the output without checking anything.
[03:16:09] And then in order to get the order data,
[03:16:11] it will start searching for this ID. So
[03:16:14] we have it here in the second row. We
[03:16:15] have the order ID and the sales. And
[03:16:18] it's still going to put those results to
[03:16:19] the output. So the search for the third
[03:16:22] one immediately going to put everything
[03:16:24] in the output. And then start searching
[03:16:27] for orders with this ID. We have it over
[03:16:30] here. So this order belongs to the user
[03:16:32] ID number three. So far we are getting
[03:16:34] the same result as the inner joint. But
[03:16:36] we are not done yet. Now exactly count
[03:16:38] the difference this guy going to go and
[03:16:40] get Martin and put it immediately in the
[03:16:42] output and start searching for an order
[03:16:44] with this ID. So do we have any order
[03:16:47] with the ID number four? Well, we don't
[03:16:49] have anything this time. SQL of course
[03:16:51] will not go and exclude the ID number
[03:16:54] four. It's going to leave it. But in SQL
[03:16:56] if there is no match, we still have to
[03:16:58] have something in the output. So SQL
[03:17:00] going to go and say the output going to
[03:17:03] be null like this. We don't know it is
[03:17:06] unknown. And the same thing for the
[03:17:08] sales. So in the lift join if there is
[03:17:09] no match you will see nulls. The same
[03:17:11] thing for the next customer for better.
[03:17:14] So SQL will go and put the result
[03:17:16] immediately in the output and then start
[03:17:18] searching the orders. So do we have
[03:17:20] anything for the ID number five? We
[03:17:22] don't have anything. That's why SQL
[03:17:25] going to go and present nulls as well in
[03:17:27] the output. And that's why you saw nulls
[03:17:29] in the output because those customers
[03:17:32] don't have any orders. So this is
[03:17:34] exactly the effect of the lift join. you
[03:17:35] will get everything from the lift table
[03:17:37] and only the matching stuff on the right
[03:17:39] side and if there is something not
[03:17:41] matching you will get nulls. So that's
[03:17:43] it is this is how scale execute the left
[03:17:45] join okay so now back to this use cases
[03:17:47] of joins if I think about lift join I
[03:17:50] can use it in order to recombine data in
[03:17:52] order to build this big picture and as
[03:17:54] well in the second use case where we use
[03:17:56] it in order to get an extra information
[03:17:58] from another table. So we have a main
[03:18:00] table and secondary table. So we use it
[03:18:03] for both use cases and as well in the
[03:18:05] third use case only with a twist that
[03:18:07] we're going to learn later. So that's
[03:18:08] all about the left join. Now we have
[03:18:10] another type that is exactly the
[03:18:12] opposite of the lift join. We have the
[03:18:14] right join. So now let's understand what
[03:18:17] this
[03:18:20] means. Okay. So now what is exactly
[03:18:23] right join? This is the total opposite
[03:18:25] of the left join. So this tag going to
[03:18:27] returns all the rows from the right
[03:18:29] table and only the matching from the
[03:18:32] left table. So here the main table the
[03:18:34] main focus is the right table. So SQL
[03:18:36] going to get you all the rows everything
[03:18:38] from the table B the right table but
[03:18:41] from the left side we will get only the
[03:18:43] matching data. So that means in the left
[03:18:45] sides you will get only the data that
[03:18:47] has a match on the right side and with
[03:18:50] that the right table going to be the
[03:18:52] primary the main source of your data. So
[03:18:54] it is very important table but the lift
[03:18:56] table is not that important. You are
[03:18:58] just joining it in order to get
[03:19:00] additional data. So again about the
[03:19:02] syntax it's not that crazy. All what you
[03:19:04] have to do is to change the join type.
[03:19:06] So instead of left you say right join
[03:19:09] and again here the order of the tables
[03:19:11] is very important because the side here
[03:19:13] makes a difference. So we start from the
[03:19:15] left table A and then right join it to
[03:19:18] the table B. So it sounds very similar
[03:19:20] to the left join. We are just switching
[03:19:22] things. Now let's go back to scale. in
[03:19:23] order to practice. Okay my friends, so
[03:19:25] now we have the following task and it
[03:19:27] says get all customers along with their
[03:19:29] orders including orders without matching
[03:19:33] customers. So again we have the
[03:19:35] customers and the orders and we are
[03:19:36] doing the join but here the condition is
[03:19:38] different. We want to see all the orders
[03:19:41] even if they don't have a matching
[03:19:43] customer. So that means I would like to
[03:19:45] see everything from the table orders and
[03:19:47] the customers table here is only like
[03:19:49] supporting and helping. So the main
[03:19:51] table that we are focusing on is in the
[03:19:53] orders. We want to see everything and
[03:19:55] from the customers only the matching and
[03:19:57] if you are looking currently to the
[03:19:59] results you can see we are seeing only
[03:20:01] three orders right but in the original
[03:20:04] table if you go back over here you can
[03:20:06] see that we have four orders. So we are
[03:20:08] currently using this query not seeing
[03:20:11] all the orders. So now how we going to
[03:20:12] solve it? If you start from the table
[03:20:14] customers you can say you know what
[03:20:16] instead of left join we're going to say
[03:20:18] right join. And with that you're going
[03:20:20] to guarantee you will get everything
[03:20:22] from the table orders. But now the left
[03:20:25] table the customers is not that
[03:20:26] important and you will see the data of
[03:20:28] the customers only if there is a match.
[03:20:30] So doing the right join like this
[03:20:32] guaranteed to see everything whether
[03:20:34] there is match or no match. Now if you
[03:20:36] go and execute it you can see on the
[03:20:38] right side the order ID and the sales
[03:20:40] and we can see now all the orders and on
[03:20:42] the left side the ID and the first name.
[03:20:45] We are seeing only the customers if they
[03:20:47] did order something. And for the orders
[03:20:49] without a known customer, we are getting
[03:20:51] nulls. So with us, you have solved the
[03:20:53] task using the right join. So now my
[03:20:55] friends, you have to go and solve this
[03:20:56] task to get the exact same results. But
[03:20:59] you are allowed to use only the left
[03:21:01] join. So you are not allowed to use the
[03:21:03] right join. So now go pause the video,
[03:21:05] solve the task and meet you
[03:21:08] [Music]
[03:21:11] soon. Now my friends, in SQL there is
[03:21:14] always alternatives on how to solve a
[03:21:16] task. So now if you want to get all the
[03:21:18] data from B and only the matching from
[03:21:20] A, you can do it like we have done using
[03:21:22] the right join. But if you go and switch
[03:21:24] the sides and you make the table B as a
[03:21:27] left table and the table A as a right
[03:21:29] table, you can do that of course in SQL.
[03:21:31] But you have to switch the join type. So
[03:21:33] instead of right, we have to use left
[03:21:35] now since the B table now on the left
[03:21:38] side and as well you have to switch the
[03:21:40] order. So you start from the B table and
[03:21:43] then you say left join the A table. and
[03:21:46] of course the same join condition. And
[03:21:47] if you do that, you will get the exact
[03:21:49] same result as the left query. So if you
[03:21:52] just switch the tables and as well
[03:21:54] switch the join type, you can get the
[03:21:56] same results. And to be honest, my
[03:21:58] friends, I don't like the right join.
[03:22:00] It's just in the last 10 years, I always
[03:22:02] tend to start from a table and then use
[03:22:04] a left join. And from my point of view,
[03:22:06] the left join is way more famous than
[03:22:09] the right join. And I think I never used
[03:22:11] a query where I'm using a right join. So
[03:22:14] my advice for you always try to skip the
[03:22:16] right join and stick with the left join
[03:22:18] just get the order of the tables in the
[03:22:20] query correct and you will get the same
[03:22:22] results. So with that you know an
[03:22:23] alternative for the right join. Now all
[03:22:25] what you have to do is to go and switch
[03:22:26] the right to left. Uh this is not enough
[03:22:29] because if I go and execute it. So now
[03:22:31] all what I have to do is to go and
[03:22:33] switch the tables like this. So we start
[03:22:36] from the table orders because I want to
[03:22:38] see everything from the orders and then
[03:22:40] lift join it with the customers. And of
[03:22:42] course we don't have to change anything
[03:22:43] here. It doesn't matter the order
[03:22:45] because we have an equal operator here.
[03:22:47] What is very important here is where you
[03:22:49] start from which table and what is the
[03:22:52] table that you are joining with. So if
[03:22:54] you go and execute it, you will get the
[03:22:56] exact same results. So now I'm seeing
[03:22:58] all the orders. I'm not missing anything
[03:23:00] and only the matching customers. And I
[03:23:03] prefer this way solving this task
[03:23:05] instead of using the right join. All
[03:23:06] right. So that's all about the right
[03:23:08] join. Next we're going to combine
[03:23:10] everything. We're going to talk about
[03:23:11] the full join. So let's
[03:23:16] go. Okay. So now what is exactly a full
[03:23:19] join? If you use it, SQL returns
[03:23:21] everything all the rows from both
[03:23:23] tables. So now if you check again our
[03:23:25] circles from the left table, we want to
[03:23:27] get everything all the rows. So you will
[03:23:30] get the whole circle and as well from
[03:23:32] the right table you want to get
[03:23:33] everything all the rows the whole
[03:23:35] circle. So that you want to get
[03:23:37] everything the matching the unmatching
[03:23:39] all the data from left and right. Now
[03:23:41] let's check the syntax. It's going to be
[03:23:43] very simple. The joint type here going
[03:23:44] to be a full join. And the full join it
[03:23:47] is very similar to the inner join. You
[03:23:49] remember the order of the tables is not
[03:23:51] important at all. So there is here no
[03:23:53] main table and secondary table. Both of
[03:23:55] the tables are important and it doesn't
[03:23:58] matter in your query where you start.
[03:23:59] You can start from A full join B or you
[03:24:02] can start from B then full join A. you
[03:24:04] will get the exact same results. It
[03:24:06] sounds simple. Let's go to SQL and
[03:24:08] practice the full join. All right. So
[03:24:10] now we have the following task and it
[03:24:11] says get all customers and all orders
[03:24:14] even if there is no match. So now again
[03:24:16] we need the data from customers and
[03:24:18] orders. But now of course which type
[03:24:20] we're going to use? It says even if
[03:24:22] there is no match but it didn't say no
[03:24:24] match from orders or customers. So you
[03:24:26] can understand from this task we are not
[03:24:28] focusing only on the orders or the
[03:24:30] customers. Both of them are equally
[03:24:32] important and we need all the data. So
[03:24:34] that means we need all the data from
[03:24:36] left, all the data from right and we can
[03:24:38] go and use the full join. So now we have
[03:24:40] this query over here. We are starting
[03:24:42] from customers and then joining to
[03:24:44] orders. But now instead of having left,
[03:24:46] we're going to say full join. So now
[03:24:48] let's go and just execute it. Now if you
[03:24:50] are looking to the left side, you can
[03:24:52] see we are getting all the customers,
[03:24:54] right? So we have our five customers and
[03:24:56] if you are looking to the right, you can
[03:24:58] see all our orders. So with that we have
[03:25:00] everything from left and everything from
[03:25:02] right and the matching data is just side
[03:25:05] by side in the results and if there is
[03:25:07] no match we are getting nulls. So
[03:25:08] actually with that we have solved the
[03:25:10] task and again it doesn't matter how you
[03:25:12] start. You can start from the orders and
[03:25:14] then join it to the customers and you
[03:25:16] will get the exact same results. So you
[03:25:19] are getting exactly the same data. Now
[03:25:20] let's go and understand exactly how is
[03:25:22] executed the full join. Okay again we
[03:25:25] have the data of the customers and the
[03:25:26] orders and our full join. So now we're
[03:25:28] still going to identify those columns
[03:25:30] that we want to see in the results. So
[03:25:32] the ID and the first name, the order ID
[03:25:34] and the sales informations to the
[03:25:36] output. Now it's still going to start
[03:25:38] from the left table since it is started
[03:25:39] with the customers. It's still going to
[03:25:41] take simply everything from the left
[03:25:43] table and present it in the output.
[03:25:45] Since it is full join, we want to see
[03:25:46] all the data from the left side. And now
[03:25:48] start searching for matches from the
[03:25:50] right table. So let's start with the
[03:25:52] first customer. And as usual, we will
[03:25:54] get the order from the customer number
[03:25:57] one. And the same thing for the second
[03:25:59] customer, we have as well here match. So
[03:26:01] we will get as well. It's like that lift
[03:26:03] join. And for the third one, we have as
[03:26:05] well a match. And we're going to have it
[03:26:07] like this. And since we don't have
[03:26:09] orders for those two customers, we will
[03:26:11] get as well nulls in the outputs. So
[03:26:14] scale going to mark it with null. The
[03:26:16] same thing over here. And as well for
[03:26:18] the last customer. So we will get nulls
[03:26:20] for those two customers. And now of
[03:26:22] course SQL will not stop here otherwise
[03:26:24] we will get a left join effect. Now SQL
[03:26:27] going to start looking at the right side
[03:26:29] to find any order that is not in the
[03:26:31] output. So SQL going to see okay the
[03:26:33] first order is in the output. The second
[03:26:35] one is as well in the output. The third
[03:26:37] but the fourth one is not in the
[03:26:39] results. So SQL going to take this
[03:26:41] result and put it in the output. So this
[03:26:44] order has no match at all from the left
[03:26:46] side. And with that if you are looking
[03:26:48] to the right side you can see SQL going
[03:26:50] to be happy because we have all the
[03:26:52] orders from the right table. And of
[03:26:54] course SQL will not leave it like this.
[03:26:56] Instead of that SQL going to show nulls
[03:26:58] on the left side. So there is no ID and
[03:27:00] there is no first name. So this is
[03:27:02] exactly why we got this results. And
[03:27:04] this is how SQL executed the full join.
[03:27:07] Okay. Okay. So now if you are looking to
[03:27:08] the use cases I can say you can use the
[03:27:10] full join in order as well to recombine
[03:27:13] the data from multiple tables if you
[03:27:15] don't want to miss anything from all
[03:27:17] four tables all data the matching and
[03:27:19] unmatching data but I don't use it
[03:27:21] usually for data enrichment for the
[03:27:23] second use case and where we can use the
[03:27:25] full join is in the last use case as
[03:27:27] well but with a little twist that we're
[03:27:29] going to learn later. So this is mainly
[03:27:31] where we can use the full join. All
[03:27:33] right. So with that we have covered the
[03:27:35] basic types of joins inner, left, right
[03:27:37] and full join. Those are the classical
[03:27:39] joins on how to combine two
[03:27:44] tables. Now we're going to start talking
[03:27:46] about the advanced SQL joins. And now
[03:27:48] we're going to cover the first part the
[03:27:50] lift anti- join. So let's see what this
[03:27:56] means. Okay. So now what is exactly a
[03:27:58] lift anti- join? Now in this mechanism
[03:28:00] we want to return rows from the left
[03:28:03] side the left table that has no match in
[03:28:06] the right table. So now by looking to
[03:28:07] our two circles from the left table we
[03:28:10] want to see only the unmatching rows. So
[03:28:13] only rows that exist in table A but it
[03:28:16] don't exist in the table B. So if there
[03:28:18] is like matching data we don't want to
[03:28:20] see it. And now from the right table we
[03:28:22] don't want anything. We don't want any
[03:28:24] data. So that means the only source of
[03:28:26] your data going to be the left table.
[03:28:28] And from the right table we don't need
[03:28:29] any data. We are just joining the tables
[03:28:31] to do a check to filter the data. So now
[03:28:34] for the syntax this can be interesting.
[03:28:36] We don't have a special type called left
[03:28:38] anti- join. At least in the SQL server
[03:28:40] we still can create this effect. Since
[03:28:42] we are saying left we can use the type
[03:28:44] left join and then as usual the join
[03:28:46] condition with the keys. But now if you
[03:28:48] leave it like this you will get the
[03:28:50] effect of the lift join. And we don't
[03:28:52] want that because with the lift join you
[03:28:53] will get the complete circle from the
[03:28:55] lift table. But now in order to remove
[03:28:57] the matching data this overlapping in
[03:28:59] the middle what we can do we can use a
[03:29:01] filter and in order to filter the data
[03:29:03] we use the wear clause. So now in order
[03:29:06] to get rid of the matching data we can
[03:29:08] take the key from the right table and we
[03:29:11] say the key must be null. So if the key
[03:29:14] is null so that means there is no match
[03:29:16] on the right side. And if you do it like
[03:29:18] this you will get the effect of the left
[03:29:21] anti-join only the data in the left that
[03:29:23] has no match on the right. So now let's
[03:29:26] go in scale and create this effect.
[03:29:28] Okay. So now we have the following task
[03:29:29] and it says get all customers who
[03:29:31] haven't placed any order. So now by
[03:29:33] looking to this query clearly we are
[03:29:35] focusing on the table customers but we
[03:29:37] want to see the customers that didn't
[03:29:39] order anything. So they are in our
[03:29:41] database but the customers are inactive.
[03:29:44] Now there are like different ways on how
[03:29:45] to solve this task but we're going to
[03:29:47] solve it using the joins. Now let's go
[03:29:49] and start by just writing a very simple
[03:29:51] query where we are selecting everything
[03:29:54] from the table customers. Now you can
[03:29:56] see this is our five customers. And now
[03:29:58] I want to check which of those customers
[03:30:00] didn't order anything yet. Now since we
[03:30:02] are talking about the orders, we can go
[03:30:04] and join it with the table orders. So
[03:30:06] we're going to say lift join the table
[03:30:08] orders as all and then we're going to go
[03:30:10] and connect the tables using the ids
[03:30:12] with the customer ID. So now if you go
[03:30:15] and execute it now we are still seeing
[03:30:17] all the customers because we are using
[03:30:19] the lift join and now we can see the
[03:30:21] orders informations of each customer and
[03:30:23] you can see immediately those two
[03:30:25] customers didn't order anything because
[03:30:28] we are seeing here nulls right so they
[03:30:30] are empty there is no orders now we can
[03:30:32] use this information in order to filter
[03:30:35] the data I just want to see Martin and
[03:30:37] Peter so what you can do we can go and
[03:30:39] say where and all what you have to do is
[03:30:42] to take the key that we are using in
[03:30:43] order to join in the tables this is this
[03:30:45] one over here and say this must be null
[03:30:48] so is null so if you see it like this
[03:30:51] that means you want to see the data if
[03:30:53] the customer ID is null so let's go and
[03:30:56] execute it perfect now you are getting
[03:30:58] the customers who haven't order anything
[03:31:01] and this is exactly the effect that we
[03:31:03] wanted the left anti-join we are getting
[03:31:05] the data from the left side where there
[03:31:08] are no match on the right side so you
[03:31:10] have always to do it in two steps first
[03:31:12] join the data as you normally do using
[03:31:14] the classical joins the lift join and
[03:31:17] then the second step you go and use a
[03:31:19] filter using the wear clause if you do
[03:31:21] it like this you can check for not
[03:31:23] existence and with that we are getting
[03:31:25] the effect of the left anti-join so
[03:31:27] that's it okay so now if you are looking
[03:31:29] to this picture I think you already know
[03:31:31] where we use the lift anti- join we're
[03:31:33] going to use it only in the last use
[03:31:34] case where we are checking the existence
[03:31:37] so if you use the lift join together
[03:31:38] with the where you can check for the
[03:31:40] notexistence of your data in another
[03:31:42] table so This is exactly for this
[03:31:44] scenario. All right. So that's all about
[03:31:46] the left anti- join. Now we're going to
[03:31:48] speak about the exact opposite of that.
[03:31:50] We will cover the right anti- join. So
[03:31:53] it's going to be very similar but we are
[03:31:55] just switching sides. So let's
[03:31:59] go. Okay. So now what is exactly the
[03:32:02] right anti- join? Well, it is the
[03:32:04] opposite of the left anti- join. So we
[03:32:06] want to return the rows from the right
[03:32:09] table that has no match in the left
[03:32:11] table. So again if you are looking to
[03:32:13] our two circles. Now what is important
[03:32:15] is the right table. We want to see only
[03:32:17] the unmatching rows from the right
[03:32:20] table. So only the rows that exist in B
[03:32:23] but not in A. And from the left table we
[03:32:25] don't need anything. So no data is
[03:32:27] needed and that means the only source of
[03:32:29] data comes from the right table and you
[03:32:32] are using the left table as a filter as
[03:32:34] a lookup just in order to check the
[03:32:36] existence. So now the syntax of that
[03:32:38] going to be very similar to the left
[03:32:39] anti- join. So we don't have a special
[03:32:41] type called right anti-join. We have to
[03:32:44] use the classical one the right join.
[03:32:46] But if you do that you will get
[03:32:47] everything from the right table. And now
[03:32:49] in order to get rid of the matching data
[03:32:51] in the middle we use a filter. We use
[03:32:53] the wear clause where we say we are
[03:32:55] interested only on the unmatching data.
[03:32:58] So we take the key from the left table
[03:33:00] and we say the key from left is null.
[03:33:02] And if you do that you will get rid of
[03:33:04] any matching data. Is null means there
[03:33:06] is no match. And again here the same
[03:33:08] thing the order of the tables is very
[03:33:10] important since here we are talking
[03:33:12] about sides and you have to do it
[03:33:14] correctly. Okay. So now the task says
[03:33:16] get all orders without matching
[03:33:19] customers. So now it is exactly the
[03:33:21] opposite. We want to see all the orders
[03:33:23] that don't have a valid customer. So
[03:33:25] this is really bad scenario. You have in
[03:33:27] your business orders without a valid
[03:33:29] customers. So let's see how we can
[03:33:30] discover that using SQL joins. Now as
[03:33:32] you can see we are focusing completely
[03:33:34] on the orders. It's not the customers
[03:33:36] anymore. And we want to see only the
[03:33:38] orders where there is no match with the
[03:33:39] customers. So now again here we have two
[03:33:41] steps. The first step we're going to go
[03:33:43] and do the normal join. So using either
[03:33:45] the left or the right join. Now by
[03:33:47] looking to this query you can leave it
[03:33:49] like this where you can start from the
[03:33:50] customers. But if you want to fully
[03:33:52] focus on the orders you have to switch
[03:33:54] this from left to right. And with that
[03:33:56] you will get all the orders and only the
[03:33:58] matching customers. And let's go and
[03:34:00] remove this workloads from here first.
[03:34:01] So I'm just adding comments. And with
[03:34:03] that SQL going to totally ignore this
[03:34:06] line of code. So let's go and execute
[03:34:08] it. Now you can see we are getting all
[03:34:09] the orders right and data from customers
[03:34:12] only if there is a match. And now of
[03:34:14] course this is not the task. We don't
[03:34:15] want to see all the orders. We want to
[03:34:17] see only the orders where we don't have
[03:34:19] a match from the customers. So if you
[03:34:21] look to this those three orders they are
[03:34:24] okay. They are totally fine. We are
[03:34:25] finding customers for them. So they have
[03:34:27] valid customers. But this order here is
[03:34:30] really bad. So there is no valid
[03:34:31] customer for this order and now our task
[03:34:34] to show only this type of orders in the
[03:34:36] result. Now what we have to do we have
[03:34:38] to use the workclass in order to get
[03:34:40] exactly the effects. So this time we're
[03:34:42] going to say if the ID of the customer
[03:34:44] here. So here we're going to say the ID
[03:34:46] of the customer from the table customers
[03:34:49] must be null. So we're going to remove
[03:34:50] this here and take the key join from the
[03:34:53] customer and we are saying this ID must
[03:34:56] be null. So let's go and execute it.
[03:34:58] Perfect. With us we have solved the task
[03:35:00] and we are getting the effect of the
[03:35:02] right anti- join and we are getting now
[03:35:04] those orders that don't have any
[03:35:06] customers. So we have solved the task.
[03:35:08] Now my friends you have to go and solve
[03:35:10] this task without using the right join
[03:35:13] but still you have to get the same
[03:35:14] effects. You want to get exactly those
[03:35:16] orders without customers. So pause the
[03:35:19] video and go solve the task.
[03:35:23] [Music]
[03:35:26] Now again as you know me I don't like
[03:35:28] the right joins. We can create the same
[03:35:30] effects if you switch the sides of the
[03:35:32] table. So if you say the B table now on
[03:35:34] the left side and the A on the right
[03:35:36] side then we will get the same effect if
[03:35:38] you go and switch the type of join from
[03:35:41] right to left and you go just switch the
[03:35:43] tables. So you start from the B table
[03:35:45] since it's on the left side and then
[03:35:47] join it with the A. And we still say of
[03:35:49] course in our work condition where the
[03:35:51] data from A is null. So there is no
[03:35:53] match. So if you do this you will get
[03:35:55] the exact same results like the lift
[03:35:57] query by using the lift join and just
[03:35:59] switching the tables. So you will get
[03:36:01] the same results and with that you know
[03:36:03] that in scale we have always
[03:36:04] alternatives. I hope that you are done.
[03:36:06] So it's very simple what you're going to
[03:36:07] do. We're going to go and switch the
[03:36:09] joins and since the orders is the main
[03:36:12] table we're going to start first from
[03:36:14] the table orders. So we are putting it
[03:36:16] on the left side and then the right
[03:36:18] table going to be the customers. And of
[03:36:20] course the condition going to stay as it
[03:36:21] is. We want to see the orders where
[03:36:23] there is no customer. So we don't have
[03:36:25] to switch anything here or in the join
[03:36:28] key. So let's go and execute it. With
[03:36:29] that you are getting the same exact
[03:36:31] results. Since we are using here the
[03:36:33] star, it's always starts from the left
[03:36:35] table and show the data from the right
[03:36:37] table. But still the result is valid. We
[03:36:39] are getting this type of orders without
[03:36:42] matching customers. And I prefer this
[03:36:44] way. All right. So now with that we have
[03:36:45] the left, the right and now of course
[03:36:47] what is next? We will get the full. So
[03:36:50] let's speak about now the full anti-join
[03:36:52] in SQL. Let's
[03:36:56] go. Okay. So now what is exactly a full
[03:36:59] anti- join? Well, this time we don't
[03:37:01] have sides. We want to return only the
[03:37:03] rows that don't match in either tables.
[03:37:06] So what this means? If you are looking
[03:37:08] to the left circle, we want only the
[03:37:10] unmatching rows. So we don't want the
[03:37:12] whole circle. We want only the data that
[03:37:14] exist in A but it don't exist in B on
[03:37:18] the right table. Sounds like the left
[03:37:19] ant join but since we are saying full
[03:37:21] then you have to do the same thing on
[03:37:23] the right side as well. So on the right
[03:37:24] table we want only the unmatching rows.
[03:37:27] So we want to see in the result the data
[03:37:29] that is in B but don't have a match from
[03:37:32] A. So it's exactly the opposite. And if
[03:37:34] you look to this then that means we want
[03:37:36] to see only the unmatching data and this
[03:37:38] is exactly the opposite effect of the
[03:37:41] inner join. In the inner join we were
[03:37:43] interested only on the matching data
[03:37:45] only when there is like overlapping. But
[03:37:47] now with the full anti-join it is
[03:37:48] exactly the opposite. We don't want to
[03:37:50] see the matching data. We want to see
[03:37:52] everything else the unmatching data. So
[03:37:54] how we going to write this query? Again
[03:37:56] here we don't have a special type called
[03:37:58] full anti-join. We will use the help of
[03:38:00] the classical full join. So the basic
[03:38:02] one. So you start from a full join b and
[03:38:05] then the same key. But now what is
[03:38:06] interesting is about the where
[03:38:08] condition. Now we have like two
[03:38:09] conditions right? So now in order to get
[03:38:11] all data from A that has no match in B,
[03:38:14] you have to make a filter where you say
[03:38:16] the key from the B table must be null.
[03:38:18] And now since we want the exact same
[03:38:20] thing from the right table, we want all
[03:38:22] the data in B that has no match in A.
[03:38:24] You have to say as well the key from the
[03:38:27] A table must be null. So now we have
[03:38:29] here like two conditions. And in SQL if
[03:38:31] you have like two conditions in the work
[03:38:33] clause, you have here two options either
[03:38:35] use and operator or the over operator.
[03:38:38] So now the one that we're going to use
[03:38:40] here is the or operator. So either the
[03:38:42] key from right is empty or the key from
[03:38:44] left is empty. If you do it like this,
[03:38:46] you will get the effect of the full
[03:38:49] anti- join. And of course since here
[03:38:50] both sides are equal then the order of
[03:38:53] the tables as well here is not that
[03:38:55] important. So you can say from A full
[03:38:57] join B or from B full join A. It doesn't
[03:39:00] matter. So now let's go back to scale in
[03:39:02] order to create this effect. Okay.
[03:39:04] Instead we have the following task and
[03:39:05] it says find customers without orders
[03:39:08] and orders without customers. So if you
[03:39:10] are looking to this this means we want
[03:39:12] to see only the unmatching data from
[03:39:14] customers and as well from orders. There
[03:39:17] is no main table and secondary table.
[03:39:19] Both of them are equally important. So
[03:39:21] now since we are talking about the
[03:39:22] unmatching data and the anti-join we
[03:39:24] have to do it in two steps. The first
[03:39:26] step we're going to do the classical
[03:39:27] join and then we focus on the wear
[03:39:29] clause. So let me remove the wear clause
[03:39:31] to make it as a comment. Now since we
[03:39:33] want the data from left and right, we're
[03:39:35] going to go and use the full join. So
[03:39:36] let's go and execute it. Now you can see
[03:39:38] we are getting the effect of the full
[03:39:40] join. We are getting all the orders and
[03:39:42] as well all the customers. But now we
[03:39:44] are interested only on the strange cases
[03:39:47] where they are like orders without
[03:39:49] customers like this one here and as well
[03:39:51] customers without orders. So that means
[03:39:53] the first three rows they are not really
[03:39:55] interesting for us because it is boring.
[03:39:57] We have here matching data and this is
[03:40:00] totally fine but we are not focusing on
[03:40:02] that now. We are focusing only if there
[03:40:04] is like missing data from left or from
[03:40:06] right. As you notice I'm saying or and
[03:40:09] this is very important because we're
[03:40:10] going to use the or operator. So now
[03:40:11] let's focus on getting this scenario
[03:40:13] over here. We want to get an order
[03:40:15] without a customer. So that means the
[03:40:17] customer ID must be null. And we have it
[03:40:20] already here. So we are saying where the
[03:40:22] ID of the customer is null. So if I go
[03:40:25] and execute it, I will get only one
[03:40:27] records only this one over here. But as
[03:40:29] well I want to get the opposite
[03:40:31] scenario. So in this scenario, the
[03:40:33] customer ID must be null. So we're going
[03:40:36] to say or the customer
[03:40:38] ID in the orders is null or we can do it
[03:40:42] like side by side like this. Either the
[03:40:44] right side is null or the left side is
[03:40:47] null. So if you go and execute it, you
[03:40:49] will get the effect of the full
[03:40:50] anti-join. And with that we are finding
[03:40:52] the customers without orders and orders
[03:40:55] without customers. I think this is
[03:40:57] really fun and as well really easy. So
[03:40:59] this is how we do the full anti- join.
[03:41:01] All right. So now if you are looking to
[03:41:02] the use cases we use the full anti- join
[03:41:04] again exactly for the last use case in
[03:41:07] order to check the existence. So if you
[03:41:09] combine the full with the where you can
[03:41:11] check the existence or the notexistence
[03:41:13] of your data in another table. So this
[03:41:16] is exactly the scenario for that.
[03:41:21] Okay, my friends, now we have a bonus
[03:41:23] section where I'm going to challenge you
[03:41:25] to solve the following task without
[03:41:27] using an inner join. So, it says, "Get
[03:41:29] all customers along with their orders,
[03:41:31] but only for customers who have placed
[03:41:34] an order, but without using an inner
[03:41:37] join." So, pause the video now and go
[03:41:39] and solve this
[03:41:40] [Music]
[03:41:42] task. Okay, so now let's see how we're
[03:41:44] going to solve this. We want the
[03:41:46] customers, the orders, blah blah blah.
[03:41:48] But we want only the customers who have
[03:41:50] placed an order. Previously, we have
[03:41:52] used the inner join in order to solve
[03:41:53] this task. But this time, we are not
[03:41:55] allowed to use it. So, let's go and
[03:41:57] solve it. This is how I'm going to do
[03:41:58] it. Select star from table customers.
[03:42:01] Can't give it the alias. So, now I'm
[03:42:03] getting all the customers, but I am
[03:42:05] interested only the customers who have
[03:42:07] placed an order. So, as we know before
[03:42:09] there's like two customers didn't order
[03:42:11] anything, and we don't want to see them
[03:42:13] in the final results. Now how we will
[03:42:15] get that? Well, we can use the help of
[03:42:17] the table orders in order to check the
[03:42:19] existence of our customers there. And of
[03:42:21] course, I'm not allowed to use the inner
[03:42:23] join. So I'm going to go and use a left
[03:42:24] join with a table orders and then
[03:42:27] combine them as usual. Nothing new with
[03:42:29] the customer ID. So now let's go and
[03:42:31] execute it. As you can see, we are doing
[03:42:33] it step by step. You don't have to rush
[03:42:35] everything in one go. So you start
[03:42:37] simple, check the results and decide on
[03:42:39] the next step. So now by looking at
[03:42:41] these results I want to get those three
[03:42:43] customers because they have ordered
[03:42:45] something and we are seeing data about
[03:42:47] their orders and I don't want to get in
[03:42:49] the result the last two. So again we
[03:42:51] still can use the customer ID from the
[03:42:53] right table in order to decide which
[03:42:56] data going to stay in the result and
[03:42:57] which data should be filtered. We're
[03:42:59] going to go and use the wear clause and
[03:43:01] then the key from the orders and this
[03:43:03] time we're going to say is not null. I
[03:43:06] know we didn't learn yet about the not
[03:43:08] and the logical operators but using the
[03:43:11] not null it means there should be data
[03:43:13] inside the column it must not be null if
[03:43:16] you do it like this and execute you will
[03:43:18] get the exact effect as the inner join.
[03:43:21] So as you can see as you are joining the
[03:43:22] tables using the left join you can
[03:43:25] control what you want to see using the
[03:43:27] wear clouds using the filter and this is
[03:43:29] how you can solve this task without
[03:43:30] using an inner join. Okay, so with that
[03:43:33] we have covered all those three
[03:43:34] scenarios in order to find the
[03:43:36] unmatching data. Left, right, full and
[03:43:38] joints. Now we can speak about one crazy
[03:43:41] join. We call it the cross join. This
[03:43:43] one is totally different from all other
[03:43:45] types that we have learned. So let's
[03:43:47] understand exactly what is the cross
[03:43:49] join. Let's
[03:43:53] go. So now what is exactly a cross join?
[03:43:56] Now in some scenarios we want to combine
[03:43:59] every row from the left, every row from
[03:44:02] the right. So that means I want to see
[03:44:04] all the possible combinations from both
[03:44:06] tables. So we are doing something called
[03:44:08] like cartesian join. So now if you look
[03:44:10] at our two circles, we want everything
[03:44:13] from A and as well everything from B. So
[03:44:16] that means I want to see everything from
[03:44:18] A combined with everything with B. So in
[03:44:20] this example, we have two rows in A and
[03:44:22] three rows in B. If you do a cross join,
[03:44:25] you will get six possible combinations
[03:44:27] by just multiplying the number of rows
[03:44:29] between A and B. So be careful using the
[03:44:31] cross join. If you use it, you will get
[03:44:33] like crazy number of rows in the results
[03:44:35] and you're going to make the database
[03:44:37] really busy finding out the result for
[03:44:39] you. So now about the syntax, it's going
[03:44:40] to be the easiest. So you start as usual
[03:44:42] from one of those tables, the A for
[03:44:44] example, and then you say cross join B.
[03:44:47] So now my friends, if you look at this,
[03:44:48] you can see it's not like the previous
[03:44:50] joins that we have done. We have always
[03:44:52] before talked about unmatching rows,
[03:44:54] matching rows and so on. But here we
[03:44:56] don't care at all about whether the data
[03:44:58] is matching or not. I just want to see
[03:45:00] all the possible combinations
[03:45:02] everything. So since we don't care about
[03:45:04] matching the two tables, we don't have
[03:45:06] to specify any condition. So there is no
[03:45:08] need to use the keyword on because we
[03:45:11] don't need any condition. So that's it.
[03:45:12] You just say cross join B and the magic
[03:45:15] can happen. So this is a cross join.
[03:45:16] Let's go to SQL to try that. Okay. So
[03:45:19] now we have the following task. It says
[03:45:20] generate all possible combinations of
[03:45:23] customers and orders. So that means we
[03:45:25] want everything with everything using
[03:45:27] the cross join and this going to be very
[03:45:29] simple. So we're going to start with
[03:45:31] select star from whatever table. So you
[03:45:33] can start from the customers and then
[03:45:35] you say cross join orders. That's it.
[03:45:39] Very simple. Let's go and execute it. So
[03:45:41] now as you know we have five customers
[03:45:42] and four orders. And if you multiply
[03:45:44] them you will get in the results 20
[03:45:46] rows. So now we are getting everything
[03:45:48] with everything. even if the data is not
[03:45:51] matching at all. So you can see for
[03:45:53] example the orders here. So this is one
[03:45:55] order that belongs only to one customer
[03:45:57] the customer ID one. So it is an order
[03:46:00] from actually Maria but still we are
[03:46:02] seeing this same order with the other
[03:46:04] customers since we want to combine
[03:46:06] everything with everything. So there are
[03:46:07] no rules. The same thing for the next
[03:46:09] set. So this is the second order
[03:46:11] actually belongs to John but we are
[03:46:13] seeing this order with all customers. So
[03:46:15] that's it. This is how the cross join
[03:46:18] works. And now you might ask me why we
[03:46:19] have this. It makes no sense, right?
[03:46:21] Well, my friends, I rarely use it. But
[03:46:23] sometimes if I want to generate like
[03:46:25] test data or maybe if you have like for
[03:46:27] example table called colors and table
[03:46:29] called products and you would like to
[03:46:30] see all the combinations between the
[03:46:33] products and the colors. So in some
[03:46:35] scenarios it makes really sense to see
[03:46:36] all your products together with all the
[03:46:38] colors without any matching conditions
[03:46:41] or whatever. So there are like few
[03:46:42] scenarios for the cross join if you are
[03:46:44] like doing simulations or testing. So
[03:46:47] this is how we do the cross join. Okay.
[03:46:49] So that's all about the cross join. And
[03:46:51] with that we have covered the four
[03:46:53] advanced types of joins. Now if you look
[03:46:55] at this you might ask okay how I'm going
[03:46:57] to choose between all those types. So
[03:46:59] you might ask me okay bar how you do it?
[03:47:01] Well I'm going to show you now my
[03:47:02] decision tree that I usually follow in
[03:47:05] order to choose the correct type.
[03:47:10] So now if I'm combining two tables and I
[03:47:13] want to see in the results only the
[03:47:15] matching data between two tables then I
[03:47:17] go and use the inner join. We don't have
[03:47:19] any other type for that. So that's
[03:47:21] simple but now if I want to see
[03:47:23] everything all the data I don't want to
[03:47:25] miss anything after joining two tables
[03:47:27] then I take different path and here I
[03:47:29] ask myself is there like one side more
[03:47:31] important than the other am I interested
[03:47:33] in all data from one table from one side
[03:47:36] like here we have like a main table or a
[03:47:39] master table then I go and use the lift
[03:47:41] join but if I want to see all the data
[03:47:43] from all tables in my query everything
[03:47:45] so there is no one table more important
[03:47:47] than other then I go with the full join
[03:47:50] So this is another path and now the
[03:47:52] third path if I'm interested to see only
[03:47:54] the unmatching data. So I'm doing some
[03:47:57] kind of checkups and so on. And here
[03:47:59] again the same thing do I want to see
[03:48:00] the unmatching data from only one side.
[03:48:02] There is like one table that is
[03:48:04] important then I go and use the lift
[03:48:06] anti- join. So I want to see the
[03:48:08] unmatching data from one table and I'm
[03:48:11] using the other table only for the
[03:48:12] check. But in my query if both of the
[03:48:15] tables are important there is no main
[03:48:17] table and secondary table both are
[03:48:19] important then I go and use the full
[03:48:21] anti- join. So actually that's it. This
[03:48:23] is the decision tree that I follow
[03:48:25] usually as I'm writing a query. And you
[03:48:27] might ask me how about the right join.
[03:48:30] Well as you know me I don't have it at
[03:48:31] all in my decision tree. So I don't use
[03:48:34] it at all. Now by looking to this I can
[03:48:36] tell you if I check most of the queries
[03:48:38] that I write very often I use the left
[03:48:41] join. So I can tell you this is my
[03:48:42] favorite way on how to join tables. So
[03:48:45] let me show you exactly
[03:48:49] why. Usually I write queries in order to
[03:48:52] do data analyzes. So in data analytics
[03:48:54] you have always like starting points.
[03:48:56] You have like a topic that you are
[03:48:58] analyzing like the customer. So you have
[03:49:00] always like a master table. So I always
[03:49:02] start with the main table of my
[03:49:04] analysis. So in my query I start from
[03:49:06] this table from table A the main table.
[03:49:09] And then what happens? The data is not
[03:49:11] enough in this table. I need some extra
[03:49:13] data that comes from another table like
[03:49:15] the table B. So the table B is only here
[03:49:18] like an additional data to the master
[03:49:20] table. So I go and use the lift join in
[03:49:23] order to connect the table B and then I
[03:49:25] find another interesting information in
[03:49:27] another table in table C. So same things
[03:49:29] happens. I go and join the tables using
[03:49:31] the lift join and so on. So I keep
[03:49:33] connecting multiple tables to this main
[03:49:36] table in the middle. And my query going
[03:49:38] to look like this. always doing lift
[03:49:40] joins with multiple tables. Now, of
[03:49:42] course, you might say, "Yeah, but
[03:49:43] sometimes you would like to see only the
[03:49:44] matching data and so on. So, it makes
[03:49:47] sense only to use the inner join." Well,
[03:49:49] in order to do that, I can control
[03:49:50] everything that I want to see in the
[03:49:52] final results using the wear clause. So,
[03:49:55] in the wear clause, I define exactly
[03:49:57] what I want to see in the final result.
[03:49:59] So, with that, I get like more
[03:50:00] flexibility on whether I want to see the
[03:50:02] matching, unmatching data and so on like
[03:50:04] we done in the lift and join, right? So
[03:50:06] as I'm analyzing data I tend very
[03:50:09] frequently having this setup where I
[03:50:11] start from the main table and I lift
[03:50:13] join all other tables and with the word
[03:50:15] conditions I control the final results.
[03:50:17] So this is how I connect multiple tables
[03:50:20] together. So now if I want to visual
[03:50:21] this in like circles it's going to look
[03:50:23] like this. We have the circle A. So this
[03:50:25] is the master table the starting point.
[03:50:27] I want to see all the data from table A
[03:50:30] and I live join it then with another
[03:50:32] table B and from table B I want to see
[03:50:34] only the matching data. So it's like the
[03:50:36] lift join. Now what going to happen? I'm
[03:50:38] going to go and add another table. So
[03:50:40] another circle the circle C. And from
[03:50:42] the circle C, we want to see only the
[03:50:44] matching data. And of course you can
[03:50:46] keep adding circles to this. But it's
[03:50:48] going to be always the same thing. And
[03:50:49] in your circle going to has only the
[03:50:51] matching data. So now as we learned we
[03:50:53] can use joins in order to combine
[03:50:55] multiple tables to get a complete big
[03:50:57] picture about topic like the customers.
[03:50:59] I would like to see everything about the
[03:51:01] customers in the final results. So
[03:51:02] either you're going to do it like me
[03:51:04] where you start from the main table and
[03:51:05] then go and lift join all other tables
[03:51:08] or maybe you say you know what there is
[03:51:10] no main table about the customer's data
[03:51:12] all the tables are equally important
[03:51:14] then you can go and join all those
[03:51:16] tables using the inner join if you are
[03:51:18] interested only on the match data so
[03:51:20] what can happen if you have again those
[03:51:22] circles from the A you need only the
[03:51:24] matching data from B you need as well
[03:51:26] only matching data and as well from the
[03:51:28] third circle so you are interested only
[03:51:30] on the overlapping between all all three
[03:51:32] tables. So you will get only this
[03:51:34] section where you have overlapping
[03:51:36] between all three tables. So this is of
[03:51:38] course another way on how to join
[03:51:40] multiple tables. Okay. So now my friends
[03:51:42] let's go back to scale in order to
[03:51:44] practice how to join multiple tables.
[03:51:46] Okay. So now let's have a task. This
[03:51:48] going to be a little bit challenging. We
[03:51:49] will be doing multi- joins using the
[03:51:52] sales DB. Retrieve a list of all orders
[03:51:54] along with the related customer product
[03:51:57] and employee details. And for each order
[03:52:00] display the following. We want to see
[03:52:01] the order ID, the customer name, the
[03:52:04] product name, sales price, salesperson
[03:52:07] name. So there is a lot of things that
[03:52:08] is going on. And the first thing that
[03:52:10] you're going to notice it does now we
[03:52:11] are using different database. We will be
[03:52:14] not using the my database, we're going
[03:52:16] to go and use the sales DB. So this is
[03:52:18] the first thing that we have to do. So
[03:52:20] instead of using my database, so we say
[03:52:23] use sales DB and then execute it. We are
[03:52:26] now connected to the sales DB. So this
[03:52:28] is the first thing. So now if you are
[03:52:30] reading this task there are a lot of
[03:52:31] tables that are involved. We need the
[03:52:33] orders, we need the customers, products
[03:52:36] and employees. So there are like four
[03:52:38] tables needed in this task and we need
[03:52:41] different stuff from each table. So now
[03:52:43] how I think about it well it is mainly
[03:52:45] focusing on the table orders right? So
[03:52:47] we need all the orders we cannot miss
[03:52:50] any order here. So this sounds for me
[03:52:52] this is the main table and then it says
[03:52:55] along with that we need other
[03:52:56] informations. So that means the other
[03:52:58] tables are not that important like the
[03:53:00] orders. So this gives me feeling about
[03:53:02] what is the main table and this going to
[03:53:04] be my starting points. So let's start
[03:53:06] from that from the table orders. So
[03:53:08] select star from and here you have to
[03:53:10] pay attention that this database has
[03:53:13] always a schema. It's called if you look
[03:53:15] to the left side sales dot the table
[03:53:17] name. So we have to write that now in
[03:53:19] our query. So we're going to write it
[03:53:21] over here sales dot and then the table
[03:53:23] name orders. Let's go and execute it.
[03:53:25] Now I know this is the first time that
[03:53:27] you are querying this table. We have a
[03:53:29] lot of informations here and as well we
[03:53:31] have a lot of ids. Those ids going to
[03:53:33] help us of course on joining our data
[03:53:36] with the other tables. So what do we
[03:53:37] need from here? We need the order ID. So
[03:53:39] we have it over here. We're going to get
[03:53:42] the order ID. This time the naming
[03:53:44] convention is different. We don't have
[03:53:46] like underscores and comm. We have
[03:53:48] different type of namings. So be careful
[03:53:50] with that. So what else do we need? We
[03:53:52] need the sales. So if you go to the
[03:53:53] right side over here, we have column
[03:53:55] gold sales and we're going to go and
[03:53:57] include it to the results. Now all the
[03:53:59] other informations are actually not
[03:54:01] needed, but I need those ids in order to
[03:54:04] join it with the other tables. So now
[03:54:06] what I'm going to do, I'm going to go
[03:54:07] and give it an alias and all. So now I'm
[03:54:09] going to go and assign it for each
[03:54:10] column. This comes from the orders and
[03:54:13] as well the same thing for the sales. So
[03:54:15] that's it for now. And if I go and
[03:54:17] execute it, I will get the orders and
[03:54:19] the sales. All right, so that's all for
[03:54:20] the first table. Let's go now and see
[03:54:22] what do we need. We need the customer's
[03:54:24] name. Well, actually we don't have this
[03:54:26] piece of information in the orders. So
[03:54:28] all what you have to do is to go and
[03:54:29] explore in the other tables in order to
[03:54:31] find this column. So how I usually do I
[03:54:34] go and explore the tables like this. So
[03:54:36] I write a symbol select from each
[03:54:39] tables. So the customers. So now I go
[03:54:42] and repeat this for each table inside
[03:54:44] the database. So we have the customers,
[03:54:47] employees, we have an orders, the orders
[03:54:52] archive and as well the products. So now
[03:54:56] I start exploring the table. So if I go
[03:54:58] to the customers over here, we can see
[03:55:00] we have here five customers and we can
[03:55:02] see the names of the customers. So we
[03:55:04] see the first name and the last name and
[03:55:06] this is exactly what I need for my
[03:55:08] query. Now of course we have to go and
[03:55:09] connect this table with the orders. So
[03:55:12] we need a common column. Usually it's
[03:55:13] going to be the ID. So here we have the
[03:55:15] customer ID and if you go and query the
[03:55:18] orders you can find here as well the
[03:55:20] customer ID. Now if you are working in
[03:55:21] big projects you're going to have a lot
[03:55:23] of tables and exploring each one of them
[03:55:25] going to be really hard. So now of
[03:55:26] course if you have like in the project
[03:55:28] hundreds of tables it's going to be
[03:55:29] really hard to explore each table. So
[03:55:31] instead of that a good project a good
[03:55:33] database usually has an entity
[03:55:35] relationship model er model like the one
[03:55:38] that we have for the course. And here
[03:55:40] you can find easily the tables that you
[03:55:42] have inside your database and as well
[03:55:44] the relationship between them and this
[03:55:46] is very important especially if you want
[03:55:49] to join tables. So now by just looking
[03:55:50] quickly to this diagram I can understand
[03:55:53] okay there is an ID called customer ID
[03:55:56] inside the table orders and it is like a
[03:55:59] foreign key to the primary key the
[03:56:01] customer ID. So that means if I want to
[03:56:03] connect the orders with the customers I
[03:56:05] have to use that customer ID. So as you
[03:56:07] can see this is really nice
[03:56:08] documentations and I can quickly
[03:56:09] understand how to join the tables. So
[03:56:11] now back to our query. Now what I'm
[03:56:13] going to do I'm going to say lift join.
[03:56:15] So with that I guarantee all the orders
[03:56:18] going to be presented in the output and
[03:56:19] I will see always 10 orders. So now
[03:56:21] let's join it with the table customers
[03:56:23] sales dot customers and let's give it an
[03:56:26] alias like this. And now we're going to
[03:56:28] build the joining condition. So it's
[03:56:30] going to be the customer ID from the
[03:56:32] table orders equal to the customer ID
[03:56:35] from the table customers. So that SQL
[03:56:37] understand how to match the two tables.
[03:56:40] And now the two tables are connected and
[03:56:42] I can get the informations now from the
[03:56:44] customers. So see let's go and get the
[03:56:46] first name and as well the last
[03:56:49] name. So now let's go and execute it. So
[03:56:52] now as you can see we have customers for
[03:56:54] each order which is really nice. So with
[03:56:56] that we got the customer name and the
[03:56:58] order ID. Now the next one we need the
[03:57:00] product name. So either you're going to
[03:57:02] go here and start exploring. I think it
[03:57:04] is inside the table products. And here
[03:57:06] you can see we have the product. This is
[03:57:09] the name of the products. And if you
[03:57:11] check our ER diagram, you can see we can
[03:57:13] connect the table orders with the
[03:57:15] products using the product ID. So we
[03:57:18] have the product ID in the left and as
[03:57:20] well in the right. And now we can go and
[03:57:22] build this join as well over here. So
[03:57:24] again I go with a lift join. I don't
[03:57:26] want to lose anything from the table
[03:57:27] orders sales products and we give it an
[03:57:31] alias P. Now the condition for that here
[03:57:34] you have to be very focused. You want to
[03:57:36] get the product from the orders. So you
[03:57:39] say O dot product id equal to the
[03:57:43] product ID from the table products. So
[03:57:46] as you can see in the joins we are
[03:57:47] always joining with the table orders.
[03:57:49] Right? We are not trying to join for
[03:57:51] example the customers with the products.
[03:57:53] Always we are joining with the main
[03:57:55] table. So with that we have connected
[03:57:57] the third table and we can get the
[03:57:59] information that we need. So we need the
[03:58:01] products as I'm going to go and rename
[03:58:03] it products name. So let's go and
[03:58:07] execute it. And with that my friends I'm
[03:58:09] getting now the product informations
[03:58:10] from the table products. So we have the
[03:58:13] sales as well and we need the price. So
[03:58:15] if you go to the products you can see we
[03:58:17] have as well price information. I forgot
[03:58:19] about it. So let's go and get it as well
[03:58:21] from the same table. price. So let's go
[03:58:24] and execute it. And with that we have as
[03:58:26] well the prices. Now the last column it
[03:58:28] says we want to get the saleserson name.
[03:58:31] So the name of the employee right now if
[03:58:33] you go and explore as well we have here
[03:58:35] employees table and execute it. You can
[03:58:37] see we have here the name and the last
[03:58:39] name of the employees and we have an ID.
[03:58:42] So now we need this ID as well in the
[03:58:44] orders. So you can see we have the
[03:58:46] product ID, the customer ID. We already
[03:58:48] used those two. But we have here one
[03:58:50] more extra ID called the salesperson ID.
[03:58:52] Of course, it is not called employee ID.
[03:58:54] So here you might be a little bit
[03:58:56] skeptical about it. That's why we have
[03:58:58] to go and check again our ER diagram.
[03:59:00] And as you can see the employee ID from
[03:59:02] the employees, it is connected to the
[03:59:05] salesperson ID. So that I have better
[03:59:07] feeling about it and I understand. Okay,
[03:59:09] I can connect the orders with the
[03:59:10] employees using the salesperson ID. So
[03:59:13] let's go and do that. I'm going to say
[03:59:15] lift join. So as you can see I'm just
[03:59:17] doing left joins sales dot employees as
[03:59:21] e and the condition again very important
[03:59:24] always the first table is included in
[03:59:26] the join condition and here we're going
[03:59:28] to say the sales person ID is equal to
[03:59:31] the employee ID. So with that we have
[03:59:34] connected as well the employees and we
[03:59:36] will get as well the first name and the
[03:59:39] last name. So perfect that's it. Let's
[03:59:42] go and execute it. And as you can see
[03:59:44] guys, now we are getting the name of the
[03:59:46] salesperson. Now here comes an issue. As
[03:59:48] you are joining multiple tables and you
[03:59:50] are getting columns from different
[03:59:52] tables, what can happen? You might
[03:59:53] encounter this scenario where you have
[03:59:55] the same names in multiple tables. So
[03:59:58] now as you can see we have the first
[03:59:59] name last name from the employees and as
[04:00:01] well we have the first name last name
[04:00:02] from the customers and it's going to be
[04:00:04] really hard from the result to
[04:00:05] understand what are we talking about? Is
[04:00:07] it the customers? Is it the employee?
[04:00:09] That's why in this scenario if you have
[04:00:10] the same names we have to go and start
[04:00:13] giving aliases. So for the first one
[04:00:15] we're going to say customer first name
[04:00:18] and as well for the last name we're
[04:00:20] going to say customer last name. Same
[04:00:22] thing for the employee. So let's say
[04:00:25] employee first name or we can call it
[04:00:28] the saleserson whatever employee last
[04:00:31] name. So if you go and execute it now
[04:00:34] it's going to be more clear. Here we are
[04:00:36] talking about the name of the customer
[04:00:38] and here we are talking about the name
[04:00:40] of the employee. And again one more
[04:00:42] thing if you are not using aliases it's
[04:00:43] going to be an issue. So for example if
[04:00:45] you go over here and you don't use the
[04:00:47] table name before the column. So if I go
[04:00:49] and remove it and execute it you will
[04:00:52] see I'm getting an error. Now SQL can't
[04:00:54] understand what are you talking about.
[04:00:56] Is it the first name of the customer or
[04:00:58] from the employees because you are not
[04:01:00] specific about it. So you have to tell
[04:01:02] SQL to which table belong this column.
[04:01:04] It's very important to use a table name
[04:01:06] or the alias before the column name.
[04:01:09] Especially if you have the same column.
[04:01:11] So now we will not get an error. And
[04:01:13] with that we have solved the task. You
[04:01:14] have really to pay attention about the
[04:01:17] join keys. The condition you have to do
[04:01:19] it correctly cuz as you can see now we
[04:01:21] have a lot of tables and a lot of
[04:01:22] columns and sometimes happens an issue
[04:01:24] where you specify the wrong columns or
[04:01:27] the joins and the result can makes at
[04:01:29] all no sense. So always double check are
[04:01:31] you using the correct keys in order to
[04:01:33] join the tables. So with that you have
[04:01:35] solved the task and this is exactly how
[04:01:37] I join tables. I have always a starting
[04:01:39] point from an important table and
[04:01:42] everything else going to be left joined
[04:01:44] and in my results if I want to remove
[04:01:46] any scenario then I go and use the wear
[04:01:49] clause. So this is how I join multiple
[04:01:51] tables. Okay my friends. So with that
[04:01:53] you have learned now everything about
[04:01:55] how to join the tables in SQL and this
[04:01:57] is very important to understand. Now
[04:01:59] moving on to the second method on how to
[04:02:01] combine your data from multiple tables.
[04:02:03] We have the set operators. So we're
[04:02:05] going to go and cover how to combine the
[04:02:07] rows from multiple tables. So let's
[04:02:12] go. All right, my friends. So now as we
[04:02:15] learned before, in order to combine two
[04:02:17] tables we have two methods. If you want
[04:02:19] to combine the columns, we use the
[04:02:21] joins. And we have learned all those
[04:02:23] different types on how to combine data
[04:02:25] using join. So we have covered this
[04:02:27] section. But now if we want to combine
[04:02:28] the rows of two tables, we can use the
[04:02:31] set operators. And here we have four
[04:02:33] different types. We have union, union
[04:02:35] all, except and intersects. So now we're
[04:02:38] going to go and deep dive into this word
[04:02:40] on how to combine the rows of tables
[04:02:42] using the set operators. And now of
[04:02:44] course in this course we're going to
[04:02:45] cover everything. So let's
[04:02:49] go. All right. So now let's have a look
[04:02:52] to the syntax of the set operators.
[04:02:54] Okay. So now let's see that we have the
[04:02:55] following query. we are selecting the
[04:02:57] data from the customers. So this is our
[04:02:59] first query or our first select
[04:03:01] statements and we have another one which
[04:03:03] is very similar where we are selecting
[04:03:05] the informations from the employees and
[04:03:08] this is our second select statement. So
[04:03:10] now what we can do we can put between
[04:03:12] those two queries a set operators like
[04:03:15] for example the union. We can use of
[04:03:17] course any other set operators like the
[04:03:18] union all intersects except and so on.
[04:03:22] So as you can see the syntax is very
[04:03:24] simple. We have two different queries
[04:03:26] and we just put between them the set
[04:03:28] operator. So this is how the syntax of
[04:03:30] the set operators looks like. All right
[04:03:32] friends. So now we're going to talk
[04:03:33] about the rules of the set operators.
[04:03:35] And we're going to start with the rule
[04:03:37] number one the SQL clauses. In each
[04:03:39] individual select statements or query.
[04:03:41] We can use almost all the SQL clauses
[04:03:44] like where join group by having. But
[04:03:47] there is only one exception with the
[04:03:49] order by. Order by you can use it only
[04:03:51] once and only at the end of the entire
[04:03:54] query. So that means we cannot use order
[04:03:57] by in each select statements or in each
[04:03:59] query. We can use it only once and only
[04:04:02] at the ends of the entire query. All
[04:04:04] right. So about the syntax again here we
[04:04:05] have our two select statements and in
[04:04:08] between them we have the set operators.
[04:04:10] So now in each query we can go and use
[04:04:12] multiple stuff like the join where group
[04:04:15] by having. So we can make each query
[04:04:18] complex as we want. So everything is
[04:04:21] allowed but not the order by the order
[04:04:24] by must be always placed at the end of
[04:04:26] the entire query. So if you want to sort
[04:04:28] the result by the first name, you have
[04:04:30] to use the order by exactly at the end.
[04:04:33] So we are not allowed to use order by in
[04:04:36] each query. Okay. Moving on to the rule
[04:04:38] number two. The number of columns. The
[04:04:40] number of columns in each query must be
[04:04:42] the same. Okay. Okay. So now in order to
[04:04:44] understand this rule, let's have this
[04:04:46] very simple example. We're going to go
[04:04:47] and select the first name and the last
[04:04:50] name from the table sales customers. So
[04:04:54] this is our first query, our first
[04:04:55] select statements and let's say that I
[04:04:58] have another one and we want to select
[04:05:00] the first name last name but this time
[04:05:02] from another table, the employees. So
[04:05:04] with that we have our two queries and I
[04:05:06] would like now to go and combine them
[04:05:08] into one result. So we're going to go
[04:05:10] and use the set operator union. Let's go
[04:05:13] and execute it. So now as you can see in
[04:05:14] the result we will get the first name
[04:05:16] and last name from two tables the
[04:05:18] customers and employees. And it is
[04:05:20] working because we are fulfilling the
[04:05:21] rule where it says the number of columns
[04:05:24] must be the same in both queries. So how
[04:05:26] many columns do we have in the first
[04:05:28] query? We have two right and as well in
[04:05:30] the second query we have two columns. So
[04:05:32] that's why everything is working. So now
[04:05:34] let's go and break the rule by adding
[04:05:36] another column to the first query. So
[04:05:38] let's say that I would like to have the
[04:05:40] customer ID as well in the first query
[04:05:42] and with that as you can see in the
[04:05:44] first query we have three columns but in
[04:05:46] the second we have only two. So let's go
[04:05:49] and execute it. Now as you can see in
[04:05:51] the result we will get an error where it
[04:05:52] says if you are using union intersect
[04:05:54] and all those set operators you must
[04:05:57] have an equal number of columns between
[04:05:59] queries. So this is the rule you have to
[04:06:02] have the same number of columns in order
[04:06:03] to repair it. So I'm going to do I'm
[04:06:05] just going to remove the customer ID.
[04:06:06] Okay. So here again we have two columns
[04:06:08] and the second one as well two columns
[04:06:10] and everything going to be working.
[04:06:12] Okay. Moving on to the rule number
[04:06:13] three. The data types of columns in each
[04:06:15] query must match must be compatible in
[04:06:19] matching. In order to check that what
[04:06:21] we're going to do we're going to go to
[04:06:22] the object explorer to the left side.
[04:06:24] Let's go and browse the customers and
[04:06:26] the columns. And as you can see we have
[04:06:28] here the first name and last name with
[04:06:30] the same data type. We have the vchar.
[04:06:32] And if you go to the employees, you can
[04:06:34] see as well the first name, last name
[04:06:36] having varchar. So the first column is
[04:06:38] varchchar from the first query and as
[04:06:40] well for the employees and as well the
[04:06:43] last name from the customers having the
[04:06:45] same data type as the last name from
[04:06:47] employees. So the data type is matching.
[04:06:50] Now let's go and break this rule.
[04:06:52] Instead of having the first name, I
[04:06:54] would like to go and use the customer
[04:06:56] ID. So now let's check the customer ID
[04:06:58] on the left side. It is an int, an
[04:07:00] integer. But the first name is
[04:07:03] invarchar. So here we have a mismatch
[04:07:05] between data types. Let's go and try to
[04:07:07] execute it. So now we are getting an
[04:07:09] error where it says SQL is trying to
[04:07:10] convert the value Frank to an integer.
[04:07:13] So what this means the first query is
[04:07:15] always controlling everything the names
[04:07:17] and as well the data types. So here we
[04:07:19] have an integer and now scale is trying
[04:07:22] as well to convert the first name values
[04:07:24] to an integer and of course it will not
[04:07:26] work because we have here characters
[04:07:28] inside and it cannot convert characters
[04:07:30] to an integer. So we have a mismatch
[04:07:32] between data types between the customer
[04:07:34] ID and the first name and that's why we
[04:07:36] will get an error. The second column we
[04:07:38] don't have an issue because it is
[04:07:40] varchar in the first table and as well
[04:07:42] for the second table. So now in order to
[04:07:44] repair it either select a first name in
[04:07:46] the first query or we can go over here
[04:07:48] and say employee ID and with that if I
[04:07:52] execute it we will not get any errors
[04:07:54] because the employee ID is as well an
[04:07:56] integer and we have a match in the data
[04:07:58] types. So as you can see it's not enough
[04:08:00] to have the same number of columns. You
[04:08:02] have to have as well matching data types
[04:08:04] between those two queries. Okay, let's
[04:08:06] move to the next rule. Rule number four,
[04:08:08] the order of columns. The order of
[04:08:10] columns in each query must be as well
[04:08:12] the same. Okay, so let's understand what
[04:08:14] this means. Now we have here again the
[04:08:16] same example where we are selecting the
[04:08:18] ID and last name from customers and we
[04:08:20] are combining it using union with the
[04:08:22] employee ID and last name from the
[04:08:24] employees. And as you can see everything
[04:08:25] is working because we have the same
[04:08:27] number of columns and we have a matching
[04:08:29] data types. So now let's go and break
[04:08:31] it. What I'm going to do I'm just going
[04:08:33] to switch between those two columns. So
[04:08:35] first I'm selecting the last name and
[04:08:37] then the customer ID. So again I have
[04:08:39] the same number of columns and the ID is
[04:08:42] integer matching the ID of the employee
[04:08:44] and the last name having the same data
[04:08:46] type. So let's go and execute it. So
[04:08:49] here again SQL going to throw an error
[04:08:51] and says SQL is trying to convert the
[04:08:52] value go back to an integer. So it's
[04:08:55] like character to integer. It will not
[04:08:56] work. So what happened here? I have here
[04:08:58] the same informations. I have an ID and
[04:09:00] last name and ID and last name. Well,
[04:09:02] SQL doesn't work like this. SQL going to
[04:09:04] go and map the first column from the
[04:09:06] first query with the first column with
[04:09:08] the second query. So it's going to go
[04:09:09] and map last name to employee ID. And
[04:09:12] since they have different data types,
[04:09:13] SQL going to throw an error. So SQL
[04:09:16] doesn't understand or don't know how to
[04:09:17] map let's say the ID with the ID and
[04:09:20] since they have different data types SQL
[04:09:21] going to go and throw an error. So as
[04:09:23] you can see here we have the same
[04:09:25] informations between customers and
[04:09:27] employees but they don't have the same
[04:09:29] order. So SQL cannot go and map the
[04:09:32] informations because of the names of the
[04:09:33] columns. It's going to go and simply
[04:09:35] just mapping the columns like this. The
[04:09:38] first column from the first query with
[04:09:39] the first column from the second query.
[04:09:42] So as you can see in this rule you must
[04:09:44] have the same order of the columns.
[04:09:46] First the ID and then the last name and
[04:09:50] with that it's going to work again. All
[04:09:52] right moving on to the rule number five.
[04:09:54] The column aliases column names that we
[04:09:56] see in the output in the result is
[04:09:58] defined and determined by the column
[04:10:00] names of the first query the first
[04:10:03] select statements. So that means the
[04:10:05] first query is responsible of naming the
[04:10:07] columns in the output. Okay. So let's
[04:10:10] understand what this rule means. Again
[04:10:11] we have the same example. The customer
[04:10:13] ID, last name from customers, union,
[04:10:15] employee ID, last name from employees.
[04:10:17] So if you check closely the output, you
[04:10:19] can see that in the output we have the
[04:10:21] customer ID and not the employee ID.
[04:10:24] Even though we have the ids from the
[04:10:26] employee ID, but as you can see the
[04:10:28] first query is controlling the naming of
[04:10:31] the output. So since the first column
[04:10:34] called the customer ID, you will see it
[04:10:36] in the output as a customer ID. So the
[04:10:38] naming of the like the next queries will
[04:10:41] be totally ignored. So that's why if you
[04:10:43] want to give aliases to the output,
[04:10:46] you're going to go and do it only for
[04:10:47] the first query. So for example, I go
[04:10:49] over here and say instead of having
[04:10:51] customer ID, I would like to call it as
[04:10:53] an ID. So now if I go and execute it, as
[04:10:56] you can see in the output, we will get
[04:10:58] an ID. So I don't have to go and in each
[04:11:00] query give this alias. So I don't have
[04:11:03] to go over here and say yeah you are as
[04:11:05] well the ID because it's enough to
[04:11:07] define it from the first query. So
[04:11:09] there's no need to give the same names
[04:11:12] in the next queries. Let's take another
[04:11:14] example where we would like to have an
[04:11:15] alias for the last name. So I would like
[04:11:17] to have it like this last name and let's
[04:11:21] go and do it in the second query. So
[04:11:24] last name let's go and execute it. So
[04:11:27] now as you can see in the output, we
[04:11:29] still have last name and there's no
[04:11:31] underscore because this is totally
[04:11:33] ignored from SQL. This is not the first
[04:11:36] query. The first query says you are last
[04:11:38] name without underscore. So again if you
[04:11:41] want to do that we go over here. Let me
[04:11:42] just get it and put it in the first
[04:11:45] query. Let's go and execute it. So my
[04:11:47] friends, the first query is very
[04:11:48] important in order to give the names for
[04:11:51] the output. So if you want to do aliases
[04:11:53] and to rename stuff, do it only on the
[04:11:55] first query. And as well the first query
[04:11:57] controls the data types. All right. Now
[04:11:59] to the last rule matching the correct
[04:12:02] informations. If in your query you
[04:12:03] fulfill all other rules and you don't
[04:12:05] have an error in the SQL that doesn't
[04:12:08] mean that your result is accurate and
[04:12:10] correct. You are the only one that is
[04:12:12] responsible of mapping the informations
[04:12:14] between queries correctly because SQL
[04:12:17] doesn't understand the content and the
[04:12:19] informations of your tables of your
[04:12:21] queries. And if you don't match the
[04:12:23] informations correctly between the
[04:12:24] queries, you will get inaccurate and
[04:12:27] wrong results in the output. Okay. So
[04:12:29] now back to our example. Let's say I
[04:12:30] would like to get the first name and as
[04:12:34] well the last name from the customers
[04:12:36] and the same informations from the
[04:12:38] employees. Let's go and execute it. Now
[04:12:40] as you can see it's very nice where we
[04:12:42] are getting the first name, last name
[04:12:43] from both tables in one result and we
[04:12:46] are fulfilling all the requirements in
[04:12:48] SQL. Same numbers, same data types and
[04:12:50] so on. Now let's go and make incorrect
[04:12:52] results. So what I'm going to do, I'm
[04:12:53] just going to swap the first name and
[04:12:56] last name in the second query. So first
[04:12:58] last name and then the first name. So
[04:13:01] let's go and execute it. So now as you
[04:13:03] can see we will get results because we
[04:13:04] are fulfilling all other rules because
[04:13:07] we have the same number of columns and
[04:13:09] as well we have matching data types. So
[04:13:11] the first one is character the first
[04:13:13] name and the last name is as well
[04:13:14] character. So SQL will just present the
[04:13:17] result as you define it. But the result
[04:13:20] is completely wrong because now we have
[04:13:22] if you check the first column here the
[04:13:24] first name. So here we can see last
[04:13:26] names inside the first names. For
[04:13:28] example, Brown and Baker those are last
[04:13:30] names but we can see them inside the
[04:13:32] first name. And the same thing in the
[04:13:34] last name. We now we can see first names
[04:13:36] inside it. Mary, Carol, they are all
[04:13:39] first names. So as you can see the
[04:13:41] result has really bad data quality. We
[04:13:42] are now mixing stuff and it doesn't
[04:13:44] makes any sense. But SQL will not know
[04:13:47] that because SQL doesn't know the
[04:13:49] information the content of your data.
[04:13:52] It's just mapping the data types. So
[04:13:54] first name is varchchar the last name as
[04:13:56] well vchar. Everything is fine and you
[04:13:59] will get the results. So my friends you
[04:14:01] are responsible of having the same
[04:14:03] informations mapped between the two
[04:14:05] queries and not having an error from a
[04:14:07] skill doesn't mean that we have now
[04:14:09] correct results. So pay attention to the
[04:14:11] informations that you are mapping
[04:14:13] between the two queries. All right. So
[04:14:15] those are the rules of the set
[04:14:17] operators. So the first one is that the
[04:14:19] order by can only be used once at the
[04:14:21] end of the entire query and all queries
[04:14:24] must have the same number of columns,
[04:14:26] the matching data types, the same order
[04:14:29] of columns and the first query always
[04:14:31] control the names and the aliases of the
[04:14:34] result set and as well the data type.
[04:14:36] And the last rule is that make sure that
[04:14:38] you are mapping the correct informations
[04:14:41] to each others between queries. So those
[04:14:43] are the rules of the set
[04:14:48] operators. Okay. So what is union? Union
[04:14:51] going to go and return all distinct
[04:14:53] unique rows from both queries. So that
[04:14:56] means it's going to go and combine
[04:14:57] everything and all the rows going to be
[04:14:59] presented at the output. So since it
[04:15:02] says all distinct unique rows that means
[04:15:05] union going to go and remove all
[04:15:07] duplicates from the combined result set.
[04:15:10] So union going to make sure that each
[04:15:12] row going to appear only once. All
[04:15:14] right. So now let's have this very
[04:15:15] simple example. We have two sets of
[04:15:17] data. We have the customers where we
[04:15:19] have five customers with the first names
[04:15:22] and as well we have another set called
[04:15:23] employees and we have as well the first
[04:15:25] names of the employees and we have five
[04:15:28] employees. And now if you take a look to
[04:15:29] the first names you can see that we have
[04:15:31] the same persons as a customers and as
[04:15:33] well as employees. We have given and
[04:15:36] marry in both sets of data. So now how
[04:15:38] is k going to execute union it's going
[04:15:40] to go and return everyone from customers
[04:15:43] and everyone from the employees. But now
[04:15:45] since we have given and married twice in
[04:15:47] the output we're going to have them only
[04:15:49] once. So this is how the union works. It
[04:15:51] going to go and return everyone from two
[04:15:53] sets but without duplicates. All right.
[04:15:55] So now we have the following task and it
[04:15:57] says combine the data from employees and
[04:15:59] customers into one table. So that means
[04:16:02] in one table we want to combine all
[04:16:04] informations from employees and
[04:16:05] customers. So which informations do we
[04:16:08] need? This is the first question that I
[04:16:09] usually ask myself. So in order to do
[04:16:11] that first we have to explore the data.
[04:16:13] So select star from sales customers and
[04:16:18] then semicolon. Then I'm going to write
[04:16:20] another query select star from sales and
[04:16:24] employees and semicolon. So now why I'm
[04:16:27] using two different semicolons because
[04:16:29] I'm telling SQL we have now two separate
[04:16:31] queries. They have nothing to do with
[04:16:32] each others. And if you go and execute
[04:16:35] it like this. And now in the output you
[04:16:36] can see we got two result grids. The
[04:16:39] first result grid is for the first query
[04:16:41] and the second one for the second query.
[04:16:44] So they have nothing to do with each
[04:16:45] others. I just want to explore those two
[04:16:47] tables in order to understand how I'm
[04:16:48] going to map those informations. So now
[04:16:50] if we check those two tables you can see
[04:16:52] that both of them has ids. So we can map
[04:16:55] those informations right. Both of them
[04:16:57] has as well first name last name. So
[04:17:00] that means I can go and map the first
[04:17:02] name and last name together. Now in the
[04:17:04] customers we have country but we don't
[04:17:07] have this informations in the employee.
[04:17:09] So we have to go and ignore it. And we
[04:17:11] have as well here score where we don't
[04:17:13] have a score for the employees. That
[04:17:15] means I can go and map three
[04:17:17] informations between the customers and
[04:17:19] employees. Now of course we can go and
[04:17:21] think do we need really the ids because
[04:17:23] it doesn't make really any sense to have
[04:17:24] the ids in the tables. It's not anymore
[04:17:27] unique because we have here the custom
[04:17:28] ID one and employee one. So I think we
[04:17:31] can go and ignore it. So the only really
[04:17:33] two informations that is useful to map
[04:17:35] is the first name and last name. So now
[04:17:37] let's go and add those two informations.
[04:17:39] So we need the first name, last name and
[04:17:42] the same informations as well from the
[04:17:44] employees. But now we want everything to
[04:17:46] be in one query. That's why I'm going to
[04:17:48] go and remove the semicolons. And now we
[04:17:51] have to go and use set operators between
[04:17:52] those two queries. And now in order to
[04:17:54] combine the data we have two options
[04:17:56] either union or union all in this
[04:17:58] example it doesn't mention anything
[04:18:00] about duplicates and so on. I would like
[04:18:02] to go with the union in order to remove
[04:18:04] the duplicates if there is any. So
[04:18:06] that's it. Let's go and execute it. Now
[04:18:09] as you can see in the output we have
[04:18:10] only one result because we have only one
[04:18:12] big query. And now we have the first
[04:18:14] names and last names from the customers
[04:18:16] and employees. And now one more thing
[04:18:18] about the order of the queries. It
[04:18:20] doesn't matter whether we start with the
[04:18:21] employees or with the customers. we will
[04:18:23] get the exact same results but pay
[04:18:25] attention to the naming of the columns.
[04:18:27] Always the first query controls the
[04:18:29] names but since now they have the same
[04:18:31] naming so it should not be a problem. So
[04:18:33] if I go and switch those two tables and
[04:18:36] start it again we will get exact same
[04:18:38] results. So now let's understand how
[04:18:40] scale did combine the data using the
[04:18:43] union. Okay. So now we have here the
[04:18:44] results from the first query and the
[04:18:46] second query employees and customers and
[04:18:48] we are combining the data using union.
[04:18:50] The first step in SQL is that it's going
[04:18:52] to go and take the columns from the
[04:18:54] first query which is from the employees.
[04:18:56] So it's going to take the first name
[04:18:58] last name as a column name to the
[04:19:00] results. And now the next that is going
[04:19:02] to go and start combining the rows
[04:19:04] between those two tables. So first going
[04:19:06] to go and take the rows from employees
[04:19:08] and as well going to check whether there
[04:19:10] is duplicates in the data. So as you can
[04:19:12] see we don't have here any duplicates.
[04:19:13] So we're going to have the five
[04:19:15] employees. And now the next step is
[04:19:17] going to start adding rows from the
[04:19:18] second query from the customers very
[04:19:21] carefully without generating any
[04:19:22] duplicates. We don't have it in the
[04:19:24] output. That's why it's still going to
[04:19:25] go and add it to the result. Append it.
[04:19:28] And then the next customer we have Kevin
[04:19:30] Brown. As you can see, we have it
[04:19:31] already in the results. That's why will
[04:19:33] not go and add it to the result.
[04:19:35] Otherwise, it's going to go and generate
[04:19:36] duplicates. So it's still going to
[04:19:38] ignore this customer. The same thing for
[04:19:40] Mary. We have Mary as well in the
[04:19:42] results. So it's going to skip it. And
[04:19:44] then we're going to go to the mark. As
[04:19:45] you can see, we don't have mark in the
[04:19:47] results. That's why SQL going to go and
[04:19:49] take this customer and put it in the
[04:19:51] output. And then the last one, we have
[04:19:53] Anna. We don't have Anna in the results.
[04:19:55] That's why SQL can go and as well add it
[04:19:57] to the results. And now with this, SQL
[04:19:59] did combine the rows between those two
[04:20:01] tables. And we have here eight persons.
[04:20:03] So as you can see, SQL is combining the
[04:20:05] data, but very carefully not generating
[04:20:07] any duplicates. All right. So that's it.
[04:20:09] This is how the union operator works.
[04:20:15] Okay. So now union all union union all
[04:20:17] going to go and return all rows from
[04:20:19] both queries. So it's very similar to
[04:20:21] union. It going to go and combine all
[04:20:23] the rows and everything going to be
[04:20:25] presented in the combined result set.
[04:20:27] But the big difference to the union all
[04:20:30] will not remove any duplicates. It is
[04:20:32] the only set operators that doesn't
[04:20:35] remove duplicates and it going to show
[04:20:37] all the rows as it is. So if you have a
[04:20:40] row 10 times from the query, you will
[04:20:42] find it as well in the output 10 times.
[04:20:44] Now you might ask me when to use union
[04:20:46] and when to use union all. I'm going to
[04:20:48] say that there is one big difference
[04:20:50] between them is that union all has way
[04:20:53] better performance and it's faster than
[04:20:55] the union. And that's because union all
[04:20:58] doesn't perform additional steps like
[04:21:00] removing duplicates. So my friends that
[04:21:02] means if you know already that in my
[04:21:04] queries there is no duplicates. I know
[04:21:06] my tables. I know my queries. There's no
[04:21:08] duplicates. Don't use union and always
[04:21:11] use union all because you will get
[04:21:13] better performance. Another scenario for
[04:21:15] the union all is that I would like to
[04:21:16] see the duplicate. I'm doing data
[04:21:18] quality checks and I would like to see
[04:21:19] whether there is duplicate after I
[04:21:22] combine multiple queries. So in this
[04:21:24] situation I go and use as well the union
[04:21:26] all. Now we have again the same example.
[04:21:28] We have the customers and employees and
[04:21:30] we have as well the same persons Kevin
[04:21:32] and Mary as customers and as well as
[04:21:34] employees. So now if you want to combine
[04:21:36] the data using union all it going to
[04:21:38] return all rows including duplicates. So
[04:21:41] that means SQL going to go and execute
[04:21:43] union all like this it going to return
[04:21:45] everything from customers and everything
[04:21:47] from employees and Kevin and Mary going
[04:21:50] to be presented twice in the output. So
[04:21:52] as you can see union all is returning
[04:21:54] all the rows as it is from the two
[04:21:57] result sets and if there's duplicates in
[04:21:59] the sets we will get as well duplicate
[04:22:01] in the output. So Kevin going to be
[04:22:02] existing twice in the output and marry
[04:22:05] as well twice. So this is how the union
[04:22:07] all works. All right. So now we have
[04:22:08] very similar SQL task and it says
[04:22:10] combine the data from employees and
[04:22:12] customers into one table including
[04:22:15] duplicates. So it's exactly like the
[04:22:17] last task but this time in the task we
[04:22:19] are saying include duplicates. So we
[04:22:21] cannot go and use union. We have now to
[04:22:23] go and use union all. We will have the
[04:22:25] exact same query. So we are selecting
[04:22:27] the employees first last name and as
[04:22:29] well customers first last name. And now
[04:22:31] instead of using union, we're going to
[04:22:33] go and use union all. So all what we
[04:22:35] have to do is that to go over here and
[04:22:36] say union all. So now pay attention to
[04:22:39] this. As you can see in the union
[04:22:40] previously, we got eight records or
[04:22:43] eight persons from the output. So now
[04:22:46] let's go and execute it and check the
[04:22:48] results. Now as you can see we got now
[04:22:49] 10 persons instead of eight. And that's
[04:22:51] because we have five customers and five
[04:22:54] employees and we have duplicates inside
[04:22:56] the data. We have two duplicates. Now if
[04:22:57] you check we have here Mary and as well
[04:22:59] over here we have Mary and same goes for
[04:23:01] given we have given over here and as
[04:23:03] well here. So we have duplicates inside
[04:23:05] the data and SQL just combine the two
[04:23:07] tables. Okay. So now we're going to
[04:23:08] understand how SQL execute union all in
[04:23:10] order to combine data. All right. Again
[04:23:12] we have the two results from queries. We
[04:23:14] have the employees and customers and SQL
[04:23:16] going to do the same steps. First going
[04:23:17] to go and get the column names from the
[04:23:19] first query and put it in the output.
[04:23:21] It's still going to go and take all the
[04:23:23] employees and put it in the output
[04:23:25] without checking anything. So that means
[04:23:27] if there is duplicates in the data, it's
[04:23:29] going to be presented as well in the
[04:23:30] output. It's very simple. Now it's going
[04:23:33] to go to the second step and as well
[04:23:34] take all the customers and append it
[04:23:37] into the output like this. So that's it.
[04:23:40] It's very fast. It's going to go and
[04:23:42] just combine all the rows from the
[04:23:44] employees and all the rows from the
[04:23:45] customers. And with that, we're going to
[04:23:46] get that 10 persons. And as you can see,
[04:23:48] we have duplicates in the data. So we
[04:23:50] have marry twice and given as well
[04:23:52] twice. And that's why union all is the
[04:23:54] fastest. It doesn't have any extra steps
[04:23:56] or checks. Just taking all rows from all
[04:23:59] queries and put it in the output. All
[04:24:01] right. So as you can see it's very
[04:24:02] simple, right? So that's all for the
[04:24:04] union
[04:24:08] all. Okay. So what is except sometime we
[04:24:11] call it minus in other databases but in
[04:24:13] SQL server we call it except. So it's
[04:24:15] going to go and return a distinct rows
[04:24:17] from the first query that are not found
[04:24:20] in the second query. So from this
[04:24:22] definition we can understand that the
[04:24:24] order of the queries can affect the
[04:24:26] final result. There is a first query and
[04:24:29] a second query. So it is the only set
[04:24:31] operator where you have to pay attention
[04:24:33] to the order of the queries. And as well
[04:24:35] it's like the others. It's going to go I
[04:24:37] remove the duplicates from the result
[04:24:39] set. All right. Again we have this very
[04:24:40] simple example. We have two sets, five
[04:24:42] customers, five employees and there is
[04:24:44] the same persons as a customer and as
[04:24:47] employees Kevin and Mary. So now we're
[04:24:48] going to go and combine those two sets
[04:24:50] using the excepts or sometime we call it
[04:24:52] minus. So it says it's going to return
[04:24:54] unique rows in the first table that are
[04:24:56] not in the second table. So what going
[04:24:58] to happen? What is the first table?
[04:25:00] Let's say the customers on the left
[04:25:01] side. So here we have five persons.
[04:25:03] Joseph, Mark, Anna, Kevin and Mary. So
[04:25:06] now the rule is we need the customers
[04:25:08] that are not employees. So it's safe for
[04:25:11] Joseph, Mark and Anna because they are
[04:25:13] not existing in the second set. That's
[04:25:16] why SQL going to return those three
[04:25:18] values. But now for the two customers
[04:25:20] given and marry here there is an issue.
[04:25:22] Given and marry they are members of the
[04:25:24] second set. The second table the
[04:25:26] employees. That's why SQL going to go
[04:25:28] and exclude them from the output because
[04:25:31] they are not fulfilling the rule. So in
[04:25:33] the output we will get only three
[04:25:35] customers and all the values from
[04:25:37] employees and the common values between
[04:25:39] customers and employees will be excluded
[04:25:42] from the output. So this is how the
[04:25:45] except works. All right. So let's have a
[04:25:47] very simple skill task and it says find
[04:25:49] the employees who are not customers at
[04:25:52] the same time. Okay. So let's see how
[04:25:53] we're going to solve that. We're going
[04:25:55] to stay with the same queries as usual.
[04:25:56] We have the employees and the customers
[04:25:59] but instead of having union all we're
[04:26:00] going to use the set operator except. So
[04:26:03] now since we are using except we have to
[04:26:04] make sure that the order of the queries
[04:26:06] are correct. So the first query is the
[04:26:09] employees which is correct because we
[04:26:11] have to find the employees who are not
[04:26:14] customers at the same time. So we are
[04:26:15] focusing on the employees. The first
[04:26:17] table is correct and the second table is
[04:26:20] customers. If the task says find the
[04:26:22] customers who are not employees at the
[04:26:24] same time then we have to go and switch
[04:26:26] it. We have first to query the
[04:26:28] customers. So now everything is correct.
[04:26:30] Let's go and execute it. And now in the
[04:26:32] output we see three employees who are
[04:26:34] not customers at the same time. So we
[04:26:36] have Carol, Frank and Michael. But as we
[04:26:38] know we have five employees Kevin and
[04:26:40] Mary. They are not here in the result
[04:26:41] because they are customers as well. So
[04:26:44] now let me show you what can happen if I
[04:26:46] just switch those informations. So we
[04:26:48] start with customers and then with
[04:26:50] employees. Let's go and execute it. As
[04:26:52] you can see, we're going to get
[04:26:53] completely different results. Now we are
[04:26:55] getting customers informations. And now
[04:26:57] in the output, we got three customers
[04:26:59] who are not employees at the same time.
[04:27:01] This is not what we want from this task.
[04:27:03] So if you do it like this, it's going to
[04:27:05] be incorrect. So pay always attention
[04:27:07] here to the order of that query. So now
[04:27:09] let's go and correct it. So we're going
[04:27:10] to have first employees and then
[04:27:12] customers. Let's execute it. And now
[04:27:14] let's go and understand how SQL execute
[04:27:17] the except operator. All right. So again
[04:27:19] we have the results from the two queries
[04:27:20] or from two tables and now we are doing
[04:27:23] except between them. So let's see how is
[04:27:25] going to execute it. It's going to take
[04:27:26] as usual first the names from the first
[04:27:29] query from the employees and put it in
[04:27:31] the output. And now SQL going to present
[04:27:33] data only from the first query in the
[04:27:36] output. And it going to go and use the
[04:27:38] customers only as a check. So SQL will
[04:27:41] not put any data or rows from the
[04:27:43] customers. It will just use the second
[04:27:45] query as a lookup in order to check the
[04:27:47] data. So, it's going to start with the
[04:27:49] first employee, Frankly. Do we have
[04:27:52] Frankly in the customers? Well, no, we
[04:27:54] don't have it. That's why it's going to
[04:27:56] accept it and put it in the output. And
[04:27:58] then in the next step, it's still going
[04:28:00] to go to the second employee and check.
[04:28:02] As you can see, we have it already in
[04:28:03] the customers. So, SQL going to go and
[04:28:05] ignore it. It's not allowed to be in the
[04:28:07] output. The same thing for Mary. We have
[04:28:10] it as well in the customers. That's why
[04:28:12] it will not be presented in the output.
[04:28:14] So Michael, we don't have a Michael in
[04:28:17] customers. That's why it can be
[04:28:19] presented in the output. And as well for
[04:28:21] Carol, the same thing. We don't have
[04:28:23] Carol as a customer and we're going to
[04:28:25] have it in the output. So as you can
[04:28:28] see, we will get data only from the
[04:28:29] first table and the second table only
[04:28:31] going to be used in order to check the
[04:28:33] informations from it. So we don't have
[04:28:35] in the output any customers, it's only
[04:28:36] employees. So now let's check quickly
[04:28:38] what going to happen if we switch the
[04:28:39] tables. So now we have the customers as
[04:28:41] the first table. SQL going to take the
[04:28:43] columns from the first table and it's
[04:28:45] going to start presenting the customers
[04:28:47] informations in the output and going to
[04:28:49] go and use the employees only as a
[04:28:51] lookup. So do we have Joseph? We don't
[04:28:53] have it in the employee. And then Kevin
[04:28:56] and Mary we have it already in the
[04:28:58] employees and Mark and Anna are not part
[04:29:01] of the employees that's why can go and
[04:29:04] present the results in the output like
[04:29:06] this. So now as you can see SQL is
[04:29:07] focusing on the table customers and we
[04:29:09] are getting data from the customers not
[04:29:11] from the employees. Employees is only as
[04:29:13] a check. So with that we understand the
[04:29:15] order of the queries is very important
[04:29:17] for the exceptions. We will get
[04:29:18] different results if we have different
[04:29:20] order. All right. So that's all for the
[04:29:22] except
[04:29:26] operator. Okay. So what is intersect?
[04:29:29] Intersect going to go and return only
[04:29:31] row that are common in both queries.
[04:29:34] It's something very similar to the inner
[04:29:36] join and as well here it's going to go
[04:29:38] and remove duplicates. So there will be
[04:29:39] no duplicates in the output. All right.
[04:29:41] Again we have this very simple example
[04:29:42] where we have five customers and five
[04:29:44] employees and now we're going to combine
[04:29:46] them using the intersect. So what
[04:29:48] intersect does it going to go and return
[04:29:50] common rows between two tables. So how
[04:29:53] SQL going to execute it? It's very
[04:29:55] simple. SQL going to go and search for
[04:29:57] the common values. So what are the
[04:29:59] common values? It's given and marry and
[04:30:02] SQL going to return only those two
[04:30:04] values given and marry and all others
[04:30:06] going to be excluded from the results.
[04:30:09] It's very simple, right? It's going to
[04:30:10] go and return only the common values and
[04:30:12] this is how the intersect works in SQL.
[04:30:15] Okay, let's have this simple task and it
[04:30:16] says find the employees who are also
[04:30:19] customers. So we're going to have the
[04:30:20] same queries employees and customers but
[04:30:23] instead of having except we're going to
[04:30:24] go and use intersect. Since we are
[04:30:27] finding the common informations between
[04:30:29] the employees and customers it's very
[04:30:31] simple and straightforward. Let's go and
[04:30:33] execute it. And with that we're going to
[04:30:34] get the Kevin and Mary. This is the two
[04:30:37] persons that are at the same time
[04:30:39] employees and customers. And of course
[04:30:41] here we don't have to pay attention to
[04:30:42] the order of the queries. It's going to
[04:30:44] be the same if we say find the customers
[04:30:46] who are also employees. So if you go and
[04:30:48] just switch for example the customers
[04:30:50] with employees you will see that we will
[04:30:53] get the exact same results. So it
[04:30:55] doesn't matter which query is first
[04:30:56] again pay attention to the first query
[04:30:58] that define the names. So now let's
[04:31:00] understand how is scale execute
[04:31:01] intersects behind the scenes. Okay again
[04:31:04] our two tables and now we are doing
[04:31:05] intersects. So as usual SQL going to go
[04:31:08] and take the columns from the first
[04:31:09] query and now we're going to go and find
[04:31:11] the common data between those two
[04:31:13] results. So it's going to do it row by
[04:31:15] row. So we have the employee Frank. Do
[04:31:16] we have it as a customer? No. So it will
[04:31:19] not be in the output. Given brown, we
[04:31:21] have it in the employees and as well as
[04:31:23] a customer over here. So that's why we
[04:31:25] will get it in the output. The same
[04:31:27] thing for Mary. So we have Mary as
[04:31:29] employee and as well as customer. So
[04:31:31] we're going to have it in the output.
[04:31:33] Michael and Carol, they are not
[04:31:35] customers. They are only employees.
[04:31:37] That's why we will not get it in the
[04:31:38] output. The same thing goes for the
[04:31:40] customers. Joseph, we don't have Mark.
[04:31:42] We don't have Anna because they are not
[04:31:44] employees. So with that we're going to
[04:31:46] get only the common informations between
[04:31:48] the two tables or two queries and it
[04:31:50] doesn't matter whether we start with
[04:31:51] customer or with employees we will get
[04:31:53] at the end the same information. All
[04:31:55] right so that's all it's very simple
[04:31:57] right this is how the intersect works in
[04:31:59] SQL.
[04:32:04] All right friends, so now we come to the
[04:32:05] part where I'm going to show you how I
[04:32:07] usually use the set operators in my
[04:32:10] projects for data analyszis or for data
[04:32:12] engineering. So here are the most
[04:32:14] important use cases for the set
[04:32:16] operators. All right, the first use case
[04:32:18] is combining similar tables before doing
[04:32:20] data analyzes. In some scenarios, we
[04:32:23] want to generate a report and we end up
[04:32:25] writing similar queries on top of
[04:32:27] similar tables and we go at the end and
[04:32:29] join all the results from the queries in
[04:32:32] order to present the final report. And
[04:32:34] now instead of doing that what we can do
[04:32:36] first we can go and combine all the
[04:32:38] similar informations into one table and
[04:32:40] then we can do on top of it a query a
[04:32:42] data analyzes in order to generate a
[04:32:44] report and we can do that using the
[04:32:47] union or union all. Let's have few
[04:32:49] examples. So let's say that we have four
[04:32:51] tables employees, customers, suppliers
[04:32:53] and students. So as you can see all of
[04:32:55] them are sharing the same informations.
[04:32:57] They hold data about persons. So now
[04:33:00] let's say that you are generating a
[04:33:01] report that requires all the individuals
[04:33:04] in the organization in the database. So
[04:33:06] what you're going to end up doing is
[04:33:08] writing SQL query for the employees,
[04:33:10] another one for customers and as well
[04:33:13] for the suppliers and the students. And
[04:33:15] then you're going to go and merge all
[04:33:16] the results from those queries into the
[04:33:19] final report. Now the issue with this
[04:33:21] setup is that you are having a lot of
[04:33:23] queries, a lot of similar queries. So
[04:33:25] you have it here four times. And now
[04:33:27] what might happen is that you go and
[04:33:29] change the logic of the first two
[04:33:31] queries and you forget later to do it
[04:33:33] for the other two and you will get
[04:33:35] really inconsistent data in the reports.
[04:33:37] So instead of that what we can do we can
[04:33:39] go and use the set operators in order to
[04:33:42] combine first all those tables in one
[04:33:45] big table. So what we're going to do
[04:33:46] we're going to go and use a union in
[04:33:48] order to combine those four tables into
[04:33:51] the table persons. So we're going to
[04:33:53] have it like this. So we will get all
[04:33:54] the rows from the employees and put it
[04:33:56] in the persons all the rows from the
[04:33:58] customers from the suppliers and as well
[04:34:01] from the students and put everything in
[04:34:03] one big table that holds all the
[04:34:06] informations about the individuals that
[04:34:07] we have inside our database. And now the
[04:34:10] next step after we combine the data now
[04:34:12] we write an SQL query in order to
[04:34:14] analyze this new big table and the
[04:34:17] result going to be presented in the
[04:34:19] reports. And now of course the advantage
[04:34:21] here is that we have only one SQL query
[04:34:23] for the data analyzers on top of this
[04:34:26] table instead of having it four times.
[04:34:28] And now if you go and change the logic
[04:34:30] of the SQL query, it going to be applied
[04:34:32] automatically on all the data that we
[04:34:34] have in the database. And we have done
[04:34:36] already this example where we have
[04:34:37] combined the data between the employees
[04:34:39] and customers. Another scenario where we
[04:34:41] have to combine data before doing any
[04:34:42] reporting. That's sometimes the database
[04:34:45] developers tend to divide a table one
[04:34:47] big table into multiple small tables in
[04:34:50] order to optimize the performance. For
[04:34:52] example, here splitting the orders by
[04:34:54] the year. We have orders 2022 2023. Now
[04:34:58] again here if you want to generate a
[04:34:59] report in order to analyze the orders
[04:35:02] over the years over the time either
[04:35:04] you're going to go and make a query for
[04:35:06] each of those tables or you're going to
[04:35:08] go first combining all those tables into
[04:35:11] one table called orders. So what we're
[04:35:13] going to do we're going to use a union
[04:35:15] between all those tables in order to
[04:35:17] generate one central table called the
[04:35:19] orders. So all the rows from the first
[04:35:21] table and all rows from the next table.
[04:35:25] next one and the last one. So, we're
[04:35:27] going to put everything in one big table
[04:35:30] and once we have the orders, we're going
[04:35:31] to go and write analytical skill query
[04:35:34] on top of the orders in order to
[04:35:36] generate the report. So, as you can see,
[04:35:39] it's very important step in order to
[04:35:40] prepare the data before doing data
[04:35:42] analyszis. Okay. So now let's have the
[04:35:44] following SQL task and it says the
[04:35:46] orders are stored in separate tables. We
[04:35:49] have the orders and orders archive. Now
[04:35:51] combine all orders data into one report
[04:35:54] without duplicates. Okay. So by looking
[04:35:56] to the task we have to combine two
[04:35:58] tables orders and orders archive. So
[04:36:00] either union or union all. But since the
[04:36:02] task says without duplicates that means
[04:36:05] we have to go with the union. But now
[04:36:07] before we combine any data we have first
[04:36:09] to understand the content of the orders
[04:36:11] and the orders archive in order to map
[04:36:13] the columns correctly. So first we have
[04:36:15] to go and explore the two tables. So
[04:36:18] let's start with selecting the data from
[04:36:21] orders everything semicolon and as well
[04:36:24] from the second table sales orders
[04:36:28] archive and as well semicolon. So let's
[04:36:31] go and execute it. So now in the output
[04:36:33] we get two results because we have two
[04:36:35] separate queries. The first result is
[04:36:37] for the orders and the second one is for
[04:36:39] the orders archive. Let me just make it
[04:36:42] a little bit bigger. And now as you can
[04:36:44] see we have almost identical tables. So
[04:36:47] as you can see we have the order ID,
[04:36:48] product ID, customer ID. So everything
[04:36:51] looks like identical and of course we
[04:36:53] can go and check that using the object
[04:36:55] explorer on the left side. So we have
[04:36:57] here the orders and those are the
[04:37:00] columns. And if you go to the orders
[04:37:02] archive, you can see that we have the
[04:37:06] exact same columns. So that means we can
[04:37:08] go and map all columns from orders with
[04:37:10] the all columns of orders archive. So
[04:37:12] let's go and do that. So I'm just going
[04:37:14] to remove all semicolons and then we're
[04:37:16] going to go and use the union. So now we
[04:37:18] have everything in one query. Let's go
[04:37:21] and execute it. Now we will get in the
[04:37:23] output one single results, one single
[04:37:25] table with all informations from orders
[04:37:28] and orders archive. So we have all
[04:37:30] orders now in one table and everything
[04:37:33] currently is matching. So with that we
[04:37:35] have solved the task. We have one result
[04:37:36] with all orders. We don't have any
[04:37:38] duplicates since we are using union and
[04:37:41] we have combined the data. But now we
[04:37:42] have one issue with that. This solution,
[04:37:45] this query is quick and dirty and
[04:37:47] actually it's not following the best
[04:37:49] practices. So now the best practices
[04:37:50] here is to list clearly all the columns
[04:37:54] in each query without using star. All
[04:37:56] right. So now let's go and do that. Now
[04:37:57] we need a list of all columns from the
[04:38:00] table orders and the table orders
[04:38:01] archive. And since we have a lot of
[04:38:03] columns, what we're going to do, we go
[04:38:04] to object explorer, right click on the
[04:38:07] table name, and then let's go select the
[04:38:09] top thousand rows. So let's click on
[04:38:11] that. And now we're going to get a very
[04:38:13] simple select statements where we have
[04:38:15] all the column names from the table
[04:38:17] orders. This is what I usually do if I
[04:38:19] need all the columns in the my select
[04:38:21] statements. So let's go and copy it and
[04:38:23] go back to our query. Then let's go
[04:38:26] replace the first star with those
[04:38:29] columns. And we're going to do the same
[04:38:30] thing as well for the orders archive
[04:38:33] since they have the same names. So let's
[04:38:35] go and do that as well. So let me just
[04:38:37] make this smaller in order to see the
[04:38:39] query. So now we have a select for the
[04:38:42] table orders with all columns and as
[04:38:45] well a select with all columns for the
[04:38:47] table orders archive. So let's go and
[04:38:49] execute it. And of course now we're
[04:38:50] going to go and get the same results.
[04:38:52] Now you might ask why we are doing this.
[04:38:54] Why didn't we stick with the star? It's
[04:38:56] quick. It's simple. Well for the
[04:38:58] following reason. So now currently the
[04:39:00] status is that everything is matching.
[04:39:02] We have 100% identical tables. But what
[04:39:05] happened with the time is that we do
[04:39:07] development in our solution and we might
[04:39:09] go and change the schema of the table
[04:39:11] orders. So we might rename stuff, we
[04:39:14] might add new columns or maybe switch
[04:39:16] the columns. So this means the table
[04:39:19] order with the time will not be anymore
[04:39:21] identical with the archive. And this is
[04:39:23] of course a problem if you are mapping
[04:39:25] the data blindly using the star. So now
[04:39:28] let me show you what I mean. Let's say
[04:39:29] that in this table we are developing the
[04:39:32] orders and we just switch those two
[04:39:34] columns in the schema for some reason.
[04:39:37] So now we have the product ID first and
[04:39:39] then the order ID. So let's go and
[04:39:42] execute it. Now if you are using star
[04:39:44] you will not notice this informations.
[04:39:46] But if you are using script you're going
[04:39:47] to see immediately that here we have
[04:39:49] first the order ID and then product ID.
[04:39:52] And here we have the opposite. So it's
[04:39:54] more clear listing the columns than
[04:39:57] using the star. And now as you can see
[04:39:58] in the output you can see that we have a
[04:40:01] problem that here we have order ids and
[04:40:04] then suddenly we have something like the
[04:40:07] product ID. So we're going to have
[04:40:08] incorrect data which leads to incorrect
[04:40:10] analyzes. So here the best practices to
[04:40:12] not use the star and to clearly list all
[04:40:15] the columns. Now one more technique that
[04:40:17] I usually use once I'm combining data is
[04:40:19] that I add the source of the data inside
[04:40:22] the query. So what I mean with that now
[04:40:24] you can see that we have here two orders
[04:40:26] with the order ID one they are not
[04:40:29] duplicates they are completely different
[04:40:30] informations and that's because they
[04:40:32] come from different tables. So what I
[04:40:34] usually do I go and add the source of
[04:40:36] each record it's really nice information
[04:40:38] for the analytics for the users to
[04:40:40] understand where these records come
[04:40:42] from. So how we going to do that? We're
[04:40:44] going to have for example on the first
[04:40:46] column the following word let's say
[04:40:49] orders and we're going to call it let's
[04:40:51] say that's source table and we're going
[04:40:54] to do the same thing as well in the
[04:40:57] second query. Right? So the source table
[04:40:59] here is not the orders it's the orders
[04:41:02] archive. So I'm just adding a static
[04:41:04] columns to my query in order to see the
[04:41:07] source of the table. So now we have here
[04:41:09] two different values. And let's go and
[04:41:12] execute it. And now you see we have
[04:41:13] created a new column called source table
[04:41:15] where it has only two values. We have
[04:41:17] the orders and the orders archive. Let's
[04:41:19] go and sort the data by the order ID. So
[04:41:22] order by order ID. So let's go and
[04:41:25] execute it. And now you can see it very
[04:41:27] clearly. The first order order ID one
[04:41:30] comes from the table orders and the
[04:41:32] second one comes from the orders
[04:41:34] archive. So this is really nice
[04:41:35] information that you can add to your
[04:41:37] data once you are combining multiple
[04:41:38] tables. So that's all about this use
[04:41:40] case on how to combine data between
[04:41:42] different
[04:41:46] tables. All right. Now we have another
[04:41:48] use case for the set operators. It's
[04:41:50] more for data engineers. We can use the
[04:41:52] except in order to find the delta
[04:41:55] between two batches of data. For
[04:41:57] example, data engineers build data
[04:41:59] pipelines in order to load daily new
[04:42:02] data from the source systems to a data
[04:42:04] warehouse or a data lake. Now, in those
[04:42:06] data pipelines, we have to build a logic
[04:42:09] in order to identify what are the new
[04:42:11] data that is generated from the source
[04:42:13] system in order to insert it in the data
[04:42:16] warehouse. One way to do it is to use
[04:42:17] the set operator except in order to
[04:42:20] compare the current data with the
[04:42:22] previous load. Let's have a very simple
[04:42:24] example. So in the day number one we
[04:42:26] have two customers one and two. So what
[04:42:28] going to happen in this day we're going
[04:42:29] to go and load those two customers into
[04:42:31] the data warehouse. So in the data
[04:42:33] warehouse we will get as well one and
[04:42:35] two. So this is for the first day
[04:42:36] nothing is crazy. We just load the data
[04:42:38] as it is. Now for the second day we will
[04:42:41] get the new data from the source system
[04:42:42] and it's going to look like this. So now
[04:42:44] if you check the second day you can see
[04:42:45] that we have again the customer number
[04:42:47] one we have already loaded to the data
[04:42:49] warehouse. So we have it as the previous
[04:42:52] day but we have a new customer ID number
[04:42:55] three. So now in order to load only the
[04:42:57] new data we don't need to load again the
[04:42:59] customer number one. What we can do? We
[04:43:02] can do an accept between the day number
[04:43:05] two with the previous load with the day
[04:43:07] number one. So now if we simply do an
[04:43:09] accept between those two sets we're
[04:43:11] going to go and identify the new data
[04:43:14] that is existing in the source system
[04:43:16] which is only the record number three.
[04:43:18] So now what going to happen if we do
[04:43:19] except between day two and day one we
[04:43:22] will get one record the new record that
[04:43:24] we're going to go and insert it inside
[04:43:26] our data warehouse. So as you can see
[04:43:28] this set operator except is very
[04:43:30] powerful in order to compare two sets
[04:43:32] and not only for data analysis we can
[04:43:34] use it as you can see for data
[04:43:35] engineering in order to identify what is
[04:43:38] the new data that is generated from the
[04:43:40] sources in order to insert it inside our
[04:43:43] data warehouse.
[04:43:48] Okay, one more use case for the set
[04:43:49] operators that I personally use a lot in
[04:43:51] my project is that if you are doing data
[04:43:54] migrations, you can use the accept in
[04:43:56] order to check the data quality and more
[04:43:59] specifically we can use it in order to
[04:44:00] check the data completeness. Okay, so we
[04:44:03] have the following scenario where we are
[04:44:04] doing data migrations between two
[04:44:06] databases. So let's say that we would
[04:44:07] like to move this table from database A
[04:44:09] to database B. So we're going to go and
[04:44:12] load the table to the new database. And
[04:44:15] now what is very important after you
[04:44:16] move the data is that to check whether
[04:44:18] all the records did move from database A
[04:44:21] to database B we are not missing
[04:44:23] anything even one record. So we want to
[04:44:25] do data completeness test and there are
[04:44:27] many methods on how to do this test. One
[04:44:30] of them is to use that set operator
[04:44:32] except. So how we going to do it? We're
[04:44:33] going to do an except between the table
[04:44:36] from database A and the table from
[04:44:37] database B in order to find any record
[04:44:40] that is still in database A which is not
[04:44:43] migrated to the database B. And of
[04:44:45] course the best result is that we will
[04:44:47] not get anything. The result should be
[04:44:50] empty. If we get an empty that means all
[04:44:52] the rows from database A exists in the
[04:44:55] database B. And now of course we are not
[04:44:57] done yet. We want to do the comparison
[04:44:59] but the way around. We want to find any
[04:45:01] new rows that is in database B that we
[04:45:04] don't find in database A. Those two
[04:45:07] tables must be identical. So now what
[04:45:09] we're going to do, we're going to do an
[04:45:10] except but the first table going to be
[04:45:12] from the database B. And then we're
[04:45:14] going to compare it with the database A.
[04:45:15] And we have the same expectation. The
[04:45:18] output should be as well empty. And now
[04:45:20] after doing the except twice for both
[04:45:22] sides and we are getting empty in the
[04:45:24] results. That means those two tables are
[04:45:27] identical and we are not missing
[04:45:29] anything. So this is another amazing use
[04:45:31] case for the set operators in order to
[04:45:34] improve the quality of your data
[04:45:35] migrations and in order to do data
[04:45:37] completeness
[04:45:41] test. Okay. So now let's have a quick
[04:45:44] summary about the set operators. So the
[04:45:46] set operator is going to go and combine
[04:45:47] the rows of multiple queries, multiple
[04:45:49] tables into one single result. And we
[04:45:52] have four different types of the asset
[04:45:53] operators. The first one is the union
[04:45:56] where it's going to go and combine all
[04:45:57] the rows but without including any
[04:45:59] duplicates. The second one we have the
[04:46:01] union all it's very similar. And the
[04:46:03] third one we have the except it's going
[04:46:05] to show all the rows from the first
[04:46:06] query that cannot be found in the second
[04:46:09] query. And the fourth one we have the
[04:46:11] intersect where it's going to show the
[04:46:12] common rows between two queries. And of
[04:46:15] course we have SQL rules in order to use
[04:46:16] the set operators. Both of the queries
[04:46:18] should have the same number of columns,
[04:46:21] the same data types and the order of
[04:46:23] columns. And the last rule, don't forget
[04:46:25] that the first query controls the
[04:46:27] aliases, the name of the columns and the
[04:46:30] data types of the entire result. And we
[04:46:32] have found amazing use cases for the set
[04:46:34] operators. Like for example, using union
[04:46:36] and union all in order to combine
[04:46:39] similar informations into one big table.
[04:46:41] Or we can go and use the amazing except
[04:46:43] operator in order to compare two
[04:46:46] different results in order to find the
[04:46:48] differences between them. And I usually
[04:46:50] use it in order to do data quality
[04:46:52] checks to test the data completeness.
[04:46:54] And another use case as a data engineer
[04:46:56] you can go and implement the except in
[04:46:58] your logic in your data pipelines in
[04:47:01] order to identify what are the new data
[04:47:03] that must be inserted in your system.
[04:47:05] Okay my friends. So with that we have
[04:47:06] learned all the set operators that we
[04:47:08] have inside SQL. And with that you have
[04:47:10] learned how to combine your data from
[04:47:12] multiple tables using SQL. So we are
[04:47:14] done with this chapter. Now we're going
[04:47:16] to go to the right side. So now we're
[04:47:18] going to start talking about the
[04:47:19] functions in SQL. And here we have two
[04:47:21] big families. The first one is the row
[04:47:24] level or the single value functions. And
[04:47:26] the second one we have the aggregate
[04:47:27] analytical functions. So let's start
[04:47:29] with the first one the rowle functions.
[04:47:31] And here we can group them into multiple
[04:47:33] categories. And we will start now with
[04:47:35] the string functions. But first let's
[04:47:37] understand what is exactly functions and
[04:47:39] why do we need them in SQL. So let's
[04:47:45] go. Okay. So what is exactly function
[04:47:48] and why we need it. Now again we have
[04:47:49] our data inside the table. Now there is
[04:47:52] like a lot of stuff that you can do with
[04:47:53] your data. So sometimes you have to
[04:47:55] change the values of your data like
[04:47:57] doing data manipulation or you want to
[04:47:59] do some aggregations and analyzes. So
[04:48:01] maybe you want to analyze your data and
[04:48:03] find insights and maybe build reports
[04:48:06] and sometimes you might find bad data
[04:48:08] inside your tables and you want to clean
[04:48:09] that up. So you want to do data
[04:48:11] cleansing and sometimes you have to do
[04:48:13] data transformations and data
[04:48:15] manipulation on our data in order to
[04:48:17] solve some SQL tasks and in SQL in order
[04:48:20] to solve those tasks we have functions.
[04:48:22] So again what is exactly a function? It
[04:48:24] is a built-in code block that accepts an
[04:48:27] input value. Then the function going to
[04:48:29] go and process this value and it going
[04:48:31] to return a result an output value. So
[04:48:34] you give an input value do some
[04:48:36] transformations and give an output. And
[04:48:38] we can group the functions into two big
[04:48:40] categories. The first one we call it
[04:48:42] single row functions. So you give the
[04:48:44] function only one value and at the
[04:48:46] return you will get as well one value.
[04:48:48] So the input for the function going to
[04:48:49] be only one single value like maria and
[04:48:52] the output of the function going to be
[04:48:54] as well single row value. So one value
[04:48:56] in one value out. And now the other
[04:48:58] category of functions we call it
[04:49:00] multirow functions. So for example if
[04:49:02] you have the function sum this function
[04:49:04] accept multiple rows multiple values
[04:49:07] like it gets 30 10 20 40 the function is
[04:49:10] then going to go and summarize all those
[04:49:12] rows and return in the output only one
[04:49:14] value. The summarization of all those
[04:49:16] values going to be 100. So the input is
[04:49:18] multiple rows and the output is one
[04:49:21] single value. So those are the two main
[04:49:22] categories of functions in scale.
[04:49:28] Now my friends you have to understand
[04:49:30] something about the functions that you
[04:49:31] can go and nest functions together. So
[04:49:34] you can use multiple functions together
[04:49:36] in order to manipulate one value. And
[04:49:38] this technique is not only in SQL in any
[04:49:40] programming language. So let's have this
[04:49:42] example. We have the function left. It's
[04:49:44] going to go and extract like few
[04:49:45] characters. Let's say two characters. So
[04:49:47] the input for this function let's say
[04:49:49] it's Maria. This value going to enter
[04:49:50] the function. The function is going to
[04:49:52] go and extract the first two characters.
[04:49:54] And in the output we will get only two
[04:49:56] characters m a. So this is one function.
[04:49:58] We have an input and output. Now you
[04:50:00] might say you know what we have multiple
[04:50:02] steps on this value. So the first step
[04:50:04] we want to extract the first two
[04:50:05] characters using the lift function. But
[04:50:07] we have a second step. So we want to
[04:50:09] transform this output into a lowercase
[04:50:12] characters. So we have another function
[04:50:13] lower and the input for this second
[04:50:15] function will be the output of the first
[04:50:17] function. So ma it is at the same time
[04:50:20] output and input for another function.
[04:50:22] So the lower function going to take this
[04:50:24] value and convert it into lowerase
[04:50:26] character. So it's like inside the
[04:50:28] factory the materials going to be
[04:50:29] processed into multiple stations and the
[04:50:31] output of one station going to be the
[04:50:33] input for the next station. And this is
[04:50:35] exactly what we can do with the
[04:50:36] functions. So now how we going to build
[04:50:38] that? The first step is to start with
[04:50:39] the first function. So this is simple
[04:50:41] one function. Now for the next step what
[04:50:44] you're going to do on the left side
[04:50:45] you're going to write lower and put the
[04:50:47] whole thing in parenthesis. So now the
[04:50:49] whole thing the first function going to
[04:50:51] be inside another function and with that
[04:50:53] you have nested one function in another
[04:50:55] and of course if you need a third
[04:50:57] function like for example the length
[04:50:59] what you're going to do you're going to
[04:51:00] put the whole thing again between two
[04:51:02] parentheses. So now that means the
[04:51:03] output of the lift going to go to the
[04:51:05] lower and the output of the lower going
[04:51:07] to go to the length. So it is very
[04:51:09] simple and the order of the execution
[04:51:10] for this will start always in the inner
[04:51:13] function. So the lift function going to
[04:51:14] be executed first and then the outside
[04:51:16] function the lower and the last function
[04:51:18] that's going to be executed is the
[04:51:20] length. This is how the nested functions
[04:51:22] works in SQL or in any programming
[04:51:27] language. Now my friends in SQL we have
[04:51:30] a lot of functions that's why we have to
[04:51:32] group them as well into subcategories.
[04:51:34] Like if you are talking about the single
[04:51:36] row functions, we have functions for the
[04:51:38] string values and as well for the
[04:51:40] numeric, the date and time and as well
[04:51:42] functions in order to handle the nulls.
[04:51:44] And if you are talking about the
[04:51:45] multirow functions, here we have
[04:51:47] basically two groups. The first one is
[04:51:49] the simple aggregate functions. Those
[04:51:51] are the basics in order to aggregate
[04:51:52] your data. And we have another advanced
[04:51:54] one. We call it the window functions or
[04:51:57] sometime we call it analytical
[04:51:58] functions. So now if I'm looking to
[04:52:00] those two groups and now my friends it
[04:52:02] is very important to understand those
[04:52:03] functions because using them you can do
[04:52:05] whatever you want with your data and if
[04:52:07] I'm looking to those two groups the
[04:52:09] single row functions those stuff here
[04:52:11] they are functions in order to
[04:52:13] manipulate and prepare the data for the
[04:52:16] second group. So if you are thinking
[04:52:17] about data engineers and data analysts
[04:52:19] the data engineers going to go and
[04:52:21] prepare the data in SQL using the single
[04:52:23] row functions. So you're going to use
[04:52:25] them in order to clean up, transform,
[04:52:27] manipulate your data in order to prepare
[04:52:29] it for the analyzes. And if you are data
[04:52:32] analyst, you will be mostly using the
[04:52:33] aggregate functions in almost every
[04:52:35] task. So I really see it like this. The
[04:52:37] single row functions for data engineers
[04:52:39] and multirow functions for data
[04:52:41] analysts. And my friends, what we're
[04:52:43] going to do in this course, we're going
[04:52:44] to visit each of those subgroups one by
[04:52:47] one, exploring the functions,
[04:52:49] understanding how they work and when
[04:52:50] we're going to use them. So let's start
[04:52:52] with the first group, the string
[04:52:53] functions. And here we're going to learn
[04:52:55] how to manipulate the string values. So
[04:52:58] let's
[04:53:02] go. Okay. So now since we have a lot of
[04:53:04] string functions, I'm going to go and
[04:53:06] divide them into categories based on the
[04:53:08] purpose. So for example, we have a group
[04:53:10] of functions that's going to go and
[04:53:11] manipulate the string values. So we have
[04:53:13] concatenation, upper, lower, replace,
[04:53:15] and so on. And another group where we
[04:53:17] have only one function. It is where we
[04:53:19] can do calculations on the string
[04:53:20] values. And the last group, it is all
[04:53:22] about how to extract something from a
[04:53:25] string value. And here we have three
[04:53:26] functions left, right, substring. So now
[04:53:28] let's go and start with the first group
[04:53:30] about the data manipulation. And the
[04:53:32] first function we have here
[04:53:36] concat. All right. So what is exactly
[04:53:39] concat or concatenation? It's going to
[04:53:41] go and combine multiple string values
[04:53:43] into one value. So if you have multiple
[04:53:46] things you can put everything in one
[04:53:47] value. So let's have a very simple
[04:53:49] example. Okay. So now let's say that you
[04:53:50] have one value called Michael. So here
[04:53:52] you have a first name and you have
[04:53:54] totally separated value for the last
[04:53:56] name another column where you have a
[04:53:58] value like Scott. And now you say you
[04:54:00] know what it makes no sense to have the
[04:54:01] first name separated from the last name.
[04:54:03] I would like to go and combine them in
[04:54:05] one value. So you can go and use the
[04:54:07] concat in order to combine those two
[04:54:09] values or multiple values into one
[04:54:11] single value like Michael Scott.
[04:54:14] I think that pretty much sums it up. So
[04:54:17] it is nicer to see the full name in one
[04:54:19] value instead of having like two columns
[04:54:21] for that. So that's it. This is why we
[04:54:23] need the concatenations. Now let's go
[04:54:25] back to scale in order to try that out.
[04:54:27] Okay. So now we have the following task.
[04:54:29] Show a list of customers first names
[04:54:31] together with their country in one
[04:54:33] column. So that means we have to make a
[04:54:35] list of customers and we have to combine
[04:54:37] two columns in one. So let's start
[04:54:39] writing the query. Select. We need the
[04:54:41] first name, the country from the table
[04:54:44] customers. So first let's go and execute
[04:54:46] this. Now as you can see we have list of
[04:54:48] customers but the issue here the first
[04:54:50] name and the countries those two
[04:54:52] informations are in different columns
[04:54:54] but the task says they should be in one
[04:54:56] column. So now in order to combine those
[04:54:58] two things we have to use the
[04:54:59] concatenate function. So concat. So I'm
[04:55:02] going to start with the first argument.
[04:55:03] It's going to be the first name and then
[04:55:05] the country like this. And we're going
[04:55:07] to give it a name. Let's call it like
[04:55:09] this name country. Now let's go ahead
[04:55:12] and execute it. Now in the output you
[04:55:13] can see we have a new column. It's
[04:55:15] called name country and we have both of
[04:55:17] the informations in one column. So we
[04:55:19] have Maria, Germany, join USA. But it
[04:55:21] doesn't really look good because there's
[04:55:23] like no spacing between them. Now we can
[04:55:25] go and make some separation between them
[04:55:28] by just adding one more thing in between
[04:55:30] like for example maybe a space. So now
[04:55:33] we are concatenating the first name
[04:55:35] together with a space this over here and
[04:55:38] then the country. So let's go and
[04:55:39] execute it. Now as you can see we have
[04:55:41] nice separations between the first name
[04:55:43] and the country. And of course you can
[04:55:44] go and add different separations like
[04:55:46] maybe my notes or underscore and you
[04:55:48] will get the same effect. So with that
[04:55:50] we have a list of customers where we
[04:55:52] have the first name together with the
[04:55:54] country in one column. As you can see
[04:55:56] it's very simple. This is how you
[04:55:57] combine two columns in one. It is really
[04:56:00] nice and easy transformation. Okay. So
[04:56:01] that's all about the concatenation in
[04:56:03] scale. Next we're going to talk about
[04:56:05] two functions. The upper and the
[04:56:10] lower. Okay. So what is upper function?
[04:56:13] It's going to go and converts all the
[04:56:14] characters of a string to an uppercase.
[04:56:17] It's going to make everything
[04:56:18] capitalized. And the lower function is
[04:56:20] exactly the opposite. It's going to go
[04:56:21] and convert everything to a lower case.
[04:56:24] So let's have very simple example for
[04:56:26] those two functions. Okay. So now we
[04:56:27] have like three values with different
[04:56:29] cases. The first one where you have only
[04:56:31] the first character capitalized and the
[04:56:33] rest is lowered and then the same value
[04:56:35] but everything is lowered and a third
[04:56:37] one where you have everything with an
[04:56:39] uppercase. Now if you go and apply the
[04:56:41] function upper to those three values
[04:56:43] what going to happen for the first value
[04:56:45] going to go and turn it into an
[04:56:47] uppercase. So everything going to be
[04:56:48] capitalized not only the first
[04:56:50] character. And now for the second value
[04:56:52] going to turn it as well to completely
[04:56:54] capitalized. So all the characters going
[04:56:56] to change. And for the last value it is
[04:56:58] already capitalized. So in the output
[04:57:00] you will get the same value. So actually
[04:57:01] nothing going to happen for that. So
[04:57:03] this is simply the uppercase. Now let's
[04:57:05] see what can happen if you use the lower
[04:57:07] case. For the first value only the first
[04:57:09] character going to be changed and then
[04:57:10] you will have everything in lower case.
[04:57:12] The second value it is already a
[04:57:14] lowerase value. So if you apply lower
[04:57:16] case nothing going to happen. You will
[04:57:18] get the same value. But for the last one
[04:57:19] everything here is capitalized and if
[04:57:21] you apply lower case all the characters
[04:57:23] going to convert to a lower case. So my
[04:57:25] friends this is very simple. Let's go
[04:57:27] back to your skill in order to practice
[04:57:29] that. Okay. So we have the following
[04:57:30] task and it says transform the
[04:57:32] customer's first name to lowerase. So
[04:57:35] now as you can see the first names here
[04:57:37] the first character is a capital the
[04:57:39] rest is lowerase. So now in this task we
[04:57:41] have to convert the whole thing into
[04:57:43] lower case. So let's go and do that.
[04:57:45] It's very simple. We're going to say
[04:57:47] lower first name and let's go and call
[04:57:50] it low name. So that's it. Let's go and
[04:57:53] execute it. Now if you go and compare
[04:57:54] the lower name with the first name, you
[04:57:56] can see all the characters now in the
[04:57:59] lower case. So that's it for the task.
[04:58:01] We have transformed the first name to
[04:58:03] lower case. All right. The next task is
[04:58:05] exactly the opposite. Transform the
[04:58:06] customer's first name to uppercase. So
[04:58:09] let's go and have a new column. We're
[04:58:11] going to say upper then the first
[04:58:15] name as app name. So that's it. It's
[04:58:19] very simple. Let's go and execute. Now
[04:58:20] you can see in the output we have a new
[04:58:22] column called up name and inside it we
[04:58:24] have the first name but now all the
[04:58:26] characters in upper case. So this is how
[04:58:29] you convert the case to lower or to
[04:58:31] upper in SQL. Okay. So that's all about
[04:58:33] the upper and the lower. Next we're
[04:58:35] going to talk about very interesting
[04:58:36] function. It is the
[04:58:41] trim. So the trim function going to go
[04:58:44] and remove the leading and trailing
[04:58:46] spaces in your string values. So it's
[04:58:48] going to go and get rid of the empty
[04:58:50] spaces at the start and at the end of a
[04:58:52] string value. Let's have very simple
[04:58:54] example. Okay. So now we're going to
[04:58:55] have different scenarios. The first one
[04:58:56] you can have like a value join where you
[04:58:58] don't have any spaces and this is the
[04:59:00] normal case. But sometimes you might
[04:59:02] have it like this where at the start you
[04:59:04] have a leading space. You have an empty
[04:59:06] space or sometimes we call it white
[04:59:08] space. In another scenario the space
[04:59:10] might be at the end of the word. So here
[04:59:12] we call it trailing space and in another
[04:59:14] scenario you might have both of them.
[04:59:16] This is really bad. where at the start
[04:59:18] you have the leading space and at the
[04:59:20] end you have the trailing space. And of
[04:59:21] course you might not have only one
[04:59:23] space, you might have multiple spaces
[04:59:25] depend on how long did the user press
[04:59:27] the space, right? So of course my
[04:59:29] friends spaces are really evil and this
[04:59:31] makes no sense to have it in your data.
[04:59:33] Now what you have to do is to do data
[04:59:34] cleansing. We have to clean up this miss
[04:59:36] and you have the best function in order
[04:59:38] to clean up the data. You have the trim.
[04:59:40] So if you apply trim for the first
[04:59:42] value, nothing going to happen because
[04:59:44] everything is clean and we don't have
[04:59:45] any spaces. Now if you apply it for the
[04:59:47] second case where you have a leading
[04:59:49] space if you do that SQL going to go and
[04:59:52] remove this space. The same thing for
[04:59:54] the trailing space. So if you have space
[04:59:55] at the end the trim function going to
[04:59:57] find it and clean that up. And if you
[04:59:59] have it at the start and at the end then
[05:00:01] it's as well no problem. It's going to
[05:00:03] go and clean that up. And as well the
[05:00:05] trim function can go and clean multiple
[05:00:07] spaces. So if you have like five spaces
[05:00:09] 10 spaces at the end or at the start the
[05:00:11] trim function going to go and clean that
[05:00:13] up. So this is how the trim works. And
[05:00:15] now let's go back to our scale in order
[05:00:16] to find out whether we have any spaces.
[05:00:18] Okay. So now we have a very tricky and
[05:00:21] interesting task. It says find the
[05:00:24] customers whose first name contains
[05:00:26] leading or trailing spaces. So now by
[05:00:29] looking to those values we have to find
[05:00:31] any spaces inside the customer's name.
[05:00:33] Now by just looking to this results you
[05:00:35] will not find any white spaces because
[05:00:38] it's really hard to see especially if it
[05:00:40] is like trailing spaces. Now we have to
[05:00:42] write query order to detect any spaces
[05:00:45] in the names. So how we can do that?
[05:00:48] Okay. So now think about it a little bit
[05:00:49] and I can give you a hint. You can use
[05:00:51] the function trim in order to remove any
[05:00:54] white spaces and you have to use it
[05:00:56] inside a wear clause. So what we're
[05:00:58] going to do we're going to say where. So
[05:00:59] now we have to build a condition to
[05:01:01] detect any spaces. So if you are saying
[05:01:04] if the first name is not equal to itself
[05:01:08] first name after applying a trim. So
[05:01:11] after trimming the first name if it is
[05:01:15] not equal to the first name so that
[05:01:17] means there was spaces. So again what is
[05:01:20] going on here? Let's go for Maria. If
[05:01:22] Maria has no nulls if you trim this
[05:01:24] value nothing going to happen. The value
[05:01:26] going to stay exactly like before
[05:01:28] because there is no white spaces. But if
[05:01:30] in Maria there is any space inside it.
[05:01:33] Trimming the value will not be equal to
[05:01:35] the first name if it contains any
[05:01:37] spaces. So if the column is not equal to
[05:01:40] the same column after trimming it that
[05:01:42] means there is spaces. So let's go and
[05:01:45] execute it. And now we can see in the
[05:01:47] output we have one customer John where
[05:01:49] we have this situation. Now if you don't
[05:01:51] believe me or you don't follow me here
[05:01:53] we can have another easier check. So
[05:01:55] let's go and comment this out and let's
[05:01:57] have a look to our first names. Now we
[05:01:59] can go and calculate the length of the
[05:02:02] first name like we have done before. So
[05:02:05] length name and let's go and execute it.
[05:02:08] Now if you can see here Maria we have
[05:02:09] five characters but John we have here
[05:02:12] four characters but the length is five
[05:02:14] and that's because we have somewhere
[05:02:16] space and the space going to count as a
[05:02:18] character. So here there is like
[05:02:20] something wrong right and you can check
[05:02:21] the others as well everything is
[05:02:23] matching but only John we have here an
[05:02:25] issue and now in order to see this more
[05:02:27] clearly we're going to use two functions
[05:02:29] the trim and the length. So first let's
[05:02:31] go and trim the first
[05:02:33] name. And after trimming the values, I'm
[05:02:36] going to calculate the length. So we are
[05:02:38] nesting together the trim and the
[05:02:41] length. And I'm going to call it length.
[05:02:43] Trim name. So let's go and execute it.
[05:02:47] Now we can see the length before
[05:02:49] trimming any value. And we can see the
[05:02:52] length after trimming the values. So you
[05:02:54] can see over here that join before
[05:02:56] trimming is five and after trimming is
[05:02:59] four. So we have here an issue. Now we
[05:03:01] can make things more clear where we can
[05:03:03] go and subtract the length of the first
[05:03:06] name with the length of the first name.
[05:03:09] But first we trim the values. So here we
[05:03:11] can call it maybe a flag or something.
[05:03:13] So let's go and execute it. Now by
[05:03:15] looking to the flag it is really easy to
[05:03:17] now to see if we have a zero then
[05:03:19] everything is fine. We don't have any
[05:03:21] white spaces. But if we have higher than
[05:03:23] zero like here one then this is an
[05:03:25] indicator that we have a white space.
[05:03:27] Either you do it like this where the
[05:03:29] first name is not equal the first name
[05:03:31] after trimming or you use more
[05:03:33] complicated solution where you say where
[05:03:36] and I'm going to remove this from here
[05:03:38] the length of the first name is not
[05:03:41] equal to the length after trimming so
[05:03:43] not equal so if you go and execute it
[05:03:46] you will get exactly again join so this
[05:03:48] is how we detect any empty spaces inside
[05:03:50] our data using the trim function or
[05:03:53] maybe as well using the length but I
[05:03:55] really prefer the first solution it is
[05:03:56] way easier using one function. All
[05:03:58] right, so that's all about how to remove
[05:04:00] the empty spaces using the trim. Next,
[05:04:02] we're going to talk about very important
[05:04:04] function called
[05:04:08] replace. Now the replace function going
[05:04:11] to go and replace a specific character.
[05:04:13] So that means we have something old and
[05:04:15] we want to replace it with something
[05:04:16] new. Let's have a very simple example to
[05:04:18] understand it. All right. So now imagine
[05:04:19] we have a phone number where the data is
[05:04:21] splitted by a dash. Now let's say that I
[05:04:24] don't like to have the dash in my data.
[05:04:25] I would like to have slash like any
[05:04:27] other special character. Now in order to
[05:04:29] replace the dash, we can use the
[05:04:31] function replace. So we have to specify
[05:04:33] for SQL two things. The old value the
[05:04:36] dash with a new value the slash. So if
[05:04:39] you do that in the output it's going to
[05:04:41] go and remove all those dashes between
[05:04:42] the numbers and the replacement going to
[05:04:44] be the dash between them. So it's very
[05:04:47] simple, right? All what you are doing is
[05:04:48] replacing an old value with a new value
[05:04:51] and that's why we call it replace. But
[05:04:53] we can use this function as well in
[05:04:54] order to remove something not only we
[05:04:57] replace and you can do that by not
[05:04:59] specifying anything in the new value
[05:05:01] like just the single quotes and with
[05:05:03] that it's going to be nothing a blank.
[05:05:05] So now what's going to happen is still
[05:05:06] going to go and replace the dash with a
[05:05:09] blank and that means I'm just removing
[05:05:11] the dashes from the output. So if you do
[05:05:13] it you will remove the dash and you will
[05:05:15] get only numbers. So if the replacement
[05:05:17] going to be a blank then that means this
[05:05:19] function will be replacing any value
[05:05:21] that you specify. So this is exactly how
[05:05:24] it works and this is why we use the
[05:05:26] replace function in SQL. Now let's go
[05:05:28] back in order to practice. So let's do
[05:05:30] the same example. This time we're going
[05:05:31] to go and select from a static value. So
[05:05:33] we're going to get 1 2 3 4 5 6 7 8 9 0.
[05:05:37] So if you go and execute it, you can see
[05:05:39] we are getting the phone number. Now
[05:05:41] let's go and remove the dashes from this
[05:05:43] value. So let's have a new line and we
[05:05:45] start with replace. The first thing that
[05:05:47] you have to specify for SQL the value
[05:05:49] itself. So let's go and get the value.
[05:05:51] This is the first argument. The second
[05:05:53] argument going to be the old value. So
[05:05:55] the old value going to be the dash. And
[05:05:57] now the third argument will be the
[05:06:00] replacement. And since we want to remove
[05:06:02] it, we don't want to replace it with
[05:06:04] anything. We will have just single
[05:06:05] quotes and nothing between them. So
[05:06:07] there's no space between those single
[05:06:09] quotes. Now we can go and rename stuff
[05:06:11] like this is the phone. And this is a
[05:06:13] clean phone. Let's go and execute it.
[05:06:16] Now, as you can see in the output of the
[05:06:18] function, we don't have any dashes
[05:06:20] between the numbers. And you can go and
[05:06:22] test stuff. Like for example, I can go
[05:06:25] and add a slash and execute it. You will
[05:06:27] see slashes between them. So you can go
[05:06:29] and try multiple stuff. So this is one
[05:06:31] nice use case for the replace function.
[05:06:33] Now there is another use case for the
[05:06:35] replace function is that sometimes in my
[05:06:37] data file names going to be stored like
[05:06:40] for example, let's say reports.t txt and
[05:06:43] now let's say that I would like to
[05:06:44] change the file format from .txt to CSV.
[05:06:48] Now how we're going to do that we're
[05:06:49] going to go with a new line say replace
[05:06:51] and then the first argument going to be
[05:06:53] the value. So let's take our value from
[05:06:56] here and now what is the old value it's
[05:06:58] going to be the txt and I want to
[05:07:00] replace it with another format with
[05:07:03] another extension. So it's going to be
[05:07:04] the CSV. So we're going to say this is
[05:07:07] the new file name and this is the old
[05:07:10] file name. So let's go and execute it.
[05:07:13] And now as you can see in the output SQL
[05:07:15] did replace the txt with SCSV. This is
[05:07:18] as well where I use the replace function
[05:07:20] in my projects. So my friends the
[05:07:22] replace function is really fun and those
[05:07:24] are two nice use cases for the replace.
[05:07:26] All right. So that's all about the
[05:07:28] replace function in SQL and with that we
[05:07:30] have covered the whole datamations. Now
[05:07:32] in the next group we're going to talk
[05:07:33] about the calculations. And here we have
[05:07:35] only one function the
[05:07:40] length. Now the length function it's
[05:07:42] very simple. It's going to go and count
[05:07:44] how many characters you have in one
[05:07:46] value. So you are calculating the length
[05:07:48] of a value. Let's have very simple
[05:07:50] example to understand it. Okay. So now
[05:07:52] let's say that we have the value Maria.
[05:07:53] If you apply the length function for
[05:07:55] that what's going to happen? It's going
[05:07:56] to go and start counting how many
[05:07:58] characters we have inside this value. So
[05:08:00] the m is 1. a 2 3 4 5 in the output you
[05:08:04] will get the number five. So five is the
[05:08:07] length or the total number of characters
[05:08:08] in this value. Now let's say that you
[05:08:10] have a number like 350. If you go and
[05:08:12] apply the length function still is going
[05:08:14] to go and count how many digits do we
[05:08:16] have. The three is 1 5 2 3. So the total
[05:08:19] length for that going to be three. So
[05:08:21] you can apply it even for numbers and
[05:08:23] not only that you can go and apply it on
[05:08:25] a date value. So let's say that you have
[05:08:26] the following date 2026 1st 23. So SQL
[05:08:30] going to go and count each digit each
[05:08:32] character even the underscores not only
[05:08:34] the numbers underscore is as well a
[05:08:36] digit right? So the total length of this
[05:08:38] date it's going to be 10. So you can
[05:08:40] apply any data type to the links
[05:08:42] function and in the output you will get
[05:08:44] always a number. That's it. This is how
[05:08:46] you can count the number of characters
[05:08:47] in any value. Let's go back to scale in
[05:08:49] order to practice that. Okay. So now we
[05:08:51] have the task calculate the length of
[05:08:53] each customer's first name. So it is
[05:08:56] very simple. We're going to go and apply
[05:08:57] the function length len to the column
[05:09:01] first name and we're going to call it
[05:09:03] length name. So let's go and execute it.
[05:09:06] And with that as you can see we are
[05:09:07] getting in the output numbers and these
[05:09:09] numbers are the number of characters of
[05:09:12] each name of our customers. So this is
[05:09:14] how we calculate the length and that's
[05:09:16] it for this group. Now moving on to the
[05:09:18] next one. It's going to be very
[05:09:19] interesting. Now we're going to talk
[05:09:20] about how to extract something from a
[05:09:22] string value. And here we're going to
[05:09:24] cover now two functions the left and the
[05:09:29] right. Now the lift function going to go
[05:09:32] and extract specific number of
[05:09:34] characters from the start of a string
[05:09:36] value. So if you want to get few
[05:09:38] characters at the beginning of a value,
[05:09:40] you can use the lift. But now the right
[05:09:42] function is exactly the opposite. It's
[05:09:44] going to go and extract specific number
[05:09:46] of characters from the end of string
[05:09:49] value. So if you want few characters
[05:09:51] from the end of your value, you can use
[05:09:53] right. Now in order to apply the left or
[05:09:55] the right function, you have to give SQL
[05:09:57] two things. The value where you want to
[05:09:59] extract a part from it and the number of
[05:10:01] characters, how many characters you want
[05:10:03] to extract and this is the same for the
[05:10:04] left and the right. Now let's say that
[05:10:06] we have again this value Mariam. And now
[05:10:08] if the task says I would like to extract
[05:10:10] the first two characters and since we
[05:10:12] are talking about the starting position,
[05:10:14] we're going to use the lift function.
[05:10:16] And since it says two characters, we're
[05:10:17] going to go with the two. So it's going
[05:10:19] to start counting M is 1, A is two and
[05:10:22] after that it's going to stop and make a
[05:10:23] cut and it's going to go and return the
[05:10:25] two characters M A. So we are counting
[05:10:27] from the left side going to the right
[05:10:29] side. Right now if your task says
[05:10:32] extract the last two characters here we
[05:10:34] are talking about the end position of
[05:10:36] your value and for that we're going to
[05:10:38] use the right function since we are
[05:10:40] approaching from the right side and
[05:10:42] since we want only two characters the
[05:10:43] number of characters going to be two. So
[05:10:45] this time going to start counting from
[05:10:46] the right side moving to the left side.
[05:10:49] So A is one, I is two and that's it.
[05:10:52] Then SQL going to stop and extract only
[05:10:54] those two characters. I A. So if you
[05:10:56] want to extract data at the starting
[05:10:58] position, you use the left. But if you
[05:10:59] want to extract characters from the end
[05:11:01] position of your value, then you use the
[05:11:04] right function. Now let's go back to
[05:11:05] scaler in order to practice. Okay. So
[05:11:07] now we have the following task. Retrieve
[05:11:09] the first two characters of each first
[05:11:11] name. So we just need the first two
[05:11:13] characters. Since we are coming from the
[05:11:15] left side, we can go and use the
[05:11:17] function left. So it's very simple.
[05:11:19] First name and we need only two
[05:11:22] characters. So two. So we're going to
[05:11:23] call it first to character. Let's go
[05:11:26] ahead and execute it. And now you can
[05:11:28] see in the output we have two characters
[05:11:30] MA. Now with John we have only G because
[05:11:32] we have a leading space. Well, you can
[05:11:35] leave it like this or you can transform
[05:11:36] it. And then George we have G and so on.
[05:11:39] So with that we are getting the first
[05:11:40] three characters. Now in order to fix it
[05:11:42] for John what we're going to do we're
[05:11:43] going to say trim first and then apply
[05:11:46] the lift. So with that we are getting
[05:11:48] rid of all white spaces and then we
[05:11:50] apply the lift. So with that everything
[05:11:52] looks perfect. So for John we have jo.
[05:11:54] So this is how we can get the first two
[05:11:56] characters of a column. Now let's move
[05:11:58] to the next one. The task says retrieve
[05:12:01] the last two characters of each first
[05:12:03] name. So this time we need the last two.
[05:12:05] So we are coming from the right side. So
[05:12:07] we're going to do it like this. We're
[05:12:08] going to say
[05:12:10] write first name and then as well too.
[05:12:14] So last two character let's go and
[05:12:17] execute it. And now as you can see in
[05:12:18] the output we have new column where we
[05:12:20] have the last two characters from the
[05:12:21] first name. So we have here I a er and
[05:12:25] for John as well working and that's
[05:12:26] because we don't have any trailing
[05:12:28] spaces but if you have any trailing
[05:12:30] spaces then go and use that trim
[05:12:32] function. All right so that's all for
[05:12:33] the left and right and now we're going
[05:12:35] to go to the last function. we have the
[05:12:40] substring. So the substring going to go
[05:12:42] and extract a part of a string at a
[05:12:45] specified position. So this time we
[05:12:47] don't want something from the beginning
[05:12:48] or the end. We want something like in
[05:12:51] the middle. So we want to specify the
[05:12:53] starting position and we want to extract
[05:12:55] few characters from there. So let's have
[05:12:57] very simple example to understand it.
[05:12:58] Now in order to use the substring you
[05:13:00] need three things. The first one is the
[05:13:02] value itself where you want to extract a
[05:13:05] specific part from it and then you have
[05:13:07] to specify the starting position where
[05:13:09] SQL going to start extracting the
[05:13:11] characters that you want and as well SQL
[05:13:13] needs the links how many characters we
[05:13:15] have to extract. So now let's say that
[05:13:17] we have the following task after the
[05:13:19] second character extract two characters.
[05:13:22] So from reading this you can see we
[05:13:24] specified the starting position this is
[05:13:26] the second character and the length
[05:13:28] going to be the two characters. So let's
[05:13:30] have this example. Well, if you have
[05:13:31] Maria, so now we have to specify the
[05:13:33] starting position. Now we are saying
[05:13:35] after the second character. So the first
[05:13:37] character m is one. Then a is two. After
[05:13:40] two, we got the position number three,
[05:13:42] right? So starting from R. So that means
[05:13:44] we have to specify for SQL three because
[05:13:47] the starting position going to be number
[05:13:49] three. This is after the two. Now we
[05:13:51] want only two characters. So we want the
[05:13:53] R and the I. If you give this to SQL
[05:13:55] Maria starting position three and the
[05:13:58] length two, SQL can go and extract the
[05:14:00] two characters the R I. And this is
[05:14:03] exactly what we want. We want two
[05:14:04] characters after the second position,
[05:14:06] the second character. So with that, we
[05:14:08] didn't extract something from the left
[05:14:10] or from the right. We extracted at
[05:14:12] specific position. And this is exactly
[05:14:14] why we need the substring. Now let's
[05:14:16] make it a little bit more difficult
[05:14:17] where we're going to say after the
[05:14:19] second character extract everything all
[05:14:22] the characters. So not only RA I I would
[05:14:24] like RA I A. So now nothing's changed
[05:14:26] about the starting position. It's going
[05:14:27] to stay at three. But now if you are
[05:14:29] looking to this value and you want to
[05:14:31] extract everything starting from R. That
[05:14:33] means you have to specify the length of
[05:14:35] three. But this is not really good
[05:14:37] because let's have another value in the
[05:14:39] same column. So we have Martin. So the
[05:14:41] starting position going to be as well R.
[05:14:43] And now the lengths going to be
[05:14:45] different. So we have here four
[05:14:47] characters. So now the length is not
[05:14:48] anymore three. It is four. But you have
[05:14:50] to specify something at the end for SQL.
[05:14:52] You can go for four. That's fine for
[05:14:54] Maria as well. But if you have a lot of
[05:14:56] values, it's going to be really hard to
[05:14:58] specify exactly the correct length.
[05:15:00] That's why instead of specifying a
[05:15:02] static number like three or four, we can
[05:15:05] use another function. So now my friends,
[05:15:07] if you use the length function, you will
[05:15:09] get the total number of characters,
[05:15:11] right? So for Maria, you will get five.
[05:15:13] For Martin, you will get six. And those
[05:15:15] numbers are okay to use in the length
[05:15:17] because they are more than what we need.
[05:15:19] And that's totally fine. So if you are
[05:15:21] saying okay for Maria start from the
[05:15:23] third position and cut for me five
[05:15:25] characters SQL going to find only three
[05:15:27] but you will not get an error. So you
[05:15:29] are extracting more than you need and
[05:15:31] you will always get all the characters
[05:15:32] after the starting position. So this is
[05:15:34] a little trick that we use in order to
[05:15:37] make the links dynamic where we cannot
[05:15:39] find one value that we can use in all
[05:15:41] scenarios. And now let's go back to SQL
[05:15:43] in order to practice the substring.
[05:15:46] Okay. So now we have the following task
[05:15:47] and it says retrieve a list of customers
[05:15:50] first names after removing the first
[05:15:52] character. So now don't ask me why but
[05:15:54] for some reason we don't want to see the
[05:15:56] first character of the first names. We
[05:15:58] want to remove it. So how we can do
[05:16:00] that? We cannot use the left or the
[05:16:01] right. We have to go with the substring
[05:16:04] because it is little bit more
[05:16:05] complicated. So substring and let's go
[05:16:08] and get and the first argument going to
[05:16:10] be the value. So it comes from the first
[05:16:12] name and then the second argument is the
[05:16:14] starting position. So where we want to
[05:16:16] start since it is saying I want all the
[05:16:18] characters after the first character. So
[05:16:21] that means we will be starting from the
[05:16:23] position number two. So for example
[05:16:25] Maria here the first character M
[05:16:27] position number one and we want to start
[05:16:29] our substring from the position number
[05:16:31] two. So that was so that was the easy
[05:16:33] part. Now the next one the question is
[05:16:36] how much characters we want to leave. So
[05:16:38] do we leave here like four characters
[05:16:41] like in Maria we have four characters
[05:16:43] but in John we have only three then the
[05:16:45] next one is four and so on. So if you go
[05:16:48] for example with four and let's call it
[05:16:51] sub name. So we make it static. What can
[05:16:54] happen? It's going to work for some
[05:16:56] scenarios like Maria. We have here Ara
[05:16:58] and for better we are getting it. But
[05:17:01] for Martin it is not working. We are not
[05:17:03] getting the last N because it has like
[05:17:06] five characters after the first one. And
[05:17:08] by just looking to the result as you can
[05:17:10] see we have here one issue with John and
[05:17:12] that's because the first character is an
[05:17:14] empty string. So this is really
[05:17:16] annoying. So that's why we use the trim
[05:17:18] first just to get rid of all those white
[05:17:21] spaces. And now you can see it's working
[05:17:23] fine. So we are not getting the J. We
[05:17:25] have everything after the first
[05:17:26] character. So now instead of having this
[05:17:28] static what we're going to do we're
[05:17:30] going to make it variable. So we're
[05:17:32] going to go and use the length of the
[05:17:35] first name. So with that we make sure we
[05:17:38] have enough length to extract. And this
[05:17:41] can work for any value inside the first
[05:17:43] name even if the name is like 20
[05:17:45] characters. So let's go and execute. And
[05:17:48] now you can see for Martin it is now
[05:17:50] working. So we have here like five
[05:17:51] characters after the M. And here we have
[05:17:53] four characters after the M as well. And
[05:17:56] here we have three characters after the
[05:17:57] G. So it is working completely and it is
[05:18:00] full dynamic. So this is the trick by
[05:18:03] using the links together with the
[05:18:04] substring. And as you can see now we are
[05:18:06] using three functions in one go. We have
[05:18:09] the length, we have the trim and we have
[05:18:11] the substring. And this is what happens
[05:18:12] in scale. we use multiple functions
[05:18:15] together in order to solve like complex
[05:18:17] tasks. So this is how you can extract a
[05:18:20] substring from a string. All right. So
[05:18:23] that's all about the substring and with
[05:18:24] that we have covered a lot of very
[05:18:26] important string functions in SQL and
[05:18:28] now you have enough tools in order to
[05:18:30] manipulate the string values in your
[05:18:32] data. Okay my friends. So with that we
[05:18:34] have learned how to manipulate your
[05:18:35] string values inside SQL using the
[05:18:37] string functions. Now we will move to
[05:18:39] the second one. you will learn how to
[05:18:41] manipulate the numbers, the numeric
[05:18:43] values. So let's
[05:18:48] go. Okay. So now let's have this example
[05:18:51] 3.516. Now let's say that you want to
[05:18:53] apply the function round and you are
[05:18:55] using two decimal places. So what going
[05:18:57] to happen? It's going to go and keep
[05:18:59] only two digits after the decimal point.
[05:19:01] So five and one and the third digit
[05:19:04] after the decimal six. It will decide
[05:19:06] whether the number going to round up or
[05:19:08] stay as it is. And now since six is
[05:19:10] higher than five. So that means SQL
[05:19:13] going to go around the numbers up. So
[05:19:15] instead of having 51 we will get 52. And
[05:19:18] after that the third digit going to
[05:19:20] reset to zero. So in the out you will
[05:19:22] get
[05:19:23] 3.52. Now let's say that you have done
[05:19:25] round but only for one decimal place.
[05:19:28] Now it's still going to go and keep only
[05:19:30] one decimal place and that is the five.
[05:19:32] And the second digit this time going to
[05:19:34] decide whether we round up or not. And
[05:19:36] now since one is less than five, there
[05:19:39] is no need to round up and the five
[05:19:40] going to stay as it is. It will not turn
[05:19:42] to six. So there is no round up and the
[05:19:45] digits after the five going to reset to
[05:19:47] zero. So we're going to get 3.5. Now
[05:19:49] let's say that you say round zero. So
[05:19:51] that means I don't want to see any
[05:19:53] digits after the decimal point. So now
[05:19:55] SQL going to go and check the first
[05:19:57] digit after the decimal point, the five.
[05:19:59] This one going to decide whether the
[05:20:01] three going to turn to four or not. And
[05:20:03] now since we have five it is good enough
[05:20:05] to round the number because either five
[05:20:07] or above five going to round the
[05:20:10] numbers. So that's why it's going to be
[05:20:12] a round up and SQL going to return at
[05:20:14] the end four and all the digits after
[05:20:16] the decimal points going to be reset to
[05:20:18] zero. So this is exactly how the round
[05:20:20] function works in SQL. So now let's see
[05:20:22] how we can do that in SQL. Okay. So now
[05:20:25] let's go and practice about the number
[05:20:26] functions. So what we're going to do
[05:20:27] we're going to write SQL select but this
[05:20:29] time we will not select any data from
[05:20:31] the database. We going to practice using
[05:20:33] our static value like for example the
[05:20:35] value 3 dot 516. So let's go and execute
[05:20:39] it. So with that I have this decimal
[05:20:41] number. Now let's go and start
[05:20:43] practicing the round function. So now
[05:20:45] let's go and round this number
[05:20:48] 3.516 and this time we are rounding to
[05:20:51] decimals. So let's go and call it round
[05:20:54] two and let's go and execute it. So as
[05:20:56] you can see in the output we are
[05:20:57] rounding two decimal places and we have
[05:21:00] the two because as we learned the six
[05:21:02] going to go and round it up. Now let's
[05:21:04] go and do the same thing for one. So
[05:21:07] let's round one execute. And as you can
[05:21:10] see in the output we are rounding to one
[05:21:12] decimal. So we have the five and
[05:21:14] everything is zero. And we don't have
[05:21:16] six here because the one is lower than
[05:21:18] five and it will not round up the
[05:21:20] numbers. And let's and round by the
[05:21:23] zero. it is rounding it to an integer to
[05:21:27] the four and all the decimal digits are
[05:21:29] zero and we have four because we have
[05:21:31] five and five going to round up the
[05:21:33] number. So as you can see it is really
[05:21:35] nice and this is how we round numbers in
[05:21:40] SQL. Now there is another number
[05:21:42] function which is really cool called APS
[05:21:45] or the absolute what it going to do it's
[05:21:47] going to go and convert any negative
[05:21:49] number to a positive. So let me show you
[05:21:51] what I mean. Let's go and say we have
[05:21:53] like minus 10. So this is a negative
[05:21:55] number. But if I say APS, so the
[05:21:57] absolute of the minus 10, what I will
[05:22:00] get? I will get a positive number. So
[05:22:02] it's like giving us the absolute of any
[05:22:04] number or in other words, it is like
[05:22:06] converting the negative to a positive.
[05:22:08] And if the number is already positive,
[05:22:11] nothing going to happen. So if I say the
[05:22:13] absolute of the 10, I will get as well a
[05:22:15] 10. So this is really nice and cool
[05:22:17] function that is really important in
[05:22:19] order to transform numbers in many
[05:22:21] scenarios like if you have mistakes on
[05:22:23] your database like let's say minus sales
[05:22:26] makes no sense to have sales that is
[05:22:28] minus. So in order to correct the data
[05:22:29] we can use the APS in order to convert
[05:22:32] all the negative numbers to a positive.
[05:22:34] So this is really nice cool and easy
[05:22:36] function to learn. All right my friends.
[05:22:38] So that's all for the numeric functions.
[05:22:40] We have covered two very simple
[05:22:41] functions and now in the next topic we
[05:22:43] have a lot of functions about how to
[05:22:45] manipulate the date and time in SQL. So
[05:22:47] let's
[05:22:51] go. So what is a date? If you take a
[05:22:54] look at calendar and you pick any date,
[05:22:57] for example, August 20th,
[05:23:00] 2025, this date could represent an event
[05:23:03] like a birth date. Happy birthday.
[05:23:10] or a project deadline at your work and
[05:23:13] mainly it has three components. The
[05:23:15] first part is a fourdigit number
[05:23:17] indicating the year. Then the next
[05:23:19] component it is the month. So normally
[05:23:22] we represent the month with a number
[05:23:24] between 1 and 12. And the last component
[05:23:26] is the day. This is a number between 1
[05:23:29] and 31 depending on the month. Now in
[05:23:31] database we call this structure of those
[05:23:33] three components a date. So this is what
[05:23:36] we mean with dates in SQL. All right.
[05:23:38] All right. So now let's move to the next
[05:23:39] one. What is time? Time refers to a
[05:23:42] specific point within a day. Like for
[05:23:44] example, we have 18:00, 55 minutes, and
[05:23:47] 45 seconds. So this structure has as
[05:23:50] well three components. The first one we
[05:23:52] call it the hours. It is as well a
[05:23:54] number between 0 and 23 indicating the
[05:23:57] hour of the day. Then the next one, it
[05:23:59] is the minutes. This is a number between
[05:24:01] 0 and 59. Moving on to the last
[05:24:03] component, we have the second. This is
[05:24:05] again the same thing a number between 0
[05:24:08] and 59. So now this structure with those
[05:24:11] three components we call it in databases
[05:24:13] and SQL a time. So this is what we mean
[05:24:15] with the time. Now to the last type if
[05:24:18] you go and combine both the date
[05:24:20] together with the time and you put them
[05:24:22] side by side you will get a new
[05:24:25] structure and a new name in the
[05:24:26] databases and we call it usually time
[05:24:29] stamp. This name is used in many
[05:24:31] databases like Oracle, Postgress and
[05:24:33] MySQL. But in the SQL server, we have
[05:24:35] another name for that. We call it date
[05:24:37] time. So again, it's very simple. The
[05:24:39] date time or time stamp has the date
[05:24:41] information together with the time
[05:24:43] information. So here in this example, we
[05:24:45] have six components from left to right
[05:24:47] and here we have like a hierarchy in
[05:24:49] this structure. So we start with the
[05:24:50] highest which is the year. Then we have
[05:24:53] the month, the day and then we continue
[05:24:55] to the hour, minutes and seconds. So
[05:24:58] those are the three different types
[05:25:00] about date and time informations in SQL.
[05:25:02] We have the date alone or the time alone
[05:25:05] or together in the date time. All right,
[05:25:08] let's explore now the data that we have
[05:25:10] inside our database searching for date
[05:25:12] and time informations. Now let's go to
[05:25:14] the table orders and if you go and
[05:25:16] expand it, you will find here two
[05:25:18] columns having the data type dates. So
[05:25:20] we have the order dates with the date
[05:25:22] and as well the shipping date with the
[05:25:24] data type dates. And if you check the
[05:25:26] last column, the creation date, this one
[05:25:28] is date time 2. So now let's go and
[05:25:30] query those informations in order to
[05:25:32] understand the structure. I'm just going
[05:25:34] to select the order ID, the order date,
[05:25:38] and the ship
[05:25:39] date and the creation
[05:25:43] time from sales orders and from is big.
[05:25:47] So let's go and execute it. Now if you
[05:25:49] go and check both order date and ship
[05:25:51] date, you can find that here we have
[05:25:53] only the structure or the informations
[05:25:55] about the date and we have nothing about
[05:25:57] the time. So again here we have a year,
[05:25:59] month and day and that's why they have
[05:26:01] the data type date. Now let's go and
[05:26:03] check the creation time. Not only we
[05:26:05] have the date information but as well we
[05:26:07] have the time information. So it start
[05:26:09] with the date information year, month,
[05:26:11] day and then we have hour, minute and
[05:26:14] seconds and then we have fractions of
[05:26:16] the seconds, milliseconds and so on. So
[05:26:18] this is how the date time or time stamp
[05:26:20] looks like in databases and this is how
[05:26:22] the date looks
[05:26:27] like. All right my friends now in SQL I
[05:26:30] can say that we have three different
[05:26:32] sources in order to query the dates. The
[05:26:35] first one is dates that are stored
[05:26:37] inside our database like we saw here in
[05:26:40] those columns like the order date,
[05:26:41] shipping date, creation time. All those
[05:26:44] are columns that holds this informations
[05:26:46] and they are stored inside our database.
[05:26:49] So this is the first source of dates
[05:26:50] that we can get inside our queries. Let
[05:26:52] me just remove those stuff and let's
[05:26:54] stick with the creation time. So let's
[05:26:56] just execute it. So those are date and
[05:26:58] time informations stored inside our
[05:27:00] database. The second type is a
[05:27:02] hard-coded date string that we can use
[05:27:05] inside our queries. Let me show you an
[05:27:07] example. So now if we go to a new line,
[05:27:09] I can go and define a date like this. So
[05:27:13] 2025 August 20th. So that in this string
[05:27:17] we have hardcoded a date that is static
[05:27:20] for all rows. Let me just call it
[05:27:23] hardcoded and let's go and execute it.
[05:27:26] Now we can see in the output we're going
[05:27:27] to get a static date for all rows. So
[05:27:30] this going to be the same for all rows
[05:27:32] inside our table. So this value is not
[05:27:34] stored inside our database. This value I
[05:27:37] just added to our query and hardcoded
[05:27:39] it. So sometimes in queries we define
[05:27:41] our dates that's going to be used maybe
[05:27:43] later in calculations and so on. Now the
[05:27:46] third source of getting dates inside our
[05:27:48] query is using the function get date.
[05:27:51] Get date is the first and the most
[05:27:53] important function that we use in SQL.
[05:27:55] It's going to go and return the current
[05:27:57] date and time at the moment of executing
[05:28:00] the query. So let's try that out. I'm
[05:28:02] going to go and get a new line. So get
[05:28:04] dates. It's very simple. It doesn't
[05:28:06] accept any values inside the function.
[05:28:08] So it's going to be empty. So let's call
[05:28:10] it today. All right. Let's go and
[05:28:12] execute it. And of course, we're going
[05:28:14] to get different results because the get
[05:28:16] date now is the date and the time that
[05:28:18] I'm recording this video. So currently
[05:28:21] it is July 18, 2024. And I'm recording
[05:28:24] this around 20 p.m. So as you can see,
[05:28:26] this going to be as well repeated for
[05:28:28] each row. We're going to get always the
[05:28:30] same value. So again, this depend on the
[05:28:32] execution of that query. So during the
[05:28:34] tutorial, you're going to learn a lot
[05:28:36] about the get date and we're going to
[05:28:37] use it in a lot of functions. So those
[05:28:39] are the three different sources of
[05:28:40] getting date information inside your
[05:28:42] query either from a column inside our
[05:28:44] database or hardcoded using a string.
[05:28:47] And the third one is using the get date
[05:28:49] in order to get the current date and
[05:28:51] time informations at the moment of the
[05:28:54] query
[05:28:58] execution. Nice. Now we have a clear
[05:29:00] understanding what is date and time in
[05:29:02] SQL. The next question is how to
[05:29:04] manipulate those informations using SQL
[05:29:07] functions. Okay. Now we have our date
[05:29:09] August 20th, 2025. One of the things
[05:29:11] that we can do with the date is we can
[05:29:13] go and extract different parts of the
[05:29:16] date. For example, we are interested
[05:29:17] only on the year. So we can go and
[05:29:19] extract only the year part. Or if you
[05:29:21] are interested in the month, you can go
[05:29:23] and extract the month and you will get
[05:29:24] August. And of course, we can go and
[05:29:26] extract the day and we will get the 20.
[05:29:28] So this is the first thing that we can
[05:29:30] do. We can extract the parts of the
[05:29:32] dates. Now another thing that we can do
[05:29:33] is we can go and change the date format.
[05:29:36] So instead of having like a small minus
[05:29:39] between those date parts, we can go and
[05:29:41] split them using slash. We can even
[05:29:43] start first with the month August then
[05:29:45] 20 the day and then the year but having
[05:29:48] only the short form of the year 25 or we
[05:29:50] can go and change the format where we
[05:29:52] say we don't need any special character
[05:29:54] we just leave it as a space. So as you
[05:29:56] can see we are changing and manipulating
[05:29:58] the format of the date. Another category
[05:30:00] or task we can go and do date
[05:30:02] calculations. So we can go and take our
[05:30:04] date and add to it for example 3 years
[05:30:07] or we can go and find the differences
[05:30:09] between two dates like we are doing a
[05:30:11] subtraction or let's say minus and we
[05:30:13] will get for example 30 days. So we can
[05:30:16] go and add stuff subtract stuff or find
[05:30:18] differences between two dates. It's like
[05:30:20] we are doing calculations on the date.
[05:30:21] Now to the last thing that we can do
[05:30:23] with this date is we can go and test
[05:30:25] this date or validate it whether it is a
[05:30:28] real date that SQL understands. So we
[05:30:31] can put it on the test and at the output
[05:30:33] we're going to get true or false or zero
[05:30:35] and one. So as you can see here we have
[05:30:36] different ways or let's say categories
[05:30:39] on how to manipulate our dates in SQL.
[05:30:41] Now we're going to go and group up the
[05:30:43] different date and time functions under
[05:30:45] four categories. The first category and
[05:30:47] the most important one we have the part
[05:30:49] extraction and here we have around seven
[05:30:51] different functions that we can use in
[05:30:54] order to do this task. Another category
[05:30:56] we have the format and casting. And here
[05:30:58] we have three different functions.
[05:31:00] Underneath this category we have the
[05:31:01] format, convert and cast. And then the
[05:31:04] third category we have the calculations
[05:31:06] of the dates. We have two functions date
[05:31:08] add and date diff. And the last category
[05:31:10] the validation. We have here only one
[05:31:12] function called is dates. So as you can
[05:31:14] see we have a lot of scale functions. We
[05:31:16] have 13 date and time functions that
[05:31:19] we're going to cover in this tutorial on
[05:31:20] how to manipulate the date and time
[05:31:22] informations in SQL. And this is how we
[05:31:24] can group them into four different
[05:31:26] categories. Let's start now with the
[05:31:28] biggest category. We have the part
[05:31:29] extraction. We're going to cover all
[05:31:31] those seven functions in details on how
[05:31:33] to extract
[05:31:37] parts. All right friends, now we're
[05:31:39] going to cover three very easy quick
[05:31:41] functions in SQL to extract the parts of
[05:31:43] the dates. So they are very simple. The
[05:31:45] day function going to return a day from
[05:31:48] a date and in the same way the month
[05:31:50] going to return the month from a date
[05:31:53] and guess what the year going to return
[05:31:56] a year from a date. Okay. So now in
[05:31:58] order to understand how they work we
[05:32:00] have a date like this one 2025 August
[05:32:03] 20th. Sometimes you are not interested
[05:32:05] in the whole date. You would like to get
[05:32:07] only a part from this date. So you go
[05:32:09] and use the function day in order to
[05:32:12] extract the two digit 20. Now in other
[05:32:15] scenario you might be interested in the
[05:32:17] month information. So you would like to
[05:32:19] get those two digits 08. So we can use
[05:32:22] the function month in order to extract
[05:32:25] the month information in order to get
[05:32:27] the August. So 08 and one more situation
[05:32:30] where you want to have only the year
[05:32:32] information. So you are interested in
[05:32:34] the four digits 2025. So you can go and
[05:32:37] use the function year in order to
[05:32:39] extract it. So in the output if you
[05:32:41] apply it you will get 2025. So it's very
[05:32:44] simple. This is how those three
[05:32:45] functions work. All right. Now let's
[05:32:47] check the syntax of those three
[05:32:48] functions. It's pretty easy. So we have
[05:32:50] it always like this. A keyword called
[05:32:52] day. This is the function name. And then
[05:32:54] it accept only one parameter. It is the
[05:32:57] date. The same things for the others. We
[05:32:59] have a function called month and it
[05:33:01] accept as well only one parameter the
[05:33:03] date and as well for the year the same
[05:33:04] thing. So the syntax is very
[05:33:06] straightforward. It accept only one
[05:33:07] value the date and we have the function
[05:33:10] name like the name of the part that we
[05:33:12] want to extract. All right. So now let's
[05:33:14] try out those functions. I will be
[05:33:16] working with the column creation time.
[05:33:18] So let's try for example extracting the
[05:33:20] year from the creation time using the
[05:33:22] year function. So it's going to be very
[05:33:24] simple. It's going to be year and then
[05:33:27] creation
[05:33:28] time like this. And let's call it year.
[05:33:32] That's it. Let's go and execute it. Now
[05:33:34] as you can see it's very simple. We have
[05:33:35] only one year 2025 from the creation
[05:33:38] time. So with that as you can see we got
[05:33:40] a new column where we have only the year
[05:33:42] informations inside it. And this
[05:33:44] information come from the creation date.
[05:33:45] So we have only 2025. Now let's go and
[05:33:48] do the same for the month. So we're
[05:33:50] going to have the same thing month
[05:33:53] creation time and let's call it month.
[05:33:56] So let's execute it. Now as you can see
[05:33:58] in the output we got as well the number
[05:34:00] of the month. So we have here January,
[05:34:02] February and March and those information
[05:34:04] as well are extracted from the creation
[05:34:07] time and the same thing using the day
[05:34:09] function. So let's go and use that. So
[05:34:13] creation time and we call it day. So now
[05:34:17] as you can see in the output we have the
[05:34:19] day part from the creation time. So here
[05:34:22] we have 1, 5, 10 and so on and all those
[05:34:25] informations come from the creation
[05:34:26] time. So as you can see those three
[05:34:28] functions are very simple and quick in
[05:34:30] order to extract parts from a date or
[05:34:33] date
[05:34:33] [Music]
[05:34:37] time. All right. So what is date part?
[05:34:39] Date part going to go and return
[05:34:41] specific part of the date as a number.
[05:34:44] All right. So now back to our example.
[05:34:45] We have learned how to extract the day,
[05:34:47] month and year. But of course now in a
[05:34:50] day we have more informations that we
[05:34:52] could extract. Not only those three we
[05:34:54] could extract for example the week right
[05:34:57] the quarter so all those informations
[05:34:59] are as well stored in this dates we
[05:35:02] cannot see it like as a value but inside
[05:35:05] the SQL you can extract the week and
[05:35:07] quarter but we don't have a function
[05:35:09] dedicated for those stuff because they
[05:35:12] are not commonly used like the year and
[05:35:14] month and day but still we can extract
[05:35:16] those information using the date parts
[05:35:19] for example we can say date part and we
[05:35:22] can specify the part as a week and with
[05:35:24] that SQL going to return for this
[05:35:26] example 34 and maybe in other situation
[05:35:28] you are interested in the quarter right
[05:35:30] so you can specify it like this date
[05:35:33] part quarter so we are interested in the
[05:35:35] part of quarter and in the output you
[05:35:37] will get three so this is exactly the
[05:35:39] power of the date part you can go and
[05:35:42] extract way more parts that is available
[05:35:45] in these dates and one more thing to
[05:35:47] notice about the date part year and day
[05:35:50] all of them are always generating the
[05:35:52] output an integer a number. So we have
[05:35:55] the for the quarter 3 for the week 34
[05:35:58] the day 20 2025 and so on. So all of
[05:36:01] those informations are integer. So
[05:36:03] integer is the data type of the output
[05:36:06] of these functions. Okay. So let's have
[05:36:08] a look to the syntax of the data part.
[05:36:09] It start with the function name date
[05:36:12] parts and it accept two parameters. The
[05:36:14] first one is the part that we want to
[05:36:16] extract. So we want to define what do we
[05:36:18] want. We want the month, the day, the
[05:36:20] year and so on. And the second parameter
[05:36:23] is the date itself. So let's have an
[05:36:25] example. We can say date part and we
[05:36:27] would like to extract the month from the
[05:36:29] order dates. So the part is the month
[05:36:31] and the order date is the date that we
[05:36:33] want to extract from. So with that we
[05:36:35] are specifying the part as a month. Now
[05:36:37] in SQL there is another way on how to
[05:36:40] specify the parts. We can go and use
[05:36:42] like an abbreviation of the month. So if
[05:36:45] you specify instead of month instead of
[05:36:47] writing the whole thing you write mm you
[05:36:49] will get the same results. So it's like
[05:36:51] abbreviation and shortcut in order to
[05:36:53] write scripts. But I rarely see that in
[05:36:55] the implementations. I always tend to
[05:36:56] write it completely like this month
[05:36:58] because it's more like standards if you
[05:37:00] are switching between different
[05:37:01] databases. So as you can see it's very
[05:37:03] simple. You have to give SQL two things
[05:37:05] which part you want to extract and the
[05:37:07] date that you want to extract from.
[05:37:09] Okay. So now we're going to go and
[05:37:10] extract different parts from the
[05:37:11] creation time using the date part. Let's
[05:37:14] start for example by extracting the year
[05:37:16] again. So let's go and do that. date
[05:37:20] parts and then we have to specify which
[05:37:23] part we need. So we're going to write
[05:37:24] year like this and then the next one
[05:37:27] going to be the value. So it's going to
[05:37:29] be the creation time. So let's call it
[05:37:33] year and let's say date parts. Let's go
[05:37:36] and execute it. So now at the output you
[05:37:38] can see we got as well again the years
[05:37:41] that is extracted from the creation
[05:37:42] time. So it's going to be identical to
[05:37:44] the year function. So there is no
[05:37:46] differences between them. Both of them
[05:37:48] are integer and it holds the year
[05:37:50] informations. Now we can go and try
[05:37:52] different parts. For example, let's copy
[05:37:54] the whole thing and let's extract for
[05:37:57] example the month. So you can go over
[05:37:59] here and change it to month and let's
[05:38:02] rename it
[05:38:03] execute. So at the output you see we got
[05:38:06] as well the months is identical as well
[05:38:08] to the function month. And the same
[05:38:10] thing for the day. So we are just
[05:38:13] changing the
[05:38:14] parts and in the output we are getting
[05:38:17] the parts. So here we have as well the
[05:38:19] days it is identical to the day
[05:38:21] function. So so far we don't have
[05:38:22] something new from the date part because
[05:38:24] we have it already from the other
[05:38:26] functions. But now we're going to go and
[05:38:28] extract other parts that are not year
[05:38:30] month and day. So for example let's go
[05:38:33] and get the hours. So we have the date
[05:38:36] part and here as a part you say hour and
[05:38:40] let's call it here as well hour. Let's
[05:38:43] go and execute it. Now you can see in
[05:38:45] the output we have a new dedicated
[05:38:47] column that shows only the information
[05:38:49] from the hour. So we have here 12 23 and
[05:38:52] so on. And those informations comes from
[05:38:54] the time and the same thing you can
[05:38:56] define minutes and so on. But now let's
[05:38:59] go and get something interesting like
[05:39:00] the quarter. So let's go and duplicate
[05:39:05] it and instead of hour let's get
[05:39:07] quarter. So this information it's not
[05:39:09] displayed in the creation time but SQL
[05:39:12] can go and extract it. So let's call it
[05:39:15] quarter and let's go and execute it. Now
[05:39:18] as you can see in the output we have one
[05:39:20] new field called quarter and inside it
[05:39:22] everywhere we have a one because all
[05:39:24] those dates are in the range of the
[05:39:26] quarter one. So as you can see this is
[05:39:28] amazing of course for reporting and
[05:39:30] analyzes. Let's go and have something
[05:39:33] else like the week day. So we are over
[05:39:36] here quarter and let's call it week day
[05:39:40] and rename as well this to week day. So
[05:39:44] let's go and execute it. All right. So
[05:39:45] now let's go and get something else like
[05:39:47] for example the week. So I just
[05:39:50] duplicated over here instead of quarter
[05:39:52] let's write week. So I would like to get
[05:39:54] the week number. So let's go and execute
[05:39:58] it. So now in the output as you can see
[05:39:59] we got a dedicated field that show us
[05:40:01] the week number from the creation time.
[05:40:04] So we can see this dates come from the
[05:40:06] week number one. Those two come from
[05:40:08] week number two and so on. So that's it.
[05:40:11] As you can see guys all those
[05:40:12] informations that you are getting from
[05:40:14] the date part are numbers. And now we
[05:40:16] can extract way more informations than
[05:40:19] only the year, month and day. And even
[05:40:21] if those informations are not displayed
[05:40:23] directly in the field itself like the
[05:40:26] quarter, weeks and so
[05:40:27] [Music]
[05:40:30] on. All right. So now we have very
[05:40:32] similar function to the date part. We
[05:40:34] have the date name. So the only
[05:40:36] difference here is that it returns the
[05:40:38] name of the date parts. All right. So
[05:40:40] now back to our example. We have learned
[05:40:42] we can extract different types of parts
[05:40:44] from one date. But we learned as well
[05:40:47] that all of them are numbers. How about
[05:40:49] we would like to extract the name of the
[05:40:51] month. So instead of eight, I would like
[05:40:53] to get the name of the month like
[05:40:55] August. Or instead of the 20, I would
[05:40:57] like to get the day name like here in
[05:41:00] this example, it going to be Wednesday.
[05:41:02] So in order to get the name of the
[05:41:04] parts, we have to use the function date
[05:41:06] name. So for example, if you use the
[05:41:08] function date name using the part month,
[05:41:11] you will not get eight in the output.
[05:41:13] You will get the full name of the month
[05:41:15] August. So as you can see we are getting
[05:41:17] a string a full name and as well the
[05:41:19] same thing if you use date name for the
[05:41:21] week day you will not get 20 like the
[05:41:24] day function you will get the name of
[05:41:26] the day Wednesday and as well here the
[05:41:28] output is string so as you can see it's
[05:41:30] very simple we are using the date name
[05:41:32] in order to get the name of the parts
[05:41:35] and the data type of the output here is
[05:41:37] a string it is not an integer so as you
[05:41:39] can see here we have different types of
[05:41:41] functions that all of them are doing the
[05:41:43] same job we are extracting ing parts
[05:41:46] from one date. Okay. So now by checking
[05:41:48] the data name syntax, it's going to be
[05:41:51] identical to the date part. So we are
[05:41:53] just switching the function name. It
[05:41:56] needs from you to define the part and as
[05:41:58] well the dates. The only difference here
[05:42:00] is that we are getting different data
[05:42:01] type at the output. So here we are
[05:42:03] getting a string instead of integer. All
[05:42:05] right. So now let's check the date name.
[05:42:07] It is very similar to the date part. So
[05:42:09] we're going to have it like this. We're
[05:42:10] going to work as well with the creation
[05:42:12] time. So we're going to say date name
[05:42:14] and then after that we have to define
[05:42:16] the parts. So let's go for example with
[05:42:18] the month and our field is as usual the
[05:42:21] creation time and let's call it month
[05:42:25] date
[05:42:26] name like this. So that's it. Let's go
[05:42:29] and execute it. Now if you go to the
[05:42:31] output over here you can see we have the
[05:42:33] month but this time we don't have
[05:42:35] numbers. We have the full name of the
[05:42:37] month. So we have January, February,
[05:42:39] March instead of having 1 2 3. So this
[05:42:42] is the big difference between the date
[05:42:44] name and date part. Date part you get
[05:42:45] numbers. Date name you get the name of
[05:42:48] the part. So let's do the same thing for
[05:42:50] the day. We would like to get the name
[05:42:53] of the day. So I'm just duplicating it.
[05:42:55] But now in order to get the full name of
[05:42:57] the day, we cannot go with the day.
[05:42:59] We're going to go with the week day as a
[05:43:02] part. So that's it. I will call it week
[05:43:05] day. So let's execute it. Now as you can
[05:43:07] see in the output, we have here a new
[05:43:10] column called week day. And inside it we
[05:43:12] have the name of the day instead of a
[05:43:14] number. So here we have Wednesday,
[05:43:16] Sunday, Friday and so on. So the full
[05:43:18] name of the day go of course with the
[05:43:20] day. Let's go and try that
[05:43:23] out. So this is the day of the month and
[05:43:27] of course the day of the month has no
[05:43:29] name and SQL of course going to return
[05:43:31] the numbers again. So you can see 1 5 10
[05:43:33] 20 and so on. But still there is a
[05:43:35] difference between the day from the day
[05:43:37] name and the day from the date parts. In
[05:43:40] the date parts we are getting integers.
[05:43:43] So if you store this information in a
[05:43:44] new table it's going to be stored as an
[05:43:46] integer. But in the date that you are
[05:43:49] getting from the date name it is a
[05:43:51] number but still it can be stored as a
[05:43:53] string value. So the data type of those
[05:43:56] numbers is a string and the data types
[05:43:58] of the day from the date part is an
[05:44:00] integer. And the same thing can happen
[05:44:02] if you extract for example a year. So
[05:44:04] you don't have like a full text of the
[05:44:06] year. So let me just do it like this. So
[05:44:10] if we say a year, you will not get the
[05:44:13] name of the year. You're still getting
[05:44:15] the numbers, the digits, but the data
[05:44:17] type here is a string. So that's it.
[05:44:19] This is the difference between the date
[05:44:21] name and the date parts. For the month
[05:44:23] and weekday, you will get the full name.
[05:44:25] For the other stuff, you will get
[05:44:26] numbers but with the string data type.
[05:44:29] So the most important thing about the
[05:44:30] date name is to present easy to read and
[05:44:34] human readable informations to the
[05:44:35] users. So imagine you are building a
[05:44:37] report called sales by month and then
[05:44:39] you show to the user the muscles as
[05:44:41] numbers 1 2 3 until 12. This is of
[05:44:43] course okay but it is way more nicer if
[05:44:46] you present those informations as a full
[05:44:48] text. So you go with the date name in
[05:44:50] order to show instead of one you show
[05:44:52] January, February, March and the full
[05:44:54] name of the month. And this going to
[05:44:56] look way nicer in reporting for the
[05:44:58] users. So this is the core use case of
[05:45:00] the date
[05:45:04] name. So what is date trunk? Date trunk
[05:45:07] going to go and truncate the date to a
[05:45:10] specific part. So let's understand what
[05:45:13] this means. Okay. Now let's check the
[05:45:15] syntax of the date trunk. It's going to
[05:45:16] be exactly the same like date part and
[05:45:18] date name. So you have to define the
[05:45:20] part and the date that you want to
[05:45:22] extract apart from it. So the only thing
[05:45:25] that is different here we are giving
[05:45:27] different function name. So as you can
[05:45:29] see all those three functions like
[05:45:31] having the same structure you have to
[05:45:33] provide which part you want to extract
[05:45:35] like a month, day, week, hour, minutes
[05:45:38] and so on and the date or date and time
[05:45:41] that you want to extract a part from it
[05:45:43] and of course with the date trunk we are
[05:45:44] getting at the output date or date time.
[05:45:47] Okay. So now let's understand exactly
[05:45:49] how the date trunk works. We have the
[05:45:51] following date time and as we learned we
[05:45:53] have like a hierarchy where we start
[05:45:55] with the highest from the year then we
[05:45:57] move to the month, day, hours, minutes
[05:46:00] and seconds and by looking to this
[05:46:02] information it is very precise. We know
[05:46:04] exact second for this information right?
[05:46:07] So the level of details here is very
[05:46:09] high. We know the seconds of this event.
[05:46:12] So now the date going to allow us to
[05:46:14] change this level of details of this
[05:46:16] information by specifying the level of
[05:46:18] details. Let's take for example if we
[05:46:20] say the date trunk minutes. So we are
[05:46:23] saying we are interested only at the
[05:46:25] minutes level. We are not interesting
[05:46:26] with the seconds. So what can happen?
[05:46:29] Everything between the year and the
[05:46:31] minutes going to be kept. That means all
[05:46:33] those information will not be changed
[05:46:35] but only the seconds going to be
[05:46:38] reseted. We are not interested anymore
[05:46:40] with the seconds. This is very detailed
[05:46:42] for us. So it's going to go and reset
[05:46:44] the seconds to 0 0. So we are saying the
[05:46:47] minimum level is the minutes and we are
[05:46:50] not interested anything like before it
[05:46:52] the seconds let's say now we say you
[05:46:54] know what the minutes is very detailed I
[05:46:56] would like to be at the hours level so
[05:46:58] we specify for the date rank hour so
[05:47:01] here things changed we're going to keep
[05:47:03] the informations now between the year
[05:47:05] and the hours and anything after that
[05:47:07] going to be reseted so now minutes and
[05:47:10] seconds going to be in the range of the
[05:47:12] resets and SQL going to go and reset the
[05:47:15] 55 to 0 0 so now the level of details is
[05:47:19] little bit lower now we know only the
[05:47:21] informations until the hours and we are
[05:47:24] not interested about the minutes and the
[05:47:25] seconds and I think you already get it
[05:47:27] if you say date trunk day what's going
[05:47:30] to happen it's going to keep everything
[05:47:32] between year and day and the whole time
[05:47:35] going to be resets so the hours and
[05:47:38] seconds all those information is going
[05:47:40] to reset to 0 0 so now by looking to
[05:47:43] this we don't know anything about the
[05:47:45] time we know only informations about the
[05:47:47] dates and now we can go one more step
[05:47:49] and we say you know what I'm not
[05:47:50] interested about the days I'm doing
[05:47:52] analyszis on the month level so what is
[05:47:54] here kept is only two informations year
[05:47:57] and month and everything below that the
[05:48:00] day and the time going to be reseted but
[05:48:03] this time SQL will not reset the date to
[05:48:05] 0 0 because there is no date called 0 0
[05:48:08] it start always with the first date so
[05:48:10] it's going to reset to 01 so the dates
[05:48:13] parts and the dates going to reset to 01
[05:48:15] one and the dates parts in the time
[05:48:17] going to reset to 0 0. So now we are at
[05:48:20] the level of the month. Now you can go
[05:48:22] to the last step and you say you know
[05:48:24] what I'm interested only on the years
[05:48:26] and I'm doing only analyzes at this
[05:48:28] level at the highest level. So you can
[05:48:30] go and say date trunk year and now
[05:48:32] what's going to happen going to keep
[05:48:34] only the year and everything below that
[05:48:37] going to be reseted. So between month
[05:48:39] and the seconds everything going to
[05:48:41] resets. So here is scale going to reset
[05:48:42] as well the August 2011. So the only
[05:48:46] value that is kept is the year and
[05:48:48] everything else is reseted. So this is
[05:48:50] the 1st of January and the time is
[05:48:52] completely reseted. So now we are at the
[05:48:54] lowest level of details. We know only
[05:48:56] information about the year and we don't
[05:48:58] care about any other parts. So as you
[05:49:00] can see the date trunk here is not
[05:49:01] really extracting a part here. Date
[05:49:04] trunk is like resetting stuff. So we are
[05:49:07] navigating through the hierarchy of the
[05:49:09] date and time and we are controlling at
[05:49:11] which level we are doing the analyszis.
[05:49:13] So as you can see at the end it's not
[05:49:15] very complicated once you understand how
[05:49:16] it works and it is very useful in
[05:49:18] analyzis. So this is how the date trunk
[05:49:21] works in SQL. Okay, let's have a few
[05:49:23] examples about the date rank together
[05:49:24] with the creation time. So as you can
[05:49:26] see the creation time the level of it is
[05:49:28] the seconds. So we have seconds
[05:49:30] information with the creation time. Now
[05:49:32] I would like to move it to the minutes.
[05:49:35] So let's go and do this date trunk and
[05:49:38] we're going to say let's tr it at the
[05:49:41] minutes level for the creation time. So
[05:49:45] let's call it minute date trunk. So
[05:49:50] let's go and execute it. Now if you go
[05:49:52] and check the output over here and
[05:49:53] compare it to the creation time, you can
[05:49:55] see here we have zeros at the seconds.
[05:49:58] So as you can see we have the seconds
[05:50:00] completely resetted compared to the
[05:50:02] creation time. Now let's say that I'm
[05:50:04] not interested in the time information
[05:50:06] inside the creation time. I would like
[05:50:08] only to get the date. So in order to do
[05:50:10] that, we can use the date trunk where we
[05:50:12] reset to the level of the day. So let's
[05:50:15] go and duplicate it. I'm going to put it
[05:50:18] over here and instead of minutes, let's
[05:50:20] say we have a day and let's go and check
[05:50:23] the output. Now if you go and check the
[05:50:25] result over here you can see all the
[05:50:27] time informations are reseted to zeros
[05:50:30] and we have here only information about
[05:50:32] the date. So we have year month and day
[05:50:35] and everything else is reset it to zero.
[05:50:37] Now of course we can go to the maximum
[05:50:39] where we say I just need the year. So I
[05:50:42] don't need anything else. So let's try
[05:50:44] that out. We're going to take date trunk
[05:50:46] and say year and let's call it year. So
[05:50:51] let's go and execute it. Now if you
[05:50:53] check the output over here you can see
[05:50:54] that everything is reseted beside the
[05:50:57] year. So we have only the year
[05:50:58] information but everything else is
[05:51:00] reseted to the first of January and the
[05:51:03] time is as well is reseted. So as you
[05:51:05] can see the output of the date trunk is
[05:51:08] always as a date time and it help us as
[05:51:11] well to navigate through the hierarchy
[05:51:13] of the day time and we can truncate at
[05:51:15] the level that we want. All right. So
[05:51:17] now we're going to check why data trunk
[05:51:18] is amazing function for data analyszis.
[05:51:21] So let's have this example. We are
[05:51:23] saying select
[05:51:25] creation time and we want to count the
[05:51:29] number of orders based on the creation
[05:51:31] time from our table sales orders and
[05:51:34] we're going to use the group by in order
[05:51:37] to group the data by the creation time.
[05:51:39] So let's go and execute it. Now as you
[05:51:41] can see we're going to get one
[05:51:42] everywhere because the level of details
[05:51:45] the granularity or the creation time is
[05:51:47] very high and that's because here we
[05:51:49] have the seconds and since our data is
[05:51:51] small we will not get like two orders at
[05:51:53] the same seconds. Now in data analytics
[05:51:55] you would like quickly to aggregate the
[05:51:57] data at different granularity like for
[05:51:59] example at the month level. So you can
[05:52:01] do that very quickly using the date
[05:52:04] trunk and you say you know what let's
[05:52:06] say at the month and let's call it
[05:52:10] creation and we're going to have the
[05:52:12] same thing for the group pie. So let's
[05:52:14] go and execute it. So now as you can see
[05:52:16] at the output we have only three rows we
[05:52:18] don't have like 10 rows and that's
[05:52:20] because we have three months. So that
[05:52:22] means we just rolled up to the month
[05:52:24] level instead of the seconds. And we can
[05:52:26] see now in the month of January we have
[05:52:27] four orders, February as well four and
[05:52:30] March we have only two. So now we are
[05:52:32] talking about different level of details
[05:52:33] in the output and granularity. And now
[05:52:36] you might say let's go and aggregate the
[05:52:37] data at different level at the year
[05:52:39] level. So you can just change over here
[05:52:41] the year and execute it. And with that
[05:52:44] now we are at the highest level of
[05:52:46] aggregations. We are at the year level
[05:52:48] and since in our data we have only 2025.
[05:52:51] So we will get the total number of
[05:52:52] orders inside the table and that is 10.
[05:52:55] And this is really amazing in data
[05:52:56] analytics. You can go and quickly change
[05:52:58] the granularity and the level of
[05:53:00] aggregation or details by simply
[05:53:02] defining the level inside the dates. So
[05:53:05] this is why the date rank is amazing. It
[05:53:07] allow us to do analyszis and
[05:53:09] aggregations by zooming in and zooming
[05:53:15] out. Okay. So now we're going to talk
[05:53:17] about the last function in the part
[05:53:19] extraction category. We have the end of
[05:53:21] the month. As the name says, it's going
[05:53:23] to go and return the last day of a
[05:53:26] month. So let's see how end of month
[05:53:28] works. This is very simple. So let's
[05:53:29] take our date 20th August 2025. If you
[05:53:32] go now and apply this function to it,
[05:53:35] what's going to happen? It's going to go
[05:53:36] and change only the day information. So
[05:53:39] instead of 20, it's going to go to the
[05:53:41] last day of the month. So it's going to
[05:53:43] go and change the 20 to 31. The last day
[05:53:47] of the month, August in 2025. Let's take
[05:53:50] another example is the 1st of February
[05:53:52] 2025. If you apply the end of the month,
[05:53:55] it's going to go and change the day from
[05:53:58] the 1st to 28. The last day of month
[05:54:01] February. So as you can see, it's very
[05:54:03] simple. Let's take another example where
[05:54:04] it is already the last day of the month.
[05:54:08] So we have 31 of March. If you apply the
[05:54:11] end of the month here, what can happen?
[05:54:13] Nothing going to happen. You're going to
[05:54:14] get in return the same value. So this is
[05:54:17] how it works. And as you can see always
[05:54:18] the output of the end of the month going
[05:54:20] to be as well a date. So this is how end
[05:54:23] of month work. It is very simple. All
[05:54:25] right. Now quickly about the syntax of
[05:54:27] the end of the month. It's going to have
[05:54:28] the exact same syntax like the day,
[05:54:31] month, year. It accepts only one
[05:54:33] parameter. It is the date. So we have to
[05:54:35] pass here a date in order to find out
[05:54:38] the end of the month. So let's go and
[05:54:40] find the end of the month of our
[05:54:41] creation time. So end of the month like
[05:54:45] this. And let's have our creation time.
[05:54:49] So let's see the end of month. Let's go
[05:54:53] and execute it. And now in the output
[05:54:54] you can see we have a new column a date
[05:54:56] column. And inside it we have values
[05:54:58] about the end of the month. So for
[05:55:00] example here we have January, January,
[05:55:01] January and so on. So you will see
[05:55:03] always here the end of January and the
[05:55:05] same thing for February and March. So
[05:55:07] that's it. This is really nice function
[05:55:09] in case you need the end of the month of
[05:55:11] each date. Maybe you're creating a
[05:55:12] report or analyzes where you need this
[05:55:14] information. And now you might ask me
[05:55:16] how about to get the first day of the
[05:55:18] month. Is there like any function for
[05:55:19] it? Well, no. But there is a trick in
[05:55:22] order to get the first day of the month
[05:55:24] using another function that we just
[05:55:26] learned. Think about it. How to get the
[05:55:28] days as one everywhere. So we have to
[05:55:31] get here the 1st of January, the 1st of
[05:55:33] February, and the 1st of March. So how
[05:55:35] we can do that? Well, using the date
[05:55:38] trunk. So let me show you how we're
[05:55:39] going to do this. So date
[05:55:42] trunk and we're going to reset at the
[05:55:44] level of month. So we don't need the
[05:55:47] days it going to reset to the first. So
[05:55:50] our field is creation time and this
[05:55:53] going to be the start of month. So let's
[05:55:57] go and execute it. So now as you can see
[05:55:59] in the output we have the start of month
[05:56:01] and you can see we have everywhere here
[05:56:02] a one since we reset it at the level of
[05:56:05] month and this going to give us the
[05:56:06] first day of the month. And now you
[05:56:08] might say you know what here we have a
[05:56:10] lot of zeros how to get it exactly like
[05:56:11] the end of the month and that's because
[05:56:13] the date rank give us date and time
[05:56:15] always. So that means we have to change
[05:56:17] the data type and that we're going to
[05:56:18] learn later using the cast function but
[05:56:21] we can go and do it right now. So we can
[05:56:23] say cast and we want to change the whole
[05:56:26] thing to date. And now that we change
[05:56:28] the data type from date time to date and
[05:56:31] in the output as you can see we have
[05:56:32] only the date information. So now it's
[05:56:34] really amazing that you got two dates.
[05:56:35] The first one is the start of the month
[05:56:37] and the second is the end of the month.
[05:56:38] And those information might be helpful
[05:56:40] if you are generating reporting and you
[05:56:42] need the start and the end of the
[05:56:43] [Music]
[05:56:46] month. So now we come to the part where
[05:56:49] we ask the question why do we need those
[05:56:51] parts? Why do we need to extract the
[05:56:53] date parts from a date? So let's have
[05:56:56] the following use cases. The first use
[05:56:58] case of extracting the part is doing
[05:57:00] data aggregations and reporting.
[05:57:03] Sometimes we are building like reports
[05:57:04] based on our data and sometimes we have
[05:57:06] to aggregate our data by a specific time
[05:57:09] unit like for example we are building a
[05:57:11] reports in order to show the sales by
[05:57:13] year. So we have different years and we
[05:57:15] are aggregating the data based on the
[05:57:18] year or you want to drill down to more
[05:57:20] details where you want to aggregate the
[05:57:21] data by the quarter. So in this report
[05:57:24] we are showing the sales by quarter Q1 2
[05:57:27] 3 4 or you decide to go in more details
[05:57:30] where you show a report says sales by
[05:57:32] month and then you start aggregating
[05:57:34] your data by the month. So you have
[05:57:36] January, February, March and so on. So
[05:57:38] as you can see we can use those
[05:57:39] different parts in order to aggregate
[05:57:42] the data based on it and these different
[05:57:44] parts can offer us different analyzes
[05:57:46] with different details. So now we have
[05:57:48] the following task and it says how many
[05:57:50] orders were placed each year. So that
[05:57:53] means we have to group up our data by
[05:57:55] the year and we have to count the number
[05:57:56] of orders. Let's go and solve it. So
[05:57:59] let's go with the select. And now what
[05:58:00] do we need? We need the order date. This
[05:58:04] going to indicate when the order is
[05:58:05] placed. So and we have to go and count
[05:58:09] the star. So this going to be number of
[05:58:11] orders. and from our table sales orders
[05:58:16] and we have to group up by the order
[05:58:19] dates. So that's it. Let's go a and
[05:58:21] execute it. So now in the output we are
[05:58:23] getting the number of orders but by the
[05:58:26] order date. So we are still not there.
[05:58:28] We have to have it as a year. So we
[05:58:31] don't need the whole date information.
[05:58:33] We need only the year information. So
[05:58:35] that means we have to go and extract the
[05:58:37] part year. In order to do that we can do
[05:58:40] it like this. So we can go with the year
[05:58:43] and we have it as well in the group I.
[05:58:46] So that's it. Let's go and execute it.
[05:58:48] And with that as you can see we got the
[05:58:49] number of orders for each year. And
[05:58:51] since in our data we have only 2025 we
[05:58:53] will get only one row. So with that the
[05:58:55] task is solved. We are now aggregating
[05:58:57] the data on the level of the year. Now
[05:58:59] let's have another task which is the
[05:59:01] same but only different parts. How many
[05:59:03] orders were placed each month. So we
[05:59:06] have to go and change it to a month.
[05:59:08] It's very simple. We're going to use the
[05:59:10] function month and as well in the group
[05:59:13] by. So let's go and execute it. And now
[05:59:15] as you can see in the output we don't
[05:59:17] have one row. Now we have three rows.
[05:59:18] And that's because we have three months
[05:59:20] inside our data. And for each month we
[05:59:22] will get the total number of orders. So
[05:59:25] for the January we have four, February
[05:59:27] we have four and March we have two
[05:59:29] orders. Now you might say you know what
[05:59:31] I don't want the months as a numbers. I
[05:59:33] would like to have the full name of the
[05:59:34] month. So in order to do that we're
[05:59:36] going to go and use the function date
[05:59:38] name. So let's go and use date name and
[05:59:41] then we have to specify the date part.
[05:59:43] It's going to be the month and the value
[05:59:45] going to be the order date and we have
[05:59:47] to have the same thing as well in the
[05:59:49] group I. So let's go and execute it. Now
[05:59:52] you can see in the output we are getting
[05:59:53] the full name of the month which is
[05:59:55] easier to read. So this is one of the
[05:59:57] use cases why we need to extract parts
[05:59:59] from a date in order to aggregate the
[06:00:02] data on a specific level.
[06:00:07] So now let's have the following task and
[06:00:09] it says show all orders that were placed
[06:00:12] during the month of February. So that
[06:00:14] means we don't need all the orders. We
[06:00:16] need only a subset of the orders based
[06:00:18] on the order dates. Now let's go and
[06:00:20] check the data. So select star first
[06:00:23] from sales orders and let's go and
[06:00:27] execute it. So now with that we have our
[06:00:28] 10 orders. Now if you check the order
[06:00:30] date over here you can see that we have
[06:00:32] orders in January, February and March.
[06:00:35] Now we are interested only on the orders
[06:00:37] that were placed in February. So only
[06:00:39] these subsets. So that means we have now
[06:00:41] to filter the data based on the month
[06:00:43] information. So what we're going to do,
[06:00:45] we're going to have a wear clause. And
[06:00:47] now we don't need the whole order date.
[06:00:49] We need only the part month. So we're
[06:00:51] going to go with the month and order
[06:00:54] date and this going to be equal to two.
[06:00:58] Since the output going to be in number.
[06:01:00] So let's go and execute it. Now as you
[06:01:02] can see SQL did filter the data and in
[06:01:04] the output we have only the orders were
[06:01:07] placed in the month of February. So this
[06:01:09] is as well very common use case. Why do
[06:01:12] we need the parts? We use it in order to
[06:01:14] filter the data based on specific part
[06:01:17] of the dates. So as you can see it's
[06:01:19] very quick and easy. And here my
[06:01:21] recommendation is that if you are
[06:01:22] filtering the data always use the
[06:01:24] numbers. So always use a date function
[06:01:26] that gives you a number because it's
[06:01:28] always faster to search for integers
[06:01:31] instead of searching for a character or
[06:01:34] for string. So don't use the date name
[06:01:36] function in order to search or filter
[06:01:38] for the data. It's better to use the
[06:01:40] date part or month, year and day. Since
[06:01:42] you can work with numbers and numbers
[06:01:44] are always faster to retrieve data and
[06:01:46] to filter your
[06:01:50] informations. Okay. So now we have a lot
[06:01:52] of functions and I would like now to do
[06:01:54] a quick recap about the data type of
[06:01:56] their results. So as we learned we have
[06:01:58] functions like day, month, year, date
[06:02:01] bar and the output of all those
[06:02:04] functions going to be integer. It's
[06:02:06] going to be a number. Now we have
[06:02:07] another function the date time. If you
[06:02:09] use it the output of this function going
[06:02:11] to be a string because here we are
[06:02:13] extracting the name of the date part.
[06:02:15] And if you go and use the date trunk you
[06:02:18] will get in the output always date time
[06:02:20] two. So you are getting both the date
[06:02:22] and time. And the last function that we
[06:02:24] learned end of month if you use it in
[06:02:26] the results you will get the data type
[06:02:28] date. So this is really important to
[06:02:30] understand the data type of the output
[06:02:32] so that you don't get any unexpected
[06:02:34] results. All right. So now you might say
[06:02:36] you know what those are a lot of
[06:02:37] functions and like I'm saying they are
[06:02:39] doing the same stuff. We are extracting
[06:02:41] the parts of the dates. So now you might
[06:02:43] ask me how do you decide on when to use
[06:02:46] which function? This is how I usually do
[06:02:47] it. First I ask myself which part I want
[06:02:50] to extract. If I want to extract a date
[06:02:52] or a month then I ask the question do I
[06:02:55] need it as an integer as a number? If
[06:02:58] it's yes then I go and use the day
[06:03:00] function or the month function because
[06:03:02] they are quick and I will get exactly
[06:03:04] what I need. But now if I need the full
[06:03:06] name of the month or the day then I go
[06:03:09] with the function date name. Now moving
[06:03:11] back if I'm interested on the part year.
[06:03:14] So here we don't have a year name or
[06:03:15] something. I'm going to go immediately
[06:03:17] with the function year. But now let's
[06:03:19] say that I don't need the day, month or
[06:03:21] year. I'm interested in other parts like
[06:03:24] the week, the quarter and so on. Only
[06:03:27] for this scenario, I go with the
[06:03:28] function date part. So this is my
[06:03:31] decision process. This is how I decide
[06:03:33] when to use which SQL function in order
[06:03:36] to extract the parts of the
[06:03:41] dates. All right. All right. So now I
[06:03:43] have prepared for you here a list of all
[06:03:45] parts that we can use inside those three
[06:03:47] functions date part date name and date
[06:03:49] trunk. And you can see in this table the
[06:03:51] different outputs using those different
[06:03:53] three functions. So for example if you
[06:03:55] go and use the month with the date part
[06:03:57] you will get eight but for the date name
[06:03:59] you will get August and for the date
[06:04:01] trunk you will get truncated date time
[06:04:04] at the level of the month where you
[06:04:06] reset the days and times. So this is a
[06:04:08] full list of all examples you can go and
[06:04:10] check it. And one more thing that I have
[06:04:11] prepared for you in order to practice
[06:04:13] with all those different parts. I have
[06:04:15] made one big query with all different
[06:04:17] parts. So if you go and download the
[06:04:19] queries of this chapter, you will find
[06:04:21] the following files and let's go now and
[06:04:23] open all date parts. So we're going to
[06:04:26] go inside it and here we have a long
[06:04:29] query. So what we're going to do, we're
[06:04:30] going to select everything and copy it
[06:04:32] and let's go back to our scale and paste
[06:04:34] it. So let me just zoom out and then
[06:04:38] let's go and execute the whole thing. So
[06:04:41] now in my code I have just done a union
[06:04:43] for each possible part. For example for
[06:04:46] the year we have date part date name and
[06:04:48] date trunk and I'm using currently the
[06:04:50] get date. So we are manipulating this
[06:04:52] one and then the output can be presented
[06:04:54] over here. So you can see it like this.
[06:04:56] So if you use the part here for the date
[06:04:58] name you will get 2024. The same thing
[06:05:01] for the date name and this is for the
[06:05:02] date rank. And with that you have all
[06:05:05] possible parts that you can use in SQL
[06:05:08] in one query. So with that you can learn
[06:05:10] what are the outputs for different
[06:05:12] parts. All right. So with that we have
[06:05:14] learned all those functions on how to
[06:05:16] extract the parts of dates. All right.
[06:05:18] Moving to the second category. We're
[06:05:20] going to learn how to do formatting and
[06:05:22] casting for the date informations in SQL
[06:05:24] using three functions.
[06:05:30] So now before we deep dive to the
[06:05:31] formatting and casting I would like you
[06:05:33] to understand what is date format. So
[06:05:35] back to our example we have here the
[06:05:37] date and time informations and we
[06:05:39] understood there is components year
[06:05:41] month day and so on. Now if you check
[06:05:43] the date time there is combination of
[06:05:45] numbers and characters. For example the
[06:05:47] 2025 is a number but between the month
[06:05:50] and the year there is like a minus
[06:05:52] between them and this is a character. So
[06:05:54] now this is a very specific format and
[06:05:57] in SQL we can have a code for this
[06:06:00] format. So for example let's start with
[06:06:01] the year we have here four digits and we
[06:06:04] can represent it with 4 Y. So Y Y and we
[06:06:08] call those characters as format
[06:06:11] specifiers. So this is how we represent
[06:06:14] the year. Then between the year and the
[06:06:16] month there is like this small minus and
[06:06:18] then the month is two digits and we're
[06:06:20] going to represent it with two big M. So
[06:06:23] m M then between the month and the day
[06:06:25] there is a minus. So we have as well
[06:06:27] minus and then the day going to
[06:06:28] represented with two digits d and then
[06:06:31] we have like a space between the date
[06:06:33] and time and then we start with the
[06:06:35] date. So it start with the hour big h
[06:06:37] and big h because here we have the
[06:06:39] system of 24 and then we have double
[06:06:41] points small m small m. So as you can
[06:06:44] see here the formats are case sensitive.
[06:06:46] So there is a big difference between
[06:06:48] small m and a big m. So a small m
[06:06:50] indicates for a minute and big m
[06:06:53] indicates for a month. So as you can see
[06:06:55] here the case format is case sensitive.
[06:06:58] So two small m means minutes but two
[06:07:01] capital m means month. Then double point
[06:07:04] and small 2s. So now the whole code is
[06:07:07] called the date format. So this is the
[06:07:10] date format representation of this
[06:07:12] value. Now in the world there are
[06:07:14] different representations on how to
[06:07:16] represent a date. So for example in SQL
[06:07:18] we have the international standard
[06:07:21] ISO6801 and the date format is like we
[06:07:23] have learned first it start with the
[06:07:25] year. So four digit for the years minus
[06:07:28] two digit for the month minus two digit
[06:07:31] for the day. So year month day but in
[06:07:34] the USA we have different standards. So
[06:07:36] first it start with the month. So we
[06:07:38] have mm and then after that it is
[06:07:41] followed with the day. So we have then
[06:07:43] the day and after that at the end we
[06:07:45] have the year. So this is the sentence
[06:07:47] format that is used in USA and in Europe
[06:07:49] we have different representations of the
[06:07:51] day. So it start first with the small.
[06:07:53] So it starts with the day then the month
[06:07:55] and then the year. So this is exactly
[06:07:58] the opposite of the international
[06:07:59] standards. So as you can see we don't
[06:08:01] have one standard. We have different
[06:08:02] ways on how we represent dates. But in
[06:08:05] SQL the SQL server is following the
[06:08:07] format of the international standards.
[06:08:10] So SQL server start always with the year
[06:08:12] then month then day. So all dates that
[06:08:15] are used in our SQL database can be
[06:08:17] following this
[06:08:21] format. Okay. So after we understood
[06:08:24] what is date format, now let's talk
[06:08:25] about formatting and casting. So what is
[06:08:28] formatting? Is changing the format of
[06:08:30] value from one to another. So we are
[06:08:32] changing how the data looks like. So for
[06:08:34] example, we have our date. So it's
[06:08:36] following the international standards
[06:08:37] start with year, month, then day. Now we
[06:08:40] can go and change the format using the
[06:08:42] function format where we can go and
[06:08:44] define a different date format like it
[06:08:46] start with the month and then we have
[06:08:48] like slash instead of minus and then the
[06:08:51] day/ year. So in the outer we're going
[06:08:53] to get it like this and even the years
[06:08:55] is only two digits not four. So here we
[06:08:57] are providing for SQL the format that we
[06:08:59] would like to see the data with or you
[06:09:01] can go with other format where you have
[06:09:03] three big M and then four digits for the
[06:09:05] year and between them is just a space.
[06:09:07] So in the output you will get
[06:09:09] abbreviation of the month name and then
[06:09:11] space and the year. So this is one way
[06:09:13] on how to format data. But in the scale
[06:09:15] there is another function that help us
[06:09:17] to format data and that is convert. So
[06:09:19] here we provide not the format itself we
[06:09:22] provide style number. So for example the
[06:09:24] style number six. So it can show it like
[06:09:26] this day space and after that we have
[06:09:28] the abbreviation name of the month and
[06:09:31] then two digits of the year. Or if you
[06:09:33] use another style the 112 then you will
[06:09:36] get the year, month, day without any
[06:09:38] separation between them. And of course
[06:09:40] not only the date and time we can style
[06:09:42] we can style as well numbers and here we
[06:09:44] can use the function format in order to
[06:09:45] change the format of the number. So here
[06:09:47] if you're using the format of numeric
[06:09:49] values then the values will be separated
[06:09:51] with comma or if you use c for the
[06:09:54] currency then you will get the dollar
[06:09:55] sign or if you go and use p then you
[06:09:58] will get the percentage and at the end
[06:09:59] you have the percentage character. So as
[06:10:01] you can see we can as well change the
[06:10:03] format of the numbers but only the
[06:10:05] dates. So this is what we mean by
[06:10:06] formatting we are just changing how the
[06:10:09] value looks like. Now in the other hand
[06:10:11] the casting the casting can go and
[06:10:13] change the data type from one to
[06:10:16] another. So for example if we have the
[06:10:18] value 1 2 3 as a string we can go and
[06:10:21] convert it from the data type string to
[06:10:23] an integer. So in the output we will get
[06:10:25] as well 1 2 3 but as a number or we can
[06:10:28] go and change the data type from dates
[06:10:30] to a string. So in the output it is not
[06:10:33] anymore dates it is a string value or
[06:10:36] the way around we can change the data
[06:10:38] type from a string to a date. So as you
[06:10:41] can see we can change the data type from
[06:10:43] one to another and we can use that using
[06:10:45] two functions. The first one is and the
[06:10:48] most famous one is cast function or in
[06:10:50] SQL server we can use as well the
[06:10:52] convert function in order to change the
[06:10:55] data type. So this is what we mean with
[06:10:57] casting changing the data type from one
[06:10:59] to
[06:11:03] another. All right. So let's start with
[06:11:05] the first function the format. So what
[06:11:07] is format? As the name suggest it
[06:11:09] formats a date or time value. So it's
[06:11:12] like we are changing how the date and
[06:11:14] time looks. Okay. So let's check the
[06:11:16] syntax of the format and here it accepts
[06:11:19] two parameters and the third one is
[06:11:21] optional. So the first one we have to
[06:11:23] provide a value. It could be a date or a
[06:11:25] number. And the second one we have to
[06:11:27] provide the format. So here we are
[06:11:28] specifying the new look the new format
[06:11:30] for this value. Now the third one it is
[06:11:33] optional one. It is the culture. Culture
[06:11:35] means show me the value whether it's
[06:11:37] date, time or number. Show me this value
[06:11:40] in the style of a specific country or
[06:11:43] region. So each country each region has
[06:11:45] different format. So here we can go and
[06:11:47] change it to specific region format. But
[06:11:50] as I said it is optional. Let's have an
[06:11:52] example. So here we are saying go and
[06:11:54] format the order dates using the
[06:11:56] following format. So dd day then slash
[06:12:00] then we have the month then slash then
[06:12:02] the year. So going to go and format this
[06:12:04] with this new format. And as you can see
[06:12:06] here we didn't specify any culture since
[06:12:07] it's optional. Let's see another option
[06:12:09] where we can say you know what I would
[06:12:11] like to have the order date formatted
[06:12:13] with this format but we would like to go
[06:12:15] and add the style of Japan. So we are
[06:12:17] specifying here the code or the style of
[06:12:20] Japan. And of course we can go and use
[06:12:22] the format not only for the date but as
[06:12:24] well for formatting the numbers. So here
[06:12:26] we are specifying the value. The format
[06:12:29] is D. And as well we have activated the
[06:12:31] culture option. We are using the style
[06:12:33] of France. So this is the syntax of the
[06:12:35] format. Using this option is not really
[06:12:37] common. So I rarely see this format or
[06:12:40] someone using it. So the first example
[06:12:42] is the most used one in the projects
[06:12:45] where we have the culture as default or
[06:12:47] we are not using the culture at all. And
[06:12:49] of course if you don't specify anything
[06:12:51] is going to go and use the default
[06:12:54] culture which is enus. So this is all
[06:12:57] about the syntax of the format. All
[06:12:58] right. So now let's have a few examples
[06:13:00] using the format. So we're going to go
[06:13:02] and format the creation time. So we're
[06:13:04] going to do it like this. Format. And
[06:13:06] what we are formatting? We are
[06:13:07] formatting the creation time and now you
[06:13:10] can go and define any specifier you
[06:13:12] want. For example, let's say DD like
[06:13:15] this. So let's go and check the outputs.
[06:13:18] So execute it. Now if you are using DD,
[06:13:20] you will get the day information. So we
[06:13:24] can see if you're using this specifier,
[06:13:26] we are getting two digits about the day.
[06:13:28] So and as well we are getting the
[06:13:29] leading zero. So we are getting the 01
[06:13:31] 05 and all those informations are the
[06:13:34] day information. Now let's go and try
[06:13:35] something else. adding one more D. So
[06:13:39] let's have it 3D and here as well. So
[06:13:42] let's go execute it. So now if you check
[06:13:44] the output, we are getting now the name
[06:13:45] of the day. It is not full. So we are
[06:13:48] getting like a short name of the day or
[06:13:50] abbreviated one. So this is sometime
[06:13:51] nice if you are creating like a calendar
[06:13:53] or something. Let's go and add one more
[06:13:56] D. So we're going to have 4 D. And let's
[06:14:00] go and check the result for this one.
[06:14:02] Now in the output we are getting the
[06:14:04] full name of the day. So it's really
[06:14:06] nice. Now we are getting full
[06:14:08] flexibility on how to format our day.
[06:14:10] Okay. So now let's keep playing. Let's
[06:14:11] get something else. I'm just going to go
[06:14:13] and duplicate everything and I will go
[06:14:16] with the month now. So this is 2 M, 3 M
[06:14:21] and 4 M. Let me do it like this. So
[06:14:26] let's go and execute it. Now as you can
[06:14:28] see we are getting the same stuff but
[06:14:30] for the month. So mm we will get the two
[06:14:32] digits and 3m we will get the
[06:14:34] abbreviated name of the month and for m
[06:14:37] we will get the full name of the month.
[06:14:39] So it's like we are extracting the date
[06:14:41] part from the format but of course we
[06:14:43] don't use it like this. We will go and
[06:14:45] write the whole format that we need for
[06:14:48] a date. So for example let's go and
[06:14:50] change this format to the USA format. So
[06:14:53] in order to do it so we're going to go
[06:14:55] over here. So let's say format again the
[06:14:58] creation time. And now we're going to
[06:15:00] write the format of USA. So it's going
[06:15:02] to be mm. Then after that then after the
[06:15:05] month we're going to have like minus
[06:15:07] then day and then after that we're going
[06:15:09] to get the year. So for time year and
[06:15:12] that's it. Let's call it USA format. So
[06:15:15] let's go and excuse it. And now you can
[06:15:18] see in the outut we got a new column
[06:15:19] where we see now the date information
[06:15:21] but as a USA standards. So it start with
[06:15:24] the month then the day and then
[06:15:26] afterward we got the year. And of course
[06:15:28] we can do the same thing in order to
[06:15:29] generate the standard format of Europe.
[06:15:32] So what we're going to do I'll just
[06:15:34] duplicate it. And now the format of that
[06:15:37] going to start with the day then the
[06:15:39] month and then the year. So now if you
[06:15:42] check the output you can see it start
[06:15:43] with day minus then we have the month
[06:15:46] then minus the year. So as you can see
[06:15:48] we are changing the format of the date
[06:15:50] from creation time to something new. All
[06:15:52] right. So now we have the following task
[06:15:54] and it says show creation time using the
[06:15:57] following format. Now we have a very
[06:15:59] weird format. So it start with the word
[06:16:01] day. Then after that we have the
[06:16:03] abbreviation of the day and then
[06:16:05] abbreviation of the month. This is the
[06:16:07] quarter informations. Then the year and
[06:16:10] after that we have the time and we're
[06:16:12] going to say whether it's PM or A.M. So
[06:16:14] it's little bit weird format that you
[06:16:16] don't see it everywhere but still we
[06:16:18] want to practice on how to construct
[06:16:20] such custom format. So let's do it step
[06:16:22] by step. I'm going to go over here and a
[06:16:24] new line. So the first one is like day.
[06:16:27] So we don't have any format for that.
[06:16:29] It's just like characters. So this one
[06:16:32] going to be static for all the format.
[06:16:34] So what we going to do? We're going to
[06:16:35] say with a string this is the day. So
[06:16:39] let's go and execute it. So with that we
[06:16:41] got a static value. Everywhere we have
[06:16:43] the word day. So that's it. And after
[06:16:46] that we have a space. So I'm going to go
[06:16:47] and include it after the day in the
[06:16:49] string. So we have a day then space and
[06:16:52] after that we need the abbreviation of
[06:16:54] the day name. So what we're going to do
[06:16:56] we're going to go first with the plus
[06:16:57] operator in order to concatenate the
[06:17:00] strings. So we need the format function
[06:17:03] for the creation time. And what do we
[06:17:06] need? We need the short name. So it's
[06:17:09] going to be three times the d. Let's go
[06:17:11] and execute it. Let me just say here
[06:17:13] custom formats. So now as you can see in
[06:17:16] the output we have here the day. Then
[06:17:17] afterward we have space and then the
[06:17:20] abbreviation of the name of the day. So
[06:17:23] it looks so far good. Now after that
[06:17:25] what do we need? We need space and then
[06:17:27] the abbreviation of the month. So we can
[06:17:29] go and add all those stuff together with
[06:17:31] the format here. So we don't have to
[06:17:33] create two formats. So space and the
[06:17:36] abbreviation of the month is 3 M. So
[06:17:39] let's go and test it. Great. So now as
[06:17:41] you can see we got the abbreviation of
[06:17:43] the month as well side by side. So we so
[06:17:46] far we have covered this part. Now we
[06:17:48] have to move to the second part. So we
[06:17:50] still need a space and then Q1. Well the
[06:17:53] Q going to be static. So we cannot go
[06:17:55] and extend this format. We have to start
[06:17:57] a new one. So what I'm going to do I'm
[06:17:59] just going to add a plus here and a new
[06:18:02] line. So what do we need? We need first
[06:18:04] a space between the month and the
[06:18:06] quarter. So let's go and add space and
[06:18:09] we need the Q as a static value like
[06:18:12] this. Let me just move it like this. And
[06:18:14] now after that we need this one like
[06:18:16] this right so now we need the quarter
[06:18:18] informations and we don't have format
[06:18:20] for that that's why we have to go and
[06:18:22] use the part extraction functions and
[06:18:24] the one that we're going to use since we
[06:18:26] are using string I will go with the date
[06:18:28] name so quarter and we are extracting
[06:18:32] from the creation time so let's go and
[06:18:35] test it so now in the output you can see
[06:18:37] we have everywhere a Q1 and that's
[06:18:39] because all of those dates are in Q1 all
[06:18:41] right so now we are so far halfway in
[06:18:43] our format Not. So now next what do we
[06:18:46] need? We need like a space and then the
[06:18:48] year information and then the time
[06:18:50] information. So now in order to go and
[06:18:52] get space we're going to do it very
[06:18:53] simply concatenate and we're going to
[06:18:56] have space. Now let's go to a new line
[06:18:59] and in order to get the year I will go
[06:19:01] with the format as well. So format and
[06:19:03] what do we have? We're going to have the
[06:19:05] creation time
[06:19:07] again. So how we going to format it now?
[06:19:10] What do we need? We need the year. So
[06:19:11] it's going to be four times the y and
[06:19:13] after that we have like space and then
[06:19:15] the time information. We still can't do
[06:19:17] that inside the format, right? So we're
[06:19:19] going to have space here. And then next
[06:19:21] what do we have? We have the hours. So
[06:19:22] it's going to be h the small h because
[06:19:24] here we are talking about the pm and am.
[06:19:27] It's not the 24hour system. And then
[06:19:29] after that what do we have? The points
[06:19:31] double points. Then the minutes going to
[06:19:33] be small 2 m. And then after that the
[06:19:36] seconds. So far this is exactly this
[06:19:38] part over here. And now what is missing
[06:19:40] a space and the PM the designator. So in
[06:19:43] order to do that we're going to have a
[06:19:45] space as well and then small 2 * tt. All
[06:19:48] right. So we are almost there. Let's go
[06:19:51] and execute it. Now you can see it is
[06:19:53] working. So we have the year then space
[06:19:56] the hours minutes and space and then we
[06:20:00] have the designator. So this is PM and
[06:20:03] this is A.M. which is correct. So that's
[06:20:06] it. We are done. This is how you can
[06:20:07] create those crazy formats in SQL using
[06:20:11] the help of format or maybe date name or
[06:20:13] maybe some static values like we just
[06:20:15] added here. So I think it's really fun
[06:20:17] formatting the dates in
[06:20:21] SQL. Now one use case for the format
[06:20:24] that I frequently use in my project is
[06:20:26] using it to format the date before doing
[06:20:29] aggregations. So it's like part
[06:20:31] extraction but here we have more
[06:20:33] customizations on how we represent the
[06:20:35] date at the reports. So we can show a
[06:20:38] report like sales by month where we
[06:20:40] display for example the date as
[06:20:41] abbreviation name of the month Jan and
[06:20:44] as well two digits for the year 25. So
[06:20:46] once we change the format like this and
[06:20:48] then do data aggregations we will have a
[06:20:51] nice report about the sales by month. So
[06:20:53] let's have a quick aggregations using
[06:20:54] the format. So, we're going to go and
[06:20:56] say select and now the order date and
[06:20:59] count the number of
[06:21:01] orders from our table sales orders and
[06:21:06] then group by. But now before we start
[06:21:09] using the order date, we have to go and
[06:21:11] format it. And then if you take the
[06:21:12] order date, let's go and execute it. So
[06:21:15] as you can see the level of details is
[06:21:17] very high and we have here 10 rows and
[06:21:19] for each day we have like one order. Now
[06:21:21] we learned we can go and use the date
[06:21:23] part in order to extract one part and
[06:21:25] then aggregate on it. So now instead of
[06:21:27] that we're going to go and use the
[06:21:28] format function. So let's go and change
[06:21:30] the format and it is the order dates.
[06:21:33] And our format going to be like this. So
[06:21:35] three big M and then two digits for the
[06:21:38] year. That's it. And let's call it order
[06:21:41] dates. And we need this as well for the
[06:21:44] order date over here for the group I and
[06:21:46] here a comma. So that's it. Let's go and
[06:21:48] execute it. So in the output as you can
[06:21:50] see over here we have three months and
[06:21:52] here we having the aggregation the
[06:21:53] number of orders for each month. So now
[06:21:55] it's like the date part but now we are
[06:21:57] customizing the format as we want. So we
[06:22:00] can use the format in order to change
[06:22:02] the granularity of the date in order to
[06:22:04] do that
[06:22:08] aggregations. Now I'm going to show you
[06:22:10] a real use case for the formatting in
[06:22:12] real projects. Now our data could be
[06:22:14] stored in different technologies like
[06:22:16] the data could be stored in CSV file or
[06:22:18] we can get our data using an API call or
[06:22:21] in very common scenario our data could
[06:22:23] be stored in database. So now what we
[06:22:25] usually do we go and extract the data
[06:22:27] from these different sources into one
[06:22:29] central storage. It could happen that
[06:22:31] you are getting different formats for
[06:22:33] the dates and of course this is a
[06:22:34] problem for analytics. You cannot
[06:22:36] present different formats for the dates.
[06:22:37] What we're going to do we're going to go
[06:22:38] and clean up the formats into one
[06:22:41] standard format. So that means we have
[06:22:43] to format the incoming data to new
[06:22:45] formats and once we have one standard
[06:22:47] format we can use it in analytics and
[06:22:50] reports. So this is very common use case
[06:22:52] in data preparation and in data cleanup
[06:22:55] by formatting different formats into one
[06:22:57] standard
[06:23:01] format. Now in SQL we have many
[06:23:04] different date and time specifiers and I
[06:23:07] said they are case sensitive and each
[06:23:09] one of them has a different meaning. So
[06:23:11] I prepared for you as well all possible
[06:23:13] specifiers that we can use with the
[06:23:15] formats. Not only that, if you go back
[06:23:16] to the queries that you can find in this
[06:23:18] chapter, you can find here date format.
[06:23:21] So all date formats. If you go inside
[06:23:23] it, you can go and copy the whole query
[06:23:26] and then go back to SQL then execute it.
[06:23:29] You can find here a live example because
[06:23:31] I'm manipulating now the get date. So
[06:23:34] you can find here a list of all possible
[06:23:37] date specifiers that you can use with
[06:23:38] the formats. So I would say go and
[06:23:40] practice with those different date
[06:23:41] formats in order to understand what is
[06:23:44] possible in SQL. So as we learned not
[06:23:45] only we can change the format of the
[06:23:47] date, we can change as well the format
[06:23:49] of the number using the function formats
[06:23:51] and those are the different possibility
[06:23:53] that you can use as a specifier for this
[06:23:55] format in order to change the format of
[06:23:58] the numbers and as well I have prepared
[06:23:59] all those different specifiers in one
[06:24:02] big query. So if you go inside it and
[06:24:04] copy it and then put it in SQL and
[06:24:06] execute it, you will find here all
[06:24:08] different possibilities that we have as
[06:24:11] a specifier to change the format of the
[06:24:17] numbers. All right. So what is convert?
[06:24:19] It's very simple. It's going to go and
[06:24:21] change the value to a different type and
[06:24:23] as well at the same time it helps
[06:24:25] formatting the value. Okay. So let's
[06:24:27] check the syntax of the convert and it
[06:24:29] looks like this. It start with the
[06:24:31] function converts and it accept two
[06:24:33] parameters the data type first since we
[06:24:36] can use this function in order to cast
[06:24:38] the data types. So you can use string
[06:24:40] integer dates and so on and then we have
[06:24:43] to specify the value. So which value
[06:24:45] should be casted. And the last parameter
[06:24:47] it is optional one where you define the
[06:24:50] style the format of the value. Let's
[06:24:53] have this very simple example. We are
[06:24:54] saying convert to the data type integer
[06:24:57] int and the value that should be
[06:24:59] converted is 1 2 3 as a string. So it's
[06:25:02] going to convert it to integer. We are
[06:25:04] saying convert to a vchart and the value
[06:25:07] that should be converted is the order
[06:25:08] date. So the order date should be a
[06:25:10] date. So we're going to convert it from
[06:25:12] date to v charts using the format or the
[06:25:15] style of 34. So here we are specifying a
[06:25:19] style a format for this value. And of
[06:25:21] course it is optional and if you are not
[06:25:23] using anything the default value that's
[06:25:26] going to be used is zero. So this is the
[06:25:29] syntax of the convert in SQL. All right.
[06:25:31] So now we're going to have few examples
[06:25:32] on how to work with the convert. So
[06:25:34] let's go and convert for example string
[06:25:36] to integer. So we're going to say for
[06:25:38] example convert. So what is the target
[06:25:41] data type? It's going to be the integer
[06:25:43] and the value. It's going to be like for
[06:25:45] example 1 2 3. So and let's call it like
[06:25:48] this string to integer and the function
[06:25:51] is convert. So now in the column name as
[06:25:54] you can see I'm using here brackets and
[06:25:55] that's because I'm using like empty
[06:25:57] spaces and so on and with that I will
[06:25:59] get more freedom on how to name things.
[06:26:01] So this is just the name. So this is no
[06:26:04] function or something. Let's go and
[06:26:06] excuse it. Now as you can see it's going
[06:26:07] to work. So we are converting from a
[06:26:09] string value to an integer and the
[06:26:12] output this 1 2 3 here is not string.
[06:26:15] This is the data type of integer. All
[06:26:17] right. So now let's have another example
[06:26:18] where we want to convert from string to
[06:26:21] date. So the target going to be the date
[06:26:24] and the value let's have this value as
[06:26:27] usual and we're going to go and call
[06:26:30] it string to date convert. Okay. So
[06:26:35] let's go and execute it. Now in the
[06:26:37] output we will get this information this
[06:26:39] string as a date. And with that we have
[06:26:41] converted the data type from string to
[06:26:43] dates. Now let's have another example
[06:26:44] where we want to convert the date time
[06:26:46] to a date. As you remember the creation
[06:26:48] time is a date time and we would like to
[06:26:51] have it as only date. So let's go and
[06:26:53] convert and we would like it to be as
[06:26:56] well date but this time it's going to be
[06:26:58] a column called creation time and let's
[06:27:01] give it the name. So we are converting
[06:27:05] date time to dates. But of course here
[06:27:08] we have to go and select. So from sales
[06:27:11] orders that's it. Let's go and execute
[06:27:14] it. Now, as you can see in the output,
[06:27:15] we got only date. I'm going to go and
[06:27:17] select the creation time in the query as
[06:27:20] well. So now, as you can see, the
[06:27:22] creation time was before a date time.
[06:27:24] So, we have the time information as
[06:27:25] well. But if you go and cast it using
[06:27:28] the convert and make it only date. So,
[06:27:30] SQL going to go and convert it to date
[06:27:32] and you're going to lose all the
[06:27:33] informations about the time. So, so far
[06:27:35] what we are doing here is just casting.
[06:27:37] So, we are changing the data type from
[06:27:39] one to another. But in the convert, we
[06:27:41] can do both. We can do casting and
[06:27:43] formatting. So let's see how we can do
[06:27:45] that. I will just get rid of those
[06:27:47] information at the start. So creation
[06:27:49] time. And now we're going to go and
[06:27:51] convert the date time of the creation
[06:27:54] time to a varchar to a string. And as
[06:27:56] well to give it the format of the USA
[06:27:58] standard format. So let's see how we can
[06:28:00] do that. We're going to start with
[06:28:01] convert. We are changing now to var. So
[06:28:05] this is the new data type and the value
[06:28:08] is the creation time. And now if I don't
[06:28:10] give it a style, it's going to stay with
[06:28:11] the standard format, but we would like
[06:28:13] to have the USA standards. So in order
[06:28:16] to do that, we're going to go and add
[06:28:17] the style of the format. So it's going
[06:28:19] to be 32. So that's it. Let's have a
[06:28:23] name like this. So
[06:28:26] USA standard and we are using the style
[06:28:30] of 32. Let's go with that. This is just
[06:28:32] a name again. So it's not a function.
[06:28:34] Let's go ahead and execute it. And now
[06:28:36] in the output we got a new field and the
[06:28:38] data type of this field is a varchar. So
[06:28:40] it's not a date or date time. And as you
[06:28:42] can see the date now is formatted using
[06:28:45] this style the 32 the US standard
[06:28:47] format. So it start with a month then a
[06:28:50] day and then a year. So now let's go and
[06:28:52] do the same thing in order to get the
[06:28:54] standard format in Europe. So I will
[06:28:56] just go and copy the whole thing. I will
[06:28:59] just change the style. So instead of 32
[06:29:02] we're going to go with the 34. And I
[06:29:04] will just change the name as well. So,
[06:29:08] so we are just changing the style. Let's
[06:29:10] go ahead and execute it. Now, as you can
[06:29:11] see, we got the same thing. We have as
[06:29:13] well a v jar and the format now is
[06:29:15] different. So, we have here the day,
[06:29:18] then the month, and then the year. So,
[06:29:20] this is how you work with the convert
[06:29:21] function. You can use it in order to do
[06:29:23] only casting or not only that, you can
[06:29:26] do casting and as well formatting. So,
[06:29:28] you have both things in one function.
[06:29:34] And now if you're talking about which
[06:29:35] styles are available, we have many
[06:29:37] styles that you can use inside the
[06:29:39] convert. So I have prepared for you a
[06:29:41] list of all styles that you can use with
[06:29:43] the convert. So we have styles only for
[06:29:45] the dates and another styles only for
[06:29:47] the time and styles for only date time.
[06:29:50] Now in the download folders you can find
[06:29:52] here one file called all culture
[06:29:54] formats. And here you can find one query
[06:29:57] that I have prepared where you can find
[06:29:59] inside it the different cultures and the
[06:30:01] examples. So let's go and copy it and
[06:30:03] let's go back to scale paste it and
[06:30:05] let's see the results. So now if you
[06:30:07] check the output we got the first column
[06:30:09] is the cultures that is used. So we have
[06:30:11] a lot of cultures like around 17s and
[06:30:13] you can see how the numbers are
[06:30:14] formatted or the date is formatted based
[06:30:16] on this culture. So it's really fun. You
[06:30:18] can check here for example how the
[06:30:20] format in Japan or Korea or France and
[06:30:23] the German one. If you scroll down, you
[06:30:25] can find the Arabic, the Russian and so
[06:30:27] on. So you can see the format of each
[06:30:29] dates is changing based on the culture.
[06:30:32] So I would say have fun. Go and try
[06:30:34] those different cultures formats in
[06:30:36] order to format your numbers or
[06:30:40] dates. So what is the cast function? It
[06:30:43] going to go and convert a value to a
[06:30:46] different data type. So it turns one
[06:30:48] data type to another. All right. So now
[06:30:50] let's check the syntax of the cast. I
[06:30:52] really like this one. It is not typical
[06:30:54] like format or syntax in SQL. So it says
[06:30:57] the cast is the function and then inside
[06:31:00] it we need two things but it's not
[06:31:02] separated like with the comma as we
[06:31:03] learned before with all other functions
[06:31:05] but this time is separated with the
[06:31:07] keyword as. So it's like the natural
[06:31:09] English you are saying cast the value as
[06:31:12] a data type. So you are casting the
[06:31:14] value to a new data type. So let's have
[06:31:16] this very simple example we have here
[06:31:18] cast the value 1 2 3 as integer. So
[06:31:22] previously it is string and it going to
[06:31:24] be converted to integer. So as you can
[06:31:26] see it's very simple. Now in this
[06:31:27] example we are saying cast this value
[06:31:30] this string value as a dates. So
[06:31:32] converted from string to dates. So as
[06:31:34] you can see with the cast we don't have
[06:31:35] here any option of formatting or styling
[06:31:38] the values. So it's only dedicated for
[06:31:40] casting the value from one data type to
[06:31:43] another one. So this is the syntax of
[06:31:45] the cast. It is very straightforward and
[06:31:47] really nice function. Okay. So now let's
[06:31:49] have a few examples about the cast. So
[06:31:51] let's go and convert a value from a
[06:31:53] string to integer. So it's very simple.
[06:31:55] We're going to say cast. So now we need
[06:31:57] the value. So let's go with the 1 2 3.
[06:32:00] So we have here a string. And then we're
[06:32:01] going to say as and then we have to
[06:32:03] define the data type. So the data type
[06:32:05] going to be integer. So that's it. So
[06:32:07] let's give it the name like this string
[06:32:10] to integer. Let's go and execute it. Now
[06:32:13] as you can see we got the value but with
[06:32:14] the data type integer. From string to
[06:32:16] integer. Now let's do the way around. We
[06:32:19] cast from integer to string. So we're
[06:32:21] going to say cast 1 2 3 as var jar and
[06:32:26] we're going to give it a
[06:32:27] name int to string. So let's go and
[06:32:31] execute it. Now in the output we have 1
[06:32:33] 2 3 but this time it has the data type
[06:32:36] varchar. Now let's go and work with the
[06:32:38] date. So we're going to go and convert a
[06:32:40] value a string value to a date. So our
[06:32:44] value going to be the usual one and we
[06:32:48] want it from string to date. So we're
[06:32:50] going to have the data type as date. So
[06:32:52] let's give it a name string to date.
[06:32:56] Let's go and execute it. Now we're going
[06:32:58] to have this value with the data type
[06:33:00] date. So that's it. Now let's say that I
[06:33:02] would like to have this value but as
[06:33:04] date time. So I will just copy the whole
[06:33:07] thing and go to a new line and say date
[06:33:10] time two. So the name of this going to
[06:33:13] be string to date time. Let's go and
[06:33:16] execute it. Now in the output as you can
[06:33:17] see we are getting not only the date but
[06:33:20] as well we are getting the time
[06:33:21] information. But now since we didn't
[06:33:23] provide SQL with any time information
[06:33:25] SQL going to go and show it as zeros.
[06:33:27] Now let's do one more casting where we
[06:33:29] change the data type from date time to
[06:33:31] date. So now we need our creation
[06:33:33] time but we have to get it from the
[06:33:36] tables. So from sales orders let's go
[06:33:39] and execute it. So now in the output you
[06:33:41] can see the creation time is a date
[06:33:42] time. We have the time information but
[06:33:44] we are not interested about the time
[06:33:45] information. I would like to have this
[06:33:47] field as a date. So it's very simple
[06:33:49] what we're going to do. We're going to
[06:33:50] say cast. Now the value is creation time
[06:33:55] and then the keyword as and we need it
[06:33:57] as a date. So we're going to give it the
[06:33:59] name date time to date. So let's go and
[06:34:03] execute it. Now as you can see in the
[06:34:04] output we got the creation time but only
[06:34:07] with the date information. We don't have
[06:34:09] anything about the time. So we get it as
[06:34:11] a date instead of date time. So that's
[06:34:14] it. This is amazing function SQL and
[06:34:16] it's very simple and we can use it only
[06:34:18] for casting. So only to change the data
[06:34:20] type from one to another. And we cannot
[06:34:22] use this function in order to change the
[06:34:24] format. So if you are casting you will
[06:34:26] get always the standard format from
[06:34:32] SQL. So now let's go and compare our
[06:34:34] functions side by side. So we have our
[06:34:36] three functions. cast, convert and
[06:34:38] format and we can do two things either
[06:34:41] casting or formatting. So by the casting
[06:34:44] for the first function cast we can
[06:34:45] change any type to any other type. So
[06:34:48] there is no restriction at all. The same
[06:34:49] thing for the converts the same thing we
[06:34:52] can convert anything to anything. But
[06:34:53] for the format we can change only to a
[06:34:56] string. So any data type like a date or
[06:34:58] number to a string value because the
[06:35:01] main thing for the format is not
[06:35:03] changing the data type. Now if you are
[06:35:04] talking about changing the format of the
[06:35:06] values, you cannot use the cast function
[06:35:08] in order to change the format. So the
[06:35:10] cast function is only for casting. It
[06:35:12] makes sense. Now about the convert, we
[06:35:14] can use it in order to change the format
[06:35:17] of the date and time. But we cannot use
[06:35:19] it in order to change the number
[06:35:20] formats. And for that we have a
[06:35:22] dedicated function called format. So we
[06:35:25] can use it to change the format of the
[06:35:26] date and time and as well the numbers.
[06:35:28] So those are the main differences
[06:35:30] between those three functions. All
[06:35:31] right. So with those three functions we
[06:35:33] have learned how to do formatting and
[06:35:35] casting on date informations. Now moving
[06:35:38] on to the third group we have the date
[06:35:40] calculations and here we have two
[06:35:41] functions on how to do date calculations
[06:35:43] or mathematical operations on the dates.
[06:35:48] If okay so now we're going to start with
[06:35:50] the first function the date add. So what
[06:35:53] is date add? Date add can allow us to
[06:35:55] add or subtract a specific time interval
[06:35:59] to or from a date. So let's understand
[06:36:01] how the date add work. So here again we
[06:36:03] have our date August 20th 2025. So now
[06:36:06] in some scenarios we would like to add
[06:36:08] years to our dates. So for example let's
[06:36:11] say I would like to add three years to
[06:36:12] our date. So we can do that using the
[06:36:14] date ad. So if you do that in the output
[06:36:17] you will get 2028 August 20th only the
[06:36:20] date part is changed and where we have
[06:36:22] added three years but in other scenarios
[06:36:25] you would like to go and add months. So
[06:36:27] for example let's go and add two months
[06:36:29] to the August. So in the output you will
[06:36:31] get 2025 10 20 with that we have added
[06:36:34] two months and of course we can go and
[06:36:36] add days to our dates. So for example
[06:36:39] we're going to go and add five days to
[06:36:41] our date. So in the output we'll get the
[06:36:43] same year 2025 the same month August but
[06:36:46] only the day will be changed to 25. So
[06:36:49] we have added five days to the original
[06:36:51] dates. And of course we can go and
[06:36:53] subtract dates even though that the
[06:36:54] function called date add. So for
[06:36:56] example, we can go and subtract three
[06:36:59] years from our dates and we will get So
[06:37:01] if you do that, you will get 2022 August
[06:37:04] 20th or if you go and subtract two
[06:37:06] months from our dates. So it's going to
[06:37:08] stay the same year 2025. But this time
[06:37:10] instead of August, we will go back to
[06:37:12] June with the same date 20. And the same
[06:37:14] thing going to happen for the days if
[06:37:16] you go and subtract five days. So the
[06:37:18] same year 2025, the same month August,
[06:37:21] but only the days going to be instead of
[06:37:23] 20, it's going to be 15. So as you can
[06:37:25] see with the date ad you can manipulate
[06:37:27] the years, the month and the days by
[06:37:30] subtracting or adding new intervals. So
[06:37:33] this is how the date ad works. All
[06:37:35] right. So now let's check the syntax of
[06:37:36] the date ad. And here things little bit
[06:37:38] more complicated. We have to provide
[06:37:40] three informations. The first one is a
[06:37:42] part. What do you want to add? Do you
[06:37:44] want to add years or months or days and
[06:37:47] so on. Then the second one is interval.
[06:37:50] So it's like how many days? How many
[06:37:52] years? How many months? And then the
[06:37:54] last one is the date. This is the date
[06:37:56] that we're going to be manipulating by
[06:37:57] adding or subtracting intervals. Let's
[06:38:00] check the following example. We are
[06:38:02] saying here date add. So what is the
[06:38:04] part here is a year. That means we want
[06:38:06] to manipulate only the year parts. Then
[06:38:09] the interval here is two. So it is
[06:38:11] positive. We want to add two years. So
[06:38:14] it's going to go to each order and start
[06:38:15] adding two years for each date value.
[06:38:18] Now let's check another example. Here we
[06:38:20] are saying date add month. So here we
[06:38:22] want to manipulate the month part. But
[06:38:24] here we are saying minus4 that means we
[06:38:27] want to go and subtract four months from
[06:38:30] each value in the order date. So as you
[06:38:33] can see the value of the interval
[06:38:34] whether it's positive or negative. We
[06:38:36] are controlling here the function
[06:38:37] whether it is subtraction or addition.
[06:38:40] So let's have few examples about the
[06:38:41] date add using our field order dates. So
[06:38:44] for example let's go and add two years
[06:38:46] for each date. So we can do it like this
[06:38:49] date adds. So we are adding years that's
[06:38:52] why we're going to go with the part year
[06:38:53] and how many years we are adding we are
[06:38:54] adding two years. So this is our
[06:38:56] interval and our field our value is the
[06:39:00] order date. So now in the output as you
[06:39:02] can see we got a date but this date is
[06:39:04] always 2 years higher than the order
[06:39:06] date. So everywhere you have see 2027.
[06:39:09] Now let's go and add maybe three months
[06:39:12] for each date. Just going to go and copy
[06:39:14] it and say a month. Let's change the
[06:39:17] interval to three and we're going to
[06:39:19] call it three
[06:39:21] months later. So now if you check the
[06:39:23] output over here we have a new date but
[06:39:25] now the difference between it and the
[06:39:27] order date we have here always three
[06:39:29] months more than the order dates. So for
[06:39:31] example here we have January but in the
[06:39:34] new one we have April and for the next
[06:39:36] one we have February and in the new
[06:39:38] field we have May. So as you can see we
[06:39:40] are adding months over here. So as you
[06:39:42] can see we are adding monthses to our
[06:39:45] original filled order date. Now let's
[06:39:47] say that I would like to go and subtract
[06:39:48] 10 days. So let's go and do the same. So
[06:39:52] we're going to have the date add. Since
[06:39:54] we are talking about the days, it's
[06:39:55] going to be the day. We're going to
[06:39:56] subtract 10 days. So minus 10 for the
[06:40:00] order date. So let's call it 10 days
[06:40:04] before. Let's go and execute it. Now we
[06:40:07] got as well a new date. And this date
[06:40:09] has always 10 days before the order
[06:40:12] date. So for example, let's take the
[06:40:14] order number seven. In the order date we
[06:40:16] have 15, but in the new column we have
[06:40:18] five. So we have subtracted 10 days from
[06:40:22] the original filled order dates. So as
[06:40:24] you can see it's very simple to add or
[06:40:26] subtract days, year, months using the
[06:40:29] date
[06:40:32] add. All right. So what is date diff?
[06:40:35] diff stands for difference and date diff
[06:40:38] can going to can allow us to find the
[06:40:40] differences between two dates. All
[06:40:42] right. So let's understand how the date
[06:40:44] diff works in SQL. Now imagine we have
[06:40:46] two dates. We have the order date 2025
[06:40:50] August 20th and the shipping date is the
[06:40:53] 1st of February in the next year 2026.
[06:40:56] Now we might ask the question how many
[06:40:58] years have passed between the order date
[06:41:01] and the shipping date. So in order to
[06:41:03] answer this question we can use the
[06:41:05] function date diff and we can define the
[06:41:07] part year. If you do it like this it's
[06:41:09] going to subtract those two dates and it
[06:41:11] going to return one. So the date
[06:41:13] difference between those two dates is
[06:41:15] exactly one year. But now if the
[06:41:17] question is how many months are between
[06:41:19] the order date and the shipping dates.
[06:41:21] So here again we can go and use the date
[06:41:23] diff between the order date and the
[06:41:24] shipping date but we use the part month.
[06:41:27] If you do it like this in the output you
[06:41:29] will get three months. And now of course
[06:41:31] if the question is how many days are
[06:41:34] between the order date and the shipping
[06:41:36] dates. So here we can use the function
[06:41:38] date diff where we specify the day
[06:41:40] inside it and in the output you will get
[06:41:42] 68. So this is how the date diff works.
[06:41:45] You go and subtract two different dates
[06:41:47] and you will get in the output a number
[06:41:50] how many years how many months how many
[06:41:52] days. So that's it. All right. Now to
[06:41:54] the syntax of the date diff. It accept
[06:41:56] here as well three parameters. So the
[06:41:58] first one is the parts as usual year,
[06:42:01] month, day. And then here we need two
[06:42:03] dates, not only one, we need two. So we
[06:42:05] need the starting dates and the ending
[06:42:08] dates. So that means here we have the
[06:42:10] youngest dates and the end date going to
[06:42:12] be the oldest dates. So for example,
[06:42:14] here we have date diff and we are saying
[06:42:16] find the differences in years between
[06:42:19] the order dates. This is the start date
[06:42:22] and the shipping dates. So which dates
[06:42:25] normally happen? First we have to order
[06:42:26] something. So we have the order date and
[06:42:28] once you order what can happen next is
[06:42:30] the shipping date. That's why the
[06:42:32] shipping date is as an end date. So we
[06:42:35] want to find the differences between
[06:42:36] them in years or of course if you want
[06:42:38] to find the differences between them in
[06:42:40] days we have to go and change the part
[06:42:42] from year to day. So as you can see the
[06:42:44] syntax is very simple and very logical
[06:42:47] right. All right let's have the
[06:42:48] following simple task and it says
[06:42:49] calculate the age of employees. So let's
[06:42:52] see how we can solve that. So we're
[06:42:53] going to go and select first all the
[06:42:55] informations from employees. So sales
[06:42:58] and employees. Okay, let's execute it.
[06:43:01] Now in the employees, we don't have any
[06:43:03] informations about the age, but we have
[06:43:05] the birthday. So we can go and transform
[06:43:07] this birthday to an age. And of course,
[06:43:10] how we calculate the age? We count how
[06:43:12] many years between this year and the
[06:43:14] birthday. So that means we have to go
[06:43:16] and use two functions the date diff and
[06:43:19] the get day in order to have the year of
[06:43:22] the current year. So that means we have
[06:43:23] to go and use the function date diff. So
[06:43:25] let's go and do that. I'm going to go
[06:43:27] first selecting only few informations.
[06:43:30] So employee ID and P date. So let's
[06:43:34] start with the date diff. So if we are
[06:43:36] talking about the age we are calculating
[06:43:37] how many years that's why we're going to
[06:43:39] say as a part going to be the year. So
[06:43:42] what is the starting date is the birth
[06:43:44] date of the person. So it's going to be
[06:43:46] the birth date. And now we need the end
[06:43:48] date. We don't have here anything about
[06:43:50] the end date. The end date going to be
[06:43:51] the current year. So in order to get the
[06:43:54] current year, we're going to go with the
[06:43:55] function get dates. And with that we are
[06:43:58] getting the current date information.
[06:44:00] And this is exactly what we want. So
[06:44:02] let's close it and let's go and call it
[06:44:04] an age. So it's very simple. We are
[06:44:06] counting how many years between the
[06:44:08] birth dates and the current dates. So
[06:44:11] let's go and execute it. So now we are
[06:44:12] getting the ages. As you can see the
[06:44:14] first person is 33, the second one is 52
[06:44:17] and so on. And now you might getting
[06:44:19] different values than I'm getting now.
[06:44:22] And that's maybe you are doing the
[06:44:23] course now in 2025 or 2026 and the
[06:44:26] employees going to be older than now.
[06:44:28] Now we are 2024 and I'm getting those
[06:44:30] ages. So this is how we calculate the
[06:44:32] age using the help of two functions. The
[06:44:35] date diff and the get date. Okay. Okay,
[06:44:37] so now we have another task for the day
[06:44:39] diff and it says find the average
[06:44:41] shipping duration in days for each
[06:44:44] month. So here we have a lot of
[06:44:45] informations. Let's do it step by step.
[06:44:47] Let's first find out the shipping
[06:44:49] durations in days. So let's go and
[06:44:51] select few informations from our table.
[06:44:53] So select order ID. We have the order
[06:44:57] date, ship
[06:44:59] date and I think that's it. So from
[06:45:03] sales orders. So let's go ahead and
[06:45:06] execute it. So now we have our 10
[06:45:08] orders. We have the order date and the
[06:45:09] shipping dates. Now we have to go and
[06:45:11] create a new field called shipping
[06:45:13] duration. So what is the shipping
[06:45:15] duration? It is the number of days
[06:45:18] between the order dates and the shipping
[06:45:20] dates. So how many days it took from the
[06:45:23] order placement until the day of the
[06:45:26] shipping. So that means we have two
[06:45:27] dates and we have to go and find the
[06:45:29] differences between them. We're going to
[06:45:31] go with the function date diff. So now
[06:45:33] since we are saying in days we have to
[06:45:35] go with the part day. So what is the
[06:45:38] start date? The start date is the order
[06:45:40] date. And what is the end date? It's
[06:45:43] going to be the shipping dates like
[06:45:45] this. So I'm going to call it day to
[06:45:48] ship like this. Let's go and execute it.
[06:45:51] So now by checking the result for
[06:45:52] example for the order one it is ordered
[06:45:54] at the 1st of January and it is shipped
[06:45:57] on 5th of January. So between those two
[06:45:59] dates we have around 4 days. So four is
[06:46:02] the shipping duration and if you go to
[06:46:04] the order number three the differences
[06:46:06] between the order date and the shipping
[06:46:08] date we have around 15 days. So with
[06:46:10] that we have solved this part shipping
[06:46:12] duration in days. But now the task says
[06:46:15] we have to find the average duration for
[06:46:18] each month. So that means we have to go
[06:46:20] and select for example the month of
[06:46:21] January and find the average duration.
[06:46:24] So we have to go and do a simple
[06:46:25] aggregation. We're going to go to the
[06:46:27] date if at the start and say average.
[06:46:29] And we're going to close it over here.
[06:46:31] And let's go and rename it average
[06:46:34] shipping. And now we have to aggregate
[06:46:36] by the month. So we don't need the whole
[06:46:39] order dates. We need the month of the
[06:46:42] order date. So like this. We don't need
[06:46:44] of course the order ID, but now we need
[06:46:47] to group up the data using this
[06:46:49] dimension, the month order dates. So
[06:46:52] that's it. Let's go and execute it. So
[06:46:54] now in the output you can see we have
[06:46:56] three months and for each month we have
[06:46:58] the average shipping durations in days.
[06:47:01] So for the first month it is around 7
[06:47:04] days for February is as well 7 days and
[06:47:07] for March we have less duration 5 days.
[06:47:09] So with that we have solved the task. As
[06:47:11] you can see the date diff is very strong
[06:47:13] function in order to do data analytics
[06:47:16] using the dates information. All right.
[06:47:18] Right. So now we have the following task
[06:47:19] and it says find the number of days
[06:47:21] between each order and the previous
[06:47:24] order. So there's a lot of stuff going
[06:47:25] on over here. Let's do it step by step.
[06:47:28] Let's start by selecting the basic
[06:47:29] stuff. So select order ID, order date
[06:47:34] from the table sales orders. Let's go
[06:47:38] and execute it. So we have our 10 orders
[06:47:40] and we have the current order dates. So
[06:47:42] now we have to find the differences
[06:47:43] between two dates. order dates, the
[06:47:46] current one and the previous order
[06:47:48] dates. So in our data, we have the
[06:47:50] current order dates, but we don't have
[06:47:52] the previous order date for each order.
[06:47:54] And in order to calculate the previous
[06:47:56] one, do you remember about the window
[06:47:58] functions? We can go and use the lag in
[06:48:00] order to access a value from a previous
[06:48:03] records. So let's go and do that. The
[06:48:06] order date, I'm just going to call it
[06:48:07] current order dates. And let's go and
[06:48:10] find the previous order dates. So we're
[06:48:12] going to go with the lag of the order
[06:48:15] date because we are interested in the
[06:48:17] value of the order date. Now over we
[06:48:20] have to sort the data. So we're going to
[06:48:22] sort it
[06:48:24] by the order date as well. So this is
[06:48:27] going to help us always to access the
[06:48:30] previous value of the order date. So
[06:48:32] we're going to call it
[06:48:33] previous order date. Let's go and
[06:48:36] execute it and let's check the result.
[06:48:38] For the first order, we don't have
[06:48:39] anything previously. So that's why we
[06:48:41] are getting a null. For the second
[06:48:43] record, the current order date is the
[06:48:45] 5th of January and the previous one is
[06:48:48] the 1st of January. And this value comes
[06:48:50] from the previous record, the previous
[06:48:53] order. Great. Amazing. So with that we
[06:48:55] have now the two dates, the current date
[06:48:57] and the previous one. And now we can go
[06:48:59] very simply finding the number of days
[06:49:01] between those two dates. And we can do
[06:49:04] using the amazing function date diff. So
[06:49:07] we are interested on the days that's why
[06:49:09] it's going to be the day. So what is the
[06:49:12] starting day? If you check those two
[06:49:13] dates, you can see that the previous
[06:49:15] order date is the starting date. So
[06:49:18] we're going to take the whole thing, the
[06:49:20] whole window function and put it over
[06:49:22] here. So I just moved my picture. So
[06:49:24] here is the previous order dates. And
[06:49:26] now the end date, what's going to be?
[06:49:28] It's going to be the current order date
[06:49:30] which is our order date like this. So
[06:49:33] again, we are finding the number of days
[06:49:35] between the previous dates and the
[06:49:38] current dates. So that's it. Let's close
[06:49:41] it. So I'm just going to call it number
[06:49:43] of
[06:49:44] days. So let's go and execute it. Now of
[06:49:47] course we have here null. So we will get
[06:49:49] as well null in the output. And now you
[06:49:51] can check over here how many days
[06:49:53] between those two dates. We have exactly
[06:49:55] four days. And as well for the next one
[06:49:57] we have around 5 days, 10 days and so
[06:50:00] on. So we have solved the task. We have
[06:50:02] now the number of days between each
[06:50:04] order and the previous order. So this
[06:50:06] type of analyszis is very important in
[06:50:08] the business. We call it time gap
[06:50:10] analyzes and we have done it using the
[06:50:12] help of the window function and as well
[06:50:14] the date function date diff. So date div
[06:50:17] function is amazing function to do data
[06:50:20] analyzes. All right. So with those two
[06:50:21] functions we have learned how to do
[06:50:23] mathematical operations on date
[06:50:25] informations or we can call it date
[06:50:27] calculations. Now moving on to the
[06:50:28] easiest and the last group, we have the
[06:50:31] date validation. And here we have only
[06:50:33] one function, the is
[06:50:37] date. Okay. So what is is date? So the
[06:50:41] is date is very simple. It's going to
[06:50:42] check whether a value is a date. So it
[06:50:45] going to return one if the string value
[06:50:47] is a valid date or zero if it is not a
[06:50:51] valid date. Okay. So let's check quickly
[06:50:53] the syntax of the is date. It's very
[06:50:55] simple. The keyword is date is the
[06:50:57] function name and it accepts only one
[06:50:59] value. So for example you can pass a
[06:51:00] string like this and you can ask SQL is
[06:51:03] it a date. So is date and the value and
[06:51:06] of course for this example you will get
[06:51:07] true or one. So as you can see we are
[06:51:09] passing here a string value and we are
[06:51:11] validating whether it is good enough to
[06:51:14] be a date or as well you can go and
[06:51:15] specify a number like here 2025. So is
[06:51:19] this value a date and of course SQL
[06:51:21] going to accept it and say yeah this is
[06:51:22] a year so you will get as well a one. So
[06:51:24] you can pass as well a number or
[06:51:26] integer. So you are just checking the
[06:51:28] values whether they are suitable enough
[06:51:30] to be a date. So that's all about the
[06:51:32] syntax of the is dates. Okay. So now
[06:51:33] let's have few examples. For example,
[06:51:35] let's go and select and we're going to
[06:51:36] say is date and we will check a value.
[06:51:39] So let's say this value is a string 1 2
[06:51:42] 3. Let's go and call it date. Check one.
[06:51:46] Let's go and execute it. Now in the
[06:51:48] output it's going to say no, it is not a
[06:51:50] date. And that's why we are getting the
[06:51:52] value zero which is correct because 1 2
[06:51:54] 3 is not a date. Let's pick another
[06:51:57] value. The same thing is dates. And now
[06:51:59] the value going to be the following. So
[06:52:02] 2025 August 20. So let's call it date
[06:52:06] check 2. And let's go and execute it.
[06:52:09] Now in the output we will get one. That
[06:52:11] means the value that we have provided is
[06:52:14] a date. And that's why we have a one in
[06:52:16] the output because ESKL is saying this
[06:52:18] is a date. Now let's have another
[06:52:20] example. We're going to take the whole
[06:52:22] thing. So this is a check three and
[06:52:26] remove this from here. But I would like
[06:52:27] to go and change the format. So let's
[06:52:29] say that we start with the day then
[06:52:31] month and then the year. Let's go and
[06:52:34] check. Now in the output you can see it
[06:52:36] is zero because SQL does not understand
[06:52:38] the formats. So we are not following the
[06:52:40] standard format of the database and
[06:52:42] scale and that's why going to say no
[06:52:44] this is not a date. This is like a
[06:52:46] string value. So this means only if the
[06:52:49] value is following the status format SQL
[06:52:51] going to understand this is a date. Now
[06:52:53] let's go and check another thing for
[06:52:55] example let's say is date and let's have
[06:52:57] only the year. So 2025 and let's give it
[06:53:01] the name date check for let's go and
[06:53:04] execute it. Now in the output we will
[06:53:06] get one. So that means is considering
[06:53:08] this value as a date. So that means
[06:53:11] Iskll is smart enough to understand okay
[06:53:12] we have provided a year information and
[06:53:15] is going to accept it and say okay maybe
[06:53:17] this is the 1st of January of 2025. Now
[06:53:20] let's go and do the same thing but for
[06:53:22] the month let's see whether SQL going to
[06:53:24] accept it. So check five and we have the
[06:53:27] month of August. Let's go and check now
[06:53:30] going to say no I don't understand this
[06:53:32] value this is zero. So that mean this
[06:53:35] value is provided is not a date. So by
[06:53:37] checking those results as you can see
[06:53:39] SQL understand only the standard formats
[06:53:41] and it allow you as well to check
[06:53:43] whether a year is a date. So this is how
[06:53:45] the is date works in SQL. And now you
[06:53:47] might ask well when I'm going to do this
[06:53:49] when I'm going to check whether the
[06:53:51] value is a date or not. Let me give you
[06:53:52] this following scenario. Now imagine
[06:53:54] that we have the following date. So we
[06:53:56] have four values as a string. And now if
[06:53:58] you check the data you can see that we
[06:54:00] are following the standard format but
[06:54:02] only one value has an issue. So we have
[06:54:05] here data quality problem. So now what
[06:54:06] we want to do, we want to go and cast
[06:54:08] this string value to a date. We don't
[06:54:11] want this to stay as a string value. We
[06:54:13] would like to have it in the final
[06:54:15] result as a date. So what we usually do
[06:54:17] is that we go and have like subquery on
[06:54:20] top of those values. So like this. So
[06:54:22] now what we're going to do, we're going
[06:54:23] to go and say we would like to go and
[06:54:25] cast the order dates as date. We don't
[06:54:29] want it as a string. And we're going to
[06:54:31] call it order dates from these values.
[06:54:34] So let me just make it like this and
[06:54:37] let's go and execute it. Now SQL going
[06:54:39] to give you an error and say well I
[06:54:41] cannot convert everything to a date
[06:54:44] because you have maybe corrupt data and
[06:54:46] this is of course because of this row.
[06:54:48] So SQL is not able to convert this
[06:54:51] string to a date. But of course now the
[06:54:53] example is very simple. We know that but
[06:54:55] if you have a huge table it's going to
[06:54:56] be really hard to identify those issues.
[06:54:59] But now still I would like to go and
[06:55:01] convert those value here. I don't want
[06:55:03] to get an error. And now if there is
[06:55:04] like some values like here that is
[06:55:07] corrupt and so on this value could be
[06:55:09] null. So how we can force SQL to convert
[06:55:12] the data type from string to date and
[06:55:14] not give us this error. And for this we
[06:55:17] can go and use the help of the function
[06:55:19] is date. Let me show you how I usually
[06:55:20] do it. So let's go and say let's check
[06:55:23] whether the order date is a date. So
[06:55:26] let's have it like this. And now before
[06:55:28] we go and execute, I'm going to make
[06:55:30] this as a comment because if I execute
[06:55:32] it like this, we will get an error. And
[06:55:34] let's go and get the order date in our
[06:55:36] select. So let's go and execute it. Now
[06:55:38] as you can see in the output, we have
[06:55:40] our string value. So they are not yet a
[06:55:42] date. And we have the result of our
[06:55:44] check. So as you can see the first row,
[06:55:46] we are getting a zero. So it's saying
[06:55:48] this value is not a date. But for all
[06:55:51] other values, we are getting one. So
[06:55:53] they are passing the check and they are
[06:55:54] dates. So now what we're going to do
[06:55:56] we're going to go and build a logic
[06:55:57] where we're going to say go and cast the
[06:56:00] value from string to date only if the
[06:56:03] flag or the check is equal to one. So
[06:56:06] that means we can go and use the help of
[06:56:08] the case when statement. Let me show you
[06:56:10] how we can do that. So let's do it step
[06:56:11] by step. We're going to say case win.
[06:56:14] Now we need the check. So is
[06:56:16] dates the order date. So if the output
[06:56:20] of this check is equal to one then you
[06:56:23] are allowed to do the casting. So let's
[06:56:24] go and get the cast as a result of this
[06:56:28] condition and if it's not equal to one
[06:56:30] then it could stay as a null. So let's
[06:56:32] have it as a null if it didn't pass the
[06:56:34] test. So end and we can call it new
[06:56:36] order dates. So now let's go and execute
[06:56:39] it. Now as you can see we are not
[06:56:41] getting error from SQL. So now if you
[06:56:42] check the output for the invalid dates
[06:56:45] we are getting a null. So we are not
[06:56:47] getting an SQL error. And now only if
[06:56:49] these string values are a valid dates
[06:56:52] it's allowed to be casted. So that you
[06:56:54] can go and cast a string value to a date
[06:56:57] even though that you have bad data
[06:56:58] quality and this is very important step
[06:57:00] in order to prepare the data before
[06:57:02] doing analyszis and it help us as well
[06:57:04] to find data quality issues. So for
[06:57:06] example we can go over here and say you
[06:57:08] know what let's go and search for all
[06:57:10] issues. So we're going to go and take
[06:57:12] the is dates. So let's go and get the
[06:57:14] check and I'm going to say let me see
[06:57:16] all string values that are invalid that
[06:57:19] are failing the test. So let me execute
[06:57:21] it. And with that we are getting this
[06:57:22] record. And now imagine we have a lot of
[06:57:24] data. So it's now it's really easy to
[06:57:27] identify those issues by just using the
[06:57:29] S dates. So this is as well amazing way
[06:57:31] in order to identify data quality
[06:57:33] issues. Now of course you might say you
[06:57:35] know what I don't want to see here null.
[06:57:36] Maybe let's get a dummy value. Well it's
[06:57:39] very easy. We can go over here and say
[06:57:41] else. So and we can go and get for
[06:57:43] example very large value something like
[06:57:46] this that is easy to identify. So now
[06:57:48] with that instead of getting nulls
[06:57:50] inside your data you can get such a
[06:57:52] dummy value. So now you understand the
[06:57:54] use case of the is dates and why this
[06:57:56] function is amazing doing data
[06:58:02] cleanup. All right. So with that we have
[06:58:04] covered 13 different date and time
[06:58:07] functions in SQL. So we have learned how
[06:58:09] to extract the date parts using seven
[06:58:12] different functions and we have learned
[06:58:13] as well when to use which one. So they
[06:58:15] are amazing in order to do data
[06:58:17] aggregations and as well filtering. And
[06:58:19] then we have learned how to change the
[06:58:21] date format from one to another and as
[06:58:23] well how to change the data types. And
[06:58:25] then we learned how to do mathematical
[06:58:27] operations on our dates. So how we can
[06:58:30] add or subtract days, years, months from
[06:58:33] a date or the amazing function the date
[06:58:36] diff where we can go and find the
[06:58:38] differences in days or years between two
[06:58:40] days. And the last one we can go and
[06:58:42] validate whether the values that we have
[06:58:44] are dates or not. So as we learned date
[06:58:47] functions are amazing functions in order
[06:58:49] to do data analyzes and reporting. All
[06:58:51] right my friends. So with that we have
[06:58:53] learned a lot of very important SQL
[06:58:55] functions and how to manipulate the date
[06:58:57] and time values in your database using
[06:58:59] SQL. Now in the next section we're going
[06:59:01] to start talking about the null
[06:59:03] functions in order to handle the nulls
[06:59:05] inside your tables. So let's go.
[06:59:11] So what are the nulls? Imagine you are
[06:59:13] filling out a forum and there will be
[06:59:15] usually like fields that are required
[06:59:17] and another fields that are optional. So
[06:59:20] what usually happens? We leave those
[06:59:21] optional fields unanswered. So we don't
[06:59:24] provide any values and we leave it
[06:59:26] empty. And now once we are done filling
[06:59:28] out the form and we click on register,
[06:59:30] the data will be inserted into database
[06:59:33] tables. So now what can happen? The
[06:59:35] fields where you have provided answers
[06:59:36] and values can be filled inside the
[06:59:38] table while the unanswered fields will
[06:59:41] have no value and this is what we call
[06:59:44] in SQL a null. So in databases a null
[06:59:47] means nothing unknown. It is not equal
[06:59:50] to anything. So it is not equal to zero
[06:59:52] or empty string or blank space. A null
[06:59:55] is simply nothing. It tells us there is
[06:59:58] no value and it is missing. It's like
[07:00:01] saying I don't know what this value is.
[07:00:03] So this is what a null means in
[07:00:08] SQL. All right friends, so now we're
[07:00:11] going to do a deep dive into special SQL
[07:00:13] functions on how to handle the nulls
[07:00:16] inside our data. Now in some scenarios
[07:00:18] we have nulls inside our tables and we
[07:00:21] would like to go and remove it and
[07:00:23] replace it with a new value like for
[07:00:25] example 40. And in order to do that in
[07:00:27] scale we have two functions. The first
[07:00:29] one called is a null and the second one
[07:00:32] called coales. But now let's say that we
[07:00:35] have another scenario where we have a
[07:00:37] value inside our table like the 40 and
[07:00:39] we want to go and make it as a null. So
[07:00:41] now we are doing the exact opposite. We
[07:00:43] are replacing the value with a null and
[07:00:46] for that we have the SQL function null
[07:00:48] if. So as you can see with those two
[07:00:50] scenarios we are replacing stuff. So
[07:00:52] from null to value or from value to
[07:00:55] null. So they are really helpful in
[07:00:57] order to manipulate the data inside our
[07:00:59] databases. Now moving on to another
[07:01:01] scenario where we don't want to
[07:01:02] manipulate anything. We want just to
[07:01:04] check. So we don't want to replace or
[07:01:06] convert anything. We want just to check
[07:01:08] in our database whether we have a null
[07:01:11] value. And for that we have a function
[07:01:13] called is a null. But between the is and
[07:01:15] null there is like space. It is
[07:01:17] different than the first function. So if
[07:01:19] you apply is null you're going to get a
[07:01:21] boolean true or false. For this scenario
[07:01:23] you will get true. Or the second option
[07:01:25] you can go and check whether the value
[07:01:27] is not null. So we can use is not null
[07:01:29] and for this example you can get false.
[07:01:31] So in the output we are getting a
[07:01:33] boolean true or false. So those keywords
[07:01:35] are really amazing in order to check
[07:01:38] whether we have nulls inside our data.
[07:01:40] So this is the big picture of all
[07:01:41] functions that we have in SQL in order
[07:01:43] to handle the nulls. So now let's go and
[07:01:45] understand those functions one by one.
[07:01:52] So let's start with the first function
[07:01:53] is null. Is null going to go and replace
[07:01:56] a null with a specific value. Now the
[07:01:59] syntax of the isnull is very simple.
[07:02:00] We're going to use the keyword is a null
[07:02:03] and it accepts two arguments. First the
[07:02:05] value and then the second the
[07:02:07] replacement value. So let's have an
[07:02:09] example. We can go and use the is null
[07:02:11] for the column called shipping address.
[07:02:13] So we are checking the nulls inside it.
[07:02:15] And if SQL encounters any null, it going
[07:02:18] to go and replace it with the value
[07:02:20] unknown. So this going to be like a
[07:02:22] default value for the nulls. So the
[07:02:24] first value is a column and the second
[07:02:26] value is like static. Always going to be
[07:02:29] the unknown if we find any nulls. Now of
[07:02:32] course in other scenarios we don't want
[07:02:33] to have it always like the unknown. We
[07:02:36] would like to use another column to help
[07:02:38] the first one. So let's have this
[07:02:40] scenario. So now with this syntax we are
[07:02:42] checking the values of the shipping
[07:02:44] address and if we find any nulls it's
[07:02:47] going to get the replacement from the
[07:02:49] billing address. So here in this example
[07:02:51] we have two columns. We don't have here
[07:02:53] any static value. We will get the values
[07:02:55] of the billing address only if the
[07:02:58] shipping address is null. So we are
[07:03:00] replacing the nulls using the help of
[07:03:02] other column. And in the first scenario
[07:03:04] we are replacing the nulls with a static
[07:03:07] value the default value. So let's have a
[07:03:09] very simple example in order to learn
[07:03:11] how this works. So what we are doing we
[07:03:13] are checking whether the value is null.
[07:03:15] If it's yes then we're going to go and
[07:03:17] get the value from the replacement and
[07:03:20] if the value is not null then show the
[07:03:22] value itself. So we have the following
[07:03:24] example. We are going to check the
[07:03:25] values from the shipping address and if
[07:03:28] there is nulls then go replace it with
[07:03:30] the default value na. So let's see how
[07:03:32] going to go and execute this very simple
[07:03:34] example. We have two orders. The first
[07:03:36] order we are checking the submit address
[07:03:39] is the value of this address is null.
[07:03:42] Well, no. We have a value a. So that's
[07:03:44] why it's scale going to go and return
[07:03:46] the same value. So in the outputs we
[07:03:49] will get a. So if it's not null, it's
[07:03:52] going to return the same value. So now
[07:03:53] it's going to move to the second order
[07:03:55] and here we have the shipment address as
[07:03:57] a null. So what going to happen here? If
[07:03:59] the value is null, then we going to get
[07:04:01] the replacement value. So what is the
[07:04:04] replacement value is the NA. So that's
[07:04:07] why in the output we will not get a null
[07:04:09] we will get the N A. So if you check the
[07:04:12] result what happens? We're going to get
[07:04:14] the addresses from the shipping address
[07:04:16] but only if we have a null we will get
[07:04:18] like default value. It's very important
[07:04:20] to understand if you are using the
[07:04:22] default value in the output you will
[07:04:24] never get a null. All right. So let's
[07:04:26] have another example for the second
[07:04:27] scenario where we are not using a
[07:04:29] default value we are using a column. So
[07:04:32] we have a supportive column that's going
[07:04:33] to be checked. So in this scenario we
[07:04:35] are saying is null shipping address and
[07:04:37] billing address. So we have two columns
[07:04:39] and of course the logic going to be the
[07:04:41] same right. So we are checking only
[07:04:43] once. Let's see how SQL going to execute
[07:04:45] this example. We have this time three
[07:04:47] orders and we have addresses from the
[07:04:49] shipments and as well from billing. So
[07:04:51] now SQL is always focusing on the
[07:04:53] shipping address since it is the first
[07:04:55] column. So we are not checking the
[07:04:57] billing address at all. So it start with
[07:04:59] the first order. Is it null? Well, no,
[07:05:01] we have the value A. So, we will get it
[07:05:04] as well in the output and SQL will not
[07:05:07] get anything from the billing address.
[07:05:09] So, we will get a. So, that's it for the
[07:05:11] first order. Now, it's still going to go
[07:05:13] to the second order. And this time,
[07:05:14] we're going to have a null. So, now in
[07:05:16] the rule, we are saying if the shipping
[07:05:18] address is a null, go get the value from
[07:05:21] the billing address. So, this time we're
[07:05:23] going to go to the replacement, right?
[07:05:25] So we will get the value C in the output
[07:05:29] because the shipping address is the
[07:05:31] null. Now let's move to the third row.
[07:05:32] As you can see here we have again null.
[07:05:34] So SQL going to go and get the value
[07:05:37] from the billing address. But here in
[07:05:39] this scenario the billing address is as
[07:05:41] well null. That's why we will get the
[07:05:44] value null in the output. So as you can
[07:05:46] see having the replacements values from
[07:05:48] a column there is no guarantee that
[07:05:51] there will be always a value like here
[07:05:53] in the third order it is a null that's
[07:05:56] why we will get null as well in the
[07:05:58] output. So if you think you are using is
[07:06:00] null to replace all the nulls by having
[07:06:03] two columns you might end up as well
[07:06:05] having a null in the output if the
[07:06:07] replacement having nulls. So if you want
[07:06:09] to make sure you don't get any nulls in
[07:06:12] the output you have to go and use a
[07:06:14] static value. So this is how SQL execute
[07:06:17] the
[07:06:21] isnull. All right. So what is coales?
[07:06:24] Coal is going to go and return the first
[07:06:26] null value from a list. All right. So
[07:06:29] now the syntax of the coales is way
[07:06:31] better than the is null. Here it accepts
[07:06:34] like a list of many values. So here for
[07:06:36] example we have value 1 2 3 you can add
[07:06:39] four five as much as you want. So we are
[07:06:42] creating here a list of values to be
[07:06:44] checked. So for example, we still can
[07:06:46] use it like the isnull where we have the
[07:06:48] shipping address where we replace the
[07:06:50] null with a static value the unknown or
[07:06:53] as we learned we can go and use two
[07:06:54] columns shipping address and the billing
[07:06:56] address. So so far it's like the same
[07:06:58] use cases as the is null but now of
[07:07:00] course the kalis is not only limited to
[07:07:02] two we can go and use three. So we are
[07:07:05] saying go check the shipping address if
[07:07:07] it's null then go check the billing
[07:07:09] address. If it is as well null then use
[07:07:12] at the end the default value the static
[07:07:14] one the unknown. So as you can see we
[07:07:16] can use more than two values with the
[07:07:18] coalis. Okay. So now let's understand
[07:07:19] the cowless and how this works. Now the
[07:07:22] workflow is something similar to the
[07:07:24] isnull. So in this example we have two
[07:07:26] columns shipping address and the billing
[07:07:28] address. It's going to consider it as a
[07:07:30] list and it's going to start checking
[07:07:32] from left to right. So it's going to
[07:07:34] check the first value from the shipping
[07:07:35] address whether it's null. If no, it's
[07:07:38] not null then we're going to go and get
[07:07:40] the value one. So we will get the value
[07:07:42] from the shipping address. And if yes,
[07:07:44] it is null then it's going to go and get
[07:07:47] the value two. So we're going to get the
[07:07:49] value from the shipping address. Now we
[07:07:51] have the similar data. We have three
[07:07:52] orders. Let's see how going to execute
[07:07:54] it. So it's going to start with the
[07:07:55] first row and it's going to focus on the
[07:07:57] shipping address. So here the value is
[07:07:59] not null. So we have it as an A. So
[07:08:02] that's why we will get the value one. So
[07:08:05] we will get the value from the shipping
[07:08:07] address and nothing else going to be
[07:08:09] checked. Now moving on to the second
[07:08:11] row. This time the shipping address is
[07:08:13] null. So it's going to go and get the
[07:08:16] value from the second column and it's
[07:08:18] going to be the C. Right? So in the
[07:08:21] output we will get C. Now to the last
[07:08:23] example, we have it as a null and it's
[07:08:25] going to go and get the value from the
[07:08:27] second column and this time we're going
[07:08:29] to get as well a null like the is null
[07:08:32] function. So at the results we are
[07:08:34] getting exactly the same result as
[07:08:36] isnull. So for this scenario it doesn't
[07:08:38] matter whether you use isnull or
[07:08:40] kowalis. So now of course we are still
[07:08:41] not happy with that because I don't want
[07:08:43] to see any nulls in the output and I
[07:08:46] will still need to use the billing
[07:08:47] address instead of any static values. So
[07:08:50] I would like to have everything the
[07:08:51] values from the billing address and as
[07:08:53] well I would like to have at the end a
[07:08:55] default value so that I don't have any
[07:08:58] nulls in the output. So how we going to
[07:08:59] solve it? So now we can use the power of
[07:09:01] the account list where we can include
[07:09:03] multiple values in one function. So what
[07:09:06] we're going to do we're going to have
[07:09:07] the shipping address first then the
[07:09:09] billing address and at the end we're
[07:09:11] going to have the default value. So we
[07:09:13] have now a list of three values and of
[07:09:15] course our workflow going to be a little
[07:09:17] bit bigger. So again here it's going to
[07:09:19] start from the left to the right. So
[07:09:21] first it's going to go and check the
[07:09:22] value one. If it is null then it's going
[07:09:24] to go as well checking the value two.
[07:09:27] And if the value two is as well null, we
[07:09:29] will get the last value. It's going to
[07:09:31] be the value three. So now let's run the
[07:09:33] example again using the new kalis. So
[07:09:35] the first thing we're going to go and
[07:09:37] check the first value which is the
[07:09:38] shipping address for the record number
[07:09:40] one. So now as you can see the value is
[07:09:42] not null. So we have here an a. So what
[07:09:45] going to happen? We're going to get the
[07:09:46] value a as well in the output. So that
[07:09:49] means this one going to be activated and
[07:09:51] we will not check anything else. So that
[07:09:54] means in the output it's going to be
[07:09:56] like this. and the first value is
[07:09:58] returned and everything else will be
[07:10:00] ignored. So, SQL will not check
[07:10:02] anything. So, as you can see, we are
[07:10:04] returning the first null value. So, now
[07:10:08] let's move to the second order. Now,
[07:10:09] we're going to check again the first
[07:10:10] value. Is it null? Well, yes. As you can
[07:10:13] see, we have here a null. So, that means
[07:10:16] we're going to go and activate this path
[07:10:18] over here on the right side. So, now SQL
[07:10:20] will not go blindly putting anything
[07:10:22] from the billing address in the results.
[07:10:24] First SQL has to check it. So SQL going
[07:10:27] to check it whether it's null or not.
[07:10:29] SQL going to go and return it as well in
[07:10:31] the output. And we have activated this
[07:10:34] path. So SQL is returning the value two
[07:10:37] which is the value from the billing
[07:10:39] address. So now let's move to the third
[07:10:40] order. SQL first going to go and check
[07:10:43] the shipping address. Is it null? Well
[07:10:46] yes it is null. So that's why SQL going
[07:10:48] to go and start checking the second
[07:10:51] value. So this time SQL will not return
[07:10:54] the billing address value since it's
[07:10:56] null. It's going to go and return the
[07:10:58] third value. And what is the third
[07:11:00] value? It is our static value the NA. So
[07:11:03] in the output we're going to get the NA
[07:11:07] our default value. So with that as you
[07:11:09] can see in the output we will not get
[07:11:11] any nulls. We are using the default
[07:11:13] value and as well multiple columns. So
[07:11:15] if you check the output, it's always the
[07:11:17] first priority to check the values from
[07:11:20] the first column, the shipping address.
[07:11:22] If it's null, then the second priority
[07:11:24] going to be the billing address. If it's
[07:11:26] null, then the last priority, it's going
[07:11:29] to be the default value. So as you can
[07:11:30] see, SQL is checking the values from
[07:11:32] left to right and it stops immediately
[07:11:35] once it encounters the first not null
[07:11:38] value and return it in the results. So
[07:11:40] this is how the cow works.
[07:11:46] All right. So now let's have a quick
[07:11:47] summary about the differences between
[07:11:49] the kowalis and isnull. So as we learned
[07:11:51] the isnull is limited only to two values
[07:11:54] where the kowalis is amazing where you
[07:11:56] can have a list of multiple values which
[07:11:59] is a great advantage compared to the
[07:12:01] isnull. Now if you are talking about the
[07:12:02] performance the isnull is faster than
[07:12:05] the kawalis. So if you want to optimize
[07:12:07] the performance of your query then go
[07:12:09] with the isnull. Now there is another
[07:12:11] problem with the isnull is that we have
[07:12:14] different keywords for different
[07:12:15] databases. So for Microsoft SQL server
[07:12:18] we use the isnull as we learned but in
[07:12:21] Oracle they have different
[07:12:22] implementations they use the NVL and
[07:12:24] other database like MySQL you have if
[07:12:28] null and all those three functions are
[07:12:29] doing the same but we have different
[07:12:31] implementations for different databases
[07:12:33] but in the other hand the cowis it is
[07:12:35] available in all different databases. So
[07:12:37] here we have like an agreement or
[07:12:39] standards between the databases of using
[07:12:41] the kowalis. So here again this is a
[07:12:43] great advantage for the kowalis because
[07:12:45] if you are writing like scripts and
[07:12:47] someday you want to migrate from one
[07:12:49] database to another. If you are using
[07:12:50] the kowalis you don't have to change
[07:12:52] anything but if you are using the isnull
[07:12:54] then you have to go and adjust your
[07:12:56] queries and scripts with the correct
[07:12:58] functions. That's why I tend always to
[07:13:01] use the kalis and avoid using the
[07:13:03] isnull. Only if it's really necessary
[07:13:06] that I have really bad performance, I go
[07:13:08] and try the isnull. But I usually stick
[07:13:10] with the kowalis. So that is my advice
[07:13:13] for you. Go with the kowalis and stick
[07:13:15] with the standard.
[07:13:19] Now the use cases of the kowalis and the
[07:13:21] isnull are very similar and we mainly
[07:13:24] use them in order to handle the null
[07:13:27] before doing any SQL task. For example,
[07:13:30] we can use them in order to handle the
[07:13:32] null before doing data aggregations. So
[07:13:35] let's understand what this means.
[07:13:36] Imagine that we have three sales. We
[07:13:38] have 15, 25, and a null. Now if you go
[07:13:41] and use an aggregate functions like the
[07:13:43] average, what's going to happen? SQL
[07:13:45] going to calculate it like this. 15 + 25
[07:13:48] divided by two and the average is going
[07:13:50] to be 20. So as you can see here SQL is
[07:13:53] including only the two values 15 and 25
[07:13:56] and ignores totally the null value. So
[07:13:59] in the calculations the null will not be
[07:14:01] included because if SQL does that the
[07:14:04] output going to be as well null. So the
[07:14:06] nulls are totally ignored. Now the same
[07:14:08] thing can happen with the other
[07:14:09] aggregate functions like the sum count
[07:14:11] if you are counting the sales min and
[07:14:13] max. There is only one exception about
[07:14:16] the aggregate function count. If you are
[07:14:17] using it with the star, SQL here is
[07:14:20] considering not the values. SQL going to
[07:14:22] consider the rows. That's why SQL going
[07:14:24] to go and include all those rows and
[07:14:26] find the output going to be three. Now
[07:14:28] in some scenarios, if your business
[07:14:30] understand the null as zero, then you're
[07:14:32] going to have a problem with the result
[07:14:34] of your analyzes if you don't handle the
[07:14:36] nulls. So what we have to do? We have to
[07:14:38] handle the null before doing the
[07:14:39] aggregations. So we have to go and
[07:14:41] replace a null with zero using either
[07:14:43] the isnar or the kowalis. So once you do
[07:14:46] that the calculation going to be changed
[07:14:48] for the average. So it's going to be 15
[07:14:50] + 25 + 0 divided by 3 and the output
[07:14:54] this time going to be 13.3. So with that
[07:14:57] you're going to get more accurate
[07:14:58] results for the business if they
[07:15:00] understand nulls as zero. All right. So
[07:15:02] now we have the following example. It
[07:15:03] says find the average scores for the
[07:15:05] customers. So let's go and solve it. So
[07:15:07] we're going to go and select the
[07:15:09] customer ID, the score from table
[07:15:14] customers. So let's go and execute it.
[07:15:16] So as you can see, we have four
[07:15:18] customers with score and the last one
[07:15:19] doesn't have any score. So we have it as
[07:15:21] a null. Let's go and calculate the
[07:15:23] average for the score and I would like
[07:15:27] to have the window function in order to
[07:15:29] see the details as well. So this is
[07:15:31] average scores. So let's go and execute
[07:15:35] it. Now of course what is going on here?
[07:15:37] The four values going to be added to
[07:15:38] each others and divided by four and the
[07:15:40] null is totally ignored. Now of course
[07:15:42] the question is what the business
[07:15:44] understand with the null. If it is zero
[07:15:47] then we have inaccurate results. So
[07:15:50] let's go and fix it. Now this time we're
[07:15:52] going to say okay we're going to have
[07:15:53] the average but instead of score we're
[07:15:55] going to handle the nulls first. So we
[07:15:57] have to replace any nulls with zero. We
[07:16:00] can go and use the kowalis or the
[07:16:02] isnull. So I will go with the cabalis
[07:16:04] like this and score if you find any null
[07:16:09] make it zero. So that's it and as well I
[07:16:12] will go with the window function. So
[07:16:14] average scores let's call it two. Now
[07:16:17] let's go and execute it. Now as you can
[07:16:19] see in the output we got 500 and this is
[07:16:22] different than the previous average and
[07:16:24] that's because we have replaced the null
[07:16:26] with zero. Let's just go and display it
[07:16:29] in order to understand it. So I will
[07:16:31] copy it and put it here. So let's call
[07:16:33] it score two and execute it. So now SQL
[07:16:36] is going to summarize all those values
[07:16:38] and divided by five and that's why we
[07:16:41] are getting the 500. So if our business
[07:16:44] understand the null as a zero this
[07:16:46] average going to be more accurate after
[07:16:49] we handle the null. As you can see in
[07:16:50] some scenarios we have to handle the
[07:16:52] nulls before doing any data
[07:16:54] aggregations.
[07:16:59] All right, moving on to the next use
[07:17:00] case for the kowalis and isnull. We can
[07:17:02] use them in order to handle the nulls
[07:17:05] before doing any mathematical
[07:17:07] operations. So let's understand what
[07:17:09] this means using the plus operator. So
[07:17:11] if you do plus operator between two
[07:17:12] numbers like 1 + 5, you are summarizing
[07:17:15] the values and you will get six. And if
[07:17:17] you do the plus operator between string
[07:17:19] values like a + b. So now what we are
[07:17:22] doing, we are doing data concatenations
[07:17:25] and the output going to be a b. So now
[07:17:27] if you go and replace the one with a
[07:17:29] value like zero. So 0 + 5 we will get
[07:17:32] five. Nothing fancy about that. And for
[07:17:34] the strings if you go and replace a
[07:17:36] value with an empty string. So there is
[07:17:39] zero characters between the two quotes
[07:17:41] plus the B. So in the output you will
[07:17:44] get only B. So it's fine and nothing is
[07:17:46] critical. But now we come to the
[07:17:48] problem. If you use a null if you
[07:17:49] replace the one with null in the output
[07:17:52] you will get a null. because you are
[07:17:54] saying okay five plus something that I
[07:17:56] don't know so SQL says okay you are
[07:17:58] summarizing now a value with a no value
[07:18:01] it is unknown so I don't as well know
[07:18:04] what going to be the answer that's why
[07:18:06] going to say it's going to be null just
[07:18:07] don't know what is the answer and the
[07:18:09] same thing can happen with anything else
[07:18:11] like the string so if you're saying null
[07:18:13] plus b and here going to say the same
[07:18:15] thing the null is unknown and the answer
[07:18:18] going to be as well unknown so my
[07:18:20] friends this is very critical in the
[07:18:22] analyzes and working with data. So this
[07:18:24] means we have to handle the nulls before
[07:18:26] doing any mathematical operations. And
[07:18:28] this is not only for the plus operator,
[07:18:31] it's as well for the other operators
[07:18:32] like minus and so on. All right. So now
[07:18:34] let's have the following task. And it
[07:18:36] says display the full name of the
[07:18:38] customers in a single field by merging
[07:18:41] their first and last names and add 10
[07:18:43] bonus points for each customer's score.
[07:18:46] So let's go and solve it. We're going to
[07:18:48] select first the basic informations.
[07:18:50] Let's get the customer ID. What do we
[07:18:52] need? the first name, the last name and
[07:18:55] we need the scores. So that's it from
[07:18:58] sales customers. Let's go and execute
[07:19:01] it. Now the first task is that we have
[07:19:03] to generate a new field called full name
[07:19:06] where we have to go and merge or
[07:19:08] concatenate their first and last names.
[07:19:10] So let's go and do that. We need the
[07:19:12] first name plus and then let's have a
[07:19:15] space between the first and last name
[07:19:19] and then plus let's have the last name
[07:19:22] as
[07:19:23] full name. So let's go and execute it.
[07:19:27] Now if you check the result for the
[07:19:29] first customer it is working. So we have
[07:19:31] Joseph Goldenberg. The same thing for
[07:19:33] the second customer. But for the third
[07:19:35] customer we have here a problem.
[07:19:37] Customer doesn't have any last name but
[07:19:40] she has a first name. So we have here a
[07:19:42] Mary. So the full name here is
[07:19:44] completely null which is not correct.
[07:19:46] For this example we have at least to
[07:19:48] show the first name Mary even though
[07:19:50] that the last name is missing. So the
[07:19:52] result is not really accurate and that's
[07:19:54] because we are doing the plus operator
[07:19:56] between a null and marry. So that means
[07:19:59] we have to go and handle the nulls
[07:20:01] before doing any plus operator. So again
[07:20:04] here we can go with the cowless or the
[07:20:06] isnull. So let's go and create a new
[07:20:08] field using the cowless. So it's going
[07:20:11] to be the last name and now we have to
[07:20:14] define a new value. If it's null so we
[07:20:17] could have like something unknown or we
[07:20:19] could have like an empty string and we
[07:20:21] can do that using two quotes and between
[07:20:23] them there is nothing. So we are using
[07:20:25] an empty string. So let's go and check
[07:20:28] the results. Last name two. So let's go
[07:20:33] and execute it. Now we can see that the
[07:20:35] last name over here for marry it has an
[07:20:39] empty string and it is not anymore a
[07:20:41] null. So now SQL knows okay this is a
[07:20:44] string and there is no characters inside
[07:20:46] it. So with that SQL knows more
[07:20:48] informations and we can go and now
[07:20:50] concatenate those informations. So let's
[07:20:52] go and do that. We're going to take the
[07:20:54] whole thing and replace the last name
[07:20:57] with the kowalis. So let me just remove
[07:21:00] this last name over here and execute it.
[07:21:03] So now as you can see things looks
[07:21:04] better. Now we have in the full name for
[07:21:06] mari only the first name. And of course
[07:21:09] if you don't like it like this you would
[07:21:10] like to have another default value. You
[07:21:12] can go over here and say something like
[07:21:14] in a not available. So let's go and
[07:21:16] execute it. And with that you can see
[07:21:18] immediately uh there is here a missing
[07:21:20] last name. But it doesn't really look
[07:21:22] good. So I will just remove it and go
[07:21:24] with the empty string. We're going to go
[07:21:26] and execute it. So with that we have
[07:21:28] solved the first part of the task where
[07:21:30] we have the full names and we are not
[07:21:32] missing any informations from the first
[07:21:34] name and the last name. Now let's go to
[07:21:35] the second part of the task where we
[07:21:37] have to add 10 bonus points for each
[07:21:39] customer score. So we have to go and add
[07:21:42] a 10 for each score. So let's go and do
[07:21:44] it. I'm going to put it at the end. So
[07:21:46] score + 10 and let's give it the name
[07:21:51] score with bonus. So that's it. Let's go
[07:21:54] and execute it. So now in the output you
[07:21:56] can see it's very easy. We have added a
[07:21:58] 10 for each score. So we have increased
[07:22:01] the score points for each customer. But
[07:22:03] now for the last customer Anna you can
[07:22:05] see over here she doesn't have a value
[07:22:07] in the scores and that's why didn't go
[07:22:10] and added 10. So we will get as well a
[07:22:12] null. And of course this might not be
[07:22:14] fair that the last customer is not
[07:22:16] getting any point even though that we
[07:22:17] have increased for all others. So that
[07:22:19] means we have to go and handle the null
[07:22:21] by replacing the null to zero. And only
[07:22:24] after that we're going to add a plus to
[07:22:26] it. So let's go and do that. I'm going
[07:22:28] to add a
[07:22:30] kalis if it is null then go and make it
[07:22:34] zero. And afterward go and add a 10
[07:22:36] points. So let's go and execute it. So
[07:22:39] now as you can see at the results
[07:22:40] everything now is fair where we have a
[07:22:43] 10 bonus points for each customers even
[07:22:45] if the customer doesn't have any values
[07:22:48] in the scores like here Anna she has
[07:22:50] like null but still she is getting a 10
[07:22:52] points. So here again as you can see if
[07:22:54] you don't handle the nulls correctly
[07:22:56] before doing the mathematical operations
[07:22:59] you might get unexpected results. So be
[07:23:01] careful with the nulls and handle them
[07:23:03] correctly before adding anything.
[07:23:09] Okay, moving on to the next use case for
[07:23:11] the kowalis and is null. We can use them
[07:23:13] in order to handle the null before doing
[07:23:16] joins. This is little bit advanced use
[07:23:18] case but it's very important to
[07:23:20] understand it. So let's understand why
[07:23:21] this is important. Let's have for
[07:23:23] example two tables table A and table B.
[07:23:26] And in some scenarios we have to go and
[07:23:28] combine those two tables using the
[07:23:29] joins. And now in order to join two
[07:23:31] tables, we have to go and specify the
[07:23:34] keys between the table A and table B in
[07:23:36] order to join on it. So in this example,
[07:23:38] we have two keys in order to join the
[07:23:40] tables. Now here comes the special case.
[07:23:43] If those keys don't have any nulls
[07:23:45] inside it and all the data are filled,
[07:23:47] then your join going to work perfectly
[07:23:49] and you will get the expected results.
[07:23:51] And now you might have a special case
[07:23:53] where there are nulls inside the keys.
[07:23:55] So there are missing values and this is
[07:23:57] a big problem because in the output you
[07:23:59] will get unexpected results and some
[07:24:01] records will be totally missing. So in
[07:24:04] this scenario we have to handle the
[07:24:05] nulls inside the keys before doing the
[07:24:08] joins. Let's have a very simple example
[07:24:10] in order to understand this behavior.
[07:24:12] All right. So now let's have this very
[07:24:13] simple example where we have two tables
[07:24:15] and we want to combine them. So in the
[07:24:16] first table we have a year type orders
[07:24:19] and in the second table we have as well
[07:24:21] year type and we have sales. So now we
[07:24:23] would like to go and combine those two
[07:24:25] tables in order to have all informations
[07:24:27] in one result. Now we can go of course
[07:24:29] and use the inner join between the table
[07:24:31] one and table two and the keys for the
[07:24:33] joins here. As you can see we have the
[07:24:36] year in both of the tables and as well
[07:24:38] the type. So we're going to go and use
[07:24:40] both of those columns as a key for the
[07:24:42] join. So let's do it step by step how
[07:24:45] going to execute this. So we need the
[07:24:46] year type and the results. So it's going
[07:24:49] to go and take those two columns to the
[07:24:51] results and we need the orders and
[07:24:53] sales. So it's going to take as well the
[07:24:56] orders and the sales from the second
[07:24:58] table. So now let's start doing it row
[07:25:00] by row. So the first key going to be
[07:25:02] those two columns. So we have 2024 and
[07:25:06] the type A. So now it's going to start
[07:25:08] searching for those two informations in
[07:25:10] the second table. And as you can see we
[07:25:12] have here a match, right? So the first
[07:25:15] row is as well matching since it's inner
[07:25:17] join it going to present in the output
[07:25:19] only the matching rows from left and
[07:25:22] right. So in the outputs we're going to
[07:25:24] get the whole row from the table one and
[07:25:27] we will get the sales from the table
[07:25:29] two. All right. So that's all for the
[07:25:31] first row. Now let's move to the second
[07:25:34] row over here. So what are the values of
[07:25:36] the keys? We have 20 24 and null. So now
[07:25:40] if you check the matches on the right
[07:25:42] side you can see we have a match here
[07:25:44] right it is logical so it's as well 20
[07:25:47] 24 and null so everything is matching
[07:25:50] and we should get it in the result right
[07:25:52] SQL cannot go and use the equal operator
[07:25:55] in order to join tables so even though
[07:25:58] that is logically it makes sense to have
[07:26:00] it at the output but still SQL cannot go
[07:26:02] and compare the nulls that's why this is
[07:26:05] a problem for this combination SQL will
[07:26:08] not find any matching So we will not get
[07:26:10] any informations for the combination of
[07:26:12] 2024 and null. So for us of course in
[07:26:15] the business this is missing
[07:26:17] informations and as well inaccurate
[07:26:19] results. So we're going to miss this row
[07:26:22] and it's still going to go and jump to
[07:26:23] the third row. So here what are the
[07:26:25] values of the key. We have 20 25 and B.
[07:26:28] Now it's going to go and search it in
[07:26:30] the second table and it's still going to
[07:26:32] find a match over here. So in the
[07:26:34] outputs we're going to get those values.
[07:26:37] The the orders going to be 50, the sales
[07:26:40] 300. Now it's going to go to the last
[07:26:42] row and we have here again the same
[07:26:44] problem. We have here 2025 and null. And
[07:26:48] of course if you check the data you will
[07:26:49] say yes we have a matching over here but
[07:26:52] SQL would ignore it. So we have exactly
[07:26:54] the same situation and we will not find
[07:26:57] it at the results. So at the output we
[07:26:59] will get only two rows even though that
[07:27:01] those two tables are like identicals if
[07:27:04] you compare the keys. So with that we
[07:27:06] are losing data at the results and we
[07:27:08] are providing inaccurate results. So my
[07:27:10] friends if you have nulls inside your
[07:27:13] keys what can happen you will lose
[07:27:15] records at the output. So here it's very
[07:27:18] important to handle the nulls inside the
[07:27:21] keys before doing the joins. All right
[07:27:24] so now in order to fix it we're going to
[07:27:25] go and use either the kalis or the
[07:27:27] isnull in the join. So as you can see we
[07:27:29] are not using the type directly. We are
[07:27:31] handling it by replacing the null with
[07:27:34] an empty string. It doesn't matter which
[07:27:36] value you are using. The main thing is
[07:27:38] that you have a value and SQL can go and
[07:27:41] map it. So you could have it as empty
[07:27:43] string or a blank or any default value.
[07:27:45] But I usually go with the empty string
[07:27:47] since it's little bit faster than having
[07:27:49] any other characters. So now what going
[07:27:51] to happen is we're going to go
[07:27:52] everywhere and replace those nulls with
[07:27:56] an empty string. So now we don't have
[07:27:58] any nulls inside our keys and let's go
[07:28:01] and see what can happen. So we're going
[07:28:02] to start with the first row again. Here
[07:28:04] we have a matching from the right table
[07:28:07] and we're going to see the whole records
[07:28:10] in the outputs. So we will get as well
[07:28:12] the sales as 100. And now it's going to
[07:28:14] go to the second row over here. So this
[07:28:17] time we don't have a null. We have 2024
[07:28:20] and an empty string. So now it's going
[07:28:22] to go and search for a match and it's
[07:28:24] going to find it over here. we have as
[07:28:25] well 2024 and an empty string. So now
[07:28:28] what can happen in the outputs we're
[07:28:30] going to get a
[07:28:32] 204 but here we will get a null. So we
[07:28:35] will not get an empty string we will get
[07:28:37] a null over here and that's because we
[07:28:40] are handling the null only on the join.
[07:28:42] So as you can see we have here the is
[07:28:44] null type on the join but we don't have
[07:28:46] it on the select. So in the select the
[07:28:49] type going to be like the original data
[07:28:51] and the original data was a null. We are
[07:28:53] just handling the null in the joints
[07:28:55] just in order to let SQL understand how
[07:28:57] to map and match the data. So in this
[07:29:00] example, I'm not changing the values in
[07:29:02] the select. So that's why we will get
[07:29:03] the original value. But the orders we
[07:29:06] will get it 40 and the sales going to be
[07:29:09] 20. Now moving on to the third row. I
[07:29:12] think you already get it. So let's going
[07:29:14] to find the match and the sales going to
[07:29:16] be 300. All right. Now we're going to
[07:29:19] move to the last one. And here we have
[07:29:20] the same scenario. So we have 2025 and
[07:29:23] an empty string. So it's not null
[07:29:25] anymore. And SQL going to go and search
[07:29:27] for all those informations and it's
[07:29:29] going to find it over here. So SQL going
[07:29:31] to take this fields over here in the
[07:29:34] type in null not an empty string because
[07:29:37] in the select we didn't handle it. So
[07:29:39] the order going to be 60 and the sales
[07:29:42] going to be 200. So as you can see now
[07:29:45] the result is complete. We successfully
[07:29:48] combined both of those tables in one big
[07:29:50] results using joins but as well using
[07:29:53] the help of the isnull function in order
[07:29:55] to have a complete results and not miss
[07:29:57] any value. So my friends be very careful
[07:30:00] check always the keys whether they have
[07:30:03] nulls or not and if you find nulls go
[07:30:05] immediately and handle it so you don't
[07:30:07] lose any records in the results and you
[07:30:10] get accurate
[07:30:14] analyzes. All right, moving on to the
[07:30:16] next use case for the isnull. We can use
[07:30:18] it in order to handle the nulls before
[07:30:22] sorting the data. So imagine we have the
[07:30:24] following sales 15 25 and null. Now if
[07:30:27] you go and sort the data by the sales
[07:30:29] ascending from the lowest to the highest
[07:30:32] what can happen? SQL going to show the
[07:30:34] nulls at the start and that is not
[07:30:37] because the null is the lowest value
[07:30:39] because null has no value. But SQL show
[07:30:42] it like this. it's going to place it at
[07:30:43] the start and then below it we're going
[07:30:46] to have the lowest value. So it is the
[07:30:48] 15 and at the end we're going to have
[07:30:49] the 25. Now if you are doing the exact
[07:30:52] opposite where we are sorting the data
[07:30:54] from the highest to the lowest using
[07:30:56] descending. So what going to happen is
[07:30:59] going to sort it like this. We're going
[07:31:00] to have 25 then 15 and the last thing
[07:31:03] that going to appear in the list going
[07:31:05] to be the null. So here SQL is showing
[07:31:07] the nulls at the end and that is again
[07:31:10] not because nulls are the lowest value
[07:31:12] it has no value but SQL do it like this
[07:31:15] show it at the end. So this is how SQL
[07:31:17] deals with the nulls if you are sorting
[07:31:19] the data. So in order to understand this
[07:31:21] use case let's have the following task.
[07:31:23] So the task says sort the customers from
[07:31:26] the lowest to the highest scores with
[07:31:29] nulls appearing last. All right. So
[07:31:31] let's solve it. This going to be very
[07:31:33] interesting one. So we need the customer
[07:31:35] informations. So let's go and select and
[07:31:37] we need the customer ID and the scores
[07:31:41] from sales customers and let's go and
[07:31:45] execute it. So we have a simple list of
[07:31:47] all customers and their scores. But now
[07:31:49] we have to go and sort the data from the
[07:31:51] lowest to the highest. So we're going to
[07:31:53] go and use the order by clause and we
[07:31:55] need the field score. And since it's
[07:31:57] lowest to the highest that means we need
[07:31:59] to have the ascending and in SQL it is a
[07:32:02] default. So we don't have to go and
[07:32:03] mention it. So let's go and execute it.
[07:32:05] So now as you can see in the results it
[07:32:07] start from the lowest to the highest and
[07:32:09] the first part of our task is solved.
[07:32:12] But now of course we have an issue right
[07:32:13] because we have a null and as we learned
[07:32:15] SQL going to put it at the first place
[07:32:18] on the list. But the task says with
[07:32:20] nulls appearing last. So we really don't
[07:32:23] want to see the nulls at the start. We
[07:32:25] don't worry about it. So we would like
[07:32:26] to have it at the end of the list. So
[07:32:28] that means we have to go and handle the
[07:32:30] nulls before sorting the data. And here
[07:32:33] we have two ways to do it. One way that
[07:32:36] is lazy and the other one is more
[07:32:38] professional. So let me show you first
[07:32:39] the lazy way. We're going to go and
[07:32:41] replace the null with a very big number.
[07:32:44] So for example, what we're going to do,
[07:32:46] we're going to go and use the kowalis
[07:32:48] and we're going to say okay score and
[07:32:50] then let's have a lot of number so that
[07:32:53] we have a really big score. I just want
[07:32:55] to select it in order to see the
[07:32:56] results. So as you can see it's a very
[07:32:58] big number here. So if you take this and
[07:33:02] replace the order by with the new score.
[07:33:05] So that's it. Let's go and execute it.
[07:33:07] So now if you check the results we have
[07:33:09] already solved the task. We have listed
[07:33:10] all the customers from the highest to
[07:33:12] the lowest and the nulls are at the end.
[07:33:14] So now the question why do we call this
[07:33:16] lazy or not professional and that's
[07:33:18] because we are defining a static value.
[07:33:21] And of course for this example it is
[07:33:22] working but we don't know later what's
[07:33:24] going to happen. Maybe things change
[07:33:26] where in this course you're going to get
[07:33:27] a higher value than this and then
[07:33:29] sorting the data will make no sense
[07:33:31] since the null going to be like in
[07:33:33] between values. So who knows your value
[07:33:36] might be a real value inside the data.
[07:33:38] Now let me show you the other way which
[07:33:40] is more professional in order to solve
[07:33:42] this task where we don't play with luck
[07:33:44] at all. So let's go and do that. Let me
[07:33:46] just move this little bit here. I'm
[07:33:48] going to go and create a new logic where
[07:33:50] we're going to say case when if the
[07:33:52] score is null then what's going to
[07:33:54] happen we want the value one otherwise
[07:33:58] the value going to be zero so end so we
[07:34:01] are just creating a flag with zero and
[07:34:04] one if the score is null then we're
[07:34:06] going to get the flag of one if we have
[07:34:08] a value for the score we will get zero
[07:34:10] so let's have it like this and I will
[07:34:12] just go and get rid of this kalis so
[07:34:16] let's go and execute it Now if you check
[07:34:18] our new nice flag you can see we have
[07:34:20] zeros everywhere where we have a value
[07:34:22] in the score but only once we have a
[07:34:25] null we will get the flag of one. So now
[07:34:27] once we got this what we're going to do
[07:34:29] we're going to go and sort our data
[07:34:30] based on this flag and the score even
[07:34:33] though the task is not mentioning
[07:34:34] anything about the flag but we are using
[07:34:36] it in order to force the nulls to be at
[07:34:39] the end of the result. Let me show you
[07:34:41] how we're going to do that. So let me
[07:34:42] just remove all this. So first we want
[07:34:45] to sort the data by our new flag in
[07:34:48] order to make sure that the nulls at the
[07:34:50] end. So we're going to have our flag and
[07:34:52] then afterward we sort the data by the
[07:34:55] score. So let's go and have the score.
[07:34:58] So again what we are doing first sort
[07:35:00] the data by the flag in order to push
[07:35:02] the nulls at the end. And now once all
[07:35:04] those values are equal to each others
[07:35:07] what's going to happen SQL going to go
[07:35:08] and sort the data by the score. So SQL
[07:35:10] going to use the scores in order to sort
[07:35:12] the data and both of them are ascending.
[07:35:15] Let's go and execute it. Now as you can
[07:35:17] see we're going to get exactly same
[07:35:18] results. The values from the lowest to
[07:35:21] the highest and the nulls are at the
[07:35:23] end. And as you can see with the order
[07:35:25] by we didn't use any static values or
[07:35:28] any big numbers. And of course we don't
[07:35:30] need the flag at the select. So we can
[07:35:31] go and remove it. So let's execute it.
[07:35:33] And with that we have solved the task.
[07:35:35] So as you can see we can use those nice
[07:35:37] functions like the cowis or the isnull
[07:35:40] in order to handle the nulls before
[07:35:42] sorting your
[07:35:46] data. So what is the function null if
[07:35:49] null if going to go and compare two
[07:35:51] values and it going to returns a null if
[07:35:54] they are equal otherwise if they are not
[07:35:56] equal it going to returns the first
[07:35:59] value. Okay. Okay. So now the syntax of
[07:36:01] the null if it accepts only two values
[07:36:04] value one and value two. So here again
[07:36:06] of course you can go and use a column
[07:36:08] with a static value like the unknown. So
[07:36:10] we are comparing the values between a
[07:36:12] column and a static value or you can go
[07:36:15] and compare two columns the shipping
[07:36:17] address and the billing address. So
[07:36:19] again here it accepts only two values.
[07:36:21] We cannot have it like the kalis where
[07:36:23] we have a list of multiple values. All
[07:36:25] right. So now let's understand exactly
[07:36:27] what do we mean with the null if. So the
[07:36:29] workflow going to be like this. SQL
[07:36:30] going to go and check two values the
[07:36:32] value one and the value two. And if they
[07:36:35] are equal then SQL going to go and
[07:36:37] return a null. But if the two values are
[07:36:39] not equal going to go and return the
[07:36:42] first value. So it is the one on the
[07:36:44] left side. So by checking the outcomes
[07:36:46] here we will never have a scenario where
[07:36:48] we're going to get the second value.
[07:36:50] That means the second value always used
[07:36:52] as a check. So we are checking against
[07:36:55] this value. So either we're going to get
[07:36:56] the value one or a null. Let's have this
[07:36:59] very simple example. We are saying null
[07:37:00] if price and we are checking whether
[07:37:03] it's equal to minus1. So we are saying
[07:37:05] if the price is equal to minus1 then go
[07:37:08] and replace it with a null because it is
[07:37:10] data quality issue that we have a price
[07:37:12] that is negative. It makes no sense for
[07:37:14] our business. And if it is minus1 then
[07:37:16] it means for us a null. We don't know
[07:37:18] the price of this product. So we will
[07:37:20] correct it using the null if. Let's
[07:37:22] check this very simple example. We have
[07:37:24] two orders. So SQL going to start with
[07:37:25] the first order and check the first
[07:37:27] value. So what is the first value? Is
[07:37:29] the price. So here we have a 90. SQL
[07:37:32] going to go and check is 90 equal to
[07:37:34] minus one. Well, no. That means it's
[07:37:36] going to go and execute this path. So
[07:37:39] that means in the output we will get the
[07:37:42] first value which is 90. So in the
[07:37:44] output we will get a 90. Now let's move
[07:37:47] to the second order. Here we have a
[07:37:49] minus one. So SQL going to check is
[07:37:51] minus one here equal to the minus one
[07:37:53] that we have in the null if well yes. So
[07:37:56] that means SQL going to go and execute
[07:37:58] this path where we were going to get the
[07:38:00] null value in the output and we're going
[07:38:02] to get it like this. So now if you
[07:38:04] compare the result from null if and the
[07:38:05] price you can see we don't have any more
[07:38:08] the minus one. And as you can see now we
[07:38:10] are doing exactly the opposite as
[07:38:11] kowalis and is null. We are replacing a
[07:38:13] real value with a null. Now moving on to
[07:38:16] the second example and this is very
[07:38:18] interesting one in the analytics where
[07:38:20] we can go and use two columns inside the
[07:38:22] null if. So in this example we are
[07:38:24] saying null if original price and
[07:38:27] discount price. So SQL have to go and
[07:38:29] compare the prices between those two
[07:38:30] columns and if they are equal it should
[07:38:33] return a null. And now you might say
[07:38:34] okay in this example why we are doing
[07:38:36] this? Well we can use it in order to
[07:38:39] highlight or flag special cases inside
[07:38:41] our data. And the special case here is
[07:38:44] if the original price is equal to the
[07:38:47] discount price and if those two prices
[07:38:49] are equals that means we have an issue
[07:38:51] in our program or something like went
[07:38:53] wrong as we are inserting data. So let's
[07:38:55] see what's going to happen for the first
[07:38:57] row we're going to go and compare the
[07:38:59] 150 from the original price with the
[07:39:01] discount price. So they are not equal
[07:39:04] right. So that means going to go and
[07:39:06] return the original price the 150 in the
[07:39:09] output. So let's move to the second
[07:39:11] order. Here we have the original price
[07:39:13] 250 and as well the discount price is
[07:39:16] 250. So they are equal and if they are
[07:39:18] equal then we will get a null in the
[07:39:21] output. So as you can see again here we
[07:39:23] are not getting any values from the
[07:39:25] discount. We are using it only for a
[07:39:26] check. So with that we have a quick flag
[07:39:28] like using the nulls as flag in order to
[07:39:31] identify where we have equal values. So
[07:39:34] this is how the null if works.
[07:39:39] All right friends, here we have a very
[07:39:41] nice use case for the null if and that
[07:39:43] is preventing the error of dividing by
[07:39:46] zero. Let's see what this means. Okay,
[07:39:48] let's have the following task and it
[07:39:50] says find the sales price for each order
[07:39:53] by dividing the sales by quantity. So
[07:39:56] let's go and solve it. This should be
[07:39:57] very easy. So we need the order ID. We
[07:40:02] need the sales and the
[07:40:05] quantity from sales orders. Let's go and
[07:40:10] execute it. So now we have 10 orders.
[07:40:12] Those are the sales and the quantity. So
[07:40:14] now it's very easy to calculate the
[07:40:16] price. It's going to be the sales
[07:40:18] divided by quantity and we're going to
[07:40:21] call it price. So let's go and execute
[07:40:24] it. Now as you can see we got an error
[07:40:26] says divide by zero error encountered.
[07:40:29] So that means somewhere we have a zero
[07:40:31] for the quantity and this is a problem.
[07:40:34] Let's go and check the data again. So
[07:40:35] I'm just going to comment the whole
[07:40:38] thing and let's go and execute it. So
[07:40:40] now by checking the result yes we got
[07:40:42] for the order ID 10 here we have
[07:40:44] quantity zero. So it will not work if
[07:40:46] you divide by zero of course. So how we
[07:40:48] can solve it? We can use the magic of
[07:40:50] the null if where we're going to go and
[07:40:52] replace the zero with a null. So getting
[07:40:55] a null is way better than getting an
[07:40:58] error. Right? So let's go and do that.
[07:41:00] I'm just going to remove the comments.
[07:41:01] And here we're going to say null if if
[07:41:04] the quantity equal to the zero value. So
[07:41:08] that's it. Let's go and execute it. Now
[07:41:10] as you can see it is working. And with
[07:41:12] that we are making sure that we are not
[07:41:15] dividing by zero. And that's because we
[07:41:17] replace it with a null. And if you
[07:41:19] divide anything by null you will get a
[07:41:21] null. So if you check the result over
[07:41:23] here the order 10 we got the price of
[07:41:25] null which is correct and for the all
[07:41:28] other values everything is working
[07:41:29] because we have values and we didn't
[07:41:31] replace it with a null that's why we
[07:41:33] have values for the price and this is
[07:41:36] very common use case for the null if we
[07:41:38] can use it in order to prevent dividing
[07:41:40] by
[07:41:44] zero. All right so what is is null? It's
[07:41:47] going to return true if the value is
[07:41:50] null. So it is checking the value if
[07:41:52] it's null it's going to return true
[07:41:54] otherwise it's going to returns a false.
[07:41:57] Now the exact opposite if you go use the
[07:41:59] is not null. So if you use these
[07:42:01] keywords it's going to returns a true if
[07:42:04] the value is not null otherwise if it is
[07:42:06] null it's going to go and return a
[07:42:08] false. Okay. So the syntax for that is
[07:42:10] very simple. It start with a value or
[07:42:13] expression and then after that we're
[07:42:15] going to have the keyword is space null
[07:42:17] and the is not is exactly the same. So
[07:42:20] we have a value then afterwards we have
[07:42:22] the is not null. So we have the not
[07:42:25] operator after that and the is not is
[07:42:27] exactly the same. So we have a value
[07:42:29] then we have the is not the not operator
[07:42:32] then the null. So it's very simple.
[07:42:34] Let's have an example. We are checking
[07:42:35] whether the values of the shipping
[07:42:37] address is null. So we can have it like
[07:42:39] this. Shipping address is null or we can
[07:42:42] check the opposite whether it's not
[07:42:43] null. So the shipping address is not
[07:42:45] null. It's very easy. Okay. So now let's
[07:42:47] understand how this works. we are
[07:42:49] checking the value. So if the value is
[07:42:52] null then return a true if it is not
[07:42:55] null then we return a false. So as you
[07:42:57] can see it never returns the value
[07:42:59] itself or any nulls. So we are getting a
[07:43:01] boolean of true and false. So we are
[07:43:04] creating like a boolean flag in order to
[07:43:06] assist us with the checks. So we have
[07:43:08] this very simple example price is null
[07:43:10] and we have those two rows. So we are
[07:43:12] checking whether the price is null in
[07:43:14] the first order it is not null right
[07:43:17] that's why we will get a false in the
[07:43:19] output and the second order the value is
[07:43:22] null so it is correct that's why we will
[07:43:25] get true now of course if we go and use
[07:43:27] the is not null is going to be exact
[07:43:29] opposite so is the price not null well
[07:43:33] yes it's not null that's why you will
[07:43:35] get a true over here so now for the
[07:43:37] second check it is null right so the
[07:43:40] output going to be false we will get the
[07:43:43] exact opposite. So that's it. It's very
[07:43:45] simple how the isnull and is not null
[07:43:50] works. All right. One very obvious use
[07:43:53] case for is null and is not null is by
[07:43:56] searching for missing informations or
[07:43:58] searching for nulls. And maybe after
[07:43:59] that we can go and clean up our data by
[07:44:02] removing the nulls from our data set.
[07:44:04] Let's have the following task and it
[07:44:06] says identify the customers who has no
[07:44:09] scores. All right, let's go and solve
[07:44:11] it. This is very simple. So let's start
[07:44:13] by selecting star from sales customers.
[07:44:17] So we need everything. Let's go and
[07:44:19] execute it. Now as you can see we have
[07:44:21] our five customers. But the task says we
[07:44:23] have to have all the customers who have
[07:44:25] no score. So that means the result
[07:44:27] should return only the last record since
[07:44:30] the score of Anna is null. So let's go
[07:44:33] and have a wear clause. So where and now
[07:44:36] what do we need? We need the score. Then
[07:44:38] we don't use the equal, we use is null
[07:44:42] like this. So that's it. Let's go and
[07:44:44] execute it. And with that, as you can
[07:44:45] see, it's very simple. We have filtered
[07:44:47] our data and now we can see all the
[07:44:49] customers where the score is null. This
[07:44:52] is a very basic check to understand
[07:44:54] whether our data contains nulls. All
[07:44:56] right, moving on to the next task and it
[07:44:58] says show a list of all customers who
[07:45:01] have scores. So back to our example,
[07:45:03] this time we're going to do exactly the
[07:45:05] opposite. We want a list of all
[07:45:06] customers where we have a value in the
[07:45:09] scores. So what we're going to do, we're
[07:45:10] going to say where score is not null. So
[07:45:14] if you go and execute it, you can see
[07:45:16] we're going to get a clean list where
[07:45:18] all the customers have score. And with
[07:45:20] that, we get rid of all nulls inside the
[07:45:22] score field. And maybe this is helpful
[07:45:24] in order to do further analyzes.
[07:45:30] All right friends, now we come to very
[07:45:32] interesting use case for the isnull and
[07:45:34] that is by introducing a new type of
[07:45:37] joints between tables that's going to
[07:45:39] help us to find the unmatching rows
[07:45:42] between two tables. Let's have a quick
[07:45:44] recap about the joints in SQL in order
[07:45:46] to understand the new types. So
[07:45:48] basically we have two sets or let's say
[07:45:50] two tables the left and the right. And
[07:45:53] if you go and use an inner join what we
[07:45:55] are doing here we are finding only the
[07:45:57] matching rows between the left table and
[07:45:59] the right table. So at the result we
[07:46:01] will get only the matching rows. Now we
[07:46:04] have another type of joints called lift
[07:46:06] outer join. And if you use this type at
[07:46:09] the result you will get all the rows
[07:46:11] from the left table and as well only the
[07:46:14] matching rows from the right table. Now
[07:46:16] we have another type which is exactly
[07:46:18] the opposite the right join. And here
[07:46:21] we're going to get all the rows from the
[07:46:22] right table and only the matching
[07:46:25] informations from the left table. And
[07:46:27] now to the last type that we learned. We
[07:46:29] have the full join where we will get all
[07:46:31] the rows from the left and as well all
[07:46:33] the rows from the right. So we will not
[07:46:35] be missing anything. So those are the
[07:46:37] four basic joints that we have learned
[07:46:39] in SQL. But in SQL we have as well other
[07:46:42] types that are more advanced. But we
[07:46:44] don't have in SQL any keywords for that.
[07:46:47] So the first one called lift anti-join.
[07:46:51] So what we are saying here we need all
[07:46:53] the rows from the left table but this
[07:46:55] time without the matching rows. So all
[07:46:59] the informations that are matching with
[07:47:01] the right table we don't want to see it
[07:47:03] at the results. And as I said we don't
[07:47:05] have here an extra keyword for this type
[07:47:07] of join. But in order to get this effect
[07:47:09] we're going to go and combine the left
[07:47:11] join together with the isnull. And with
[07:47:13] that we're going to get all the data
[07:47:15] from the left side but without anything
[07:47:17] that is matching the right side. And
[07:47:19] this we call it left anti- join. And we
[07:47:22] have another advanced type for the
[07:47:24] joints called the right anti- join. This
[07:47:26] is exactly the opposite. So we are
[07:47:28] saying all the rows from the right table
[07:47:31] without having any matching rows from
[07:47:34] the left table. So all the informations
[07:47:36] on the right side that is not matching
[07:47:38] the left side. So again here we don't
[07:47:39] have a keyword for that. We're going to
[07:47:41] go and work with the right join plus and
[07:47:44] is null. So with that, as you can see,
[07:47:46] we have two new types of joins added to
[07:47:48] our four basic joins. Now this might be
[07:47:50] confusing. Let's have the following task
[07:47:52] in order to understand it. Show a list
[07:47:54] of all details for customers who have
[07:47:57] not placed any orders. All right. So
[07:47:59] let's see how we can create the effect
[07:48:01] of the left anti-join. So let's do it
[07:48:03] step by step. We need here two tables.
[07:48:06] We need the customers and as well the
[07:48:08] orders. So since we are focusing on the
[07:48:10] customers, the lift table going to be
[07:48:12] the customers. So let's go and do that.
[07:48:15] We're going to go and say select star
[07:48:18] from sales customers. This is our first
[07:48:22] table. So we are using the alias of C.
[07:48:25] So let's go and execute it. Now as you
[07:48:27] can see we got the list of all
[07:48:28] customers. So that we have all the
[07:48:30] details for our customers. But now we
[07:48:32] have to go and join it with the orders.
[07:48:34] So in order to do that let's have a new
[07:48:37] line. left join
[07:48:40] sales sales orders and let's have the
[07:48:44] lso and now we have to go and define the
[07:48:46] key for the join so on it's going to be
[07:48:49] the customer ID equal the customer ID in
[07:48:53] the order table so now if you go and
[07:48:55] execute it now what we're going to do
[07:48:57] we're going to go and show the order ID
[07:49:00] from the table orders so order ID just
[07:49:03] to see whether we have a match or not so
[07:49:05] let's have it like this and execute it
[07:49:07] Now let's go and check the results. As
[07:49:09] you can see those four columns comes
[07:49:12] from the table customers and only the
[07:49:14] last column come from the orders. So now
[07:49:16] what is interesting is to check the
[07:49:18] order ID whether we have nulls or not.
[07:49:21] So as you can see for the customer one
[07:49:23] we have everything matching. For the
[07:49:25] customer two as well we have orders the
[07:49:27] three as well for only the last one
[07:49:30] customer ID 5 we have here a null. So
[07:49:33] that means SQL was not able to find any
[07:49:37] order for this customer. So again what
[07:49:39] this means we have only one customer
[07:49:41] Anna where she doesn't have any order
[07:49:45] but all other customers they did have an
[07:49:48] order and that's because we have values
[07:49:51] from the right table. So once we have
[07:49:53] values that means we have matching but
[07:49:55] since here we have a null that means we
[07:49:58] don't have any matching. So now since
[07:50:00] the left anti- joint says we would like
[07:50:02] to have all the data from the left table
[07:50:05] without having any matching from the
[07:50:07] right table. So that means for this
[07:50:09] example we would like to get only this
[07:50:11] customer Anna. And this is exactly as
[07:50:14] well fulfilling our task. The task says
[07:50:16] list all details for customers who have
[07:50:19] not placed any order. All data from
[07:50:21] customers where we don't have matching
[07:50:24] from the orders. Now I think you already
[07:50:26] got it how to get this effect. We're
[07:50:28] going to go and filter the data like the
[07:50:30] following. So we're going to have the
[07:50:32] wear clause and now we need the column
[07:50:34] from the right table from the orders. So
[07:50:36] we're going to go with the customer ID
[07:50:39] comes from the orders. So we're going to
[07:50:41] say oh customer ID is null. And of
[07:50:47] course you can go with the order ID as
[07:50:48] well. You're going to get the same
[07:50:49] effect. But I would like always to use
[07:50:51] the key that we are using with the join.
[07:50:54] So let's go and execute it. And now as
[07:50:56] you can see we got the effect of the
[07:50:58] left anti join and with that as you can
[07:51:00] see we got the customer that we are
[07:51:01] aiming for. So here we have the data
[07:51:03] from the left side that is not matching
[07:51:05] the right side. So the customers who
[07:51:07] have not placed an order and with that
[07:51:10] we have solved the task. So as you can
[07:51:11] see we have implemented the left and
[07:51:13] join by combining the left join together
[07:51:18] with the is null. So this is the power
[07:51:20] of playing with the nulls in SQL.
[07:51:26] Now my friends, there is something that
[07:51:28] is really confuses a lot of developers
[07:51:30] or anyone that is working with data in
[07:51:33] databases and SQL and that is the
[07:51:36] differences between nulls, empty string
[07:51:39] and blank spaces. So the nulls as we
[07:51:41] learned we are saying I don't know what
[07:51:44] the value is it is unknown. But now in
[07:51:46] the other hand the empty string you are
[07:51:48] saying I know the value it is nothing.
[07:51:52] So the empty string is a string value
[07:51:55] which has a zero characters. This is
[07:51:57] totally different than the nulls. The
[07:51:59] nulls we don't know anything about it.
[07:52:01] So now sometimes maybe happens to you as
[07:52:03] you are filling a forum and you come to
[07:52:05] one field you go and by mistake hit a
[07:52:08] space bar and with that you are entering
[07:52:10] space into the field and you just jump
[07:52:12] to the next field without entering any
[07:52:14] other values. So we have now like a
[07:52:17] space character inside the field. This
[07:52:19] is really evil in databases because once
[07:52:21] the user enter a blank space, it's going
[07:52:24] to go and store it as a value inside the
[07:52:26] database and it's going to take storage.
[07:52:28] So it could be one space or many spaces
[07:52:30] depends on how long you press the space
[07:52:33] bar. So the blank space is a string but
[07:52:36] the size is not zero like the empty
[07:52:38] string. We're going to have a size of
[07:52:40] how many spaces you have entered. So
[07:52:42] here it's not like the null. We know the
[07:52:44] value it is string and the character of
[07:52:46] that going to be space. Okay. So let's
[07:52:48] see those three scenarios inside scale.
[07:52:50] Now I have like a dummy data using the
[07:52:52] city statements. Don't worry about it.
[07:52:54] I'm going to teach you all those stuff
[07:52:55] in the next tutorials. So now we have
[07:52:57] here like four rows. The first one with
[07:53:00] a value a. The next one with null. The
[07:53:02] third one with empty string. So as you
[07:53:05] can see there is nothing between those
[07:53:07] two quotes. And the last one we have a
[07:53:09] space between those two quotes. Now
[07:53:11] let's go and query this temporal table.
[07:53:14] So select star from orders and execute.
[07:53:19] So now by looking to the values of the
[07:53:21] categories you can find all the
[07:53:23] scenarios now. So now the first scenario
[07:53:25] is the easiest one where we have a
[07:53:27] normal value. We have here an a. But the
[07:53:29] other three rows we don't have normal
[07:53:31] values. We have like empty stuff. So the
[07:53:34] first one going to be the null. So we
[07:53:36] don't have a value. This is the special
[07:53:38] marker from SQL. It says null. So there
[07:53:41] is no value. And the other two they are
[07:53:43] really confusing. As you can see it's
[07:53:45] really hard by just looking to the data
[07:53:47] or to the results whether it is an empty
[07:53:50] string or a blank space. And this confus
[07:53:53] a lot of developers or anyone working
[07:53:54] with data seeing those results. It's
[07:53:56] really hard to detect the data quality
[07:53:58] issues by just looking at the results.
[07:54:01] So now in this scenario what I do I go
[07:54:03] and calculate the length of each value
[07:54:06] inside my column. So let's go and do
[07:54:08] that. Now we're going to go in the SQL
[07:54:11] server. We're going to go and use the
[07:54:12] function data length and our field going
[07:54:15] to be the category. So let's call it
[07:54:18] category length. So let's go execute it.
[07:54:22] And now let's check the result. The
[07:54:23] first one since we have only one
[07:54:25] character, the length of that is going
[07:54:26] to be one which is correct. And now to
[07:54:28] the next row we have the category null.
[07:54:30] We don't know the value and as well we
[07:54:33] don't know the length of the value,
[07:54:34] right? So that's why we will get a null.
[07:54:36] So now by moving to the next one as you
[07:54:38] can see those two looking really exactly
[07:54:40] the same. But now with the help of the
[07:54:43] length or the data length function we
[07:54:46] can see that the third row or the third
[07:54:48] category value has the length of zero.
[07:54:51] That means it is an empty string and we
[07:54:54] don't have any characters over here that
[07:54:56] is hidden. So with that we are sure this
[07:54:58] is an empty string. But now let's move
[07:55:00] to the last one. Here it is very tricky
[07:55:02] and evil. we have a hidden space inside
[07:55:05] this value and we can understand that by
[07:55:08] the length of this field. So as you can
[07:55:10] see we have here a one that means we
[07:55:12] have here one hidden space inside this
[07:55:15] value and it is not empty string. So
[07:55:18] that means I have here only one space
[07:55:20] let's go and give it another space and
[07:55:23] calculate the length. So as you can see
[07:55:24] we have two spaces and that's why the
[07:55:26] length is two. So don't count on your
[07:55:29] eyes in order to understand the spaces.
[07:55:31] go and calculate the length in order to
[07:55:34] be very precise. So now let's go and
[07:55:36] compare the three scenarios side by
[07:55:37] side. So let's start with the first one
[07:55:39] about the representations in the table.
[07:55:41] The null we're going to see it as a null
[07:55:43] inside the table. The empty string going
[07:55:45] to be like two quotes and nothing
[07:55:47] between them. And the blank space it's
[07:55:49] as well two quotes and between them one
[07:55:52] or many spaces. And now if you are
[07:55:54] talking about the meaning the null means
[07:55:56] unknown. We don't know the value. The
[07:55:59] empty string it is known but it is
[07:56:02] nothing it is empty value. And the third
[07:56:04] one blank spaces it is as well known and
[07:56:07] the spaces are the value. And now if you
[07:56:09] are talking about the data types since
[07:56:11] the null is no value. So we don't have a
[07:56:14] data type for this and it is like a
[07:56:16] special marker in the SQL. And now the
[07:56:19] empty string has a data type. It is a
[07:56:21] string and the size of this string going
[07:56:24] to be zero since we have zero characters
[07:56:26] inside the empty string. Moving on to
[07:56:28] the blank spaces, it is a string since a
[07:56:30] space is a character and it's going to
[07:56:32] be the size of one or many. And now if
[07:56:35] we are talking about the storage, the
[07:56:36] null is the best. They don't consume or
[07:56:39] occupy a lot of storage. While the empty
[07:56:41] string and the blank spaces, they occupy
[07:56:43] here storage and memory and they waste
[07:56:46] the space. So if you are worried about
[07:56:48] the storage, the best option here is a
[07:56:50] null. Now talking about the performance,
[07:56:52] you will get the best performance if you
[07:56:55] are using nulls. Now the empty string is
[07:56:57] as well fast but it is not that fast
[07:56:59] like the nulls. Now the worst option
[07:57:01] here is the blank spaces it is slow. So
[07:57:04] again if the speed is important for you
[07:57:06] you have to have those scenarios as a
[07:57:08] null. So now if you are talking about
[07:57:09] the comparison and you are searching for
[07:57:11] those values if you want to search for
[07:57:13] the null you have to go and use is null.
[07:57:16] But in the other hand if you want to
[07:57:18] search for the empty string and the
[07:57:19] blank spaces you have to go and use the
[07:57:21] operator equal. So that's all those are
[07:57:24] the main differences between the null
[07:57:26] empty string and blank spaces.
[07:57:31] Now you might ask you know what why do I
[07:57:34] have to understand the differences
[07:57:35] between all those stuff the nulls empty
[07:57:37] strings and the blanks everything's like
[07:57:39] empty so why do I care well in new
[07:57:42] projects I'm going to promise you that
[07:57:44] you will be working with sources and
[07:57:46] data that has bad data quality and you
[07:57:48] might encounter all those three
[07:57:50] scenarios in your data and now if you
[07:57:52] don't do any data preparations like
[07:57:54] cleaning up the data handling those
[07:57:56] three scenarios and bringing standards
[07:57:58] to your data and you jump immediately to
[07:58:01] the analyzes without doing all those
[07:58:02] stuff, you will end up providing
[07:58:04] inaccurate results in your reports and
[07:58:07] analyzes which leads to wrong decisions.
[07:58:10] So preparing your data before doing any
[07:58:12] analyszis by cleaning up the data,
[07:58:14] handling those three scenarios and as
[07:58:16] well bringing standards is very
[07:58:18] important step before doing any
[07:58:21] analyszis. So this is how we're going to
[07:58:23] do it together with the stakeholders and
[07:58:25] the users of your reports and analyzes.
[07:58:28] You have to define a clear data
[07:58:30] policies. It's like rules and you have
[07:58:33] to commit yourself during the
[07:58:34] implementations by following those
[07:58:36] rules. And here we have three different
[07:58:38] options. The first one you can go and
[07:58:40] define the data policies like this. Only
[07:58:42] use nulls and empty string but avoid
[07:58:45] using blank spaces. In my project I
[07:58:48] cannot imagine that there is a scenario
[07:58:51] where we need blank spaces. They are
[07:58:53] just evil. Just go get rid of them. All
[07:58:55] right. Right. So with this policy, we
[07:58:57] have to go and get rid of all blank
[07:58:59] spaces inside our data. And in order to
[07:59:01] do that, we have a wonderful function in
[07:59:02] SQL called trim. The trim function in
[07:59:05] SQL going to go and remove the spaces
[07:59:07] from a string from the left side and as
[07:59:10] well from the right side. So all the
[07:59:12] leading spaces and the trailing spaces
[07:59:14] going to be removed. So now if we go and
[07:59:16] apply the trim function on that
[07:59:17] category, what's going to happen? All
[07:59:19] the blank spaces going to be removed and
[07:59:21] it going to be turned into empty string.
[07:59:24] So let's go and do that. It's very
[07:59:26] simple. So we're going to use the trim
[07:59:28] function and we're going to apply it on
[07:59:30] the
[07:59:31] category. Let's go and call it policy
[07:59:34] one. So let's go and execute it. So now
[07:59:37] by just comparing the policy one with
[07:59:39] the category. You see like it's
[07:59:41] identical but it's not. Now in order to
[07:59:43] have a better feeling about this we can
[07:59:45] go and test it using the data length.
[07:59:47] Now let's go again and use the data
[07:59:49] length function. So we're going to use
[07:59:52] it for the whole results and as well I'm
[07:59:54] going to go and use it for the category
[07:59:56] in order to just compare it. So without
[07:59:59] the
[08:00:03] trim so like this. Let's go and execute
[08:00:06] it. Now if you go and check the result
[08:00:08] as you can see here again we have the
[08:00:09] length of two because here we have two
[08:00:11] spaces but with the policy one we have
[08:00:14] zero. So those two values after applying
[08:00:17] the trim function they have the length
[08:00:20] of zero and with that we don't have
[08:00:22] blank spaces. So that means now we are
[08:00:24] sure after applying the trim we have
[08:00:27] either a null or empty string. So let me
[08:00:29] just get rid of all those informations.
[08:00:32] Now I am sure both of them are empty
[08:00:34] string. So as you can see it's very
[08:00:36] simple using only one SQL function you
[08:00:39] are cleaning up the data and bringing
[08:00:41] standards. All right moving on to the
[08:00:43] option two. You can define your data
[08:00:45] policies like this. Only use nulls and
[08:00:47] avoid both empty strings and as well
[08:00:50] blank spaces. So that means in our
[08:00:52] business we don't have anything
[08:00:54] meaningful for the empty string and the
[08:00:56] blank spaces. We can go and use only the
[08:00:58] nulls. Okay. So now let's go and
[08:01:00] implement this rule. We have to go and
[08:01:01] convert a value to a null. So the value
[08:01:05] going to be empty string to a null. And
[08:01:07] as we learned we can go and use the null
[08:01:09] if function in order to get nulls
[08:01:11] instead of values. So let's go and apply
[08:01:13] this policy. But now here we have two
[08:01:15] values the empty string and spaces. Now
[08:01:18] instead of having two rules for that I'm
[08:01:21] going to convert first the blank spaces
[08:01:23] to an empty string like we have done
[08:01:26] here. So I'm going to take the result of
[08:01:28] this function first as a first step and
[08:01:31] afterwards we're going to go and use the
[08:01:33] null if. So we're going to say null if
[08:01:37] for the result of the trim if if you
[08:01:40] find any empty strings convert it to
[08:01:43] null. So that's it policy 2. So as you
[08:01:46] can see in the result we have converted
[08:01:48] those empty spaces and planks to a null.
[08:01:51] So with that we are getting three nulls
[08:01:53] and of course we're going to get the
[08:01:54] value a. And now if you compare those
[08:01:56] three columns side by side you're going
[08:01:57] to see the bully C2 is really easier to
[08:02:00] understand compared to the previous
[08:02:02] ones. Right? So now if you compare the
[08:02:04] policy two now to the policy one, you
[08:02:05] can see it's easier to understand and
[08:02:08] it's easier as well to handle. So again
[08:02:10] it's very easy to do data cleanup with
[08:02:12] only two functions we have now like
[08:02:14] standards inside our data. And now
[08:02:16] moving on to the last option, we can
[08:02:18] define our data policy like this. Use
[08:02:20] only a default value unknown and avoid
[08:02:24] using anything else like nulls, empty
[08:02:26] strings and blank spaces. So that means
[08:02:29] in the analyzes and reports we want to
[08:02:31] see the value unknown and we have to
[08:02:33] handle all those three informations and
[08:02:35] convert them to unknown. So now in order
[08:02:38] to implement the policy three we have to
[08:02:39] go and convert a null with a value a
[08:02:42] default value and here we have two
[08:02:44] options either use the is null or we can
[08:02:46] go and use the kalis and I will go with
[08:02:49] the kowalis so
[08:02:51] kowalis and I'm going to use directly
[08:02:54] the category. So if you find any null
[08:02:57] replace it with the default value
[08:02:59] unknown and let's call it policy 3. So
[08:03:02] let's go and execute it. So now if you
[08:03:04] check the result over here you see that
[08:03:06] we got it only once correct. So we
[08:03:08] replaced the null with the unknown but
[08:03:10] we still have like empty spaces and
[08:03:12] blanks and that's because we rushed
[08:03:14] using the qualis and we skipped the
[08:03:16] other steps. So as you can see preparing
[08:03:18] the data you have to do it slowly step
[08:03:20] by step. So first we have to go and
[08:03:22] convert everything to a null like the
[08:03:24] policy 2. And after that the last step
[08:03:27] we're going to go and use the default
[08:03:28] value. So that means instead of using
[08:03:30] the category we have to go and get the
[08:03:32] result of the policy 2. So let's go and
[08:03:35] copy it and replace the category with
[08:03:37] those two steps and let's go and execute
[08:03:40] it. So now as you can see we have the
[08:03:42] default value for all those three
[08:03:44] scenarios. First we have to trim the
[08:03:46] data in order to remove all the blank
[08:03:48] spaces. The second step, we're going to
[08:03:50] go and replace all the empty strings
[08:03:53] with a null. And with that, we're going
[08:03:55] to get a null for all those three
[08:03:57] scenarios. And finally, we're going to
[08:03:59] go and replace the nulls with a default
[08:04:02] value, the unknown. So, that's it for
[08:04:04] the three policies. And this is the
[08:04:06] different ways in order to clean up the
[08:04:08] data and bring standards before doing
[08:04:10] analyszis. And now you might ask me,
[08:04:12] okay, which one should I use in my
[08:04:13] project? Like if I want to suggest
[08:04:15] something for the users, which one
[08:04:17] should I use? Well, it really depends on
[08:04:19] the business, but I tried always to
[08:04:21] avoid this one, the policy one, because
[08:04:23] it's always confusing and I have always
[08:04:25] explained for the users. So now we are
[08:04:27] left with the two and three. Well, I use
[08:04:29] both of them in different scenarios. I
[08:04:31] normally go with the policy 2 because it
[08:04:34] takes less storage and as well the
[08:04:36] performance of your queries afterward
[08:04:38] going to be really good. So if I'm doing
[08:04:40] data preparations in my ETL before
[08:04:42] inserting it inside a table, I go with
[08:04:45] the policy too. But in other hand, if
[08:04:47] I'm doing a preparation step before
[08:04:50] showing it in a report like in Tableau
[08:04:52] or PowerBI. So if it is like one of the
[08:04:54] last steps before showing the data to
[08:04:57] the users, I go with the policy 3
[08:04:59] because if you present a null inside a
[08:05:01] report, it's going to be really hard to
[08:05:03] read. So having like a word like
[08:05:05] unknown, it's easier to understand.
[08:05:07] Okay, we have here missing data. So
[08:05:09] again if the data preparations is
[08:05:11] exactly before I present the data to the
[08:05:13] users I go with the policy 3 where I use
[08:05:16] default values but if I'm using a data
[08:05:19] preparations before inserting it in the
[08:05:21] database I go with the policy 2 because
[08:05:24] it's going to optimize the storage and
[08:05:26] it's going to be really bad if you go
[08:05:28] with the policy 3 because it's really
[08:05:30] bad to store the whole world each time
[08:05:32] there is no value like the unknown. it's
[08:05:34] gonna take a lot of space and as well
[08:05:36] you're going to get bad performance as
[08:05:37] you are building the queries. That's why
[08:05:39] I tend to store the data using nulls. If
[08:05:42] you present it to the users go and show
[08:05:44] it as a default value. So as you can see
[08:05:46] it's very important to understand the
[08:05:47] differences between the nulls empty
[08:05:49] strings and blanks and how to prepare
[08:05:51] the data by cleaning up the data and
[08:05:53] bringing standards and policies before
[08:05:55] doing any analyszis. So with this we
[08:05:57] have cleared up the confusion between
[08:05:59] those scenarios and if you encounter it
[08:06:01] in your projects you know how to deal
[08:06:03] with
[08:06:06] it. All right. So now let's have quick
[08:06:09] summary about the nulls. Nulls are
[08:06:11] special markers in SQL in order to say
[08:06:14] there is no value. It is missing. It is
[08:06:16] unknown. So nulls are not equal to zero
[08:06:19] or empty string or any blank spaces. And
[08:06:22] using nulls inside our database is going
[08:06:24] to save some storage and as well provide
[08:06:27] a strong performance in your queries.
[08:06:29] And in scale we have different functions
[08:06:30] in order to handle the nulls. So now if
[08:06:32] you want to replace a null with the
[08:06:34] value we can go either with the function
[08:06:36] kowalis or is null or if you want to do
[08:06:39] the opposite where you want to replace a
[08:06:41] value with null you can go use the
[08:06:43] function null if or in other cases we
[08:06:45] want only to check whether there is
[08:06:47] nulls or not we can use the is null or
[08:06:50] is not null. And we have learned as well
[08:06:52] that we have to treat the nulls
[08:06:54] especially before doing any tasks. So
[08:06:56] that means we have to handle the nulls
[08:06:58] before doing for example data
[08:07:00] aggregations like average, sum, max, min
[08:07:03] and so on. And we have to handle the
[08:07:05] nulls as well before doing any
[08:07:06] mathematical operations like using the
[08:07:08] plus operator to concatenate two
[08:07:11] strings. And in some scenarios as we
[08:07:13] learned we have to handle the nulls as
[08:07:14] well before doing joins. And in other
[08:07:17] cases we have as well to handle the
[08:07:18] nulls before sorting the data. And we
[08:07:21] have learned as well by combining the
[08:07:23] joins and the isnull we introduce new
[08:07:26] types of joins like as we learned the
[08:07:28] left anti- join and the right anti-join
[08:07:31] where we exclude the matching rows using
[08:07:33] the isnull and we can use the null
[08:07:35] functions in order to provide standards
[08:07:38] and data policies in our data like using
[08:07:41] the nulls or using a default values like
[08:07:43] the unknown. All right my friends. So
[08:07:45] with that you have learned how to handle
[08:07:46] the nulls inside your data and now we're
[08:07:48] going to move to a very special topic
[08:07:50] called the case statements. This is very
[08:07:52] important tool in order to do data
[08:07:54] transformations. So let's
[08:07:59] go case statements. It can allow you to
[08:08:02] build a conditional logic in your SQL
[08:08:04] query by evaluating a list of conditions
[08:08:07] one by one and return a value when the
[08:08:10] first condition is met. So now let's
[08:08:12] understand the syntax of the case
[08:08:13] statements and what this
[08:08:18] means. Okay. So now let's see the syntax
[08:08:20] step by step. It start with the keyword
[08:08:23] case. This case indicates now we are
[08:08:25] starting a logic a conditional logic in
[08:08:27] SQL. It's like programming languages as
[08:08:29] you start with the if else. So the if is
[08:08:32] the keyword of a logic and the whole
[08:08:34] logic as well ends with another keyword
[08:08:36] called end. So once SQL sees the end. So
[08:08:39] this is the end of the conditional
[08:08:41] logic. So the case is the start and the
[08:08:43] end is the end. So now what we're going
[08:08:45] to have in between is the conditional
[08:08:47] logics right. So the conditional logic
[08:08:49] start with the keyword when. Now we are
[08:08:51] telling SQL we have a condition to be
[08:08:53] evaluated and then we're going to go and
[08:08:55] specify that conditional logic. So now
[08:08:58] we have to tell SQL what can happen if
[08:09:00] this condition is fulfilled. So now we
[08:09:03] have to use another keyword called then.
[08:09:05] So now we are telling SQL show this
[08:09:07] results if the condition is true. So as
[08:09:10] you can see it's very simple. It's like
[08:09:11] the natural language, right? It's like
[08:09:13] in English when the condition one is met
[08:09:16] then show the results. It's very logic,
[08:09:18] right? And now of course we can go and
[08:09:19] add a second condition inside our case
[08:09:22] statements. So we're going to have the
[08:09:23] same setup. When condition two if this
[08:09:26] is true then show the result number two.
[08:09:28] We specify the keyword when then we have
[08:09:30] a second condition. And if this
[08:09:32] condition is true then we tell SQL to
[08:09:35] show another results. And of course it's
[08:09:37] very important to understand in the
[08:09:38] syntax of that SQL going to go and
[08:09:40] process the conditions from the top to
[08:09:42] the bottom. So the first most important
[08:09:45] condition should be at the start. So SQL
[08:09:47] going to first check this condition. If
[08:09:50] it fails and it's not true then it going
[08:09:52] to go and jump to the second condition.
[08:09:54] So the order of the conditions is very
[08:09:56] important in your logic. And now of
[08:09:58] course we can go and add multiple
[08:09:59] conditions depend on the logic using the
[08:10:02] keyword when. And now once we are done
[08:10:04] defining all the conditions we can go
[08:10:06] and specify an else keyword. The else
[08:10:09] can introduce the default value and it
[08:10:11] is optional. You can go and skip it. So
[08:10:13] the value of the else or the default
[08:10:15] going to be used only if all the
[08:10:18] condition failed. So that means all our
[08:10:20] conditions are not true and nothing is
[08:10:22] fulfilled then SQL going to go and use
[08:10:24] the value from the else. So it is the
[08:10:27] default value that's going to be used if
[08:10:29] all conditions are false. So those are
[08:10:31] the keywords that you must use inside
[08:10:33] each case statement. So we have case
[08:10:35] when then and only the else is an
[08:10:39] optional. So you can go and use it or
[08:10:41] skip it. So this is the main structure
[08:10:43] and the syntax of each case
[08:10:48] statement. Now let's have a very simple
[08:10:50] example in order to understand how SQL
[08:10:53] execute the case statements behind the
[08:10:55] scenes. All right, let's have this very
[08:10:56] simple example where we have only one
[08:10:58] condition. So as you can see in the
[08:11:00] syntax, it starts with case and end and
[08:11:03] then we have only one condition and we
[08:11:05] are evaluating here the sales. So the
[08:11:07] condition says if the sales is higher
[08:11:10] than 50 then show as a result the value
[08:11:13] of high. So it's very simple only one
[08:11:15] condition and on the right side we have
[08:11:16] here a flowchart in order to understand
[08:11:18] how the logic is executed. And now what
[08:11:20] we're going to do, we're going to go and
[08:11:21] evaluate those four sales through this
[08:11:24] logic and see what the output going to
[08:11:26] be with the case statement. So let's do
[08:11:28] it one by one. Let's start with the
[08:11:30] first sales. It is 60. So here we're
[08:11:32] going to go and check is 60 higher than
[08:11:35] 50. Well, yes. That means this sales is
[08:11:38] meeting this condition and we will get
[08:11:41] true and we're going to get in the
[08:11:42] output the value of high. So here we're
[08:11:45] going to get the value high in the
[08:11:47] output. So that means the first sales is
[08:11:50] fulfilling the requirement the condition
[08:11:52] and SQL going to give us the value from
[08:11:54] this condition. All right. So now SQL
[08:11:55] going to go to the next value and we're
[08:11:57] going to start evaluating the 30. Now
[08:11:59] we're going to ask the same question the
[08:12:00] same condition is 30 higher than 50.
[08:12:03] Well no. So that means in the output for
[08:12:06] this condition we will get false. So we
[08:12:09] will take the path of the false. Now if
[08:12:11] you take the path of the false we will
[08:12:13] not get any value. Right? So that means
[08:12:15] the output going to be a null. So the
[08:12:18] output for the 30 is null. And that's
[08:12:21] because we didn't define in our logic
[08:12:23] anything about the default option. So we
[08:12:25] don't have here an else. And this is
[08:12:27] what going to happen. If you don't use
[08:12:28] else, you will get a null in the output
[08:12:31] for the case statement. So now let's
[08:12:32] move to the next one. It's going to be
[08:12:34] the same thing. So 15 is smaller than
[08:12:37] 50. So it's not fulfilling the
[08:12:39] condition. And as well we're going to
[08:12:41] get a null. And for the last one since
[08:12:43] it's null we will get as well a null
[08:12:45] since it will not fulfill the condition.
[08:12:47] So now after evaluating all those sales
[08:12:49] only the first sales is fulfilling that
[08:12:52] condition and that's why we have only
[08:12:54] one value the high. All right. So now
[08:12:55] let's keep moving and adding stuff to
[08:12:57] our case statements. Now we are adding a
[08:12:59] second condition. So it says after
[08:13:02] checking the sales whether it's higher
[08:13:03] than 50 and it fails check again the
[08:13:06] sales whether it's higher than 20. If
[08:13:08] yes then show the value of medium. So
[08:13:11] now in our workflow we are adding a
[08:13:13] second condition to be checked if the
[08:13:16] first one is false. So now let's go and
[08:13:18] evaluate our sales again and check the
[08:13:20] output the first one the 60. So as you
[08:13:22] can see the 60 is higher than 50. So we
[08:13:25] are fulfilling the first requirement
[08:13:27] that's why we will get the value of
[08:13:29] high. So it's same like before. So here
[08:13:31] we're going to get high in the output.
[08:13:33] And now here very important to
[08:13:35] understand one thing is that SQL didn't
[08:13:37] evaluate here in this scenario the
[08:13:40] second condition. So SQL didn't waste
[08:13:42] any time by checking the other
[08:13:43] condition. It skipped everything once it
[08:13:45] get a true from one condition. So this
[08:13:48] is exactly how SQL process the case win.
[08:13:50] It going to check each conditions from
[08:13:52] top to down and once it finds a true
[08:13:56] it's going to stop everything
[08:13:57] immediately and throw the value from
[08:13:58] this condition and it will not evaluate
[08:14:01] any other conditions. So now it's going
[08:14:03] to go and jump to the next value. We are
[08:14:05] at the value of 30. So let's evaluate
[08:14:07] the conditions. Is 30 higher than 50?
[08:14:10] Well, it's not. So it's false. So now
[08:14:12] what can happen is going to go and jump
[08:14:14] to the next condition and start
[08:14:15] evaluating the second one whether it's
[08:14:17] true or false. So now we're going to
[08:14:19] check here. Is 30 higher than 20? Well,
[08:14:21] yes. So it's going to be fulfilled and
[08:14:24] we will get the value of medium. So it's
[08:14:27] going to stop everything and show in the
[08:14:28] output for this value the medium. So
[08:14:32] we're going to get medium here. So in
[08:14:34] this scenario, we have evaluated both of
[08:14:36] the conditions that we have in the case
[08:14:38] statement. Now it's going to go to the
[08:14:40] third one. We have 15. Is 15 higher than
[08:14:42] 50? Will no. So we will get to false for
[08:14:45] the first condition. Then it's going to
[08:14:47] go and jump to the second condition and
[08:14:48] check it. Is 15 higher than 20? Will as
[08:14:52] well no. So now what going to happen?
[08:14:54] The false going to be activated over
[08:14:55] here. And we will not get any value as a
[08:14:58] return. So we will get the value of null
[08:15:01] in the output. And now for the last one
[08:15:03] we have null. We will get as well null
[08:15:05] because it will not fulfill any of those
[08:15:08] conditions and that's because we didn't
[08:15:10] define an else in the case statement. So
[08:15:12] if we define these conditions like this,
[08:15:14] we will get the category medium for the
[08:15:17] 30. And this is how SQL evaluate
[08:15:19] multiple conditions in the case
[08:15:21] statement. All right. Now we're going to
[08:15:22] go to the final form of our case
[08:15:24] statements and we're going to go and add
[08:15:26] an else. So we're going to have a
[08:15:28] default value. So we are seeing here if
[08:15:29] the sales is not higher than 50 or
[08:15:31] higher than 20 then show a default value
[08:15:34] as low. So that means any sales that is
[08:15:37] equal or smaller than 20 going to get
[08:15:39] the value of low. And now very
[08:15:41] interesting if you check the workflow
[08:15:42] over here you can see that we have now a
[08:15:45] value for each path. So for the first
[08:15:47] condition we're going to get high for
[08:15:48] the second one medium. And if nothing is
[08:15:51] fulfilled we're going to get always the
[08:15:53] value of low. So there is no way in this
[08:15:55] chart to get any nulls. Right? So let's
[08:15:58] go and evaluate again our values. I
[08:16:00] think you already get it. The 60 is
[08:16:02] fulfilling the first requirement and SQL
[08:16:04] going to stop everything immediately and
[08:16:06] just show the value of high. So on the
[08:16:09] right side over here nothing going to be
[08:16:10] evaluated because the first condition is
[08:16:13] true. So here in the output we're going
[08:16:15] to get the value of high. So nothing
[08:16:17] changed like the two previous examples.
[08:16:19] Now it's going to go to the next value.
[08:16:21] We have the 30. So we're going to
[08:16:23] evaluate the first one. It's going to be
[08:16:24] false. The next one it's higher than 20.
[08:16:28] It is true. And that's why is still
[08:16:30] going to show the value of medium. And
[08:16:32] this is as well. We had it in the
[08:16:34] previous example. So medium. So now
[08:16:37] scale going to move to the next one. And
[08:16:38] here things going to get interesting. So
[08:16:40] the value of 15. We're going to evaluate
[08:16:42] the first condition. Is it higher than
[08:16:44] 50? Well, no. Is it higher than 20?
[08:16:46] Well, no. So now we are in scenario
[08:16:48] where none of those conditions are true.
[08:16:51] So that's why SQL going to go and
[08:16:53] execute the else. So if you check our
[08:16:56] chart it's going to be false and we will
[08:16:58] get the value of low. So in the output
[08:17:01] we will not get this time a null because
[08:17:04] we have else we will get the value of
[08:17:06] low. The same thing now for the null.
[08:17:08] Null will not fulfill the first
[08:17:10] condition as well the second condition
[08:17:12] and that's why we will get as well the
[08:17:15] value from the else. So here in the
[08:17:17] output we will get as well the value of
[08:17:19] low. So now as you can see if you use an
[08:17:21] else inside the case statements you will
[08:17:24] make sure that there will be no nulls in
[08:17:26] the output. So that you have learned the
[08:17:28] different options that we have inside
[08:17:30] the case statements and how skill
[08:17:32] execute the case behind the
[08:17:37] scenes. All right friends so now we come
[08:17:40] to the part where I'm going to show you
[08:17:41] the most useful use cases of the case
[08:17:44] statements that I usually use in my
[08:17:46] projects. So let's start. The main
[08:17:48] purpose of the case statement is to do
[08:17:50] data transformations. And data
[08:17:52] transformations is very important
[08:17:53] process in each data project. And one
[08:17:56] very important task in data
[08:17:57] transformations is that we can generate
[08:17:59] new informations. We can go and create
[08:18:01] new columns based on the existing data
[08:18:04] that we have in the database using the
[08:18:06] case statements and this of course can
[08:18:08] help us deriving new informations for
[08:18:10] our analyzes without modifying the
[08:18:12] source database only for analytics. So
[08:18:15] my friends, the main purpose of the case
[08:18:17] statement is to do data transformations
[08:18:20] by creating and generating new columns.
[08:18:22] So now let's start with the first use
[08:18:24] case and the most important and famous
[08:18:26] one is we use case statement in order to
[08:18:29] categorize the data. This means we are
[08:18:31] going to group up the data into
[08:18:33] different categories based on certain
[08:18:35] conditions. And now you might ask why
[08:18:36] this use case is important. Well,
[08:18:39] classifying and grouping data is
[08:18:41] fundamental in data analysis and
[08:18:43] reporting because it makes the data
[08:18:45] easier to understand and as well to
[08:18:47] track. But what's more important, it
[08:18:49] going to help us aggregating the data
[08:18:51] based on the categories. All right. So
[08:18:53] now let's have the following task. And
[08:18:54] it says generate a report showing total
[08:18:58] sales for each of the following
[08:18:59] categories. category high if the sales
[08:19:01] is over 50. Category medium if the sales
[08:19:04] is between 20 and 50 and low if the
[08:19:07] sales is 20 or less and sort the
[08:19:10] categories from the highest sales to the
[08:19:12] lowest. Okay, so let's do it step by
[08:19:14] step. And now before we do any data
[08:19:15] aggregations, we have to go and create a
[08:19:17] new column called categories because we
[08:19:19] don't have it in the database. So now
[08:19:21] let's start with very simple select
[08:19:23] statements. So select what do we need?
[08:19:25] Let's take the order ID, the sales and
[08:19:28] that's it for now. So from sales orders
[08:19:32] let's go and execute it. And now we have
[08:19:34] our 10 orders and we have to go and now
[08:19:36] create a new column called categories.
[08:19:39] And we're going to do that using the
[08:19:40] case statements. So let's take a new
[08:19:43] line and we start with case and then
[08:19:45] again a new line in order to define the
[08:19:47] first condition using the when. So the
[08:19:49] first condition is the high where sales
[08:19:52] is over 50. So it's very simple. So when
[08:19:55] the sales is higher than 50, what can
[08:19:58] happen if this is true? We want to show
[08:20:00] the value high. So this is the first
[08:20:03] condition. And then let's move to the
[08:20:05] second one. If the sales is higher than
[08:20:09] 20, that means it's less than 50 and
[08:20:12] higher than 20, then we want to see the
[08:20:15] value medium. And now for the last
[08:20:17] category, the low, we don't have to go
[08:20:19] and create a condition for that because
[08:20:21] if those two fails, then that means that
[08:20:23] the sales either equal to 20 or less. So
[08:20:25] what we're going to do, we're going to
[08:20:26] just do a simple else and show the value
[08:20:29] low like this. Let me make this a little
[08:20:32] bit smaller. Now what is missing in our
[08:20:35] case is of course the end. Without it,
[08:20:37] you're going to get an error. So end and
[08:20:39] let's give it a name
[08:20:40] category. So we are ready. Let's go and
[08:20:43] execute it. So now let's check randomly
[08:20:45] stuff. So as you can see here we have
[08:20:46] the sales of 50 it is low which is
[08:20:49] correct and then we have here 60 it's
[08:20:51] above 50 and we have the category high
[08:20:53] and now if you check the order number
[08:20:54] six we have the order 50 it's medium
[08:20:57] because it is not higher than 50 it is
[08:21:00] between 50 and 20. So now as you can see
[08:21:02] we have now classified our orders using
[08:21:05] the category. Now the next step that
[08:21:07] we're going to go and aggregate the
[08:21:08] data. So how we going to do that? We
[08:21:10] will use a subquery. So let's do it like
[08:21:13] this. So we're going to go and select
[08:21:14] and of course we're going to group up
[08:21:15] the data by the category. So we're going
[08:21:17] to go and select that category and we
[08:21:20] need the total sales. That means we're
[08:21:22] going to go and use the function sum for
[08:21:24] the sales and we're going to call it
[08:21:26] total sales. So now we have to nest the
[08:21:29] queries together. So from this is our
[08:21:33] query like this and then we have to
[08:21:36] close it and group by. So we are
[08:21:39] grouping by the category. Okay. So with
[08:21:42] that we are now aggregating the sales by
[08:21:45] that category. It's very simple. Let's
[08:21:47] go and execute it. So now in the result
[08:21:49] we have only three categories. We don't
[08:21:50] have the 10 orders because now we are
[08:21:52] doing data aggregations. So now the
[08:21:54] granularity now on the level of
[08:21:56] category. So now we can see the total
[08:21:58] sales for the high is 2010. The low we
[08:22:01] have 65 and the medium we have 105. And
[08:22:04] of course we are not done yet because in
[08:22:05] the task it says sort the categories
[08:22:08] from the highest sales to the lowest.
[08:22:10] That means we have to go and use an
[08:22:12] order by statement at the end and we're
[08:22:14] going to sort the data by the sales from
[08:22:16] the highest to the lowest. That means
[08:22:18] descending. So that's it. Let's go and
[08:22:20] execute it. And now with that we have
[08:22:22] our reports. Now we are showing the
[08:22:24] total sales by the categories and the
[08:22:25] data is sorted from the highest to the
[08:22:27] lowest. So the highest category is high
[08:22:30] then medium and then the last one is
[08:22:32] low. So my friends as you can see with
[08:22:34] the help of the case win we have created
[08:22:36] the new informations from our data we
[08:22:38] have the category and then we have
[08:22:39] created insights or report based on this
[08:22:42] new informations where we have
[08:22:44] aggregated our data using this new
[08:22:46] information. So the use case of
[08:22:48] categorizing data using case statements
[08:22:49] is fundamental and very important in
[08:22:52] each data
[08:22:56] project. Okay. Okay. So now one more
[08:22:58] thing before we jump to the next use
[08:22:59] case is that there is one rule to follow
[08:23:01] if you are using case statements and
[08:23:03] that is the data types of the result
[08:23:06] must be matching. So what this means if
[08:23:08] we check again our example over here we
[08:23:10] can see that the result of each
[08:23:12] condition is a string. So as you can see
[08:23:14] we have here high, medium and low and
[08:23:16] all of those informations are following
[08:23:18] the same data type. So it is correct. So
[08:23:20] now if I go and break this rule for
[08:23:22] example after this then let's have the
[08:23:25] value two. So now we have a number and
[08:23:27] we have characters. So let's go and
[08:23:29] execute it. And now of course we're
[08:23:30] going to get an error because now SSQL
[08:23:32] is trying to convert the value low to an
[08:23:35] integer which is incorrect. So the data
[08:23:37] types of the output of the result must
[08:23:40] be matching and that's not only include
[08:23:42] the value after the then but also the
[08:23:45] value after the else because this value
[08:23:48] is as well part of the output. So let's
[08:23:50] have here again medium. And now let's go
[08:23:52] and change this to let's say one. So
[08:23:55] let's go and excuse it again. Isl going
[08:23:57] to throw an error because this is an
[08:23:59] integer in number and the others are
[08:24:01] string characters. So this is the rule
[08:24:03] of using the case statement. The data
[08:24:05] types after then and after else must be
[08:24:07] matching. And if you ask me whether
[08:24:09] there is restriction about where you can
[08:24:11] use the case statement in which clauses
[08:24:13] you can use it everywhere in select, in
[08:24:15] joins, from, where, group by, order by,
[08:24:19] everywhere. So there are no restrictions
[08:24:21] and we have only this one rule.
[08:24:27] Okay friends, another use case for the
[08:24:28] case statement. We can use it in order
[08:24:30] to map values. So we can use the case
[08:24:33] statement in order to transform the data
[08:24:35] from one form to another in order to
[08:24:37] make it more readable and more usable
[08:24:39] for analytics. One scenario of mapping
[08:24:41] values is that sometimes the database
[08:24:43] developers stores the data and values
[08:24:46] inside the database as codes and as
[08:24:48] flags. So for example, the status of the
[08:24:50] order could be stored as one and zero
[08:24:53] instead of having inactive and active.
[08:24:56] And this is one technique in order to
[08:24:57] optimize the performance of the database
[08:24:59] for the application because one and zero
[08:25:01] is way faster than storing the whole
[08:25:04] string. But in data analyzes, we usually
[08:25:06] generate a report to be read by human by
[08:25:08] persons. And now instead of showing the
[08:25:11] data as zero and one, it's going to be
[08:25:12] more nicer and readable if you show the
[08:25:15] data as active and inactive. So for
[08:25:17] these scenarios, we're going to go and
[08:25:18] use the case statement in order to
[08:25:20] translate those cryptical and technical
[08:25:23] values into readable terms. Otherwise,
[08:25:26] each one going to consume your report.
[08:25:27] Going to ask you what do you mean with
[08:25:29] the zero and one. Let's have the
[08:25:30] following task and it says retrieve
[08:25:32] employee details with gender displayed
[08:25:35] as full text. Okay. So now let's go and
[08:25:37] solve it. First we're going to go and
[08:25:38] explore few informations. So let's go
[08:25:41] and show the employee
[08:25:42] ID and let's take the first name, last
[08:25:46] name and we need the gender
[08:25:48] informations. So gender from sales
[08:25:52] employees. So that's it. Let's go and
[08:25:55] execute it. So now as you can see in the
[08:25:57] result we got our five employees and now
[08:25:59] the gender informations are stored as
[08:26:01] only one character F and M. And of
[08:26:04] course it's easy to understand that the
[08:26:05] F is female and M is male. but we would
[08:26:08] like to show it in the report as a full
[08:26:10] text. So, female and male instead of
[08:26:13] those abbreviations. So, in order to do
[08:26:15] that, we're going to go and use the case
[08:26:17] statement in order to do the mapping
[08:26:19] between the old value and the new value.
[08:26:21] So, let's go and create a new column
[08:26:23] using the case. So, we're going to have
[08:26:25] here two conditions because we have two
[08:26:26] values. Let's start with the first one.
[08:26:28] So, we're going to have a new line and
[08:26:30] when. So when the gender equals to f
[08:26:34] ladies first then female and now for the
[08:26:38] second value it's going to be exactly
[08:26:39] the same when gender equal to m then
[08:26:44] we're going to have male be careful for
[08:26:47] the case sensitivity of the values. So
[08:26:49] of course we will not end this without
[08:26:51] an else. So else then we can have the
[08:26:53] default value. We're going to have the
[08:26:55] default value not available. It's better
[08:26:57] than having nulls. So what we are
[08:27:00] missing is the end. So we're going to
[08:27:02] have an end over here and we're going to
[08:27:04] call you gender full text. So that's it.
[08:27:08] Let's go and execute it. Now if you
[08:27:10] check the results, we have now done the
[08:27:12] mapping between the old format of the
[08:27:14] value with the new format. So instead of
[08:27:16] m we have males and females. And of
[08:27:18] course we don't have here any nulls.
[08:27:20] That's why we don't have a not available
[08:27:22] in the data. But if you have huge data
[08:27:24] of course you can have somewhere a null
[08:27:26] and then you will get this default
[08:27:28] value. So this is how you can do mapping
[08:27:30] between values very easily using the
[08:27:32] case statements. Okay let's have another
[08:27:34] task for the mapping use case and the
[08:27:36] task says retrieve employee details with
[08:27:39] abbreviated country code. Sometimes as
[08:27:41] we are generating reports maybe using
[08:27:42] PowerBI or Tableau we don't have enough
[08:27:45] spaces in order to use the full name of
[08:27:48] values. So what do we need? We need
[08:27:50] abbreviations. we need short form of the
[08:27:52] values and we can go and use in SQL the
[08:27:55] case statement in order to map the full
[08:27:57] value to an abbreviated value. So it's
[08:28:00] like the previous example but the way
[08:28:02] around. All right. So now let's go and
[08:28:03] solve it. We're going to go and select
[08:28:04] few details like the customer ID. Let's
[08:28:07] take the first name, last name and what
[08:28:10] do we need? We need the country
[08:28:12] information from sales customers. So
[08:28:15] that's it. Let's go and execute it. And
[08:28:18] now as you can see we get our five
[08:28:19] customers and we have the country
[08:28:21] informations as a full name. Now of
[08:28:23] course for the report we need
[08:28:24] abbreviated values from this. So we're
[08:28:26] going to go and map those full names of
[08:28:28] the countries to a short form. But in
[08:28:30] real project you might get big tables
[08:28:32] where you have thousands and millions of
[08:28:34] records. So you cannot just check it
[08:28:36] like this. So how I usually do it I go
[08:28:38] and retrieve a distinct list of all
[08:28:40] values from one column. So I usually go
[08:28:42] and have a separate query for that. So
[08:28:45] we're going to have select distinct
[08:28:48] country from the table sales customers.
[08:28:51] It's just for me to see all the possible
[08:28:53] values inside the database. So now you
[08:28:55] see the second result over here. We have
[08:28:56] only two values Germany and USA. And
[08:28:58] then I can go and map the data
[08:29:00] correctly. So always if you are mapping
[08:29:02] data using the case win you have to
[08:29:04] understand all the possible values that
[08:29:06] you have inside the table. So let's go
[08:29:08] and generate this new informations.
[08:29:10] Let's start with case and then you line
[08:29:13] when country equal to the first value.
[08:29:16] It's going to be Germany. Make sure you
[08:29:18] write it exactly like in the database.
[08:29:20] The first character is capital and the
[08:29:22] rest is small. So what happened? We're
[08:29:25] going to have the abbreviation of
[08:29:27] Germany. It's going to be de. All right.
[08:29:29] So this is for the first value. And then
[08:29:30] let's move to the second one. It's going
[08:29:32] to be country equal to USA. It's already
[08:29:36] abbreviated but maybe we can get only
[08:29:39] two characters.
[08:29:40] So us like this. And now let's go and
[08:29:43] add an else. It's optional but in case
[08:29:45] that we have nulls in the data or we get
[08:29:47] a new value. So else it's not available.
[08:29:50] So na. So that's it. And never forget
[08:29:53] about the end. So end. And the name
[08:29:56] going to be country abbreviation. So
[08:29:59] that's it. Let me just get rid of the
[08:30:01] other query. So the mapping is correct.
[08:30:04] Let's go and execute it. And now if you
[08:30:06] check the results, we got a new column
[08:30:08] called country abbreviation. And as you
[08:30:10] can see now the mapping is working. Here
[08:30:12] we have Germany and we have here DE and
[08:30:14] for the USA we have US. So with that we
[08:30:17] have solved the task and we done the
[08:30:18] mapping correctly between the old value
[08:30:20] and the new
[08:30:24] value. All right friends, now there is
[08:30:26] special case for the syntax of the case
[08:30:28] statements if you are using it for
[08:30:31] mapping values. So now let's go and
[08:30:32] check it. So now let's say that we have
[08:30:34] a lot of different distinct values
[08:30:35] inside the country not only to values
[08:30:37] you have a lot of values and if you are
[08:30:39] mapping the values using the case when
[08:30:41] you're going to end up always writing
[08:30:42] the same thing country equal Germany
[08:30:45] country equal India country equal United
[08:30:47] States and so on. So we are always using
[08:30:49] the column country. So the conditions
[08:30:51] over here using always one column and
[08:30:54] it's always the operator is equal. So
[08:30:57] now only for this scenario we have
[08:30:59] another syntax for the case statements
[08:31:01] and it looks like this. We start with
[08:31:03] the keyword case but after that
[08:31:05] immediately we're going to use the
[08:31:07] column that we want to evaluate and here
[08:31:09] you can use only one column you cannot
[08:31:11] use multiple columns. So now we are
[08:31:13] telling SQL we are now evaluating one
[08:31:15] column the country and then for each
[08:31:18] condition we have the following stuff we
[08:31:19] say when Germany that means when country
[08:31:22] is equal to Germany then de so as you
[08:31:25] can see here we don't have here the
[08:31:26] whole condition we have only a possible
[08:31:29] value that we can see inside the
[08:31:31] country. So we are saying is the value
[08:31:34] country if it's true then show de the
[08:31:36] next one is it India then en United
[08:31:39] States US and so on. So we call this
[08:31:41] syntax a quick form of the case
[08:31:43] statements and on the left side we call
[08:31:45] it full form of the case statements and
[08:31:47] of course the restriction and limitation
[08:31:50] using the quick format is that you can
[08:31:52] use only one column and it's only for
[08:31:55] the equal operator. So that means only
[08:31:57] for these scenarios you can go and use
[08:31:59] the quick format. If things get a little
[08:32:01] bit complicated where you have to mix
[08:32:03] and make complex logic, you cannot use
[08:32:05] the quick formats. So I would say if you
[08:32:06] are sure that the logic will not get
[08:32:08] complicated and you can stay always with
[08:32:10] the same column, you can go with the
[08:32:12] quick format. But I would recommend
[08:32:13] always to go with the full format
[08:32:15] because for one simple reason if you add
[08:32:17] one small logic you have to go and
[08:32:19] rewrite the whole case statements back
[08:32:22] to the full format in order to add any
[08:32:24] small logic. But of course there is
[08:32:26] nothing wrong using the quick form in
[08:32:28] order to do the case statements if the
[08:32:30] logic can stay static and you are sure
[08:32:32] we are using only one column and we are
[08:32:34] just doing mapping. There is no any
[08:32:36] extra logic. Okay. So now let's try this
[08:32:37] quick format for the case statement for
[08:32:39] the previous example. So I will just go
[08:32:41] and copy everything to a new column. So
[08:32:44] I'm just going to rename it to two. And
[08:32:46] now how we going to do it? So it's going
[08:32:49] to be case but this time we're going to
[08:32:51] write country and then inside the wind
[08:32:54] we will have only the values. So no need
[08:32:57] for the condition. So it's going to be
[08:32:59] like this. Let me scroll up. So that's
[08:33:01] it. As you can see it's smaller and
[08:33:03] quicker than writing the whole condition
[08:33:05] each time. So now let's go and execute
[08:33:08] this. And as you can see in the result
[08:33:09] we're going to get identical values. So
[08:33:11] now you know one more trick in the case
[08:33:13] statement.
[08:33:18] All right, moving on to the next use
[08:33:19] case for the case statements. We can use
[08:33:21] it in order to handle nulls. Handling
[08:33:23] nulls means replace a null with a value.
[08:33:26] And as we learned before with the window
[08:33:27] aggregate functions, sometimes nulls
[08:33:29] leads to incorrect calculations and
[08:33:32] results which leads to wrong decision-m.
[08:33:34] We're going to have later a dedicated
[08:33:36] chapter on how to handle nulls in SQL.
[08:33:38] But now we're going to learn how to
[08:33:39] handle nulls using case statements. So
[08:33:42] now let's have the following task and it
[08:33:43] says find the average scores of
[08:33:46] customers and treat nulls as zero and
[08:33:48] additionally provide details such as
[08:33:50] customer ID and the last name. Okay. So
[08:33:52] now let's solve it step by step and
[08:33:54] again we have here details and as well
[08:33:56] we have to do aggregations that means we
[08:33:57] have to go and use the window functions
[08:33:59] and we don't have to forget that we have
[08:34:01] to treat the null so we have to handle
[08:34:02] it. So now let's go and start with very
[08:34:05] simple uh select. So select
[08:34:08] customer ID we need the last name and as
[08:34:12] well we need the scores. So from sales
[08:34:16] customers let's go and execute it. So as
[08:34:19] usual we have our five customers and the
[08:34:21] scores. And here we have a null. Now
[08:34:22] we're going to go and write the window
[08:34:24] function but without handling the nulls
[08:34:26] just in order to see the differences. So
[08:34:28] we need the average function for what
[08:34:31] for the scores. Do we have to now
[08:34:33] partition the data? Well no. So we're
[08:34:35] going to leave it as empty. We need the
[08:34:37] average score of all customers. So
[08:34:39] that's it. Let's go and give it a name
[08:34:42] and then execute it. I think I have here
[08:34:44] mistake. So it is a score not scores. So
[08:34:46] and now as you can see we have the
[08:34:48] average of 625. And as you learned
[08:34:50] before SQL going to go and summarize all
[08:34:53] those four values and divide it by four.
[08:34:55] But our business understand the nulls as
[08:34:57] zero not as missing information. So we
[08:34:59] have to go and handle the null. Let's go
[08:35:02] and create a new column for the scores.
[08:35:05] But this time we're going to go and use
[08:35:07] the case statements. It's going to be
[08:35:09] very simple. So we're going to say when
[08:35:12] the score is null. So in SQL we don't
[08:35:15] write equal null, we say is null. So
[08:35:18] with that we are replacing the nulls
[08:35:20] with zero. Right? So now otherwise what
[08:35:23] can happen? So if it's not null so we
[08:35:25] need the score as it is. We should not
[08:35:27] manipulate anything. So the default
[08:35:30] value is the score itself if the score
[08:35:32] is not null. So now let's go and end it
[08:35:35] and let's call it score clean. So let's
[08:35:39] go and execute it. Now if you check the
[08:35:41] result over here, it's like almost
[08:35:43] identical as the score. So we don't have
[08:35:45] any new values for the scores but only
[08:35:48] the nulls now are zero and all other
[08:35:50] values they are not affected. So we
[08:35:52] didn't touch it. We didn't transform it
[08:35:54] at all. So this is what do we mean with
[08:35:56] handling nulls replacing nulls with
[08:35:58] another value. So now in order to finish
[08:35:59] the task we have to do the average for
[08:36:01] the score clean and not for the original
[08:36:04] score. So how we going to do it? Let's
[08:36:05] go and copy the whole case statements.
[08:36:07] I'm just going to do it in another
[08:36:09] column. So let's have an average and
[08:36:12] inside it we have the case statements
[08:36:14] like this. Let me just sort it like
[08:36:17] this. And now what is missing is the
[08:36:19] over and it's going to be empty. So
[08:36:23] average customer let's call it clean. So
[08:36:26] this is the logic. Let me just make
[08:36:28] everything smaller. So now as you can
[08:36:30] see it's exactly like the previous one
[08:36:32] but instead of using the original score
[08:36:35] now we are using the column that we have
[08:36:38] created. But of course we don't need the
[08:36:39] alias over here. So we have to remove
[08:36:41] it. So it start with case and end. So
[08:36:44] let's go and execute it. And now you can
[08:36:46] see in the output we got a new value for
[08:36:49] the average and it is more accurate for
[08:36:51] the business. So now we have 500.
[08:36:53] Previously we had
[08:36:54] 625. So as you can see you have to
[08:36:56] understand what the nulls means in your
[08:36:58] business and handle it correctly.
[08:37:00] Otherwise you will get wrong results. So
[08:37:02] that's it. We use case statements in
[08:37:04] order to handle the nulls inside our
[08:37:07] data.
[08:37:10] Conditional aggregations means we're
[08:37:12] going to go and apply an aggregate
[08:37:13] function in SQL like some average count
[08:37:16] but this time only on a subset of data
[08:37:19] that meet specific conditions. This
[08:37:22] technique is amazing in order to do deep
[08:37:24] dive analyzes or target analyzes on a
[08:37:27] specific subset of the data. So now
[08:37:29] let's have the following SQL task in
[08:37:30] order to understand this use case. The
[08:37:32] task says count how many times each
[08:37:34] customer has made an order with sales
[08:37:37] greater than 30. All right. So, as
[08:37:39] usual, we can do it step by step. So,
[08:37:41] what do we need? We need the orders. So,
[08:37:43] let's get the order ID and as well,
[08:37:45] let's get the customer ID like this and
[08:37:49] the sales from sales orders. Let's go
[08:37:53] and execute it. So now, what else I'm
[08:37:55] going to do with I'm going to go and
[08:37:57] order the data by customer ID. So, let's
[08:38:00] execute it again. Okay. So, now the task
[08:38:02] sounds easy, but it's a little bit
[08:38:04] tricky. We have to count the number of
[08:38:06] orders for each customer where the sales
[08:38:08] is higher than 30. Let's have an
[08:38:10] example. For example, this customer
[08:38:12] number one. So the total number of
[08:38:13] orders is three orders, right? But we
[08:38:16] have to count only the orders where the
[08:38:17] sales is higher than 30. And in this
[08:38:20] example, we have only one order where
[08:38:22] the sales is higher than 30. So it's
[08:38:24] only the order number four. So the count
[08:38:26] for the customer ID number one should be
[08:38:28] one. Now let's check another customer.
[08:38:30] For example, the two. And as you can
[08:38:32] see, we have three orders, but none of
[08:38:34] them have the sales higher than 30. So
[08:38:37] the count should be zero here. So how we
[08:38:39] going to do that? We have to go and flag
[08:38:41] each row whether it's higher than 30 or
[08:38:44] not. So if it's higher than 30, it gets
[08:38:47] the flag of one. If it's less than 30 or
[08:38:50] equal to 30, it's going to get zero. And
[08:38:52] then we're going to go and summarize all
[08:38:53] those flags in order to get the count.
[08:38:56] So let's do it step by step. Let's first
[08:38:58] create the flag. So we're going to go
[08:39:00] and use case and then our condition is
[08:39:02] very easy. We're going to say when. So
[08:39:04] what is the condition? Sales greater
[08:39:06] than 30. So sales is higher than 30.
[08:39:10] Then what can happen? We're going to
[08:39:11] flag it with the one because later we're
[08:39:14] going to go and summarize the one. And
[08:39:15] now else if it's not higher than 30,
[08:39:18] equal to 30 or less. So it's going to
[08:39:20] get zero. All right. So now let's go and
[08:39:22] end it. So let's say sales flag. Now
[08:39:26] let's go and execute it and check the
[08:39:28] results. All right. So now if you check
[08:39:29] the results we got now a very nice flag
[08:39:31] in order to see which orders has sales
[08:39:34] higher than 30. So now for example let's
[08:39:36] take the customer ID number one. As you
[08:39:38] can see only the order number four has
[08:39:40] sales higher than 30 and it's flagged
[08:39:42] with one and all others are zero. Now
[08:39:45] let's take the customer ID number three.
[08:39:47] And as you can see we have now two
[08:39:49] orders where the sales is higher than
[08:39:51] 30. And as you can see we have the one
[08:39:53] twice. And now we can use this flag in
[08:39:55] order to do the aggregation. So now if
[08:39:57] you go and summarize the flag for the
[08:39:59] customer id number three we will get two
[08:40:02] and this is the count of orders where
[08:40:04] the sales is higher than 30 right and
[08:40:06] let's take another example the customer
[08:40:08] ID number two we have everywhere zero
[08:40:10] and if we summarize those values we will
[08:40:12] get zero which is the count of orders
[08:40:14] where the sales is higher than 30 which
[08:40:15] is correct so now as you can see first
[08:40:17] we have built an extra column in order
[08:40:19] to help us doing the aggregation and now
[08:40:21] in the next step we're going to go and
[08:40:23] aggregate this column so let's go and do
[08:40:25] that we don't need all those
[08:40:26] informations the order ID we need the
[08:40:29] customer ID because it is the
[08:40:31] granularity for the aggregation and
[08:40:33] let's remove the order by and now let's
[08:40:36] go and group up the data by customer ID
[08:40:40] but of course we need the aggregate
[08:40:42] function so how we going to do it we're
[08:40:44] going to go and summarize the whole flag
[08:40:46] so and now of course we're going to go
[08:40:48] and rename this since now it is an
[08:40:50] aggregated column so we're going to call
[08:40:52] it total orders so now let's go and
[08:40:55] execute it. So now let's go and check
[08:40:57] the result. As you can see, now we have
[08:40:58] our four customers. And for the customer
[08:41:00] ID number one, we got only one order
[08:41:03] higher than 30. The second one has no
[08:41:05] orders higher than 30. The third we have
[08:41:08] two and one. And with that, we have
[08:41:10] solved the task. Now I would like to add
[08:41:12] one more thing to our query in order to
[08:41:13] see the normal aggregations, not the
[08:41:16] conditional aggregations. So usually we
[08:41:18] go and count for example the star in
[08:41:22] order to get the total orders. And let's
[08:41:24] rename the previous one to high sales.
[08:41:27] So let's go and execute it. So we are
[08:41:29] just now doing aggregations without any
[08:41:32] conditions. And now we can see how many
[08:41:34] orders did each customer. So we can see
[08:41:36] that the customer ID number one did
[08:41:38] order three times but only one order
[08:41:40] higher than 30. So this is a normal
[08:41:42] aggregation and this is a conditional
[08:41:44] aggregations using the case
[08:41:49] statements. All right friends. So now
[08:41:51] let's do a recap about the case
[08:41:53] statements. Case statement can go and
[08:41:54] evaluate a list of conditions one by one
[08:41:57] and return a value once the first
[08:41:59] condition is met. And if you are talking
[08:42:01] about the rules of using the case
[08:42:02] statements, we have only one where the
[08:42:04] data types of each condition after the
[08:42:07] then and else must be matching. And now
[08:42:09] if we talk about the use cases of the
[08:42:11] case statements, the main use case is to
[08:42:13] do data transformations and especially
[08:42:15] by creating new columns and deriving new
[08:42:18] informations. So as we saw there are
[08:42:20] amazing use cases for the case
[08:42:22] statements. For example, we can use it
[08:42:23] in order to categorize our data. As we
[08:42:25] learned, we can go and create a new
[08:42:27] groups of data then to be aggregated for
[08:42:29] our reports. And then we saw another use
[08:42:31] case is mapping values. We can use the
[08:42:34] case statement in order to help us
[08:42:36] mapping the cryptical technical values
[08:42:38] that is stored in databases to new
[08:42:41] values which is more readable and more
[08:42:43] friendly to be used. And the next use
[08:42:45] case that we have learned is handling
[08:42:46] the nulls. We can use the case statement
[08:42:49] in order to replace the nulls with value
[08:42:51] to make our aggregations more accurate.
[08:42:54] And the last use case that we have
[08:42:55] learned and I think the most used one in
[08:42:57] my project is doing conditional
[08:42:59] aggregations where we can aggregate a
[08:43:01] subset of data that meets specific
[08:43:03] conditions in order to do focus and
[08:43:06] target analyszis. Okay my friends. So
[08:43:08] with that we have covered all the topics
[08:43:09] and all the functions in order to
[08:43:11] transform single value in SQL the role
[08:43:14] level functions that was very important
[08:43:16] especially for data engineers. So we are
[08:43:19] done with this chapter. Now we are
[08:43:20] moving to very interesting chapter.
[08:43:22] Finally we're going to talk about data
[08:43:24] analytics in SQL and we will be covering
[08:43:26] now the aggregate and the analytical
[08:43:28] functions that we have in SQL. So first
[08:43:30] we're going to start with the basics. So
[08:43:32] we will learn simple functions on how to
[08:43:34] aggregate your data. So let's go.
[08:43:40] Hey my friends. So now we're going to
[08:43:42] talk about the aggregate functions in
[08:43:44] SQL. They are amazing if you are a data
[08:43:46] analyst or data scientist where we
[08:43:48] usually use them in order to uncover
[08:43:50] insights about our data. So the
[08:43:51] aggregate functions they accept multiple
[08:43:54] rows as an input and the output of the
[08:43:56] aggregate function usually is one single
[08:43:58] value. So now we're going to go and
[08:44:00] cover first the basic aggregate
[08:44:02] functions in SQL. So let's go. So now in
[08:44:04] our database we have four orders and we
[08:44:06] have the sales informations for each one
[08:44:08] of them. So now one question that comes
[08:44:10] in our mind what is the total number of
[08:44:12] orders in our business. So how many
[08:44:14] orders do we have? Now in order to do
[08:44:16] that we use the function count because
[08:44:18] what it does it's going to go and count
[08:44:20] the number of rows inside our table. So
[08:44:22] if you apply the count function on this
[08:44:24] data SQL going to go and start counting
[08:44:26] how many rows do we have. So the total
[08:44:28] number is four and in the output we will
[08:44:30] get four. So as you can see we don't
[08:44:32] really care about the content of the
[08:44:33] tables. Scale is just counting how many
[08:44:36] rows. So the number is not based on the
[08:44:38] sales or formations or the orders. So
[08:44:40] this is how the count function works.
[08:44:42] Now we have another question and we say
[08:44:44] I would like to find the total sales in
[08:44:46] our data in our business. So that means
[08:44:48] we have to go and summarize all those
[08:44:50] sales that we have in the order and for
[08:44:52] that we have the sum function. So if you
[08:44:54] go and apply the sum function, it's
[08:44:56] going to go and summarize all the sales
[08:44:58] and return at the end the total sales.
[08:45:00] In this example, it's going to be 80.
[08:45:01] So, as you can see, the aggregate
[08:45:03] function accept multiple rows, multiple
[08:45:05] values, and the output going to be one
[08:45:07] single value, the aggregated value. Now,
[08:45:10] moving on, I would like to understand
[08:45:11] what is the average sales in our
[08:45:13] business. So, it sounds simple. In order
[08:45:15] to do that, we're going to use the
[08:45:16] average function. So, if you apply it on
[08:45:19] the sales, it's going to go and
[08:45:20] summarize all those values and divide it
[08:45:22] by the number of values. So, you will
[08:45:24] get the average of 20. Now comes
[08:45:26] interesting question where you want to
[08:45:28] find what is the highest sales in my
[08:45:30] data. So for that we can use the
[08:45:32] function max. So once you apply it it's
[08:45:34] going to go and start searching for the
[08:45:36] highest value inside our table. So this
[08:45:38] time we are not really aggregating the
[08:45:40] data into something new. It's like
[08:45:43] searching for the highest value between
[08:45:44] multiple values. So in this example we
[08:45:47] will get the 35 as the highest sales.
[08:45:49] Now of course if you want to see the
[08:45:51] lowest sales inside your business you
[08:45:53] can use the min function. And if you go
[08:45:55] and apply it as well, the same thing is
[08:45:57] going to go and start searching for the
[08:45:58] lowest value in the sales. And in this
[08:46:01] example, it's going to be the 10. So as
[08:46:02] you can see guys, the aggregate
[08:46:03] functions is very simple but yet very
[08:46:05] powerful. So it is really useful for
[08:46:07] insights in order to understand how well
[08:46:09] your business is performing. So now
[08:46:10] let's go to SQL in order to try those
[08:46:12] functions. Okay. So now we're going to
[08:46:14] go and analyze the orders table inside
[08:46:16] our database by doing very simple
[08:46:18] aggregations. So let's start with the
[08:46:20] first task. It says find the total
[08:46:22] number of orders. So this time we are
[08:46:24] targeting the table orders. So let's
[08:46:28] just start with the select. So now we
[08:46:29] can see we have like four orders. And
[08:46:31] now we would like to have like one
[08:46:32] number. What we can do? We can go and
[08:46:34] say count star as total number of
[08:46:38] orders. So let's go and execute it. And
[08:46:41] with that we got one number. It is the
[08:46:43] four. This is the total number of
[08:46:44] orders. Now let's move to the second
[08:46:46] task. It says find the total sales of
[08:46:49] all orders. So this time we have to
[08:46:51] summarize all the sales values in one
[08:46:54] big value. So how to do it? We're going
[08:46:56] to use the function sum and this time we
[08:46:59] are targeting the sales and we're going
[08:47:01] to go and call it total sales. So let's
[08:47:04] go and execute it. And with that we have
[08:47:06] 80 as the total number of sales. So all
[08:47:09] the sales values are summarized in one
[08:47:11] big value. So as you can see now we are
[08:47:12] exploring the business right? We are
[08:47:14] understanding how many sales, how many
[08:47:16] orders. So this is really the basics of
[08:47:18] analytics in SQL. Now let's go to the
[08:47:20] second task. Let's find the average
[08:47:22] sales of all orders. So we're going to
[08:47:24] have average this time the sales as
[08:47:29] average sales. Again very simple. Let's
[08:47:31] go and execute it. Now the total sales
[08:47:33] is 80 but the average sales is 20. So
[08:47:36] all the values of the sales is
[08:47:37] summarized and then divided by the
[08:47:39] number of orders. So 80 divided by four.
[08:47:42] And with that SQL finding the 20 as an
[08:47:45] average. Now let's go and get
[08:47:46] interesting stuff. Let's go and find the
[08:47:48] highest sales of all orders. So what is
[08:47:51] the highest sales that happens in our
[08:47:53] business? In order to do that, we can
[08:47:54] use the function max sales as highest
[08:47:59] sales. Very nice. Let's go and execute.
[08:48:02] So the highest sales in the database is
[08:48:05] 35. And now I think you already know
[08:48:07] what is the next task. Find the lowest
[08:48:09] sales of all orders. So this is exactly
[08:48:12] the opposite. So we're going to go and
[08:48:13] use the min sales as lowest sales. So
[08:48:18] let's go and execute. The lowest sales
[08:48:20] in our business was 10. So my friends,
[08:48:22] as you can see, the aggregate functions
[08:48:24] are really amazing. And if you use it
[08:48:26] like this, you will get like the big
[08:48:28] numbers about our business. But now
[08:48:29] don't forget about the aggregate
[08:48:31] functions. If you combine it with a
[08:48:33] group by then you will be breaking those
[08:48:36] big numbers into something like you are
[08:48:39] aggregating by the customer ID maybe by
[08:48:42] a date by a country. So anything you
[08:48:45] specify with the group by it going to
[08:48:47] breaks those big numbers into smaller
[08:48:49] number based on the column that you are
[08:48:51] using. For example let's go with the
[08:48:52] customer ID over here and let's put it
[08:48:55] at the start as well. And now if you go
[08:48:58] and execute it. So now as you can see in
[08:49:00] the output all those numbers are not
[08:49:02] anymore like big numbers. We drill down
[08:49:04] to more details based on the column that
[08:49:07] we have specified. So now we have for
[08:49:09] each customer the total number of
[08:49:11] orders, the total number of sales, the
[08:49:13] average sales, the highest sales or the
[08:49:15] lowest sales. Of course the data is very
[08:49:17] small and those numbers can be more
[08:49:19] interesting if you have bigger data. So
[08:49:20] if you combine the aggregate functions
[08:49:22] together with the group by, you will
[08:49:24] break those big numbers into more
[08:49:26] details based on the column that you are
[08:49:28] grouping by. So now what you can do, you
[08:49:30] can go and apply those functions as well
[08:49:32] for the customers. There we have a score
[08:49:35] and you can go and find the average
[08:49:37] score, the highest score, the lowest
[08:49:39] score and then you can group up the data
[08:49:41] by the country for example. So pause the
[08:49:44] video and do some aggregations on the
[08:49:46] table customers.
[08:49:48] [Music]
[08:49:52] All right my friends. So with that you
[08:49:54] have learned the basics on how to
[08:49:55] aggregate your data using SQL. Now we're
[08:49:58] going to move to more advanced way on
[08:49:59] how to aggregate your data. We will
[08:50:01] start talking about the window functions
[08:50:03] the analytical functions. So first we're
[08:50:05] going to start talking about what is
[08:50:07] exactly window functions and we're going
[08:50:08] to cover the basics about this topic. So
[08:50:10] let's go.
[08:50:15] window functions or sometimes we call
[08:50:17] them analytical functions. They are very
[08:50:20] important functions in SQL. Everyone
[08:50:22] must know them especially if you are
[08:50:24] doing data analyszis. Each time I write
[08:50:26] SQL script in order to do data
[08:50:28] analytics, I end up using them. So as
[08:50:30] usual, we're going to go and now
[08:50:31] understand the concept behind them and
[08:50:34] then we're going to start practicing. So
[08:50:36] let's
[08:50:39] go. Okay guys, so now let's start with
[08:50:41] the first question. What are SQL window
[08:50:44] functions? They are functions that
[08:50:46] allows you to do calculations like
[08:50:48] aggregations but on top of subset of
[08:50:51] data without losing the level of details
[08:50:53] of the rows. So it is something very
[08:50:56] similar to the group pi but here we have
[08:50:57] special case you don't lose the level of
[08:51:00] details. So now in order to understand
[08:51:02] the definition let's have a very simple
[08:51:04] example. Okay. So now let's understand
[08:51:05] how SQL works with the group by clouds.
[08:51:08] Let's say that we have the very simple
[08:51:10] example. We have four orders. two orders
[08:51:12] for the cabs and two order for the
[08:51:14] gloves. And let's say that I would like
[08:51:16] to see the total sales for each
[08:51:18] products. So now if we decided to use
[08:51:20] the group by what SQL going to do going
[08:51:22] to take the first two orders for the
[08:51:25] caps and put it in one row. So in the
[08:51:27] output we're going to have only one row
[08:51:29] for the caps with the total sales of 40.
[08:51:32] And the same thing going to happen for
[08:51:33] the gloves. So we're going to take the
[08:51:34] two rows of the gloves from the input
[08:51:36] and in the output we're going to have
[08:51:38] only one row for the gloves. So that
[08:51:40] means the number of rows going to be
[08:51:42] depending on the number of products we
[08:51:44] have on our data. We have two products,
[08:51:46] we get two rows. So that means SQL is
[08:51:49] really like smashing or squeezing the
[08:51:51] results in the output. And this is
[08:51:53] exactly what the grouper does to our
[08:51:55] data. It aggregate the rows, aggregate
[08:51:57] the data into different level of
[08:51:59] details. So now on the left side we see
[08:52:02] four rows. On the right side we have two
[08:52:04] rows and with that we are losing some
[08:52:06] details in the results. But still we
[08:52:08] have solved the tasks. So now let's see
[08:52:10] what going to happen if you use window
[08:52:12] function in SQL. Okay. So now we have
[08:52:13] the same data and as with the same task
[08:52:15] we have to find the total sales for each
[08:52:17] product. Now if you use window function
[08:52:20] SQL going to do the following. It going
[08:52:22] to go and execute each rows individually
[08:52:25] from each others. So what going to
[08:52:27] happen it start with the first row the
[08:52:28] order ID one. In the output we're going
[08:52:31] to get as well the same stuff the order
[08:52:33] ID one the same row but we will get the
[08:52:35] total sales for the caps. So here the
[08:52:38] total sales is going to be 10 30 we will
[08:52:40] get 40. Then it's going to jump to the
[08:52:42] second row and it's going to process it
[08:52:44] as well. So in the output we will get
[08:52:46] the order ID two the product caps and as
[08:52:48] well we have the same aggregation since
[08:52:50] we are talking about the same product.
[08:52:52] So we will get 40. Then it's going to go
[08:52:54] to the third order and here we have the
[08:52:55] gloves. So in the output again we have
[08:52:57] the order ID 3 the product gloves and
[08:53:00] the total sales this time going to be 5
[08:53:02] + 20 so we will get 25 then it goes to
[08:53:05] the last row to the order ID number four
[08:53:08] in the output we're going to get four
[08:53:09] gloves and as well 25. So now we can
[08:53:12] notice that if you use the window
[08:53:14] function you will not lose the level of
[08:53:16] details of your data. So we are doing
[08:53:18] something called rowle calculations. So
[08:53:21] if in input data we have four orders in
[08:53:23] the output we're going to get four
[08:53:25] orders and as well we will get our
[08:53:27] aggregations correctly. So now if you
[08:53:29] compare both of the methods side by side
[08:53:31] we can see that we are solving the same
[08:53:33] task. So we are finding the total sales
[08:53:35] for each products but with the group by
[08:53:38] we are smashing squeezing the results
[08:53:40] from four orders into two rows one row
[08:53:44] for each order. So that means with the
[08:53:46] group by the granularity is changing
[08:53:48] right in the input the order ID is
[08:53:50] controlling the level of details but in
[08:53:52] the output of the group by the product
[08:53:54] is controlling the level of details. So
[08:53:56] we have different granularity but in the
[08:53:58] other hand with the window functions we
[08:54:00] are still able to do aggregations but we
[08:54:02] are not losing the level of details. So
[08:54:04] the granularity of the input going to be
[08:54:07] the same like the output in the results.
[08:54:09] So this is exactly the main difference
[08:54:11] between the group eye and the window
[08:54:12] function. If you want just to do simple
[08:54:15] aggregations, then go with the group by.
[08:54:17] But if you care about the level of
[08:54:19] details and you need to add more details
[08:54:21] to your results, then you can go with
[08:54:23] the window function where you can do
[08:54:25] aggregations plus having more details.
[08:54:27] And now if you go and compare the
[08:54:29] functions between the window and the
[08:54:30] group by, we can find that both of them
[08:54:32] has exactly the same functions for the
[08:54:34] aggregations. So we have the count, sum,
[08:54:36] average, min, max. And here comes
[08:54:38] another difference between the window
[08:54:40] and the group by. The group I has only
[08:54:42] the aggregate functions. So that's it.
[08:54:44] But in the window functions, we have way
[08:54:46] more functions to use for analytics. So
[08:54:49] for example, we have the ranking
[08:54:50] functions. And we have here another
[08:54:52] group of functions for the value or we
[08:54:54] call it analytical functions. So that
[08:54:56] means in the SQL window, we have a lot
[08:54:58] of functions. We can cover a lot of
[08:55:00] analytical use cases and advanced
[08:55:02] complex stuff. But with the group by we,
[08:55:05] we have only the aggregate functions
[08:55:06] only for simple use cases. So this is
[08:55:09] another difference between the group by
[08:55:10] and the window. Group by use it if you
[08:55:13] have simple analyzes, simple
[08:55:15] aggregations, window functions, we're
[08:55:17] going to use it for more advanced data
[08:55:18] analyszis where we're going to cover a
[08:55:20] lot of use cases. All right guys, so now
[08:55:22] we're going to have few tasks in order
[08:55:23] to understand one thing. Why do we need
[08:55:26] scale window functions and why in some
[08:55:28] scenarios group is not enough and we
[08:55:31] have to use scale window functions. So
[08:55:33] let's go. All right. So let's start with
[08:55:34] very simple task. It's going to say find
[08:55:37] the total sales across all orders. So we
[08:55:40] need one value with the total sales.
[08:55:42] Let's see how we can do that. First make
[08:55:44] sure that you are using the database. So
[08:55:46] use sales database in case you have
[08:55:48] closed the clients so that we don't get
[08:55:51] any errors. So now we're going to start
[08:55:53] with the first thing. We're going to go
[08:55:54] and select the sales. You're going to
[08:55:56] find it in the table sales orders. So
[08:55:59] now let's just query the data. And as
[08:56:01] you can see we have 10 orders with 10
[08:56:03] sales. We didn't aggregate anything yet.
[08:56:06] So we have the row data now. So now in
[08:56:07] order to solve the task, we're going to
[08:56:09] use the function sum. So sum of sales
[08:56:12] and we're going to give it new name
[08:56:14] total sales. We don't have to use any
[08:56:16] group by because we don't have to group
[08:56:18] up anything. So that's it. Let's go and
[08:56:21] execute that. And as you can see SQL
[08:56:22] going to return one value 380. This is
[08:56:25] the total sales that we have inside our
[08:56:27] data. And this is the highest level of
[08:56:30] aggregations. So with that we have
[08:56:32] solved the task. We have the total sales
[08:56:34] across all orders. We don't have to
[08:56:36] group up anything. Let's move to the
[08:56:38] next example. Let's say that in the next
[08:56:40] task, this time we want to find the
[08:56:42] total sales but for each products. So
[08:56:45] not for the all orders, for each
[08:56:47] products we want to find the total
[08:56:49] sales. So this time we don't need only
[08:56:51] one value. We need one value for each
[08:56:54] product. In order to do that now, we're
[08:56:56] going to go and use the group by
[08:56:58] function. And we're going to group up by
[08:57:00] the product ID. and group up need as
[08:57:02] well the dimension in the selection. So
[08:57:05] we can do it like this. So that's it.
[08:57:07] Let's go and execute the query. Now as
[08:57:09] you can see in the results we don't have
[08:57:10] one value. We don't have the highest
[08:57:12] aggregations. This time we are drilling
[08:57:15] down to the next level of details. So
[08:57:17] the level of details here is the product
[08:57:19] ID. We have one row for each product. So
[08:57:22] for the first product we have 140. The
[08:57:25] next one 105 and so on. So as you can
[08:57:27] see we are now splitting the data at the
[08:57:30] level of product ID and we went from 10
[08:57:33] orders now in the results we have four
[08:57:35] orders and that's because we have four
[08:57:37] products. So the number of rows at the
[08:57:40] output going to be defined by the
[08:57:42] dimension the product ID and with we
[08:57:44] have solved the task we have the total
[08:57:46] sales for each product. All right guys
[08:57:48] so let's keep progressing in our
[08:57:49] examples. Now the next one going to be a
[08:57:52] little bit advanced where we have the
[08:57:53] same aggregation. Find the total sales
[08:57:56] for each product. Additionally, provide
[08:57:58] details such order ID and the order
[08:58:01] date. So, as you can see, we have
[08:58:03] already solved the first part. We are
[08:58:05] finding the total sales for each
[08:58:06] product. Now, we just have to add some
[08:58:08] additional informations like the order
[08:58:10] ID and the order date. So, let's go over
[08:58:12] here and just add it in our select. So,
[08:58:14] order ID and let's have the order date.
[08:58:17] So, let's go and execute that. Just
[08:58:19] going to make it a little bit bigger.
[08:58:21] So, let's go. But now as you can see SQL
[08:58:23] will not be happy going to throw an
[08:58:24] error and says the stuff that you are
[08:58:26] adding to your select are not included
[08:58:29] in the group by. So as you can see in
[08:58:31] the group buy we have only one dimension
[08:58:32] or one field called the product ID. But
[08:58:35] in our selection we have three
[08:58:36] dimensions the order ID, the order date
[08:58:39] and the product ID. So there is no
[08:58:41] matching between the select and group by
[08:58:43] and SQL will not allow it. And now you
[08:58:45] might say you know what let's add
[08:58:47] everything to the group by. So with that
[08:58:49] we're going to get our aggregation and
[08:58:50] as well we're going to get our details.
[08:58:52] So let's try that. I'm just going to
[08:58:53] zoom out a little bit and instead of
[08:58:55] having the product ID let's add
[08:58:57] everything. So the order ID, order date
[08:59:00] and the product ID. So now we have
[08:59:02] matching and SQL should not throw any
[08:59:04] error. Let's go and execute it. So now
[08:59:06] let's check whether we have solved the
[08:59:08] task. The task has two parts right. We
[08:59:10] have to do the aggregations and to
[08:59:12] provide details. So as you can see we
[08:59:14] have solved the second part. We have the
[08:59:16] details, order ID and order dates. But
[08:59:18] now the first part finding the total
[08:59:20] sales for each product is destroyed
[08:59:22] because if you check the results, we
[08:59:24] have the product ID 101. It has the
[08:59:26] total sales of 10. But in the third
[08:59:28] order, we have it as a 20 for the same
[08:59:31] product. So actually the data is not
[08:59:33] aggregated and that's because we are
[08:59:34] aggregating at different levels and we
[08:59:36] have included way more stuff that we
[08:59:39] don't need for the aggregations. We are
[08:59:40] aggregating at the order ID level. So as
[08:59:43] you can see now we are hitting the
[08:59:45] limits of group by. We cannot provide
[08:59:47] aggregations and as well provide
[08:59:49] additional informations from our data.
[08:59:51] You have to pick one. That's why we have
[08:59:53] to go to the second option where we can
[08:59:55] use the window functions. So let's do
[08:59:57] that. I'm just going to get rid of the
[08:59:58] group by parts and as well all the
[09:00:01] fields. Let's back to the root. So now
[09:00:03] we have the sum of sales and if execute
[09:00:05] this I'm going to get one value. So we
[09:00:07] are at the highest level of
[09:00:08] aggregations. So now we need to use the
[09:00:10] window function. I'm just going to
[09:00:12] remove the name. And now we're going to
[09:00:14] tell SQL this is a window functions
[09:00:17] using over after the aggregations or the
[09:00:19] functions tells SQL we are talking about
[09:00:22] window functions. So let's just execute
[09:00:23] it like this. And with that we got 10
[09:00:26] rows and that's because we have 10
[09:00:28] orders and for each row we have exactly
[09:00:30] the same value. So we have the total
[09:00:32] sales of all orders for each row. So as
[09:00:35] you can see SQL understands this is a
[09:00:37] window function and SQL should not like
[09:00:40] group all the data in one row. It should
[09:00:42] keep exactly the same rows or same
[09:00:44] number of rows like the input. So with
[09:00:46] that we have the window function but we
[09:00:48] have to split the data by the products.
[09:00:51] So now we're going to use the keyword
[09:00:52] partition by it's like the group by but
[09:00:54] another wording products ID the same
[09:00:57] dimension. So with that we have the
[09:00:59] total sales by products as a name. So
[09:01:02] let's go and execute this. So now as you
[09:01:04] can see in the output we still have the
[09:01:06] same number of rows. We have 10 orders.
[09:01:08] We have 10 rows but the result did
[09:01:10] change because now we are aggregating
[09:01:12] the data at the level of product ID. In
[09:01:14] order to understand the results we have
[09:01:16] to add more informations to our select.
[09:01:18] So now let's add the same dimension.
[09:01:20] It's going to be the product ID. I'm
[09:01:22] just going to add it at the front over
[09:01:23] here. So let's select. And as you can
[09:01:26] see now it makes more sense. We have
[09:01:28] those products and they have always the
[09:01:31] exact same uh sales. and as well for the
[09:01:34] next product and so on. And now here
[09:01:35] comes the magic of the window function.
[09:01:37] We can add more informations to our
[09:01:39] select statement without having any
[09:01:41] errors. So now we need additional
[09:01:43] informations like the order ID. So we
[09:01:45] can go over here and say order ID, order
[09:01:48] date, any type of column you can add it
[09:01:51] to your select and let's go and execute.
[09:01:53] So as you can see now we got the result
[09:01:55] even though that those three dimensions
[09:01:57] in the select are not part of the window
[09:02:00] aggregation. So with that we have solved
[09:02:02] the tasks. We have additional
[09:02:04] informations. We have the order ID, the
[09:02:06] order date and as well the first part of
[09:02:08] the task to find the total sales for
[09:02:10] each products. So each of those values
[09:02:13] are the total sales for each product.
[09:02:15] And with that we have solved the tasks
[09:02:17] and this is exactly why we need window
[09:02:19] functions. In real projects things get
[09:02:21] really complicated. You are doing
[09:02:23] different tasks in one query. So you are
[09:02:25] doing aggregations, you are doing some
[09:02:27] other stuff. So just focusing on the
[09:02:30] aggregations is not going to be enough.
[09:02:31] You have always to add additional
[09:02:33] informations to your query. So as you
[09:02:35] can see we use group eye to do simple
[09:02:36] analyszis but as things get complicated
[09:02:39] in the analytics we use the window
[09:02:41] functions in order to show the
[09:02:43] aggregations and as well add additional
[09:02:45] informations. So as you can see we use
[09:02:47] group eye to do simple analyszis but as
[09:02:49] things get complicated in the analytics
[09:02:52] we use the window functions in order to
[09:02:54] show the aggregations and as well add
[09:02:56] additional informations.
[09:03:01] All right everyone. So now we're going
[09:03:02] to go and deep dive into the syntax of
[09:03:04] the SQL window functions. We're going to
[09:03:06] cover everything each part of the syntax
[09:03:08] for you to understand how to use them.
[09:03:11] So let's go. All right. So let's start
[09:03:12] first by understanding the basic
[09:03:14] components or the basic parts of each
[09:03:16] window syntax. Mainly we have two parts.
[09:03:19] The first part going to be the window
[09:03:21] function. We have like sum, average and
[09:03:23] so on. The second main part is going to
[09:03:25] be the over close. And inside the overlo
[09:03:28] we have three different parts. The first
[09:03:30] one going to be the partition close. The
[09:03:32] second order close and the last one we
[09:03:35] have the frame close. And those are all
[09:03:37] components that you can use inside the
[09:03:39] window function. So two main parts
[09:03:41] window function and the offer close. And
[09:03:43] inside the over we have partition order
[09:03:45] and frame. Let's go more in details. So
[09:03:47] for example we have the following window
[09:03:49] function. So as you can see we have a
[09:03:51] lot of stuff going on here. We're going
[09:03:53] to understand them step by step
[09:03:54] component by component. Let's start from
[09:03:56] the left from the first one. So what do
[09:03:58] we have over here? We have a function
[09:04:00] window function. So what is a window
[09:04:02] function? Like here we have the average.
[09:04:04] It's like any other function in SQL. You
[09:04:06] can use it in order to do calculations
[09:04:08] on top of the window. So the first thing
[09:04:10] to do or to define in a window is to
[09:04:13] define the function of the window. And
[09:04:15] as we learned before, we have a long
[09:04:17] list of many window functions available
[09:04:19] in SQL. And we group them into three
[09:04:21] groups. The first one we have the
[09:04:23] aggregate functions. So we have the
[09:04:25] count, sum, average, max. All those
[09:04:27] functions we have them as well for the
[09:04:29] group by. So those are used for the
[09:04:32] aggregations. The second group of
[09:04:34] functions we have the ranking functions.
[09:04:37] So we have the row number, rank, entile
[09:04:39] and so on. So we can use those groups in
[09:04:41] order to give a rank for our data. The
[09:04:43] last group we call it value or sometimes
[09:04:46] analytics functions. So here we have
[09:04:48] very important functions like the lead,
[09:04:49] lag, first value and the last value in
[09:04:52] order to access a specific value and of
[09:04:54] course we're going to go and learn all
[09:04:56] of them one by one understanding the
[09:04:58] concepts some examples and as well for
[09:05:00] you to understand when to use them for
[09:05:02] that analyzers. All right so now let's
[09:05:04] keep moving understanding the other
[09:05:05] parts of the window syntax. Now inside
[09:05:08] the function average we have here a
[09:05:10] field name or column name called sales.
[09:05:12] This called a function expression. It's
[09:05:15] like a value, a parameter, argument that
[09:05:17] we can pass it to the function. And here
[09:05:19] we can use multiple different stuff. For
[09:05:22] example, depend of the function of
[09:05:23] course. So here it could be empty like
[09:05:25] here in the ranking. It doesn't allow to
[09:05:27] use an expression. So it should be
[09:05:29] always empty. Or we can use a column
[09:05:31] like in the example we use the sales. So
[09:05:34] we use the column name as an argument or
[09:05:36] an expression. For the average we are
[09:05:39] finding the average of sales or we could
[09:05:41] use a number. So here in the intile we
[09:05:43] are allowed only to use numbers or we
[09:05:46] could have multiple stuff. For example
[09:05:48] in the lead we can have sales then
[09:05:50] numbers and so on. So things get
[09:05:52] complicated. Don't worry about it. I'm
[09:05:53] going to explain that. So here we have
[09:05:55] multiple stuff or we can have a whole
[09:05:58] conditional logic. So for example here
[09:06:00] we have the case win so on inside the
[09:06:03] sum. So the whole thing over here holds
[09:06:06] an expression for the sum. So as you can
[09:06:08] see we can build here a complex logic
[09:06:10] and the output of this logic can be
[09:06:12] passed to the function sum. So that
[09:06:14] means as an expression for the function
[09:06:16] we can use different stuff of course
[09:06:18] depends whether the function allows it
[09:06:20] or not. All right. So now let's have a
[09:06:21] quick overview in order to understand
[09:06:23] which data types are allowed in the
[09:06:25] expressions for those functions. Let's
[09:06:27] see the aggregate functions. As you can
[09:06:29] see the count function accept any data
[09:06:31] type but the others like the sum,
[09:06:34] average, min, max, they allow only
[09:06:36] numerical data types. All right. So now
[09:06:38] let's move to the rank function. The
[09:06:40] expressions it's pretty easy. It should
[09:06:42] be empty. It doesn't allow any argument
[09:06:44] or anything inside those functions. So
[09:06:46] as you can see all of them are empty but
[09:06:49] only one that accept numerical values
[09:06:51] which is the end tile. You have to
[09:06:53] define a numeric value. And now moving
[09:06:55] on to the last type we have the value
[09:06:57] functions. they accept any data types
[09:06:59] inside the expressions. So as you can
[09:07:01] see each functions has its own
[09:07:03] specifications and you have to be
[09:07:05] careful which data type you are using in
[09:07:07] the expressions. Okay. So now let's keep
[09:07:09] moving to the next one. We have a very
[09:07:11] important part in the window syntax. So
[09:07:13] so far what do we have? We have a
[09:07:14] function. We have an expression. It's
[09:07:16] like usual stuff. We have done that
[09:07:18] before using the group by. Now we have
[09:07:20] to tell SQL that we are dealing with the
[09:07:23] window function. It's not a normal one.
[09:07:25] In order to do that we have to specify
[09:07:27] the keyword over. So the second main
[09:07:30] part in the syntax is the over clause
[09:07:32] and we use it in order to define a
[09:07:34] window and inside it we can define
[09:07:36] multiple stuff like the partition pie
[09:07:38] the order by the frame but all those
[09:07:41] stuff are optional. We can skip it and
[09:07:43] leave it empty. So the main task of the
[09:07:46] over it tells first SQL we are dealing
[09:07:48] with the window function here and as
[09:07:50] well you can use it in order to define a
[09:07:53] window of your data. So now we're going
[09:07:55] to go and cover everything inside the
[09:07:56] over clause and we're going to start
[09:07:58] with the first one the partition
[09:08:03] pi. All right. So now we're going to
[09:08:05] learn how to define a window inside the
[09:08:07] overlaus. The first part that we can
[09:08:10] define is the partition pi. So for
[09:08:12] example here we have partition pi
[09:08:14] category. We have to define the
[09:08:15] dimension. It's very similar to the
[09:08:17] group by another wording. So the first
[09:08:19] part is going to be the partition
[09:08:20] clause. What it going to do? It's going
[09:08:23] to divide the entire data sets into
[09:08:26] groups or you can call it windows
[09:08:28] partitions. So here we tell SQL how to
[09:08:31] divide our data. And here we have two
[09:08:33] options. Let me just show you. So if we
[09:08:36] don't use anything so we have it empty.
[09:08:38] You see over and partition by is not
[09:08:41] used. What going to happen? SQL going to
[09:08:44] use the entire data in order to do the
[09:08:46] calculations. So the whole data the
[09:08:49] entire data going to be counted as one
[09:08:51] window. So we are telling SQL don't
[09:08:53] divide anything leave it as it is. The
[09:08:56] second option that we have is to divide
[09:08:57] the data by partition pi. So we define
[09:09:00] the window like this partition pi
[09:09:02] products for example. So SQL going to go
[09:09:04] and divide the entire data into
[09:09:06] different windows. For example here two
[09:09:08] windows. And here this time the
[09:09:10] calculation the sum of sales will not
[09:09:12] apply on the entire data set. This time
[09:09:15] it going to be applied on the different
[09:09:17] windows individually. So we're going to
[09:09:19] find the sum of sales for window one
[09:09:22] separately from the total sales of
[09:09:24] window 2. All right. So now we have this
[09:09:25] very simple example. We have here three
[09:09:27] fields. The month, product, sales. They
[09:09:30] are really easy informations. And now we
[09:09:32] have the following SQL window function.
[09:09:34] So we have sum of sales and inside the
[09:09:38] overlo we are not using anything. So we
[09:09:41] are not using partition by. So how SQL
[09:09:43] going to define the window. Now SQL
[09:09:45] going to say okay I don't have to divide
[09:09:47] anything. The entire data set is one
[09:09:50] window. So SQL going to go over here and
[09:09:53] say the whole thing is one window. So
[09:09:56] there is no partitions, there is
[09:09:57] nothing. We have only one window. So the
[09:10:00] entire data going to be aggregated. So
[09:10:02] this is what happen if you don't use
[09:10:04] partition pi and you leave the
[09:10:06] overclos. The entire data is one window.
[09:10:09] All right. So now let's move to the next
[09:10:10] example. We don't want to have only one
[09:10:12] window. We would like to have multiple
[09:10:13] windows. So we have to divide the data
[09:10:16] by something. So in the over clause
[09:10:18] we're going to define the window like
[09:10:19] the following partition by month. So
[09:10:22] it's not empty. We are now dividing the
[09:10:25] data by the field month. So the values
[09:10:28] inside this column going to divide the
[09:10:30] data sets. So here we have two months
[09:10:33] January and February. So what's going to
[09:10:35] do? SQL going to go and divide the data
[09:10:38] into two sets. The first window going to
[09:10:40] be this one of January. So we have the
[09:10:43] first window going to make it smaller
[09:10:45] and the second window going to be the
[09:10:47] February. So it's going to be two
[09:10:49] windows inside our data and the
[09:10:51] calculation going to be happening on
[09:10:53] each window separately. So here as you
[09:10:55] can see we are using the month in order
[09:10:57] to divide our data sets into two
[09:11:00] windows. One window for January and
[09:11:03] another window for the February. So now
[09:11:05] let's have a quick overview about the
[09:11:06] options that we have with the partition
[09:11:08] p. The first option as we learned we can
[09:11:10] just skip it. So without partition by
[09:11:12] for example here total sales across all
[09:11:15] rows and here we don't find anything
[09:11:16] inside the SQL. The second option we can
[09:11:19] use one field one column for example
[09:11:22] partition by product. So we are using
[09:11:24] one dimension but we can go and mix
[09:11:27] stuff. We can use multiple columns or
[09:11:29] multiple dimensions in the partition by
[09:11:31] for example here partition by product
[09:11:33] and order status. So here with the
[09:11:35] partition by we can define a list of
[09:11:37] dimensions that could be used in order
[09:11:39] to divide our data. So in this example
[09:11:41] we are saying find the total sales for
[09:11:44] each combination of products and order
[09:11:46] status. So those are the different
[09:11:48] options on how to work with the
[09:11:49] partition pi. So now let's have this
[09:11:51] overview again for all functions. The
[09:11:54] partition pi for all those functions is
[09:11:56] optional. So if you don't use the
[09:11:58] partition pi in all those functions you
[09:12:00] will not get any errors. So now let's go
[09:12:02] back to SQL in order to start practicing
[09:12:04] with this clause. Okay. So now we have
[09:12:06] the following task. Find the total sales
[09:12:08] across all orders. And we have to
[09:12:10] provide additional informations like the
[09:12:12] order ID and the order date. So let's go
[09:12:15] and solve it step by step. First I would
[09:12:17] like to provide the details. So I'm
[09:12:19] going to select the order ID and the
[09:12:21] order dates from the table sales orders.
[09:12:26] And next we're going to work with the
[09:12:27] aggregations. So we need to find the
[09:12:28] total sales across all orders. Again
[09:12:31] since we have here details and
[09:12:32] aggregations we cannot use group by. We
[09:12:35] have to use the window function. So
[09:12:36] we're going to go use the function sum
[09:12:39] for sales. And now we have to tell SQL
[09:12:42] we are working with window functions.
[09:12:44] That's why we're going to use the over
[09:12:45] close. And now the next step we have to
[09:12:47] think about defining the window. So
[09:12:50] let's check the task. It says total
[09:12:51] sales across all orders. So that means
[09:12:54] we don't have to partition or divide the
[09:12:57] data sets into like chunks or
[09:12:59] partitions. We have to leave it as it is
[09:13:01] like the whole data going to be one
[09:13:03] window. And that's why we don't use
[09:13:05] partition pi inside the definition.
[09:13:07] We're going to leave it empty. Let's go
[09:13:08] now and give it a name. It's going to be
[09:13:10] the total sales. Let's go and execute
[09:13:12] this. And now at the results, as you can
[09:13:14] see, we have all the orders, all the
[09:13:16] details, and as well, we have the total
[09:13:18] sales across all orders. So with that,
[09:13:21] we have solved the tasks. We have the
[09:13:22] total sales and as well some details
[09:13:24] about the order. All right. So now let's
[09:13:26] move to the next task. It's going to be
[09:13:28] very similar. So it says find the total
[09:13:30] sales for each product. And we have to
[09:13:32] provide additional informations like the
[09:13:34] order ID and the order dates. So it's
[09:13:36] going to be very similar task but this
[09:13:38] time we have to divide the entire data
[09:13:42] into windows and that's going to be by
[09:13:44] the product. Since we are saying total
[09:13:46] sales for each product. So this time we
[09:13:48] have to go and divide the data. So we're
[09:13:51] going to define the window like this
[09:13:52] partition by and we can use the
[09:13:55] dimension product ID. Let's go and
[09:13:58] execute this. So now you can see in the
[09:14:00] total sales we don't have anymore the
[09:14:02] total sales of the whole data but they
[09:14:04] are divided but in order to understand
[09:14:06] the results let's go and include the
[09:14:08] product ID in the results. So product ID
[09:14:11] and execute. So now by looking to the
[09:14:13] results you can see that the data is
[09:14:15] divided into four windows. Let's see
[09:14:18] them. It's going to be by the product
[09:14:19] ID. So this dimension going to be
[09:14:21] controlling the partition. So the first
[09:14:23] window going to be the product ID 101.
[09:14:26] So we have the total sales for this
[09:14:27] product 140 and the next window going to
[09:14:30] be 102. The third one 104 and the last
[09:14:34] window it's going to be only one row the
[09:14:36] 105 and the total sales of 60. So with
[09:14:39] that we have solved the task. We have
[09:14:40] the total sales for each product and as
[09:14:43] well we have some details. Now I would
[09:14:44] like to show you the dynamic of the
[09:14:46] window function. We can add multiple
[09:14:49] aggregations on multiple levels. Let me
[09:14:51] show you what I mean. Let's say we stay
[09:14:53] with the same example but we're going to
[09:14:55] find the total sales across all orders
[09:14:57] and as well the total sales for each
[09:15:00] products. So what we can do we can do
[09:15:03] the window functions on different levels
[09:15:05] by for example here removing the whole
[09:15:07] definition. So here we have the total
[09:15:08] sales for the entire data for the first
[09:15:11] task and the next one going to be the
[09:15:12] total sales but divided by the product
[09:15:15] ID. Let's here rename it by
[09:15:17] products. Let's go and execute this. And
[09:15:21] now you know what I'm going to go and
[09:15:22] add the sales as well just to explain
[09:15:24] the flexibility of the window function.
[09:15:26] So let's go add the sales and execute it
[09:15:28] again. And now by looking to the results
[09:15:30] you can see we have the sales
[09:15:32] informations three time but with
[09:15:34] different granularities. The first sales
[09:15:37] the sales it sales without any
[09:15:38] aggregations. It is the highest level of
[09:15:41] details of the sales and we're going to
[09:15:42] have the sales for each order. The next
[09:15:45] one the total sales with the window
[09:15:47] function. Here we have the highest level
[09:15:48] of aggregation. So we have the total
[09:15:50] sales of all orders and the last one the
[09:15:53] total sales by product it's something
[09:15:54] like in the middle we are aggregating on
[09:15:57] a window and the window going to be the
[09:15:59] product ID. So as you can see we have
[09:16:01] different granularities of the
[09:16:02] aggregations and this is exactly the
[09:16:04] flexibility that we have with the window
[09:16:06] function. We can do all those stuff in
[09:16:08] one query. Okay. So now let's keep
[09:16:10] moving and adding stuff to our task.
[09:16:12] It's going to say find the total sales
[09:16:14] for each combination of the products and
[09:16:16] the order status. So this time we have
[09:16:19] to divide the data not only by the
[09:16:20] product as as well with another
[09:16:23] dimension the order status. So now let's
[09:16:25] see how we can do that. I'm going to
[09:16:27] just show the dimension order status and
[09:16:29] the results and we're going to add the
[09:16:32] following thing. So sum sales over since
[09:16:37] it's a window function and let's go now
[09:16:39] and define the window partition by. So
[09:16:41] we have again the product ID but not
[09:16:43] only this dimension as well the order
[09:16:46] status and let's go and call it sales by
[09:16:50] products and status. Let me just rename
[09:16:53] those stuff. Okay. So let's go and
[09:16:56] execute. All right. So now let's check
[09:16:57] the results. It is the last aggregation
[09:16:59] over here. And as you can see here the
[09:17:01] aggregation has different granularities
[09:17:03] as the previous one. And we have more
[09:17:06] details. This time we are splitting the
[09:17:08] data by two dimensions. So the first
[09:17:10] window going to be the product ID with
[09:17:12] the order status it's going to be only
[09:17:14] those two rows. So we have the order ID
[09:17:16] 101 and the order status delivered. So
[09:17:19] the total sales of this going to be 10 +
[09:17:22] 20 and we're going to have 30. The next
[09:17:24] window going to be the same product but
[09:17:27] with different status. So it's going to
[09:17:29] be the 101 shipped and we're going to go
[09:17:31] and summarize those two values and we're
[09:17:33] going to have 110. The next product and
[09:17:36] order status going to be the 102 and we
[09:17:38] have it only once. So 102 delivered it's
[09:17:41] only once. So it's going to be the same
[09:17:43] value. The next partition or window it's
[09:17:45] going to be two rows. 102 with the
[09:17:48] shipped is going to be those two things
[09:17:51] 60 + 15 we're going to get 75. So as you
[09:17:54] can see here the product ID and the
[09:17:56] order status they are controlling how
[09:17:59] many windows we're gonna get. So we get
[09:18:00] here around like six windows. With the
[09:18:02] product ID we got only four windows and
[09:18:05] without using anything inside the
[09:18:07] overlause we will get only one window.
[09:18:10] So this is how the partition by
[09:18:15] works. All right. So that was the first
[09:18:17] part of the window definition within the
[09:18:19] overclo. Let's move to the next part. We
[09:18:21] have the order by. For example, we can
[09:18:23] use order by order date. It's just a
[09:18:25] field. So the order close is very
[09:18:28] important in order to sort your data
[09:18:30] within a window. So the order by is very
[09:18:32] important as well for many functions. So
[09:18:34] by just checking the overview over here
[09:18:36] for the aggregate functions it is
[09:18:38] optional. So you could just leave it or
[09:18:39] add it. But for the rank function and as
[09:18:42] well for the value functions they are a
[09:18:45] must. So if you want to use those
[09:18:47] functions you must use the order clause
[09:18:50] because it makes no sense for example if
[09:18:52] you are ranking the data without sorting
[09:18:54] your data first. Okay guys. So now back
[09:18:56] to our very simple example and we have
[09:18:57] the following query. So the function
[09:18:59] this time going to be rank. So we have
[09:19:01] to rank the data and the definition of
[09:19:03] the window going to be partition by
[09:19:05] month. So that means we divide the data
[09:19:07] by the months. So we have it over here.
[09:19:10] And then the second part going to be
[09:19:12] order by sales descending. So we have to
[09:19:14] sort each window by descending order.
[09:19:17] That means we start with the highest
[09:19:18] value and we end up by the lowest value.
[09:19:21] So let's see how SQL going to go and
[09:19:22] execute this. So first partition by
[09:19:25] month. So it's going to divide the data
[09:19:26] into two partitions because we have two
[09:19:28] values by the month. So let's see how
[09:19:31] this going to look like. So one window
[09:19:33] for January and another window for
[09:19:36] February. All right. So now SQL going to
[09:19:38] go to the second part and execute order
[09:19:40] by sales descending. So what's going to
[09:19:42] happen? SQL going to go for each window
[09:19:44] separately and start sorting the data
[09:19:46] from the highest to the lowest without
[09:19:48] checking the other window. So in those
[09:19:50] three values, the highest one is this
[09:19:52] one. So it's going to be on top. Let me
[09:19:54] just sort it. This is going to be the
[09:19:56] lowest. You're going to be in the
[09:19:58] middle. So SQL going to sort this window
[09:20:01] separately from the next one. And then
[09:20:02] once it's done, it's going to go to the
[09:20:04] second one. So the highest value going
[09:20:06] to be this one. You are the lowest. Let
[09:20:09] me just do it like this. So SQL going to
[09:20:12] sort it like this. The highest one is
[09:20:14] 70. The next one is 40. And the last one
[09:20:16] is five. So with that SQL done with the
[09:20:18] definition of the window. So it's
[09:20:20] splitted by the month. And each window
[09:20:23] is sorted by the cells. The next step is
[09:20:26] going to go and rank those values. So
[09:20:28] it's really simple. In the output, it's
[09:20:30] going to rank the data like this. So the
[09:20:32] first one going to be this value. The
[09:20:34] next one going to be two and the third
[09:20:36] one going to be three. So as you can
[09:20:38] see, SQL is sorting only this window and
[09:20:40] it's going to go and repeat the same
[09:20:42] stuff for the second window. So each
[09:20:44] rank is separately from the others. So
[09:20:46] as you can see, it's very simple. This
[09:20:47] is how SQL executes partition by
[09:20:50] together with the order buy for the rank
[09:20:52] function. All right. So now let's have a
[09:20:53] quick task for the order by. It says
[09:20:55] rank each order based on their sales
[09:20:57] from the highest to the lowest. And we
[09:21:00] have to provide additional informations
[09:21:01] like order ID and order date. So let's
[09:21:03] see how we can write the query. So we
[09:21:05] have the basic stuff order ID, order
[09:21:07] date and sales. And now we're going to
[09:21:09] go and rank the data using window
[09:21:11] function. So we're going to use the
[09:21:12] function rank and then we're going to
[09:21:15] tell SQL this is a window function and
[09:21:18] inside it we have now to provide the
[09:21:20] definition of the window. So now by
[09:21:21] checking the task you can see that we
[09:21:23] don't have to divide the data. So we
[09:21:25] don't have to use partition by we have
[09:21:27] just to use rank and with rank we have
[09:21:29] to use the order by it is must. So we're
[09:21:31] going to use order by the field going to
[09:21:34] be the sales and from the highest to the
[09:21:36] lowest. So let's just call it rank sales
[09:21:39] and let's go and execute this. And as
[09:21:41] you can see our results going to be
[09:21:43] sorted from the highest to the lowest.
[09:21:45] So you can see the sales 90 at the top
[09:21:47] and the lowest going to be the 10. And
[09:21:49] as well we have a rank. So for the top
[09:21:51] rank it's going to be one and the lowest
[09:21:52] rank going to be 10. So as you can see
[09:21:54] we just quickly create a rank in SQL.
[09:21:57] It's very simple. The whole thing is one
[09:21:59] window since we are not using partition
[09:22:01] pi. And of course if you want to have
[09:22:03] ascending so from the lowest to the
[09:22:06] highest you can just remove it because
[09:22:08] optionally going to be ascending. So
[09:22:10] let's go and execute the query. So now
[09:22:12] we can see the orders are sorted the way
[09:22:14] around. So we start with the lowest and
[09:22:16] end up with the highest. And of course
[09:22:17] you're going to get the same results if
[09:22:19] you go over here and add ascending. So
[09:22:22] if you execute you see we got exactly
[09:22:24] the same results. So this is how you use
[09:22:26] the order by inside the window
[09:22:28] definition.
[09:22:32] Okay guys, so with that we have covered
[09:22:34] the second part of the window
[09:22:35] definition. Now we're going to go to the
[09:22:37] last part to the most advanced part of
[09:22:39] window and we have the following stuff.
[09:22:41] So we have rows unbounded proceeding. We
[09:22:45] call this frame close or window frame.
[09:22:47] So what we are doing over here that we
[09:22:49] are defining a subset of rows within
[09:22:52] each window that is relevant for the
[09:22:54] calculation. Totally understand if this
[09:22:56] is confusing at the start or complex. It
[09:22:58] was for me as well. So what we're going
[09:23:00] to do we're going to deep dive into the
[09:23:01] concept in order to understand how this
[09:23:03] works and we're going to do it step by
[09:23:05] step. So don't worry about it. All
[09:23:06] right. So now let's understand what is
[09:23:08] going on with the frame close from the
[09:23:10] basics. So now if you do aggregations
[09:23:12] and you don't use window function you're
[09:23:14] going to consider the entire data or
[09:23:16] rows inside the table. But what we can
[09:23:18] do we can go and divide the data using
[09:23:20] partition pi to a window. So for example
[09:23:23] here we have window one and window two.
[09:23:25] Now if you go and do aggregations all
[09:23:27] the rows in the window one going to be
[09:23:29] aggregated and then scale going to go to
[09:23:31] the window two and aggregate all the
[09:23:33] rows. What we can do in scale is that we
[09:23:35] can say you know what I don't want all
[09:23:37] rows inside the window. I want a subset
[09:23:40] of rows inside the window. So what we
[09:23:42] are doing over here is that we have
[09:23:44] those two windows but we specify a scope
[09:23:47] or we specify subset of data from each
[09:23:50] window to be involved in the
[09:23:51] aggregations. And of course not only
[09:23:53] aggregations we can do ranking other
[09:23:55] stuff. So I mean calculations. So here
[09:23:57] like we have a window inside a window.
[09:23:59] So we are defining a scope of rows. Not
[09:24:02] all rows should be involved in the
[09:24:04] calculation but only specific subset of
[09:24:07] data. And we can do that using the frame
[09:24:09] clause. So again the partition by you
[09:24:12] can use it in order to divide the entire
[09:24:14] data set into multiple windows. And now
[09:24:17] for the frame close. If you don't want
[09:24:18] to consider all the rows within each
[09:24:20] window in the calculation, you want to
[09:24:23] focus and specify only a subset of data
[09:24:25] within each window. Then you going to go
[09:24:27] and use the frame close. All right. So
[09:24:29] now let's go and understand the syntax
[09:24:31] of the frame close. Let's have the
[09:24:33] following example. We are saying the
[09:24:34] window function is the average of sales
[09:24:37] and then we define the window. So we
[09:24:38] have the first partition by categories,
[09:24:41] order by order dates and then we have
[09:24:43] the frame close. It's going to be the
[09:24:45] following rows between current row and
[09:24:48] unbounded preceding. This is the frame
[09:24:50] types and we have two types. We have the
[09:24:52] rows and groups. Then we have like
[09:24:54] between and range. So the first range
[09:24:57] going to be the frame boundary, the
[09:24:59] lower value. And here it accepts three
[09:25:02] types of keywords like the current row
[09:25:05] or number of preceding or the unbounded
[09:25:07] preceding. And then we have another
[09:25:09] frame boundary. It's going to be the
[09:25:11] higher values and it accepts the
[09:25:12] following stuff. We can use the current
[09:25:14] row in following or unbounded following.
[09:25:17] So as you can see we are defining like
[09:25:19] boundary or a range from low value to
[09:25:21] higher value. So now we have some rules.
[09:25:24] We cannot use the frame clause without
[09:25:26] order by. So order by must be exist in
[09:25:29] the definition in order to use frame
[09:25:32] clause. And the second rule it says
[09:25:34] lower boundary must be before the higher
[09:25:36] boundary. So always we start with the
[09:25:38] lower boundary and we end up having the
[09:25:40] higher boundary. You cannot switch that.
[09:25:43] Okay. So now we have a very simple
[09:25:44] example. We have the month and the sales
[09:25:46] and the following query. Sum of sales.
[09:25:48] This is the window function. And the
[09:25:50] definition of the window going to be
[09:25:51] order by month. We are not using
[09:25:53] partition by just in order to make our
[09:25:54] life easier. And the frame close going
[09:25:57] to be defined like this. Rows between
[09:25:59] current row and the two following. So
[09:26:01] now let's see how SQL going to execute
[09:26:03] this. The first definition order by
[09:26:05] month. As you can see the months are
[09:26:06] sorted already. So now SQL going to work
[09:26:09] with the frame definition current row
[09:26:11] and the two following. So SQL going to
[09:26:13] process this row by row. So it's going
[09:26:15] to start with the first row and it's
[09:26:17] going to be our current row as here in
[09:26:19] the SQL. So this is our current row and
[09:26:22] we say the range until two rows, two
[09:26:24] following rows. So it's going to be
[09:26:25] February and March. So that means the
[09:26:28] pointer going to be over here for the
[09:26:30] two following. So with this we have the
[09:26:32] frame boundaries and SQL have the
[09:26:34] following scope for the first row. So we
[09:26:36] have three rows and the summarization of
[09:26:39] those three rows going to be around 70.
[09:26:42] So we will get for the first row 70
[09:26:44] because the scope is not all rows but
[09:26:46] only this subset of data. Okay. So with
[09:26:49] that scale is done with the first row
[09:26:50] it's going to jump to the second row. So
[09:26:52] the pointer going to be the current row
[09:26:54] at the February and the second two
[09:26:56] following going to be at April. So with
[09:26:58] that as you can see we are sliding down
[09:27:01] in the subset of data or in the window
[09:27:03] and with that we have a new scope a new
[09:27:05] subset and the summarization of all
[09:27:06] those values going to be 45. So that's
[09:27:09] it. I think you get it already. It's
[09:27:11] going to go to the next one. The pointer
[09:27:13] going to be on March and the two
[09:27:14] following going to be on June and it's
[09:27:16] going to slide like this. We have those
[09:27:18] three rows in the scope and the
[09:27:19] summarization of that going to be 105.
[09:27:22] So now things gets interesting for the
[09:27:24] next row. So the pointer for the current
[09:27:26] row going to be April but the two
[09:27:28] following going to be like after the end
[09:27:30] of the table or something like that. So
[09:27:32] as we slide down as you can see the
[09:27:34] scope now or the subset of the frame
[09:27:35] going to be only two rows and the output
[09:27:38] going to be 75. And finally if you go to
[09:27:40] the last row it's going to be the
[09:27:42] current row and we're going to have only
[09:27:44] one row for this subset because the two
[09:27:46] following is just outside of the table
[09:27:48] and we're going to get the same value as
[09:27:50] the summarization. So as you can see
[09:27:51] that's it. It's very simple right? So
[09:27:53] the frame we use it in order to scope
[09:27:56] which rows are involved in the
[09:27:58] calculations. So all you have to do is
[09:28:00] to define the boundaries of the frame,
[09:28:01] the lower and the upper boundary. Let's
[09:28:04] see what other options do we have with
[09:28:05] the frames. Okay. So here we have the
[09:28:07] same example but we redefine the
[09:28:09] boundaries of the frame like this. Rows
[09:28:11] between current row this is the first
[09:28:13] boundary and unbounded following. This
[09:28:16] means that we are targeting always the
[09:28:18] last record in the window or in the
[09:28:20] table. So unbounded following going to
[09:28:22] be always static and it's going to be in
[09:28:24] this example pointing to June. And now
[09:28:26] it's still going to go row by row and
[09:28:28] the current row going to be like the
[09:28:30] start January and then February. I'm
[09:28:32] just going to take this example the
[09:28:34] pointer is on February and the subsets
[09:28:36] or the frame going to be those four
[09:28:38] rows. So it's going to be February,
[09:28:40] March, April, June. So it's going to be
[09:28:41] four rows and the total aggregation of
[09:28:44] that going to be 115. So you can do it
[09:28:47] like this. And previously it was like
[09:28:50] flexible more flexible it was two
[09:28:52] following but this time we have
[09:28:54] unbounded following that means always
[09:28:56] the boundary going to be the last one.
[09:28:58] So as we are moving with the records
[09:29:00] over here the boundary is going to be
[09:29:03] smaller smaller and like this and the
[09:29:06] last one they going to be both in the
[09:29:08] same record. So the current record going
[09:29:11] to be as well the unbounded following.
[09:29:13] Okay let's see the next one. The
[09:29:14] definition of the window going to be the
[09:29:16] following rows between one proceeding
[09:29:18] and the current row. So here is the way
[09:29:21] around one proceeding is lower than the
[09:29:23] current row. So let's see how SQL going
[09:29:25] to execute this. Let's say that we are
[09:29:28] currently at March. So this is the
[09:29:30] current row and we are saying between
[09:29:32] one proceeding. So that means one row
[09:29:34] before the current row. So the frame
[09:29:37] going to be like this and we have only
[09:29:39] two rows. So the value going to be the
[09:29:41] summarization of those two rows and it's
[09:29:43] going to be 40. So that means we are
[09:29:45] always targeting the rows before the
[09:29:48] current row. Okay. So now let's keep
[09:29:49] going with the other options in order to
[09:29:51] understand everything about the frame.
[09:29:52] So we redefine like this rows between
[09:29:55] unbounded preceding and the current row.
[09:29:57] So unbounded preceding going to be the
[09:29:59] first row in the table or in the window.
[09:30:01] So it's going to be static like this.
[09:30:03] It's going to be the first one January.
[09:30:04] And let's say that we are at this
[09:30:07] current row in March. So the window or
[09:30:10] the subset going to look like this.
[09:30:12] Those three rows and the total of that
[09:30:14] going to be 60. So now as SQL is
[09:30:16] proceeding to the next one, it's going
[09:30:18] to fix the first boundary. So it's going
[09:30:20] to be always pointing to January and the
[09:30:22] subset going to be a little bit bigger
[09:30:24] until we reach the last one. And with
[09:30:27] that we're going to have the subsets the
[09:30:30] whole rows. So with that we get really
[09:30:32] great flexibility on how to define the
[09:30:34] subset and how the subset is shifting
[09:30:37] through the window. Okay, so now we are
[09:30:38] just having fun. So we are just playing
[09:30:40] around with the boundaries. We don't
[09:30:41] have always to use the current row. So
[09:30:43] we can use for example here in this
[09:30:45] definition row is between one proceeding
[09:30:48] and one following. So we don't include
[09:30:50] at all the current row in the
[09:30:51] boundaries. So let's say again our
[09:30:54] current row going to be in March. So one
[09:30:56] preceding going to be February and one
[09:30:59] following going to be April. So with
[09:31:01] that our frame going to be the three
[09:31:03] rows. And let me get it like this. And
[09:31:06] the aggregation of this going to be
[09:31:08] around 45. So with that as you can see
[09:31:10] the boundary is going to be one
[09:31:11] proceeding and one following. So it
[09:31:13] should not be always the current row.
[09:31:15] All right. So now I think you already
[09:31:16] get it. What going to be the last
[09:31:17] option? We're going to have everything.
[09:31:19] So the definition of the frame going to
[09:31:21] be rows between unbounded preceding and
[09:31:23] unbounded following. What we're going to
[09:31:25] have over here. The unbounded preceding
[09:31:27] going to be January and the unbounded
[09:31:29] following going to be June. And now the
[09:31:32] frame going to be everything all the
[09:31:34] rows. And it doesn't matter where are we
[09:31:37] with the current row, it's going to be
[09:31:38] always a fixed subsets. So it's going to
[09:31:41] be always everything. So if we are over
[09:31:43] here or February or March, we're going
[09:31:45] to be considering all rows and the total
[09:31:48] sales of that going to be 135. So we
[09:31:52] will get the exact same results for
[09:31:54] everything for all rows. So with that I
[09:31:57] think it's not that complicated, right?
[09:31:59] We just have to provide the boundaries
[09:32:01] and then the calculation going to be
[09:32:02] depending on the frame on the subset of
[09:32:05] data. Okay guys, so now let's go back to
[09:32:07] SQL and start practicing in order to
[09:32:09] understand how the frame work. So let's
[09:32:11] go and define a window like this. So sum
[09:32:14] of sales and the window definition like
[09:32:17] this. We going to divide the data by
[09:32:19] order status and let's say we're going
[09:32:22] to sort it by order date. And let's
[09:32:26] define a frame like this. rows between
[09:32:29] current row and two following. Let's
[09:32:33] give it a name total sales. So let's go
[09:32:36] and execute it. So now let's look to the
[09:32:38] data. You see that SQL going to divide
[09:32:40] our results into two sections, two
[09:32:42] windows delivered and shipped. And you
[09:32:45] can see that the data is sorted by the
[09:32:47] order date. So as you can see over here
[09:32:49] for example in this status delivered we
[09:32:52] can see that 1 of January 10 and so on.
[09:32:56] And then the third part we have defined
[09:32:57] a frame in each window. So for example,
[09:33:00] let's take the first one. So this is the
[09:33:02] current row. So we say the frame is
[09:33:04] between the current row and the two
[09:33:06] following orders. So that means the
[09:33:08] scope going to be like this. So 10 + 20
[09:33:12] 25 it's going to be 55. And now what is
[09:33:14] interesting as well to check here is the
[09:33:16] last record of each window. So now let's
[09:33:19] take this window over here and the last
[09:33:21] record going to be number seven. So this
[09:33:23] order and let's say this is the current
[09:33:25] record. So we set the frame between
[09:33:28] current record and the two following.
[09:33:30] But since it is the last record of this
[09:33:33] window, it will not go and consider the
[09:33:35] next two orders because those two orders
[09:33:38] are outside of the window and that's why
[09:33:40] we have here 30 and SQL doesn't go and
[09:33:43] summarize all those value. So we have it
[09:33:45] 30 and there is nothing after that.
[09:33:48] That's why we will get 30. So as you can
[09:33:51] see the frame going to be calculated
[09:33:52] within one window. So it will not
[09:33:55] consider anything outside of the window.
[09:33:57] So this is how the frame works within
[09:34:00] partitions. So now I would like to show
[09:34:01] you as well a few stuff about the
[09:34:03] frames. We can use shortcuts but we can
[09:34:05] use them only with the proceeding. So
[09:34:08] for example let's say I'm going to
[09:34:09] change the definition like this to
[09:34:12] proceedings and current row. So let's go
[09:34:15] and execute it and we will get those
[09:34:17] results. So now if you want to check the
[09:34:18] results quickly, let's take for example
[09:34:20] this order over here and we are always
[09:34:23] summarizing the values of the two
[09:34:25] previous orders. So that means those
[09:34:28] three orders going to be involved in the
[09:34:29] frame and the output going to be 55. So
[09:34:32] now there is a shortcut for SQL but only
[09:34:34] for the proceeding where we can remove
[09:34:37] the range. So we can go and remove
[09:34:39] everything and we can leave it like this
[09:34:42] rows to proceeding and if you go and
[09:34:44] execute it we will get exact results. So
[09:34:46] this is a quick way or a shortcut on how
[09:34:48] to define a window but it only works
[09:34:50] with the proceeding. So for example, if
[09:34:52] I go over here and say for example
[09:34:55] unbounded it's going to work. So we will
[09:34:57] get the results between the unbounded
[09:35:00] proceeding and the current row. But if
[09:35:02] you go over here and you say you know
[09:35:03] what let's have the unbounded following
[09:35:05] SQL going to say there's an error. And
[09:35:07] the same thing if you remove the
[09:35:08] unbounded let's say for example one
[09:35:10] following SQL will not like it. So you
[09:35:12] can use the shortcut only with the
[09:35:15] proceeding. And one last thing about the
[09:35:17] frames it does there is a default frame.
[09:35:20] So if you don't use any frame and you
[09:35:22] use order by what can happen SQL going
[09:35:24] to use a default frame. So if you check
[09:35:27] the result you will notice that for this
[09:35:29] window over here those values are not
[09:35:31] like the whole values of the sales.
[09:35:33] There is like frame there is hidden
[09:35:35] frame and the default frame in SQL going
[09:35:37] to be like this rows between unbounded
[09:35:41] preceding and current row. So this is
[09:35:44] the default frame if you use order by.
[09:35:47] So now if you go and just execute it you
[09:35:49] will see that we will get the exact
[09:35:51] results. So be careful once you use
[09:35:53] order by with the aggregate functions
[09:35:55] there will be a hidden frame or a
[09:35:57] default frame like this between the
[09:35:59] unbounded proceeding and the current
[09:36:01] row. So that means there are three ways
[09:36:02] in order to do this scenario framework
[09:36:04] between unbounded proceding and current
[09:36:06] row. Either write it like this or you
[09:36:08] can go and have a shortcut like this.
[09:36:11] Let me just execute it. So we'll get the
[09:36:13] same result or just remove it
[09:36:15] completely. We will get as well the same
[09:36:17] results. Now again the hidden frame or
[09:36:19] the default frame is only working with
[09:36:21] the order by. So if you go for example
[09:36:23] here and remove the order by let's see
[09:36:26] the results. The whole window will be
[09:36:28] aggregated. So again let me just select
[09:36:30] it. So you can see that SQL going to
[09:36:32] consider all the rows in the
[09:36:34] aggregations and we will get the total
[09:36:36] sales for the whole window. So there
[09:36:38] will be no frame defined only it's going
[09:36:40] to be present once you use order by. All
[09:36:42] right friends so with the frame closed
[09:36:44] we have now covered all the components
[09:36:46] on how to define a window inside an
[09:36:49] overclo and with that we have covered
[09:36:51] everything about the syntax of the
[09:36:54] window functions.
[09:36:59] Okay guys, so now we're going to go and
[09:37:00] understand the rules or let's say the
[09:37:02] limitations of window functions. So
[09:37:04] let's learn what you are not allowed to
[09:37:06] do while using window functions. Okay,
[09:37:08] so the first rule that you are allowed
[09:37:10] to use the window function only in the
[09:37:13] select close and as well in the order by
[09:37:16] clause. So here we have again the same
[09:37:17] example where we finding the total sales
[09:37:19] by the order status. So as you can see
[09:37:21] we used the window function in the
[09:37:23] select clause and we didn't get any
[09:37:25] error right. So now we can go and use it
[09:37:28] as well in the order by. So let's say
[09:37:30] order by and let's go and copy
[09:37:32] everything but not the name in the order
[09:37:35] by. So if I go and execute this there
[09:37:37] will be no errors and SQL going to allow
[09:37:39] it. And as you can see the result didn't
[09:37:41] change. So let's go and sort it for
[09:37:42] example descending. So I'm going to
[09:37:44] write here descending and let's execute.
[09:37:47] Now we have the total sales with the
[09:37:49] highest values then the lowest values.
[09:37:51] So having this rule that we can use it
[09:37:52] only in select and order by that means
[09:37:54] we cannot use window functions in order
[09:37:57] to filter data. So let me show you for
[09:38:00] example instead of order by let's have
[09:38:03] clause where the total sales let's say
[09:38:06] bigger than 100. So let's go and execute
[09:38:08] this. And as you can see XQL going to
[09:38:10] say no you are not allowed to do that.
[09:38:12] You can do that only for select and
[09:38:13] order by. We are not allowed to use it
[09:38:16] for filtering data using the wear clause
[09:38:18] and as well you are not allowed to use
[09:38:20] it in the group by. So if I go and do a
[09:38:22] group by and as well remove the
[09:38:24] condition over here. So if you execute
[09:38:26] it you're going to get the same error.
[09:38:28] You are not allowed to use the window
[09:38:29] function in the group by. So only with
[09:38:32] the order by or as well in the select
[09:38:35] clause. Okay. So now to the second rule.
[09:38:37] You cannot use window functions inside
[09:38:39] another window function. So that means
[09:38:41] you cannot go and nest window functions
[09:38:44] together. Let me show you what I mean
[09:38:45] with that. So let's remove the group
[09:38:47] pie. Now everything should be working.
[09:38:50] Let's take and copy the whole window
[09:38:51] function over here and let's just nest
[09:38:53] it. So instead of sales, we're going to
[09:38:56] have now window function inside another
[09:38:58] window function. So as you can see this
[09:39:00] is the inner window function and the
[09:39:03] rest the outside is the outside window
[09:39:06] function. So if I go and execute this
[09:39:08] you will see that scale going to tell us
[09:39:10] you cannot use the window function in
[09:39:12] the context of another window function.
[09:39:14] So we cannot do nesting using window
[09:39:17] functions. So as you can see this is
[09:39:18] another limitation for those functions.
[09:39:20] All right moving to the third rule or
[09:39:22] let's say an info the window function
[09:39:24] will be executed after filtering the
[09:39:27] data with the work clause. Let's have an
[09:39:29] example. So okay so now let's say that I
[09:39:31] would like to have the same
[09:39:31] informations. the total sales for each
[09:39:33] status but only for two products 101 and
[09:39:36] 102. So let's go and do that. We're
[09:39:39] going to use the wear clause and then
[09:39:41] we're going to say product ID in we're
[09:39:43] going to specify 101 and 102. So let's
[09:39:47] go and execute this. Now you can see we
[09:39:49] still have two partitions. So one for
[09:39:51] the delivered and one for the shipped
[09:39:53] but the total sales is reduced because
[09:39:55] we are only focusing on two products and
[09:39:58] we filtered the whole data sets. So how
[09:40:00] SQL works? First the workflow is going
[09:40:02] to be executed and then the window
[09:40:05] functions going to be calculated. So
[09:40:07] that means first filtering and then
[09:40:08] aggregations. Okay guys, now we're going
[09:40:10] to move to the last rule to the most
[09:40:11] interesting one and it says the
[09:40:13] following. You are allowed to use the
[09:40:15] window function together with the group
[09:40:17] by clause only if you use the same
[09:40:19] columns. So let me explain what do I
[09:40:21] mean but first some coffee.
[09:40:26] Let's have the following task and it
[09:40:28] says rank the customers based on their
[09:40:30] total sales. So now it sounds really
[09:40:33] easy but if you check it you have here
[09:40:35] two calculations. The first one you have
[09:40:37] to rank the customers and the second
[09:40:39] calculation is an aggregation. You have
[09:40:42] to find the total sales for each
[09:40:43] customers. Okay. So now I'm going to
[09:40:45] show you step by step how I usually
[09:40:46] solve those tasks. So for now let's
[09:40:49] check the total sales. It is an
[09:40:51] aggregation right? So we can use the sum
[09:40:53] function and this function is available
[09:40:55] in both group pi and as well in the
[09:40:57] window function. So for now I'm going to
[09:40:59] go with the group by and that's because
[09:41:01] the task is very simple. We don't have
[09:41:03] to show any other details. Right? So
[09:41:05] it's all about aggregations. So why not
[09:41:07] using the group by and now to the first
[09:41:09] part where we have to rank the
[09:41:11] customers. We cannot use the rank
[09:41:13] function with the group by right. Groupy
[09:41:15] uses only aggregations. So here we are
[09:41:17] forced to use the window function. So
[09:41:19] that means for the rank I'm going to use
[09:41:21] window function. For the total sales I'm
[09:41:23] going to use a group by. So now let's do
[09:41:25] it step by step. So first we have to
[09:41:27] find the total sales for each customer
[09:41:30] using group by. It's very simple. So I'm
[09:41:32] just going to remove all those stuff in
[09:41:34] our select statements. We need the
[09:41:36] customer ID and then we don't need a
[09:41:39] window function over here. And then
[09:41:41] after the from we're going to have a
[09:41:43] group by customer ID. So now I'm just
[09:41:45] grouping the customers and finding the
[09:41:47] sum of all sales. Let's go and execute
[09:41:50] this. So now as you can see in the
[09:41:51] results we have four customers and
[09:41:53] that's why we have four rows and as well
[09:41:55] we have the total sales. So let's say
[09:41:57] the half of the tasks is already solved.
[09:42:00] Right now what is missing that we need a
[09:42:03] rank. So let's go and build that. The
[09:42:05] second step we're going to use the rank
[09:42:08] function and we can define a window for
[09:42:09] that. So over and inside it will not
[09:42:12] partition the data at all because it's
[09:42:13] already like grouped up. So what we're
[09:42:16] going to do over order by the rank
[09:42:18] function always needs an order by don't
[09:42:20] worry about it we can talk about it
[09:42:21] later. So now we are ranking the data
[09:42:23] based on the total sales that means the
[09:42:25] sum of sales. So what we're going to do
[09:42:27] let's just go and copy this and put it
[09:42:30] after the order buy. And now we have to
[09:42:31] decide whether ascending or descending.
[09:42:33] It's going to be descending. So the
[09:42:34] highest sales first and then the lowest
[09:42:36] sales. So now as you can see we have now
[09:42:38] a rank customers and we have a window
[09:42:41] function now together with the group by.
[09:42:44] So now let's go and execute this and see
[09:42:46] whether SQL going to allow it. So let's
[09:42:48] run it and as you can see SQL runs it
[09:42:50] and we will get the rank for each
[09:42:52] customers. So the customer three has the
[09:42:55] highest total sale. Then the customer
[09:42:57] number one and the last one going to be
[09:42:59] customer number two with the lowest
[09:43:00] total sales. All right. So we solve the
[09:43:03] tasks. We have now ranked the customers
[09:43:05] based on their total sales. So as you
[09:43:07] can see SQL allows you to use window
[09:43:09] function together with the group by but
[09:43:11] only with one rule. Anything that you
[09:43:13] are using inside the window function
[09:43:15] should be part of the group I. So for
[09:43:18] example, we fulfilled the rule because
[09:43:19] we are using the sum of sales and the
[09:43:21] sum of sales is a part of the group I
[09:43:24] right. So now if I go I just break the
[09:43:26] rule by not using the sum just using the
[09:43:28] sales. So if I just remove the sum and
[09:43:31] use only the sales, SQL will not allow
[09:43:33] it because the sales is not part of the
[09:43:36] group I. So as you can see SQL is very
[09:43:38] strict with this. If you want to use
[09:43:39] everything in one query without using
[09:43:42] like subqueries and so on, you have to
[09:43:44] use the exact same columns. So for
[09:43:46] example, if I go over here instead of
[09:43:48] sales, I use the customer ID. So since
[09:43:50] the customer ID is a part of the group
[09:43:53] by, SQL can allows it. So be careful
[09:43:55] using window function together with the
[09:43:57] group by. As long as you are using the
[09:43:59] same columns, nothing going to go wrong
[09:44:01] and SQL going to allows it. Okay, so now
[09:44:03] I'm just going to go and fix this and
[09:44:06] let's run it. So now as you can see it's
[09:44:08] really easy if you follow those steps.
[09:44:10] First build the query using group by. So
[09:44:12] don't you think about the window
[09:44:13] function just build the group by and
[09:44:15] then the next step the last one you go
[09:44:18] and define and build the window
[09:44:19] function. So with that you can solve
[09:44:21] really nice analytical use cases with a
[09:44:23] simple one query without having you to
[09:44:25] build like some queries and so on. You
[09:44:27] can go and use group by together with
[09:44:29] the window functions. All right guys so
[09:44:31] those are the four rules for the SQL
[09:44:33] window functions.
[09:44:37] All right friends, so now let's have a
[09:44:39] quick recap about the SQL window
[09:44:41] functions. Let's start with the
[09:44:42] definition. It will go and perform
[09:44:44] calculations like aggregations on top of
[09:44:47] subset of data without losing the level
[09:44:49] of details. So that means we can do
[09:44:51] aggregations and at the same time we are
[09:44:54] not losing the details. Now, of course,
[09:44:56] there is a lot of similarity between the
[09:44:57] window function and the group by. But
[09:44:59] the main difference is that window
[09:45:01] functions are very powerful and dynamic
[09:45:04] compared to the group by. We have way
[09:45:06] more functions than the group by. Right?
[09:45:08] But now if you are doing data analyzes
[09:45:10] and you have an advanced use case, then
[09:45:12] you have to go and use window function.
[09:45:14] It's more suitable for complex and
[09:45:16] advanced data analyzes. But in the other
[09:45:19] hand if you have a simple question
[09:45:21] simple data analyzes then you can go and
[09:45:23] use the aggregate functions using the
[09:45:25] group by and of course you can go and
[09:45:27] use them in the same query in the same
[09:45:29] select you can go and mix the group by
[09:45:32] together with the window function with
[09:45:34] only one rule you have to use the same
[09:45:36] columns and of course the first step is
[09:45:38] to do the group by and then later you do
[09:45:40] the window function in the same query.
[09:45:42] And now to the next point about the
[09:45:44] window components we have two main
[09:45:46] components. The first one is the window
[09:45:49] function and the second part is the
[09:45:51] window definition using the over close.
[09:45:53] And inside the overlo we can define
[09:45:55] three things. If you want to divide the
[09:45:57] data to create windows you can use the
[09:46:00] partition by the second section we have
[09:46:02] the order by in order to sort your data.
[09:46:05] And the last part you can go and specify
[09:46:07] a subset of data like a frame within
[09:46:10] each window. Now let's move to the last
[09:46:12] part. We have rules for the SQL window
[09:46:14] functions. So the first thing is that if
[09:46:16] you have two window functions or
[09:46:18] multiple window functions, you cannot go
[09:46:20] and nest them together. You have to go
[09:46:22] and use multiple subqueries. The next
[09:46:25] point is that you can use the window
[09:46:27] function only in the select and the
[09:46:29] order by clause. So for example, you
[09:46:32] cannot use the window together with the
[09:46:33] wear clause in order to filter the data.
[09:46:36] Talking about filtering data, how SQL
[09:46:38] going to go and execute the window
[09:46:40] function? It's always after SQL filter
[09:46:43] the data. All right. So those are the
[09:46:44] basic stuff about the SQL window
[09:46:46] function. So with that we have learned
[09:46:47] the basics about the window functions in
[09:46:49] SQL. And next we're going to start
[09:46:51] talking about the functions. So the
[09:46:53] first group is the window aggregate
[09:46:54] functions. And here we're going to learn
[09:46:56] how to summarize our data for a specific
[09:46:58] group of rows. So let's
[09:47:02] go. Okay guys, let's say that in our
[09:47:05] data we have the following informations.
[09:47:07] We have the months and the sales. Now if
[09:47:10] you apply any aggregate functions in SQL
[09:47:12] what going to happen SQL going to go
[09:47:14] through all rows of the window or the
[09:47:16] entire data and start aggregating the
[09:47:18] data. So that means in the result in the
[09:47:20] output SQL going to give you one single
[09:47:23] aggregated value. SQL going to go and
[09:47:26] summarize all those values and in the
[09:47:28] output you're going to find for example
[09:47:29] here the total sales it's going to be
[09:47:32] 175 or you can use the average or count
[09:47:34] the data and so on. So the aggregate
[09:47:36] functions going to deliver at the end
[09:47:39] one aggregated value for a window or for
[09:47:42] the entire data. Okay. So now let's have
[09:47:43] a quick overview of the syntax of all
[09:47:45] aggregate functions. Most of them follow
[09:47:48] the same rule. So first as usual we have
[09:47:50] to define the function name. And in this
[09:47:52] example we have the average. Then to the
[09:47:53] next part we have to define inside it as
[09:47:56] well the expression. We cannot leave it
[09:47:58] empty. So here we are using the sales
[09:48:00] and the second rule for all functions
[09:48:02] beside the count. The data type of this
[09:48:05] field should be a number. And this of
[09:48:07] course makes sense, right? So we cannot
[09:48:09] find the average of the first name of
[09:48:11] customers or something like that. So we
[09:48:13] have to define a number. Then next we
[09:48:14] have to define the frame. So we have the
[09:48:16] partition pi and it is optional. So you
[09:48:19] could use it or leave it depends. And
[09:48:21] then the next one we have the order by.
[09:48:22] It is as well optional. It is not a must
[09:48:25] or required. So you could use it or
[09:48:27] leave it. That mean the whole definition
[09:48:29] of the window could be empty for the
[09:48:31] aggregate functions. Let's have a look
[09:48:32] to all functions. So we have the count,
[09:48:35] sum, average, min, max. And as you can
[09:48:37] see, only the count accepts all data
[09:48:39] types as an expression or arguments. All
[09:48:42] others require you to have a number as a
[09:48:44] data type. And for all functions, the
[09:48:46] partition by is optional. The same for
[09:48:48] order by and frame. So everything is
[09:48:50] optional over here. So now what we're
[09:48:52] going to do with that, we're going to go
[09:48:53] and deep dive into each of those
[09:48:54] functions in order to understand how
[09:48:56] they work, what are the use cases, and
[09:48:59] of course, we're going to practice in
[09:49:00] SQL. So we're going to start with the
[09:49:02] first one with the function
[09:49:07] count. Okay. So what is the count
[09:49:09] function? It's really simple. It's going
[09:49:10] to return the number of rows within each
[09:49:13] window. So it's going to help you to
[09:49:14] understand how many rows do you have
[09:49:16] within each subset of data. So now let's
[09:49:18] go and understand how SQL works with
[09:49:21] this function. All right guys, so now we
[09:49:22] have again this very simple example for
[09:49:24] the orders and we have the following
[09:49:26] informations. We have the products and
[09:49:27] sales and now we want to solve very
[09:49:29] simple task. How many orders do we have
[09:49:32] within each products? So in order to
[09:49:34] solve it, we can use the function count
[09:49:36] like the following. So we can say count
[09:49:38] and then we pass for it an argument or
[09:49:41] expression the star. So with that we are
[09:49:43] telling SQL go and count how many rows
[09:49:45] do we have in our table. But we have a
[09:49:48] window definition like this over
[09:49:50] partition by products. So now what SQL
[09:49:52] going to do? We're going to go and
[09:49:53] divide the data sets into two
[09:49:55] partitions. We're going to have one
[09:49:56] partition for the caps and another one
[09:49:59] for the gloves. So with that we have
[09:50:01] prepared our data into windows and we
[09:50:03] are ready to do aggregations. So how
[09:50:04] many rows do we have within each window?
[09:50:06] It's going to be three. So for this
[09:50:07] window it's going to be three rows and
[09:50:10] as well for the next window we have as
[09:50:11] well three rows. So we're going to have
[09:50:13] three three and three. It's very simple
[09:50:16] right guys? We are just finding the
[09:50:18] number of rows within each window. But
[09:50:20] now with the aggregate functions we have
[09:50:21] to be very careful with the null values
[09:50:24] for the count star. As you can see over
[09:50:26] here we are not specifying anything
[09:50:28] about the sales. So we are just saying
[09:50:30] find me the number of rows. So that
[09:50:32] means SQL will just count the nulls as
[09:50:34] one row. So that means if we are using
[09:50:36] the star as an argument for the function
[09:50:38] count the null will not affect anything.
[09:50:41] So whether we have nulls or nots we are
[09:50:43] just counting how many rows do we have
[09:50:45] inside our data. But in some scenarios,
[09:50:47] we should be ignoring the nulls in our
[09:50:49] account. For example, let's say that I
[09:50:51] would like to count how many sales do we
[09:50:53] have within each product. That means if
[09:50:56] we have nulls, it should not be counted.
[09:50:58] So now in order to achieve this task,
[09:51:00] what we're going to do, we're going to
[09:51:02] use instead of a star over here, we're
[09:51:04] going to have the filled sales. So now
[09:51:06] with this, we are telling SQL, don't
[09:51:08] just count blindly how many rows do we
[09:51:10] have within each window. You should be
[09:51:12] very careful with the values. Find how
[09:51:15] many cells do we have within each
[09:51:17] window. So now let's see what's going to
[09:51:19] happen. For the first window we have
[09:51:21] three cells. So we have three values. So
[09:51:23] the number of rows is correct. But for
[09:51:25] the next one, how many cells do we have?
[09:51:27] We have two. So we have this sale and
[09:51:30] then the 70. But the last one is null.
[09:51:32] So it will not be counted. It will be
[09:51:34] ignored. That's why we're going to get
[09:51:36] in the output the value two. We have two
[09:51:39] sales. So as you can see the result did
[09:51:41] change and we are now more sensitive to
[09:51:43] the null values. So be careful what you
[09:51:45] are specifying for the count. If you are
[09:51:47] using a column name like this it will
[09:51:49] ignore the nulls. But if you have a star
[09:51:52] it just going to go and find how many
[09:51:54] rows do we have within each partition.
[09:51:56] Okay. So now if you go and compare the
[09:51:57] results side by side you can see that if
[09:51:59] you specify a column within the count
[09:52:01] function it's going to be sensitive with
[09:52:03] the nulls. So it's going to ignore it
[09:52:05] and will not use it within the
[09:52:07] aggregations. That's why we have here
[09:52:08] only two rows. But if you go and use the
[09:52:11] star within the count function, what
[09:52:13] going to happen? SQL just going to go
[09:52:15] and count it. So we're going to find the
[09:52:17] number of rows that we have inside our
[09:52:18] table. And there is one more way in
[09:52:20] order to do the same thing here on the
[09:52:22] left side. You can use instead of star
[09:52:24] you can use one. So you might find it
[09:52:26] somewhere that people are using count
[09:52:29] one and then the same window function
[09:52:31] and we will get exactly the same result.
[09:52:33] So the nulls will be counted and will
[09:52:35] not be ignored. So now you might ask me
[09:52:37] which one should I use the one or the
[09:52:38] star? Well, I would say it doesn't
[09:52:41] matter right we are getting the same
[09:52:42] results and if you are thinking about
[09:52:43] the performance I hardly find any
[09:52:46] differences between them so you can go
[09:52:47] and try both of them and stick with the
[09:52:49] one that is giving you like more better
[09:52:52] performance. Now we have special case
[09:52:53] for the count function compared to all
[09:52:55] other aggregate functions it allows any
[09:52:57] data type. So that means we can use
[09:53:00] numbers we can use characters dates and
[09:53:02] so on. So that means we can go and
[09:53:03] specify something like the product for
[09:53:05] the count instead of sales. So we can go
[09:53:07] over here and say product and it's going
[09:53:09] to go and count how many rows do we have
[09:53:10] for the product. So it's going to be
[09:53:12] three over here. And since here we don't
[09:53:14] have any nulls, it's going to go and
[09:53:16] count it like this. So we have three
[09:53:17] rows and be careful here. We are not
[09:53:19] counting the unique rows. We are just
[09:53:21] counting the rows that we have inside
[09:53:23] our data. So this will not be counted as
[09:53:25] one and this as well would not be one.
[09:53:28] So we have three times the caps. That's
[09:53:30] why we have here three. Okay. Okay. So
[09:53:31] now we have this very simple example.
[09:53:33] Find the total number of orders. This is
[09:53:35] very simple task in order to find how
[09:53:37] many rows, how many records do we have
[09:53:40] inside the table orders. So let's go and
[09:53:43] solve it. So let's start by selecting
[09:53:45] just star from the table orders without
[09:53:48] anything like this. So as you can see we
[09:53:50] have 10 orders. It's very simple. It's
[09:53:52] very easy as well. But now let's say
[09:53:54] that you have thousands or millions of
[09:53:56] rows. You cannot do it like this by just
[09:53:57] checking the rows. What you're going to
[09:53:59] do? We're going to go and use the
[09:54:00] function count. So we can go over here
[09:54:02] and say counts star and then let's give
[09:54:05] it a name total orders. So let's go and
[09:54:09] execute it. So now as you can see we got
[09:54:10] only one record, one value. We don't see
[09:54:13] any other details. We got the 10 orders.
[09:54:16] So this is the total number of orders.
[09:54:18] This is very helpful in order to
[09:54:19] understand the content of your data. So
[09:54:21] this we call it overall analyzes or
[09:54:24] let's say having the big numbers about
[09:54:26] your business. For example, how many
[09:54:28] orders do we have? how many customers,
[09:54:30] products, employees and so on. So having
[09:54:32] those big numbers going to help us to
[09:54:34] track our business to understand how
[09:54:36] well we are doing with the orders and
[09:54:39] with the customers and so on. So this is
[09:54:41] the basics of reporting. Now let's go
[09:54:43] and extend our task by saying provide
[09:54:46] details such as the order ID and the
[09:54:48] order dates. So let's go and do that. So
[09:54:51] select order ID, order dates. And now of
[09:54:54] course we cannot do it like this. So let
[09:54:56] me just execute it. we will get an error
[09:54:58] because here we have different level of
[09:55:00] details in our select. So in order to
[09:55:02] solve this what we going to do we're
[09:55:04] going to use the over clause and with
[09:55:07] that we are telling SQL this is a window
[09:55:09] function. So now let's go and execute
[09:55:11] it. So with that you can see with that
[09:55:12] we have solved the task we have details
[09:55:14] we have the order ID order dates. So
[09:55:16] this is the highest level of details
[09:55:19] since we have the order ID and as well
[09:55:21] we have the highest level of
[09:55:22] aggregations. we have the total number
[09:55:24] of orders in the entire table orders. So
[09:55:27] now let's keep going and add more stuff
[09:55:29] to our task. Let's say that we want to
[09:55:31] find the total number of orders but for
[09:55:34] each customers. So that means this time
[09:55:36] we have to go and divide our data by the
[09:55:39] customers. So let's go and do that.
[09:55:41] We're going to use as well a window
[09:55:42] function. So count star over we have to
[09:55:45] divide the data using partition by and
[09:55:48] we're going to use the field customer
[09:55:50] ID. So let's call it orders by customers
[09:55:54] and I would like to see as well the
[09:55:55] customer informations in the query.
[09:55:57] That's why I'm going to go and add it.
[09:55:59] All right. So that's all. Let's go and
[09:56:00] execute it. Now as we learned before
[09:56:02] that SQL first going to go and divide
[09:56:04] the data. So that means we have four
[09:56:06] customers. We're going to get four
[09:56:07] windows. The first window going to be
[09:56:09] for the customer ID number one. And as
[09:56:11] you can see we have three rows. That's
[09:56:13] why we have here three orders. And the
[09:56:15] same thing for the customer two. We have
[09:56:17] three orders. customer three three
[09:56:19] orders but only the last customer the
[09:56:22] customer ID number four we have only one
[09:56:24] row and one order. So now if you go and
[09:56:25] look to the total orders and the orders
[09:56:27] by customers you can see now we are not
[09:56:29] doing the overall analyzes we are doing
[09:56:32] like comparison between different
[09:56:34] categories and of course in this example
[09:56:35] the category is the customers and with
[09:56:38] that we can understand as well the
[09:56:39] behavior of our customers. So you can
[09:56:42] see that we have three customers that
[09:56:44] has exactly the same amount of orders.
[09:56:47] So they are very similar but we have one
[09:56:49] extreme which is the customer ID number
[09:56:51] four. This customer has only one order.
[09:56:54] So this is the only customer that has
[09:56:56] different behavior than all other
[09:56:58] customers. So you see with very simple
[09:57:00] query we are able now to analyze our
[09:57:02] business and understand the behavior of
[09:57:04] our customers. So if you divide the data
[09:57:07] by partition by and using count you can
[09:57:10] go and now compare stuff together. All
[09:57:12] right. So now let's keep moving. Next
[09:57:13] we're going to understand the special
[09:57:15] cases that we have with the function
[09:57:17] count. So now we have this very simple
[09:57:19] task. It says find the total number of
[09:57:21] customers and additionally we have to
[09:57:23] provide all customers details. So I
[09:57:25] think it's very easy to solve. What
[09:57:27] we're going to do we're going to go and
[09:57:28] select star since we need all details
[09:57:30] from customers from sales customers. So
[09:57:34] let's just have a look. So we have five
[09:57:36] customers and the function is count star
[09:57:39] over and we don't have to divide the
[09:57:41] data since we have to find the total
[09:57:43] number of customers for the entire table
[09:57:45] and it's going to be total customers. So
[09:57:47] nothing new that's it we have five
[09:57:50] customers and now as we learned before
[09:57:52] if you are passing the star to the count
[09:57:54] function what you are telling to escale
[09:57:56] is that just go and count how many rows
[09:57:58] do we have inside the table customers.
[09:58:00] So SQL just going to go and start
[09:58:02] counting and going to say we have five
[09:58:04] customers, five rows. So it doesn't
[09:58:06] matter whether we have nulls inside our
[09:58:08] data like in the last name or the score.
[09:58:10] It's just going to count the number of
[09:58:12] rows. So now let's say that we have the
[09:58:13] following task. It's going to say find
[09:58:16] the total number of scores for
[09:58:18] customers. So what do we need with this
[09:58:19] task is to find out how many scores
[09:58:21] inside our data. So as you can see we
[09:58:23] have around four scores but the last
[09:58:26] customer doesn't have any score. So we
[09:58:28] have it as a null. So the result should
[09:58:30] be four. We cannot go now and use the
[09:58:32] star for it because we're going to get
[09:58:34] five. We have to go and count the
[09:58:35] scores. So let's see how we're going to
[09:58:37] do that. We're going to count as well.
[09:58:39] But this time the score and the
[09:58:42] definition of the window going to be
[09:58:43] empty. So total scores and let's go and
[09:58:46] execute this. So now we can see in the
[09:58:48] results we got four scores which is very
[09:58:50] correct because SQL did ignore the null
[09:58:53] and SQL now focusing only on one column.
[09:58:56] So focusing on those values the nulls
[09:58:58] will not be counted. This is really
[09:59:00] great in order to check the quality of
[09:59:01] your data. So let's say that you are not
[09:59:03] expecting any nulls inside your data. So
[09:59:06] instead of going manually through the
[09:59:07] whole records what you can do you can go
[09:59:09] and find the total number of customers
[09:59:11] like this and then you can go and count
[09:59:14] the total number of scores and you can
[09:59:16] see there is a difference. So by just
[09:59:18] checking the data I can say you know
[09:59:20] what we have one null without checking
[09:59:23] every record in our data. So with that
[09:59:25] we can check the quality of our data and
[09:59:27] understand very quickly how many nulls
[09:59:30] do we have in the field score and you
[09:59:32] can do the same stuff for example for
[09:59:33] the first name show it to you. So I'm
[09:59:36] just going to go and copy this and let's
[09:59:37] say first name or let's say country
[09:59:40] actually. So I will go with the country.
[09:59:42] So let's go with the country total
[09:59:45] countries. So let's go and execute this.
[09:59:48] So now if you check the result you can
[09:59:49] see we have five rows with the
[09:59:51] countries. So SQL going to go and focus
[09:59:53] on the countries and it will not find
[09:59:54] any nulls. So we have here complete
[09:59:57] data. We don't have any nulls because
[09:59:59] the total number of customers is equal
[10:00:01] to the total number of values within the
[10:00:03] country. And I can immediately find okay
[10:00:05] the data quality of the country is very
[10:00:08] good. All right. So now one more thing
[10:00:09] about the count function that we have
[10:00:10] learned before. We can use either star
[10:00:13] or one in order to count how many rows
[10:00:15] do we have. So let's just try it. I'm
[10:00:17] just going to go and duplicate it. And
[10:00:19] instead of having a star, let's have a
[10:00:21] one. Just going to give it a name here.
[10:00:23] It's going to be one and you are star.
[10:00:25] So let's go and execute it. So now if
[10:00:27] you check the output, we got exactly
[10:00:30] identical results. So there is no
[10:00:32] difference between those two queries.
[10:00:34] It's up to you. You can try it and check
[10:00:36] the performance. I usually go with the
[10:00:38] star instead of one. Okay. So now we're
[10:00:39] going to talk about a very important use
[10:00:41] case for the SQL window function count
[10:00:43] that I frequently use in my real
[10:00:45] projects. The data that we use for data
[10:00:47] analyzes has usually bad data quality.
[10:00:51] And if we don't find those data quality
[10:00:53] issues and we don't clean it before
[10:00:54] doing the analyzes, what going to
[10:00:56] happen? We're going to deliver bad
[10:00:58] results, bad analyzes which going to
[10:01:01] lead to bad decisions. And one very
[10:01:03] common data quality issue that you might
[10:01:05] encounter in your project or on your
[10:01:06] data is that having duplicates.
[10:01:08] Duplicates are really bad for doing data
[10:01:10] analyszis. So now in order to discover
[10:01:13] or let's say identify the duplicates in
[10:01:15] our data, we can go and use the SQL
[10:01:18] window function count. So now let's go
[10:01:20] and have some examples. Okay. So now the
[10:01:22] task says check whether the table orders
[10:01:24] contains any duplicate rows. So how we
[10:01:27] going to do that? By checking now the
[10:01:28] table orders over here. We can see that
[10:01:31] there are many orders. But how to find
[10:01:33] out the duplicates? Well, the first step
[10:01:35] is to understand what is the primary key
[10:01:38] of the table orders. So what we usually
[10:01:40] do we go and check the data model if
[10:01:42] there is one. So for example for this
[10:01:44] course we have the following data model
[10:01:46] and we can see that it is defined that
[10:01:48] the order ID is the primary key for the
[10:01:50] orders. The product ID is primary key
[10:01:52] for the products. So that means for our
[10:01:55] table the orders we have the order ID as
[10:01:57] the primary key and it should be unique.
[10:01:59] It should not contain any duplicates. So
[10:02:01] now let's go to our data and check the
[10:02:04] order ID. By just looking at the data
[10:02:06] you can see that we don't have any
[10:02:08] duplicates. Rightes all of them are
[10:02:09] unique. So we have 1 2 3 4 and so on.
[10:02:12] But of course in real projects you
[10:02:13] cannot do it like this. You have to go
[10:02:15] and build a query in order to find out
[10:02:17] whether the primary key is unique. But
[10:02:20] now you might say the primary keys are
[10:02:22] usually unique because we can define it
[10:02:23] in the DDL in the rules of building the
[10:02:26] table. Well that's true. If you have it
[10:02:29] like this then you don't have to find
[10:02:31] any duplicates. But usually in data
[10:02:33] analyzes we export a lot of files and a
[10:02:35] lot of data inside an extra database and
[10:02:37] we don't build such a rules. So now in
[10:02:39] order to check the quality of the
[10:02:41] primary keys that you get from the
[10:02:42] source we can use the count function. So
[10:02:45] let's go and build it. I'm just going to
[10:02:46] select the order ID first as a detail.
[10:02:49] And now we're going to do the following.
[10:02:50] So count and then star. And let's go and
[10:02:53] define the window. So it's going to be
[10:02:56] partition by and here the field going to
[10:02:58] be the primary key. So the order ID I'm
[10:03:01] checking now the quality of this field.
[10:03:04] This should not contain any duplicates.
[10:03:06] And now we're going to go and give it a
[10:03:07] name check primary key. So now my
[10:03:10] expectation is that the result of this
[10:03:13] should be at maximum one. That means we
[10:03:15] have one row for each primary key. And
[10:03:17] that means as well it is unique. So if
[10:03:19] we get anything more than one then it
[10:03:21] means we have duplicates. Let's go and
[10:03:23] run the query. And as you can see in the
[10:03:25] results we get for each primary key one.
[10:03:28] So that's great. That means we don't
[10:03:29] have any duplicates inside our data and
[10:03:32] the primary key is unique. So that means
[10:03:34] the table orders is clean and we don't
[10:03:36] have any duplicates inside it. Now let's
[10:03:39] check our database. We have here another
[10:03:41] table called orders archive. Let's go
[10:03:44] and check the table. So first I'm just
[10:03:46] going to go and select the data. So
[10:03:48] select from orders archive. So sales do
[10:03:51] orders archive. Let's check the results.
[10:03:55] And here we can see that we have exactly
[10:03:57] the same structure as the table orders.
[10:04:00] So now let's go and check whether the
[10:04:01] data quality is well clean. So now what
[10:04:03] we're going to do, we're going to use
[10:04:04] exactly the same query as before, but
[10:04:07] instead of using the table orders, we're
[10:04:09] going to take the orders archive. So
[10:04:11] that's it. Let's go and execute it. So
[10:04:14] now by checking the data, you can see
[10:04:15] that we don't have everywhere one.
[10:04:17] Sometimes we have two rows for the same
[10:04:20] primary key, which is really bad. So we
[10:04:22] have here for the order ID four we have
[10:04:24] two orders with the same order ID and as
[10:04:27] well for this order id six we have three
[10:04:30] orders that means those stuff are
[10:04:32] duplicates and they are against our data
[10:04:35] model. So now what else we can do is
[10:04:36] that to generate a list specifically for
[10:04:38] the data quality issue where we have
[10:04:40] duplicates. So anything that has one we
[10:04:43] are not interested in it. In order to do
[10:04:45] that we're going to use the subquery. So
[10:04:48] let's say select star from and then
[10:04:51] we're going to use the first query as a
[10:04:53] subquery and we're going to say in our
[10:04:55] filter where the check primary key is
[10:04:58] higher than one. So that means I need
[10:05:00] only the order ids where we have
[10:05:02] duplicates. So let's go and execute
[10:05:04] this. Now I have a list with the primary
[10:05:06] keys where we have duplicates. So we
[10:05:08] have the order ID 4 and as well the
[10:05:10] order ID six. So guys, as you can see,
[10:05:12] the window count function is wonderful
[10:05:14] in order to find data quality issues
[10:05:16] like the duplicates. All right guys, so
[10:05:18] those are the four most important use
[10:05:20] cases in the SQL window function count.
[10:05:22] So the first one we can use it in order
[10:05:24] to do overall analyzes or we can use it
[10:05:27] in order to do category analyzes like we
[10:05:29] have done the analyzes on the customer
[10:05:31] behavior or another use case we can use
[10:05:33] it in order to check the nulls inside
[10:05:35] our data. And the last use case we can
[10:05:38] use it in order to identify or discover
[10:05:40] the data quality issue duplicates in our
[10:05:43] data. So now let's go and check the next
[10:05:45] function. We have the
[10:05:46] [Music]
[10:05:49] sum. All right. So now let's understand
[10:05:52] what is the sum function. It's very
[10:05:54] simple. It's going to return the sum of
[10:05:56] all values within each window. So now
[10:05:58] let's go and understand how SQL works
[10:06:00] with this function. All right. So this
[10:06:02] is very easy and we are using the same
[10:06:04] simple example and now we would like to
[10:06:06] find the total sales for each products.
[10:06:08] So we can define like this sum of sales
[10:06:11] since we are finding the total sales and
[10:06:13] then we define the window like this over
[10:06:16] partition by products. So as we learned
[10:06:18] SQL going to go first and divide our
[10:06:20] data into two windows. So one window for
[10:06:22] the caps and another window for the
[10:06:25] gloves right. So now after SQL define
[10:06:27] the windows it's going to go and starts
[10:06:28] aggregating the data. So the sum of
[10:06:31] sales that means for the first window we
[10:06:33] have the three sales and it's going to
[10:06:34] go and just simply summarize all those
[10:06:37] values. So we are adding 20 + 10 + 5 and
[10:06:40] we will get the result 35. So in the
[10:06:43] outputs we will get everywhere 35. So
[10:06:47] that's it for the first window and as
[10:06:48] you can see SQL going to go aggregate
[10:06:50] the data within each window separately.
[10:06:53] So that means as we are aggregating the
[10:06:54] data for the caps will not check
[10:06:57] anything with the gloves. So they are
[10:06:59] completely separated. So now it's going
[10:07:01] to go for the next window. And here we
[10:07:03] have two values and a null. So again
[10:07:05] here the null will just be ignored. So
[10:07:07] what we going to have? We're going to
[10:07:08] have 30 + 70 and the total sales for
[10:07:12] that going to be 100. So as you can see
[10:07:14] it is very simple, right? So 100 100 and
[10:07:18] so guys that's it. It's really simple.
[10:07:19] We don't have here like a lot of special
[10:07:21] cases like the count function. It's only
[10:07:24] that it ignores the null in the
[10:07:25] calculation and as well the requirement
[10:07:28] here it allows only integers or let's
[10:07:30] say numbers. So we cannot go and say sum
[10:07:33] the products since the products are not
[10:07:35] numbers they are characters. So you can
[10:07:37] only use numbers for the sum function.
[10:07:40] Let's go now and have some tasks and
[10:07:42] some use cases in order to practice in
[10:07:43] SQL. find the total sales across all
[10:07:46] orders and as well find the total sales
[10:07:48] for each product and additionally we
[10:07:50] have to provide some details like the
[10:07:51] order ID and the order dates. So let's
[10:07:54] go and do that. Select order ID, order
[10:07:56] date and let's get as well the sales.
[10:07:58] And now we have to find the total sales
[10:08:00] across all orders. That means we're
[10:08:02] going to use the window function sum
[10:08:04] sales and the definition of the window
[10:08:06] going to be empty since we don't have to
[10:08:08] divide the data. So that's it. total
[10:08:11] sales and we have to select the table
[10:08:13] sales orders. So that's it. Let's go and
[10:08:16] execute it. So with that as you can see
[10:08:18] we got all the details that we need and
[10:08:20] as well the total sales the
[10:08:22] summarization of all those sales in one
[10:08:25] field. So with that we have our overall
[10:08:28] analyzes one big number for our
[10:08:29] reporting. We know how much sales we did
[10:08:32] made in the entire business. So now
[10:08:34] let's go for the next task. It says
[10:08:36] total sales for each product. I think
[10:08:38] you know already what we're going to do.
[10:08:39] So sum of sales and we're going to do it
[10:08:42] like this. Partition
[10:08:44] by product ID. So that's it. We're going
[10:08:48] to call it sales by products. And with
[10:08:51] that we are dividing the data by the
[10:08:53] product. So let's go and execute it. So
[10:08:55] as you can see we don't have the product
[10:08:56] information. So let's go and add the
[10:08:58] product ID in the query just in order to
[10:09:01] analyze the results. So we can see from
[10:09:03] the data that the winner is the product
[10:09:05] ID 101. So as you can see we have here
[10:09:08] the highest sales if you compare it with
[10:09:10] the other products and the lowest one
[10:09:12] going to be the products ID 105. So as
[10:09:15] you can see we can use the window
[10:09:16] function sum together with the partition
[10:09:18] by in order to compare stuff to do
[10:09:21] comparison between the products in order
[10:09:22] to understand the performance for
[10:09:24] example of the products. So it's really
[10:09:26] great analyzes for the performance. All
[10:09:28] right. Now we're going to move to very
[10:09:30] interesting use case for the aggregate
[10:09:31] functions not only for the sum but as
[10:09:33] well for the others. It is the
[10:09:35] comparison analyzes. Okay. Okay, so
[10:09:37] let's understand quickly what is the
[10:09:38] comparison use cases. So it's going to
[10:09:40] go and compare the current value. For
[10:09:42] example, let's say we are currently at
[10:09:44] the month of March and the sales is 30.
[10:09:47] So we're going to compare this value,
[10:09:49] the current sales with an aggregated
[10:09:51] value. For example, let's say the total
[10:09:53] sales using the sum function. So what
[10:09:55] happen if you compare the current value
[10:09:57] with the total sales? You are comparing
[10:09:59] here or doing analyszis called part to
[10:10:01] whole analyszis where it's going to help
[10:10:03] us to understand how important was the
[10:10:05] sales in this month compared to the
[10:10:08] total sales or we can go and compare it
[10:10:10] to the best months to the highest value.
[10:10:12] For example, the highest value is June
[10:10:14] and we can go and compare this month
[10:10:16] with the best months of the year or to
[10:10:18] the lowest month in the year or we can
[10:10:20] go and compare the sales of the current
[10:10:22] month with the average in order to
[10:10:24] understand are we above the typical
[10:10:26] sales or below the average. And this is
[10:10:28] very important analysis in order to
[10:10:30] study and understand the performance of
[10:10:33] the current data. All right, let's have
[10:10:34] an example in order to understand the
[10:10:36] use case. Find the percentage
[10:10:38] contribution of each product sales to
[10:10:40] the total sales. So let's go and solve
[10:10:42] it step by step. What we're going to do,
[10:10:44] we're going to go and let's select the
[10:10:46] order ID and as well let's take the
[10:10:49] product ID and the sales just like this
[10:10:53] from sales orders. So let's go and
[10:10:56] execute it. Okay. Okay. So now as you
[10:10:57] can see in the results we got the first
[10:10:59] part of the equation. We have the sales.
[10:11:01] So nothing like a crazy over here. Now
[10:11:04] we need the total sales over all data.
[10:11:07] So what we're going to do we're going to
[10:11:09] have the sum of sales and the definition
[10:11:12] going to be empty. So this is the total
[10:11:15] sales. Let's go and execute it. So now
[10:11:18] we have everything for the equation. We
[10:11:19] have the sales and as well the total
[10:11:21] sales and that is enough in order to
[10:11:23] find the percentage of the contribution.
[10:11:25] So the calculation for that is going to
[10:11:27] be very simple. We're going to divide
[10:11:28] the sales by the total sales. So it's
[10:11:32] really simple. Let's go and do that.
[10:11:33] It's going to be the sales divided by
[10:11:36] the total sales. So we're going to go
[10:11:37] and copy the whole window function over
[10:11:40] here. And then we're going to multiply
[10:11:41] it with 100. So that's it. Let's go and
[10:11:44] execute it. So now you notice that in
[10:11:46] the output we got zeros. This is because
[10:11:48] of the data type. So now if we go to our
[10:11:50] table over here on the left side you can
[10:11:52] see that the orders has the data type of
[10:11:56] integer. So if you divide integers you
[10:11:58] will not get a float or decimal number.
[10:12:00] You have to go and change the data type.
[10:12:02] So now what we're going to do we're
[10:12:03] going to go and change the data type for
[10:12:05] one of them. So it's enough for the
[10:12:06] sales over here. So we're going to use
[10:12:08] the following statement. So cast sales
[10:12:11] as floats. So that's it. I'm just
[10:12:13] converting the integer to floats. So
[10:12:15] that's it. Let me just give it a name.
[10:12:17] So it's going to be percentage of total.
[10:12:20] So that's it. Let's go and execute it.
[10:12:23] So now in the output, you can see we got
[10:12:24] now the percentage of the total or let's
[10:12:27] say percentage of contribution. So now
[10:12:29] what we're going to do with that, we're
[10:12:30] going to go and round those numbers
[10:12:31] because we have a lot of decimals. In
[10:12:33] order to do that, we're going to use the
[10:12:35] round function like this. Then we're
[10:12:37] going to have two decimals. And let's go
[10:12:40] and execute it. So now, as you can see,
[10:12:42] it is really easier to read because we
[10:12:44] have only two decimals. And we can find
[10:12:46] immediately that the order rate is the
[10:12:48] highest contributor to the total. So
[10:12:51] this is what we call part to whole
[10:12:52] analyszis where we find the percentage
[10:12:55] of total. It is very common analyzes in
[10:12:57] order to understand the performance of
[10:13:00] each order compared to the total. So
[10:13:02] this is an example how the window
[10:13:04] function is helping us here to compare
[10:13:06] the current value with an aggregated
[10:13:08] value. All right everyone. So that's all
[10:13:10] for the window function sum. Next we're
[10:13:12] going to talk about the average
[10:13:13] function.
[10:13:17] All right. So now let's understand what
[10:13:19] is an average function. As the name
[10:13:21] says, it's going to find the average of
[10:13:23] values within each window. So now let's
[10:13:25] go and understand how SQL works with the
[10:13:28] average. All right. So now back to our
[10:13:29] very simple example and the task says
[10:13:31] find the average sales for each product.
[10:13:33] So it's really easy. We're going to use
[10:13:35] the average then pass to it the column
[10:13:37] sales and we define the window like this
[10:13:39] partition by products. So the first
[10:13:41] thing that SQL going to go is to define
[10:13:43] the window. So it's going to divide our
[10:13:45] data into two partitions. One for the
[10:13:47] caps and one for the gloves. And now I
[10:13:49] hope that everyone knows how to
[10:13:50] calculate the average. So as you know
[10:13:52] that it's going to go and summarize all
[10:13:54] the values and divide it by the number
[10:13:56] of rows. So it's going to go and
[10:13:57] summarize 20 + 10 + 5 and divide it on
[10:14:01] three rows and the output going to be
[10:14:03] 11. So we're going to get it for each
[10:14:05] row. So as you can see SQL just ignored
[10:14:08] everything in the next window. We are
[10:14:10] focusing only on the caps. Now it's
[10:14:12] going to go to the second window and
[10:14:14] start doing the same aggregations. But
[10:14:15] here we have the special case of null.
[10:14:17] So the null is going to be ignored in
[10:14:19] the calculations and we're going to have
[10:14:20] it like this. It's going to say you know
[10:14:22] what 30 + 70 and we are just including
[10:14:25] two rows. So it's going to be divided by
[10:14:27] two and the average going to be 50. So
[10:14:29] we will get the result 50 for each row
[10:14:32] and we are completely ignoring the
[10:14:34] nulls. But now we might be in scenario
[10:14:36] where your users understand the business
[10:14:37] like this. If we find a null in the
[10:14:40] sales it means a zero. So there is no
[10:14:42] sales and it is actually a zero. But we
[10:14:44] store it in the database as a null. So
[10:14:46] that means the average that we have
[10:14:47] provided is not really correct. We have
[10:14:49] to divide by three. So that means first
[10:14:52] we have to handle the nulls before doing
[10:14:54] the aggregations before finding the
[10:14:56] average. Now we're going to have a whole
[10:14:58] chapter on how to handle nulls in SQL.
[10:15:00] What are the different functions? But
[10:15:02] for now we're going to go with the
[10:15:03] functions qualisk. Okay. So now what
[10:15:05] we're going to do, we will not use the
[10:15:06] sales as it is. First we're going to
[10:15:08] handle the nulls. So that means we're
[10:15:10] going to use the qualisk sales and
[10:15:13] replace it with zeros. So as you can see
[10:15:15] we are not using immediately the sales
[10:15:17] we are handling it first and then we're
[10:15:19] going to find the average. So SQL going
[10:15:21] to go over here and if it finds any null
[10:15:24] going to go and replace it with zero and
[10:15:26] that's going to have then an effect on
[10:15:28] our average over here. So it going to be
[10:15:30] 30 + 7 + 70 but now plus 0. And now we
[10:15:33] have three rows. So instead of dividing
[10:15:36] by two, it's going to go and divide it
[10:15:37] by three and the total result going to
[10:15:39] be like this 33. So that means we're
[10:15:41] going to have in the output 33 for each
[10:15:44] row and with that we are now fulfilling
[10:15:47] the expectation from the business. If
[10:15:49] you have a null it's going to be handled
[10:15:50] as zero and the result going to be more
[10:15:52] accurate. You see right it is very
[10:15:54] tricky. If you are doing data analyszis
[10:15:56] and aggregations be very careful with
[10:15:58] the nulls. understand them, understand
[10:16:01] what they mean for the business, handle
[10:16:03] them correctly in order to get correct
[10:16:05] results in your analysis. So now let's
[10:16:08] go back in order to practice SQL using
[10:16:10] some tasks and use cases. Okay, so let's
[10:16:13] start with the basics. We have the
[10:16:14] following task. Find the average sales
[10:16:16] across all orders and as well find the
[10:16:18] average sales for each product. And
[10:16:20] don't forget the details. So now let's
[10:16:22] go and solve it step by step. So select
[10:16:24] order ID, order date, and let's get the
[10:16:28] sales as well. And let's go and find the
[10:16:30] average sales. So it's going to be a
[10:16:31] window function. And we have the sales
[10:16:33] inside it. The usual stuff. The window
[10:16:35] going to be empty. So average sales,
[10:16:38] we're going to call it the table going
[10:16:40] to be sales orders. So that's it. Let's
[10:16:44] go and execute it. Oh, we have to select
[10:16:46] everything of course. So what SQL did in
[10:16:48] the output, it going to go and summarize
[10:16:50] all those values and then divide it by
[10:16:52] 10. So with that we have the average
[10:16:54] sales of 38. Very easy. So this is again
[10:16:57] what we call an overall analyzis. Let's
[10:16:59] move to the next one. Find the average
[10:17:01] sales for each products. So again we're
[10:17:03] going to go and build the window
[10:17:05] function like this. Average sales over
[10:17:09] and we're going to divide it by product
[10:17:11] ID. And we're going to call it average
[10:17:13] sales by products. And we're going to go
[10:17:16] and add the product ID in the query. So
[10:17:19] that's it. Let's go and execute. And we
[10:17:22] missed something here. So it is the
[10:17:24] partition by going to execute again. So
[10:17:27] with that we have the following data. So
[10:17:28] now SQL going to go and divide the data.
[10:17:30] So for example for this products we have
[10:17:32] those four orders. So what going to
[10:17:34] happen is still going to go and
[10:17:35] summarize the four values and then
[10:17:38] divide it by four. That's why we have
[10:17:39] here 35. The same thing for the next
[10:17:42] order. It's going to divide it by three.
[10:17:44] And the last one is just going to divide
[10:17:46] it by one. That's why we have 60. So as
[10:17:48] you can see the aggregation can done
[10:17:50] separately for each window and this is
[10:17:52] as well very nice way in order to
[10:17:54] compare the averages between the
[10:17:56] different products. Okay. So now let's
[10:17:58] have an example in order to learn how to
[10:18:00] deal with the nulls. Let's say that we
[10:18:01] have the following task. Find the
[10:18:03] average scores of customers and show as
[10:18:06] well additional informations like the
[10:18:08] customer ID and the last name. So let's
[10:18:10] go and solve this. We are now targeting
[10:18:13] the table customers. So let's just
[10:18:16] select it first.
[10:18:18] like this. And now let's go and include
[10:18:20] the customer ID and the last name. And
[10:18:24] let's have as well the score. But this
[10:18:26] time we're going to go and find the
[10:18:27] average score. So it's going to be the
[10:18:29] average score. And since we don't
[10:18:31] partition the data, we're going to leave
[10:18:33] the definition like this and it's going
[10:18:35] to be the average score. So that's it.
[10:18:39] Let's go and execute it. So now as you
[10:18:40] can see, we have the average score of
[10:18:42] 625. SQL is going to go and summarize
[10:18:45] the four values and divide it by four.
[10:18:48] But here we have a null. So now we have
[10:18:50] to understand the business or ask about
[10:18:51] it what the null means in the scores of
[10:18:54] the customers. Is it zero or is it
[10:18:56] something empty? If it's zero then the
[10:18:59] average that we have is wrong because it
[10:19:00] should be divided by five and not four.
[10:19:03] So let's say it's zero that means we
[10:19:06] have to go and handle the nulls. So what
[10:19:07] we're going to do now we're going to go
[10:19:08] and use the function kalis. So qualis
[10:19:12] and for the score and replace the null
[10:19:15] with zero. So you are the customer
[10:19:17] score. Let's go and execute this. So now
[10:19:19] as you can see if there is a value it's
[10:19:21] going to be exactly the same value but
[10:19:23] only if we have null it's going to be
[10:19:24] replaced with zero. So now let's go and
[10:19:26] correct the average. I'm just going to
[10:19:28] do it like this. So let's go and copy
[10:19:31] the whole thing. But now instead of
[10:19:33] using the score we're going to use the
[10:19:35] score that is handled with nulls. So I'm
[10:19:37] just going to go and replace it like
[10:19:39] this. So here without nulls. So let's go
[10:19:43] and execute it. So now as you can see we
[10:19:45] are getting more valid result at the
[10:19:47] output compared to the previous one. And
[10:19:49] this is only for the case if the null
[10:19:51] means zero. So guys as you see be very
[10:19:54] careful with the nulls especially if you
[10:19:55] are doing aggregations and handle it
[10:19:57] correctly before doing any aggregations
[10:19:59] like the average. All right. Moving on
[10:20:01] to the last use case. We have the
[10:20:03] comparison analyzes and the task says
[10:20:05] find all orders where the sales are
[10:20:08] higher than the average sales across all
[10:20:10] orders. So that means we have to go and
[10:20:13] compare the current sales with the
[10:20:15] aggregated value and this time the
[10:20:17] average of sales. So now let's go and do
[10:20:18] it step by step. So what we're going to
[10:20:20] do we're going to go and select of
[10:20:22] course the order ID. What do we need the
[10:20:24] let's take the product ID and we need
[10:20:26] the current sales. So it's going to be
[10:20:28] the sales as it is and that's it for
[10:20:31] now. So from sales orders. So that's it.
[10:20:35] Let's go and execute it. So now by
[10:20:37] checking the result, you can see that we
[10:20:39] got the first part of the equation,
[10:20:40] right? We have the sales for each order.
[10:20:43] Now we need the second part, the average
[10:20:45] sales across all orders. In order to do
[10:20:47] that, we're going to go and use the
[10:20:48] window function average sales and we're
[10:20:51] going to use over since across all
[10:20:53] orders that means it's going to be
[10:20:54] empty. So let's give it a name average
[10:20:56] sales. So let's go and execute it. So
[10:20:59] now in the output we got the average
[10:21:01] sales. So it's going to be 38. So now we
[10:21:03] need all the orders that are higher than
[10:21:06] the average. So as you can see for
[10:21:07] example the order one is not higher but
[10:21:10] the order for is higher than the
[10:21:12] average. So in order to filter the data
[10:21:14] we cannot use the window function in the
[10:21:17] wear close. Right? So what we're going
[10:21:19] to do sadly we're going to go and use
[10:21:21] the subquery. So it's going to be like
[10:21:23] this. select star from and then we're
[10:21:27] going to define the condition outside
[10:21:30] the subquery. So it's going to be where
[10:21:32] the sales is higher than the average
[10:21:35] sales. So that's it. Let's go and
[10:21:38] execute it. And now as you can see it's
[10:21:40] very simple. We got all the orders that
[10:21:42] are higher than the average. Right? So
[10:21:44] you can see all those sales are higher
[10:21:46] than the average. It would be nice if we
[10:21:48] can do all those stuff in the first
[10:21:50] query. But since we cannot do that, we
[10:21:53] need to use the subqueries in order to
[10:21:55] filter the data afterward. So that we
[10:21:58] can understand the importance of the
[10:21:59] comparison analyszis. For example, here
[10:22:01] we are finding or evaluating the data
[10:22:04] whether they are above the average or
[10:22:06] below the average. And this is very
[10:22:08] important in the business analyzes. All
[10:22:10] right, everyone. So that's all for the
[10:22:11] window function average. Next, we're
[10:22:13] going to talk about two very interesting
[10:22:15] functions, the min and max.
[10:22:20] All right guys, so what is min and max
[10:22:22] functions? They are very simple but yet
[10:22:25] very powerful functions for analytics.
[10:22:27] So the min simply is the function that
[10:22:29] can return the minimum or let's say the
[10:22:31] lowest value within a window where the
[10:22:34] max it's exactly the opposite. It's
[10:22:36] going to find the maximum value or the
[10:22:38] highest value within a window. So now
[10:22:41] let's go and understand how SQL works
[10:22:43] with these functions. All right. So now
[10:22:44] we have the same data and we have two
[10:22:46] tasks. First we have to find the lowest
[10:22:49] sales for each product. And the second
[10:22:51] one side by side we would like to find
[10:22:53] the highest sales for each product. So
[10:22:55] we're going to go and use the min max.
[10:22:57] And as you can see the syntax is very
[10:22:58] simple. Min the sales and then the
[10:23:00] partition going to be by the products.
[10:23:02] And here as well the same stuff but
[10:23:04] having the max. Okay. So now let's see
[10:23:06] how going to execute the first query. As
[10:23:08] usual first it's going to prepare the
[10:23:09] data. So it's going to split the data
[10:23:11] into two windows. One for the caps and
[10:23:13] another one for the gloves. And after
[10:23:15] that it's going to search for the lowest
[10:23:17] sales within each window separately. So
[10:23:20] for the first window we have the
[10:23:21] following values 20 10 and five. And of
[10:23:24] course the lowest value going to be the
[10:23:26] five. So that's why SQL going to find it
[10:23:28] over here. And everywhere for this
[10:23:30] window it's going to be the value five.
[10:23:32] So we have it as the lowest sales for
[10:23:34] the product caps. So now it's going to
[10:23:36] jump to the next window for the gloves
[10:23:37] and start searching the values. So as
[10:23:39] you can see we have 30 70 and null. Null
[10:23:42] will be ignored. So null will not be
[10:23:44] considered as the lowest value. So SQL
[10:23:46] going to find the lowest sales with the
[10:23:48] 30. So it's going to be actually the
[10:23:50] first row within this window and the
[10:23:52] value the output going to be 30 for each
[10:23:55] row. So that's it. It's very simple,
[10:23:56] right? Now let's move to the next one.
[10:23:58] We have the same stuff but using max. So
[10:24:00] the data is partitions and for the first
[10:24:02] partition what is the highest value?
[10:24:04] It's going to be the first row, right?
[10:24:06] The 20. So SQL going to find it and in
[10:24:08] the output we will get the highest sales
[10:24:11] 20 for this window and then it's going
[10:24:12] to go to the second window and search
[10:24:14] for the highest value. So here we have
[10:24:16] two values 30 and 70 and it's going to
[10:24:18] be the 70 right. So it's going to point
[10:24:20] it over here and in the output we will
[10:24:22] get everywhere 70. So guys it's really
[10:24:24] simple right now let's back to our
[10:24:26] scenario in the average where in our
[10:24:28] business we understand nulls as zero in
[10:24:30] the sales. So that means first we have
[10:24:32] to handle the nulls and replace it with
[10:24:34] zero and then we're going to go and
[10:24:36] search for the value. So what's going to
[10:24:38] happen? We're going to go and replace
[10:24:40] nulls with zero. For the max nothing
[10:24:42] going to change the highest value going
[10:24:44] to be 70 and we're going to get the same
[10:24:45] output. But for the min now we have new
[10:24:49] lowest value. So it's not anymore the
[10:24:51] 30. It's actually the zero. So SQL going
[10:24:54] to go over here and replace the 30 with
[10:24:56] nulls. So nulls is the lowest sales for
[10:25:00] the product gloves. So again guys, the
[10:25:02] nulls are very tricky and those
[10:25:04] functions are really sensitive with the
[10:25:05] nulls. Understand what the nulls means
[10:25:08] and handle it correctly so that you get
[10:25:10] correct results in the output. So that's
[10:25:12] it. Let's go back to SQL to have some
[10:25:14] tasks and use cases in order to practice
[10:25:16] SQL. All right everyone, let's start
[10:25:18] with the basic stuff. find the highest
[10:25:20] and lowest sales of all orders and as
[10:25:22] well find the highest and lowest sales
[10:25:24] for each product and we have to provide
[10:25:26] additional informations. So let's go and
[10:25:29] solve it. Select order ID order and
[10:25:33] let's take as well the product ID. Now
[10:25:34] let's find the highest sales of all
[10:25:36] orders. It going to be the max function
[10:25:39] for the sales and the window function
[10:25:42] going to be empty since of all orders.
[10:25:44] So you are the highest sales. Let's go
[10:25:47] for the lowest sales of all orders. It's
[10:25:48] going to be exactly the opposite. The
[10:25:50] main function for sales over then we
[10:25:53] have the lowest sales. So I'm just going
[10:25:56] to make it bigger capital. So let's
[10:25:58] select the table sales orders. So I
[10:26:02] think that's it. Let's have as well the
[10:26:04] sales actually. All right. So now let's
[10:26:06] go and execute it. So now this is very
[10:26:08] simple, right? This is the wholesales.
[10:26:10] What is the highest sales? We have the
[10:26:13] 90 of the order eight. So, as you can
[10:26:15] see, we have now the highest sales, the
[10:26:17] 90, and the lowest sales is the 10. The
[10:26:20] first order is the lowest. So, it's very
[10:26:22] easy. Now, we're going to go and repeat
[10:26:24] the same stuff for the product. So, we
[10:26:26] have go and partition the data by the
[10:26:28] product ID. So, what I'm going to do,
[10:26:29] I'm just going to go and copy paste
[10:26:31] stuff around. So, the first one going to
[10:26:33] be partition by the product ID. So,
[10:26:36] highest sales by product. And the next
[10:26:39] one going to be the same stuff. Copy
[10:26:41] paste by the product. So that's it.
[10:26:45] Let's go and execute it. So now again
[10:26:48] the data going to be partitioned and
[10:26:49] divided by the product. So for the first
[10:26:52] window what is the highest sales? It's
[10:26:54] going to be the 90 and the lowest sales
[10:26:55] is going to be the 10. So it's exactly
[10:26:57] like the overall rights now let's go to
[10:26:59] the second window over here. We can see
[10:27:01] that the lowest or the highest sales is
[10:27:03] the 60 the first one and the lowest this
[10:27:05] time is 15. And this is great in order
[10:27:08] to see that the SQL going to execute
[10:27:10] each of those functions for each window
[10:27:12] separately. So let's go to the last
[10:27:14] window. It's funny one. So the sales is
[10:27:16] 60 and we have only one row. So it's
[10:27:18] going to be the highest and as well the
[10:27:19] lowest sales. So with that as you can
[10:27:21] see we can define a range for each
[10:27:22] product and the range are different from
[10:27:24] each product to another one. For
[10:27:26] example, for this product 101 the range
[10:27:28] from 10 until 90. But for the second
[10:27:31] product we have it between 15 and 60.
[10:27:33] Okay guys, let's move to the next one
[10:27:35] which is one of my favorites in the
[10:27:36] window function where we filter the data
[10:27:39] using the minmax functions. Let's have
[10:27:41] the following task. It says show the
[10:27:43] employees who have the highest salaries.
[10:27:46] So this sounds very simple but we can
[10:27:48] use the help of window functions in
[10:27:50] order to solve it. So now we are working
[10:27:52] with the table employees. Let's just
[10:27:54] select the data. So select from sales
[10:27:58] employees. So that's it. Let's go and
[10:28:01] execute it. So now we have five
[10:28:03] employees and we have those different
[10:28:04] salaries. Let's go and find the highest
[10:28:08] salary. So max salary and let's use the
[10:28:11] window function over but we don't
[10:28:13] partition the data at all. So it's going
[10:28:15] to be like this highest salary. So let's
[10:28:18] go and execute it. And now by checking
[10:28:21] the results we got a new column called
[10:28:22] highest salary and inside it we have the
[10:28:25] 90k. So if you check those five salaries
[10:28:27] you can see that the highest is from the
[10:28:30] employee Michael. But still the task is
[10:28:32] not solved. We have to show only the
[10:28:34] employees who have the highest salaries.
[10:28:36] So we have somehow to filter the data
[10:28:38] and only show this employee. So in order
[10:28:41] to do that we have to use the subqueries
[10:28:44] since we cannot use the window function
[10:28:46] in the wear clause. So what we're going
[10:28:48] to do select star from and then our
[10:28:52] first query going to be the inner query.
[10:28:55] So we have the following condition. It's
[10:28:57] going to be the
[10:28:59] salary should be equal to the highest
[10:29:02] salary. So it's very simple. So with
[10:29:04] that we are comparing the salaries with
[10:29:06] the highest salaries. If there is a
[10:29:08] match the data going to be presented. So
[10:29:10] let's go and execute that. And that's
[10:29:12] it. As you can see we got the employee
[10:29:14] with the highest salary. But if there
[10:29:16] are like multiple employees with the
[10:29:18] same salary of 90k of course we're going
[10:29:20] to get it in the results. I think
[10:29:22] Michael going to need a new job. Right.
[10:29:24] This is the worst.
[10:29:30] So this is another use case for the
[10:29:32] window functions minmax. All right. So
[10:29:34] now we come to the use case of the
[10:29:36] comparison analyzers where we want to
[10:29:38] compare the current sales with the
[10:29:40] highest and the lowest value. So we have
[10:29:42] the following task. It says find the
[10:29:44] deviation of each sales from the minimum
[10:29:47] and the maximum sales amount. So now as
[10:29:49] you can see this is our sales. This is
[10:29:50] the highest and this is the lowest. So
[10:29:52] now we just have to go and subtract the
[10:29:54] data from each others in order to get
[10:29:56] the deviation. So it's very simple.
[10:29:58] Let's get the first deviation where
[10:30:00] we're going to go and subtract the sales
[10:30:02] with the lowest value. So it's going to
[10:30:05] be like this. So now what we are doing
[10:30:06] over here, we are subtracting the sales
[10:30:08] from the lowest sales of all records. So
[10:30:12] we're going to go and call you
[10:30:14] deviation from min. So let's go and
[10:30:17] execute it. So now we can see from those
[10:30:19] values how far is the current value from
[10:30:23] the extreme. The extreme here is the
[10:30:25] lowest value. So this is a really great
[10:30:27] way on to analyze the extremes in your
[10:30:29] data. So now as we are near to the
[10:30:31] extreme the value going to be low. So as
[10:30:34] you can see here we have a zero. This is
[10:30:35] the lowest because we have it exactly as
[10:30:38] the extreme. So actually this is our
[10:30:41] value. So the 10. Now the next one is
[10:30:43] little bit far away from the extreme
[10:30:45] which is 15. So we have it here as a
[10:30:47] five. So this is not far away from our
[10:30:49] extreme value. And then if you check
[10:30:51] this value over here we have it 80. So
[10:30:53] the distance is very far away from our
[10:30:56] extreme value the lowest sales. So this
[10:30:58] is really nice analyszis in order to
[10:31:00] analyze and evaluate the sales of your
[10:31:03] data. Now of course we can go and
[10:31:05] evaluate our data with an another
[10:31:07] extreme which is the highest sales. So
[10:31:10] in order to do that we're going to first
[10:31:12] say let's get the highest sorry this one
[10:31:15] the highest sales and subtract it from
[10:31:18] the sales. So you are the deviation from
[10:31:21] the max. So let's go and execute it. So
[10:31:24] now we can see in the output we're going
[10:31:26] to get exactly the opposite distances.
[10:31:28] So the order number one is the farthest
[10:31:31] from the extreme. So as you can see we
[10:31:33] have the value of 80 and the order eight
[10:31:36] is the identical one. So that's why we
[10:31:38] have the distance of zero. So now we can
[10:31:40] see as well very quickly which data
[10:31:42] points are the nearest to the extreme to
[10:31:44] the highest sales. So as you can see
[10:31:46] guys using the window function min and
[10:31:48] max it is very powerful in order to
[10:31:50] understand and evaluate your data points
[10:31:53] to the
[10:31:54] [Music]
[10:31:57] extremes. All right everyone so now
[10:31:59] we're going to focus on very important
[10:32:00] use case. One of the must know use cases
[10:32:03] for data aggregations is doing running
[10:32:05] total and rolling total. These two
[10:32:08] concepts are very important for data
[10:32:10] analyszis and doing reporting that you
[10:32:12] must know. The key use case for those
[10:32:14] two concept is to do tracking. For
[10:32:17] example, we can go and track the current
[10:32:19] total sales with the target sales in our
[10:32:22] business. And as well, it's great in
[10:32:24] order to do historical analyszis for the
[10:32:26] trends. Okay. So now the question is
[10:32:28] what is running a rolling total. They
[10:32:31] are basically very similar. They're
[10:32:32] going to go and aggregate a sequence of
[10:32:34] members and the aggregation going to get
[10:32:36] updated each time we add a new member to
[10:32:38] the sequence. A sequence could be like a
[10:32:41] time sequence. That's why we call this
[10:32:43] type an analyzes over time. So now we
[10:32:46] still have the question, what is the
[10:32:47] difference between the running and the
[10:32:49] rolling totals. The running total going
[10:32:51] to go and aggregate everything from the
[10:32:53] beginning until the current data point
[10:32:56] without dropping off any old data. Where
[10:32:58] on the other hand in the rolling total
[10:33:00] it going to go and focus on a specific
[10:33:02] time window like the last 30 days or the
[10:33:05] last two monthses and each time we add a
[10:33:07] new member or a new data point to the
[10:33:09] window we will be dropping off the
[10:33:12] oldest data point in the window and with
[10:33:14] this we're going to get the effect of
[10:33:16] rolling or let's say shifting window
[10:33:18] okay I totally understand if this might
[10:33:20] be complicated now let's go and have
[10:33:22] very simple example in order to
[10:33:23] understand this concept and as well how
[10:33:25] we can solve it using SQL all right guys
[10:33:27] so now We have very simple example. We
[10:33:29] have the months and sales and we have it
[10:33:31] twice because I want to show you side by
[10:33:33] side how SQL works with the running
[10:33:35] total and the rolling total. So now what
[10:33:37] is the task on the left side? We want to
[10:33:39] find the running total of sales for each
[10:33:42] month and on the right side we would
[10:33:44] like to find three month rolling total
[10:33:47] of the sales for each month. So they
[10:33:49] sound very similar but on the right side
[10:33:51] we have only fixed window. So now how we
[10:33:54] can solve this using SQL. On the left
[10:33:56] side we can use sum of sales. So we want
[10:33:58] to go and aggregate all the sales using
[10:34:00] the sum function. And the definition for
[10:34:02] the window going to be like this order
[10:34:04] by month and of course you can go and do
[10:34:06] anything like you can have here an
[10:34:07] average. And if you use an average with
[10:34:09] order by you will get the running
[10:34:11] average or the running max or the
[10:34:13] running count and so on. So that means
[10:34:16] always if you go and mix an aggregate
[10:34:18] function together with an order by you
[10:34:20] will generate an effect of running
[10:34:22] total. Now on the right side we can have
[10:34:24] the same stuff. So we can have an
[10:34:26] aggregate function together with order
[10:34:27] by. So sum of sales, order by month. So
[10:34:31] far we have everything like the left
[10:34:32] side, right? But now you might ask why
[10:34:34] is going to go and generate this effect
[10:34:36] the running total. We didn't here
[10:34:38] specify like crazy stuff, right? It's
[10:34:40] all about the definition of the frame
[10:34:42] close. So now do you remember if you use
[10:34:44] an order by and you don't specify a
[10:34:46] frame close you will get like hidden or
[10:34:49] let's say default frame close and it's
[10:34:51] going to look like this rows between
[10:34:53] unbounded preceding and current row. And
[10:34:55] what was the definition of the running
[10:34:57] total? It's going to go and aggregate
[10:34:59] all the data from the very first
[10:35:01] beginning well the unbounded proceeding
[10:35:03] until the current position the current
[10:35:06] row without dropping off any old
[10:35:08] members. So that means the definition of
[10:35:09] the running total going to be the exact
[10:35:12] definition of the default frame clause.
[10:35:14] That's why it's going to go and generate
[10:35:16] the effect of the running total. Now
[10:35:18] let's go to the right side the rolling
[10:35:20] total. Here again we have the same stuff
[10:35:22] right. We're going to go and aggregate
[10:35:24] the data using the sum function and
[10:35:26] we're going to go and sort the data
[10:35:27] order by month. So with that we are as
[10:35:29] well generating the effect of running
[10:35:32] total. So each time you use order by
[10:35:34] with aggregate function. So now in the
[10:35:35] running total we want always to specify
[10:35:37] a frame. So here in this example three
[10:35:39] months. So that means if we are getting
[10:35:41] a new month we don't want to include the
[10:35:43] latest months. We want always to be
[10:35:45] fixed window. Now in order to have this
[10:35:48] fixed window effect we have to go and
[10:35:50] redefine the frame close because if you
[10:35:52] leave it as a default like the running
[10:35:54] total the frame going to keep extending.
[10:35:56] You will see this effect in the example.
[10:35:58] So now we define it like this rows
[10:36:00] between two preceding and current row.
[10:36:03] So the total number of rows going to be
[10:36:05] included in each window going to be
[10:36:07] maximum of three months. So now I know
[10:36:10] you might saying bar what you are
[10:36:12] talking about you didn't get anything.
[10:36:14] It's total normal you will understand it
[10:36:16] only with an example. So in order to do
[10:36:19] this let's start with the left side. So
[10:36:20] first going to go and sort the data. So
[10:36:22] everything is sorted from the smallest
[10:36:24] month until the highest one. So from
[10:36:26] January until July everything is good.
[10:36:28] And now it's going to go and start
[10:36:30] working with the frame. So the frame
[10:36:32] says unbounded proceeding. So this going
[10:36:34] to be static. It's going to be always
[10:36:36] pointing to January. This is the
[10:36:39] unbounded proceeding. The first row in
[10:36:41] the data set. And now of course we are
[10:36:43] starting from top to bottom. The current
[10:36:45] row going to be pointing as well to
[10:36:47] January. So the frame going to look like
[10:36:49] this. It's going to be only one row and
[10:36:52] the total sale of this row going to be
[10:36:54] 20. So that's why we're going to have in
[10:36:55] the output 20. So now let's move to the
[10:36:57] right side. The current row going to be
[10:36:59] as well January. And what is the two
[10:37:01] proceeding? We don't have it yet. So
[10:37:03] it's going to be pointing maybe
[10:37:04] somewhere here before the table. So
[10:37:06] again, what is the frame? It's going to
[10:37:08] be as well one row. So in the output, we
[10:37:10] will get exactly the same result 20. So
[10:37:13] so far there is no differences between
[10:37:15] the running total and the rolling total.
[10:37:17] But let's keep going. Now we're going to
[10:37:19] go to the next row over here. So what
[10:37:21] can happen to our frame? It going to go
[10:37:23] and extend, right? So we're going to
[10:37:24] have now two months in this frame. And
[10:37:27] what is the total sales over here? It's
[10:37:28] going to be 30. So we added a new
[10:37:30] member. You can calculate it like this.
[10:37:32] Either go and calculate all the sales
[10:37:34] within the frame or you can go and say
[10:37:37] this is the previous aggregated value
[10:37:39] plus the new member. So the previous one
[10:37:41] was 20. The new member is 10. We will
[10:37:44] get 30. Both of them is correct. So now
[10:37:46] let's move to the right side. What's
[10:37:48] going to happen? We're going to be as
[10:37:49] well at February. The two preceding is
[10:37:51] still like pointing somewhere outside.
[10:37:53] And here the window going to go and
[10:37:55] extend like this. We have two months and
[10:37:58] the same aggregation going to happen. So
[10:37:59] we have 30. So so far nothing crazy
[10:38:02] right. Let's go to the next month March.
[10:38:04] The frame going to be extended. So we
[10:38:06] have now three months. And the
[10:38:08] aggregation going to be either here 60
[10:38:10] or 30 + 30. We will get the running
[10:38:13] total of 60. And now on the right side
[10:38:15] what going to happen? We're going to
[10:38:16] point as well to March. And this time
[10:38:18] the two proceeding going to be pointing
[10:38:19] to January. And this is the first time
[10:38:22] we are getting the whole fixed frame.
[10:38:24] Right? So we have here three muscles in
[10:38:26] this frame. So what is the total of
[10:38:28] that? It's going to be 60. Okay. So now
[10:38:30] you say, okay, we're still getting the
[10:38:31] same results. There's no difference. I'm
[10:38:33] going to say wait for it. It's going to
[10:38:34] be the next one. So as we go to April,
[10:38:36] the effect here is that the frame going
[10:38:38] to get extended to four months because
[10:38:41] always we start from the first month
[10:38:43] until the current month without dropping
[10:38:45] any member outside. So what is the total
[10:38:47] of this? It's going to be 65. Sorry,
[10:38:50] like this. So now on the right side,
[10:38:52] what going to happen? We're going to go
[10:38:53] and add a new member. the April but we
[10:38:55] are at the maximum sides of the window
[10:38:57] we have only three and that's because
[10:38:59] the two preceding going to shift as well
[10:39:01] down over here so the boundary going to
[10:39:04] be from February until April and with
[10:39:06] that we are dropping off January and now
[10:39:08] you're going to see the effect it is
[10:39:10] sliding it is rolling or shifting from
[10:39:13] top to bottom and that's because the
[10:39:15] boundaries as well shifting so you can
[10:39:17] see now the effect of the rolling total
[10:39:20] the newest member going to be added the
[10:39:22] oldest member going to be But we are
[10:39:24] allowed only to have three muscles. So
[10:39:26] what is the total of this? It's going to
[10:39:28] be 45. So this times we are not
[10:39:30] aggregating this value the 60 together
[10:39:32] with the five. We are aggregating the
[10:39:35] values within the window. So now let's
[10:39:37] keep going. Now we are at June. What can
[10:39:40] happen on the left side? The frame going
[10:39:41] to get bigger. And with that we will get
[10:39:43] the result of 135. So the frame is
[10:39:47] getting really bigger. But on the right
[10:39:48] side it's going to has a fixed frame. So
[10:39:51] we are just sliding, shifting and
[10:39:53] rolling. So with that we are adding new
[10:39:56] member. Another member is leaving the
[10:39:57] oldest one. And the total over here
[10:39:59] going to be 105. And now we're going to
[10:40:01] go to the last row. We will have
[10:40:03] everything for the ring total. So the
[10:40:05] whole data set is going to be
[10:40:07] aggregated. So this is the maximum what
[10:40:09] we're going to get. It's going to be
[10:40:11] around 175. But on the right side it
[10:40:15] just going to keep shifting until we
[10:40:16] reach the last record. the window the
[10:40:19] frame going to be as well shifting like
[10:40:20] this. So the total of this going to be
[10:40:22] 105. Okay guys so you see it's very
[10:40:25] simple the running total it's always
[10:40:27] consider everything from the starting
[10:40:30] position until the current row without
[10:40:32] dropping any member. The rolling total
[10:40:35] it's always drop the oldest member in
[10:40:37] order to add something new and the
[10:40:39] window is keep shifting. So the running
[10:40:42] total is very great in order to do
[10:40:43] tracking like for example budget
[10:40:45] tracking or we track for example the
[10:40:48] current total sales with a target or
[10:40:50] something like that. So always we are
[10:40:52] considering the whole data sets but with
[10:40:54] the rolling total we always do here
[10:40:56] focused analyzes. We are always
[10:40:58] interested with the window of 3 months.
[10:41:01] So they might sound very similar but
[10:41:03] they have completely different scope for
[10:41:04] analyzes but both of them are doing
[10:41:07] aggregations over time. So they're going
[10:41:09] to help us to do analyzes over time like
[10:41:11] checking whether our business is growing
[10:41:13] over time or declining. So guys as you
[10:41:15] can see using very simple SQLs using the
[10:41:17] window functions we can do really great
[10:41:20] analysis on our data. So those stuff are
[10:41:22] really fundamental of data analyzes or
[10:41:25] doing reporting for our business. So
[10:41:27] window functions are really powerful for
[10:41:29] data
[10:41:33] analytics. Okay. Okay. So now we have
[10:41:35] the following task and it says calculate
[10:41:37] the moving average of sales for each
[10:41:39] products over the time. So now we have
[10:41:42] here something called moving average. It
[10:41:44] is very similar to the running total. In
[10:41:47] the running total we used count and sum
[10:41:49] and so on. But here we're going to go
[10:41:51] and use the function average and instead
[10:41:53] of calling it running average we call it
[10:41:56] moving average. So let's go and solve
[10:41:58] the task. Let's start always by
[10:42:00] selecting the usual stuff. So let's get
[10:42:02] the order ID. Let's get the product ID
[10:42:05] and I would say since it's over the time
[10:42:07] I will get the order date as well and
[10:42:09] the last one the sales from our table
[10:42:12] sales orders. So that's it. Let's go and
[10:42:17] execute it. So now we got our 10 orders
[10:42:19] with the products order date and sales.
[10:42:21] Let's start building our window function
[10:42:23] step by step. So which function do we
[10:42:26] need? We need the average. This is the
[10:42:27] easiest one. It says moving average. So
[10:42:30] that's it. We need the sales. So it's
[10:42:32] going to be the average of sales. Let's
[10:42:34] go and define the window. So now do we
[10:42:36] have to divide the data, partition the
[10:42:38] data? Well, yes. It says for each
[10:42:41] product that means we're going to go and
[10:42:43] use the partition by clause by the
[10:42:45] product ID. So now I would say that's it
[10:42:48] for the first step. So average by
[10:42:51] product. So let's go and execute it. So
[10:42:54] now if you check the result, you can see
[10:42:55] that we got our windows. So the first
[10:42:57] one for the product 101 and the total
[10:43:00] average of the sales going to be 35. So
[10:43:03] we have like aggregated one value for
[10:43:06] each window. The same thing for the next
[10:43:08] product and for the next and so on. So
[10:43:10] we don't have any progress over time or
[10:43:12] something like moving average all the
[10:43:14] time. Right? We don't have this effect.
[10:43:16] We have just one average for each
[10:43:18] window. So now in order to have the
[10:43:19] effect of the moving average, it's going
[10:43:21] to be like the running total. We have to
[10:43:23] use the aggregate function together with
[10:43:25] the order by. So I'm just going to make
[10:43:28] it in the new column. I'm just going to
[10:43:30] copy everything like here. And now what
[10:43:33] we going to do? Order by. And since it's
[10:43:36] over the time, we're going to go and use
[10:43:38] the order dates. Order dates. And we're
[10:43:42] going to have it ascending because it's
[10:43:44] overtime. Over time always like start
[10:43:46] with the earliest dates, end up with the
[10:43:48] latest dates. So from the lowest to the
[10:43:50] highest, we're going to leave it like
[10:43:52] this. So let's call it moving average.
[10:43:55] So now let's go and execute it. And we
[10:43:58] got here an extra comma because of the
[10:44:00] copy paste. So let's execute it again.
[10:44:02] All right. So now let's check the
[10:44:03] results. Let's take the first window
[10:44:05] over here. And you can see we have on
[10:44:07] the moving average like a progress. So
[10:44:10] it start with 10 15 14 35. So there is
[10:44:14] like moving average. We don't have one
[10:44:16] solid number for the average. We have
[10:44:19] different values. So now how SQL going
[10:44:21] to solve this? It's really simple. It's
[10:44:22] going to start row by row. So the first
[10:44:24] row what is the average of 10? It's
[10:44:26] going to be 10. Then moving on to the
[10:44:28] next one it's going to be 10 + 20
[10:44:30] divided by 2 you will get 15. So now
[10:44:33] moving to the third one all those three
[10:44:35] values going to be summarized divided by
[10:44:37] three you will get 40. And now to the
[10:44:40] last row in the window it's going to be
[10:44:42] summarizing all those four values
[10:44:44] divided by four and you will get 35. And
[10:44:47] this is exactly the same value in the
[10:44:49] previous column. You have here the
[10:44:51] average byproducts. We don't have order
[10:44:52] by you got as well 35 exactly like this
[10:44:56] last row and that's because we have the
[10:44:58] same calculation. It is summarizing all
[10:45:00] those four values dividing it by four.
[10:45:02] But now it's interesting the next value.
[10:45:05] So as you can see the next value it
[10:45:06] comes from another window. So you see
[10:45:09] here we have 15 for the product 102 but
[10:45:12] the average going to be as well 15. So
[10:45:15] scale is not considering the old values
[10:45:17] from the other window. So SQL going to
[10:45:20] calculate each window separately. So
[10:45:22] it's again here this is the first value
[10:45:24] of this window 15 the average 15 then
[10:45:27] the same stuff right. So summarizing
[10:45:29] those values divided by two and so on.
[10:45:31] And this we call in data analyzes this
[10:45:34] last field over here we call it a moving
[10:45:36] average and you can implement it very
[10:45:38] simply using an average function
[10:45:40] together with the order by. All right,
[10:45:42] let's move to the next task and it says
[10:45:44] calculate the moving average of sales
[10:45:46] for each product over time including
[10:45:49] only the next order. So as you can see
[10:45:52] the first part we have already done it
[10:45:53] right. We have the moving average and
[10:45:55] divided by partition by for the products
[10:45:58] but here we have more specifications. It
[10:46:00] says including only the next order. That
[10:46:03] means we are talking about the current
[10:46:05] order and as well the next order. So
[10:46:08] here we have like a fixed frame or fixed
[10:46:10] window. So we don't need the whole
[10:46:12] average of the window. We need only
[10:46:14] maximum two orders in each calculation.
[10:46:17] So how we going to do that? We can have
[10:46:19] our custom frame close inside our window
[10:46:22] function. So that means we cannot leave
[10:46:24] it as a default. We have to specify it.
[10:46:27] So let's go and do that. I will just
[10:46:29] copy the old definition of the window
[10:46:31] because we have the exact stuff. So we
[10:46:33] have the average sales over partition by
[10:46:36] product ID order by date. So this is the
[10:46:39] first part. So now we would like to have
[10:46:41] this fixed window. So we're going to go
[10:46:43] now and define our frame close. I'm just
[10:46:46] going to zoom out a little bit. It's
[10:46:48] going to be rows between. So we have now
[10:46:50] the boundaries of the frame. It says
[10:46:52] including the next order. So we're going
[10:46:54] to go and use the following. So the
[10:46:56] first boundary going to be the current
[10:46:58] row. And since it's next order, so it's
[10:47:00] going to be one following. So that is
[10:47:03] our frame including only the next order.
[10:47:06] And we have it like this one following.
[10:47:09] Let's call it yeah rolling average. So
[10:47:12] that's it. Let's go and execute. So now
[10:47:14] let's go and check the result. You can
[10:47:16] see the moving average has completely
[10:47:18] different values as the rolling average.
[10:47:20] So let's go and understand why. We're
[10:47:22] going to do it row by row. Let's take
[10:47:24] the first row over here. So the sales
[10:47:26] here is 10 and the rolling average is
[10:47:28] 15. So why is that? Because in the
[10:47:31] calculation we are considering the next
[10:47:33] value. So 10 + 20 divided by 2 you will
[10:47:37] get 15. So that means the SQL defined
[10:47:40] the frame like this those two rows for
[10:47:42] this calculation for the first row. So
[10:47:44] now moving on to the second row. SQL
[10:47:47] going to include as well the third one
[10:47:49] right the next one. But since the window
[10:47:52] is only two orders it's going to go and
[10:47:55] drop the first row. So the next frame
[10:47:57] going to be like this. And as you can
[10:48:00] see it's going to be 20 + 19 divided by
[10:48:02] 2. You will get 55. So now you can see
[10:48:05] the effect of the rolling average.
[10:48:07] Right? So now for the next one going to
[10:48:09] be exact same. So we are at the third
[10:48:11] row. It's going to go and include the
[10:48:12] next one and we're going to get the same
[10:48:14] value because 19 + 20 divid by two you
[10:48:17] will get 55. Now interesting to the last
[10:48:20] row in the window over here. It will not
[10:48:22] go and consider the next value because
[10:48:25] it is outside of the window. So it's
[10:48:27] going to be 20 and it's going to stay as
[10:48:30] well 20. So that's it. All right guys.
[10:48:32] So with that we have learned about the
[10:48:34] moving average, the rolling average and
[10:48:36] those amazing concepts using the window
[10:48:39] function. All right. Now we're going to
[10:48:40] have a quick overview of the different
[10:48:41] use cases in the aggregate functions and
[10:48:44] how the definition of the window going
[10:48:46] to change the whole use case. So now the
[10:48:48] first use case is finding the overall
[10:48:50] total. And here if you don't define
[10:48:53] anything in the window if you leave it
[10:48:54] empty what going to happen you are doing
[10:48:56] here overall analyzes. So you're going
[10:48:59] to go and aggregate the whole data sets
[10:49:02] and then provide this aggregation for
[10:49:04] each row. So this is what happen if you
[10:49:06] leave it empty. You don't define
[10:49:07] anything. You are aggregating the whole
[10:49:10] data sets. Now moving to the next step,
[10:49:12] we can do analysis called total pair
[10:49:14] groups. So what we're going to do, we
[10:49:17] will add partition by to the definition
[10:49:19] of the window. So by adding for example
[10:49:21] here partition by products, what can
[10:49:23] happen? The data going to be splitted
[10:49:25] into two categories or two groups and
[10:49:27] the aggregation going to be done for
[10:49:29] each window separately. This is of
[10:49:31] course a great analysis in order to go
[10:49:32] and compare different products like here
[10:49:35] the caps and gloves. So this is helpful
[10:49:37] in order to compare categories. So you
[10:49:39] can do this analysis total pair groups
[10:49:41] if you use the partition by. Now if you
[10:49:44] go and use the order by you're going to
[10:49:46] land in the third use case. As we
[10:49:48] learned we will be doing running total.
[10:49:50] So as you can see here in the output we
[10:49:52] are building a cumulative value for the
[10:49:54] sales and this going to help us in order
[10:49:56] to do progress over time analyzes in
[10:49:59] order to understand the performance of
[10:50:00] our business. And now moving on to the
[10:50:02] last use case the final phase of the
[10:50:04] window function with the aggregation.
[10:50:06] Here you have the aggregate function
[10:50:08] together with the order by with
[10:50:10] customized fixed window. And of course
[10:50:12] we can use it in order to help us
[10:50:14] building progress over time in specific
[10:50:16] fixed window. And of course you can use
[10:50:18] those use cases you will get the same
[10:50:20] effect if you use the other functions
[10:50:22] not only the sum you can use average
[10:50:24] count max so all aggregate functions. So
[10:50:26] guys as you can see the window function
[10:50:28] in scale is very important in order to
[10:50:30] do data analytics by just like changing
[10:50:33] the part of the window you are
[10:50:34] generating a whole new use case for data
[10:50:40] analytics. All right friends so now
[10:50:42] let's do a quick recap about the window
[10:50:44] aggregate functions. So what they do
[10:50:46] they're going to go and aggregate a set
[10:50:48] of values and return a single aggregated
[10:50:51] value for each row. So it's very similar
[10:50:53] to the groupy but here we don't lose
[10:50:55] details. Now to the next point what are
[10:50:57] the rules for the syntax about the
[10:51:00] expressions they all expect a number in
[10:51:02] the expression. So you have to pass a
[10:51:04] number like sales or any integer but
[10:51:06] only for the count you can go and use
[10:51:08] any data type. And the things for the
[10:51:10] aggregate functions are very simple.
[10:51:12] Everything is optional inside the
[10:51:13] definition of the overclouds or the
[10:51:15] definition of the window. So you can go
[10:51:17] and use partition by order by frames or
[10:51:19] not or just leave everything empty. So
[10:51:21] everything is optional. So now as we
[10:51:23] learned we have a lot of use cases for
[10:51:25] the aggregate functions and they are
[10:51:26] really amazing for analytics. So the
[10:51:28] first one the simplest one you can do
[10:51:30] overall analyzes if you just leave the
[10:51:32] window function empty. So you will get
[10:51:34] one big number about your business. And
[10:51:36] the next use case we can do total bear
[10:51:38] groups analyzes. As you've learned, we
[10:51:40] can use partition by in order to compare
[10:51:42] categories with each others like
[10:51:44] comparing the products or customers and
[10:51:45] so on. Moving on to the next one, we can
[10:51:47] do partto-hole analyszis. We can go and
[10:51:49] compare the performance of each data
[10:51:51] point with the overall. So you can for
[10:51:53] example compare the sales to the total
[10:51:55] sales in the window or to the all data
[10:51:58] sets. And we have many comparison
[10:52:00] analyzes. We can go and compare the
[10:52:01] current value with the average or we can
[10:52:04] compare them to the extreme to the
[10:52:05] highest sales to the lowest sales and so
[10:52:07] on. And another use case, we can go and
[10:52:09] identify data quality issues in our
[10:52:11] data. So we can go for example and
[10:52:13] identify duplicates using the count
[10:52:15] function. Moving on to the next use
[10:52:17] case, we have the outlier detection. We
[10:52:18] can go and find out which data points
[10:52:21] are above the average and below the
[10:52:23] average and so on. Then the next one we
[10:52:25] have the running total. As we learned,
[10:52:27] it is great tool in order to track the
[10:52:29] progress or to monitor the performance
[10:52:31] of our business over the time. Or if you
[10:52:34] want to be more specific, you can go and
[10:52:36] use the rolling total in order to have
[10:52:39] like a specific window and only track
[10:52:41] this window like three months or
[10:52:43] something like that. And the last use
[10:52:44] case, we can go and calculate the moving
[10:52:46] average of our data. So it's really
[10:52:48] amazing how order by and aggregate
[10:52:50] functions can open for you a door for
[10:52:52] amazing or advanced analyzers. So guys,
[10:52:55] as you can see, we have a lot of use
[10:52:56] cases for the window aggregate functions
[10:52:58] in the world of data analytics. All
[10:53:00] right. Right. So with that we have
[10:53:01] covered the aggregate window functions
[10:53:03] and in the next step it's going to be
[10:53:04] very important. We will learn how to
[10:53:06] rank our data using window functions. So
[10:53:09] let's
[10:53:12] go. All right. So now let's say that we
[10:53:14] have the following data. We have
[10:53:16] products and their sales. If you want
[10:53:18] now to go and rank your products first
[10:53:21] you have to sort the data based on
[10:53:22] something like for example ranking the
[10:53:25] products based on their sales. So that
[10:53:27] means SQL first is going to go and start
[10:53:29] sorting your data from the highest to
[10:53:31] the lowest. So sorting the data is
[10:53:33] always the first thing SQL has to do
[10:53:36] before ranking anything. Now in order to
[10:53:39] rank our data we have two methods. The
[10:53:41] first method we call it the integer
[10:53:43] based ranking. So that means SQL going
[10:53:45] to go and assign for each row an integer
[10:53:48] a whole number based on the position of
[10:53:51] the row. So now by looking to the
[10:53:52] example the first row we have the
[10:53:54] product E with the sales 70 it's going
[10:53:57] to be rank number one then the next row
[10:54:00] the product B with 30 sales we will get
[10:54:02] the rank number two then the next one
[10:54:04] going to be three four and the last one
[10:54:06] going to be five. So that means SQL here
[10:54:08] is assigning an integer for each row
[10:54:11] based on their position in the sorted
[10:54:13] list. So this method we call it integer
[10:54:16] based ranking. Now let's go to the
[10:54:18] second method we have the
[10:54:19] percentagebased ranking. So in this
[10:54:21] methods going to go first and calculate
[10:54:24] the relative position of the row
[10:54:26] compared to all others and then assign a
[10:54:29] percentage for each row. So here in the
[10:54:31] output is going to start assigning
[10:54:33] percentages instead of integer and we're
[10:54:36] going to have a scale from 0 to one. So
[10:54:38] now if you go and compare both of the
[10:54:40] methods you can see that on the left
[10:54:41] side on the integer base ranking we have
[10:54:44] discrete distinct values. So it starts
[10:54:47] from 1 then 2 3 and end up in this
[10:54:50] example by five. So it really depends on
[10:54:52] how many rows do we have in the results.
[10:54:55] So it could be five, it could be 500, 5
[10:54:57] million and so on. But in the right side
[10:55:00] we have always the same scale from one
[10:55:02] to zero. So between 0 and one we have
[10:55:05] infinite number of data points and this
[10:55:07] scale we call it a normalized scale or
[10:55:10] we call it continuous scale continuous
[10:55:13] values. So now the question is when to
[10:55:15] use which method. So for example for the
[10:55:17] percentage based ranking it is great to
[10:55:19] answer such questions find the top 20%
[10:55:22] products based on their sales. So this
[10:55:24] method is a great way in order to
[10:55:26] understand the contributions of data
[10:55:28] values to the overall total and we call
[10:55:31] this kind of analyszis a distribution
[10:55:34] analyszis where in the other hand in the
[10:55:36] integer based ranking we can answer
[10:55:38] questions like find the top three
[10:55:40] products. So with this question we are
[10:55:42] not interesting about the contributions
[10:55:44] of each product to the overall total. We
[10:55:46] are just interested in the position of
[10:55:49] the value within a list. So this is as
[10:55:51] well very commonly used analyzes and
[10:55:53] reporting. We call it top button in
[10:55:56] analyzers. So now let's group up our
[10:55:57] ranking functions based on those two
[10:55:59] methods. For the first group in the
[10:56:01] integer based ranking we have four
[10:56:03] functions. Row number rank d rank and
[10:56:06] inile. But in the other hand we have
[10:56:08] only two functions that generate
[10:56:10] percentage based ranking. We have the
[10:56:12] cumid list and as well the percentile.
[10:56:15] So now that was an introduction an
[10:56:17] overview of those methods and how we
[10:56:19] group up those ranking functions. Next
[10:56:21] we're going to go and learn about the
[10:56:23] syntax of the ranking functions. Most of
[10:56:25] them follow the same rules. So for
[10:56:27] example we start always with the
[10:56:28] function name. So we have here the rank.
[10:56:31] But as you can see we don't use any
[10:56:33] expressions. So they don't allow you to
[10:56:35] use any argument inside it. It must be
[10:56:38] empty. So this is the first rule using
[10:56:40] rank functions. Then about the
[10:56:42] definition of the window as usual the
[10:56:44] partition by it is an optional thing.
[10:56:46] You can use it or leave it. And now to
[10:56:48] the second part we have the order by it
[10:56:50] is as well required. So you must order
[10:56:53] the data or sort your data in order to
[10:56:55] do ranking. So you cannot leave it
[10:56:57] empty. So that means for the definition
[10:56:59] of the window at least we should have an
[10:57:02] order by for example here sales. So we
[10:57:05] cannot leave it empty. All right. So the
[10:57:07] two requirements you cannot use any
[10:57:09] expressions for those functions and as
[10:57:11] well you have to sort your data using
[10:57:13] order by. Okay. So now let's have an
[10:57:15] overview of all functions. So as you can
[10:57:16] see all those functions are ranking
[10:57:18] functions and almost all of them don't
[10:57:21] allow to use any expressions inside
[10:57:23] them. Beside this function here we have
[10:57:26] the end tile. it accepts a number inside
[10:57:28] it. So that means you cannot use it
[10:57:30] empty. You should use a number inside
[10:57:32] it. All others must be empty. So now for
[10:57:35] the partition by all of them are
[10:57:36] optional and for the order by all of
[10:57:39] them are required. So you must use order
[10:57:42] by and the frame clause they are not
[10:57:45] allowed to use in the ranking functions.
[10:57:47] So you cannot change the definition of
[10:57:49] the frame inside the window function. So
[10:57:51] now what we're going to do as usual,
[10:57:53] we're going to go and deep dive into all
[10:57:54] of those functions in order to
[10:57:56] understand when to use them and what are
[10:57:58] the use cases and as well practice in
[10:58:01] SQL. So we're going to start with the
[10:58:02] first one, the row
[10:58:07] number. All right. So what is a row
[10:58:09] number in SQL? The row number function
[10:58:11] going to go and assign for each row a
[10:58:13] unique number as a rank and it doesn't
[10:58:16] care at all about the ties. That means
[10:58:18] if you have two rows sharing the same
[10:58:21] value, they will not share the same
[10:58:23] rank. Okay. So now we have very simple
[10:58:25] example. We have a list of all sales and
[10:58:28] we have the following query. So it's
[10:58:30] going to start with the ranking function
[10:58:32] row number. It doesn't accept any
[10:58:33] argument inside it. And the definition
[10:58:35] of the window going to be like this
[10:58:37] order by sales disk. So that means we're
[10:58:40] going to go and sort the data descending
[10:58:43] from the highest to the lowest. So SQL
[10:58:45] going to go and do the following. The
[10:58:47] highest going to be the 100. The lowest
[10:58:50] going to be the 20. And here we have
[10:58:52] twice the 80. So now once SQL done
[10:58:55] sorting the data, what's going to
[10:58:56] happen? It's going to start assigning a
[10:58:58] rank. So the row number going to go and
[10:59:00] assign a unique number for each row. So
[10:59:03] that means it's going to start with the
[10:59:04] first one. The 100 going to be the rank
[10:59:07] number one. The next one going to be
[10:59:09] rank number two. The 80 going to be rank
[10:59:11] number three. And the 54. And then the
[10:59:14] last one going to be five. And now if
[10:59:15] you check the output you can see that
[10:59:17] all those numbers are unique. We don't
[10:59:19] have any repetitions. So 1 2 3 4 5
[10:59:22] there's no repetitions. They are unique
[10:59:24] distinct value. And as well there are no
[10:59:27] skipping of ranking. So that means we
[10:59:29] have here 1 2 3 there is no jumping to 6
[10:59:31] 7 or something. They are clear sequence
[10:59:34] of distinct value and there are no gaps.
[10:59:37] But still there is something special in
[10:59:38] our data. We can see that in the sales
[10:59:41] we have the same value twice. So we have
[10:59:43] two rows with the same sales. As you can
[10:59:46] see in the row number they will get
[10:59:48] distinct values. So they will not share
[10:59:51] the same ranking. So that means row
[10:59:53] number does not handle the ties. If you
[10:59:56] have multiple rows sharing the same
[10:59:58] values they will not share the same
[11:00:00] rank. They going to have a distinct rank
[11:00:03] different ranks. So this is how the row
[11:00:05] number works in SQL. It generates unique
[11:00:07] ranks for each row. It does not handle
[11:00:09] the ties and as well it doesn't leave
[11:00:12] any gaps. So there is no skipping of
[11:00:14] ranking. So now let's go to SQL in order
[11:00:16] to have few examples and use cases. All
[11:00:18] right. So now we have the following
[11:00:19] task. It's very simple. Rank the orders
[11:00:21] based on their sales from the highest to
[11:00:24] the lowest. So now this is very easy.
[11:00:25] We're going to go and select first the
[11:00:27] data. So order ID, product ID. Let's
[11:00:30] take the sales as well and select the
[11:00:33] table. So it's going to be sales orders.
[11:00:35] Let's go and execute it. So with that we
[11:00:38] got all our orders. What we're going to
[11:00:39] do now is to assign for each row a rank.
[11:00:42] So that means we need a column here that
[11:00:45] contains the rank for each row. So in
[11:00:47] order to do that we're going to go and
[11:00:48] use the window function row number. It
[11:00:51] doesn't accept any argument inside it.
[11:00:53] So should be empty. And then we have to
[11:00:54] define the window. So as we learned in
[11:00:57] the ranking functions we cannot leave it
[11:00:59] empty. We have to sort the data using
[11:01:00] order by. So order by is a must. We
[11:01:03] don't have to use any partition by. So
[11:01:05] we're going to rank all the data that we
[11:01:07] have inside the table. So how to sort
[11:01:08] the data? It says it should be based on
[11:01:10] their sales from highest to lowest. That
[11:01:12] means we order by sales since from
[11:01:15] highest to lowest we have to use the
[11:01:17] descending. And now we're going to go
[11:01:18] and give it a name sales rank and let's
[11:01:21] say row since we are using the row
[11:01:23] number. So that's it. It's very simple.
[11:01:26] Let's go and execute it. So now let's
[11:01:28] have a look to the results. Before SQL
[11:01:30] did sort the data by the order ID since
[11:01:32] we didn't define anything. But since now
[11:01:34] we are order by sale descending SQL went
[11:01:37] and sorted the data by the sales from
[11:01:39] the highest to the lowest and start
[11:01:41] assigning a rank or let's say an integer
[11:01:44] unique integer for each row. So now the
[11:01:46] highest order going to be the order
[11:01:48] number eight. We have the sales of 90.
[11:01:50] This is the highest one. So as you can
[11:01:51] see we have 1 2 3 4 5 until 10. So now
[11:01:54] by checking the results you can see that
[11:01:56] the ranking here is unique. So there is
[11:01:59] no duplicates over here and as well
[11:02:01] there is no skipping or gaps. So we have
[11:02:03] everything between 1 and 10 even though
[11:02:05] that we have in our data a couple of
[11:02:07] sales that sharing the same value. So
[11:02:09] for example we have those two orders you
[11:02:11] can see both of them has the 60 at the
[11:02:13] sales but they don't share the same
[11:02:15] ranking. Right? So we have here as well
[11:02:18] the 9 and three they share the same
[11:02:20] value 20 but they don't share the same
[11:02:22] ranking. So with that we have solved the
[11:02:24] task. It's very simple. We have now a
[11:02:26] rank based on the sales from highest to
[11:02:28] the lowest.
[11:02:33] All right. So what is a rank function in
[11:02:35] SQL? The rank function going to go and
[11:02:36] assign for each row a number a rank and
[11:02:39] this time it going to go and handle the
[11:02:41] ties. So that means if in your data you
[11:02:44] have two rows having the same values
[11:02:46] they going to share the same ranking.
[11:02:49] One thing about the ranking function is
[11:02:50] that it's going to go and leave gaps in
[11:02:52] the ranking. So there is possibility of
[11:02:55] skipping ranks. In order to understand
[11:02:57] how the rank function works in SQL,
[11:02:59] we're going to have a very simple
[11:03:00] example. All right. So again with the
[11:03:02] same data but with different function.
[11:03:04] So our window looks like this. It start
[11:03:06] with the function rank doesn't accept
[11:03:08] any argument inside it. Then we have the
[11:03:10] window like this. Order by sales
[11:03:12] descending from the highest to the
[11:03:14] lowest. And our data is already sorted
[11:03:16] like that. So now how is scale going to
[11:03:18] go and assign the ranks. The first row
[11:03:19] going to be the highest rank. So the
[11:03:21] value 100 is going to be one. Then the
[11:03:23] second one going to be two. But now for
[11:03:25] the third one, as you can see, we have
[11:03:27] here two values that are the same. So we
[11:03:29] have a tie and this time SQL going to go
[11:03:32] and as well let them to share the same
[11:03:35] rank. So both of them going to be the
[11:03:37] rank two. So it's not like the row
[11:03:39] number where we have over here three.
[11:03:41] This time we have two because we have a
[11:03:43] tie. So having same values means they
[11:03:46] going to share the same rank. And now
[11:03:48] moving to the next value going to be
[11:03:49] tricky one because if you check over
[11:03:51] here you can see that the next rank
[11:03:54] should be like the three right? So we
[11:03:55] have one two and then the next value
[11:03:57] that generated in the rank should be
[11:03:59] three but going to say you know what
[11:04:02] this value position going to be number
[11:04:04] four. So as you can see 1 2 3 four. So
[11:04:07] actually the position number here is
[11:04:08] four and going to go and give it the
[11:04:11] rank of four. So with that SQL going to
[11:04:14] be leaving a gap in the ranking. You can
[11:04:16] see we are skipping the rank number
[11:04:18] three and this always happen once you
[11:04:20] have a tie where you are sharing the
[11:04:22] same ranking. So for the next one it's
[11:04:24] going to be easy. It's going to be the
[11:04:25] row number five. So now by looking to
[11:04:27] the output of the rank function you can
[11:04:29] see that we don't have a unique ranking.
[11:04:31] Here we have shared ranking in case of
[11:04:34] the ties. So it handles the ties but
[11:04:36] here we have gaps in the ranks. So we
[11:04:39] are skipping ranks. When I think about
[11:04:41] the rank function I think about the
[11:04:43] Olympics. If two athletes tie for the
[11:04:46] gold medal, the first place, there will
[11:04:49] be no silver medal for the second place,
[11:04:51] the next medal going to be given to the
[11:04:53] bronze to the third place. All right. So
[11:04:55] now let's go in SQL in order to practice
[11:04:57] the rank function. All right. Now we're
[11:04:59] going to go and solve the same task but
[11:05:00] using the rank function. So what we're
[11:05:03] going to do, we're going to stay with
[11:05:04] the same example over here and we're
[11:05:05] going to rank the order based on their
[11:05:07] sales from highest to lowest but this
[11:05:08] time using the rank function. So we use
[11:05:12] the rank and everything inside is going
[11:05:14] to be empty and then our window going to
[11:05:16] be exactly the same as before. So over
[11:05:19] order by sales and disk. So let's give
[11:05:23] it a name sales rank. Yeah, let's give
[11:05:26] it a rank. So that's it. As you can see
[11:05:28] the syntax is very simple and very
[11:05:29] similar to the row number. We just
[11:05:31] changed the function. So now let's go
[11:05:33] and execute this in order to check the
[11:05:35] results. So now let's go and check the
[11:05:37] results by looking to the new rank. If
[11:05:39] you go and compare it with the old rank,
[11:05:41] we can see that we are sharing some
[11:05:43] ranking, right? We have here the two
[11:05:44] twice. So the rank number two, we have
[11:05:46] it twice because we have over here the
[11:05:49] same value. So 60 60 we have it here two
[11:05:51] and two. But if you compare it to the
[11:05:54] row number, you can see that it is not
[11:05:56] sharing the same ranking. So this is one
[11:05:58] difference. And as well here the same
[11:06:00] thing. They have the same value. The
[11:06:01] sales is 20. So we have it twice the
[11:06:04] rank number seven. And here we have it
[11:06:06] as different values. And the next value
[11:06:08] as you can see we are skipping the rank.
[11:06:11] So there is gap there is no rank of
[11:06:13] eight. So you can see that this is the
[11:06:15] row number nine and that's why it get
[11:06:17] the nine. The same thing I believe over
[11:06:20] here. So now if you check those two
[11:06:21] ranks the next one should be three. But
[11:06:23] since it is in the row number four it's
[11:06:26] going to get the rank four. So by
[11:06:27] checking the results we can see that
[11:06:29] sharing the same ranks and as well we
[11:06:31] have gaps. So this is how the rank
[11:06:33] works.
[11:06:37] All right. So what is a dense rank? It
[11:06:40] is very similar to the ranking function.
[11:06:41] It's going to go and assign for each row
[11:06:43] a number rank and it as well handles the
[11:06:46] ties. So same values they going to share
[11:06:48] the same ranking but this time it
[11:06:51] doesn't leave any gaps like the rank
[11:06:53] function. So the d rank it will not
[11:06:56] leave any gaps. It will not skip any
[11:06:58] ranking. So in order to understand this
[11:07:00] we're going to have a very simple
[11:07:01] example. So let's go. All right. So
[11:07:03] again the same data but with different
[11:07:04] function. We have this time the rank
[11:07:06] function dense rank and the window going
[11:07:09] to be the same order by sales descending
[11:07:11] from the highest to the lowest. So now
[11:07:13] the data is as well sorted already.
[11:07:15] Let's see how SQL going to go and assign
[11:07:17] the ranks as usual. The first row going
[11:07:19] to be the rank number one the second as
[11:07:22] well but again here we have the same
[11:07:24] values. So we have same values and it's
[11:07:26] like the rank it's going to go and share
[11:07:28] the same rank. So both of them going to
[11:07:30] has the rank number two. And now you
[11:07:33] might say, well this is very similar to
[11:07:35] the rank function. So why do we have
[11:07:36] dense rank? I'm going to say wait for
[11:07:38] it. We're going to have the difference
[11:07:40] in the next value. So it's going to come
[11:07:42] over here. This value is exactly after
[11:07:44] the tie. In rank SQL went and took the
[11:07:47] position number. So the row number it
[11:07:49] was four, right? So 1 2 3 4. But this
[11:07:52] time with the dense rank SQL will not
[11:07:54] leave gaps in ranking. So there will be
[11:07:56] no skipping the next rank in the
[11:07:59] sequence going to be three. So that's
[11:08:01] why we're going to have the rank three
[11:08:03] for this value. So as you can see there
[11:08:05] is no gap. We have one, we have two and
[11:08:07] three. So we are not skipping, we are
[11:08:09] not leaving any gaps. And the last one
[11:08:11] going to be four. So this is exactly the
[11:08:14] difference between the dense rank and
[11:08:15] the rank. So now by checking the output
[11:08:17] of the dense rank, you can see that we
[11:08:20] don't have unique ranks. We have here
[11:08:23] shared ranks. As you can see, we have
[11:08:24] here repetition. So, it handles the ties
[11:08:27] and as well it doesn't leave any gaps.
[11:08:30] It doesn't skip anything in the ranking.
[11:08:32] Okay, so that's it. Now, let's go back
[11:08:34] to SQL to practice the dense rank. All
[11:08:36] right, so now we have the same task.
[11:08:37] Rank the orders based on their sales
[11:08:39] from highest to lowest. So, we're going
[11:08:41] to do the same stuff, but this time
[11:08:42] using the function dense rank. So, dense
[11:08:46] rank is going to be empty. And then
[11:08:47] we're going to define it like all others
[11:08:49] over order by sales disk. And then we're
[11:08:53] going to give it the name of sales rank
[11:08:55] dense. And that's it. So as you can see
[11:08:57] all of those functions having the exact
[11:09:00] syntax, right? So let's go and execute
[11:09:02] it. Okay. So now let's go and check the
[11:09:04] results. We got our newest rank using
[11:09:06] the dens. And by just checking the
[11:09:08] results, you can see that it handles the
[11:09:10] tie. We have two twice, right? So let's
[11:09:13] check the example over here. We have the
[11:09:15] sales 60 twice. That's why they are
[11:09:17] sharing the same ranking in the dense
[11:09:19] and as well in the normal rank. But now
[11:09:21] what is interesting is the value after
[11:09:24] the tie. So as you can see over here
[11:09:26] with the dense rank we have three. So we
[11:09:28] didn't skip any ranking. We don't have
[11:09:30] any gap 1 2 and then three. But with the
[11:09:33] rank it's just focus on the position
[11:09:36] number. So it is the row number four.
[11:09:38] That's why it's four. With that we have
[11:09:40] a gap. So as you can see now we don't
[11:09:42] have any gaps in the dense rank. So we
[11:09:44] have three four five. And now we have
[11:09:46] over here the same two values. So we
[11:09:49] have sales of 2020 and they share the
[11:09:51] six twice. So as you can see there is
[11:09:53] difference now between the dense and the
[11:09:55] rank. So here we have seven seven but
[11:09:57] here we are at the rank 66. So that's
[11:09:59] why we have differences between them
[11:10:01] because we skipped before in the rank
[11:10:03] number three. Now the other stuff you
[11:10:04] can see we have seven and eight. So now
[11:10:06] if you compare those three ranking you
[11:10:08] can see that they all start with the
[11:10:10] rank number one but they didn't all end
[11:10:13] with the same ranking. So the row number
[11:10:15] and the rank they really focus on the
[11:10:18] position number or the row number of the
[11:10:20] orders. So you can see over here it is
[11:10:23] the row number 10. That's why we have
[11:10:24] here 10 and 10. So the scale is from 1
[11:10:27] to 10. And that is exactly the same for
[11:10:30] the row number from 1 to 10. But with
[11:10:32] the d over here we have it from 1 to 8
[11:10:35] and that's because we shared the same
[11:10:37] ranking and with that we wasted let's
[11:10:40] say few ranks. So the scale is different
[11:10:42] from the two others. And that's because
[11:10:44] we have ties twice. This is one tie and
[11:10:47] as well we have over here one tie.
[11:10:49] That's why we are missing over here two
[11:10:51] ranks. So this is how the dense ranks
[11:10:53] works. And you can go and compare now
[11:10:55] all three togethers in order to
[11:10:57] understand how those ranks are
[11:11:02] working. All right. So now let's quickly
[11:11:04] compare the three functions side by
[11:11:05] side. Let's start with the first point
[11:11:07] about the uniqueness of the rank. And if
[11:11:09] you compare those three you can see that
[11:11:11] only the row number generates unique
[11:11:13] distinct rank. So this going to be
[11:11:16] unique rank and the two others we have
[11:11:18] duplicates or let's say shared ranks.
[11:11:20] Okay. So now the second point whether
[11:11:22] the function handles the ties and the
[11:11:25] only one that doesn't handle the ties is
[11:11:26] the row number. So this one doesn't
[11:11:29] handle the ties and the two others
[11:11:31] handles the ties since they offer the
[11:11:33] shared rank. And now we have the last
[11:11:35] point about leaving gaps or skipping
[11:11:37] ranking. So now if you check the row
[11:11:39] number and the dense rank you can see
[11:11:41] there will be no skipping. So there is
[11:11:44] no gaps for the row number and as well
[11:11:47] for the dense rank only for the rank
[11:11:50] function the middle one we are skipping
[11:11:52] ranks and we are leaving gaps. So that's
[11:11:54] it guys. This is the differences between
[11:11:56] those three functions. I tend usually to
[11:11:58] work with the row number more often than
[11:12:00] that to others.
[11:12:05] All right guys, so now I had a look to
[11:12:07] those three functions and I checked my
[11:12:09] projects real projects and I found out
[11:12:11] that there are many use cases for the
[11:12:14] function row number compared to the
[11:12:16] other functions dense rank and rank. So
[11:12:19] now what we're going to do I'm going to
[11:12:20] show you a few use cases for the rank
[11:12:22] number that I usually use in my real
[11:12:24] projects in order for you to understand
[11:12:26] how important is the row number
[11:12:28] function. So let's go to SQL. All right.
[11:12:30] So now let's start with the first use
[11:12:31] case and we have the task of find the
[11:12:33] top highest sales for each product. So
[11:12:36] this is very classic in reporting or
[11:12:39] data analyzes. We call this top end
[11:12:41] analyzes. So here the managers or
[11:12:44] decision makers they would like to have
[11:12:46] the best performers or the best success
[11:12:48] in our data. So for example the top
[11:12:50] highest five customers or the top five
[11:12:54] products or categories and so on. So
[11:12:56] this is very important analyzis in order
[11:12:58] to focus on the best products or on to
[11:13:01] the most important customers and so on
[11:13:03] and this is as I said very classic and
[11:13:05] very important in order to make
[11:13:06] decisions in the business. So now let's
[11:13:09] see how we can solve this. So we're
[11:13:10] going to start with the usual stuff.
[11:13:11] Let's first select the data. So select
[11:13:14] order ID. Let's take as well the product
[11:13:16] ID and the sales from sales orders. So
[11:13:21] let's go and execute this. And now as we
[11:13:23] know that for each product we have
[11:13:24] multiple orders and we have multiple
[11:13:26] sales but we are interested only in the
[11:13:28] highest sales for each product. So we
[11:13:30] have to go and create a rank. In order
[11:13:32] to do that we're going to use the row
[11:13:34] function row number and we have to
[11:13:37] define the window now. So do we need
[11:13:38] partition by check the query. So it says
[11:13:42] for each product that means we have to
[11:13:44] divide the data by the product ID. So
[11:13:46] let's go and use the partition by
[11:13:49] products ID. And now we must use the
[11:13:52] order by. So order by. And now how to
[11:13:55] sort the data by the sales, right? And
[11:13:57] it is from the highest to the lowest. So
[11:13:59] let's go sales. And we have here
[11:14:02] descending. So from highest to lowest.
[11:14:04] Let's go and give it a name. So you're
[11:14:06] going to be rank by products. So let's
[11:14:09] go and execute this. And now by looking
[11:14:12] to the result, you can see that SQL did
[11:14:14] divide the data by the product ID. So we
[11:14:16] have here like around four windows. The
[11:14:19] first one over here you can see that the
[11:14:20] rank starts from one end with four. So
[11:14:23] the highest rank can be the order number
[11:14:25] eight with the sales of 90 and then it
[11:14:28] goes to the four. Now as you can see
[11:14:31] that the second window we have a new
[11:14:33] ranking. So it resets the first going to
[11:14:35] be uh the order number 10 and the last
[11:14:38] one going to be order number two. So as
[11:14:40] you can see each window has its own
[11:14:42] ranking and as well the last one we have
[11:14:44] it only as one row. So now of course in
[11:14:47] the task we have to return the highest.
[11:14:49] So we are not interested in the others.
[11:14:51] We have to return this row this row as
[11:14:53] well and this one and this one. So as
[11:14:55] you can see we have to return everything
[11:14:57] that has the rank one. We are not
[11:14:59] interested in the rank 2 3 4 and so on.
[11:15:02] So we would like to have the highest. So
[11:15:04] now in order to filter the data what
[11:15:05] we're going to do we're going to go and
[11:15:07] use subqueries. So select star from and
[11:15:12] then we're going to have the following
[11:15:13] condition. So where and we're going to
[11:15:15] say rank by product equals to one. So we
[11:15:20] are interested only on the rank number
[11:15:22] one. So let's go and execute it. And
[11:15:24] with that since we have four products in
[11:15:26] our data, we're going to have only four
[11:15:27] rows and we have the highest sales. So
[11:15:30] as you can see we have only number one
[11:15:31] over here. And those sales are the
[11:15:33] highest for each product. And with that
[11:15:36] we have solved the tasks by finding the
[11:15:38] top end analyzers.
[11:15:44] Okay, moving on to the next use case. We
[11:15:46] have the following task and it says find
[11:15:48] the lowest two customers based on their
[11:15:50] total sales. So now we have the exact
[11:15:53] opposite use case. We call it button in
[11:15:56] analyzes. So now in this example in the
[11:15:58] business the decision makers want to
[11:16:00] optimize the costs want to cut costs and
[11:16:03] with that they have to analyze the
[11:16:05] lowest performers in the products or the
[11:16:08] lowest performance in the employees in
[11:16:10] order to cut costs. So now with this
[11:16:12] analysis the decision makers are not
[11:16:14] focusing on the best successful stuff.
[11:16:16] We are focusing on the lowest stuff the
[11:16:19] lowest performers. So now let's solve
[11:16:21] this tasks. So now if you check the
[11:16:22] question we have multiple stuff right we
[11:16:24] have the total sales and as well we have
[11:16:27] to find the lowest two customers. So we
[11:16:29] have ranking and as well aggregations
[11:16:31] remember we can do stuff together with
[11:16:33] the group I. So now let's do it step by
[11:16:35] step. First let's select the data right.
[11:16:37] So what do we need? Order ID customer ID
[11:16:40] and we need the sales from sales orders.
[11:16:45] So let's go and execute this. So now if
[11:16:47] you check the customers over here we
[11:16:49] have around four customers and they have
[11:16:51] multiple sales. Now we would like to
[11:16:53] have the total sales for each customers
[11:16:56] in order to find the lowest two. So
[11:16:58] let's start first with the aggregations.
[11:17:00] So what we going to do? We're going to
[11:17:01] go and aggregate the sales. So the sum
[11:17:03] of sales and let's call it total sales.
[11:17:06] And now in order to do the group by we
[11:17:08] have to have only the customer. So group
[11:17:11] by and we have the customer ID. So it is
[11:17:14] very simple group by statements. Let's
[11:17:17] go and execute this. So now by checking
[11:17:18] the result we can see that SQL did
[11:17:20] aggregate the data. We have four rows
[11:17:22] and that's because we have four
[11:17:23] customers and we have their total sales.
[11:17:26] So we have solved the first part of the
[11:17:28] task. We have the total sales for each
[11:17:30] customers. Now let's move to the second
[11:17:32] part. It says lowest two customers. That
[11:17:35] means we have to use the ranking
[11:17:37] functions in order to rank those
[11:17:39] customers. So we are not interested in
[11:17:41] all customers. We are interested only in
[11:17:43] the lowest two. So in order to do that
[11:17:45] now we're going to go and use the window
[11:17:47] function row number. So and then over.
[11:17:51] Now do we have to partition the data?
[11:17:53] Well no we don't have to do that. We
[11:17:55] have now to sort the data. So order by.
[11:17:58] So this time we're going to go and use
[11:18:00] the aggregations in the order by. So the
[11:18:02] sum of sales and we want to have it
[11:18:04] sorted from the lowest to the highest.
[11:18:07] So I'm just going to go and use the
[11:18:09] defaults. So it is ascending. Now let's
[11:18:11] call it rank customers. So that's it.
[11:18:15] Again here the rule is that if you are
[11:18:17] using a window function together with
[11:18:19] the group by function, you have to use
[11:18:21] only columns that is used in the group
[11:18:23] by. So this should be working. Let's go
[11:18:25] and execute it. So now as you can see in
[11:18:27] the results, we got an extra column for
[11:18:29] the rank. So now the lowest customer
[11:18:31] going to be the customer number two. The
[11:18:33] second one going to be four with the 90
[11:18:35] total sales. And the highest customer
[11:18:38] with the sales is going to be the last
[11:18:39] one, the 125 customer number three. So
[11:18:42] now we have almost everything but the
[11:18:44] list should contain only the last two.
[11:18:47] So in order to do that to filter the
[11:18:48] data, we're going to go and use
[11:18:50] subquery. So select star
[11:18:53] from and then we have to define the
[11:18:55] condition where rank customers it should
[11:18:59] be smaller or equal to two. Right? So
[11:19:01] with that we will get the first two. So
[11:19:03] let's go and execute this. And with that
[11:19:06] we got the lowest two customers based on
[11:19:08] their total sales. So customer number ID
[11:19:10] you two and the four. So that's it. We
[11:19:13] have solved the task and now we have
[11:19:14] done button in
[11:19:19] analyzes. Okay let's keep moving to the
[11:19:21] next use case and we have the following
[11:19:23] task. It says assign unique ids to the
[11:19:26] rows of the table orders archive. So now
[11:19:30] guys we might be in situation where you
[11:19:31] have a table without any primary key and
[11:19:34] you would like to create an ID for each
[11:19:36] row. So in order to do that we can use
[11:19:38] the function row number in order to
[11:19:40] generate unique identifier ids for each
[11:19:44] row inside our table if we don't have
[11:19:45] one. And generating such ID for each
[11:19:47] row. It's very important to do stuff
[11:19:49] like importing data, exporting data,
[11:19:52] maybe joining tables as well using this
[11:19:53] ID or let's say optimizing the
[11:19:56] performance of query using the ID. So
[11:19:58] now let's see how we can generate that
[11:20:00] using row number. Okay. So now let's
[11:20:02] first select the table order archives in
[11:20:04] order to understand the content. So
[11:20:06] select star from sales orders archive.
[11:20:11] So let's go and execute. So now by
[11:20:13] checking the result you can see that we
[11:20:14] have 10 orders and we have repetitions
[11:20:17] in the order ID over here. So it is not
[11:20:19] really primary key. As you can see over
[11:20:21] here we have twice the ID four and here
[11:20:24] we have three times the ID6. So now what
[11:20:26] we're going to do we're going to go and
[11:20:27] generate unique identifier for each row.
[11:20:30] So in order to do that what we're going
[11:20:32] to do going to go over here and say row
[11:20:34] number and then we're going to define
[11:20:37] the window function. We don't partition
[11:20:39] the data at all but we have to sort the
[11:20:41] data by the order ID. So order by order
[11:20:46] ID or you can use something else as well
[11:20:48] using the order date or something
[11:20:50] doesn't matter. So let's add to it order
[11:20:53] data as well and let's call it unique
[11:20:57] ID. Let's go and execute this. Now by
[11:21:00] checking the data you can see that we
[11:21:02] have a new ID over here that comes from
[11:21:05] the row number and we have like a unique
[11:21:08] identifier. As you can see we have 10
[11:21:10] rows and with that we have as well 10
[11:21:12] different distinct unique ids. So with
[11:21:14] this as you can see we have solved the
[11:21:16] task and we have now a unique identifier
[11:21:18] an ID for the table orders archive. So
[11:21:21] now having this ID we can do many stuff
[11:21:23] like joining tables or doing something
[11:21:25] special and important called pagenating.
[11:21:28] Imagine we have like a huge table and we
[11:21:30] would like to retrieve the data. So now
[11:21:32] in order to not have all the data in one
[11:21:35] go we can go and divide the data by the
[11:21:37] primary ID or by unique identifier. For
[11:21:39] example, we can make a page from 1 until
[11:21:42] 100,000 and then the second page starts
[11:21:45] from 100K to 200ks. So now by dividing
[11:21:48] the data, we can maybe improve exporting
[11:21:50] or importing data or we can have faster
[11:21:53] retrieval for the users. We don't want
[11:21:54] to have the whole data in one go in one
[11:21:57] page. So it has a lot of benefits using
[11:21:59] pagionating and we can do that only if
[11:22:01] we have a nice ID like
[11:22:06] this. All right. Right. Today I'm going
[11:22:08] to show you the last use case for the
[11:22:09] function row number that I usually use
[11:22:11] in my real projects. So sometimes if you
[11:22:14] are doing data analyszis you're going to
[11:22:15] find out that there are data quality
[11:22:17] issues especially with the duplicates.
[11:22:19] So what I usually use I use the raw
[11:22:22] number in order to identify the
[11:22:23] duplicates. Not only that I can use it
[11:22:26] in order to delete the duplicates. So we
[11:22:28] can use it in order to do data
[11:22:30] cleansing. And this is essential task
[11:22:32] for each data engineer not only data
[11:22:35] analysts in order to prepare and clean
[11:22:37] up the data before doing data analyzes.
[11:22:40] So let's have the following task.
[11:22:41] Identify duplicate rows in the table
[11:22:43] orders archive and return a clean result
[11:22:46] without any duplicates. So not only we
[11:22:48] have to identify the duplicates, we have
[11:22:50] to return no duplicates in our results.
[11:22:53] So let's see how we can do this. Let's
[11:22:54] first select the data. So select star
[11:22:56] from sales orders archive. So let's go
[11:23:01] and execute. So now by looking to the
[11:23:02] data you can see that we have
[11:23:03] duplicates. We have an issue. So the
[11:23:05] order ID number four is twice in our
[11:23:07] database. It doesn't make sense, right?
[11:23:09] It should be only one. So which one is
[11:23:11] the correct one? If you check the data
[11:23:13] over here, you can see that this order
[11:23:15] is shipped and then delivered. So it
[11:23:17] looks like the last one is the correct
[11:23:19] one. So how we can do that? If you just
[11:23:22] scroll to the right, you can see that we
[11:23:24] have a creation time. And we usually use
[11:23:26] such a time stamp in order to identify
[11:23:29] what was the last valid like order. And
[11:23:32] here we can see immediately that this
[11:23:34] order time is higher than the previous
[11:23:36] one. Which means this is the more up to
[11:23:38] date, right? The more current. So what
[11:23:41] we're going to do, we're going to go and
[11:23:43] rank our data for each order ID and sort
[11:23:46] the data by the creation time in order
[11:23:48] to find the last inserted or created row
[11:23:51] for this order. So let's see how we can
[11:23:53] do that. What we going to do? We're
[11:23:55] going to go over here and say let's have
[11:23:57] a row number and then over and what
[11:24:01] we're going to do, we're going to
[11:24:02] partition by the primary key. So
[11:24:04] partition by order ID and as we said we
[11:24:07] have to order the data by this time stab
[11:24:09] at the end. So partition by or order by
[11:24:13] creation time and descending. So we want
[11:24:16] the highest then the lowest. So that's
[11:24:19] it. Let's call it Rn and execute the
[11:24:22] query. So now by checking the data if
[11:24:24] everything is clean and we don't have
[11:24:25] duplicates everything should be one
[11:24:28] because maximum for each primary key we
[11:24:30] should has one row. So but you can see
[11:24:32] over here we have here two and we have
[11:24:34] here three two. So that means this is
[11:24:36] indicator that we have duplicates inside
[11:24:38] our data. So now by checking one by one
[11:24:40] as you can see the order ID is only one.
[11:24:43] So we have the rank one the second one
[11:24:45] as well we have the rank one but here we
[11:24:47] have the issue. So as you can see we
[11:24:49] have now two ranks for the order ID
[11:24:51] four. So now which one is the correct in
[11:24:54] our logic? We say it is the last row
[11:24:56] that is inserted inside our data and
[11:24:58] this is rank number one. So if you
[11:25:00] scroll to the right side you can see
[11:25:01] that the creation time here is higher
[11:25:04] than the second one. So with that we
[11:25:06] have identified what we want. We want
[11:25:08] the last inserted row for each ID. And
[11:25:12] now let's check this over here. So here
[11:25:14] we have it three times. So it says the
[11:25:17] first one is the highest creation date.
[11:25:19] So if you go to the right side and now
[11:25:21] by comparing those time stamps you can
[11:25:22] see that this record the first one is
[11:25:26] the la latest one that is inserted
[11:25:28] inside our data. So as you can see this
[11:25:31] one is the one that we need the other
[11:25:33] two we don't need it because it is old
[11:25:35] informations. So now everything that
[11:25:37] doesn't has the rank number one is not
[11:25:39] valid. It's something old and it's
[11:25:41] actually bad data quality. So we want to
[11:25:44] remove it or not to select it. So now in
[11:25:46] order to have a clean data what we going
[11:25:48] to do we're going to go and select the
[11:25:50] following as sub select. So select star
[11:25:52] from the table and now we are interested
[11:25:56] only with the rank number one. We don't
[11:25:59] need anything else. So let's go and
[11:26:00] execute. And now if you check the
[11:26:02] results you can check the order ID over
[11:26:03] here. It is unique. We don't have any
[11:26:05] duplicates. Right? 1 2 3 4 5 6 7. There
[11:26:08] is no duplicates at all. And we have now
[11:26:11] only the latest inserted data inside the
[11:26:13] orders. and we don't have any duplicates
[11:26:16] or data quality issue. So now of course
[11:26:18] now we can go with this results in order
[11:26:19] to do for the analyzes and this is
[11:26:21] exactly what data engineers usually do
[11:26:23] clean up the data and prepare the data
[11:26:25] before doing any data analyzes. And of
[11:26:28] course if you want to communicate those
[11:26:30] data quality issues to the source of the
[11:26:32] data let's say you are not the owner of
[11:26:33] those informations. You can generate a
[11:26:35] list of all bad data quality issues and
[11:26:38] you can send it to the source system and
[11:26:39] tell them to clean it up from the
[11:26:41] sources. So now in order to select the
[11:26:43] bad data what we're going to do is we
[11:26:45] can just change here the condition and
[11:26:47] say if it is higher than one then you
[11:26:50] are like bad data. So let's go and
[11:26:52] execute this. And now with this we have
[11:26:54] in the results all records that
[11:26:56] shouldn't exist in the data in the first
[11:26:58] place. So we can go and export it and
[11:27:00] communicate it to the source and tell
[11:27:01] them check here you have something wrong
[11:27:03] in your system and those information
[11:27:05] should not be inserted in the data. So
[11:27:07] everyone it is very strong right? It is
[11:27:09] very powerful. I use it a lot in my
[11:27:11] projects. There are many use cases for
[11:27:13] the row number function in SQL. We can
[11:27:15] do it in order to find the top end
[11:27:18] analyzes, the bottom end analyzes, the
[11:27:20] best performance, worst performance and
[11:27:22] as well we can assign unique ids to do
[11:27:24] benating or we can use it in order to
[11:27:26] discover data quality issues to clean up
[11:27:29] our data. So it is amazing function in
[11:27:31] SQL and you're going to use it a lot. So
[11:27:33] that's it for the three functions ro
[11:27:34] number, rank and dense rank. Now we're
[11:27:36] going to talk about the
[11:27:40] inile. Okay. So what is inile? Intile in
[11:27:44] SQL is very simple. It's going to go and
[11:27:45] divide your rows, your data into
[11:27:48] specific number of almost equal groups
[11:27:51] or sometimes we call them packets. So
[11:27:53] now in order to understand this and how
[11:27:55] it scale works with this function, we're
[11:27:57] going to have a very simple example. So
[11:27:58] let's go. Okay, we have the following
[11:28:00] setup. We have four rows for sales and
[11:28:02] we would like to divide it into two
[11:28:04] groups or into two buckets. So in order
[11:28:07] to do that we can use the entile
[11:28:09] function. It has different syntax than
[11:28:11] the other ranking functions. So it
[11:28:13] starts with entile then we must define a
[11:28:16] number. So we cannot leave it empty like
[11:28:18] the other ranking. So here we have two
[11:28:20] buckets then over and here again we have
[11:28:23] to sort the data. So it is must order by
[11:28:26] sales descending from the highest to the
[11:28:28] lowest. So now as usual SQL going to go
[11:28:30] and sort the data. We have it already
[11:28:32] sorted in this example. Then it going to
[11:28:34] start assigning each of those rows into
[11:28:36] buckets. But SQL first has to calculate
[11:28:40] the bucket size. So how many rows we can
[11:28:43] like insert inside each bucket. So the
[11:28:45] calculation is very simple. It says the
[11:28:47] bucket size equals to the number of rows
[11:28:51] divided by the number of buckets. So
[11:28:53] what is the number of rows here? We have
[11:28:55] four rows, right? So we have four over
[11:28:58] here. Then the number of buckets we
[11:29:00] define it in the syntax of the query. So
[11:29:02] here we defined two buckets. We need two
[11:29:04] groups. So that means we are dividing
[11:29:07] four by two. And the size of the bucket
[11:29:09] going to be two. So now with this SQL is
[11:29:11] ready and going to start assigning each
[11:29:13] row to a bucket. So it's going to start
[11:29:15] on the top. The first one going to be in
[11:29:16] the bucket number one. Then go to the
[11:29:19] next one. It's going to say okay we
[11:29:20] still have enough space in the bucket.
[11:29:22] Right? So it's going to sign as well to
[11:29:24] one. But with this we reach the maximum
[11:29:27] number of rows within each bucket. So
[11:29:29] the next row going to be assigned to
[11:29:31] another bucket. So it's going to be two
[11:29:33] and the last one going to be as well
[11:29:35] too. So as you can see it's very simple.
[11:29:37] We have just assigned our sales based on
[11:29:39] the sorting of course into two buckets.
[11:29:41] These two sales belongs to the bucket
[11:29:43] number one and the other two belongs to
[11:29:45] the bucket number two. Very easy. So
[11:29:47] that was very straightforward because we
[11:29:49] are dividing even numbers and we got
[11:29:51] perfectly sized buckets. But now what
[11:29:53] going to happen if we have an odd
[11:29:55] number? So we have here five instead of
[11:29:57] four. So the bucket size going to be
[11:30:00] dividing five by two. We're going to get
[11:30:02] 2.5. And now of course SQL will not go
[11:30:04] and divide like two half for each
[11:30:06] bucket. Then we are splitting this into
[11:30:08] two packets. Of course this will not be
[11:30:10] working. We should has now a bucket with
[11:30:12] three and another bucket with two. So
[11:30:14] now the rule in SQL make it very clear.
[11:30:17] It says larger groups comes first then
[11:30:20] smaller. So that means if we have here
[11:30:22] an even number like this, the larger
[11:30:24] group going to be the first group. So
[11:30:26] that's going to look like this. It's
[11:30:28] going to like reset everything. So let's
[11:30:32] see what's going to happen. The first
[11:30:33] one going to be one. The second one has
[11:30:35] bill one. The third one going to be as
[11:30:37] well one. So it going to has a larger
[11:30:40] package than the second one. Then the
[11:30:43] rest going to be two. So as you can see
[11:30:45] the larger group comes first then the
[11:30:48] smaller. And this is how a scale going
[11:30:50] to work. if you have odd numbers. So you
[11:30:52] don't have here perfectly sized buckets.
[11:30:54] You have approximately or roughly
[11:30:56] equally sized buckets. So this is how
[11:30:58] the intel works. Now let's go back to
[11:31:00] scale in order to practice this
[11:31:01] function. Okay. So now let's have some
[11:31:03] fun working with this function. So we
[11:31:05] just going to select something like
[11:31:07] order ID sales from sales orders. So
[11:31:13] let's go and execute it. And with that
[11:31:15] we got our 10 rows. Now let's say that I
[11:31:17] would like to create only one bucket
[11:31:19] from the data. So entile and only one
[11:31:23] bucket over partition let's say not
[11:31:26] partition by let's take order by sales
[11:31:29] descending. So that's it. I'm going to
[11:31:32] call it one bucket. So let's go and
[11:31:35] execute it. As usual it's still going to
[11:31:37] go and sort the data and then calculate
[11:31:38] the bucket. It's going to be 10 rows
[11:31:41] divided by one. So the size of the
[11:31:43] bucket going to be 10. So that's why
[11:31:45] you're going to see everywhere here as
[11:31:47] one because all those rows going to fit
[11:31:49] into one bucket. So this is very simple.
[11:31:52] We have only one bucket. Let's go and
[11:31:54] now have two buckets. So I'm just going
[11:31:56] to copy and paste. And instead of one,
[11:31:59] we're going to have two and let's call
[11:32:00] it two buckets. So let's go and execute
[11:32:04] this. So now here again, what is the
[11:32:06] size of the buckets? It is 10 divided by
[11:32:08] two. So we will get perfectly grouped
[11:32:11] buckets. So the first bucket going to be
[11:32:13] five rows and the second one going to be
[11:32:15] the next five rows. So it is very
[11:32:17] perfect. Let's go to the next one. Let's
[11:32:20] have three buckets. So three. So let's
[11:32:24] go and execute. So now what going to
[11:32:26] happen is going to go and divide 10 by
[11:32:28] three in order to get the size of the
[11:32:30] bucket. And it's going to be 3.3. So it
[11:32:33] is decimal and we will not get perfectly
[11:32:35] sized buckets. So again the larger group
[11:32:38] comes first then the smaller. So as you
[11:32:41] can see we have to fit then in the first
[11:32:43] group four in order to get the others
[11:32:45] with three. So that's why the first
[11:32:47] bucket is going to be the biggest one.
[11:32:49] So four rows into the first bucket. Then
[11:32:51] the second three rows going to be in the
[11:32:53] bucket two. And as well the last one
[11:32:55] going to be bucket three. So as you can
[11:32:57] see the largest group is going to be the
[11:32:59] first bucket. So now let's keep playing
[11:33:01] with the data. Let's go and take now
[11:33:04] four. We would like to have four
[11:33:05] buckets. Now things going to get
[11:33:08] interesting. So now by checking the
[11:33:09] result it's going to be interesting. SQL
[11:33:11] going to divide 10 by four and we will
[11:33:13] get something like 2.5. So again we will
[11:33:16] not get perfectly sized groups. So SQL
[11:33:18] has to fit now 10 rows into four groups.
[11:33:22] So the first three rows going to be fit
[11:33:24] in the bucket number one and as well the
[11:33:27] second three rows like this going to be
[11:33:29] in the bucket number two. And then you
[11:33:32] can see over here we have two buckets
[11:33:34] with a size of two. And with that we can
[11:33:36] fit 10 into four groups. And again you
[11:33:38] can see the larger groups comes first
[11:33:41] like this one and then the second and
[11:33:43] the smallers comes later. Okay. So this
[11:33:46] is how the inter works in SQL. And now
[11:33:47] you might say you know what why do I
[11:33:49] need buckets in the first place. So what
[11:33:51] is the use
[11:33:55] case? There is two use cases for the
[11:33:58] intel function in my projects. In one
[11:34:00] hands if I am data analyst I'm going to
[11:34:02] use the intel function in order to
[11:34:04] segment my data. In the other hand, if
[11:34:06] I'm data engineer, I'm going to use the
[11:34:08] intel function in order to do ETL
[11:34:10] processing and as well to do load
[11:34:13] balancing. So now let's start with the
[11:34:14] first use case as a data analyst where
[11:34:17] you want to do segmentations with the
[11:34:18] entire function. Segmentations is very
[11:34:21] nice way in order to understand your
[11:34:22] data. So you can go and segment your
[11:34:25] data into different buckets or groups
[11:34:27] like for example doing segmentations for
[11:34:29] the customers. So you can go and group
[11:34:32] up your customers depend on their
[11:34:33] behavior like the total sales or the
[11:34:36] total number of orders. So with that you
[11:34:38] can make like for example VIB section
[11:34:41] and then the medium and then the low. So
[11:34:43] now in order to understand the
[11:34:44] segmentation use case let's have the
[11:34:46] following task. Okay. The task says
[11:34:48] segment all orders into three categories
[11:34:51] high medium and low sales. So in order
[11:34:54] to solve this let's do the basic stuff
[11:34:57] right. So select order ID. Let's take
[11:34:59] the sales from our table sales orders
[11:35:03] and let's go and execute it. So as usual
[11:35:06] we got our 10 sales. So now if you check
[11:35:08] the task it says we need three
[11:35:10] categories. So that means we need three
[11:35:12] buckets right and it says high, medium
[11:35:15] and low sales. So that means we are
[11:35:18] dividing by the sales. So let's go and
[11:35:20] do it step by step. So we're going to
[11:35:22] use inile since we need to segment the
[11:35:24] data. Three categories means three
[11:35:26] buckets. And then let's define the
[11:35:29] window over we don't have to divide the
[11:35:31] data by partition by we just need to
[11:35:33] sort it first by the sales. So it's
[11:35:36] going to be by sales and let's take
[11:35:38] discrete since we want to sort it from
[11:35:40] the highest to the lowest. So that's it.
[11:35:42] Let's say you are our buckets. So let's
[11:35:46] go and execute this. So now if you check
[11:35:48] the data you can see that they are
[11:35:49] segmented into three buckets. So the
[11:35:52] first bucket going to contain all orders
[11:35:54] with the high sales. Then the second one
[11:35:57] going to be all sales with the medium.
[11:35:59] And then the last one going to be all
[11:36:01] sales with the low sales. So as you can
[11:36:03] see we have already categorized our data
[11:36:06] into three groups. But now as you can
[11:36:08] see we have numbers and maybe the user
[11:36:10] is expecting to have those text high,
[11:36:12] medium, low. So that means what we're
[11:36:14] going to do now we're going to go and
[11:36:15] translate those numbers into text into
[11:36:17] words. And of course we cannot do that
[11:36:19] inside the window function. We're going
[11:36:21] to use data transformation using the
[11:36:23] case when statements. Don't worry about
[11:36:25] it. We're going to have complete
[11:36:26] dedicated section explaining the case
[11:36:28] when. So for now just follow me in order
[11:36:31] to see how this works. We're going to go
[11:36:33] and use subquery. So it's going to be
[11:36:35] select and let's take the star for
[11:36:38] everything and then let's have the
[11:36:39] following logic. Case when buckets equal
[11:36:43] to one then it is high the sales is
[11:36:46] high. So we are just mapping the numbers
[11:36:49] into text. So otherwise case when the
[11:36:52] brackets equal to two then we are
[11:36:54] targeting the medium medium and then the
[11:36:58] last group packets equal to three then
[11:37:02] those sales are low. So let's call it
[11:37:05] end it and let's call it sales
[11:37:08] segmentations. So that's it. Let me just
[11:37:10] make it a little bit smaller in order
[11:37:12] for you to see it. And all right so from
[11:37:17] and then we have our subquery like this.
[11:37:20] So as you can see we just mapped the
[11:37:22] numbers into text. We are just doing
[11:37:25] translations. So let's go and execute
[11:37:27] it. And now by checking the results we
[11:37:29] got our three categories for the users.
[11:37:32] So the first category going to be the
[11:37:33] high sales. The second one going to be
[11:37:35] the medium sales and the third one going
[11:37:37] to be the low sales. So guys you see
[11:37:39] Intel is very powerful in order to
[11:37:41] segment our data. So now you can go and
[11:37:43] segment stuff like the customers by
[11:37:46] their total sales or the products by
[11:37:48] prices, employees by their salaries and
[11:37:51] so
[11:37:54] on. All right. So this is the first use
[11:37:56] case for the Intel function as a data
[11:37:58] analyst where you go and segment your
[11:38:00] data in order to understand the
[11:38:01] behavior. Now in the other hand, if you
[11:38:04] are data engineer, you can use Intel
[11:38:06] function in order to do load balancing
[11:38:08] in your ETL. So now I'm just going to
[11:38:10] explain it in very simple sketch. All
[11:38:12] right. So now we have the following
[11:38:13] scenario where we have two databases and
[11:38:15] we would like to move one big table from
[11:38:18] the database A to database B. So in this
[11:38:21] case I'm doing something called full
[11:38:22] load. That means I'm loading all the
[11:38:24] rows from one database to another. So if
[11:38:27] you do it in one go what could happen is
[11:38:30] that it could take long time. So it
[11:38:32] could take hours or even sometimes days
[11:38:34] and maybe at the end you will get maybe
[11:38:36] some network errors because you have
[11:38:38] stressed the networks between those two
[11:38:40] databases and everything going to break
[11:38:41] and you're going to lose the data and
[11:38:43] you have to start again. So now instead
[11:38:44] of loading this table in one go what we
[11:38:47] can do we can go and split it into
[11:38:49] fractions or let's say packets. So we
[11:38:51] can split this table for example into
[11:38:53] four small tables using the function
[11:38:56] entile. So now after we split this big
[11:38:58] table into small tables, we're going to
[11:39:00] go and start moving those small tables
[11:39:02] one after another and with that we are
[11:39:05] not stressing the networks and it's
[11:39:06] going to succeed. So now after loading
[11:39:08] everything at the end in the target
[11:39:10] database we're going to have those small
[11:39:12] tables and of course we can go and use
[11:39:14] the union in order to merge them in
[11:39:16] order to have again the big table that
[11:39:18] we have it in the original database. So
[11:39:20] this is very common use case for the
[11:39:22] entile in order to split the load and to
[11:39:24] balance the processing of extracting
[11:39:27] data. All right. So now we have the
[11:39:28] following SQL task. It says in order to
[11:39:30] export the data divide the orders into
[11:39:33] two groups. So let's go and do that.
[11:39:35] First we're going to select everything
[11:39:37] from the table just in order to see the
[11:39:39] data sales orders. So let's go and
[11:39:43] execute it. So now we got our 10 orders
[11:39:45] and what we have to do is that to go and
[11:39:47] split it into two groups. In order to do
[11:39:49] that we can use the entile function. Two
[11:39:53] groups means two buckets. So let's
[11:39:55] define the window. So here we don't have
[11:39:57] to partition the data using partition by
[11:39:59] but we have to specify the order by. So
[11:40:02] now which column we're going to use in
[11:40:04] order to sort the data. Of course here
[11:40:06] there is no rule like you can go and
[11:40:07] split the data by sales or by the order
[11:40:10] status by date by anything you want. But
[11:40:12] we usually go and use the primary key.
[11:40:15] It's just systematic, better, and more
[11:40:17] clean, especially if you have a sequence
[11:40:19] of numbers in the order ID. So you can
[11:40:21] export the first range of the orders,
[11:40:23] then you can go to the next group and so
[11:40:25] on. So let's go with the order ID and
[11:40:28] let's give it a name buckets. So that's
[11:40:31] it. Let's go and hit execute. Now, as
[11:40:33] you can see, it's very simple. We got
[11:40:35] our two groups. So this is the first
[11:40:37] batch of of the data and this is the
[11:40:40] second batch of data. So now we can go
[11:40:42] and select the first batch and export
[11:40:44] it, import it in the next system. And
[11:40:46] then after that we go with the second
[11:40:48] batch. And of course if you still suffer
[11:40:49] from the size of those packets, you can
[11:40:51] go and split it to more smaller size. So
[11:40:54] you can go over here and make it four.
[11:40:56] So with that we're going to get smaller
[11:40:58] buckets and it might be easier to export
[11:41:01] the data. So this is really great use
[11:41:02] case for the entile function. All right
[11:41:04] everyone. So with this you have learned
[11:41:06] the two use cases for the entile
[11:41:07] function that I usually follow in my
[11:41:09] projects. So as a data analyst you can
[11:41:11] use it in order to do segmentations and
[11:41:13] as a data engineer you can use it in
[11:41:15] order to do load balancing of the
[11:41:20] ETL. Okay everyone so with that we have
[11:41:23] covered everything about the integer
[11:41:24] based ranking functions. Now we're going
[11:41:26] to talk about the second methods. We
[11:41:28] have the percentagebased ranking
[11:41:30] functions and here we have two functions
[11:41:32] the cubist and as well the percentile.
[11:41:35] So now let's have a quick recap. So with
[11:41:37] the percentage based ranking SQL going
[11:41:39] to go and calculate a relative position
[11:41:42] as a percentage and assign it for each
[11:41:44] row. So the output going to be a
[11:41:46] continuous normalized scale from 0 to
[11:41:50] one. And this is really amazing in order
[11:41:52] to do distribution analyszis. So those
[11:41:54] functions going to consider in their
[11:41:56] calculation the overall total the whole
[11:41:59] size of the data set which can help us
[11:42:01] in order to find out the contribution of
[11:42:03] each value to the overall total. And now
[11:42:06] in SQL in order to generate the
[11:42:08] percentage we have two different
[11:42:10] formulas. So in one hand we have the
[11:42:12] function cumist and in the other hand we
[11:42:15] have the percent rank. So that means we
[11:42:17] have two different functions with
[11:42:18] different formulas in order to generate
[11:42:20] and calculate the percentage. So now
[11:42:23] let's start with the first function the
[11:42:28] cumist. All right everyone. So now let's
[11:42:30] start with the first function. We have
[11:42:32] the dis and it stands for
[11:42:34] commumulative distribution. It's going
[11:42:36] to go and focus or calculate the
[11:42:38] distribution of your data points within
[11:42:40] a window. So what this means in order to
[11:42:42] understand it, we're going to go and
[11:42:44] have very simple example to understand
[11:42:46] how SQL works with this function. So
[11:42:48] let's go. All right. Again we have our
[11:42:49] very simple example of the sales and we
[11:42:52] have the following query. So dist
[11:42:55] then we don't give any argument inside
[11:42:56] it. So it's going to be empty and the
[11:42:58] window going to be like usual order by
[11:43:00] sales descending from the highest to the
[11:43:02] lowest and the order by is must. So the
[11:43:05] first step is SQL going to go and sort
[11:43:06] the data. We have it already sorted from
[11:43:08] the highest to the lowest. So now the
[11:43:10] next step is that SQL going to go and
[11:43:11] start calculating the percentage for
[11:43:13] each row. And we have a very simple
[11:43:16] formula. It says the cumist equals to
[11:43:18] the position number of the value divided
[11:43:21] by the number of rows. So now the next
[11:43:24] step is still going to go and start
[11:43:25] calculate the percentage for each row.
[11:43:27] And we have this very simple formula. It
[11:43:30] says the cubist equals to the position
[11:43:32] number of the value divided by the
[11:43:35] number of rows. It's very simple. Let's
[11:43:37] do it step by step. So SQL going to
[11:43:39] start with the first value in our list.
[11:43:41] So it going to be calculated like this.
[11:43:43] So what is the position number of the
[11:43:45] first value? It's going to be one,
[11:43:47] right? So this is the first value in our
[11:43:50] list. And what is the total number of
[11:43:52] rows? We have five rows, right? So 1 2 3
[11:43:54] 4 5. So we're going to divide one by
[11:43:57] five. And the result going to be 0.2. So
[11:44:01] this going to be the first value for the
[11:44:02] first row. Okay. So now SQL going to go
[11:44:04] to the next row. And this time we're
[11:44:06] going to get a special case. As you can
[11:44:08] see, we have the 80 twice. So we have
[11:44:10] here a tie. So now first we need the
[11:44:12] position number. As you can see, we are
[11:44:14] at the position number two, right? But
[11:44:17] since we have the 80 multiple times, SQL
[11:44:20] going to go and take the last position
[11:44:22] that we see the value 80 and the last
[11:44:24] position going to be the record number
[11:44:27] three. So that's why SQL going to say
[11:44:29] for this record it's going to be the
[11:44:31] position number three and not two and
[11:44:34] then it's going to go and divide it by
[11:44:35] five and we will get the value of 0.6.
[11:44:39] So this is the most confusing thing with
[11:44:40] this function. So if SQL finds a tie, it
[11:44:43] will completely ignore the current
[11:44:45] position number. So we don't have two.
[11:44:47] It going to go and take the last
[11:44:49] position number for the same value. And
[11:44:52] the last in our list going to be the
[11:44:54] record number three. So that's why we
[11:44:56] have three over here. Okay. So now let's
[11:44:58] keep moving. Let's go to the third row.
[11:45:00] And as you can see, we are again in the
[11:45:02] tie. But this time, this is the last
[11:45:04] time we see 80. So next we don't have
[11:45:06] 80. So what's going to happen? We're
[11:45:08] going to have exact same result. So it's
[11:45:10] going to be 3 divided by 5. So as you
[11:45:13] can see if we have a tie they going to
[11:45:15] share the same percentage. So that means
[11:45:17] with the cube list if you have same
[11:45:19] values they going to share the same
[11:45:21] rank. So let's keep moving to the fourth
[11:45:24] one. So now what is the position number
[11:45:26] of the 50 we are at the record four. So
[11:45:29] position number four divided by five we
[11:45:32] will get 0 comma 8. Okay. So now let's
[11:45:35] move to the last one and it is the
[11:45:37] easiest one. So which position do we
[11:45:39] have over here? It is the position
[11:45:41] number five. It's the last one. And the
[11:45:43] number of rows is five. That's why we
[11:45:45] will get one. So guys, that's it. This
[11:45:47] is how the cumulative distribution
[11:45:48] works. Once you understand the formula,
[11:45:51] it's going to be very easy in order to
[11:45:52] understand the output. So as you can
[11:45:54] see, calculating the percentage always
[11:45:56] depends on the total size of our data
[11:45:58] sets. You can see here the number of
[11:46:00] rows. So with this we're going to get an
[11:46:02] output that help us in order to
[11:46:03] understand the distribution of our data
[11:46:05] points within the data
[11:46:10] sets. All right everyone. So now we're
[11:46:12] going to go and focus on the second
[11:46:13] function that generate percentage as a
[11:46:16] rank. We have the percent rank. So the
[11:46:18] percent rank going to go and focus on
[11:46:20] generating the relative position of each
[11:46:23] row within a window. So in order to
[11:46:26] understand what this means, we can have
[11:46:27] a very simple example in order to
[11:46:29] understand how scale works with this
[11:46:31] function. So let's go. Okay, again we
[11:46:33] have those sales very simple example and
[11:46:35] the syntax going to be like this percent
[11:46:37] rank and inside it we don't use any
[11:46:39] arguments and the window going to be
[11:46:41] like this order by it is a must sales
[11:46:44] descending from the highest to the
[11:46:46] lowest the first step that is going to
[11:46:48] do is that it's going to go and sort the
[11:46:49] data from the highest to the lowest and
[11:46:51] we have it already like this and next
[11:46:53] SQL going to go and start calculate the
[11:46:54] percentage which is very similar to the
[11:46:57] cumulative distribution but this time
[11:46:59] it's going to be like this position
[11:47:01] number then we subtract it from one and
[11:47:04] as well divided by the number of rows
[11:47:07] subtracted from one. So it's like exact
[11:47:10] formula but we are only subtracting here
[11:47:12] once for both numbers. Okay. So now
[11:47:14] let's go through all rows step by step
[11:47:16] and see the output. So it's still going
[11:47:18] to start with the first row right. So
[11:47:20] what is the position number of the first
[11:47:22] row? It's going to be one. Then we have
[11:47:24] to subtract it by one. That's why we
[11:47:26] will get zero. Now what is the total
[11:47:28] number of rows? So we have here five
[11:47:30] rows and it is subtracted by one that's
[11:47:32] why we're going to get four. So now 0
[11:47:35] divided by any value the output going to
[11:47:37] be a zero. So that's why for the first
[11:47:39] value we will get a zero. All right. So
[11:47:41] now let's move to the second row over
[11:47:43] here. And here we have our special case
[11:47:45] where we have a tie. So we have two
[11:47:47] sales sharing the same value 80. So now
[11:47:50] for the percent rank SQL gonna have
[11:47:51] different behavior than the cumist.
[11:47:54] Remember in the list SQL did search
[11:47:56] for the last position of the shared
[11:47:58] value. So it was the position number
[11:48:00] three since this is the last time we see
[11:48:03] 80. But now with the person rank is
[11:48:05] still going to stick with the first
[11:48:06] occurrence of the shared value. So now
[11:48:08] by checking those two 80s what is the
[11:48:10] first occurrence? It is the record
[11:48:12] number two. So that's why we have
[11:48:14] position number two subtracted by one we
[11:48:16] will get one. And here the same going to
[11:48:18] be number of totals we have five
[11:48:21] subtract by one we have four. So now if
[11:48:22] you divide one by four we will get the
[11:48:24] result of 0 comma 25. So this is the
[11:48:28] percentage of this value. So now let's
[11:48:30] go to the second row. Here we have again
[11:48:31] the tie. So scale going to stick with
[11:48:34] the position number two the first
[11:48:35] occurrence. So it's going to be the same
[11:48:38] two subtracted by one we will get one.
[11:48:40] And as well the total number of rows
[11:48:42] five subtract by one we will have four.
[11:48:44] That's why we will get the same exact
[11:48:46] results. So here as you can see with the
[11:48:48] percent rank it's like the list the
[11:48:50] shared value going to share as well the
[11:48:52] same percentage rank. Now let's move to
[11:48:54] the fourth one. So we have the value 50.
[11:48:57] So what is the position of this? It's
[11:48:58] going to be the record number four.
[11:49:00] Subtract it by one we will get three.
[11:49:03] And if you divide three by four you will
[11:49:05] get
[11:49:06] 0.75. And now moving to the last value
[11:49:09] over here it's going to be easy. So what
[11:49:11] is the position number of the 30? It is
[11:49:13] five. Five subtracted by one it's going
[11:49:15] to be four. And as well we're going to
[11:49:17] have four as well here for the total
[11:49:19] numbers subtracted by one. So if you
[11:49:21] divide four by four you will get one. So
[11:49:23] that's it guys. This is how the percent
[11:49:25] rank works. It always has the scale from
[11:49:28] 0 to one. So it's always like this.
[11:49:30] Doesn't matter which values do we have
[11:49:32] inside and it's going to has like
[11:49:34] continuous scale. And again here if you
[11:49:36] have a tie they're going to go and share
[11:49:37] the same percentage rank. Okay guys. So
[11:49:39] now if you go and compare those two
[11:49:41] functions you're going to see that they
[11:49:42] are really similar to each others. The
[11:49:43] output of both functions we are
[11:49:45] generating percentage based ranking and
[11:49:48] both of them as well handling the ties
[11:49:50] perfectly. So they share the same
[11:49:52] percentage rank. If you check the syntax
[11:49:54] they are very similar. And now by
[11:49:55] checking the formulas of both of them we
[11:49:57] are always considering the overall size
[11:50:00] of the data sets. So here the size is
[11:50:03] considered in the calculation to help us
[11:50:05] finding the relative position of each
[11:50:08] value to the overall and this is very
[11:50:10] important in the analyszis in order to
[11:50:12] measure the contribution of each value
[11:50:14] to the overall. So now about the use
[11:50:16] cases if you want to focus on the
[11:50:18] distribution of your data points go with
[11:50:20] the cumulative distribution but if you
[11:50:22] want to focus on the relative position
[11:50:24] of each rows then go with the percent
[11:50:26] rank. All right. So now there is one
[11:50:28] more difference between the and the
[11:50:30] percent rank and that's if you check the
[11:50:32] formulas. You can see that the is
[11:50:35] more inclusive. We always consider the
[11:50:38] position number of the current row. But
[11:50:40] with the person rank we don't consider
[11:50:42] the current row. We like skip it or make
[11:50:45] it exclusive. So we say for the person
[11:50:47] rank it is more exclusive and the
[11:50:50] cumulative distribution it is more
[11:50:52] inclusive. So now if you ask me the hard
[11:50:53] question which one to use, I'm going to
[11:50:55] say if you want to be more inclusive, go
[11:50:58] with the commutive distribution. If you
[11:51:00] want to be more exclusive with the
[11:51:01] current row, go with the person rank. So
[11:51:04] they are very similar to each others. So
[11:51:06] if you want to calculate the
[11:51:07] distribution of your data, go with the
[11:51:08] cumulative distribution. If you want to
[11:51:10] find the relative position of each row,
[11:51:13] then go with the percent rank. All
[11:51:15] right. So now we have the following task
[11:51:16] that says find the products that fall
[11:51:19] within the highest 40% of the prices.
[11:51:22] Let's go and solve this. Now we are
[11:51:23] targeting the table products and I will
[11:51:26] just select like two columns products
[11:51:28] price from sales products. So that's it.
[11:51:33] Let's go and execute this. So now as you
[11:51:36] can see we got five products and their
[11:51:37] prices. And the task says find the
[11:51:40] highest 40%. So we have to find and
[11:51:42] generate a percentage rank. In order to
[11:51:45] do that we have the two functions cumist
[11:51:47] and the percent rank. I will go this
[11:51:49] time with the list. So let's go and
[11:51:52] do that. So list and then let's go
[11:51:55] and define the window like this. It's
[11:51:58] going to be order by we are targeting
[11:52:00] now the prices right? So order by the
[11:52:03] price from the highest to the lowest and
[11:52:05] let's go give it a name this rank. So
[11:52:08] let's go and execute this. So now with
[11:52:10] that SQL going to go and generate for us
[11:52:12] a percentage ranking using the formula
[11:52:14] that we just learned before. So now in
[11:52:16] the output we are getting all the
[11:52:17] products but the task says we have to
[11:52:19] get only the products that are in the
[11:52:21] highest 40%. So that means the first row
[11:52:24] the second row and that's it. So those
[11:52:27] rows are in the highest 40% the rest are
[11:52:31] below that. So in order to do that to
[11:52:32] filter the data we're going to use the
[11:52:34] subquery. So select star from and then
[11:52:38] we have our sub query like this and then
[11:52:41] our filter going to be this rank smaller
[11:52:45] or equal to 0.4. So this is our
[11:52:49] threshold in order to get the data. So
[11:52:51] let's go and execute this. And now as
[11:52:53] you can see we got the top products the
[11:52:55] top 40%. Now of course you can go and
[11:52:58] format the percentage. We can do that
[11:53:00] like this. So let's take the test
[11:53:05] rank multiply it with 100. So let's go
[11:53:08] and execute this. So as you can see we
[11:53:09] got 20 and 40%. We can go and add to it
[11:53:13] as well the percentage character right.
[11:53:15] So we can go and say concat and we're
[11:53:18] going to add the character after that
[11:53:21] like this and let's call it test rank
[11:53:25] percentage. So that's it. Let's go and
[11:53:27] execute it. So that we have solved the
[11:53:29] task. We have the products that fall
[11:53:31] within the highest 40%. Now, of course,
[11:53:33] you can go and try the percent rank. So,
[11:53:36] it's very simple. We just have to go and
[11:53:38] switch the cumulative distribution with
[11:53:40] the function percent bank. So, let's go
[11:53:44] and execute it. Now, as you can see, we
[11:53:46] will get the exact same results. So,
[11:53:48] we're still getting the gloves and caps
[11:53:50] as the highest products within the 40%
[11:53:53] of the price. So, guys, that's it. It's
[11:53:54] very simple, right?
[11:53:59] All right friends, so now let's have a
[11:54:01] quick recap for the window ranking
[11:54:02] functions. So what they're going to do,
[11:54:04] they're going to go and assign a rank
[11:54:05] for each row within a window. And we
[11:54:08] have two types of ranking, right? The
[11:54:10] first one is the integer based ranking.
[11:54:12] It's going to go and assign a number an
[11:54:14] integer for each row. And here we have
[11:54:16] four functions. Row number, rank, dense
[11:54:19] rank, and in tile. And the second type
[11:54:21] of ranking, we have the percentage based
[11:54:23] ranking. So scale fair is going to go
[11:54:25] and calculate a rank and then assign it
[11:54:27] for each row. And here we have two types
[11:54:29] of formula or functions. So we have the
[11:54:31] cube dist the cumulative distribution
[11:54:34] and the second one we have the percent
[11:54:35] rank. And now to the next point if we
[11:54:37] are talking about the rules of the
[11:54:38] syntax. So the expression should be
[11:54:41] empty. We should not pass any argument
[11:54:42] to the functions. We must use order by
[11:54:45] in order to sort our data. So it is
[11:54:47] required and the frame clause are not
[11:54:50] allowed to use. So you cannot go and
[11:54:51] customize a frame within the window
[11:54:54] function. And as we learned there are
[11:54:56] many use cases for the ranking
[11:54:57] functions. For example, we have the top
[11:54:59] end analyzes the button end analyzes in
[11:55:02] order to identify our top performers or
[11:55:04] the worst performers in our business.
[11:55:07] Another use case using the row number we
[11:55:09] can identify and remove duplicates in
[11:55:11] our data. So we can use it in order to
[11:55:14] find data quality issues and as well to
[11:55:16] improve the quality. And another use
[11:55:18] case if our table don't have a clean
[11:55:20] primary key we can go and generate
[11:55:22] unique ids using the row number in order
[11:55:24] to do as well by generating one more use
[11:55:26] case it was the data segmentations you
[11:55:29] can use the intel in order to segment
[11:55:31] your customers your products employees
[11:55:33] and so on and another use case we can do
[11:55:35] data distribution analysis as we learned
[11:55:37] we can use the cubeist in order to
[11:55:39] understand the data distributions of our
[11:55:42] data points compared to the overall and
[11:55:44] the last use case it's more for data
[11:55:46] engineering we can use the intel
[11:55:47] function in order to equalize the
[11:55:49] loading process of our ETLs. So as you
[11:55:51] can see there are many use cases for the
[11:55:53] ranking functions. Okay, so that's all
[11:55:55] about how to rank your data using the
[11:55:57] window functions and now we're going to
[11:55:59] cover the last group. We will learn
[11:56:00] about the value window functions. How to
[11:56:03] access another records. So let's
[11:56:08] go. All right everyone. So now we have
[11:56:10] this very simple example. We have the
[11:56:12] months and the sales. Now we can use the
[11:56:15] value functions in order to access a
[11:56:17] value from another row. So in order to
[11:56:20] understand it let's say that SQL now
[11:56:22] processing the months and we are
[11:56:24] currently at the month of March. So now
[11:56:26] for example I would like to access the
[11:56:28] value from the previous month from
[11:56:30] February. So in order to do that we can
[11:56:32] use the lag function in order to get the
[11:56:35] value of 10. So with that we have in the
[11:56:37] same row the current sales of the month
[11:56:40] March and as well the sales from the
[11:56:42] previous month the February. And maybe
[11:56:45] in other cases I would like to get the
[11:56:47] sales of the next month from April. In
[11:56:50] order to do that we can use the function
[11:56:52] lead and we will get at the same row the
[11:56:55] value five. So now I can very quickly
[11:56:57] compare the current month with the
[11:56:59] previous month and as well with the next
[11:57:02] month. And now in the other cases you
[11:57:03] might be interested in the first month
[11:57:06] of your list. So it's going to be here
[11:57:07] January. So in order to get the sales of
[11:57:10] the first month you can use the function
[11:57:13] first value. So we're going to get at
[11:57:15] the same row 20. And now for the last
[11:57:17] option I think you already get it. We
[11:57:19] can go and get the value of sales of the
[11:57:22] last month. So here we can get the July.
[11:57:24] So for that we're going to use the
[11:57:25] function last value and we will get the
[11:57:28] value of 40. So this is exactly the
[11:57:30] purpose of the value functions or
[11:57:32] analytical functions. We can access a
[11:57:35] value from another rows. And here is
[11:57:37] really important to understand as well
[11:57:38] the value functions is like the ranking
[11:57:40] functions. We have to use the order by
[11:57:43] in order to sort the data in order to
[11:57:45] understand what is the first row and the
[11:57:47] last row. In this example, the data is
[11:57:49] sorted by the month. So guys, the access
[11:57:51] functions are really important for
[11:57:52] analytics. You can use it in order to
[11:57:54] access a value from other rows in order
[11:57:57] to do comparison. All right. Right. So
[11:57:58] now let's have a quick overview of the
[11:58:00] syntax and the rules for the value
[11:58:02] functions. So here we have four
[11:58:04] functions lead, lag, first value and
[11:58:06] last value. So as you can see we can
[11:58:08] group them into two groups. So we have
[11:58:10] the lead and lag. They are very similar
[11:58:12] to each others. Especially with the
[11:58:13] syntax we can use three things or three
[11:58:16] arguments inside it. Expression offset
[11:58:18] default for both of them. For the first
[11:58:21] value we can use only an expression. So
[11:58:23] that means we have to pass a value for
[11:58:26] those functions. You cannot leave it
[11:58:27] empty. So now about the expression data
[11:58:29] type, you can use any field with any
[11:58:31] data type. There is no restrictions
[11:58:32] about only for example using numbers.
[11:58:35] Any data type is allowed. Now about the
[11:58:37] definition of the window. The partition
[11:58:39] by as usual is optional like any other
[11:58:41] group. The order by here is a must. You
[11:58:44] must define an order by. It's like the
[11:58:46] ranking. So here you cannot leave it
[11:58:48] empty. And now we come to the last one.
[11:58:50] We have the frame clause. There are
[11:58:52] really different stuff over here. So for
[11:58:54] the first two functions lead and lag you
[11:58:56] are not allowed to define any frame. So
[11:58:58] you are not allowed to define any subset
[11:59:00] of data. It's very similar to the
[11:59:02] ranking. So you must use order by but
[11:59:05] you cannot define the frame of the
[11:59:07] window. But for the other two functions
[11:59:09] the first value and the last value they
[11:59:11] are optional. You can go and use them.
[11:59:13] And for the last value it is recommended
[11:59:16] to define frame close. Don't worry about
[11:59:18] it. We're going to have enough examples
[11:59:19] in order to understand. So as you can
[11:59:21] see those functions has different
[11:59:22] requirements. So there is no generic
[11:59:24] rule for all of them. But one thing that
[11:59:26] they all agree on that you must use
[11:59:29] order by. So now as usual what we're
[11:59:31] going to do we're going to go and deep
[11:59:32] dive into those functions. We're going
[11:59:34] to address first the two functions lead
[11:59:36] and lag because they are very similar to
[11:59:38] each others. We're going to understand
[11:59:40] the use cases when to use them and of
[11:59:42] course we're going to practice in SQL.
[11:59:44] So let's go.
[11:59:48] lead and lag functions. The lead
[11:59:50] function can allow you to access a value
[11:59:52] from the next row within a window where
[11:59:56] the lack function is exactly the
[11:59:57] opposite. It's going to allow you to
[11:59:59] access a value from a previous row
[12:00:02] within a window. It sounds very easy,
[12:00:04] right? So let's understand how is SQL
[12:00:05] going to execute those functions. Okay.
[12:00:07] So now let's have a quick overview of
[12:00:08] the syntax for both of the functions
[12:00:10] lead and lag. We have here very simple
[12:00:12] example for the lead function. So as
[12:00:15] usual we start with the function name.
[12:00:16] It's going to be the lead. And now after
[12:00:18] that we're going to go and pass the
[12:00:20] arguments. And as you can see we have
[12:00:21] here multiple stuff. So let's do it step
[12:00:24] by step. So the first thing is that
[12:00:25] we're going to go and specify an
[12:00:26] expression. And the data type could be
[12:00:28] any data type. It could be a number like
[12:00:31] here the sales. It could be a character
[12:00:32] like names or dates or anything. So this
[12:00:36] is required. We have to specify an
[12:00:39] expression. We cannot leave it empty.
[12:00:41] And we can use any data type. Now moving
[12:00:43] on to the next one. We have here a
[12:00:44] number. So what is this? This is the
[12:00:47] offset and this offset is optional. So
[12:00:49] you can go and skip it. So what offsets
[12:00:51] means? What we are doing over here? We
[12:00:53] are specifying for SQL the number of
[12:00:56] rows forward or backward from the
[12:00:59] current row. So here in this example we
[12:01:00] are specifying the offset as two using
[12:01:02] the lead. And with that we are telling
[12:01:04] SQL go jump to the next two rows and get
[12:01:06] me the value. And if you are using lag
[12:01:08] it means you are telling SQL go back two
[12:01:11] rows up and get me the value. So here
[12:01:13] you are telling SQL how many rows it
[12:01:15] needs to jump and if you don't specify
[12:01:17] anything like leave it empty SQL going
[12:01:19] to go and use a one. So the default of
[12:01:22] this with the offsets going to be one if
[12:01:23] you don't specify anything. All right
[12:01:25] moving on to the last one and to the
[12:01:26] third one. This is as well optional. You
[12:01:28] can go and leave it empty. So here it is
[12:01:31] the default value. Now what happens with
[12:01:33] those functions is that sometimes SQL
[12:01:35] jump to the next two rows or something
[12:01:37] like that and SQL doesn't find anything.
[12:01:39] So there is no more rows available to
[12:01:41] access and with that SQL going to go and
[12:01:44] return a null. So that means if SQL goes
[12:01:46] to the next rows or go to the previous
[12:01:48] rows and doesn't find anything SQL as a
[12:01:51] default going to go and return a null.
[12:01:53] So if you don't specify anything over
[12:01:55] here in those scenarios you will have a
[12:01:57] null values as a return from the whole
[12:02:00] function. But in some scenarios you
[12:02:01] don't want to have a null you would like
[12:02:03] to have a value. So here you are
[12:02:05] defining the default value. So it should
[12:02:08] not be a null, it should be a 10. So
[12:02:10] scale if you don't find anything return
[12:02:12] a 10. Don't return a null. So again
[12:02:15] guys, the default values, the offsets,
[12:02:17] all those informations are optional for
[12:02:18] you in order to configure it. But you
[12:02:20] should know the default if you don't use
[12:02:22] anything for the offset is going to be
[12:02:24] one for the default value going to be
[12:02:26] null. But you must specify an
[12:02:28] expression. So here you cannot leave it
[12:02:30] empty. All right. So that's all about
[12:02:32] the arguments that you can pass to the
[12:02:34] lead or lag functions. Then the next
[12:02:36] stuff are the standard stuff. So we have
[12:02:38] the overclos then we have the partition
[12:02:40] by as usual partition by is optional.
[12:02:43] And then to the order by those functions
[12:02:45] it's like the rank functions. It
[12:02:47] requires you to sort the data. So it is
[12:02:50] a must to sort the data otherwise will
[12:02:52] not know what is the next row what are
[12:02:54] the previous rows. So we have to sort
[12:02:56] the data. It is required. You cannot
[12:02:58] skip this. So it is not optional. All
[12:03:00] right. So the syntax is not crazy right?
[12:03:02] We have the usual stuff but only we can
[12:03:04] go and configure the default value and
[12:03:06] the offsets. Okay guys, now we have very
[12:03:07] simple example. We have months and sales
[12:03:10] and we're going to go and understand how
[12:03:11] the SQL works for both of the functions
[12:03:14] lead and lag side by side. So now in the
[12:03:16] first example we are interested in the
[12:03:18] sales of the next month. So in order to
[12:03:21] do that we're going to use the lead
[12:03:22] function. So lead and then we're going
[12:03:25] to specify the argument. It is the
[12:03:26] sales. We want the value of sales and
[12:03:28] then we define the window like this
[12:03:31] order by month. So it's going to be
[12:03:33] ascending. And now in the right side
[12:03:34] we're going to be interested in the
[12:03:36] sales of the previous months. So in
[12:03:38] order to do that we're going to use the
[12:03:40] lag function. So it's going to be very
[12:03:42] similar to the lead. We have lag and
[12:03:44] then the sales since we are interested
[12:03:45] in the sales and we're going to sort the
[12:03:47] data by the month. So now let's see how
[12:03:49] going to do it step by step and side by
[12:03:51] side. So going to start with the first.
[12:03:53] So now let's see how skill going to
[12:03:55] process those informations side by side
[12:03:57] and row by row. So it's going to start
[12:03:59] with the first row over here. What is
[12:04:02] the next month of January? It is
[12:04:04] February and we are interested in the
[12:04:07] sales of this row. So SQL going to take
[12:04:10] the value from the next row and we're
[12:04:12] going to have the value of 10. So now by
[12:04:14] looking through the January we can see
[12:04:16] the sales of the next month of February
[12:04:18] in the same row. So now let's check the
[12:04:20] right side over here. Now we are
[12:04:22] interested in the previous month. So
[12:04:24] what is the previous month of the first
[12:04:26] row? It will be nothing. Right? So we
[12:04:28] cannot point it with anything. That's
[12:04:31] why going to say this is null. There is
[12:04:33] no previous month for the current row.
[12:04:36] And we're going to have it as a null.
[12:04:37] Okay. So now it's going to go to the
[12:04:39] next row. We are at February. What is
[12:04:41] the next month? It's going to be March.
[12:04:43] And it's going to point to it. So we
[12:04:45] will get the 30 as the sales of the next
[12:04:48] month of March. And on the right side,
[12:04:51] what is the previous month of February?
[12:04:54] It's going to be January, right? So,
[12:04:56] it's going to get the value the sales of
[12:04:58] the previous month. And here we will get
[12:05:01] 20. So, as you can see, it's very
[12:05:02] simple. On the lead, we are always
[12:05:04] checking the next values. On the lag, we
[12:05:06] are always checking the previous value.
[12:05:08] So, let's keep going. We are currently
[12:05:10] at March. What is the next month? It's
[12:05:12] going to be April. So, it's going to go
[12:05:14] and point to it like this. and we will
[12:05:18] get the sales of the next month April.
[12:05:21] For the March on the right side, what is
[12:05:22] the previous month? It is February.
[12:05:25] Right? So, it's going to go and point to
[12:05:27] February. So, we will get the sales of
[12:05:30] 10. And now, interesting to the last row
[12:05:32] over here. You can see that we are at
[12:05:34] April. What is the next month of April?
[12:05:37] There is nothing because we are at the
[12:05:39] end of our table, right? So, since there
[12:05:41] is no month after that, we will get a
[12:05:44] null in the output. But for the lag, we
[12:05:47] still have a previous month for April.
[12:05:48] So what is the previous month? It is
[12:05:50] March. And we will get the sales of the
[12:05:54] March. So it's going to be 30. So that's
[12:05:56] it guys. It's really simple, right? It's
[12:05:58] just like they are doing the opposite
[12:05:59] things. So now if you check those values
[12:06:01] side by side, you can see that with the
[12:06:03] lead, we will always get a value for the
[12:06:06] first row, but for the last row, it can
[12:06:08] be always empty because there is no next
[12:06:11] value. We are at the end of the table.
[12:06:13] But if you check the lag for the first
[12:06:15] value, we will always get a null because
[12:06:18] there is no previous value or previous
[12:06:20] record from the first row. And for the
[12:06:22] last record, as you can see, we're
[12:06:24] always going to get a value because we
[12:06:26] will have a previous value. Okay, let's
[12:06:28] move on in order to understand how scale
[12:06:29] this time works with the offsets and the
[12:06:31] default value. So now we have the same
[12:06:33] data, but we have different task. So now
[12:06:35] on the left side, we would like to get
[12:06:36] the sales of two months ahead. So it's
[12:06:39] not the next month, it's going to be two
[12:06:41] months. And we would like to tell SQL if
[12:06:43] you don't find any value don't return
[12:06:45] null return for us is zero. So this is
[12:06:47] going to be our default. Now if you
[12:06:49] check the syntax it's going to be exact
[12:06:51] like before but we are adding now an
[12:06:53] offset of two because we are interested
[12:06:55] in two months ahead and we are
[12:06:58] specifying here a default value zero. So
[12:07:00] if you don't find anything put zero
[12:07:02] don't put null. Now on the right side we
[12:07:04] have the exact opposite. We are
[12:07:06] interested in the sales of two months
[12:07:08] ago. So we are not interested in the
[12:07:10] direct previous month we need the sales
[12:07:12] of two months ago. And here the same
[12:07:14] thing if you don't find anything don't
[12:07:16] return null give us a zero. So as you
[12:07:18] can see we have the same syntax but
[12:07:19] using the function lag. So now let's
[12:07:21] understand how going to execute this
[12:07:23] step by step and side by side. So going
[12:07:25] to start with the first month January.
[12:07:27] So now SQL going to ask what is the
[12:07:28] sales of two months ahead. So we are at
[12:07:32] January. It will not be February it's
[12:07:34] going to be the month of March. So it's
[12:07:36] going to go and point it like this and
[12:07:38] we will get the value of 30. So 30 is
[12:07:41] the sales of two months ahead. And now
[12:07:44] on the right side we are as well at
[12:07:45] January. It's going to ask the question
[12:07:47] what is the sales of two months ago. So
[12:07:50] we don't have any previous data. Right?
[12:07:52] So we will not get anything. It's going
[12:07:54] to return null but it's going to check
[12:07:56] do we have a default value? Well yes. So
[12:07:59] this time HQL will not return null. It's
[12:08:01] going to return the default value. And
[12:08:03] this time it's going to be zero. All
[12:08:04] right. All right. So now let's go to the
[12:08:06] next value. We are currently at
[12:08:07] February. What is the sales of two
[12:08:10] months ahead? So it will not be March,
[12:08:12] it's going to be April. So it's going to
[12:08:14] go and point it like this and we will
[12:08:16] get the value of five. So now on the
[12:08:18] right side we are currently at February.
[12:08:20] Now the question is what is the sales of
[12:08:22] two months ago? We have history. We have
[12:08:24] the previous month but we don't have two
[12:08:26] months in the history. That's why we
[12:08:29] will still get zero as the output with
[12:08:31] the default value. Okay. Okay. So now
[12:08:33] let's keep going to the next value. We
[12:08:34] are currently at March. SQL going to ask
[12:08:37] what is the sales of the two months
[12:08:38] ahead. We have only one month after that
[12:08:41] but we don't have two months. That's why
[12:08:43] SQL will not find anything and it's
[12:08:45] going to return null but it's going to
[12:08:47] go and use the default. So here we're
[12:08:48] going to go and get the value of zero.
[12:08:50] There is no more data available in the
[12:08:52] table. But now on the right side we are
[12:08:54] currently at March and we are asking
[12:08:56] what is the sales of two months ago. So
[12:08:59] now we have enough history in the past
[12:09:01] and it's going to get the value of 20.
[12:09:04] All right. So now let's go to the last
[12:09:05] month over here in our table. April.
[12:09:07] What is the sales of two months ahead?
[12:09:10] We don't have any data. So it's going to
[12:09:12] be zero as well. But now on the right
[12:09:13] side, we are currently at April. What is
[12:09:16] sales of two months ago? We have enough
[12:09:18] history. That's why SQL going to get and
[12:09:20] point it like this. So we will get the
[12:09:23] February going to be 10. So that's it.
[12:09:25] This is how SQL works with the lead and
[12:09:27] lag using offsets and as well default
[12:09:30] value. Let's go back in SQL in order to
[12:09:32] practice those two
[12:09:36] functions. Okay, so now we have the
[12:09:38] following task and it says analyze the
[12:09:40] month over month performance by finding
[12:09:42] the percentage change in sales between
[12:09:45] the current and the previous month. So
[12:09:46] that means we have to go and compare the
[12:09:48] current month with the previous month.
[12:09:50] So the main use case for the lead unlock
[12:09:52] is to do comparison analyszis and we
[12:09:55] have a very common use case it's called
[12:09:57] time series analyzes. So it is the
[12:10:00] method of analyzing our business our
[12:10:02] data in order to understand the patterns
[12:10:05] and trends over the time. And one of the
[12:10:08] most important and classical question
[12:10:10] that you're going to get from the
[12:10:11] decision makers or business is to do
[12:10:14] year-over-year analyszis or month over
[12:10:16] month analyszis. So the year-over-year
[12:10:18] analysis is going to help us in order to
[12:10:20] understand the overall growth or decline
[12:10:23] in the performance of our business over
[12:10:26] the years over the time. But in the
[12:10:27] other hand, we have month- over-month
[12:10:29] analyszis in order to do shortterm
[12:10:32] trends analyzes and as well discover the
[12:10:34] patterns in the seasonality. So the main
[12:10:36] focus is to understand the performance
[12:10:38] of our business over the time. So now
[12:10:40] let's go back to it in order to solve
[12:10:42] the task. Okay guys, so now let's go and
[12:10:44] do it step by step. Now what is the
[12:10:46] first step? Before we go and compare
[12:10:48] things together, we have to collect the
[12:10:49] data. We have to do the calculations
[12:10:51] first. So we have to find out first the
[12:10:53] total sales for the current month and
[12:10:55] then the total sales for the previous
[12:10:57] month. And after that we can go and
[12:10:59] compare them. So now let's start with
[12:11:00] the easy stuff. We have to find out the
[12:11:03] current sales for the current month. So
[12:11:05] in order to do that, let's just do very
[12:11:07] simple select. So what do we need? We
[12:11:09] need let's take the order ID. Let's take
[12:11:11] the order date because inside it we have
[12:11:14] the month. Uh let's go and collect the
[12:11:16] sales. So that's it for now from sales
[12:11:21] orders. So let's go and execute this. So
[12:11:24] now in the result we got the usual
[12:11:25] stuff. We have 10 orders, sales and
[12:11:28] order dates. But the order date is on
[12:11:30] the level of the days and we are not
[12:11:32] interested on the whole date. We would
[12:11:34] like to get only the month in order to
[12:11:36] calculate the total sales for the month.
[12:11:38] Now we're going to go and use a function
[12:11:39] in order to extract the month from a
[12:11:41] date. Don't worry about it. We're going
[12:11:42] to have a dedicated chapter in order to
[12:11:45] show you how to deal with the dates
[12:11:46] format in SQL. So now what we're going
[12:11:48] to do, we will use a very simple
[12:11:51] function called month and order dates.
[12:11:54] And let's call it order month. So that's
[12:11:57] it. Let's go and execute it. Now, as you
[12:11:59] can see, we got a new field where we
[12:12:01] have only the month of informations. So
[12:12:03] here we have January, February, and
[12:12:04] March. So now the next step is that we
[12:12:06] want to find the total sales for each
[12:12:08] month. So what we're going to do, we're
[12:12:10] going to go and use group by. So, let's
[12:12:12] do that. We're going to go and say we
[12:12:15] want the sum of sales. I'm just going to
[12:12:18] call it current month sales. And let's
[12:12:22] go and get rid of all those
[12:12:23] informations. We're going to go and
[12:12:25] group by the month, right? So, group by
[12:12:28] and let's have the month. So, that's it.
[12:12:31] Let's go and execute it. So, it's very
[12:12:33] simple, right? We got now the three
[12:12:35] months and the total sales of the
[12:12:37] current month. So now with that we got
[12:12:39] the first information that we need in
[12:12:40] order to do the comparison. We have for
[12:12:42] each row the total sales for the current
[12:12:45] month. So now the next thing that we're
[12:12:46] going to do is to find out the total
[12:12:49] sales for the previous month like side
[12:12:51] by side in the same row. And in order to
[12:12:53] do that we have learned we can go and
[12:12:55] use the lag function. So we're going to
[12:12:57] go and integrate the lag window function
[12:12:59] in the same group by. So we're going to
[12:13:01] do it like this. So lag we are now
[12:13:04] interested in the previous month. So
[12:13:06] that's why we're going to go and get the
[12:13:08] sum of sales as an expression inside it.
[12:13:11] And after that we're going to define the
[12:13:12] window. It's going to be like this over
[12:13:14] and order by is a must. So we're going
[12:13:16] to go and sort the data by the month.
[12:13:19] Right? So let's go and do it. And with
[12:13:21] that we have defined the previous month
[12:13:24] sales. So you are the previous month
[12:13:27] sales. So now let's go and execute it in
[12:13:29] order to see the results. All right. So
[12:13:31] now let's check the results. The first
[12:13:33] row what is the previous month? There is
[12:13:35] no previous month. We are at the first
[12:13:37] record and the first month that's why we
[12:13:39] have null. Now let's go to February.
[12:13:41] What is the sales of the previous month
[12:13:44] from January? It is 105. So this is
[12:13:47] correct. And now to the last value to
[12:13:49] the March. What is the sales of
[12:13:51] February? The previous month it is 195.
[12:13:54] So with that we got the two
[12:13:56] informations. We have the current month
[12:13:58] and as well the previous month. So guys
[12:14:00] as you can see it's magic right? It's
[12:14:02] very simple. we can go and use the lead
[12:14:04] and lag functions in order to access
[12:14:07] another values from another rows without
[12:14:09] doing any complicated joins and so on.
[12:14:12] Okay. So now what is the next step?
[12:14:13] We're going to go and subtract the total
[12:14:15] sales from the current month with the
[12:14:17] previous month. So in order to do that
[12:14:19] we're going to go and use a sub query
[12:14:21] like this. So select star from and we're
[12:14:26] going to have it like this as subquery.
[12:14:28] And now the calculation is very simple.
[12:14:30] Let me just move this a little bit down.
[12:14:33] So it is the current month subtracted
[12:14:36] from the previous month and let's go and
[12:14:39] call it month over month change. So
[12:14:43] that's it. Let's go and execute this. So
[12:14:45] now let's go and check the results for
[12:14:47] the first month. You can see that we
[12:14:49] don't have any value and that is correct
[12:14:50] because the previous month is empty. So
[12:14:53] there is no change. And now moving on to
[12:14:55] the February. You can see over here we
[12:14:57] got plus 90. That means we have here
[12:15:00] improvement in the performance of our
[12:15:02] sales. Now moving on to the last one.
[12:15:04] It's really bad. We have decline in our
[12:15:06] performance. We can see that we have
[12:15:08] minus 115. So that means the current
[12:15:11] month is doing really bad compared to
[12:15:13] the previous month. So the March is
[12:15:15] really bad month. Okay. So now as you
[12:15:16] can see in the output we got the
[12:15:18] absolute numbers but the task says find
[12:15:20] the percentage change. So we have to
[12:15:22] convert this to a percentage and we can
[12:15:24] do it like this. It's very simple. Let's
[12:15:26] do it in a new column. Just going to
[12:15:28] zoom out a little bit. So, it's going to
[12:15:31] be the change the differences divided by
[12:15:34] the previous month sales. And then let's
[12:15:36] go and multiply it with 100 in order to
[12:15:39] get the percentage. So, like this. And
[12:15:41] now, as you can see, we got zeros. And
[12:15:43] that's because those numbers are
[12:15:45] integer. So, we have to go and cast one
[12:15:47] of those values. Just going to do it for
[12:15:49] the first. So, cast and float. So,
[12:15:53] that's it. Let's go and execute it
[12:15:55] again. Now the result looks better. We
[12:15:57] have the percentages but we have a lot
[12:15:59] of decimals. So let's go and round the
[12:16:02] number to let's say one decimal. So only
[12:16:05] one and let's give it a name. So you are
[12:16:07] month over month percentage. So let's
[12:16:11] execute. So now as you can see things
[12:16:12] get better. And with that we have
[12:16:14] calculated the percentage change in
[12:16:16] sales between the current and the
[12:16:19] previous months. And this is how we do
[12:16:21] month overmonth analyszis.
[12:16:26] All right. So now we have another use
[12:16:28] case for the lead and lag function. We
[12:16:30] can use them in order to do customer
[12:16:31] retention analyzes. It's all about
[12:16:33] measuring the customer behavior and
[12:16:35] loyalty. So we are helping the business
[12:16:37] and decision makers to build strong
[12:16:40] relationship with the loyal customers
[12:16:42] and for them as well to focus on their
[12:16:44] needs. So now let's see how we can use
[12:16:46] lead and lag function in order to do
[12:16:48] customer retention analyszis. So let's
[12:16:50] go. All right. Right. So now we have the
[12:16:51] following task and it says in order to
[12:16:53] analyze customer loyalty rank customers
[12:16:56] based on the average days between their
[12:16:58] orders. So there is a lot of things
[12:17:00] going on over here. Let's do it step by
[12:17:02] step. And I would like always to start
[12:17:03] with a very simple select. So let's go
[12:17:06] select informations like the order ID.
[12:17:09] Let's get the customer ID and as well
[12:17:12] since we want the days we would like to
[12:17:14] have the date. So order dates from the
[12:17:17] table sales orders and let's go and sort
[12:17:21] the data. So order by customer ID and
[12:17:25] order dates. So that's it. Let's go and
[12:17:28] execute. So now as usual we got our 10
[12:17:30] orders, the customers and when they did
[12:17:32] order. So now let's check the task.
[12:17:35] Let's solve this over here. Days between
[12:17:37] their orders. So we have to find how
[12:17:39] many days are between two orders. For
[12:17:42] example, if we check the customer number
[12:17:44] one over here, he did order around 10
[12:17:47] January and the second order is like
[12:17:50] after 10 days 20 January. So we have to
[12:17:53] go and subtract those two dates. Now in
[12:17:56] order to subtract those informations and
[12:17:58] do calculations, we have to have
[12:17:59] everything in the same row. So for
[12:18:01] example, if we are at the first row over
[12:18:03] here, I would like to have as well one
[12:18:05] column about the next order. So the date
[12:18:08] of the next order. So we have to access
[12:18:11] a value from another row. Of course, we
[12:18:13] can go and do joins, but we have lead
[12:18:16] and lag functions. And for this
[12:18:17] scenario, we're going to go and use the
[12:18:19] lead window function. So let's go and do
[12:18:21] that. I'm going to go and call the order
[12:18:23] date over here as a current order. And
[12:18:26] let's go and calculate the lead. So we I
[12:18:30] would like to get the next order date.
[12:18:32] So I would like to get this value over
[12:18:34] here in the same row. That's why we this
[12:18:37] time we're going to get the order dates.
[12:18:39] And now let's go and define the window.
[12:18:42] Now we have to go and partition the data
[12:18:44] because we are analyzing each customers
[12:18:46] separately, right? So that's why we have
[12:18:48] to partition that by the customer ID.
[12:18:51] And of course in order to do the lead,
[12:18:53] we have to use the order by. So let's go
[12:18:55] and define that as well. Order by and
[12:18:58] it's going to be by the order date. So
[12:19:00] now we have to give it a name. The order
[12:19:02] date here is the current order. This
[12:19:04] going to be the next order. So next
[12:19:07] order. Let me zoom out a little bit and
[12:19:10] make this smaller. So let's go and
[12:19:12] execute it. So now as you can see in the
[12:19:14] output we got a new column called next
[12:19:16] order. And with that we got the current
[12:19:19] order, the current row and as well the
[12:19:22] value from the next row. So what is the
[12:19:24] next row? It's going to be the 20
[12:19:25] January. The same thing of course for
[12:19:27] the next row. Over here we have the
[12:19:29] current order date and the next order
[12:19:32] date. So this value going to be exactly
[12:19:34] as the next one over here 15 of February
[12:19:37] and then since we are working with
[12:19:39] window since this is the whole window
[12:19:40] over here the last order for this
[12:19:43] customer it's 15 of the February there
[12:19:46] is no next order so this going to be
[12:19:48] null the same thing if you check the
[12:19:50] other customers you're going to see
[12:19:52] always the last order don't have any
[12:19:54] next order so looks like everything is
[12:19:57] fine and for the last customer he has
[12:19:59] only one order so now with this we got
[12:20:01] all the informations for our
[12:20:03] calculations. So we have the current
[12:20:05] order and the next order in the same
[12:20:07] row. Now we can go and subtract them in
[12:20:10] order to get the days between those two
[12:20:12] orders. And now in order to subtract
[12:20:14] date we has to use the function date
[12:20:16] div. Don't worry about those functions.
[12:20:18] We're going to explain all those stuff
[12:20:19] in the next chapters. So now just follow
[12:20:21] me with those steps. What we're going to
[12:20:23] do, we're going to go and subtract this
[12:20:25] date the order date with the whole thing
[12:20:27] over here. Right? So the whole thing
[12:20:29] here is the next order. So let's do it
[12:20:31] in a new line and it's going to be very
[12:20:34] simple. So date diff we are finding the
[12:20:37] differences between two dates. So the
[12:20:39] syntax going to be like this. First we
[12:20:41] have to define what we are talking
[12:20:42] about. Are they days, months, years and
[12:20:44] so on. So we have to tell SQL find me
[12:20:46] the differences in days. Now we have to
[12:20:49] specify two days. So the first one going
[12:20:51] to be the order date. This is the
[12:20:53] current date and the second date going
[12:20:55] to be the whole thing from here. So
[12:20:58] let's take it and put it side by side
[12:21:00] and this calculation going to give us
[12:21:02] number of days. So we're going to call
[12:21:04] this days until next order. All right.
[12:21:08] So now let's go and execute the whole
[12:21:10] thing. So now let's check the result. As
[12:21:11] you can see over here we got 10. So this
[12:21:13] is 10 days between those two dates and
[12:21:16] the next one we have around 26 days.
[12:21:19] Here we have a null because we don't
[12:21:20] have here a date and for the next one we
[12:21:23] have 31 days. So we have a whole month
[12:21:25] over here. So everything is working
[12:21:27] perfectly and with that we have solved
[12:21:30] only this part days between their
[12:21:32] orders. So guys you see right this is
[12:21:34] the magic of the lead and lag function.
[12:21:36] We can very easily access any
[12:21:38] information you need in the same row in
[12:21:40] order to do such a important analyzis
[12:21:42] and with very simple query. We are not
[12:21:44] doing any crazy stuff like joining and
[12:21:46] stuff. We are just specifying the lead
[12:21:48] function. So now we got all the
[12:21:50] informations that we need. Next we're
[12:21:52] going to go and calculate the average of
[12:21:53] those days. So in order to do that we
[12:21:56] have to go and use a subquery. So let me
[12:21:58] just zoom out. So let's go and select
[12:22:01] star just prepare the subquery. So the
[12:22:05] whole thing going to be a subquery. I'm
[12:22:06] going just get rid of the order by it's
[12:22:08] not now necessary. So let's me just put
[12:22:11] it like this and shift it. So now what
[12:22:14] do we need? We need the average of the
[12:22:16] days. So we need the average of this
[12:22:17] value. So what can we do? We're going to
[12:22:19] go and use a group by. So customer ID
[12:22:22] since we have to find the average for
[12:22:24] each customers and we're going to get
[12:22:26] this value and say average days until
[12:22:29] the next order and we're going to call
[12:22:30] it average days. So and we have here to
[12:22:34] group by. So group by customer ID. So
[12:22:38] like this just make this a little bit
[12:22:41] smaller and zoom in here. So that's it.
[12:22:43] Now we are just doing a very simple
[12:22:45] average and group I statements. So let's
[12:22:47] go and execute it. Now as you can see
[12:22:49] it's going to go and aggregate the data.
[12:22:51] So we have now only four customers and
[12:22:53] for each customer we have the average
[12:22:56] days between their orders. So now what
[12:22:58] is missing in our task? If you check
[12:23:00] over here it says rank the customers
[12:23:02] based on this average. So we have to go
[12:23:05] and use the rank function. So here again
[12:23:07] another window function that we have to
[12:23:09] go and use. We're going to do it
[12:23:10] together with the group by. So let me
[12:23:12] just make this a little bit smaller and
[12:23:15] then let's do it over here. So I'm just
[12:23:17] going to go with the rank function. Then
[12:23:19] we're going to define the window like
[12:23:21] this over order by and then we're going
[12:23:23] to go and sort the data by the average
[12:23:25] days. So that means we're going to go
[12:23:27] and get this calculation over here and
[12:23:29] put it as order by it's going to be
[12:23:31] ascending. So we are focusing on the
[12:23:33] lowest average days. So that's it. Let's
[12:23:36] call it rank average. So now let's go
[12:23:39] and execute this. So now by checking the
[12:23:41] result, you can see now we have a
[12:23:43] ranking for the average. And here skill
[12:23:46] says that the number one customer or the
[12:23:49] number one loyal customer is the
[12:23:51] customer number four which is not really
[12:23:52] correct because the number four we don't
[12:23:54] have a lot of informations about this
[12:23:56] customer he or she did order only once.
[12:23:59] So either now you go and like filter the
[12:24:01] data and remove this customer where you
[12:24:03] say if the average is null then don't
[12:24:05] put it in the rank or we can go and
[12:24:07] replace this value with a very huge
[12:24:09] value in order to make it at the end of
[12:24:11] our list. For example, we can go over
[12:24:13] here and replace the null with
[12:24:16] qualisk like this. And we say if the
[12:24:19] average is null, then let's say give me
[12:24:22] a crazy number like this very huge one.
[12:24:25] So that's it. Let's go and execute. And
[12:24:27] now as you can see this customer going
[12:24:29] to be at the end of our list. And now we
[12:24:31] can see that the most loyal customer is
[12:24:33] number one. And then the other two
[12:24:35] customers are in the rank two. Here we
[12:24:38] are sharing the same rank since we have
[12:24:39] the same average. So guys with that we
[12:24:41] have solved the task and we have ranked
[12:24:42] the customers based on the average days
[12:24:45] between their orders. So we have now a
[12:24:47] really nice rank and we can understand
[12:24:49] now the behavior of the customers and
[12:24:51] maybe we have to go and focus on the
[12:24:53] customer number one and understand her
[12:24:55] or her needs. And of course the function
[12:24:56] that helped us here in order to do such
[12:24:58] a customer retention analyszis is the
[12:25:01] lead function in order to find the next
[12:25:03] order to calculate the days. So this is
[12:25:05] how you use lead functions to do such a
[12:25:07] use case.
[12:25:12] the first value and the last value
[12:25:14] functions. I think the name says
[12:25:16] everything, right? So the first value
[12:25:17] going to allow you to access a value
[12:25:19] from the first row within a window where
[12:25:22] the last value exactly the opposite. It
[12:25:24] going to allow you to access a value
[12:25:26] from the last row within a window. Easy,
[12:25:29] right? So now let's understand how SQL
[12:25:31] execute those functions. Okay. So now as
[12:25:33] usual, we have this very simple example.
[12:25:34] we have the months and sales and we have
[12:25:36] it twice because we would like now to go
[12:25:38] and compare side by side the two
[12:25:40] functions first value and last value. So
[12:25:42] now for the left sides we would like to
[12:25:44] get the sales of the first month and on
[12:25:47] the right sides we would like to get the
[12:25:49] sales of the last month. So now for the
[12:25:51] first task we can go and use the first
[12:25:53] value. It's very simple. So the first
[12:25:55] value function then the argument going
[12:25:56] to be sales since we want the sales and
[12:25:59] then the window going to be defined like
[12:26:00] this order by month because we want to
[12:26:03] get the first month. So as usual we must
[12:26:06] use order by now on the right side in
[12:26:08] order to get the sales of the last
[12:26:09] months we can go and use the last value
[12:26:11] right so the same things last value
[12:26:13] sales over order by month. So as you can
[12:26:16] see on the left and right we don't use
[12:26:18] any frame definition but the default
[12:26:20] going to be used from this. All right.
[12:26:21] So now let's see how SQL going to
[12:26:22] process both of those queries side by
[12:26:25] side. So the first step is SQL going to
[12:26:26] go and sort the data. They are already
[12:26:28] sorted from the lowest to the highest.
[12:26:30] And then the next step is going to start
[12:26:32] row by row finding the first value on
[12:26:34] the left side. So what is the unbounded
[12:26:36] proceeding? It's going to be static and
[12:26:39] always pointing to January. So this is
[12:26:42] always going to be the unpounded
[12:26:43] proceeding. We have it in both sides
[12:26:45] like this. And what is the current row?
[12:26:48] It's going to be at the start the first
[12:26:50] row. And on the right side the same
[12:26:52] things over here. So the window
[12:26:54] definition going to be is only one row
[12:26:57] right. So what is the first value in
[12:27:00] this window? It is 20. Right? The same
[12:27:03] things on the right side. What is the
[12:27:05] last value in this window? It is as well
[12:27:08] 20. So we will get exactly same results.
[12:27:10] Now let's move to the second row. So
[12:27:12] it's going to be pointing to February.
[12:27:14] And the frame definition going to be
[12:27:16] here extended like this. So what is the
[12:27:19] first value in this frame? It's going to
[12:27:21] be as well 20. Right? So in the output
[12:27:24] we're going to get 20. And now in the
[12:27:25] right side the current row going to be
[12:27:27] as well pointing to February and the
[12:27:30] window going to go get extended. So now
[12:27:32] what is the last value of this frame?
[12:27:35] It's going to be 10. Now let's keep
[12:27:37] going. We're going to go to the March
[12:27:39] and the window going to get extended.
[12:27:41] What is the first value? It's always
[12:27:44] going to be the same. So 20 on the right
[12:27:46] side window going to get extended. What
[12:27:49] is the last value? It's going to be 30.
[12:27:52] So as you can see the default definition
[12:27:53] is always having the static start always
[12:27:56] the same start of the subset and as we
[12:27:58] are moving with the current row the
[12:28:00] frame going to get extended. So now
[12:28:02] moving to the last one and with that we
[12:28:04] will get the whole data set inside the
[12:28:07] frame and the first cell is going to be
[12:28:09] 20 on the right side. the same things
[12:28:12] going to get extended like this and this
[12:28:14] time the last one going to be April and
[12:28:16] five. So now if you go and compare them
[12:28:18] side by side you see that on the left
[12:28:20] side the task is solved and everything
[12:28:22] is working correctly right. So we have
[12:28:24] for each row always the sales of the
[12:28:26] first row and what is the first row it
[12:28:29] is January. So we have everywhere a 20
[12:28:31] which is correct. But now if you check
[12:28:33] the right side you can see there is
[12:28:35] something wrong right? We are getting
[12:28:37] not the last value. We should always get
[12:28:39] April right? We should have here
[12:28:40] everywhere a five. So we have here
[12:28:43] exactly the same result as the sales. So
[12:28:45] it's really useless to use it like this,
[12:28:47] right? And that's of course because SQL
[12:28:49] is using the default definition of the
[12:28:51] window frame. Last value is the only
[12:28:53] function from all window functions that
[12:28:55] you cannot use the default frame
[12:28:58] definition. You have to go and customize
[12:29:00] the frame definition in order to get the
[12:29:02] effect of the last value. For the first
[12:29:04] value, everything is working. If you're
[12:29:05] using a default frame, if you are not
[12:29:07] specifying anything, but for the last
[12:29:09] value, you will not get the effect
[12:29:10] correctly without customizing the frame
[12:29:13] window. So my friends, you can go and
[12:29:15] use the first value function like all
[12:29:17] other window functions without defining
[12:29:19] a frame. You can go with the default and
[12:29:21] you will get the effect of the first
[12:29:22] value, but the last value you have to go
[12:29:24] and define a frame. So let's see how we
[12:29:26] can solve that. All right. So now in
[12:29:28] order to solve this, we going to define
[12:29:29] the frame like this. It's going to be
[12:29:31] the rows between the current row and the
[12:29:34] unbounded following. So we just switch
[12:29:36] things around. So now let's see how this
[12:29:38] going to work. Now of course it's going
[12:29:40] to go and sort the data and so on. Now
[12:29:42] it's still going to have a pointer to
[12:29:44] the unbounded following. So it's going
[12:29:46] to point always to the last row in our
[12:29:48] data set and then it's going to proceed
[12:29:50] step by step. So the first row going to
[12:29:52] be like this and the frame going to be
[12:29:54] the whole thing, right? So from the
[12:29:56] current row until the unbounded
[12:29:58] following. So what is the last value the
[12:30:01] last row? It's going to be the five,
[12:30:03] right? The April. So we will get in the
[12:30:04] output five. Now let's proceed to the
[12:30:07] next value. The frame going to be
[12:30:09] shorter and smaller. And what is the
[12:30:11] last value? It's going to be as well the
[12:30:13] five. Right? So now we jump to the next
[12:30:15] one. And the frame going to be like
[12:30:17] this. What is the last value? As well
[12:30:19] five. And then we will get the last
[12:30:21] value like this. Current row is equal to
[12:30:24] the unbounded following. We have only
[12:30:25] one row and it's going to be as well
[12:30:27] five. So as you can see it's very simple
[12:30:29] just fix the frame clause and you will
[12:30:31] get the last value working as expected.
[12:30:34] So this is how SQL going to go and do
[12:30:36] it. Now let's go back to SQL and start
[12:30:38] practicing. All right. So now we have
[12:30:39] the following task. It says find the
[12:30:42] lowest and highest sales for each
[12:30:44] product. So now let's see how we can do
[12:30:46] this. As usual we're going to start with
[12:30:47] very simple select statement. So select
[12:30:50] order ID. We need the product ID and as
[12:30:54] well their sales. So let's select the
[12:30:56] table sales orders. So that's it. Let's
[12:31:00] go and select this. Now in the output we
[12:31:02] got our orders, products and sales. So
[12:31:05] now let's start with the first part of
[12:31:06] the task. Find the lowest sales for each
[12:31:09] product. So in order to do that, we can
[12:31:11] use the first value function. So let's
[12:31:14] go and do that. First value. Then what
[12:31:17] we are talking about, we have to give an
[12:31:18] expression. We need the lowest and
[12:31:21] highest sales. So let's go and have the
[12:31:23] sales inside it. And now we have to
[12:31:25] define the window. So over since we are
[12:31:28] saying for each product that means we
[12:31:29] have to go and make windows. So we have
[12:31:32] to divide the data using partition by
[12:31:34] products ID. And then we must use an
[12:31:37] order by right. So we have to go and
[12:31:39] sort the data by the sales. Since the
[12:31:42] first value should be the lowest value,
[12:31:45] we have to do it as ascending from the
[12:31:47] lowest sales to the highest sales. So
[12:31:49] we're just going to leave it like this
[12:31:50] as a default and we're going to call it
[12:31:53] lowest sales. So let's go and execute
[12:31:57] this. So now let's go and check our
[12:31:58] results. First going to go and partition
[12:32:00] the data by the product ID. So as you
[12:32:02] can see we got now here four windows.
[12:32:05] Then sort the data by the sales. So the
[12:32:07] data are sorted from the lowest to the
[12:32:09] highest from 10 to 90. So now what is
[12:32:12] the first value of the sales? It is the
[12:32:14] first row, right? So it's going to be
[12:32:16] 10. That's why we have everywhere a 10.
[12:32:19] Let's check another one. Let's take this
[12:32:21] one here. So this window has two rows
[12:32:23] and it is sorted the lowest sales or
[12:32:26] let's say the first value is 25. So with
[12:32:29] that we have solved the first part of
[12:32:31] the task finding the lowest sales for
[12:32:33] each product. Let's go to the next one.
[12:32:35] We have to find out the highest sales
[12:32:36] for each product. So let's go and use
[12:32:39] the last value for this. So let's have a
[12:32:41] new line. We're going to have a last
[12:32:44] value again the
[12:32:47] sales. Then we're going to go and define
[12:32:49] the window. So it's going to be the
[12:32:50] exact same window. We have to partition
[12:32:52] the data by the product ID and order the
[12:32:54] data by sales. So let's go and just copy
[12:32:58] the previous one and let's call it for
[12:33:00] now highest sales. So let's go and
[12:33:04] execute it. So now if you check the
[12:33:06] results, you will see our issue over
[12:33:07] here again. Right? We are not getting
[12:33:09] the highest sales for this window. The
[12:33:11] highest sales is 90. But as you can see,
[12:33:13] we are getting the exact same sales. And
[12:33:15] we have explained that in the previous
[12:33:16] example. So in order to fix this, we're
[12:33:19] going to go and add for it the frame. So
[12:33:22] rows between current row and the
[12:33:26] unbounded following.
[12:33:28] So now let's go and execute this. So now
[12:33:32] let's check the result. As you can see
[12:33:34] over here, we got the highest sales
[12:33:36] correctly. So for this window, the
[12:33:37] highest ones is 90. and as well for this
[12:33:40] window the 60 and so on. So with that
[12:33:42] you have solved both of the tasks the
[12:33:44] lowest and the highest sales. But now I
[12:33:46] would like to show you my honest opinion
[12:33:48] about this tasks. I will not go and use
[12:33:51] the last value to find the highest
[12:33:53] sales. So let me show you how I usually
[12:33:55] do it. I'm going to go and use the first
[12:33:57] value in order to find the last value.
[12:34:00] So now let me show you what I mean.
[12:34:01] Let's go and add a new row. I will just
[12:34:04] take the whole thing from the lowest
[12:34:06] sales. But what I'm going to do, I'm
[12:34:09] just going to go and change the order.
[12:34:10] So that means we will not go and sort
[12:34:12] the data like this ascending from the
[12:34:15] lowest sales to the highest sales. We're
[12:34:16] going to go and switch it. So we're
[12:34:19] going to go and sort the data from the
[12:34:20] highest sales to the lowest sales. And
[12:34:23] with that, the first value going to be
[12:34:25] the highest sales. So let me just rename
[12:34:28] it highest sales. Let's give it like
[12:34:31] two. So let's go and execute this. And
[12:34:34] now you can see over here we got the
[12:34:36] exact same results because we sorted the
[12:34:38] data differently and we get the first
[12:34:40] value. So this is going to give you the
[12:34:42] exact same effect like the last value.
[12:34:45] And as you can see I don't have to
[12:34:46] define now any window or something like
[12:34:48] that. I can stick with the default frame
[12:34:51] but just twisting the order by. So this
[12:34:54] is how you can do it as well using only
[12:34:56] the first value. So now just for the
[12:34:58] sake of this task there's as well
[12:35:00] another possibility in how to solve
[12:35:01] this. You can go and use the minmax
[12:35:04] functions. So let me just take the same
[12:35:07] and have a new one the lowest sales. We
[12:35:10] can go and say you know what let's get
[12:35:13] the min. So we are saying find me the
[12:35:15] minimum sales and we don't have to go
[12:35:18] and sort anything. So we can go and just
[12:35:20] divide it like this. So let's give it
[12:35:22] another ID. Let's go and execute it. So
[12:35:25] as you can see we got the exact same
[12:35:27] results like the other two highest
[12:35:29] sales. So as you can see we can solve
[12:35:31] this task using three different
[12:35:32] functions. Either go and use the last
[12:35:35] value but you have to define the frame
[12:35:37] or you can go and use the first value
[12:35:39] where you switch or flip the order by or
[12:35:42] simply just using the max function in
[12:35:44] order to get the highest sales. So guys
[12:35:47] as you can see we can use the first
[12:35:48] value and the last value in order to
[12:35:50] find out the extremes like here in this
[12:35:52] example the lowest and the highest
[12:35:54] sales. So there is like similarity
[12:35:56] between those two functions and as well
[12:35:57] the min and max. And of course what
[12:35:59] we're going to do with this value over
[12:36:01] here we can go and compare it with the
[12:36:03] current sales. So for example we can go
[12:36:05] and extend our task where we say find
[12:36:07] the difference in sales between the
[12:36:09] current and the lowest sales. So in
[12:36:12] order to do that let me just clean up
[12:36:13] all those stuff and let's stick with the
[12:36:16] first value and the highest value like
[12:36:18] this. So we have to compare now the
[12:36:20] current sales which is this field over
[12:36:23] here. the sales the original one with
[12:36:25] the lowest sales with the whole thing
[12:36:27] from here. So let's go and do that. So
[12:36:29] we're going to have a new line and we're
[12:36:30] going to say just simply subtract the
[12:36:33] sales from the lowest sales like this.
[12:36:36] And let's give it a name sales
[12:36:39] difference. So that's it. Let's go and
[12:36:42] execute it. Now as you can see the
[12:36:44] result in one row I'm comparing the
[12:36:46] current sales which is 90 with the
[12:36:48] lowest sales from this product. It's
[12:36:50] going to be the 10. So with that we're
[12:36:52] going to get the distance let's say
[12:36:54] between those two informations and it
[12:36:56] going to be 80. So now for the next one
[12:36:58] the distance between this value and the
[12:37:00] lowest value is shorter. So we are near
[12:37:03] the lowest value. So as you can see over
[12:37:05] here we can now compare the sales
[12:37:08] between the current sales and one
[12:37:10] extreme in order to find the distances
[12:37:12] between two values. So this is again
[12:37:15] very important analysis in order to do
[12:37:17] comparison analyszis.
[12:37:22] All right friends, so now let's do a
[12:37:24] quick recap about the value functions or
[12:37:25] we call them sometimes analytical
[12:37:27] functions. So what they do, they're
[12:37:29] going to go and allow you to access a
[12:37:30] specific value from another row. This
[12:37:33] going to help you in order to do complex
[12:37:34] calculations with very simple SQL
[12:37:37] without having you joining tables
[12:37:39] together or doing self joins. And for
[12:37:41] the value functions we have four types
[12:37:43] or let's say for functions the first one
[12:37:45] allows you to access the previous value
[12:37:47] like the previous month using the lag
[12:37:50] function. The next one it allows you to
[12:37:52] access the next values the next month
[12:37:55] using the lead function. Then we have
[12:37:57] another one it allows you to access the
[12:38:00] first value in a subset using the first
[12:38:02] value function. And another option we
[12:38:04] can go and access the last value in a
[12:38:07] subset using the last value function.
[12:38:09] Moving on to the next one, we have the
[12:38:11] rules of the syntax. So about the first
[12:38:13] point, it is the expressions. We can go
[12:38:15] and use any data type. It could be a
[12:38:17] number, string, a date, anything. Now in
[12:38:19] order to perform those functions, we
[12:38:21] have to go and sort the data by the
[12:38:22] order by. So order by is required. It is
[12:38:25] a must. Then for the frame, you are
[12:38:28] allowed to use it. So it is an optional
[12:38:30] thing. I would say always leave it empty
[12:38:32] for the frame. But only for the last
[12:38:33] value, you have to go and customize
[12:38:35] otherwise it will not work. Now to the
[12:38:37] next point, we have the use cases. We
[12:38:39] have simply very important use cases for
[12:38:41] the value functions in data analytics.
[12:38:43] So what we can do? We can do time series
[12:38:45] analyszis. As we learned, we can do
[12:38:47] month overmonth analyzes and
[12:38:49] yearover-year analyzes. Those analyszis
[12:38:52] are classical and it's always the first
[12:38:54] question in that analyszis in order to
[12:38:56] measure are we growing with the business
[12:38:58] or are we declining? How the performance
[12:39:00] between the current year and the
[12:39:02] previous year. So as you can see we are
[12:39:03] doing always comparison using those
[12:39:06] window functions. The next use case is
[12:39:08] as well about the time we can do time
[12:39:10] gap analyzes as we analyzed the customer
[12:39:13] behavior the customer retention where we
[12:39:15] have calculated the average days between
[12:39:17] two orders and the last use case it's as
[12:39:20] well about comparison comparison
[12:39:21] analyzes we can go and use the value
[12:39:24] functions in order to compare the
[12:39:25] current value with extreme like
[12:39:27] comparing the current sales with the
[12:39:29] highest sales or to the lowest sales. So
[12:39:32] my friends those analyzers are essential
[12:39:34] in data analyzers you will be countering
[12:39:37] them in each company in each business
[12:39:39] you have to answer those questions and
[12:39:41] you can do that very easily using the
[12:39:43] SQL window functions all right my
[12:39:45] friends so that's all about the window
[12:39:47] value functions and with that we have
[12:39:49] covered everything about how to
[12:39:50] aggregate your data using SQL and those
[12:39:53] are very important tools on how to do
[12:39:55] data analytics in SQL especially if you
[12:39:58] are a data scientist and data analyst.
[12:40:00] So with that we are done with this
[12:40:01] chapter and I can tell you with that we
[12:40:03] have covered the intermediate level. So
[12:40:05] we have learned how to filter the data,
[12:40:07] how to combine the data and as well the
[12:40:09] most important functions in SQL. Now
[12:40:11] we're going to go to the third and last
[12:40:13] level we will cover now the advanced
[12:40:15] level. So the first level going to be
[12:40:17] about the advanced SQL techniques. So
[12:40:19] now if you go inside it and in SQL there
[12:40:21] are like different techniques in order
[12:40:23] to organize our complex projects. So
[12:40:26] first I'm going to explain for you what
[12:40:27] is exactly I'm talking about what is
[12:40:29] complex queries and why we have it and
[12:40:31] then we're going to start with the first
[12:40:32] topic the subqueries. So let's
[12:40:38] go. Normally in projects we have a
[12:40:40] database and we have a person that is
[12:40:42] responsible for the database the
[12:40:44] database administrator that take cares
[12:40:47] of the database structure. And now in
[12:40:49] very simple scenario we're going to have
[12:40:50] a user that is writing queries in order
[12:40:53] to retrieve data from the database. So
[12:40:55] he or she going to write an SQL query
[12:40:57] and then this query going to be sent to
[12:40:59] the database where it's going to execute
[12:41:00] it and then the database going to return
[12:41:03] the results. So at the end our user
[12:41:05] going to see the result of the query
[12:41:07] that he wrote. So this is a very
[12:41:09] simplified scenario on how we use a
[12:41:13] database. But my friends in the real
[12:41:15] world things are totally different.
[12:41:17] Things in real projects get very
[12:41:19] complicated like this. So for example,
[12:41:21] you have a financial analyst that is
[12:41:24] writing a huge block of SQL query that
[12:41:27] is very complex and there will be like
[12:41:30] another user that have different role
[12:41:32] like a risk manager that is as well
[12:41:34] writing a very complex query and from
[12:41:38] different departments from different
[12:41:39] projects for different tasks. You will
[12:41:42] have a lot of analysts that are writing
[12:41:45] many complex queries. So all those
[12:41:47] analysts and managers have a direct
[12:41:50] access to your database and they are
[12:41:52] executing a complex analytical queries
[12:41:54] in order to generate maybe a report or
[12:41:57] something. Now not only those guys are
[12:41:59] doing analyszis on your database you
[12:42:01] will have as well our friend the data
[12:42:03] engineer that is saying you know what
[12:42:05] I'm building a data warehouse and I
[12:42:07] would like to extract your data. So that
[12:42:09] data engineer going to go and write an
[12:42:11] extract query in order to extract the
[12:42:13] data from the database. And then he has
[12:42:16] a different script for the
[12:42:17] transformations in order to manipulate,
[12:42:20] filter, clean up, aggregate your data.
[12:42:22] And then a third script in order to
[12:42:25] collect the result of the
[12:42:26] transformations and load it in another
[12:42:29] database called data warehouse. A data
[12:42:32] warehouse is like special database that
[12:42:34] collect data from different sources and
[12:42:36] integrate it in one place. in order to
[12:42:38] do analytics and reporting. And now at
[12:42:40] the end of this chain, you will have a
[12:42:42] data analyst and she writes as well
[12:42:44] queries in order to analyze the data in
[12:42:47] the data warehouse. Or you might have a
[12:42:49] different query in order to prepare the
[12:42:51] data before inserting it to a tool like
[12:42:53] PowerBI in order to generate
[12:42:56] visualizations and reports. So we call
[12:42:58] this a data warehouse system or a
[12:43:00] business intelligence system that
[12:43:02] extract and extract from your data and
[12:43:05] manipulate it and transform it for
[12:43:07] analyzes. Now not only we have a data
[12:43:09] engineer and data analyst accessing your
[12:43:11] database and doing queries, we have as
[12:43:13] well our friend the data scientist. So
[12:43:15] now our data scientist as well has a
[12:43:17] direct access to your database. So he
[12:43:20] might write like different queries in
[12:43:22] order to extract the data and as well to
[12:43:24] manipulate the data that are needed in
[12:43:26] order to develop a model and doing
[12:43:28] machine learning and AI. And now one
[12:43:30] more scenario that I see in many
[12:43:32] projects where the result of the data
[12:43:34] analyst going to be used in another
[12:43:36] query in order to prepare the results
[12:43:38] for data visualizations PowerBI or in
[12:43:41] order to export like a Excel list. So as
[12:43:43] you can see we have a lot of people with
[12:43:45] different roles that want to access your
[12:43:48] database and do analyzes on top of it
[12:43:50] and that's because everyone want to
[12:43:52] answer questions based on the data and
[12:43:54] now if I look to this I still think this
[12:43:56] is a simplified version and how things
[12:43:59] works in the data projects and I can
[12:44:01] tell you in real projects things are way
[12:44:03] more complicated than this so now if you
[12:44:05] sit back and look to this we will find
[12:44:08] many challenges and problems for example
[12:44:10] all those people are not talking to each
[12:44:12] others And each one of them are creating
[12:44:14] like their own query. But if you go and
[12:44:17] take all those queries and compare them
[12:44:19] side by side, you will find in the
[12:44:21] scripts and queries logic that is keep
[12:44:23] repeating. So the queries from the
[12:44:25] analyst or the data scientists and data
[12:44:28] engineers, they might contain a
[12:44:30] redundant logic. And of course the issue
[12:44:33] of this we have the same effort
[12:44:35] repeating over and over and maybe not
[12:44:38] everyone is getting the logic
[12:44:39] implemented correctly because not all of
[12:44:41] them having the right skills in SQL. So
[12:44:44] this is a big issue in this setup. And
[12:44:46] now we have another challenge having
[12:44:48] this scenario. If you don't optimize it
[12:44:51] you will have a performance issue
[12:44:53] everywhere. So the data warehouse or the
[12:44:55] data engineer scripts might take like 5
[12:44:57] hours and the query from the analyst
[12:45:00] might take like 40 minutes and before
[12:45:02] inserting the data to reports we might
[12:45:04] have 30 minutes and 1 hour there 30
[12:45:06] minutes there and everyone else is as
[12:45:08] well suffering from bad performance on
[12:45:10] their queries and the performance
[12:45:12] everywhere is really bad. So if everyone
[12:45:14] is writing big complex queries don't
[12:45:16] expect that they will have a good
[12:45:18] performance. Now to the third challenge
[12:45:20] that I observed in many projects and
[12:45:22] that is the complexity. Now behind the
[12:45:24] original database you might have a data
[12:45:26] model that is prepared and optimized
[12:45:29] only for one application. So you will
[12:45:31] have in the data model a lot of tables
[12:45:32] and all those tables have different
[12:45:34] relationship between them and of course
[12:45:36] only the developers and the experts of
[12:45:39] this database understand the physical
[12:45:41] data model behind this database. And now
[12:45:43] if you give access to all those analysts
[12:45:46] they will have a lot of questions
[12:45:48] because first they have to understand
[12:45:50] the data model before writing any query.
[12:45:53] So that means a lot of data workers are
[12:45:55] keep asking our expert from this
[12:45:58] database questions. So for example how
[12:46:00] to connect the table A with the table B
[12:46:02] and where do I find my columns? What
[12:46:04] this table means? I'm getting bad result
[12:46:06] in my query because your data is really
[12:46:08] corrupt. So the developers of the
[12:46:10] database will get a lot of questions
[12:46:12] from the analyst and they have to
[12:46:14] explain over and over their data model
[12:46:16] so that the users are able to write
[12:46:19] those complex queries. So that means all
[12:46:21] those users are stressing the database
[12:46:23] team by many questions and as well the
[12:46:26] users are writing very complex queries.
[12:46:28] So the complexity is a really big
[12:46:30] challenge. Now as well by looking to
[12:46:32] this picture you will find a lot of
[12:46:34] errors from those queries to the
[12:46:35] database and this might cause a lot of
[12:46:39] database stress. So keep executing
[12:46:41] repeatedly a big complex queries going
[12:46:44] to makes really big stress for the
[12:46:46] database and it going to bring the
[12:46:48] database down. And the last challenge of
[12:46:50] this picture is that the data security.
[12:46:52] So if you leave it like this by giving
[12:46:54] the users a direct access to your
[12:46:56] database tables you might have a problem
[12:46:59] because it might be okay for like some
[12:47:01] data engineers and so on but you don't
[12:47:03] want to give for each data analyst a
[12:47:05] full access to the database tables. So
[12:47:08] you have to protect your tables the
[12:47:10] columns the rows everything. So you
[12:47:12] cannot leave it like this where everyone
[12:47:14] having a direct access to the physical
[12:47:16] database tables. Now enough talking
[12:47:18] about challenges problems and issues.
[12:47:20] Let's be solutionoriented. So what are
[12:47:23] the solutions of those issues? Of
[12:47:25] course, there are many solutions, but
[12:47:26] we're going to focus now on five
[12:47:28] techniques. We can go and use sub
[12:47:30] queries or CTE, common table
[12:47:32] expressions. We can introduce views to
[12:47:34] our database or temporary tables or we
[12:47:37] can go and use the technique of the CTAs
[12:47:40] carrier table as select. So this is
[12:47:43] exactly why we have to understand those
[12:47:45] five techniques in order to solve all
[12:47:47] those issues that we might face in our
[12:47:49] data
[12:47:53] projects. All right friends, so now
[12:47:55] after we understood the importance of
[12:47:57] those five techniques, let's take a
[12:48:00] quick and simplified look to the
[12:48:01] database architecture because I want you
[12:48:03] to understand what happens behind the
[12:48:06] scenes and how the database execute the
[12:48:09] queries from these five techniques. So
[12:48:12] by understanding this architecture you
[12:48:14] will understand how things works. So
[12:48:16] let's go. For each story there are two
[12:48:18] sides. We have the server side and the
[12:48:20] client side. In the client side it's
[12:48:22] like for example you you are writing an
[12:48:24] SQL query for a specific purpose. Now in
[12:48:27] the server side we have many things. So
[12:48:30] the server is where the database lives
[12:48:32] and it has many components like the
[12:48:34] database engine. The database engine is
[12:48:37] the brain of the database that handles
[12:48:40] different operations like storing,
[12:48:42] retrieving and managing data in the
[12:48:45] database. So each time you execute a
[12:48:47] query, the database engine going to take
[12:48:49] care of it. And now in the database we
[12:48:51] have very important component that is
[12:48:53] the storage and the two main types of
[12:48:55] storage in a database are disk storage
[12:48:58] and cache. The disk storage is like a
[12:49:01] long-term memory where the data is
[12:49:03] stored permanently. So it's like the
[12:49:05] disk at your PC. It stores the data
[12:49:08] permanently even if you turn off the
[12:49:10] system. And one important feature of the
[12:49:12] disk is that it can stores a lot of
[12:49:14] data. But the disadvantage of the disk
[12:49:16] storage is that it is slow. So it is
[12:49:19] slow to write and to read. Now in the
[12:49:21] other hand we have the cache is a
[12:49:23] short-term memory where the data is
[12:49:25] stored temporary. It's like the RAMs at
[12:49:27] your PC. It holds the most frequently
[12:49:30] used data. So the database can access it
[12:49:33] quickly in order to retrieve data. And
[12:49:35] the big advantage of the cache is that
[12:49:37] it is fast. So it is very fast for the
[12:49:39] database to retrieve data from the cache
[12:49:41] compared to the disk. But the
[12:49:43] disadvantage of the cache, the data is
[12:49:45] stored there only for short period. So
[12:49:47] it's like tradeoff between the speed and
[12:49:49] how much data you can store and how
[12:49:51] long. Now let's talk about the disk
[12:49:53] storage. This is very important in
[12:49:55] databases. There are typically three
[12:49:57] types of storage areas. There we have
[12:49:59] the user data, the system catalog and
[12:50:02] the temporary data and each storage type
[12:50:05] has a different purpose. So what is user
[12:50:07] data storage? It is the main content of
[12:50:10] the database. So it stores the actual
[12:50:13] data all the informations that are
[12:50:15] relevant for the users. So it's stored
[12:50:18] there all the important data that the
[12:50:20] users cares about. So this is the
[12:50:22] storage where the users are interacting
[12:50:24] all the time. So where do we find the
[12:50:26] user data? If you go to our database
[12:50:28] sales DB and then you go to the tables
[12:50:30] now we find all these tables that we are
[12:50:32] already used the customers employees
[12:50:34] orders and so on those tables are the
[12:50:37] user data. So now if I go and say select
[12:50:41] from sales orders and all those
[12:50:44] informations that we are seeing now are
[12:50:46] the users data. So this is what we users
[12:50:49] actually care about. All other stuff
[12:50:51] that we see inside databases as a user
[12:50:54] we don't care about it. We care only
[12:50:55] about our data. But in the database, we
[12:50:58] don't have only the user data. We have
[12:51:00] many other informations. So this is what
[12:51:02] we mean with the user data
[12:51:07] storage. Now what is system catalog?
[12:51:10] This is the internal storage for the
[12:51:12] database for its own information. So
[12:51:15] it's like a blueprint that keeps
[12:51:17] tracking everything about the database
[12:51:20] itself. So that means the main purpose
[12:51:22] of the system catalog is that it holds
[12:51:25] the metadata informations about the
[12:51:28] database. So what is a metadata?
[12:51:30] Metadata is data about data. Now let's
[12:51:33] understand what this means. What we have
[12:51:35] done so far is that we have created a
[12:51:37] table called customers and we have
[12:51:39] defined inside it like multiple columns
[12:51:41] like the customer ID, first name, last
[12:51:43] name and then we have inserted our data
[12:51:45] inside this table. So we have inserted
[12:51:48] five customers. So those informations
[12:51:50] are my data. I have created those
[12:51:52] informations and stored it inside the
[12:51:54] database. That's why we call it the user
[12:51:57] data. So nothing so far is new. So now
[12:51:59] what happens behind the scenes is that
[12:52:01] the database server will not only store
[12:52:04] the user data that you have provided but
[12:52:06] also it's going to go and store a
[12:52:08] different type of data inside the
[12:52:10] database and this data is the metadata.
[12:52:13] So the database server going to store
[12:52:15] the metadata of the customer's table and
[12:52:18] it going to look like this. There is
[12:52:20] like a table name, there is a column
[12:52:22] names and those are the column names
[12:52:24] that you have defined inside your
[12:52:26] database and those are the column names
[12:52:29] that you have defined as you are
[12:52:31] creating the table customers and it's
[12:52:33] going to store as well additional
[12:52:35] informations like which data type like
[12:52:37] the customer ID is int and the last name
[12:52:39] is v charts and many other informations
[12:52:41] like the length of the column and
[12:52:44] whether the column is nullable or not.
[12:52:46] So as you can see in the metadata we are
[12:52:48] having a description a data about the
[12:52:52] structure of the customers and in the
[12:52:54] metadata we can find a lot of
[12:52:56] informations about not only the tables
[12:52:57] and columns but as well about the
[12:52:59] schemas and the database. So you can
[12:53:02] find a full catalog about the structure
[12:53:04] of your database. Basic table the
[12:53:06] customers table it contains data about
[12:53:09] the actual data. So it stores data about
[12:53:12] the customers. But the metadata of the
[12:53:14] customers table contains data about
[12:53:17] data. So in the databases each table
[12:53:20] that you are using in order to store
[12:53:21] your data has a table twin that
[12:53:24] describes the structure of your data. So
[12:53:27] this is what we mean with a system
[12:53:29] catalog or a metadata. And now you might
[12:53:31] ask where I can find all those system
[12:53:33] catalog and metadata inside our client
[12:53:36] here. Well, you cannot navigate through
[12:53:38] those informations in the object
[12:53:40] explorer like we used to do for the user
[12:53:42] data. But you can find those
[12:53:43] informations in a special hidden schema
[12:53:47] called the information schema. The
[12:53:49] information schema in SQL server is a
[12:53:52] systemdefined schema that contains a set
[12:53:55] of built-in views that help us to find
[12:53:58] information about our database like
[12:54:00] tables, columns, and other objects. So
[12:54:03] let's go and explore it. We're going to
[12:54:05] go and say select star from
[12:54:09] information schema. And then let's have
[12:54:11] a dot. And now we get from SQL a list of
[12:54:14] all views that are available in order to
[12:54:16] browse the metadata of our database. So
[12:54:19] for example, you can see here tables.
[12:54:21] You can see informations about the views
[12:54:24] and as well about the columns. So let's
[12:54:26] go and select the columns and let's go
[12:54:29] and execute it. And now in the output we
[12:54:31] can find informations about the schema
[12:54:33] about the table names like for example
[12:54:36] here the customers. Let me just go and
[12:54:38] select this table. And then we find all
[12:54:41] the columns inside this table how they
[12:54:44] are sorted. So we have here the order of
[12:54:46] each column and as well the data type
[12:54:49] and the size of each column and many
[12:54:52] other stuff. So as you can see we got
[12:54:54] here all the informations all the
[12:54:56] metadata of each table and as well for
[12:55:00] each column inside the table. So with
[12:55:02] that you can check which tables does
[12:55:04] exist in your database. For example I
[12:55:05] find here like something called test
[12:55:07] two. So maybe I was trying to test
[12:55:09] something. I can go now and clean up
[12:55:11] stuff right and this is exactly why the
[12:55:13] database maintain such a catalog. It
[12:55:16] helps the database to quickly find the
[12:55:18] structure of each table and of each
[12:55:20] column. and it helps me as well as a
[12:55:22] user to browse the catalog of the
[12:55:24] database. So for example I can go over
[12:55:26] here and say okay let's get a distinct
[12:55:29] table name. So with that I will get a
[12:55:32] list of everything that I have inside
[12:55:35] the database. So we have the customers
[12:55:37] employees and some tests that I have
[12:55:39] done. So metadata are
[12:55:43] awesome. Now we come to the third
[12:55:45] storage that temporary data storage. It
[12:55:48] is a temporary space used by the
[12:55:50] database for short-term task like
[12:55:53] processing a query or sorting data. And
[12:55:55] once these tasks are done, what going to
[12:55:58] happen? The database going to go and
[12:55:59] clean up the storage. And now of course
[12:56:01] the question is where we can find these
[12:56:02] temporary tables that is using the
[12:56:05] temporary storage in the disk. Well
[12:56:07] actually if you go to the object
[12:56:08] explorer you will not find it inside our
[12:56:11] database sales DB but you will find it
[12:56:14] inside the system databases. Now since
[12:56:16] we are working locally we have the full
[12:56:18] access to everything inside the SQL
[12:56:20] server. But in real projects if you are
[12:56:22] just a user or let's say developer you
[12:56:25] will not have access to the system
[12:56:26] databases only for the database
[12:56:29] administrators. But now we are working
[12:56:30] on the local copy. So let's go to the
[12:56:33] system database and here you have a
[12:56:35] special database from the SQL server
[12:56:37] called temp DB. And if you go inside it
[12:56:41] we will find here tables and temporary
[12:56:43] tables. So this is exactly where you can
[12:56:45] find all the temporal tables that you
[12:56:47] are generating. Now currently we didn't
[12:56:49] create any temporary tables that's why
[12:56:52] it's empty. But once you start creating
[12:56:54] temporary tables you will find those
[12:56:56] tables underneath this folder. We will
[12:56:58] learn about the temporary tables in the
[12:57:00] next
[12:57:04] sections. So these are the main
[12:57:06] component of the database architecture.
[12:57:09] So now let's have an example. Now we
[12:57:10] have a table called orders that is
[12:57:12] stored inside the user storage and the
[12:57:15] metadata of this table is stored in the
[12:57:16] catalog. So now let's say that you are
[12:57:18] at the client side and you write a
[12:57:20] simple select query in order to select
[12:57:22] the data of the orders. So now that
[12:57:24] query is sent to the server in order to
[12:57:26] be executed and the database engine
[12:57:28] going to take the query in order to
[12:57:29] process it. So first the database engine
[12:57:32] going to check whether we have the data
[12:57:33] in the cache because if the data is
[12:57:35] stored in the cache then things going to
[12:57:37] be really fast and the database engine
[12:57:38] can solve the task quickly but in this
[12:57:40] scenario we don't have the orders
[12:57:42] informations in the cache that's why the
[12:57:44] database engine going to say okay it's
[12:57:46] not in the cache let's check the disk so
[12:57:48] it will find the orders information in
[12:57:50] the disk and the query going to be
[12:57:51] executed then the result of this query
[12:57:54] going to be sent back to the client side
[12:57:56] where at the end in return you will see
[12:57:58] in the output the result of the table
[12:58:01] orders. So this is how the SQL database
[12:58:03] execute very simple select
[12:58:09] query query is a query inside another
[12:58:13] query. So what this means let's have a
[12:58:16] sketch to understand it. So so far what
[12:58:18] we have learned we have different
[12:58:19] database tables like the orders
[12:58:21] customers and so on and we write a
[12:58:23] simple SQL queries like select from
[12:58:26] where. So the SQL going to retrieve data
[12:58:28] from the database tables and in the
[12:58:30] output we will get some kind of results.
[12:58:32] So this is so far what you have done. We
[12:58:34] have done very simple queries. Now in
[12:58:36] our query we can have things little bit
[12:58:38] different. So we could have another
[12:58:40] query that is inside our query where we
[12:58:42] do the same things like select from
[12:58:44] where. So we have now a query inside our
[12:58:47] query and we call this embedded query we
[12:58:50] call it a sub query and the original
[12:58:52] query the first one where we have select
[12:58:54] from we call it main query. So now if
[12:58:56] you execute the whole query what going
[12:58:58] to happen SQL first going to go and
[12:58:59] select the subquery and then it's going
[12:59:01] to execute it. So it's going to go and
[12:59:03] select and retrieve data from our
[12:59:05] database tables and the result of the
[12:59:07] subquery will not be sent to the users
[12:59:09] to us. So we cannot see it. What can
[12:59:12] happen? the result can stay inside the
[12:59:14] query as an intermediate results and
[12:59:17] then now our main query can go and start
[12:59:20] interacting with this intermediate
[12:59:22] result from the subquery. So the main
[12:59:24] query going to do some kind of
[12:59:25] operations on top of this intermediate
[12:59:27] results and use it for filtering or
[12:59:29] joining or any purpose and still the
[12:59:31] main query can go and query the original
[12:59:34] database tables. So now the main query
[12:59:36] has two sources for data. The original
[12:59:38] database tables and as well the result
[12:59:41] from another query. So now by looking to
[12:59:44] this you can see the subquery is a query
[12:59:46] inside the main query and it play a role
[12:59:49] of supporter. So it supports the main
[12:59:52] query with data and the main job of the
[12:59:55] main query is of course to get all those
[12:59:57] data and to show us at the end the final
[13:00:00] results. Now there is now two things
[13:00:02] about this intermediate results that we
[13:00:04] got as a result from the subquery. Once
[13:00:06] the execution of the query is completely
[13:00:09] done, what can happen is going to go and
[13:00:11] destroy this intermediate result. So
[13:00:13] it's going to totally drop it. So we
[13:00:15] will not find it anywhere. It's
[13:00:17] completely lost. Now the other thing
[13:00:19] about the intermediate results is that
[13:00:21] imagine you are making another query
[13:00:23] that is completely outside of the first
[13:00:25] query. We are selecting few tables from
[13:00:27] our database. Now you might say you know
[13:00:29] what is it possible to access the
[13:00:32] intermediate results from the first
[13:00:34] query. So now we are talking about
[13:00:36] completely external query you cannot do
[13:00:38] that. The intermediate result of the
[13:00:40] subquery is only locally known from the
[13:00:44] main query itself and it is not globally
[13:00:46] available for any other query. So the
[13:00:48] subquery can be used only from the main
[13:00:51] query.
[13:00:55] So with that we have understood what are
[13:00:57] subqueries and now you might ask me why
[13:01:00] do we need them in the first place? Why
[13:01:02] sub queries are important? Let's have
[13:01:04] the following sketch. Now in our complex
[13:01:06] task we might have to do several stuffs
[13:01:08] in our query. Like for example the first
[13:01:10] step we have to go and join tables in
[13:01:12] order to prepare the data and then the
[13:01:14] outcome of the joins should be filtered.
[13:01:16] So this going to be our step two. And
[13:01:18] then on top of that in the step three we
[13:01:20] have to go and do transformations like
[13:01:22] maybe handling the nulls or creating new
[13:01:25] columns and many other stuff. And the
[13:01:27] last step we want to go and do data
[13:01:29] aggregations like summarizing the data
[13:01:31] or finding average. Now if you go
[13:01:32] immediately and start writing the SQL
[13:01:34] query without having a plan what can
[13:01:36] happen you're going to end up having a
[13:01:39] long complex SQL query and it's going to
[13:01:41] be really hard to write and as well to
[13:01:44] understand and read. And now what we can
[13:01:45] do instead of that we're going to go and
[13:01:47] divide our task based on those steps. So
[13:01:50] we're going to write one query section
[13:01:52] for each step. For example, for joining
[13:01:53] tables we're going to have one query for
[13:01:55] filtering another one transformation
[13:01:57] another one and for the aggregation
[13:01:59] we're going to have the last query. So
[13:02:00] now since each step is like a
[13:02:03] preparation for the next step we can go
[13:02:05] and say each of those queries is a
[13:02:07] subquery. So for step one, step two,
[13:02:10] step three, we have sub queries and they
[13:02:13] are all doing like calculations and
[13:02:15] preparations for the last step to the
[13:02:17] aggregations and we call the last step
[13:02:19] the main query and of course the whole
[13:02:21] thing can exist in one single query. So
[13:02:23] if you want to visual this like you have
[13:02:25] a subquery in circle and then this
[13:02:28] circle belongs to a bigger circle called
[13:02:30] the main query. By the way, sometimes we
[13:02:32] call the main query as the outer query
[13:02:35] and the subquery we can call it an inner
[13:02:37] query. And of course, we can have many
[13:02:40] subqueries and many small circles inside
[13:02:42] each others to form something called
[13:02:44] nested queries. So this is the main
[13:02:46] purpose of using subqueries in our
[13:02:49] scripts and queries. It's going to help
[13:02:51] us to reduce the complexity and going to
[13:02:53] make it easier to read and we can have
[13:02:56] like a flow logical flow inside our
[13:02:58] queries.
[13:03:03] Now for the sub queries there are many
[13:03:05] different types and categories. So now
[13:03:07] what we're going to do I'm going to show
[13:03:08] you an overview of all those types and
[13:03:10] categories and then later we're going to
[13:03:12] deep dive into each of those types. So
[13:03:14] first of all if you are thinking about
[13:03:16] the dependencies between the subquery
[13:03:18] and the main query. There is mainly two
[13:03:20] types of subqueries. We have the
[13:03:23] non-correlated subquery. That means the
[13:03:26] subquery is independent from the main
[13:03:28] query. And the second type is the
[13:03:30] correlated subquery. It's exactly the
[13:03:33] opposite. The subquery gonna depend on
[13:03:35] the main query. Of course, we can
[13:03:37] explain all those stuff in details.
[13:03:39] Don't worry about it. So, this is the
[13:03:40] first group. Now, there is another group
[13:03:42] on how to group up the subqueries
[13:03:45] depending on the result type. So, I mean
[13:03:48] with this that the subquery has
[13:03:50] different output and results. For
[13:03:52] example, we have scalar subquery. It
[13:03:54] returns only one single value. or
[13:03:56] another type it's called the row
[13:03:58] subquery. It's going to return multiple
[13:04:01] rows and the final type called the table
[13:04:03] subquery. It is a subquery that returns
[13:04:06] multiple rows and as well multiple
[13:04:08] columns. Now we come to the third way
[13:04:10] and the last way on how to categorize
[13:04:12] the subqueries and this time based on
[13:04:14] the location and the clauses. So we are
[13:04:17] describing here where the subquery going
[13:04:19] to be used within the main query. So we
[13:04:22] can use it in different locations and
[13:04:24] clauses like the select clause or we can
[13:04:27] use it in the from clause and this is
[13:04:29] the most common type for the subqueries
[13:04:31] or we can use it before joining tables
[13:04:33] and we can use it in order to filter the
[13:04:35] data in the work clause and in the work
[13:04:38] clause as we learned there are two
[13:04:39] different sets of operators. We can use
[13:04:42] the subgrade together with the
[13:04:43] comparison operators the less, greater,
[13:04:46] equal and so on. Or we can use it with
[13:04:48] the logical operators like the in, any,
[13:04:52] all and exists. So now those are the
[13:04:55] different types and categories for the
[13:04:57] subqueries and we're going to now deep
[13:04:59] dive into all of them. So now let's go
[13:05:01] and start with the easiest category, the
[13:05:03] result types of the subqueries.
[13:05:10] Now we have different types of
[13:05:11] subqueries based on the results. So this
[13:05:14] means the amount of data that the
[13:05:16] subquery going to return. So the first
[13:05:18] type is the scalar subquery. So it is a
[13:05:21] subquery that it's going to return only
[13:05:24] one single value like for example the
[13:05:26] value three. Let's have an example for
[13:05:28] the scalar subquery. So in this query
[13:05:30] for example if you are saying select
[13:05:32] star you will get all columns all the
[13:05:34] rows from one table. But for the scalar
[13:05:36] subquery we need only one value. So how
[13:05:39] we usually get it is by doing some
[13:05:41] aggregations. For example, if you go and
[13:05:43] say let's get the average of sales. So
[13:05:46] let's execute it. And with that in the
[13:05:49] output we have only one value with a 38.
[13:05:51] We call such a query as a scalar query.
[13:05:54] So it has only one row and only one
[13:05:57] column. So this is a scalar query. All
[13:05:59] right. So now to the second type we have
[13:06:02] the row subquery. So it is a subquery
[13:06:05] that going to return multiple rows and a
[13:06:08] single column. So we're going to have
[13:06:09] like values 1 2 3. So it is only one
[13:06:12] column with multiple rows. Let's have an
[13:06:14] example for the row query. As you can
[13:06:16] see now we are saying select star from
[13:06:18] the table orders and now we are getting
[13:06:21] multiple rows and multiple columns. But
[13:06:24] for the row queries we need only one
[13:06:26] column. So you can go over here for
[13:06:27] example say customer ID. And if you go
[13:06:31] and execute it. So now if you check the
[13:06:32] output we have a single column and as
[13:06:35] well multiple rows. So we have like a
[13:06:37] list of values and this is what we call
[13:06:40] row query. All right. So now to the last
[13:06:42] type we have the table sub query. It's
[13:06:45] going to go and return multiple rows and
[13:06:48] as well multiple columns like any
[13:06:50] regular tables. So this subquery going
[13:06:52] to return a lot of values. Okay. So
[13:06:54] let's see an example of that table
[13:06:56] query. So if you check our example here,
[13:06:58] select star from orders, we got here
[13:07:01] multiple rows and as well multiple
[13:07:04] columns and of course we can go and
[13:07:06] select multiple columns like for example
[13:07:08] the order ID and the order dates. So if
[13:07:12] we execute it here in the output we have
[13:07:15] multiple columns we have two columns and
[13:07:17] as well multiple rows that's why this
[13:07:19] kind of query is as well a table query.
[13:07:22] All right. So with that we have learned
[13:07:23] the different types of subqueries based
[13:07:25] on the result type. Now we're going to
[13:07:27] go and learn how to use the subqueries
[13:07:30] in different locations in our query. So
[13:07:32] we're going to start with how to use
[13:07:34] subquery in the from
[13:07:36] [Music]
[13:07:39] clause. Okay. So we typically use the
[13:07:42] subqueries in the from clause in order
[13:07:45] to create temporary result sets that act
[13:07:48] as a table for the main query. So it's
[13:07:51] like in some scenarios we cannot use the
[13:07:54] tables directly from the database. We
[13:07:56] have to prepare it somehow before we do
[13:07:59] our actual query. Okay. So let's check
[13:08:01] the syntax of the sub query inside the
[13:08:03] from clause. So we start with the usual
[13:08:05] stuff where we go and say select and few
[13:08:08] columns that we want to retrieve and
[13:08:10] then we say okay from usually after the
[13:08:12] from comes the table name from our
[13:08:14] database that we want to query. But this
[13:08:17] time instead of writing the table name,
[13:08:19] we're going to have another SQL query.
[13:08:22] So that means we don't define the table
[13:08:24] name, we define another select
[13:08:26] statements where we have as well again
[13:08:28] select a column from specific table and
[13:08:31] then maybe we have a filter. And in
[13:08:33] order now to tell SQL this is a
[13:08:35] subquery, we have to use the
[13:08:37] parenthesis. So we're going to have the
[13:08:38] parenthesis at the start and at the end.
[13:08:41] This is a subquery. This is not the main
[13:08:43] query. And after the parenthesis, we can
[13:08:45] go and define the alias for the results
[13:08:48] that we're going to get from this
[13:08:49] subquery. In many databases, this alias
[13:08:52] is an optional, but for the SQL server,
[13:08:54] we have to go and specify an alias. So,
[13:08:57] it is a must in SQL server. So, again,
[13:08:59] we call this a subquery and the outer
[13:09:03] query we call it a main query. So, this
[13:09:05] is the syntax of the subquery in the
[13:09:07] from clause. Okay. Okay, so now we have
[13:09:09] the following task and it says find the
[13:09:11] products that have a price higher than
[13:09:14] the average price of all products. So
[13:09:16] we're going to do it step by step and
[13:09:18] here we have two steps. The first one is
[13:09:20] that we have to go and calculate the
[13:09:22] average price of all products and the
[13:09:24] second step we're going to use this
[13:09:25] value in order to filter the table
[13:09:28] products in order to find the prices
[13:09:30] that is higher than this average price.
[13:09:33] So let's start with the first step where
[13:09:34] we're going to find the average price.
[13:09:36] I'm going to select the following
[13:09:38] informations. So product ID,
[13:09:42] price from the table sales products. So
[13:09:47] let's go and execute it. So now we have
[13:09:49] the product and as well the prices and
[13:09:51] we need this price here in order to
[13:09:53] compare it with the average price. So
[13:09:56] that means we need this price and as
[13:09:58] well side by side we need the average
[13:10:00] price. So that means we need
[13:10:02] aggregations and details and that's why
[13:10:04] we're going to go with the window
[13:10:05] function average. So let's go and do
[13:10:07] this. This is very simple. So it's going
[13:10:09] to be the average
[13:10:11] price and we don't want to partition the
[13:10:14] data. So it's going to be an over empty
[13:10:16] and this going to be the average price
[13:10:19] like this. So let's go and execute it.
[13:10:21] And with that we have calculated the
[13:10:23] average price. So now we have all the
[13:10:24] informations in the first step. We have
[13:10:26] the average price, we have the price and
[13:10:28] as well the products. So now the next
[13:10:30] step is that we have to go and filter
[13:10:32] the data to find out all the products
[13:10:35] where the price is higher than the
[13:10:37] average. That means we will do this step
[13:10:40] based on those information that we have
[13:10:42] now. So that means we have to go and use
[13:10:44] the logic of subquery and main query.
[13:10:47] Since this is the first step to prepare
[13:10:49] the data, we're going to use this as a
[13:10:52] subquery. So we're going to call this a
[13:10:54] sub query like this. And we have to go
[13:10:58] and use it in the main query. So how we
[13:11:01] going to do that? We have to go and
[13:11:02] write the main query. So it's going to
[13:11:04] be I'm going to start over here. Select
[13:11:07] and then I will take all the columns
[13:11:10] from. So this is the main query. Let me
[13:11:14] just make this a little bit smaller. And
[13:11:17] what we're going to do now so now the
[13:11:18] main query going to get the data from
[13:11:20] the sub query. So the whole thing going
[13:11:23] to be used inside the from close. So now
[13:11:26] in order to put the subquery inside the
[13:11:28] main query we have to go and use the
[13:11:30] parenthesis. So we're going to have it
[13:11:32] at the start and as well at the end and
[13:11:34] what we usually do we go and add like a
[13:11:37] tab in order to understand okay this is
[13:11:40] the subquery and then this is the main
[13:11:42] query. So now one more thing that we
[13:11:44] have to add for the whole subquery in
[13:11:46] the SQL server that we have to give it
[13:11:48] an alias. So you can go and give it any
[13:11:50] name that you would like. I usually go
[13:11:52] with only one character with the T. It
[13:11:55] stands for table. So you can use
[13:11:56] anything that you want. But we have in
[13:11:57] SQL Server to give an alias for the
[13:12:00] subquery. So now what we are saying, we
[13:12:02] are saying select everything from the
[13:12:04] subquery. If you go over here and
[13:12:06] execute it, you will get the exact same
[13:12:09] results because the main query is doing
[13:12:11] nothing. It's saying just select
[13:12:12] everything from the subquery. But now in
[13:12:15] order to solve the task, we are not
[13:12:16] interested with all products. We are
[13:12:18] interested only the products where the
[13:12:19] price is higher than the average. That's
[13:12:22] why we have to go and use the where
[13:12:24] clause. So we're going to say where the
[13:12:27] price is higher than the average price.
[13:12:32] So this filtering is done in the main
[13:12:34] query. It's not inside the subquery. So
[13:12:37] now that means in the main query we are
[13:12:39] doing something. Let's go and execute
[13:12:41] it. And with that we saw the task. We
[13:12:43] are getting now two products where the
[13:12:45] price is higher than the average price.
[13:12:47] So as you can see it's very simple. If
[13:12:49] the task has multiple steps then we can
[13:12:51] do that using multiple sub queries until
[13:12:54] we have the main query and we can learn
[13:12:56] from this that the subquery is here is
[13:12:58] only to support the main query. So we
[13:13:01] are preparing here that all the data
[13:13:03] that we need in order to have the final
[13:13:05] result for the main query. So for this
[13:13:07] task we cannot go immediately
[13:13:09] calculating the results we have first.
[13:13:12] So for this kind of task we cannot
[13:13:13] immediately like put everything in one
[13:13:15] select query. We have first to prepare
[13:13:18] the data in one subquery and then pass
[13:13:20] the values for the main query. And this
[13:13:23] is what we mean with the table subquery.
[13:13:26] And here one quick tip for you. If you
[13:13:27] would like to see the intermediate
[13:13:29] results that we are getting from the
[13:13:31] subquery, you can go and highlight the
[13:13:33] subquery itself without the parenthesis.
[13:13:36] So we are just highlighting the
[13:13:38] subquery. You can go now and execute it.
[13:13:41] And with that SQL will not go and
[13:13:43] execute everything. SQL going to execute
[13:13:45] only what you are highlighting. So this
[13:13:47] is really nice way in order to see the
[13:13:49] results of the subquery as you are like
[13:13:52] debugging or searching for errors. You
[13:13:54] can go and see the intermediate results
[13:13:56] that is used from the main query. And of
[13:13:58] course if you deselect and not highlight
[13:14:00] anything and execute SQL going to go and
[13:14:03] execute everything the whole query. So
[13:14:05] this is how we use the table sub query
[13:14:08] inside the from close. All right. Right.
[13:14:10] So let's have another task and it says
[13:14:12] rank the customers based on their total
[13:14:15] amount of sales. So again if you check
[13:14:17] here we have like two steps. First we
[13:14:20] have to find the total amount of sales
[13:14:22] and then after that we have to go and
[13:14:25] rank the customers. So again we have
[13:14:27] like two steps and we can use the
[13:14:29] subqueries in order to solve it. So
[13:14:31] let's start with the first step where
[13:14:32] we're going to find the total amount of
[13:14:34] sales. So let's go and select the
[13:14:36] customer ID and as well the
[13:14:39] sales from the table sales
[13:14:44] orders. Let's go and execute it. So now
[13:14:47] in the output we have like multiple
[13:14:48] customers and their sales. We have to go
[13:14:50] and now find the total amount of sales
[13:14:53] for each customer. That means we have to
[13:14:55] go and use the group by. So we're going
[13:14:57] to go and summarize the
[13:15:00] sales. So total sales and then group up
[13:15:05] the data by the customer ID. So like
[13:15:08] this. Let's go and execute it. Now as
[13:15:11] you can see in the output we have four
[13:15:12] customers and we have the total sales
[13:15:14] for each customer. And with that we have
[13:15:16] solved the first step. We have the total
[13:15:18] amount of sales for each customer and we
[13:15:20] have now prepared the data for the next
[13:15:23] query in order to rank the customers. So
[13:15:26] now I think you already getting how
[13:15:28] important are the subqueries in order to
[13:15:30] do stepby-step analyszis. So this is our
[13:15:33] subquery. Now we need the main query. So
[13:15:37] I will start preparing it. So main query
[13:15:39] like this. And let's go first and select
[13:15:42] everything. So select star from let me
[13:15:46] just make this a little bit bigger like
[13:15:48] this. And now we have to go and convert
[13:15:50] this query to a subquery. So we need the
[13:15:52] parenthesis. So the starting and the
[13:15:55] ending and for the SQL server I'm going
[13:15:57] to give it an alias and I would like to
[13:15:59] push everything to the right side. So
[13:16:02] let's go and execute it. Perfect. So it
[13:16:04] is working with that the subquery is
[13:16:07] passing the data in the from clause to
[13:16:09] the main query. Now of course the main
[13:16:11] query is now is useless. It's just like
[13:16:13] selecting the data. We have to go and
[13:16:15] calculate the rank and for that we have
[13:16:17] a very nice window function. So we're
[13:16:20] going to go and use the rank. So it
[13:16:22] doesn't need any parameters over we have
[13:16:25] to sort the data order by. So we have to
[13:16:28] go and sort the data by the total sales
[13:16:30] descending from the highest to the
[13:16:32] lowest. So we're going to go with the
[13:16:34] total sales and descending. So now as
[13:16:37] you can see we are using the total sales
[13:16:39] that we have already prepared in the
[13:16:41] subquery. So without preparing first the
[13:16:43] data we will not be able to rank the
[13:16:46] customers in the main query. So that's
[13:16:48] it. Let's go and execute it. And with
[13:16:50] that SQL sorted our data and we have a
[13:16:52] nice ranking based on the data that we
[13:16:55] had from the subquery. So this is the
[13:16:58] highest customer with the sales and then
[13:17:00] the customer number one and so on. So
[13:17:02] again in this task we have like multiple
[13:17:04] steps and we use the power of the
[13:17:06] subqueries in order to do it step by
[13:17:08] step. So that's all on how to use the
[13:17:10] subquery inside the from close. Okay. So
[13:17:13] now let's see quickly how SQL executed
[13:17:15] our query. So we have here our query and
[13:17:17] we are quering the table orders. So the
[13:17:19] first step is that SQL going to go and
[13:17:21] identify the subquery and then it going
[13:17:24] to go and execute it. So SQL going to go
[13:17:26] and execute the subquery part where we
[13:17:28] are aggregating the data based on the
[13:17:30] customer ID. So once the subquery is
[13:17:32] executed the next step is that the
[13:17:35] result going to be introduced as an
[13:17:37] intermediate results. So these results
[13:17:39] we will not see it in the output. It's
[13:17:41] going to be like temporarily saved in
[13:17:43] the memory. So now the next step is that
[13:17:46] SQL going to go to the main query and
[13:17:48] it's going to execute it based on the
[13:17:50] intermediate results. So that means the
[13:17:52] main query will not go back to the
[13:17:54] original table. It's going to go and
[13:17:56] query the intermediate results. So here
[13:17:58] what SQL going to do going to go and
[13:18:00] rank the intermediate results by
[13:18:02] introducing a new column where we see
[13:18:04] the ranks 1 2 3 4 and the output of the
[13:18:06] main query going to be the final
[13:18:08] results. So as you can see it's very
[13:18:09] simple. First SQL is executing the
[13:18:11] subquery and the result of the subquery
[13:18:14] going to be used in the main query and
[13:18:16] once the main query is executed we will
[13:18:19] get the final results. So the subquery
[13:18:21] here is only supporting the main query.
[13:18:24] So those are the steps that SQL uses in
[13:18:26] order to execute the
[13:18:32] subqueries. So now let's understand how
[13:18:34] the database server execute the
[13:18:36] subqueries behind the scenes. Let's go.
[13:18:39] So now let's say that you are data
[13:18:40] analyst and you are writing a query at
[13:18:42] the client side where you have a
[13:18:44] subquery inside the main query. So once
[13:18:46] you go and execute it what's going to
[13:18:48] happen the database engine going to go
[13:18:50] and identify the subquery and in this
[13:18:53] situation the database going to execute
[13:18:55] first the subquery. So here subquery is
[13:18:57] like selecting and retrieving data from
[13:18:59] the table orders. So that means the
[13:19:01] database has to retrieve the data from
[13:19:04] the disk storage from the user data. So
[13:19:07] now once the subquery is executed the
[13:19:09] result the intermediate results going to
[13:19:11] be stored in the cache. So this means
[13:19:14] the result of the subquery is temporary
[13:19:16] and as well very fast to retrieve. And
[13:19:19] now once the database engine is done
[13:19:21] with the subquery it going to go and
[13:19:23] start executing the main query. So let's
[13:19:25] see in this scenario it's completely
[13:19:27] depending on the result of the subquery.
[13:19:29] So that means the main query going to go
[13:19:31] and interact with the cache storage. So
[13:19:34] this means now the data going to be
[13:19:35] retrieved very fast from the result of
[13:19:37] the subquery. Once it's done, it's going
[13:19:39] to forward the result to the database
[13:19:41] engine and the database engine going to
[13:19:43] forward the results to the client side.
[13:19:45] And at your side, you will find the
[13:19:47] final result. And of course, once
[13:19:49] everything is executed, the database
[13:19:51] engine going to go and clean up the
[13:19:53] cache. So the subquery results going to
[13:19:55] be destroyed and removed completely from
[13:19:57] the cache in order to have a free space
[13:19:59] for other queries. So this is how the
[13:20:01] database server execute the subqueries
[13:20:04] behind the
[13:20:08] scenes. All right. So now we're going to
[13:20:10] talk about how to use the subquery in
[13:20:12] the select clause. So now we typically
[13:20:14] use the subqueries in the select clause
[13:20:17] to aggregate the data side by side with
[13:20:19] the columns of the main query. Okay. So
[13:20:22] let's check the syntax of the subquery
[13:20:24] in the select clause. So we start with
[13:20:26] the simple stuff where we say okay let's
[13:20:28] go and select a column that we want to
[13:20:30] retrieve from specific table. So nothing
[13:20:33] new we are just quering a table. And now
[13:20:35] what we can do in this query is that not
[13:20:37] only we can go and select the columns
[13:20:39] from specific table we can go and insert
[13:20:42] here inside the select another query
[13:20:45] like a full query like select from and
[13:20:48] where. So again it's like query inside
[13:20:50] another query and we call this of course
[13:20:52] a subquery. In order to tell SQL this is
[13:20:55] a subquery we go and add the
[13:20:57] parenthesis. So with SQL going to
[13:20:59] understand huh this is a subquery and
[13:21:01] the result of this query going to be
[13:21:03] used in the select. So we can handle it
[13:21:06] like any other column. We can go and
[13:21:07] give it like an alias. It is here
[13:21:09] optional and not m to add an alias. So
[13:21:12] this inner query we call it a subquery
[13:21:14] and the outer query going to be the main
[13:21:16] query. So this is how you put a subquery
[13:21:18] in the select clause. But there is one
[13:21:21] rule for this query that the result of
[13:21:24] this subquery must be a scalar query.
[13:21:27] That means the result must be a single
[13:21:29] value because otherwise it will not
[13:21:31] work. SQL here is expecting only one
[13:21:34] value. So this is how we use the
[13:21:36] subquery inside the select clause. All
[13:21:38] right, let's have the following task and
[13:21:39] it says show the product ids, product
[13:21:42] names, prices and the total number of
[13:21:45] orders. So now if we check the task
[13:21:47] there is like two parts. The first part
[13:21:49] is that we are showing the details about
[13:21:51] the products and the second part that we
[13:21:53] have to go and calculate the total
[13:21:55] number of orders. So let's see what
[13:21:56] we're going to do. First let's go and
[13:21:58] solve this simple part here where we
[13:22:00] have the product ID, product names and
[13:22:02] prices. So we're going to go and select
[13:22:05] the product ID and the product and then
[13:22:08] the price from the table sales products.
[13:22:13] Let's go and execute it. So with that we
[13:22:15] have solved the first part of the task.
[13:22:17] We have the details about the products.
[13:22:20] Now we go and solve the second part. We
[13:22:22] have to go and calculate the total
[13:22:23] number of orders. Now this information
[13:22:25] come from different table from the
[13:22:27] products. We cannot calculate it from
[13:22:29] products. We have to go and query the
[13:22:30] orders. So now what am I going to do?
[13:22:32] I'm going to go and calculate this part
[13:22:34] in separate query. Instead of having it
[13:22:37] here inside the products. So let's have
[13:22:39] a semicolon in order to have a second
[13:22:42] query. So we're going to go and select
[13:22:44] the total number of orders. That means
[13:22:46] we can go simply do account star from
[13:22:49] the table sales orders. Let me just make
[13:22:53] it a little bit bigger. So we're going
[13:22:55] to call it total orders and a semicolon
[13:22:59] as well. So now if you just execute the
[13:23:01] whole thing, you will get here like two
[13:23:03] parts in the results. First you have the
[13:23:05] details of the products and the second
[13:23:07] part we have now the total number of
[13:23:09] orders. We have 10 orders. But now with
[13:23:12] that we have like two different queries
[13:23:14] like separated from each others and we
[13:23:16] have two different results. But in the
[13:23:18] task we have to show all those
[13:23:19] informations in one result. So now what
[13:23:22] we can do we can put one query inside
[13:23:24] another query. So now if you check the
[13:23:26] second query the total orders you can
[13:23:28] see we have only single value. So we
[13:23:31] have a scalar query scalar subquery.
[13:23:34] That's why we can go with this as a
[13:23:36] [Music]
[13:23:37] subquery like this. And I'm going to go
[13:23:40] and put everything in one line in order
[13:23:42] to see it. So let's remove the
[13:23:44] semicolons. We don't need it. And now
[13:23:46] what we're going to do, we're going to
[13:23:47] go and take the whole thing and put it
[13:23:50] inside the main query. So this is the
[13:23:54] main query. And now think about it as
[13:23:57] new column. So I will put the query
[13:24:00] here. So it is just one new column in
[13:24:02] our select. But in order to have it as a
[13:24:05] subquery, we have to use the parenthesis
[13:24:08] at the start and at the end. And of
[13:24:10] course, we have to go and give it a
[13:24:11] name. So I'm going to go and use the
[13:24:13] same name over here. So it's going to be
[13:24:16] as total orders. So with that, the setup
[13:24:19] for the subquery is ready and it is
[13:24:21] inside the select clause in the main
[13:24:25] query. Let's go and execute it. Now, as
[13:24:27] you can see, we have everything
[13:24:28] together. We have the three informations
[13:24:30] the product details and as well side by
[13:24:33] side with the total orders and since it
[13:24:35] is always the same value it going to go
[13:24:37] and be repeated for each row. So this is
[13:24:39] what we call scalar sub query inside the
[13:24:42] select clause and here again very
[13:24:44] important to understand if you are using
[13:24:46] a subquery inside the select clause only
[13:24:50] the scalar subquery is allowed. So for
[13:24:53] example instead of having one value from
[13:24:54] the aggregation we can go and use the
[13:24:57] order ID. So let's see what going to
[13:25:00] happen. We will get an error. It going
[13:25:02] to says subquery is returning more than
[13:25:04] one value and this is not allowed
[13:25:06] because we are using the subquery in the
[13:25:08] select clause. So that's why we have to
[13:25:11] have only one value and by using the
[13:25:13] aggregation you will get one value. So
[13:25:15] let's repair it. And it's working. And
[13:25:17] now again if you would like only to see
[13:25:19] the results from the subquery what you
[13:25:21] can do you can go and highlight the
[13:25:23] subquery like this without the
[13:25:25] parenthesis of course and you go and
[13:25:27] execute it and with that you can see in
[13:25:29] the output the 10 this is the
[13:25:31] intermediate results that's going to be
[13:25:33] passed to the main query and if you want
[13:25:35] the whole thing to be executed just like
[13:25:38] unmark it and execute and with that
[13:25:40] everything can be executed the subquery
[13:25:42] and the main query. So this is the
[13:25:43] scalar subquery in the select clause.
[13:25:46] Okay, so now let's see quickly how SQL
[13:25:48] executed this query step by step. So
[13:25:50] this is our original query and we need
[13:25:52] two tables from our database for it. So
[13:25:54] the first step is that SQL going to go
[13:25:56] and identify the subquery and it's going
[13:25:59] to go and execute it. So this is the
[13:26:01] first step. So the query is targeting
[13:26:03] the orders table and we are just simply
[13:26:05] doing a count. So in the output we will
[13:26:08] get an intermediate results where we are
[13:26:10] counting the number of rows of the
[13:26:12] orders. Now the next step is that SQ is
[13:26:14] going to go and pass this value to the
[13:26:17] main query. So this is the second step
[13:26:19] and if you go and pass this value to the
[13:26:21] main query, it's going to look like
[13:26:23] this. So you are saying product ID,
[13:26:25] products and the tin. So after SQL
[13:26:27] prepared the main query, SQL going to go
[13:26:30] and execute it. So this time we are
[13:26:32] targeting the products and in the output
[13:26:34] we will get all the informations from
[13:26:36] the products without any filter because
[13:26:37] here we don't have any work clouds and
[13:26:39] the final results we will get it like
[13:26:42] this. So we will have the product ID,
[13:26:43] the product and the total that we got it
[13:26:46] from the subquery. So as you can see
[13:26:47] here the subquery here is a scalar
[13:26:49] subquery where we have only one single
[13:26:52] value. So again it's very simple always
[13:26:54] SQL starts with the subquery and then
[13:26:57] it's going to go and pass the values to
[13:26:59] the main query and at the end the main
[13:27:01] query going to be executed and we will
[13:27:03] get the final result from it. So this is
[13:27:05] how SQL executed our query.
[13:27:12] All right, next we're going to talk
[13:27:13] about how to use the subquery in the
[13:27:16] join clause. All right, so now as we are
[13:27:18] joining tables in SQL, sometimes we have
[13:27:20] to go and prepare the data before doing
[13:27:23] the join to dynamically create a result
[13:27:25] sets for joining with another table. So
[13:27:28] again here we cannot join tables
[13:27:30] directly. We have to do a preparation
[13:27:32] step before doing the joins. Okay, let's
[13:27:34] have the following task and it says show
[13:27:37] all customer details and find the total
[13:27:40] orders of each customer. Now, of course,
[13:27:42] in SQL, you don't have only one
[13:27:43] solution, you have multiple solutions.
[13:27:45] But I would like to solve this task
[13:27:47] using the subquery. So, now if you check
[13:27:49] the task, we have like two parts. The
[13:27:51] first part we have to show all the
[13:27:53] customer details. And the second part,
[13:27:54] we have like here an aggregation find
[13:27:57] the total orders of each customer. So,
[13:27:59] now let's solve those different parts
[13:28:01] using two different queries. Let's start
[13:28:03] with the easiest one. Show all customer
[13:28:05] details. So I think this is very simple.
[13:28:07] So select star from sales customers. So
[13:28:13] let's go and execute it. So in the
[13:28:15] output we have all the details about the
[13:28:16] customers and we have solved the first
[13:28:18] part. Very simple. Now let's go and
[13:28:20] solve the second part. We have defined
[13:28:23] the total number of orders of each
[13:28:25] customer. That means let me just have a
[13:28:28] semicolon over here. We have to go to
[13:28:30] the table orders. So let's go and select
[13:28:33] first the order ID, customer
[13:28:36] ID from the table sales orders like
[13:28:40] this. So I will just highlight the
[13:28:42] second query and execute it. Now in the
[13:28:44] output we have 10 orders and we have the
[13:28:46] different customers. Now in order to
[13:28:48] find the total orders for each customer
[13:28:49] we have to go and use the group pie. In
[13:28:51] order to do that it's very simple. We're
[13:28:54] going to go over here and say so count
[13:28:57] let's go with the star and then we're
[13:28:59] going to go and group up the data by the
[13:29:01] customer ID. I will go and call this
[13:29:04] total orders. So let's go and execute
[13:29:08] only these parts and with that we have
[13:29:10] four customers and we have the total
[13:29:12] number of orders. So with that we have
[13:29:14] solved the second part of the task. So
[13:29:17] now what I'm going to do, I'm going to
[13:29:18] go and execute both of those queries
[13:29:20] using the semicolon separately like
[13:29:23] this. I will just make this a little bit
[13:29:25] bigger. So let's go and execute it. Now
[13:29:27] in the output we have the two results,
[13:29:29] all details about the customers and the
[13:29:32] total number of orders for each
[13:29:34] customer. So now what we want to do is
[13:29:35] to go and combine those two results in
[13:29:38] one. And in order to do that we can use
[13:29:41] the joins. So now we have to think about
[13:29:43] what is the first query, what is the
[13:29:45] second query. Since the first query
[13:29:47] returns all the customers that we have
[13:29:49] in the database, I would like to have
[13:29:51] this as the left table and since in the
[13:29:54] second query we have only four
[13:29:55] customers, I would like to have it then
[13:29:57] as the right table and I will go with
[13:30:00] the left join so that I don't miss any
[13:30:03] customer because if I do the inner join,
[13:30:05] I will lose the customer number five. So
[13:30:07] let's go and do that. So this is the
[13:30:09] first query in the main query. So I'm
[13:30:11] going to call this main query.
[13:30:15] And now I'm going to give this as well
[13:30:17] an alias like the C. And now we're going
[13:30:19] to go and join this table from the
[13:30:22] database together with the results the
[13:30:24] output of this query. So that means
[13:30:27] we're going to do it like this. Left
[13:30:29] join and now we're going to join with a
[13:30:32] sub query. So we will have our
[13:30:34] parenthesis. I will just put here few
[13:30:36] spaces so that it's clear it is a
[13:30:39] subquery and we need for this an alias.
[13:30:41] So let's go and say for example the O.
[13:30:44] So with that we are joining a table with
[13:30:46] the result of a sub query. And now of
[13:30:49] course what is missing is joining the
[13:30:51] tables using a key. Now if you check the
[13:30:53] two results you can see in both queries
[13:30:55] we have the customer ID. That's why
[13:30:58] we're going to join with the customer
[13:31:00] ID. So on then the customer
[13:31:04] ID with the customer ID from the sub
[13:31:08] query like this. So we have everything
[13:31:12] and let's go and execute it. Now as you
[13:31:14] can see in the output we have all the
[13:31:16] details about the customer and as well
[13:31:18] together with the total number of orders
[13:31:21] for each customer together with the
[13:31:23] total number of orders for each customer
[13:31:25] and as you can see we didn't miss any
[13:31:27] customer. So we have all the customers
[13:31:29] from the database and we can see that
[13:31:31] Anna doesn't have any orders. Now you
[13:31:34] might say you know what we have here the
[13:31:35] customer ID twice. So what I'm going to
[13:31:37] do I will select all the columns from
[13:31:40] the customers but from the subquery I'm
[13:31:43] interested only on the total orders. So
[13:31:47] like this let's go and execute it. Let's
[13:31:49] make this a little bit smaller. So now
[13:31:51] the results are really clean. We have
[13:31:53] all details from the customers and as
[13:31:55] well the total orders of each customer.
[13:31:58] And of course as we learned if you would
[13:31:59] like to check the results from only the
[13:32:01] subquery you go and highlight it and
[13:32:03] execute it. So as you can see you can
[13:32:05] put the subqueries almost everywhere and
[13:32:07] this is how we use subqueries inside
[13:32:13] joins. Okay. So now we're going to focus
[13:32:15] on how to use the subquery in the wear
[13:32:18] clause. So now in this scale as we
[13:32:20] learned we can go and filter the tables
[13:32:22] using the wear clause by using like
[13:32:24] static values. But now in real data
[13:32:27] projects we're going to go and filter
[13:32:28] the data based on like complex logic. So
[13:32:31] now in order to prepare this complex
[13:32:33] logic we go and use the sub queries in
[13:32:36] order to make like dynamic filtering for
[13:32:39] our main tables. And now in order to
[13:32:41] filter data using the wear clause we
[13:32:43] have to go and use operators and we can
[13:32:45] split it into like two groups. We have
[13:32:47] the comparison operators and another
[13:32:49] sets we can call it logical operators or
[13:32:52] sometime we call it subqueries
[13:32:53] operators. So now first we're going to
[13:32:55] talk about the comparison operators. So
[13:32:57] there are operators that we can use in
[13:32:59] order to compare two values in order to
[13:33:01] help us filtering the data based on
[13:33:04] specific condition. And now in SQL
[13:33:06] basics we have learned that we have
[13:33:07] different comparison operators and they
[13:33:09] are very simple. So in order to compare
[13:33:11] two values we have operator like the
[13:33:13] equal we have as well not equal the
[13:33:15] opposite. So we have greater than less
[13:33:18] than and as well we have greater than or
[13:33:21] equal to and the last one we have less
[13:33:23] than or equal to. So they are very
[13:33:25] simple. Now instead of comparing two
[13:33:27] values, we're going to go and compare a
[13:33:29] value with the result of subquery using
[13:33:32] the comparison operators. All right,
[13:33:34] let's check the syntax of the subquery
[13:33:36] inside the wear clause using the
[13:33:38] comparison operators. So we start with
[13:33:40] the standard stuff where we say select
[13:33:42] few columns that we want to retrieve and
[13:33:44] we want to get the data directly from
[13:33:46] specific table in our database and now
[13:33:48] we come to the where condition where we
[13:33:50] want to filter the table. So we say
[13:33:52] where and then we select specific column
[13:33:55] from the table one. Now since we are
[13:33:57] talking about the comparison operators
[13:33:59] we can go with operator for example
[13:34:00] equal and usually we go and specify here
[13:34:03] like static value like a number or
[13:34:05] string but instead of having a static
[13:34:07] value what we can do we can get the
[13:34:10] value from another select statements
[13:34:12] another query like here for saying
[13:34:14] select a column from table two and with
[13:34:17] a filter. So now whatever comes from
[13:34:19] this subquery going to be used in order
[13:34:22] to filter the table number one. And of
[13:34:24] course we are telling SQL this is a
[13:34:26] subquery by defining the parenthesis at
[13:34:28] the start and at the ends and the outer
[13:34:30] query going to be the main query. So as
[13:34:32] you can see we are using the subquery in
[13:34:35] order to filter the main query. And here
[13:34:37] in SQL if you're using subquery with the
[13:34:39] comparison operators we have a rule the
[13:34:42] subquery must be a scalar subquery. So
[13:34:45] only one single value. So that's all
[13:34:47] about how to use the subquery in the
[13:34:49] wear clause using the comparison
[13:34:51] operators. All right. So now we have
[13:34:53] again the same task and it says find the
[13:34:56] products that have a price higher than
[13:34:58] the average price of all products. We
[13:35:01] have solved this task already using the
[13:35:02] subquery inside the from clause. But now
[13:35:05] we're going to go and solve it again
[13:35:07] using the subquery but this time inside
[13:35:09] the wear clause. So let's do it step by
[13:35:11] step. Let's go and get the informations
[13:35:14] that we need. So we need the product ID,
[13:35:17] we need the price from the table sales
[13:35:21] products. So let's go and execute it. So
[13:35:23] now we got the list of all products. But
[13:35:26] we have to go and filter those
[13:35:28] informations using the column price. So
[13:35:31] with that in the result, we got all the
[13:35:32] products, but we don't need all the
[13:35:34] products. We need only the products
[13:35:36] where the price is higher than the
[13:35:38] average. That means we have to go and
[13:35:40] filter the table based on the values of
[13:35:42] the price. So now in order to do that
[13:35:44] what we're going to do we're going to
[13:35:45] use the wear clause and we have to go
[13:35:48] and filter the data based on the price
[13:35:50] and since we need higher than we're
[13:35:52] going to go and use the compressor
[13:35:53] operator higher than now next we need
[13:35:55] the value average price. So how we going
[13:35:58] to do it? We don't have the average
[13:36:00] price like out of the box in the table
[13:36:02] products. We have to go and calculate
[13:36:04] it. That's why we're going to go and
[13:36:06] write another query where we're going to
[13:36:08] go and find the average
[13:36:11] price from the table sales products like
[13:36:16] this. So now let's go and highlight it
[13:36:18] and then execute it. And with that we
[13:36:21] got now the average price of our
[13:36:23] products. And as you can see in the
[13:36:24] output we have only one single value. So
[13:36:27] this is a scalar query. So now what we
[13:36:30] need? We need this value in order to be
[13:36:32] used in order to filter the first query.
[13:36:35] So that's why the first query is the
[13:36:38] main query bigger. The second one is the
[13:36:41] subquery that going to support the main
[13:36:44] query in order to filter the data. So
[13:36:46] now what we're going to do, we're going
[13:36:48] to take the subquery and use it in the
[13:36:51] wear clause. And now of course we have
[13:36:52] to tell SQL this is a subquery. That's
[13:36:54] why we have to put it inside two
[13:36:56] parenthesis. So with that we have the
[13:36:58] sub query inside the wear clause in
[13:37:01] order to filter the main query. So let's
[13:37:03] go and execute it. And now as you can
[13:37:05] see in the output we have now only two
[13:37:07] products where the price is higher than
[13:37:10] the average price. So with that we have
[13:37:12] solved the task but this time using the
[13:37:14] subquery in the wear clouds in order to
[13:37:17] filter the main query. And of course in
[13:37:18] order to see this value in our select
[13:37:21] since it is scalar sub query we can as
[13:37:23] well go over here and put it in our
[13:37:26] select just in order to see the value.
[13:37:28] So average price. So let's go and
[13:37:31] execute it. And with that we can see as
[13:37:32] well in our results the average price.
[13:37:35] So this is how we use the subquery in
[13:37:36] the workcloud using the comparison
[13:37:38] operator. Okay. So let's see quickly how
[13:37:40] is going to execute our query step by
[13:37:42] step. So as usual first is going to go
[13:37:44] and identify the subquery. It's going to
[13:37:47] be our select average price and so on.
[13:37:49] And now the next step SQL going to go
[13:37:51] and execute our sub query. So it is
[13:37:53] based on the products and since we are
[13:37:55] doing aggregations without group by at
[13:37:57] the output we will get only one value.
[13:38:00] So the average going to be 20. This
[13:38:02] value is start intermediately in the
[13:38:04] memory. So we will not see it in the
[13:38:05] output. SQL going to go and pass this
[13:38:08] value to the main query. So the main
[13:38:10] query going to look like this. We are
[13:38:12] selecting few columns from the table and
[13:38:15] we are filtering the data based on the
[13:38:16] price that is higher than the value 20
[13:38:20] that we got it from the subquery. So now
[13:38:22] once SQL have everything for the main
[13:38:24] query SQL going to go and execute it. So
[13:38:26] SQL going to go to the products and only
[13:38:28] select the products where the price is
[13:38:31] higher than 20. So it's only those two
[13:38:33] rows and in the output we will get the
[13:38:35] final results the two products as well.
[13:38:38] So product ID and product price. So
[13:38:40] that's it. It's very simple. This is how
[13:38:41] SQL executed our query. So as usual
[13:38:43] first starting with the subquery passing
[13:38:46] the value to the main query and at the
[13:38:48] end so the main query going to be
[13:38:49] executed with the informations from the
[13:38:51] subquery and we will get at the end the
[13:38:53] final results. So that's
[13:38:55] [Music]
[13:38:58] it. All right. So now we're going to
[13:39:00] talk about the second group of operators
[13:39:02] and we're going to start with the in
[13:39:04] operator. So what is in operator? As we
[13:39:07] learned before in the comparison
[13:39:08] operators, we can go and filter the data
[13:39:10] based on only one single value. But now
[13:39:13] in some scenarios, we have to go and
[13:39:14] filter the data based on multiple
[13:39:17] values, not only one. In this case, we
[13:39:19] can go and use the n operator. So if you
[13:39:21] go and use the n operator, it's going to
[13:39:23] go and check whether the value matches
[13:39:25] any value from a list. So a list of
[13:39:28] multiple values. If it matches any of
[13:39:30] them, so we will get a true. Okay. Okay.
[13:39:32] So now let's have a quick look to the
[13:39:34] syntax of the sub query using the in
[13:39:37] operator. So we start with the classic
[13:39:39] stuff where we say okay we would like to
[13:39:40] retrieve the column one column two from
[13:39:43] the table one and we want to filter the
[13:39:45] data based on the column from the table
[13:39:48] one. Now after specifying the column
[13:39:50] we're going to use the in operator and
[13:39:52] after that we can go and specify static
[13:39:54] values but since we are talking about
[13:39:56] the subqueries the values going to come
[13:39:58] from another query. So here we have
[13:40:01] another select statements from table two
[13:40:03] and we filter the data for this query.
[13:40:06] And now the result of this subquery
[13:40:08] going to be used in order to filter the
[13:40:10] data using the in operator. And now the
[13:40:13] big difference between the in operator
[13:40:15] and the comparison operators that the
[13:40:17] subquery is allowed to have multiple
[13:40:20] rows. So there is no rule about having
[13:40:22] like one single value scalar subquery.
[13:40:25] We can have in the result a list of
[13:40:28] multiple values. So this is the syntax
[13:40:30] of the subquery using the in operator.
[13:40:33] All right, let's practice using this
[13:40:35] task. It says show the details of orders
[13:40:38] made by customers in Germany. So let's
[13:40:40] see how we can solve this task. First it
[13:40:42] needs the details of orders. So as we
[13:40:46] know we have the
[13:40:47] table sales orders. So let's go and
[13:40:50] execute it. So in the output we have all
[13:40:53] orders and with all details. But for the
[13:40:55] task we don't need all the orders. We
[13:40:57] need only the orders that made by
[13:40:59] customers from Germany. So now if you
[13:41:01] check the table orders, you don't find
[13:41:03] any informations about the countries,
[13:41:05] right? So we have to go and get it from
[13:41:07] another table. And as we know, we can
[13:41:09] find these informations in the table
[13:41:11] customers. So let's build another query.
[13:41:14] So let's say select star from sales
[13:41:19] customers like this. So let's go and
[13:41:22] execute only the second query like this.
[13:41:25] Now, as you can see in the customers, we
[13:41:27] have the country column, and this is
[13:41:29] exactly what we need. So, now let's make
[13:41:30] a list of all customers from Germany.
[13:41:33] So, we don't need all customers. We need
[13:41:35] only the one that come from Germany.
[13:41:36] That's why we're going to go and use the
[13:41:38] work clause and we say country equal to
[13:41:41] the value Germany like this. So, let's
[13:41:45] go and execute it again and check the
[13:41:48] results. Now, in the output, we have our
[13:41:50] German customers number one and number
[13:41:52] four. So now we're going to go and use
[13:41:54] this information in order to filter the
[13:41:56] table orders. So let's go back to the
[13:41:58] table orders over here. And here we have
[13:42:00] the customer ID informations. And as we
[13:42:02] can see we need the orders where the
[13:42:04] customer is either one or four. Now in
[13:42:07] order to filter that we're going to go
[13:42:09] to the first query and use the work
[13:42:11] clause like this and say the customer
[13:42:15] ID. So now since we have like two values
[13:42:17] one on four we can go and use the
[13:42:20] operator in. So let's go and use the in
[13:42:22] and let's go and build the list. So
[13:42:25] let's go and have the one and four. So
[13:42:28] let's go and execute it. Now we can see
[13:42:30] the results. We have the orders but only
[13:42:33] from the customers one and four. So with
[13:42:36] that we have solved the task. We have
[13:42:37] the details of orders made by customers
[13:42:39] in Germany. Right? And now of course
[13:42:41] this is really bad solution because what
[13:42:43] about if we get like in the future new
[13:42:45] customer you don't want to go and keep
[13:42:48] adding here like values and so on for
[13:42:50] each time you have a new customer. We
[13:42:52] want to make the values for this list to
[13:42:55] be dynamic. So we don't need a static
[13:42:57] value we need like dynamic values and we
[13:43:00] can use the subqueries in order to
[13:43:02] retrieve those informations. Right? And
[13:43:04] we have it already in the second query.
[13:43:06] So let's go back to the second query
[13:43:08] over here. We need only those two values
[13:43:10] one and four. That's why we're going to
[13:43:12] go to the query and say okay let's
[13:43:15] retrieve the customer ID. So let's go
[13:43:18] and execute it again. And with that we
[13:43:20] have with a one and four exactly like we
[13:43:22] have it here in the first query. And of
[13:43:24] course in the future if there's like
[13:43:25] another customer that come from Germany
[13:43:27] this list going to be little bit longer.
[13:43:29] So this query going to always retrieve
[13:43:31] all the customer ids that have the
[13:43:34] country equal to Germany. So now what
[13:43:36] we're going to do, we're going to take
[13:43:38] this as a sub query. Let's go and get
[13:43:41] everything from it and now put it
[13:43:43] instead of those static values. So of
[13:43:45] course we're going to go now and put few
[13:43:47] spaces to the right side in order to
[13:43:49] understand this is subquery and of
[13:43:52] course here we don't use any aliases. So
[13:43:54] now what we are doing the results from
[13:43:56] this subquery going to be used in order
[13:43:59] to filter our main query. So let me just
[13:44:02] call it main
[13:44:04] query like this and make this smaller.
[13:44:08] So let's go and execute it. And now we
[13:44:10] are getting the same results. We are
[13:44:12] getting all the orders from only the
[13:44:14] customers one and four where they come
[13:44:16] from Germany. And this informations come
[13:44:18] dynamically from the subquery and we
[13:44:21] don't have to worry about new customers
[13:44:22] from Germany. It's going to be added
[13:44:24] here automatically. And this query going
[13:44:26] to always return all the orders from
[13:44:28] Germany. So this is the power of the
[13:44:30] subquery together with the in operator
[13:44:32] if you are having like multiple values
[13:44:34] multiple rows. So we have solved the
[13:44:36] task. All right. Now one more thing.
[13:44:38] Let's say that the task is exactly the
[13:44:39] opposite. It says show the details of
[13:44:42] orders made by customers who don't come
[13:44:45] from Germany. So now here there's like
[13:44:47] two ways in order to do it. Either you
[13:44:49] go to the subquery and you say you know
[13:44:51] what the country should not be equal to
[13:44:54] Germany. So if you go and execute it,
[13:44:57] you will get all the customers ids that
[13:44:59] are not from Germany. And if you execute
[13:45:01] the whole thing, you will get all the
[13:45:03] orders where the customers are not from
[13:45:04] Germany. So either you do that or you
[13:45:07] stay with the equal to Germany, but you
[13:45:09] go and convert the whole logic by using
[13:45:12] the operator not. So now we are saying
[13:45:14] the customer ID should not be equal to
[13:45:17] one of those values. So it should not be
[13:45:20] equal to one or four. And for that we
[13:45:22] are using the notin operator. So let's
[13:45:25] go and execute it. So now with that we
[13:45:27] are getting all the orders where the
[13:45:29] customers don't come from Germany by
[13:45:31] just using notin operator. So that's all
[13:45:34] about the notin and the in operators.
[13:45:37] All right. So now let's see step by step
[13:45:38] how is execute our query. So we are
[13:45:41] targeting two tables the customers and
[13:45:43] the orders. So the first step is that
[13:45:44] SQL going to go and identify the
[13:45:46] subquery and it's going to go and
[13:45:48] execute it. So the subquery here is
[13:45:50] filtering the data based on the country.
[13:45:53] So the query going to be executed and in
[13:45:55] the output we will get only two rows. So
[13:45:57] it is one column with multiple rows.
[13:45:59] This is the row subquery and this is our
[13:46:02] intermediate results where it's going to
[13:46:03] be passed to the main query. So our main
[13:46:06] query going to look like this. We are
[13:46:07] selecting few informations from the
[13:46:09] orders and we are filtering the table
[13:46:11] orders based on the customer ID where we
[13:46:14] are saying the customer ID must be one
[13:46:16] of those values one or four. So the
[13:46:18] subquery here is supporting the main
[13:46:21] query with the informations for the
[13:46:23] filter. Now once SQL have everything
[13:46:25] going to go and execute our main query
[13:46:28] and this going to be like the following.
[13:46:30] So we will start with the first row and
[13:46:32] here the customer ID is equal to two. So
[13:46:34] the value two is not equal to 1 or four.
[13:46:37] That's why this row will be excluded
[13:46:39] from the final results. Now let's move
[13:46:42] to the second row. We have here the
[13:46:43] value three and the value three is not
[13:46:46] equal to one of those values. That's why
[13:46:48] this value going to be as well failing.
[13:46:51] So we will not have it at the output.
[13:46:53] And then it's still going to go to the
[13:46:54] next one. Now this time the customer ID
[13:46:56] is one and it is equal to one of those
[13:46:59] values. It's equal to one. So we have a
[13:47:01] match. That's why this row will be
[13:47:03] included to the results. And the same
[13:47:05] thing for the next row because we have
[13:47:07] the customer ID one and so on. Now after
[13:47:10] SQL checking all those customer ids
[13:47:12] whether they are in the list one or four
[13:47:14] we will get the final results where we
[13:47:16] have here all the orders where the
[13:47:18] customer ID either one or four. So this
[13:47:21] is how SQL executed the in operator
[13:47:24] using the
[13:47:28] subqueries. Okay. So now moving on to
[13:47:30] the any operator. So we can go and use
[13:47:32] the any operator in order to compare a
[13:47:34] value if it matches any value from a
[13:47:38] list. So that means we can go and use it
[13:47:40] in order to check whether a condition is
[13:47:42] true for at least one of the values in a
[13:47:45] list. Okay. So now let's check quickly
[13:47:47] the syntax of the subquery using the any
[13:47:49] and all operators. So as we learned
[13:47:51] before we can go and use a subquery
[13:47:53] inside the wear clause in order to
[13:47:55] filter the main query using like the
[13:47:57] comparison operators like here less
[13:47:59] than. Now the syntax of the any operator
[13:48:02] is that you're going to go and use the
[13:48:04] comparison operator and after that
[13:48:07] immediately you use the keyword any. And
[13:48:09] for the all operator going to be exactly
[13:48:11] the same where you're going to go and
[13:48:13] put after the comparison operator the
[13:48:15] keyword all. So the syntax is very
[13:48:17] simple. We just add those keywords. So
[13:48:20] let's practice using the following task.
[13:48:22] Find female employees whose salaries are
[13:48:25] greater than the salaries of any male
[13:48:28] employee. So that means we want to go
[13:48:30] and compare the salaries between the
[13:48:31] male and female and specifically we are
[13:48:34] searching for female employees whose
[13:48:36] salary is greater than at least one male
[13:48:39] employee. So let's solve it step by
[13:48:41] step. Let's go and start selecting few
[13:48:43] informations like for example the
[13:48:45] employee
[13:48:46] ID and first
[13:48:49] name, gender, salary from the table
[13:48:53] sales employees. So let's go and execute
[13:48:57] it. So now we have like five employees.
[13:48:59] Three of them are male and two are
[13:49:01] female. So now since we want to compare
[13:49:03] the data between male and female let's
[13:49:05] go and create two queries. The first one
[13:49:07] is filtering the data based on the
[13:49:10] gender. So the first one is for the
[13:49:12] female. So and we can go and remove this
[13:49:15] information over here. Let me just make
[13:49:17] this little bit smaller and zoom out.
[13:49:20] And the second query it's going to be
[13:49:23] the exact opposite. Let's go and get
[13:49:25] employee informations for the male. So
[13:49:28] let's go and execute it. Now the first
[13:49:30] results are the female employees and the
[13:49:32] second one are So now for the first
[13:49:34] result is for the female employees and
[13:49:36] the second one is for the male
[13:49:38] employees. So now what do we need in the
[13:49:40] output? We need the female employees.
[13:49:42] That means this is going to be our main
[13:49:46] query. So we are focusing on the female
[13:49:49] employees and we are using the male
[13:49:51] employees only as a filter and what we
[13:49:54] need we need only the salary
[13:49:55] informations that's why we can prepare
[13:49:58] it like this. I will just put everything
[13:50:01] in one line to make it clear. So this
[13:50:04] going to be our sub query. So now we're
[13:50:06] going to go and work with the main query
[13:50:08] where we're going to add one more filter
[13:50:10] where we're going to filter the data
[13:50:12] based on the salary. Right? So we're
[13:50:14] going to say if the salary is greater
[13:50:17] than and now we need the values from the
[13:50:20] subquery right so this is our subquery
[13:50:22] we're going to put it like this and
[13:50:24] don't forget about the parenthesis at
[13:50:26] the start and at the ends and I would
[13:50:28] like still to have those two uh queries
[13:50:31] so let's go ahead execute it and now we
[13:50:34] will get an error and that's because our
[13:50:36] sub query is returning multiple rows and
[13:50:39] this is not acceptable we are using the
[13:50:41] comparison operator and SQL expect from
[13:50:44] the subquery to have scalar subquery. So
[13:50:47] only one single value. But now in order
[13:50:49] to solve this issue, we can go and use
[13:50:52] the logical operators either all or any.
[13:50:54] So now since we are saying it's enough
[13:50:56] for the salary of the female employee to
[13:50:58] be higher than at least one male
[13:51:00] employee, we will go with the operator
[13:51:03] any. So let's go after the comparison
[13:51:06] operator and have the keyword any. And
[13:51:08] let's go and execute it again. And now
[13:51:10] as you can see in the output we got only
[13:51:12] one female employee where her salary is
[13:51:15] higher to one of those male employees.
[13:51:18] So let me just go and get the first name
[13:51:20] as well from the
[13:51:22] second query just to have it like this.
[13:51:24] So now if you go and compare the salary
[13:51:26] of Mary it is not higher than Michael
[13:51:29] but it is higher than Frank and Kevin.
[13:51:32] And since we are using the any operator
[13:51:34] it's enough for Mary to have salary
[13:51:36] higher to one of those values. In this
[13:51:38] case, it's higher than both Frank and
[13:51:40] Kevin. And the condition is fulfilled.
[13:51:42] That's why we are getting the marry. And
[13:51:44] the other female, let me just check. Do
[13:51:47] we have else? So, we have Carol is
[13:51:49] salary is less than all the salaries of
[13:51:51] the male employees. So, it must be at
[13:51:53] least higher than one of the male
[13:51:56] employees. So, with that, we have solved
[13:51:57] the task, right? All right. So, now we
[13:51:59] have another operator that is similar.
[13:52:01] We call it the all operator. We can go
[13:52:04] and use it in order to compare a value
[13:52:06] if it matches all values in a list. So
[13:52:09] that means we can go and use it if we
[13:52:11] need to check whether a condition is
[13:52:13] true every value in a list. I know that
[13:52:16] might sound a little bit complicated but
[13:52:18] don't worry about it. We can have
[13:52:19] examples. Now let's say that our task
[13:52:21] says find female employees whose salary
[13:52:24] are greater than the salaries of all
[13:52:28] male employees. So that means now the
[13:52:30] condition is more restrictive. Mary
[13:52:32] should now has a salary higher than
[13:52:35] every male employee. So it should be
[13:52:37] higher to all those values that we have
[13:52:40] from the male employees. And of course
[13:52:42] in this scenario it's not because we
[13:52:44] have Michael. Mary has less salaries
[13:52:47] than Michael. And this is a problem
[13:52:49] because Mary should has higher salary
[13:52:52] than everyone. So let's go and try it.
[13:52:54] If I go and write here all and let's go
[13:52:57] and execute it, you will see we will not
[13:52:59] find any results that fulfill this
[13:53:01] requirement. So we don't have any female
[13:53:03] employee who her salary is higher than
[13:53:06] all male employees and that's because we
[13:53:08] have a very small data sets. So this is
[13:53:10] how we use all and any operators in our
[13:53:13] subqueries in SQL. All right. So with
[13:53:15] that we have covered almost everything
[13:53:17] about how to use the subqueries in
[13:53:20] different locations and clauses. But we
[13:53:22] didn't talk about the exist operator and
[13:53:24] that's because I would like you to
[13:53:25] understand a very important concept in
[13:53:27] the subqueries where we have two
[13:53:29] different types of the subqueries based
[13:53:31] on the dependencies the non-correlated
[13:53:34] and correlated subqueries. And after
[13:53:36] that we're going to go back to the exist
[13:53:41] operator. All right friends. So now we
[13:53:44] come to the part where it is a little
[13:53:45] bit complicated about the subqueries.
[13:53:47] Now we're going to talk about the
[13:53:49] dependencies between the subquery and
[13:53:51] the main query. So far all the examples
[13:53:54] and the subqueries that we have learned
[13:53:56] where a noncorrelated subquery. A
[13:53:59] non-correlated subquery means a subquery
[13:54:02] that can run independently from the main
[13:54:05] query. So that means the subquery is
[13:54:07] like standalone query. But in the other
[13:54:09] hand we have the exact opposite type of
[13:54:12] the subquery. We have the correlated
[13:54:14] subquery. A correlated subquery is a
[13:54:17] subquery that relies on values from the
[13:54:20] main query for each row it processes. So
[13:54:24] that means the subquery here is
[13:54:26] completely depending on the main query.
[13:54:29] So I know this might be a little bit
[13:54:30] confusing. That's why we can have the
[13:54:32] following very simple sketch in order to
[13:54:34] exactly understand how this works. So as
[13:54:37] usual we have a database tables and now
[13:54:40] this time going to go and start
[13:54:42] executing the main query first. This is
[13:54:45] the first thing happens. So the main
[13:54:47] query going to go and query the database
[13:54:50] in order to get results and SQL going to
[13:54:53] process the results row by row. So now
[13:54:55] what going to happen? The main query
[13:54:57] going to go and pass the first row
[13:54:59] informations to the sub query. So now
[13:55:02] the subquery going to get the data from
[13:55:04] the main query. So SQL going to execute
[13:55:06] the subquery. So here the subquery going
[13:55:09] to return a value like for example one.
[13:55:11] So here it's very important to
[13:55:13] understand that now the SQL or the main
[13:55:15] query going to check is there a result
[13:55:18] from the sub query in this example yes
[13:55:20] we have a results. So here SQL is
[13:55:22] checking the output for the subquery for
[13:55:26] the first row. So if there is a result
[13:55:28] SQL going to go and return the row in
[13:55:31] the final result. So this is the whole
[13:55:34] iteration happened only for the first
[13:55:36] row. So we're going to process the whole
[13:55:38] thing again from the start for the
[13:55:40] second row. So the main query going to
[13:55:42] get the second row from the database and
[13:55:44] it going to pass it to the subquery.
[13:55:47] Once the subquery gets this new
[13:55:49] informations, SQL going to go and
[13:55:51] execute the subquery once again. So now
[13:55:54] let's say that after executing the
[13:55:56] subquery, there were no results. So the
[13:55:58] subquery is not returning anything after
[13:56:01] the execution. So now what can happen?
[13:56:04] SQL and the main query going to check
[13:56:06] okay there is no result from the sub
[13:56:08] query and this means this row should be
[13:56:11] excluded and not presented in the
[13:56:13] output. So we will not see this row at
[13:56:15] the output. So as you can see SQL is
[13:56:18] executing the subquery once again for
[13:56:21] the second row. So this will keep
[13:56:23] happening as long as we have row. For
[13:56:25] example, we have another row. The main
[13:56:27] query going to pass it to the subquery.
[13:56:29] The subquery going to be executed for
[13:56:31] the third time and the result of the
[13:56:33] subquery is going to be one. So the same
[13:56:35] thing going to happen. SQL going to
[13:56:37] check it. Okay, we have a value. So this
[13:56:39] row is allowed to be in the final
[13:56:41] results and so on. The cycle going to
[13:56:43] keep repeating for each row that's going
[13:56:46] to be retrieved from the main query and
[13:56:48] once we have processed all the rows, the
[13:56:51] final result going to be presented in
[13:56:53] the output. So what we have understood
[13:56:54] so far the correlated subqueries is
[13:56:57] always depending on the main query and
[13:57:00] the subquery going to be executed for
[13:57:03] each row that we're going to get from
[13:57:05] the main query. So in this example we
[13:57:07] have four rows and the subquery is
[13:57:09] executed four times. So this is how the
[13:57:12] correlated subquery works. It's a little
[13:57:14] bit more complicated than the
[13:57:16] non-correlated subquery. The
[13:57:18] non-correlated subqueries are really
[13:57:20] straightforward. So first the subquery
[13:57:22] going to go and execute the database
[13:57:25] only once and the output of the subquery
[13:57:27] going to be like an intermediate results
[13:57:30] that going to be used from the main
[13:57:32] query. So the main query going to go and
[13:57:34] query the intermediate results and in
[13:57:36] the output we're going to get the final
[13:57:38] results. So as you can see in the
[13:57:40] execution of the non-correlated subquery
[13:57:42] it is straightforward. There's no
[13:57:44] iterations everything going to be
[13:57:46] executed only once. So now if you
[13:57:48] compare them side by side you can see
[13:57:50] that with the non-correlated subquery it
[13:57:52] is completely independent from the main
[13:57:55] query. So that means the subquery going
[13:57:57] to be executed only once and after that
[13:58:00] SQL going to go and as well execute the
[13:58:02] main query only once using the result
[13:58:05] from the subquery. But on the left side
[13:58:07] the subquery is going to be executed
[13:58:09] multiple times and it is completely
[13:58:11] depending on the main query and there is
[13:58:14] like an iteration for each row that's
[13:58:16] going to be retrieved from the main
[13:58:18] query. So the process going to be
[13:58:20] cycling until all the rows are processed
[13:58:23] and this is exactly how the correlated
[13:58:25] and the non-correlated subqueries work
[13:58:28] in SQL. All right. So now let's have the
[13:58:30] following task and it says show all
[13:58:31] customer details and find the total
[13:58:34] orders of each customer. We have already
[13:58:36] solved this task and you know in scale
[13:58:38] we don't have only one query in order to
[13:58:40] solve something. We have multiple ways
[13:58:42] in order to do it. So we solved this
[13:58:44] task before using the subqueries and the
[13:58:47] joins. Now we're going to go and solve
[13:58:48] this task using subquery in the select
[13:58:51] clause and as well using the correlated
[13:58:54] subqueries. So again let's do it step by
[13:58:57] step. It's very simple. First we need
[13:58:59] all the customer details. So as we
[13:59:02] learned select star from sales
[13:59:05] customers. So if you execute it you will
[13:59:07] get all the details of all customers.
[13:59:09] Now we need to find the total number of
[13:59:12] orders of each customer. Now before we
[13:59:14] have solved this using a simple query
[13:59:16] where we have used the count function
[13:59:19] together with a group I but this time
[13:59:21] we're going to do it little bit
[13:59:22] different. So let's go and write query
[13:59:25] saying select count star from the table
[13:59:30] sales orders. So now let's go and
[13:59:34] execute it. With that we have the total
[13:59:36] number of orders. So let's go and take
[13:59:38] this sub query and use it in the select.
[13:59:42] So we are using it as a scalar subquery.
[13:59:45] So let's just put it over here. And this
[13:59:47] is the main query. And in order to make
[13:59:50] this as a subquery, what we're going to
[13:59:52] do, we're going to have the parenthesis
[13:59:54] and we're going to say the total sales.
[13:59:56] So now let's go and execute it. So now
[13:59:58] as you can see, we have here all the
[14:00:00] details about the customers and we have
[14:00:01] the total sales. But we have one issue.
[14:00:03] We don't need just the total order. We
[14:00:05] need the total orders for each customer.
[14:00:08] So each customer has different total
[14:00:10] orders. So we cannot have like the
[14:00:12] following setup. We cannot say group by
[14:00:15] customer ID. And then you have like here
[14:00:18] the customer ID and so on. So if you go
[14:00:21] and execute it, you will get a problem.
[14:00:23] And that's because if you go and execute
[14:00:25] this subquery over here, you will get
[14:00:27] like multiple rows and multiple columns.
[14:00:30] So you have like a table query. And this
[14:00:32] type of subquery is not allowed to be
[14:00:34] used in the select clause, right? We
[14:00:36] have to have only scalar
[14:00:38] subquery. So that's why we cannot do
[14:00:41] that. So we have to go and remove all
[14:00:43] those stuff.
[14:00:45] But we can go and solve it using the
[14:00:47] correlated subqueries. So now the
[14:00:49] subquery is completely independent from
[14:00:52] the main query. So in order to correlate
[14:00:55] it, what we're going to do, we're going
[14:00:56] to go and connect it. So I'm going to
[14:00:57] give aliases for the tables and I'm
[14:01:00] going to say where the customer ID equal
[14:01:04] to the customer ID from the main query
[14:01:07] from the customers. So again we are
[14:01:09] connecting the customer ID from the
[14:01:11] orders in the subquery with the customer
[14:01:14] ID from the table customers that comes
[14:01:17] from the main query. So now we are
[14:01:19] saying okay execute this only for a
[14:01:22] specific customer not for the whole
[14:01:24] table. So let's go and execute it. So
[14:01:27] now in the output we have the total
[14:01:29] sales for each customer and we don't
[14:01:31] have here like the total sales in the
[14:01:33] whole table orders and that's because
[14:01:35] what is happening for each row the
[14:01:37] subquery going to be executed. So for
[14:01:39] the customer number one this query going
[14:01:42] to be executed like this count the total
[14:01:44] number of orders where the customer ID
[14:01:48] equal to the one. So let me just show
[14:01:50] you what this means. If I go and remove
[14:01:52] this from here and just put the number
[14:01:54] one. So if I go and execute this, you
[14:01:57] will see the customer ID one has three
[14:02:00] orders. And let's just put it back and
[14:02:03] execute. And the same thing going to
[14:02:04] happen for each customer. So for each
[14:02:06] customer, for each row, this subquery
[14:02:09] going to be executed and it can be
[14:02:11] filtered with the customer ID that comes
[14:02:13] from the main query. So this is another
[14:02:15] way in how to solve this task using the
[14:02:17] correlated subqueries. So now let's
[14:02:19] summarize and understand quickly what
[14:02:21] are the differences between the
[14:02:22] non-correlated and the correlated
[14:02:24] subqueries. So now if you are talking
[14:02:25] about the definition the non-correlated
[14:02:28] subquery are subqueries that are
[14:02:30] independent of the main query but in the
[14:02:33] other hand the correlated subqueries are
[14:02:35] dependent of the main query. And now if
[14:02:37] you're talking about the execution the
[14:02:39] non-correlated subquery is going to be
[14:02:41] executed only once and then the results
[14:02:44] going to be used by the main query but
[14:02:46] by the correlated subqueries the
[14:02:48] subquery going to be executed for each
[14:02:50] row that we have from the main query.
[14:02:52] And as we learned for the non-correlated
[14:02:54] subqueries we can execute it on its own.
[14:02:57] So we can go and select it and execute
[14:02:59] it. But the correlated subqueries we
[14:03:01] cannot execute it on its own. So we have
[14:03:03] to execute always the whole thing. And
[14:03:06] if you are talking about which one is
[14:03:07] easier, I think it's clear that the
[14:03:09] noncorrelated subqueries are easier to
[14:03:12] write and to read. And in the other
[14:03:13] hand, the correlated subqueries are
[14:03:15] harder to read and as well it's complex.
[14:03:18] Now, if you're talking about the
[14:03:19] performance of the database since the
[14:03:21] correlated subqueries can be executed
[14:03:23] only once, this of course going to lead
[14:03:25] you to have better performance because
[14:03:27] things are really straightforward and
[14:03:28] not complicated. But in the other hand
[14:03:30] with the correlated subqueries there is
[14:03:32] more effort because SQL has to check a
[14:03:35] lot of stuff and the subquery going to
[14:03:36] be executed many times. So the
[14:03:38] noncorrelated subqueries are faster. We
[14:03:40] use the noncorrelated subqueries in
[14:03:42] order to do static comparison. So the
[14:03:46] value that we are getting from the
[14:03:47] subquery is executed only once and we
[14:03:49] will get only one static value in order
[14:03:52] to use it for filtering and so on. But
[14:03:54] in the other hand we use correlated
[14:03:55] subqueries in order to do rowby row
[14:03:57] comparison. And since we don't have here
[14:03:59] a static value each time the subquery
[14:04:01] going to run we're going to have
[14:04:02] different results. This going to add
[14:04:04] more dynamic to the filters and we don't
[14:04:06] have a static value. So those are the
[14:04:08] big differences between the
[14:04:09] non-correlated and the correlated
[14:04:10] subqueries. All right. So now after we
[14:04:13] understood the concept of the two types
[14:04:15] correlated and non-correlated subqueries
[14:04:17] we're going to go now and cover the last
[14:04:20] operator for the subqueries. We have the
[14:04:22] exists. So what is exist
[14:04:27] operator? All right. So now we're going
[14:04:29] to talk about a very interesting
[14:04:31] operator function in SQL the exists. So
[14:04:34] now in some scenarios as you are
[14:04:35] querying the data from one table you
[14:04:38] would need to go and check whether the
[14:04:39] rows of this table exist in another
[14:04:42] table. So that means you are checking
[14:04:44] like the existence of your rows in
[14:04:46] different table. And exactly in this
[14:04:48] scenario we go and use subqueries
[14:04:51] together with the operator exists. So
[14:04:53] the exist operator is very simple. It
[14:04:55] just simply check whether the subquery
[14:04:58] returns any results any rows. All right.
[14:05:00] So now let's understand the syntax of
[14:05:02] the correlated subqueries using the
[14:05:04] exist operator. This can be a little bit
[14:05:07] complicated but we're going to do it
[14:05:08] step by step. Don't worry about it. So
[14:05:10] let's start with the easy stuff. In the
[14:05:12] main query we're going to go and write a
[14:05:14] simple select. We are selecting few
[14:05:16] columns from the table two. And now we
[14:05:18] don't need all the data from table two.
[14:05:20] We want to filter the table using the
[14:05:22] wear clause. Now what we're going to do
[14:05:24] after the wear clause, we're going to
[14:05:25] write immediately another keyword called
[14:05:27] exists. So we don't specify any column
[14:05:30] before the exist like we have done in
[14:05:32] the comparison operator or the in
[14:05:33] operator. We don't need that because we
[14:05:35] are not filtering based on a value. We
[14:05:37] are filtering based on the logic. That's
[14:05:39] why we have the word exist immediately.
[14:05:42] And now directly after they exist, we're
[14:05:44] going to go and define the subquery like
[14:05:46] this. So we're going to start saying
[14:05:47] select one from the table number one.
[14:05:50] Well, it is not like a must or
[14:05:52] something. But it is very commonly used
[14:05:54] to specify here a one. We are not using
[14:05:56] the subquery in order to retrieve
[14:05:58] informations from the table one. We are
[14:06:00] just testing whether the subquery going
[14:06:03] to return a value or not. And we don't
[14:06:05] care about the returned value. It could
[14:06:07] be one, it could be column, it could be
[14:06:09] anything. So we don't care about the
[14:06:12] data that is retrieved. We are just care
[14:06:14] whether the subquery is returning
[14:06:16] anything. So that's why we go and write
[14:06:18] any value like here a one. So now we are
[14:06:20] not done yet. This subquery is not yet
[14:06:23] connected to the main query. We have
[14:06:25] somehow to go and connect them together.
[14:06:27] And we can do that using the wear clause
[14:06:30] where we go and connect the ID from the
[14:06:32] table one from the subquery with the ID
[14:06:35] from the outer query from the main
[14:06:38] query. And with that we are building
[14:06:39] like a relationship between the subquery
[14:06:43] and the main query. So with that the
[14:06:45] subquery is now depending on the values
[14:06:48] from the main query because here we have
[14:06:50] the table 2 id. So the ids from the main
[14:06:53] query going to filter the subquery. So
[14:06:57] this is the syntax of correlated sub
[14:06:59] queries using the exist where we are
[14:07:01] making the subquery depending totally on
[14:07:04] the main query. So let's understand how
[14:07:06] exist works. So now for each row that we
[14:07:09] have from the main query, it's going to
[14:07:11] trigger and cause an execution of the
[14:07:14] subquery. This subquery going to help us
[14:07:16] to evaluate this row. So we are testing
[14:07:18] this row. Now if the subquery doesn't
[14:07:21] return anything, so there is no results,
[14:07:23] what can happen? The row that we are
[14:07:25] evaluating from the main query will be
[14:07:28] excluded from the final results. But now
[14:07:30] in the other hand if the subquery is
[14:07:33] returning a value so we have like some
[14:07:35] kind of results then this row that we
[14:07:37] are evaluating going to be included in
[14:07:40] the final results. So the subquery is
[14:07:42] used in order to do a test. Do we have a
[14:07:45] results or we don't and based on this
[14:07:47] SQL either going to include or exclude
[14:07:50] the row from the final results. So this
[14:07:52] is the logic behind the exist in SQL.
[14:07:55] All right. So now we're going to go and
[14:07:56] solve the same task using the exists. So
[14:07:58] the task says show the details of orders
[14:08:01] made by the customers in Germany. So we
[14:08:04] have already solved this task using the
[14:08:05] in operator and the subquery. Now we're
[14:08:08] going to go and solve it using the
[14:08:09] exists. So again we're going to have the
[14:08:11] same logical steps that we have done
[14:08:12] before. So first we're going to go and
[14:08:14] select all the details from the table
[14:08:17] sales orders. So let's execute it. And
[14:08:20] with that we have all the orders and all
[14:08:22] the details. But of course we don't need
[14:08:25] all those informations. We need only the
[14:08:27] orders that's made by customers from
[14:08:29] Germany. So that is the first query.
[14:08:31] Let's go and construct the second query.
[14:08:34] We're going to say select star from
[14:08:37] sales customers. But we don't need all
[14:08:40] the customers. We need only the
[14:08:41] customers from country equal to the
[14:08:45] value Germany. So let's go and execute
[14:08:49] it. So now we have all customers that
[14:08:52] come from Germany. Now we have to go and
[14:08:54] put those two queries together in order
[14:08:57] to get the final results. So as we
[14:08:59] learned before the second query going to
[14:09:01] be our subquery. So it's going to be
[14:09:03] supporting the first query in order to
[14:09:05] filter the data. So the first query
[14:09:07] going to be our main query. Let me just
[14:09:10] make this smaller and the text as well.
[14:09:13] Now we don't need all the orders, right?
[14:09:16] We need only the orders where the
[14:09:17] customer come from Germany. So we need
[14:09:19] the work clause. So now we can have the
[14:09:21] filter logic like this. Show the order
[14:09:24] details only if the customer ID exist
[14:09:27] from the subquery. And now we have to go
[14:09:30] and put our subquery. So our subquery
[14:09:32] going to be this one over here. So let's
[14:09:36] just move it to the right side. And in
[14:09:39] order to have it as a subquery, we have
[14:09:40] to close the parenthesis. And now since
[14:09:43] exist is correlated subquery, we cannot
[14:09:45] have it like this. we have to go and
[14:09:47] connect the subquery together with the
[14:09:50] main query. So now the subquery is
[14:09:52] currently independent from the main
[14:09:54] query because we want to check each
[14:09:56] order information from the order table
[14:09:58] to check whether the customer exist in
[14:10:01] the sub query. We're going to go and add
[14:10:03] the condition like the following. And
[14:10:06] now it's like the joins we have to go
[14:10:07] and connect the customer ids together.
[14:10:10] So we're going to go over here and give
[14:10:11] it like an alias and as well for the
[14:10:13] subquery. And now we're going to say
[14:10:15] customer ID from the orders should be
[14:10:19] equal to the customer
[14:10:21] ID from the subquery the table customers
[14:10:25] like this. So again this customer ID
[14:10:28] come from the subquery and this customer
[14:10:30] ID comes from the main query. So now
[14:10:33] since we are using the subquery only in
[14:10:36] order to test the existence of the
[14:10:38] customer. So if the subquery returns
[14:10:40] anything or not, it doesn't matter what
[14:10:42] you are selecting in the subquery. So so
[14:10:45] you can go with the star or a column or
[14:10:47] any static value. But for some reason
[14:10:50] all the SQL developers decided to go
[14:10:52] with the static value one. And of course
[14:10:54] you can go and add like a column like
[14:10:56] the customer ID but it's like
[14:10:58] unnecessary step for the SQL in order to
[14:11:00] retrieve the information from the
[14:11:02] customer ID. So it's going to be way
[14:11:04] faster for SQL if you say okay select
[14:11:06] one. So let's stick with the best
[14:11:08] practices. Use the one value if you are
[14:11:11] working with exist. So this is our sub
[14:11:14] query and I think we have everything.
[14:11:17] Let's go and execute it. Now as you can
[14:11:19] see in the output we got all the orders
[14:11:21] where the customers come from Germany.
[14:11:23] Now of course if you want to go and try
[14:11:25] another value and execute you will get
[14:11:28] exactly the same results. So it doesn't
[14:11:30] matter which value you are using. So
[14:11:32] with that we have solved the task this
[14:11:33] time using the exists. Now if the task
[14:11:35] says show the details of orders made by
[14:11:38] customers that don't come from Germany
[14:11:40] it's going to be very simple. We're
[14:11:42] going to go and use the operator not
[14:11:44] before the exist. So where not exists.
[14:11:47] So now we are flipping the whole logic
[14:11:49] and we are saying there should be no
[14:11:51] matching with the subquery. So now if
[14:11:54] you go and execute it you will get all
[14:11:56] the orders where the customers don't
[14:11:59] come from Germany by simply using the
[14:12:01] not logic. And there is one more thing
[14:12:03] that is annoying about the correlated
[14:12:05] subqueries. If you compare to the
[14:12:06] non-correlated subqueries as we learned
[14:12:09] before, let me go back to the n
[14:12:11] operator. Now this is a non-correlated
[14:12:13] subquery. And if I go and select only
[14:12:16] the subquery, I can go and execute it
[14:12:18] independently. So I can go and check the
[14:12:21] intermediate results and like validate
[14:12:23] my query. But the problem with the
[14:12:25] correlated subquery, I cannot go and
[14:12:27] highlight the subquery and then go and
[14:12:29] execute it. And that's because in the
[14:12:31] syntax of the subquery we are adding a
[14:12:34] column that is outside our subquery that
[14:12:36] come from the main query. So this piece
[14:12:39] of information currently for the SQL is
[14:12:41] unknown and that's why we are getting
[14:12:43] this error because SQL saying okay I
[14:12:45] don't know where this column come from.
[14:12:47] So this is little bit annoying using the
[14:12:49] correlated subqueries you cannot go and
[14:12:51] test the intermediate results. But how I
[14:12:53] usually do it I go and test like an
[14:12:55] intermediate result for only one row. So
[14:12:57] for example, I'm going to go and pick
[14:12:58] like a customer here. For example, two.
[14:13:01] So I'm going to go and say okay, the
[14:13:04] customer ID should be equal to two. So
[14:13:06] let me just remove this from here. I got
[14:13:09] this value from the main query. So if I
[14:13:12] go now and execute it, I can see here.
[14:13:14] Okay, the subquery is not returning
[14:13:16] anything because there is no such a
[14:13:18] value. So with that, I'm just testing
[14:13:20] like one row. And of course in order to
[14:13:22] make this working I have to go and add
[14:13:24] as well the column from the main query.
[14:13:28] So this is why correlated subqueries are
[14:13:30] a little bit more hard to understand
[14:13:32] compared to the non-correlated because
[14:13:34] we cannot go and test the intermediate
[14:13:36] results like we can do there. So this is
[14:13:38] another way on how to solve this task
[14:13:40] using a correlated subqueries with the
[14:13:43] operator exists. Okay. So now let's see
[14:13:45] step by step how SQL executed the
[14:13:47] correlated subqueries using the exists
[14:13:50] operator. So now this time SQL will not
[14:13:52] start with the subquery. SQL going to go
[14:13:55] and start immediately with the main
[14:13:56] query. SQL first going to identify the
[14:13:59] main query and it going to go and
[14:14:01] execute it. But it's going to executed
[14:14:03] row by row. So the first row going to be
[14:14:05] the first customer. So now SQL going to
[14:14:07] go and put the first customer under the
[14:14:10] test. So now the next step is that SQL
[14:14:12] going to go and pass the value of the
[14:14:14] customer ID from the main query to the
[14:14:17] subquery. So we are doing now exactly
[14:14:19] the opposite. So now what going to
[14:14:21] happen? SQL going to prepare the
[14:14:22] subquery with the following information.
[14:14:24] So we are saying the customer ID equal
[14:14:27] to one and then SQL going to go and
[14:14:29] execute it. So now once SQL executed
[14:14:31] this query, we will get the result of
[14:14:33] one and that's because we have here
[14:14:35] multiple times where the customer ID is
[14:14:38] equal to one. So there is rows in the
[14:14:41] order table where the customer ID equal
[14:14:43] to one. So now what going to happen? the
[14:14:45] row from the main query going to pass
[14:14:47] the test and this customer going to be
[14:14:50] included in the final results. So now
[14:14:52] the next step with that is going to go
[14:14:53] and start testing the second customer.
[14:14:56] So we're going to put this customer
[14:14:58] under the test. Now we're going to go
[14:15:00] and pass the value to the subquery. So
[14:15:02] here we're going to have the value of
[14:15:03] two and then SQL going to go and execute
[14:15:05] this query and of course we will get a
[14:15:07] result because we have here multiple
[14:15:09] times where the customer ID equal to
[14:15:11] two. So that's why in the output of this
[14:15:13] subquery we will get one. So now it's
[14:15:15] still going to say great we have a value
[14:15:17] from the subquery that's why it is safe
[14:15:20] to show this customer in the output. And
[14:15:22] now it's still going to go to the next
[14:15:24] row and so on. So for the next two
[14:15:26] customers the same things going to
[14:15:28] happen. All of those customers will have
[14:15:31] a value from the subquery and that's why
[14:15:33] they are all like passing the test. So
[14:15:36] we will have it in the output. Now skill
[14:15:38] going to go to the last row from the
[14:15:40] table customers. So we have the Anna and
[14:15:43] we're going to put Anna to the test. So
[14:15:45] now what going to happen? SQL going to
[14:15:46] go and pass the value five to the
[14:15:49] subquery and SQL going to go and execute
[14:15:51] this query to the table orders. Now once
[14:15:54] SQL execute this query there will be
[14:15:56] nothing returned and that's because we
[14:15:59] don't have here in the table orders a
[14:16:01] customer ID equal to five. And now SQL
[14:16:03] going to say well we are not getting any
[14:16:05] results from the subquery. That's why
[14:16:08] this customer going to fail and SQL will
[14:16:11] not show it at the output. So it will be
[14:16:14] completely removed. So the customer Anna
[14:16:16] is excluded because the subquery is not
[14:16:18] returning anything. Customer ID number
[14:16:20] five Anna does not exist in the table
[14:16:24] orders. So it's going to fail the test
[14:16:25] and we will have in the final results
[14:16:28] only for customers. So this is exactly
[14:16:30] the purpose of the exist. we are
[14:16:32] checking and testing the existence of
[14:16:34] our rows from another table from another
[14:16:37] query. So this is how SQL executes the
[14:16:40] correlated subqueries using the operator
[14:16:43] [Music]
[14:16:47] exists. All right friends, so with that
[14:16:49] you have covered everything about the
[14:16:51] subqueries, all the different categories
[14:16:53] and types of the subqueries and now
[14:16:55] we're going to do a quick recap about
[14:16:56] the subqueries. So as we learned
[14:16:58] subqueries is just simply a query inside
[14:17:01] another query. And we use the subqueries
[14:17:04] in order to break down a complex queries
[14:17:07] into smaller, simpler, easy to manage
[14:17:10] pieces that makes everything easier to
[14:17:12] develop and as well to read. And as we
[14:17:14] learned there are like many different
[14:17:16] use cases for the subqueries. So we use
[14:17:18] subqueries in order to create temporary
[14:17:21] result sets to be used later from
[14:17:23] another query. And we learned that we
[14:17:25] can use the subqueries in order to
[14:17:27] prepare the data before joining the
[14:17:30] tables. And another very important use
[14:17:32] case for the subquery is that we can use
[14:17:34] it in order to filter our data using a
[14:17:37] dynamic and as well complex filter
[14:17:40] logics. And as we learned, we can go and
[14:17:42] use the correlated subqueries using the
[14:17:44] exist operator in order to check the
[14:17:47] existence of data and rows from another
[14:17:50] tables. and as well using the correlated
[14:17:52] subqueries help us to do rowby row
[14:17:55] comparison. All right my friends, so
[14:17:56] with that we have covered an important
[14:17:58] technique on how to nest your queries in
[14:18:00] SQL. Now in the next step we're going to
[14:18:02] talk about one of the most famous
[14:18:04] technique on how to do multi steps in
[14:18:06] SQL the city common table expression. So
[14:18:09] let's go.
[14:18:14] A city common table expression is a
[14:18:17] temporary named result set like a
[14:18:19] virtual table that could be used
[14:18:21] multiple times within your query to
[14:18:24] simplify and organize complex query. So
[14:18:27] let's understand what this means using
[14:18:29] the following sketch. So we have our
[14:18:31] database tables like orders, customers
[14:18:33] and so on. And in very simple scenario
[14:18:35] we write a simple SQL in order to query
[14:18:38] and retrieve the data from the database
[14:18:40] and then in the output we will get the
[14:18:42] result of the query. So this is the
[14:18:44] simplest version of querying data. Now
[14:18:46] things get complicated in our project
[14:18:48] and we could have the following
[14:18:50] technique in our query. So we still have
[14:18:52] this section where we are saying select
[14:18:54] from. But now inside our query we can
[14:18:57] write another query like for example
[14:18:59] select from where which is completely
[14:19:02] nothing to do with the first query and
[14:19:04] we can give this new query inside our
[14:19:06] query a name CTE and we can call this
[14:19:09] query a CTE query common table
[14:19:12] expression. And the first query outside
[14:19:14] this CDE we call it a main query. Now if
[14:19:17] you check this we have like a query
[14:19:19] inside another query. So now let's see
[14:19:21] what is going to do with this. The first
[14:19:23] thing is going to go and execute the
[14:19:25] city query. So the city query going to
[14:19:28] be executed and we're going to go and
[14:19:30] retrieve few informations from our
[14:19:31] database tables. Now the output going to
[14:19:34] be available only in the query and the
[14:19:36] output going to have the shape of like a
[14:19:38] table like for example the sales. So now
[14:19:40] the sales table and the orders tables
[14:19:43] both of them are tables but one is
[14:19:45] stored in the database and the other one
[14:19:48] is an intermediate virtual table. So now
[14:19:51] what can happen in the main query we can
[14:19:53] go and start querying the sales table
[14:19:56] the result from the CTE as any other
[14:19:59] normal table like we do to the database
[14:20:01] tables. So the main query going to go
[14:20:03] and retrieve few informations and maybe
[14:20:05] do some manipulations on top of the
[14:20:07] sales table or let's say the CTE results
[14:20:10] and of course the main query as well can
[14:20:12] go and say you know what let's go and
[14:20:13] query as well few tables from the
[14:20:16] database. So the main query has two
[14:20:18] sources of tables. Either get it
[14:20:20] directly from the database or get it
[14:20:22] from the table that is created inside
[14:20:25] the query and then once everything is
[14:20:27] done the final results of the main query
[14:20:29] going to be presented for the user as a
[14:20:32] final result. So as you can see the CTA
[14:20:34] query has one task where it generates
[14:20:37] like a table that lives inside our query
[14:20:40] and we can go and use it as we want. So
[14:20:42] now this intermediate table that is
[14:20:44] created from the city has two features.
[14:20:46] First this table will not live long. So
[14:20:49] once the query ends what going to happen
[14:20:51] is going to go and destroy this table.
[14:20:54] So it will not be available afterward
[14:20:56] and we are not able to query it anymore.
[14:20:58] So SQL is doing here like a cleanup and
[14:21:00] the second character about this let's
[14:21:02] imagine that we have another side query
[14:21:05] and it's retrieving tables directly from
[14:21:07] the database tables. Now if you say
[14:21:09] let's go and join those tables as well
[14:21:12] with the sales from the first query well
[14:21:14] it will not be working because SQL going
[14:21:16] to say I don't know what you are talking
[14:21:18] about and that's because the sales is
[14:21:21] only locally available for the main
[14:21:24] query in the same query. So that means
[14:21:28] it's not globally available like the
[14:21:30] database tables for any query. It is
[14:21:32] dedicated only for the main query within
[14:21:35] the same query. And now you might tell
[14:21:37] me bar wait I have heard this story
[14:21:40] before right? So this is an identical
[14:21:42] story to the one that you have told us
[14:21:44] about the subqueries. So what is exactly
[14:21:46] the difference between the subquery and
[14:21:48] the CTE? Well, you are totally right.
[14:21:50] The story is identical between the
[14:21:52] subqueries and the CTE but still there
[14:21:54] are differences between them. So let me
[14:21:56] show you few differences. Now let's put
[14:21:58] them side by side. We have on the left
[14:22:00] side the subqueries on the right side we
[14:22:02] have the CTE. So now if you look on how
[14:22:04] we wrote the CT and the subqueries you
[14:22:07] can see that on the subquery we are
[14:22:08] writing it from bottom to top. So first
[14:22:11] we have this inner query the subquery
[14:22:14] and then on top of it we have the main
[14:22:16] query. But now on the other hand the CTE
[14:22:18] we are writing it from top to bottom. So
[14:22:20] first we write this inner query the CTE
[14:22:23] query and then beneath it we're going to
[14:22:25] go and write the main query. So this is
[14:22:27] the first difference between them on the
[14:22:30] way we write the query. So if I'm
[14:22:32] thinking about subqueries, I start from
[14:22:34] bottom to top. If I'm thinking about
[14:22:35] CTE, I think from top to bottom. But
[14:22:38] still you say, you know what, I don't
[14:22:39] care how we write it. They are doing the
[14:22:41] same thing. The subquery is introducing
[14:22:44] an intermediate result that is used
[14:22:46] later from the main query. And the same
[14:22:49] thing for the CTE. It present like
[14:22:51] intermediate table that is used as well
[14:22:53] from the main query. Now let me tell you
[14:22:55] the big differences between them is that
[14:22:57] in the subquery the result can be used
[14:23:00] only once. So you cannot have another
[14:23:02] place in your main query where you go
[14:23:05] and reuse the result from the subquery.
[14:23:07] So you can use it maximum only in one
[14:23:10] position and only once. But in the other
[14:23:12] hand with the city technique, you can
[14:23:14] think about the sales table as a virtual
[14:23:17] table and not only you can use it in one
[14:23:19] place in the main query, you can go and
[14:23:22] use it in many other places. So you can
[14:23:25] go and join it again. So that means I'm
[14:23:27] using the output from the CTE query in
[14:23:30] two different places in the main query
[14:23:32] or maybe from three different places. So
[14:23:34] you can have another place where you go
[14:23:36] as well and query the sales table that
[14:23:39] is only available in our query. So this
[14:23:41] is the main and the most important
[14:23:43] difference between the subquery and the
[14:23:44] CTE. It's from the name common table
[14:23:46] expression. We think about the result of
[14:23:48] the CTE as a table. So we can go and
[14:23:52] select it. We can go and join it with
[14:23:54] any other table. So it is like a hidden
[14:23:56] virtual table lives inside our query.
[14:23:59] But the subqueries it's totally
[14:24:01] different. It's a result only for one
[14:24:03] position in the main query and it's used
[14:24:06] only once. So that means if you want the
[14:24:08] subquery in two three different places,
[14:24:10] you have to go and write the subquery
[14:24:12] three different times. So now you
[14:24:14] understand why do we have CTE and why do
[14:24:17] we have
[14:24:21] subqueries. All right. So with that you
[14:24:23] have understood what is CTE. Now the
[14:24:25] question is why do we need CTE in the
[14:24:27] first place? What is the main purpose of
[14:24:30] the CTE? Let's go back to the sketch.
[14:24:32] Now let's say in our complex SQL task we
[14:24:35] have to do the following step. Step one
[14:24:37] we have to go and join the tables
[14:24:38] together in order to prepare all the
[14:24:40] data that we need for the next step. And
[14:24:42] now in the second step we have to go and
[14:24:44] aggregate the data. Maybe we are doing
[14:24:46] summarizations. Now in our task we have
[14:24:48] to do as well different types of
[14:24:50] aggregations based on different data.
[14:24:52] And now what might happen is that we
[14:24:54] have to go and join again the same
[14:24:56] tables in order to prepare the data and
[14:24:58] perform different type of aggregations
[14:25:00] like for example the average which going
[14:25:02] to be in the last step. Now we have
[14:25:04] learned before we can go and use the
[14:25:06] subqueries in order to make this logical
[14:25:08] flow. So for step one, step two, step
[14:25:11] three, we will have subqueries and the
[14:25:13] final step going to be in the main
[14:25:15] query. But now if we keep doing this
[14:25:17] we're gonna have a problem and that is
[14:25:19] we are repeating the same step more than
[14:25:22] once. So we are joining the table twice
[14:25:24] in step number one and three for
[14:25:26] different purposes which cause us to
[14:25:28] have two different subqueries that looks
[14:25:31] exactly the same and this is exactly the
[14:25:34] weak point of the subqueries. It might
[14:25:36] introduce redundancies. So that means
[14:25:39] the subqueries alone will not help you
[14:25:41] to eliminate all the duplicates in your
[14:25:44] code. But still we have different
[14:25:45] techniques in order to solve this issue.
[14:25:47] So what we going to do? We're going to
[14:25:49] have only one step in order to join the
[14:25:52] tables. And then this data going to be
[14:25:53] used in the step two in order to
[14:25:55] aggregate the data. And then we don't
[14:25:57] need the step three of joining again the
[14:25:59] data. We're going to reuse the step one.
[14:26:02] And we're going to use the same data for
[14:26:04] the step four which is aggregating the
[14:26:06] data using average. And we can do this
[14:26:09] with the help of the amazing CTE. So now
[14:26:11] if you compare the steps in the
[14:26:13] subqueries with the steps with the CTE
[14:26:15] you can see with the CTE we are reducing
[14:26:18] the number of steps which can lead to
[14:26:20] reduce the size of the query. So now
[14:26:22] again here in subquery we think about
[14:26:24] the steps from bottom to top but in the
[14:26:27] city it's the way around we think from
[14:26:30] top to bottom. So that means the first
[14:26:31] step on the top it's going to be joining
[14:26:33] the tables and then below it going to be
[14:26:35] step two and step three. And of course
[14:26:37] since we are repeating the join we're
[14:26:38] going to put it in CTE and then we can
[14:26:40] use it twice in different places in the
[14:26:43] main query. So as you can see there are
[14:26:45] a lot of benefits of the CTE. It's like
[14:26:47] the subqueries. We are breaking down
[14:26:49] complex queries into smaller pieces that
[14:26:52] are easier to write manage understand
[14:26:55] and as well we have like a logical flow
[14:26:57] from step one to three but with one more
[14:26:59] benefit that we reduce the redundancies
[14:27:03] of our code. So we don't have to join
[14:27:05] the tables twice. Now I'm going to show
[14:27:06] you a simple example how the CTE makes
[14:27:09] our life easier in our query. We might
[14:27:11] have to do different stuff like for
[14:27:13] example we have to go and find the top
[14:27:15] customers. So we can put this in one CTE
[14:27:18] and we might need as well to calculate
[14:27:20] what are the top products and we can put
[14:27:22] as well this in another city. So you
[14:27:25] don't have to put everything in one big
[14:27:26] city. Then you can have the same issue
[14:27:28] of having complex query. And let's say
[14:27:30] that we have as well to find and
[14:27:31] calculate the daily revenue. And for
[14:27:34] this as well, we have to put it in one
[14:27:35] CTE. Now once we have all those parts,
[14:27:38] we can put everything together in the
[14:27:40] main query. So now if you look to this
[14:27:42] structure, you can see it's really easy
[14:27:44] to understand this code. It's easy to
[14:27:47] read. So CTE improves the readability of
[14:27:50] our queries. So that means your code is
[14:27:52] divided into clear sections making it
[14:27:55] easier to understand what each part
[14:27:57] does. Now if you keep looking to this we
[14:27:59] have another advantage of the CTE
[14:28:02] introduces modularity. So that means it
[14:28:04] breaks your code into smaller manageable
[14:28:07] parts. So this means instead of writing
[14:28:10] one huge complex query you break it down
[14:28:13] into smaller chunks using CTE. Each city
[14:28:16] is like self-contained and handles
[14:28:19] specific part of the problem and then
[14:28:21] you can combine them all together in the
[14:28:23] final query. It's like we are putting
[14:28:25] together a puzzle piece by piece. And
[14:28:27] now one very important advantage of the
[14:28:29] CTE is the reusability. So that means we
[14:28:32] can have a result set that is used
[14:28:34] multiple times inside our query. So that
[14:28:37] means you write the logic the code only
[14:28:39] once and then use it in different places
[14:28:41] inside your query. This is very
[14:28:43] important. Not only you are wasting time
[14:28:45] writing the same stuff over and over,
[14:28:47] but also it reduces the errors and
[14:28:49] mistakes that you might do if you are
[14:28:51] repeating the same code. Especially if
[14:28:53] later you want to go and change the
[14:28:54] logic then you have to go and visit each
[14:28:57] time you have done this logic and then
[14:28:59] do the changes and you might forget some
[14:29:01] places. That's why the CTE is amazing.
[14:29:04] You can write the logic once and then
[14:29:06] you go and reuse it in different places.
[14:29:08] So these are the advantages of using
[14:29:10] this technique the CTE inside your
[14:29:13] [Music]
[14:29:16] queries. So again you are at the client
[14:29:19] side and you are data analyst. You are
[14:29:21] writing a query where you are defining a
[14:29:23] CTE called details and inside it you
[14:29:25] have some logic and now in the main
[14:29:27] query you are selecting the data from
[14:29:29] the orders and as well you are joining
[14:29:31] it with the details with the CTE
[14:29:33] multiple times using multiple
[14:29:35] conditions. Now once you go and execute
[14:29:37] this query the database engine going to
[14:29:39] read the query and say aha we have here
[14:29:41] a CTE and it has the main priority. So
[14:29:44] that means it going to go and execute
[14:29:46] the CTE first. And now let's say that in
[14:29:48] the city you are retrieving data from
[14:29:50] the table orders and the table orders of
[14:29:52] course in the disk storage inside the
[14:29:54] user data. And now once the city is
[14:29:57] completely executed the database engine
[14:29:59] going to go and place the results in the
[14:30:01] cache and it's going to name this result
[14:30:03] as details. It's like a table name. So
[14:30:06] the database engine is done with the
[14:30:07] CTE. It's going to go now and grab the
[14:30:09] main query and it's going to start
[14:30:11] executing it step by step. So the first
[14:30:13] step is that to get the data from the
[14:30:15] orders. So since the orders exist in the
[14:30:17] disk storage, it going to go and
[14:30:19] retrieve it from there. Now the database
[14:30:21] engine going to check the details. Okay,
[14:30:22] we have it in the cache. That means we
[14:30:25] don't have to search for it in the disk
[14:30:26] storage and it going to start retrieving
[14:30:28] the data from the details with high
[14:30:30] speed. And now it's going to go to the
[14:30:32] second step as well joining the data
[14:30:34] with the details. So again the database
[14:30:36] engine going to go to the cache and
[14:30:37] going to see the table details and
[14:30:39] retrieve the data based maybe in
[14:30:40] different conditions. And then to the
[14:30:42] third time as well we are joining to the
[14:30:44] details and we're going to get the data
[14:30:45] from the cache. So as you can see from
[14:30:47] the main query we are using the result
[14:30:50] from the CTE multiple times in different
[14:30:52] places and the retrieval of all those
[14:30:54] informations is happening in high speed.
[14:30:57] So this is one big benefit of using the
[14:30:59] CTE is to utilize using the high-speed
[14:31:02] memory of the cache. So that means
[14:31:04] retrieving the data from the cache from
[14:31:06] the details is way faster than
[14:31:08] retrieving the data from the disk
[14:31:10] storage from the orders. Now once the
[14:31:12] main query is completely executed the
[14:31:14] result going to be returned to the
[14:31:16] database engine and then it's going to
[14:31:18] send it back to the client side and we
[14:31:20] will see the results in the output. So
[14:31:22] that's it. It's amazing right? This is
[14:31:24] how the database server execute the
[14:31:26] amazing technique the CTE behind the
[14:31:29] scenes.
[14:31:33] All right. So now for the CTE, we don't
[14:31:35] have only one CTE. We have different
[14:31:37] types of CTE. So mainly there are like
[14:31:40] two types of CTE. We have the
[14:31:42] nonrecursive CTE and recursive CTE. And
[14:31:45] we can say for the nonrecursive CTE, we
[14:31:48] have two subtypes. The first type is the
[14:31:51] standalone CTE and the second one is the
[14:31:54] nested CTE. And now what we're going to
[14:31:56] do, we're going to deep dive into each
[14:31:57] type. And we will start with the easiest
[14:32:00] form of the CTE, the standalone CTE. It
[14:32:03] is the simplest
[14:32:08] form. So what is standalone CTE? It is a
[14:32:11] CTE query that is defined and used
[14:32:14] independently in the query. So that
[14:32:16] means it is self-contained and it
[14:32:18] doesn't depend on anything. It doesn't
[14:32:20] depend on any other CTE or queries. So
[14:32:24] that means we can run the standalone
[14:32:26] query independently from anything inside
[14:32:28] our query. So let's understand what this
[14:32:30] means. We have our CTE. It's going to go
[14:32:32] and query the database tables and in the
[14:32:35] output we will get an intermediate
[14:32:37] results and then the output can be used
[14:32:39] from the main query. So the main query
[14:32:41] going to query the intermediate results
[14:32:43] and present in the output the final
[14:32:45] results. So now if you check our CTE, it
[14:32:47] is completely independent from anything
[14:32:49] else. So it simply query the database
[14:32:52] and it has one output. So since this CTE
[14:32:55] is independent from anything else we
[14:32:57] call it a standalone CTE. Now if you
[14:33:00] compare this CT with the main query you
[14:33:02] can see that the main query cannot be
[14:33:04] executed alone. And that's because it
[14:33:06] needs the result from the first query.
[14:33:09] So we cannot say the main query is
[14:33:11] independent cannot be executed alone. It
[14:33:14] always depend on the city query. So that
[14:33:17] means city first need to be executed
[14:33:19] then the main query can be executed. So
[14:33:21] this is what we mean with the standalone
[14:33:24] city. It doesn't depend on anything
[14:33:26] else. So now we can understand the
[14:33:28] syntax of the CTE. So we have a very
[14:33:30] simple query select from where. So it is
[14:33:32] a very simple select statement. Now in
[14:33:35] order to put it inside a CTE we can go
[14:33:38] and use the with clause. So it starts
[14:33:41] with the keyword with then the CTE name.
[14:33:44] It's like a table name and then we have
[14:33:46] the keyword as in order to say this CTE
[14:33:49] is defined like the following. So this
[14:33:52] is the definition of the CTE and it has
[14:33:54] two parenthesis the starting and the
[14:33:56] ending. So with this you are telling a
[14:33:58] scale okay now we are talking about CTE
[14:34:01] and it has a name. So if you are using a
[14:34:03] query inside with clause we call this a
[14:34:06] CTE query it is where you define the
[14:34:08] CTE. Now of course we don't want only to
[14:34:11] define a CTE. We want to use it. So
[14:34:13] outside of this definition we can go and
[14:34:15] use it like this. So we are saying
[14:34:17] select from the CTE name. So that means
[14:34:21] we want to select the data from the
[14:34:23] result of the CTE. And here it's very
[14:34:25] important to use exactly the same name
[14:34:27] as you define it in the width clause. So
[14:34:30] if you leave it like this, we can call
[14:34:32] this the main query. It is the place
[14:34:34] where we use the CTE. So this is the
[14:34:37] syntax of a very simple CTE in SQL.
[14:34:40] Okay. So now what we're going to do,
[14:34:42] we're going to have like a task that's
[14:34:44] going to keep progressing through this
[14:34:45] section. So we're going to start with
[14:34:47] the first step and we will keep adding
[14:34:49] steps as we progress in the CTE. So now
[14:34:51] the first step in this task says find
[14:34:54] the total sales per customer. And now of
[14:34:56] course since we have only one step, it
[14:34:58] makes no sense to use the CTE. But we
[14:35:00] will use it since we know that there
[14:35:02] will be different steps later. So let's
[14:35:05] start doing that. Now before I use any
[14:35:07] CTE, I would like just to write our
[14:35:09] query first. So we need the total sales
[14:35:11] for each customers. It's very simple. So
[14:35:13] we're going to go and select and what do
[14:35:15] we need? Let's go and get the customer
[14:35:17] ID and we need to do aggregations on the
[14:35:21] sales. So summarize the sales and we're
[14:35:24] going to call it total sales from the
[14:35:28] table. And now since this is our first
[14:35:30] query, we have to get the data from our
[14:35:32] database. So we don't have any other
[14:35:34] option. Our data going to be in the
[14:35:35] sales orders. So let's go and get it.
[14:35:39] And don't forget to group by for the
[14:35:41] aggregation. We are grouping by the
[14:35:43] customer ID. That's it. Let's go and
[14:35:46] execute it. And as you can see in the
[14:35:47] output, nothing is fancy. We are just
[14:35:49] aggregating the sales by the customers.
[14:35:51] So with that, we have solved the task.
[14:35:53] But now I would like to put my query in
[14:35:55] a CTE. And that's because later we're
[14:35:57] going to add more steps. So let's put
[14:35:59] our query in a city. And in order to do
[14:36:02] that, we're going to start with the with
[14:36:04] keyword. And now we have to define the
[14:36:07] name of the CD. So I'm going to call it
[14:36:09] city
[14:36:10] total sales like this. And then
[14:36:13] afterward we're going to say as and then
[14:36:15] we have to go and add the parenthesis at
[14:36:17] the start and as well at the end. And
[14:36:20] with that we are telling SQL this query
[14:36:22] is a CTE query. So that means the SQL
[14:36:26] should store the result of this query in
[14:36:29] a cache in memory to be used later in
[14:36:31] the main query. our CTE and of course
[14:36:34] what is missing is the main query and
[14:36:36] you have to do it exactly after the
[14:36:38] definition of the CTE. I will just make
[14:36:40] here a small comment about the main
[14:36:42] query. Uh let me just make this smaller
[14:36:45] like this. And now we have to go and
[14:36:47] have a very simple select
[14:36:49] statements from. And now I would like to
[14:36:51] get more details from the customers
[14:36:53] table. So I will just go now to the
[14:36:57] customers. So now we are not querying
[14:37:00] the CTE right? We are just querying the
[14:37:02] database table that we have and I would
[14:37:04] like to get from the customer the
[14:37:06] customer ID and the first name and let's
[14:37:11] go and get as well the last
[14:37:13] name. So now if we go and query this
[14:37:17] what happens in the output we are
[14:37:18] getting the data actually completely
[14:37:21] from the database table the customers
[14:37:24] and of course we are not using at all
[14:37:26] the CTE inside our main query. Of
[14:37:28] course, we can do that, but it's just
[14:37:29] waste of like space in the memory
[14:37:31] because SQL did execute this and stored
[14:37:34] it in the database memory. And of
[14:37:36] course, we would like to use the city in
[14:37:38] our main query. So, let's go and do
[14:37:40] that. So, let's go and do a join, but
[14:37:43] this time we're going to join the data
[14:37:45] from the CTE. So, let's go and get the
[14:37:47] name and I will just call it CTS. So
[14:37:51] what we are doing now we are joining the
[14:37:53] physical table the customers with the
[14:37:56] virtual table that we have created with
[14:37:58] the CTE that exist only in our query and
[14:38:02] of course not only we are joining the
[14:38:04] tables we would like to get the
[14:38:06] informations from the CTE. So CTS and we
[14:38:09] need only the total sales. So total
[14:38:12] sales. So that means those three columns
[14:38:16] comes from our database table customers
[14:38:18] and only this column the total sales
[14:38:20] comes from our CTE. So let's go and
[14:38:22] execute the whole thing. Now as you can
[14:38:24] see in the output everything is working.
[14:38:26] We have the three columns from the table
[14:38:28] customers and we have the total sales
[14:38:31] for each customer and this total sales
[14:38:33] comes from our city. Now as you can see
[14:38:35] the last customer has a null over here
[14:38:38] and that's because in the table orders
[14:38:40] we don't have the customer five. And now
[14:38:42] you might say you know what I would like
[14:38:43] to see the intermediate result from the
[14:38:45] CTE because what we are seeing now in
[14:38:47] the output is the final result from the
[14:38:50] main query. So now what we can do in
[14:38:52] order to see the result of the CTE we're
[14:38:55] going to mark the query in the CTE of
[14:38:57] course without any parenthesis or the
[14:38:59] width. So just the query and execute it.
[14:39:02] And with that you can see in the output
[14:39:04] the intermediate results that we are
[14:39:06] passing to the main query. And as you
[14:39:08] can see we don't have here customer
[14:39:10] number five. That's why in the final
[14:39:12] results we are getting null and that's
[14:39:14] of course because we are using the lift
[14:39:16] join. So if I execute the whole thing
[14:39:19] you can see we are getting the customer
[14:39:20] five over here with the null. So as you
[14:39:22] can see is very simple. We just treat it
[14:39:24] as any normal database table. But this
[14:39:26] table is created from our query that we
[14:39:29] have defined in the city over here. Now
[14:39:31] of course in the city you can use any
[14:39:34] kind of clauses like select from join
[14:39:37] group by having everything that you want
[14:39:39] window functions all aggregate functions
[14:39:41] but there is only one restriction you
[14:39:44] cannot go and use the order by clause so
[14:39:47] you cannot sort the data in the city so
[14:39:50] let's go and try it out let's go and say
[14:39:53] order by and let's say I want to sort by
[14:39:56] the order ID for example so let's go and
[14:39:58] execute it you can see here SQL is
[14:40:00] saying Okay, I cannot do it for you
[14:40:03] because order by is not allowed in many
[14:40:05] things. So you cannot use it in views,
[14:40:07] in sub queries, in comment table
[14:40:10] expressions, the CTE over here. So it is
[14:40:12] not allowed. You cannot use order by in
[14:40:15] the CTE. But of course you can go and
[14:40:17] sort the data in the main query. So if
[14:40:20] you go over here and say order by
[14:40:23] customer ID. So if we execute it, it's
[14:40:27] going to be working. So in the main
[14:40:29] query you can use order by but in the
[14:40:31] CTE this is the only thing that you
[14:40:33] cannot use inside the city. So that's
[14:40:35] it. This is our first CTE in this
[14:40:38] section. All right. So this is the
[14:40:40] simplest form of the CTE the standalone.
[14:40:42] Now we can have not only one CTE, we can
[14:40:45] have multiple
[14:40:50] CTE. So it's going to look like this. We
[14:40:52] have our database and this time we don't
[14:40:54] have only one CTE. We have multiple
[14:40:57] CTEes in our query and each CTE is going
[14:41:01] directly to the database and it will
[14:41:03] query the database in order to prepare
[14:41:06] the intermediate results. So in this
[14:41:08] example four CDEs is going to the
[14:41:10] database and preparing four different
[14:41:13] intermediate results and of course SQL
[14:41:15] going to execute it from the top to the
[14:41:16] bottom. So first the CD 1 then 2 3 four
[14:41:19] but they have nothing to do with each
[14:41:21] others. So now once we have all the four
[14:41:23] intermediate results the main query
[14:41:25] going to go and retrieve all those
[14:41:26] informations and do some magic in order
[14:41:28] to prepare the final result for the end
[14:41:31] user. So now by looking to this sketch
[14:41:33] you can understand all those CTE are
[14:41:35] independent from each others. So there
[14:41:37] is no nesting or something. Each CTE is
[14:41:40] self-contained and it could be executed
[14:41:42] on its own without depending on any
[14:41:45] other results from any other CTE or any
[14:41:48] other query. So it goes directly to the
[14:41:50] database and get the data. So that's why
[14:41:53] all of them are standalone CDs. And
[14:41:55] since we have multiple CDs, then it is
[14:41:57] standalone multiple CDs. That's it. It's
[14:42:00] simple. So now let's check the syntax of
[14:42:02] the multiple standalone cities. So we're
[14:42:04] going to start writing our first city.
[14:42:06] So it start with the with clause and
[14:42:08] then we have the city name and then the
[14:42:10] logic of our city. So nothing new. This
[14:42:12] is how we define the city. And then in
[14:42:15] order to use it, we're going to have our
[14:42:16] main query where we select from our new
[14:42:19] city and we make sure we are using the
[14:42:21] name of our city. So nothing new. Now in
[14:42:24] order to add another city to our query,
[14:42:27] what we're going to do, we're going to
[14:42:28] go after the definition of the city. And
[14:42:31] below it, we're going to go and start
[14:42:33] defining the city too. But this time, as
[14:42:36] you can see, we are not using the width
[14:42:38] clause. We are using a comma. So that
[14:42:40] means only the first city going to be
[14:42:43] using the with clause in order to tell
[14:42:45] SQL we are talking about CTE. All the
[14:42:48] other CDEs you're going to separate it
[14:42:50] using the comma. So the syntax going to
[14:42:52] be comma instead of with then the name
[14:42:55] of the CTE and then we're going to say
[14:42:57] as the following definition. So we're
[14:42:59] going to write here the query of the
[14:43:01] second CTE. So now of course if you want
[14:43:03] to go and add more CTE you go and use
[14:43:06] the comma below it and as well you
[14:43:08] define the third city. So you can have
[14:43:10] as much cities as you want and always
[14:43:13] separate it with comma but only the
[14:43:15] first city start with the width. And of
[14:43:17] course in the main query we can go and
[14:43:18] use the results from the city 2 where we
[14:43:21] are for example here joining the data
[14:43:23] between the city 1 and city 2. So as you
[14:43:25] can see in the main query here we are
[14:43:27] like collecting the data from these
[14:43:28] different cities in order to do the
[14:43:31] final step in the main query. It start
[14:43:34] with the width. So SQL understands okay
[14:43:36] now we are talking about CTE and once
[14:43:38] SQL sees after the parenthesis a comma
[14:43:40] SQL can understands okay now we are
[14:43:42] talking about another city and now if
[14:43:44] you don't go and use a comma after the
[14:43:46] parenthesis SQL can understands okay we
[14:43:49] don't have any more CDEs the next query
[14:43:51] it's about the main query so this is how
[14:43:54] you create multiple standalone CTE all
[14:43:56] right so now back to our task where we
[14:43:58] are creating like a report step by step
[14:44:00] so now we have in the task a second step
[14:44:02] where it says find the last order date
[14:44:06] for each customer. So now we have to go
[14:44:07] and add one more information about our
[14:44:09] customer. So when the last time the
[14:44:12] customer did order. So how we going to
[14:44:13] do it? Now we have to add this to our
[14:44:16] query. And I would like to use as well
[14:44:18] the CTE in order to have this logic. So
[14:44:20] as we learned from the first task, this
[14:44:22] is the first step in order to find the
[14:44:25] total sales for each customer. And here
[14:44:27] we have the main query. Now I would like
[14:44:29] to put now in between another CTE. And
[14:44:32] as we learned from the syntax, we have
[14:44:34] to go and add a comma. We cannot go and
[14:44:36] use the width again. And we have to give
[14:44:38] it a name. So let's call it CTE and last
[14:44:42] order. So latex and we have to define
[14:44:44] it. So as and then double parenthesis.
[14:44:48] And now in between we have to go and add
[14:44:49] our logic. So now we have to focus only
[14:44:51] in this logic. So forget about the other
[14:44:54] CTE and the main query. So we have to
[14:44:57] find the last order date for each
[14:44:59] customer. So we're going to go and query
[14:45:01] again the table orders. So what do we
[14:45:04] need? We need the customer ID. We need
[14:45:06] the order
[14:45:08] date from our table sales orders. So
[14:45:13] that's it for now. Let's just select it
[14:45:16] and execute it. And now with that you
[14:45:18] can see all the customers and as well
[14:45:20] all the orders. But we would like to
[14:45:22] have the highest order for each
[14:45:24] customer. And we can go and use our
[14:45:26] aggregate function, the max function. So
[14:45:29] what we're going to do it's like here at
[14:45:30] the top. So we have to go and use the
[14:45:33] function max and group up by the
[14:45:36] customer ID. So group up the customer
[14:45:41] ID. Uh let me just shift it like this.
[14:45:44] And let's give it the name last order.
[14:45:47] So like this. And as you can see I'm
[14:45:49] just selecting now only my query. I'm
[14:45:52] not selecting everything. And I keep
[14:45:54] executing in order just to check the
[14:45:56] results before we integrate it in the
[14:45:59] main query. So now as you can see we
[14:46:01] have for each customer one row and we
[14:46:03] have as well the highest order for each
[14:46:05] customer. So with that we have solved
[14:46:07] this subtask. So as you can see it's
[14:46:09] really easy to extend. I'm just making
[14:46:11] like another box and I'm adding inside
[14:46:14] it the business logic that I want and
[14:46:16] this going to solve one problem from the
[14:46:18] whole task. So you feel now exactly the
[14:46:21] power of the CTE. We are making complex
[14:46:23] logic but still it's easy to add. Now
[14:46:26] imagine you are not doing this. You are
[14:46:27] always extending one big query. It's
[14:46:30] going to be really hard to extend and
[14:46:31] that's why a lot of SQL developers
[14:46:34] really love using CTE and they like use
[14:46:37] it in each query or in each task that
[14:46:39] they have. So we have solved this task
[14:46:42] and we have to go now integrated in the
[14:46:44] main query. It's going to be very
[14:46:45] simple. So we're going to get over here
[14:46:48] and we will go and just add another
[14:46:50] join. So we're going to join it with the
[14:46:52] city and as you can see SQL now is
[14:46:54] offering it as a table even though it is
[14:46:57] not a physical table that exists in our
[14:46:59] database. It only lives inside our data
[14:47:02] but still SQL treat it as a table. And
[14:47:04] this is exactly what we are doing. We
[14:47:06] treat those informations as table. So
[14:47:08] city the last order and I will call it
[14:47:11] CL. And then of course we have to go and
[14:47:14] do the same condition like here. So the
[14:47:18] CLLO customer ID should be equal to the
[14:47:21] customer ID from the first table, the
[14:47:23] customers. And of course we have to go
[14:47:25] and add this new information to the main
[14:47:27] query. So
[14:47:28] CL the last order. So now what we're
[14:47:31] going to do, we're going to go and
[14:47:33] execute the whole thing. So we have now
[14:47:35] two CDs and as well our main query. So
[14:47:37] let's go and execute it. Now again let's
[14:47:39] check the data. The first three columns
[14:47:41] comes from the physical table customers.
[14:47:44] The fourth one, the total sales comes
[14:47:46] from our first city over here. So from
[14:47:50] here and the last order comes from our
[14:47:52] new city that we just defined the city
[14:47:55] number two. So as you can see guys,
[14:47:56] everything feels like organized and
[14:47:58] structures and we have like flow and of
[14:48:01] course those cities are standalone
[14:48:03] cities. So we can go always and select
[14:48:06] the city and execute it separately. It
[14:48:08] doesn't need anything else from outside
[14:48:11] this query. It just needs the tables
[14:48:13] inside your database. So guys again here
[14:48:15] pay attention if you want to add more
[14:48:17] CDs use the comma. You cannot go and use
[14:48:19] for example here I another width. So if
[14:48:21] I execute it I will get an error. So you
[14:48:24] have to separate it with this comma. And
[14:48:25] another mistake that I do frequently
[14:48:27] that I forget and go add here like to
[14:48:30] the last CTE a comma and this happens to
[14:48:33] me if I'm using a lot of CDEs. So if I
[14:48:36] go and do it like this, I will get as
[14:48:38] well an error because the main query
[14:48:41] doesn't need a comma. So the last city
[14:48:43] should not has a comma after the
[14:48:44] parenthesis. So I just removed it and
[14:48:47] execute. So guys with us we have now
[14:48:49] multiple cities inside our
[14:48:54] query. All right. So now what is a
[14:48:56] nested CTE? It is a city inside another
[14:49:00] city. So it's kind of like subqueries, a
[14:49:02] query inside another query. So not only
[14:49:05] a main query can use the result of CTE
[14:49:08] another CTE can use the result from a
[14:49:10] CTE and of course the nested CTE is like
[14:49:12] a main query is depend on other query
[14:49:15] that means you cannot go and select it
[14:49:17] and run it independently from the query.
[14:49:20] So always you have to run the CTE inside
[14:49:22] it first before seeing the result of the
[14:49:25] nested CTE. Okay. So now let's
[14:49:27] understand what this means. Again we
[14:49:28] have our database and we have a city
[14:49:31] query that goes directly to the database
[14:49:33] and queries the data from there and in
[14:49:36] the output we will get the intermediate
[14:49:38] results. And now in this scenario this
[14:49:40] time we will not have only one
[14:49:41] intermediate results because we have
[14:49:43] many different steps. We need another
[14:49:46] intermediate results before everything
[14:49:47] is prepared for the main query. So that
[14:49:50] means we have another step that's going
[14:49:52] to be built up on top of the first
[14:49:55] intermediate results. So that means we
[14:49:57] can have another CTE that's going to be
[14:50:00] quering the results from the first CTE
[14:50:02] and build on top of it another
[14:50:04] intermediate result. So as you can see
[14:50:06] here we have CTE1 and CTE2 and that
[14:50:10] means now we have like two intermediate
[14:50:12] results. And now of course we can go and
[14:50:14] add CTE 3 4 and so on. But now let's say
[14:50:17] that the CTE2 going to prepare the final
[14:50:20] intermediate result for the main query.
[14:50:22] So now the main query going to go and
[14:50:24] query the second intermedator results
[14:50:26] and it's going to do the final step
[14:50:28] where the final result can be presented
[14:50:30] for the user and of course if it is
[14:50:32] needed the main query can access not
[14:50:35] only the second intermediate result from
[14:50:37] the second CTE but also the first
[14:50:40] intermediate result from the CTE1. Now
[14:50:42] we call the first CTE a standalone CTE
[14:50:45] because it doesn't depend on any
[14:50:47] intermediate results. It goes directly
[14:50:49] to the database and gets the data. But
[14:50:52] now since the second city is completely
[14:50:54] depending on the city one. So this time
[14:50:57] we're going to call this CTE a nested
[14:50:59] CTE because we cannot go and execute it
[14:51:02] on its own. It always depends on the
[14:51:04] city one. And of course the main city is
[14:51:06] depending on everything. So as you can
[14:51:07] see we're using the CTE we're going to
[14:51:09] go and build like a chain. So this is
[14:51:11] what we mean with the standalone city
[14:51:14] and nested city. Okay. So now let's
[14:51:16] understand the syntax of the nested
[14:51:17] city. So we start as usual with the
[14:51:20] definition of the first city using the
[14:51:22] with clause and then the name of the
[14:51:24] city and the definition of the city. So
[14:51:26] here it's nothing new. Now we go and
[14:51:29] define the second city as we learned
[14:51:31] using the comma then the name of the CTE
[14:51:34] and the definition. So this is our CTE
[14:51:36] number two. So now the second CTE is
[14:51:39] depending on the results of the first
[14:51:41] CTE. So how we going to do it? It's very
[14:51:43] simple. Now for the CTE number two,
[14:51:46] we're going to select the data from the
[14:51:48] CTE number one. And with that, we are
[14:51:50] making the second city depending on the
[14:51:53] first one. So this means the second CTE
[14:51:55] is getting the data from the first one
[14:51:57] and it's querying the data in order to
[14:51:59] do the second step. And with that we are
[14:52:02] nesting one CTE in another. And the CTE2
[14:52:06] is completely depending on the first
[14:52:08] one. So again we call the first CTE as a
[14:52:10] standalone CTE because it doesn't depend
[14:52:13] on anything. We can execute it on its
[14:52:16] own and it just need the data directly
[14:52:18] from the database. But the second city
[14:52:20] since is completely depending on the
[14:52:22] city number one we call it a nested
[14:52:25] city. So they are very similar. We are
[14:52:27] just selecting the data from the city
[14:52:29] number one. And now comes our main
[14:52:31] query. And of course it's going to go
[14:52:33] and use the data from the second step.
[14:52:36] So it's going to go and select the data
[14:52:37] from the city number two. But it's still
[14:52:40] of course it's not a rule. It can go and
[14:52:42] access the data and select the data from
[14:52:44] the city number one. So this is how we
[14:52:46] can create a nested city in SQL. All
[14:52:49] right guys, back to our project where we
[14:52:51] are creating a report about the
[14:52:52] customers and we would like to add one
[14:52:54] more step. So the task is rank the
[14:52:57] customers based on total sales per
[14:53:00] customer. So this is one more step
[14:53:02] inside our projects and we would like to
[14:53:04] go and use as well the CTEs in order to
[14:53:07] implement this step. So now what do we
[14:53:09] need? We need to rank the customers
[14:53:11] based on total sales for each customer.
[14:53:13] So here like we have two steps. First we
[14:53:16] have to calculate the total sales per
[14:53:18] customer and then we have to go and rank
[14:53:20] it based on this information and of
[14:53:22] course the sales are stores inside the
[14:53:24] orders. So now let's go and start
[14:53:26] implementing the CDE. So we're going to
[14:53:29] have a comma and we're going to call it
[14:53:31] CTE customer
[14:53:34] rank as and then we're going to go have
[14:53:36] the parenthesis and inside it we're
[14:53:39] going to develop now the logic. So first
[14:53:41] we have to go and aggregate the data by
[14:53:43] the total sales. So select customer ID
[14:53:47] and then sum the
[14:53:50] sales from the table sales orders and
[14:53:54] then of course group
[14:53:56] by the customer id. And now I can hear
[14:54:00] you even telling me bar we have already
[14:54:02] done this. We have already this logic.
[14:54:04] So why we are repeating? If we go to the
[14:54:06] first CTE you can see we have already
[14:54:09] done that. And you are totally right. We
[14:54:11] have already the logic. So it makes no
[14:54:13] sense to repeat it again. And if we do
[14:54:15] this then we didn't understood the power
[14:54:18] of the city. So we don't have to repeat
[14:54:20] the same logic and we can reuse the city
[14:54:24] inside another city. So now we don't
[14:54:26] need all those stuff. We can go and
[14:54:28] focus immediately with ranking the
[14:54:30] customers. So first let me just select
[14:54:32] the data from the first city. So I'm
[14:54:35] going to go and select. So what do we
[14:54:37] have? We have customer
[14:54:39] ID and we have total
[14:54:42] sales. And we're going to select it this
[14:54:44] time not from any physical table. We're
[14:54:47] going to select our city. So like this.
[14:54:50] And now what we're going to do, we're
[14:54:51] going to go and select the whole thing
[14:54:53] and execute it. Well, this is the issue
[14:54:56] of nesting cities. Sadly, this CTE is
[14:55:00] completely depending on the first city.
[14:55:02] So we cannot go and execute it on its
[14:55:04] own. And this is of course very annoying
[14:55:07] because each time I execute the query by
[14:55:10] the end of the query SQL gonna go and
[14:55:12] destroy all the CTE. So in the memory we
[14:55:16] will not find the CT and that's why once
[14:55:19] I executed it SQL don't know anything
[14:55:22] about this city. And in order now to see
[14:55:25] the result of this we have always to
[14:55:27] execute as well with it the city that
[14:55:30] I'm using. So what I usually do I go
[14:55:33] over here and make everything in comment
[14:55:36] in the main query and now I can go and
[14:55:39] execute the whole thing and now I will
[14:55:41] see in the output the outcome of this
[14:55:43] nested city. So this is the big
[14:55:46] difference between the standalone cities
[14:55:48] like here and the nested. So now let's
[14:55:50] go back to our task. We have to rank
[14:55:52] those sales based on the total sales. So
[14:55:55] we can go and use the rank function from
[14:55:58] the window function. So rank over and
[14:56:02] now we don't have to partition the data.
[14:56:04] We just want to sort the data by the
[14:56:07] total sales
[14:56:10] descending. So like this the highest
[14:56:12] sales going to get the rank number one.
[14:56:14] So let's go and give it the name as
[14:56:17] customer rank. Now as you can see we
[14:56:19] have a really nice rank beside those
[14:56:21] informations. Customer three has the
[14:56:23] highest sales and customer two has the
[14:56:26] lowest total sales. So with that, as you
[14:56:28] can see, we didn't repeat ourself. We
[14:56:30] just reused another CTE in our current
[14:56:33] city. And this is exactly why this
[14:56:35] technique is very amazing in order to
[14:56:38] reduce redundancies and to reduce the
[14:56:40] complexity of the whole query. So nested
[14:56:43] are annoying to execute, but they reduce
[14:56:46] the redundancies of our code. Now we are
[14:56:48] done with our logic. We tested
[14:56:50] everything. So what we're going to do,
[14:56:51] we're going to go and integrate it in
[14:56:53] our main query. So let me just remove
[14:56:56] the comments from here and let's go and
[14:56:58] add it in the main query. So we will do
[14:57:01] the same thing. We're going to go and do
[14:57:02] a left join with the last city that we
[14:57:05] just created. So let me just call it
[14:57:08] CCR and the same conditions. We are
[14:57:11] always joining on the customer ID. But
[14:57:13] don't forget to rename the alias. So it
[14:57:17] is CCR customer ID equal to the customer
[14:57:21] ID from the first table. And of course
[14:57:23] we have to go and select the new
[14:57:25] information. So CCR dot customer rank.
[14:57:30] And now let's go and execute the whole
[14:57:32] thing. Now as you can see in the results
[14:57:34] those three columns comes from the
[14:57:36] customers table. The total sales comes
[14:57:39] from the first city. The last order from
[14:57:42] the second city and the customer rank
[14:57:44] comes from our nested city that we just
[14:57:47] created. So guys, it is not a simple
[14:57:50] task creating such a reports because it
[14:57:53] involves different aggregations and
[14:57:55] different functions, but our work is
[14:57:57] organized. As you can see, it's very
[14:57:59] simple. We have step one, step two, step
[14:58:01] three, and the main query. And it's
[14:58:03] really easy to add more components to
[14:58:05] our query. Now, I would like really to
[14:58:08] keep practicing using those nested
[14:58:10] queries. So, we have the following task.
[14:58:11] We would like to add one more step in
[14:58:13] our report. segment the customers based
[14:58:16] on their total sales. So I would like to
[14:58:19] implement this as well using CTE. So
[14:58:22] let's go and solve it. We want to go and
[14:58:24] add a new CTE. It's going to be CTE
[14:58:27] customer
[14:58:29] segments as and then we have to go and
[14:58:32] define our logic. Now if you check our
[14:58:35] task, it has two parts. We have to find
[14:58:37] the total sales and then we have to
[14:58:40] segment the customers based on this
[14:58:41] information. So it is something very
[14:58:44] similar to what we have done in the step
[14:58:46] three. So that means we don't have to go
[14:58:49] and calculate again the total sales. We
[14:58:50] have to go and use as well our amazing
[14:58:53] first city. So let's go and do it. What
[14:58:56] do we need? We need the customer ID like
[14:59:00] this. And let's do basic segmentations
[14:59:03] using the case win. So let's say case
[14:59:05] when the total sales if it's higher than
[14:59:10] 100 then let's say the customer going to
[14:59:13] belong to the group high and let's go
[14:59:16] and add another category. If it's not
[14:59:19] higher than 100 if it is higher than
[14:59:24] 50 then the customer going to belong to
[14:59:27] medium. And if the total sales is less
[14:59:30] or equal to 50. So what's going to
[14:59:33] happen? We're going to say else the
[14:59:35] customer belong to the low category. So
[14:59:38] that's it. We're going to have an end
[14:59:40] and let's call it customer
[14:59:43] segments. All right. But of course we
[14:59:45] have to go and select it from a table
[14:59:47] and it's going to be our city. So total
[14:59:51] sales and let's put it in our new city.
[14:59:54] And I would like to test it before like
[14:59:56] putting it inside our main query. That's
[14:59:58] why I will put everything in comments in
[15:00:01] my main query since it is a nested city
[15:00:04] sadly. And we will just go and select
[15:00:06] our new nested city like we have done
[15:00:10] before. So let's go and execute it. Now
[15:00:13] as you can see in the output we have two
[15:00:15] customers with the category high and two
[15:00:18] customers with the medium. But in order
[15:00:20] to make sure that everything working
[15:00:22] perfectly, I would like to go and add
[15:00:23] the total sales just to see the numbers.
[15:00:26] So let's go and execute it. Well, you
[15:00:28] can see everything is correct. So those
[15:00:30] customers having higher than 100 in the
[15:00:33] total sales and those two having higher
[15:00:35] than 50. But let's go and change stuff
[15:00:37] around. I would like to have it like 80
[15:00:39] as a medium just in order to have a low.
[15:00:42] So with that the customer number two
[15:00:44] having a lower sales than 80. That's why
[15:00:46] we are getting the segment low.
[15:00:48] Everything is done and we have segmented
[15:00:49] the users into different categories. So
[15:00:52] I don't need to test anymore. Let's go
[15:00:54] integrate it in our main query. So we're
[15:00:57] going to do the same things over here.
[15:00:58] We're going to say lift join and we're
[15:01:01] going to get our new CTE. So CCS and we
[15:01:07] have to do the join condition. Don't
[15:01:11] forget to change it. And we have to
[15:01:13] select our new nice information. It's
[15:01:15] going to be the customer segments. And
[15:01:17] now we can go and execute the whole
[15:01:20] thing. So we have now like four
[15:01:22] different cities and one main query. And
[15:01:25] now we can see in the output we got all
[15:01:26] three informations from the table
[15:01:28] customers. The first city, the second,
[15:01:30] third and this is our new column that we
[15:01:33] just created. So again we have done this
[15:01:35] using a necessityd like this. Let me
[15:01:39] just add
[15:01:41] it and it was really easy to extend and
[15:01:44] to add to our report. All right guys, so
[15:01:46] with us we have done like a many
[15:01:47] projects where we have analyzed the
[15:01:49] customer information based on different
[15:01:52] aspects from our data and we have done
[15:01:54] it like step by step and now you have
[15:01:56] like a feeling on how to write complex
[15:01:58] SQL queries using the help of the CTE
[15:02:01] and we have done it like step by step.
[15:02:03] So as you can see if you go through the
[15:02:05] scripts you can understand okay it is
[15:02:07] divided into multiple steps and each
[15:02:09] block is responsible for one specific
[15:02:12] problem of the whole report and this is
[15:02:14] exactly the power of the CTE it
[15:02:16] introduce modularity. So each CTE is
[15:02:20] self-contained and talk about one issue
[15:02:23] and this is amazing way on how to
[15:02:25] organize your project using SQL and how
[15:02:27] to structure your work.
[15:02:33] All right, my friends. So, now let's
[15:02:34] have a little break in order to have a
[15:02:35] real talk about the city. But first,
[15:02:37] some
[15:02:39] coffee. And now I can say that I'm
[15:02:41] working with SQL since really long long
[15:02:43] time ago, over 15 years. And I can say
[15:02:46] as well, I have met a lot of SQL
[15:02:48] developers in different projects. And if
[15:02:50] there is one thing that all those SQL
[15:02:52] developers love is the CTE, they love
[15:02:55] using it everywhere. like each time they
[15:02:57] write a query they going to be writing
[15:02:59] SQL CTE and of course it's fine it's not
[15:03:02] a bad thing but the problem with that
[15:03:04] they overuse it of course not all of
[15:03:06] them but a lot of SQL developers overuse
[15:03:09] using the CTE of course the CTE is very
[15:03:12] powerful but with power comes great
[15:03:14] responsibility
[15:03:16] remember with great power comes great
[15:03:20] responsibility so my advice for you
[15:03:22] especially if you are new to the CTS try
[15:03:25] to not add a new CTE each time you are
[15:03:28] doing something new and I saw it a lot
[15:03:30] like for each new calculation for each
[15:03:32] new column they jump immediately and
[15:03:34] create a new CT and what happens at the
[15:03:36] end we can have like massive number of
[15:03:39] CTE inside one query and the developer
[15:03:41] thinks now everything is organized and
[15:03:43] easy to read but believe me it's exactly
[15:03:46] the opposite if you open any code and
[15:03:48] you have a lot of CDEs and especially if
[15:03:51] they are necessities it is impossible to
[15:03:53] understand what is going on even if the
[15:03:56] developer like describe each CTE and the
[15:03:58] task of the CTE, it's going to be really
[15:04:00] hard to understand and as well to read.
[15:04:02] If everything is like nested and you
[15:04:04] have like I don't know 20 cities in one
[15:04:06] query. So it's going to be impossible to
[15:04:08] read and to understand and as well
[15:04:10] you're going to be using a lot of memory
[15:04:12] and you might get bad performance. So my
[15:04:15] advice for you try always as you are
[15:04:17] creating new CDs to think about how
[15:04:19] about to merge two CDEs in one. So it is
[15:04:22] really always important to rethink and
[15:04:24] refactor your CDEs in order to merge it
[15:04:27] into one and to reduce the number of
[15:04:29] CTE. But now if you ask me how many CTEs
[15:04:31] are okay in one query, well I don't have
[15:04:34] a magic number for that. But normally I
[15:04:36] tend to say between three and five CTE
[15:04:39] it's fine. So it's going to be easy to
[15:04:41] understand and to read and so on. But
[15:04:43] once you get more than five CTE then you
[15:04:46] have to rethink your code. Maybe you
[15:04:48] have to create another complete query so
[15:04:51] you don't have to put everything in one
[15:04:52] query. So this is my advice for you. Try
[15:04:55] to not overuse the CTEs in your
[15:04:57] projects. Not for each step always
[15:04:59] refactor the CTE, consolidate them and
[15:05:02] try to not have more than five CTEs in
[15:05:04] one query. So that's my advice for you.
[15:05:06] Be responsible using the CTE. And let's
[15:05:09] go back to our course.
[15:05:14] So with that we have learned the
[15:05:16] standalone CTE and the NIST CDE and both
[15:05:18] of them belongs to a type called
[15:05:21] nonrecursive CTE. So what is a
[15:05:23] non-recursive CDE? It means it is a city
[15:05:26] that is executed only once. So there is
[15:05:29] no repetitions or looping or anything.
[15:05:31] So the SQL going to execute it in one go
[15:05:33] and that's it. But in the other hand the
[15:05:35] recursive city is exactly the opposite.
[15:05:38] So a recursive city it is a
[15:05:40] selfreferencering query that repeatedly
[15:05:43] processing the data until a certain
[15:05:46] condition is met and we usually use the
[15:05:48] recursive city if we have like
[15:05:50] hierarchical structure and we want to
[15:05:52] navigate and travel through the
[15:05:54] hierarchy. I know this might be
[15:05:56] confusing but don't worry about it.
[15:05:57] We're going to have very simple
[15:05:58] examples. Now again we have our tables
[15:06:00] in the database and we have a CTE. Now
[15:06:03] the query of the CTE going to be
[15:06:04] executed for the first time and in the
[15:06:07] results we're going to have the initial
[15:06:08] data from the CTE but it is not
[15:06:11] everything yet. Now this intermediate
[15:06:13] result is not ready yet for the main
[15:06:15] query but instead of that it's going to
[15:06:18] go back to the CTE and CTE going to
[15:06:21] check whether the current results is
[15:06:23] meeting a specific condition. So now if
[15:06:26] the check says no it's not meeting the
[15:06:29] condition what's going to happen the
[15:06:31] city query going to be executed for the
[15:06:33] second time. So as you can see we are
[15:06:35] looping through the CTE. Now the result
[15:06:38] of the second iteration the second
[15:06:40] execution will be added to the
[15:06:43] intermediate result. So now the
[15:06:44] intermediate result has more data and
[15:06:47] again before we can use it from the main
[15:06:49] query it going to be checked from the
[15:06:51] CTE. Does the result fulfill the
[15:06:54] condition? If it's still no, then go and
[15:06:57] execute the CTE again. So we're going to
[15:06:59] have a third iteration and a new data
[15:07:01] going to be added to the intermediate
[15:07:03] result. So this is our third iteration.
[15:07:06] Now it's going to be checked again from
[15:07:07] the CTE. Did we fulfill the condition?
[15:07:10] If the answer is yes, then the loop
[15:07:13] going to break and everything else. So
[15:07:15] there will be no fourth iteration of the
[15:07:16] CTE. So with that, the CTE says okay,
[15:07:19] I'm done. This is the final result of
[15:07:22] the intermediate result. then the loop
[15:07:24] going to break and everything ends and
[15:07:27] the city will not be executed for the
[15:07:29] first time and now the city going to say
[15:07:31] okay I'm done now my intermediate result
[15:07:34] is ready to be used from the main query
[15:07:36] and now nothing new happens the main
[15:07:38] query going to go and retrieve the data
[15:07:40] from the intermediate results and do
[15:07:42] some magic in order to prepare the final
[15:07:44] results so that means there will be no
[15:07:46] iterations or looping inside the main
[15:07:48] query the looping going to be happen
[15:07:50] only in the CTE and that's why we call
[15:07:52] it recursive CTE. So now if you compare
[15:07:55] it with the other types, all other types
[15:07:57] are always in one direction and all the
[15:07:59] CTE is going to be executed only once
[15:08:02] but the recursive CTE going to be keep
[15:08:04] looping until the condition is met and
[15:08:07] only then it's going to forward the data
[15:08:09] to the main query. And normally we use
[15:08:11] the recursive CTE if you are navigating
[15:08:14] through hierarchical structure. So if
[15:08:16] you have in your data like hierarchal
[15:08:17] structures, you can go and use the
[15:08:19] recursive CTE in order to navigate
[15:08:21] through it. So this is the recursive
[15:08:24] city. Okay. So now let's check the
[15:08:25] syntax of the recursive CTE. It is a
[15:08:27] little bit complicated but we're going
[15:08:28] to do it step by step. So what do we
[15:08:30] have? We have a query and we would like
[15:08:32] to put it in a city. So we're going to
[15:08:34] have the usual stuff with clause the
[15:08:37] name of the city and as and then the
[15:08:40] query. So this is the definition of our
[15:08:42] city. But now if you leave it like this
[15:08:44] SQL going to execute it only once. But
[15:08:46] we would like to make a loop iteration.
[15:08:48] So in order to do that we have to go and
[15:08:50] define a second select statement inside
[15:08:53] our CTE like this. So we are selecting
[15:08:56] the data and here we have to define a
[15:08:58] breaking condition. So here in the
[15:09:00] second query we are defining a condition
[15:09:02] in order to break the loop otherwise
[15:09:05] it's going to loop for infinite or the
[15:09:07] system going to break. You could use it
[15:09:08] in the wear clause or you can use it
[15:09:10] even in an inner join because both of
[15:09:12] them are filtering the data and you can
[15:09:14] use it in order to break the condition.
[15:09:16] All right. So now still there is
[15:09:18] something missing. How we going to make
[15:09:19] like things looping? Well, we have to
[15:09:21] reference this CTE to itself. So what we
[15:09:24] going to do? We're going to say the
[15:09:26] second query going to select the data
[15:09:28] from the same CTE. So that means we have
[15:09:31] now a query that is quering itself. And
[15:09:33] this is of course what we want. We want
[15:09:35] to make iterations and we want to make a
[15:09:37] loop. That's why we have to go and
[15:09:39] reference it to itself. And now in SQL
[15:09:41] you cannot have it like this. You cannot
[15:09:43] have like two select statements in one
[15:09:45] query. you have to connect it somehow.
[15:09:47] That's why we can go and use the union
[15:09:49] all or union depend if you want to have
[15:09:51] duplicates or not. So now we call the
[15:09:53] first query the anchor query. The anchor
[15:09:56] query going to be the first query that
[15:09:58] interacts with the database and provide
[15:10:00] us the initial intermediate results. So
[15:10:03] it is the starting point of the
[15:10:05] iteration and we can say it is the first
[15:10:08] step in the process. So this going to be
[15:10:10] executed only once and it going to
[15:10:12] provide us the initial step the first
[15:10:14] step in the process. Now we call the
[15:10:16] second step as a recursive query and we
[15:10:19] call it like this because this query
[15:10:21] going to be executed multiple times and
[15:10:24] it will keep repeating and add data to
[15:10:26] the intermediate results until the
[15:10:29] condition is met or let's say there will
[15:10:31] be no more data that is available to be
[15:10:34] processed. So this is the syntax of the
[15:10:36] city query for the main query nothing is
[15:10:38] changed. So we have to go and use the
[15:10:41] city name in the main query. So this is
[15:10:44] the syntax of the recursive city. So
[15:10:47] think about it like this. SQL going to
[15:10:48] go and execute the anchor query only
[15:10:51] once and then after that going to go
[15:10:53] through the recursive query and keep
[15:10:56] looping and looping and iterating until
[15:10:59] a certain condition is met and then SQL
[15:11:02] going to go out from the CTE. So this is
[15:11:04] actually what we mean with the anchor
[15:11:05] and recursive queries. All right. Right.
[15:11:07] So now let's have a simple task in order
[15:11:09] to understand the recursive city. So the
[15:11:11] task says generate a sequence of numbers
[15:11:14] from 1 to 20. So now let's do it step by
[15:11:17] step. So that means we have to create a
[15:11:19] loop from 1 to 20 and after 20 the loop
[15:11:22] should stop. So let's go and do it. Now
[15:11:25] the first step of the recursive CTE is
[15:11:27] to build the anchor query. So the anchor
[15:11:30] query is responsible for the first
[15:11:32] iteration. So that means the first row
[15:11:34] of the output. So what is the first
[15:11:36] value between 1 and 20? It is the one.
[15:11:39] So let's go and write a query that
[15:11:41] generate the value one. So select and
[15:11:45] we're going to say one as I'm going to
[15:11:47] give it the name my number. So that's
[15:11:51] it. Let's go and execute it. Now you can
[15:11:53] see in the output we have the first
[15:11:55] member of our sequence. And this is
[15:11:57] exactly the task of the anchor query. It
[15:12:00] retrieves the first step in the
[15:12:02] iteration. So let's go and call
[15:12:05] it
[15:12:07] anchor query. Now the next step with
[15:12:10] that we have to go and build the
[15:12:11] iteration. So we need a CTE. So I will
[15:12:14] build now the city. So we're going to
[15:12:16] say with we're going to call it series
[15:12:19] and then we're going to put everything
[15:12:21] in parenthesis and then we're going to
[15:12:23] go to the main query. So this is the
[15:12:25] main query and we will go and select
[15:12:28] everything from the Sirius the city. So
[15:12:32] let's go and execute it just to make
[15:12:34] sure that everything is working fine. So
[15:12:36] we didn't create any loop or anything.
[15:12:38] We have just created a city on top on
[15:12:41] the anchor query and we just call it
[15:12:43] from the main query. So now we come to
[15:12:45] the second step of building the
[15:12:47] recursive city. We have to build the
[15:12:49] recursive query. So let's do it. I will
[15:12:52] just make this little bit smaller. And
[15:12:53] now before we start writing the query,
[15:12:55] we have to go and use union
[15:12:58] all in order to go and connect the
[15:13:00] anchor query with the recursive query.
[15:13:03] And let me say this is the
[15:13:05] recursive query. So how we going to
[15:13:08] build it? Let's go and start with the
[15:13:10] select. And now next what I usually do I
[15:13:12] just make sure that we are making a
[15:13:14] recursive city. So I go with selecting
[15:13:16] from and then we're going to use the
[15:13:19] name of the current city so that we are
[15:13:21] referencing the city to itself in order
[15:13:23] to make the city recursive and to do the
[15:13:26] looping. Now here comes the tricky part.
[15:13:29] So we need to create like the sequence.
[15:13:32] Now what is the current value? The
[15:13:33] current value is one. Right? Now what do
[15:13:35] we need? We need the second value in the
[15:13:37] sequence which is two. So we can do it
[15:13:40] by 1 + 1. So if you do it like this you
[15:13:43] will get the output two. But actually
[15:13:45] what we are doing here we are always
[15:13:47] taking the current value and we are
[15:13:49] saying plus one in order to generate the
[15:13:52] next value. So in order to do that
[15:13:54] instead of saying one we're going to
[15:13:56] take the my number the current value and
[15:13:58] we're going to add to it plus one in
[15:14:01] order to generate the second value in
[15:14:03] the sequence. So that means my number
[15:14:05] always holds the current value and we do
[15:14:09] the operation + one in order to generate
[15:14:11] the next sequence. So having it like
[15:14:14] this what we are doing we are generating
[15:14:16] the sequence of numbers. Now if you go
[15:14:18] and execute it like this let me just
[15:14:20] execute it what will happen it going to
[15:14:23] breaks because SQL will not allow it and
[15:14:25] SQL set it to 100 iterations. So more
[15:14:28] than 100 SQL going to break the query so
[15:14:30] that we don't have infinite number of
[15:14:33] looping. So this is bad because we
[15:14:35] didn't define the breaking mechanism of
[15:14:37] the looping. So now we have to define as
[15:14:39] well in the recursive query how the loop
[15:14:42] going to ends and we usually use a
[15:14:44] condition. For example, we can go and
[15:14:45] use the wear clause and we can say okay
[15:14:48] keep looping and keep generating but
[15:14:50] always check whether the value of the my
[15:14:53] number is less than 20. And you might
[15:14:58] ask okay it should be less or equal to
[15:15:00] 20 right? Well no because if you are
[15:15:03] making less and equal to 20 what going
[15:15:05] to happen once the my number is equal to
[15:15:07] 20 you are allowing one more iterations
[15:15:10] where you will get in the output 21. So
[15:15:13] that's why we are making it with 20. So
[15:15:15] now let's go and execute it and let's
[15:15:18] check the sequence. It start with 1 2 3
[15:15:20] 4 5 and until we reach the 20. So with
[15:15:23] that we have solved the task. Again here
[15:15:25] it's not that hard right? We are just
[15:15:28] providing the initial step and then we
[15:15:30] are providing the loop where we are
[15:15:32] defining inside it how the loop going to
[15:15:34] ends. Now there is one more thing that
[15:15:36] you can do with the recursive CTE is to
[15:15:38] define the limit of iterations. So for
[15:15:41] example in your code if you say okay if
[15:15:43] this iterates more than 10 times then
[15:15:46] the SQL should breaks and stops. So you
[15:15:49] can define for the SQL the maximum
[15:15:51] number of recursions. So how we can do
[15:15:54] that? We can do that in the main query.
[15:15:56] So if you go over here and say option
[15:15:59] then two parenthesis and then max
[15:16:02] recursion and after that you can define
[15:16:05] the limit. So for example let's go with
[15:16:07] the 10. Now of course we are iterating
[15:16:09] in our code now more than 20 but here we
[15:16:12] are making the rule it should not
[15:16:13] iterate more than 10. So let's go and
[15:16:16] execute it. So now we can see that our
[15:16:17] SQL breaks and it says the maximum
[15:16:20] recursion is 10. So as you can see now
[15:16:22] in the output we are getting the error
[15:16:24] of having more than 10 iterations which
[15:16:26] is not allowed. So with that you can
[15:16:28] control how many recursions you can
[15:16:30] have. Let's say that you would like to
[15:16:31] have like thousand iteration. So if you
[15:16:34] go over here and say you know what I
[15:16:35] would like to have a sequence of 1,000.
[15:16:37] If you let me just comment this out. So
[15:16:40] if you execute it you will get an error
[15:16:42] because the default is 100. But of
[15:16:45] course you can go and increase the
[15:16:47] maximum recursion. For example let's go
[15:16:49] with 5,000s. in the output it will work
[15:16:52] and you will get a sequence of 1,000. So
[15:16:54] with this you can control how many
[15:16:56] iterations are allowed in your query. So
[15:16:58] that you have like a control on it.
[15:17:00] Okay. So now we can understand step by
[15:17:02] step how SQL executed the recursive
[15:17:04] query. And here we have like flow
[15:17:06] diagram in order to understand the
[15:17:08] process the steps of executing the
[15:17:10] recursive query. So let's go and do it.
[15:17:12] Now in the start we have the first step
[15:17:14] is to run the anchor query. So our
[15:17:17] anchor query is just a select for the
[15:17:19] value one. So in the output we will get
[15:17:22] the value one in my number and as you
[15:17:24] can see the anchor query going to be
[15:17:26] executed only once. So there is no
[15:17:29] iterations or anything. SQL executed
[15:17:31] once and then goes to the next step. So
[15:17:34] what is the next step? It's going to
[15:17:35] execute the recursive query. So it's
[15:17:38] going to go over here and now what going
[15:17:39] to happen? We will get the current value
[15:17:41] of my number. The current value is one.
[15:17:44] and then we're going to add to it a one.
[15:17:46] So 1 + 1 we will get from the recursive
[15:17:49] query the two which is added to our
[15:17:52] results. Now it's going to check the
[15:17:54] condition is my number now smaller than
[15:17:58] 20. Well yes it's smaller than 20 and
[15:18:01] what's going to happen since it's true
[15:18:03] is going to go and reexecute the
[15:18:06] recursive query. So now we are doing the
[15:18:08] second iteration. So again it's going to
[15:18:10] go to the recursive query and going to
[15:18:12] say okay what is the current value of my
[15:18:14] number? It is two. So 2 + 1 the second
[15:18:18] iteration will give us the value three.
[15:18:21] So as you can see each time the
[15:18:22] recursive query is executed it is adding
[15:18:25] more values to our result. So the same
[15:18:28] question can be asked is now my number
[15:18:31] smaller than 20. Well yes it is smaller.
[15:18:34] Well what can happen is still going to
[15:18:36] reexecute the recursive query. So SQL
[15:18:39] going to keep looping and iterating and
[15:18:41] adding values to the output until we
[15:18:45] reach the value 20. So now SQL going to
[15:18:48] ask is 20 my number now smaller than 20.
[15:18:52] Well no. So it is false and what's going
[15:18:55] to happen the chain will break and we
[15:18:57] will not loop anymore. So it's going to
[15:19:00] be the end of the city and this going to
[15:19:02] be the final results that's going to be
[15:19:04] used from the main query. So this is how
[15:19:07] SQL executed this recursive CD. Okay. So
[15:19:10] now let's have another task for the
[15:19:11] recursive CD. This time it's going to be
[15:19:13] a little bit more advanced. So the task
[15:19:15] says show the employee hierarchy by
[15:19:18] displaying each employees level within
[15:19:20] the organization. So that means we have
[15:19:22] to show for each employee for each row a
[15:19:25] level that tells us the hierarchy of the
[15:19:27] employee. So first let's go and explore
[15:19:30] the table employees. So let's go and
[15:19:32] select everything prompt sales
[15:19:36] employees. Okay, let's go execute it. So
[15:19:39] now by looking to the results we have
[15:19:40] like few informations about the
[15:19:42] employee. We have information about
[15:19:43] which department the gender salaries but
[15:19:46] here we have the key. It is the manager
[15:19:48] ID. So this is like self referencing to
[15:19:51] the same table. So for example the first
[15:19:54] employee the value is null. That means
[15:19:56] this employee has no manager which makes
[15:19:59] this employee like the big boss, the
[15:20:01] CEO. Then now by looking to the next two
[15:20:04] employees, they have a manager ID one.
[15:20:06] So who is the manager of those two? It's
[15:20:09] going to be the first row, the manager
[15:20:11] ID number one. So the manager ID number
[15:20:13] one is the post of those two employees.
[15:20:16] And then for the fourth one, we can see
[15:20:18] the manager ID number two. So the
[15:20:20] manager of Michael is actually Kevin,
[15:20:23] the second row. And for Carol the
[15:20:25] manager ID is three. That means Mary is
[15:20:28] the manager of Carol. And this is
[15:20:30] exactly what we can do with the
[15:20:32] recursive CTE. We can use such
[15:20:34] informations in order to create like a
[15:20:36] loop. So let's go and do it step by
[15:20:38] step. First we're going to start with
[15:20:40] the anchor query as usual. So this is
[15:20:44] the anchor query and here the first step
[15:20:47] or the first record going to be the
[15:20:49] highest manager which is the CEO, right?
[15:20:52] The first record. So in order to select
[15:20:55] now the only the first record what we
[15:20:57] can say we can say where manager id is
[15:21:03] null. So let's go and execute it. And
[15:21:06] with that we have now the first row and
[15:21:08] we can use this as the first step in our
[15:21:10] iteration. So now let's go and pick few
[15:21:11] informations in the select like the
[15:21:13] employee ID and the first name and as
[15:21:17] well let's go and get the manager ID.
[15:21:19] And now we have to start creating the
[15:21:21] levels. Right? So this is the first
[15:21:24] level. So I'm going to have the value
[15:21:25] one as let's have it like level. So our
[15:21:29] CEO has the level number one. So let's
[15:21:32] go and execute it. So now as you can see
[15:21:34] Frank is the CEO and he is in the level
[15:21:38] number one. So this is our anchor query.
[15:21:40] Now we have to do the iteration right.
[15:21:43] So we have to go and start creating the
[15:21:44] city. So let's call it with CD employee
[15:21:50] hierarchy and then as and then this is
[15:21:54] the definition of our CD. So let me just
[15:21:57] make it like this. And of course what do
[15:21:59] we need? We need the main query.
[15:22:02] So main query we will select
[15:22:07] everything from our new city like this.
[15:22:11] So let's go and test it. All right. So
[15:22:14] now we have prepared the CTE and the
[15:22:16] main query and of course the next step
[15:22:17] with that we're going to go and build
[15:22:19] the recursive query but first we need
[15:22:21] the union all in order to connect the
[15:22:25] two queries and recursive query and now
[15:22:29] we can start building the logic. So now
[15:22:31] we want to find all the employees where
[15:22:33] their manager is the employee ID number
[15:22:36] one right because they going to have the
[15:22:38] second level in the hierarchy. So what
[15:22:40] we're going to do, we're going to go and
[15:22:41] select and we need the same stuff. So we
[15:22:44] would like to get the employee ID, the
[15:22:46] first name, and the manager ID. And we
[15:22:48] need the level. So this going to be the
[15:22:50] level number two. It's not correct yet.
[15:22:52] I'm just want to show what this means
[15:22:55] because we need to get the employee ID
[15:22:57] and the first name and so on. We cannot
[15:22:59] get it yet from the CT because in the
[15:23:01] city we have only one employee. So we
[15:23:03] still have to go to the database and
[15:23:05] grab the next employees. So now I will
[15:23:08] give this as an alias like E and I will
[15:23:11] select it as well from those employees.
[15:23:13] So so far we are not doing any recursive
[15:23:16] yet right in the recursive query we're
[15:23:18] still querying the database but now we
[15:23:21] don't need all the employees from this
[15:23:23] table we need all the employees where
[15:23:25] the manager ID equal to one right now.
[15:23:28] Of course, in order to get those
[15:23:29] employees where the manager equal to
[15:23:32] one. So we can do it with the workclouds
[15:23:34] for example and say manager ID equal to
[15:23:38] one. Let me just select this and query
[15:23:40] it. Now we will get those two employees
[15:23:43] where their manager is the CEO the top
[15:23:45] manager. But of course we cannot do it
[15:23:47] like this. What we're going to do we're
[15:23:49] going to join this table with our
[15:23:51] current CTE in order to make a loop. So
[15:23:54] let me show you what I mean. We will
[15:23:55] remove this. We're going to use the
[15:23:57] inner join and we're going to reference
[15:24:01] it from the CTE and let's give this the
[15:24:04] name C H and we connect it like this. So
[15:24:08] on we're going to say the manager ID of
[15:24:11] the employee should be equal to the
[15:24:14] employee ID. So the employee ID at the
[15:24:17] start going to be the number one. So
[15:24:19] it's going to be like
[15:24:20] this employee ID. Now we are connecting
[15:24:24] the manager ID with the employee ID and
[15:24:27] we are as well reusing the CD inside
[15:24:30] itself in order to make the iterations
[15:24:32] and here we don't need the work clause
[15:24:34] because the inner join going to filter
[15:24:36] the data automatically as we learned the
[15:24:38] inner join going to show only the
[15:24:40] matching rows from the left and to right
[15:24:42] so that mean there will be filtering. So
[15:24:44] we are almost there but of course we
[15:24:46] don't want to show it as a two. What
[15:24:48] we're going to do, we're going to show
[15:24:49] it like this. Level + one. So the
[15:24:52] current level is one. The second
[15:24:54] iteration going to be two. And the third
[15:24:56] iteration going to be three. So I think
[15:24:58] we have everything for our iteration.
[15:25:01] Let me just check and make this smaller.
[15:25:04] Now again we have here our anchor query.
[15:25:06] This is only for the top level manager.
[15:25:08] And then here we are just connecting the
[15:25:11] managers with the employees. And we are
[15:25:13] reusing the CTE in order to make the
[15:25:16] effect of the loop. And as well we are
[15:25:18] using the inner join in order to break
[15:25:20] the loop once there are no more rows to
[15:25:23] process. So let's go and execute it. Now
[15:25:26] let's check the output. This is our top
[15:25:28] manager. So level one. This information
[15:25:30] comes from the anchor query. Then the
[15:25:32] second iteration it is the employees
[15:25:35] where the manager ID equal to one. So
[15:25:38] it's going to be those two employees. So
[15:25:40] those employees in our hierarchy are the
[15:25:43] second level in our organization. And
[15:25:45] then we're going to search for employees
[15:25:47] where their manager ID is equal to
[15:25:50] either two or three. And this is going
[15:25:52] to be those two employees, Carol and
[15:25:54] Miracle. And now to the third iteration,
[15:25:57] we're going to search for all employees
[15:25:58] where their manager ID equal to either
[15:26:01] two or three. And now to the third
[15:26:03] iteration, we're going to search for all
[15:26:05] employees where their manager ID equal
[15:26:07] to either two or three. And this going
[15:26:10] to result having those two employees
[15:26:12] because their manager ID is equal to
[15:26:14] three or two and they're going to get
[15:26:16] the level of three. And then after that
[15:26:18] SQL going to try to search for employees
[15:26:20] where their manager ID equal to five and
[15:26:23] four and SQL will not find anything and
[15:26:25] that's why it kind of breaks. So with
[15:26:28] that we have solved the task. All right.
[15:26:29] I totally understand if this is
[15:26:31] complicated but now we're going to do it
[15:26:33] step by step in order to understand how
[15:26:35] SQL executed this and why we have done
[15:26:38] it in this way. So again we have our
[15:26:39] flow diagram. We start by running the
[15:26:42] anchor query then the recursive query
[15:26:45] and then we have a check. If the check
[15:26:47] fails we iterate otherwise we end. So
[15:26:50] let's do it step by step. Here we have
[15:26:51] the table employees and beneath it we
[15:26:54] have the result of the city. So the
[15:26:56] first step it says we run the anchor
[15:26:58] query and we run it only once. So it's
[15:27:00] going to go to the anchor query and
[15:27:02] start executing it. So here we are
[15:27:04] selecting from the table employees but
[15:27:06] we are making a filter on the manager
[15:27:08] ID. So the manager ID should be null. So
[15:27:11] that means we will get the record of
[15:27:14] Frank and Frank going to be at the
[15:27:16] output and we are saying the level of
[15:27:19] this employee is one. So we will have
[15:27:21] here at the level one. So this is the
[15:27:24] output of the anchor query and that's
[15:27:26] it. This will never be executed. Now we
[15:27:28] go to the next step. Now we will run the
[15:27:31] recursive query. So what's going to
[15:27:33] happen in the recursive query we are
[15:27:35] saying okay I would like to select as
[15:27:36] well data from the employees and join it
[15:27:39] with the city results but the join
[15:27:41] should be an inner join so only the
[15:27:43] matching data between the CTE and the
[15:27:45] employees and now comes the join
[15:27:48] condition and this is the key for this
[15:27:50] iteration we are saying the manager ID
[15:27:52] of the employee should be matching to
[15:27:55] the employee ID from the CTE. So SQL
[15:27:58] going to go and join the table with the
[15:28:00] CTE. So now we have here only employee
[15:28:02] number ID one. So it's still going to do
[15:28:04] it step by step searching for any
[15:28:06] matches. So for the first one we don't
[15:28:09] have a match because the manager ID is
[15:28:11] not equal to one. So that's why it will
[15:28:13] not be included in the result. The
[15:28:16] second row here the manager ID is equal
[15:28:18] to one and this is a match with the
[15:28:20] employee ID. So SQL going to take it and
[15:28:23] put it at the output. Not only that, SQL
[15:28:26] going to increase the level. So we have
[15:28:28] here the current value is one. So level
[15:28:31] + one. What can happen? We will get the
[15:28:33] value two. We are still in the same
[15:28:35] iteration. We are not iterating yet. So
[15:28:37] this is the first iteration of the
[15:28:40] recursive query. So until the whole join
[15:28:43] is done to the next row, we have a match
[15:28:46] as well because the manager ID is equal
[15:28:48] to one. And we're going to have the same
[15:28:51] thing. The level going to be as well too
[15:28:53] because the value of the level didn't
[15:28:54] change. It's still the current value is
[15:28:56] equal to one. And this going to keep
[15:28:58] going. So two, three, we don't have any
[15:29:00] matches. And with that, SQL is done
[15:29:03] executing the recursive query. All
[15:29:05] right. So now the SQL going to say,
[15:29:07] okay, did we process everything? Well,
[15:29:10] no. We still have missing output. We
[15:29:12] still have missing employees. That's why
[15:29:14] we didn't fulfill the condition. And
[15:29:16] we're going to run this again. So now in
[15:29:18] the second iteration, it's going to join
[15:29:20] as well again the city result with the
[15:29:23] employees by matching the manager ID and
[15:29:26] the employee ID. But this time it's
[15:29:27] going to focus only on those two ids. So
[15:29:30] the two and three. So SQL going to go
[15:29:32] and find any matching where the major ID
[15:29:35] equal to two or three. So it's going to
[15:29:38] do it step by step. The first one is
[15:29:40] not. The second one is as well not. The
[15:29:43] third one is not because the manager ID
[15:29:45] is one. But now to the employee number
[15:29:49] four we have a match. So it's still
[15:29:51] going to take this one and put it in the
[15:29:54] output like this. And now in this
[15:29:56] iteration what is the current level? It
[15:29:58] is two but we add to it one that's why
[15:30:01] we will get in the output three. And
[15:30:04] then SQL keep going. So we have here the
[15:30:06] employee number five and the manager ID
[15:30:09] is equal to three. So what happens? SQL
[15:30:11] takes it as well and put it in the
[15:30:14] output as the result of the CTE and as
[15:30:17] well the current level is two + one.
[15:30:20] We're going to have as well three. So
[15:30:22] with that SQL done joining the tables
[15:30:25] and going to ask again did we process
[15:30:27] all employees? Well yes it's true that
[15:30:30] means we don't have to do any more
[15:30:32] iterations because if you do any
[15:30:34] iterations SQL will not find anything.
[15:30:37] So for example if you go over here let
[15:30:39] me just remove this and let's say we are
[15:30:41] joining with the four and five. So what
[15:30:43] can happen isql going to search in the
[15:30:45] manager's ID for four and five and it
[15:30:48] will not find anything. So that means we
[15:30:50] will not be adding anything to the CTE.
[15:30:52] That's why SQL stops. So we have a
[15:30:54] complete results and we have now all the
[15:30:56] data from the employees in the output
[15:30:58] and this results going to be passed to
[15:31:00] the main query. So this is why we have
[15:31:02] done it like this and this is how
[15:31:04] executed this recursive query. I would
[15:31:07] like to visual for you what this means
[15:31:09] the level or the structure of the
[15:31:11] organization. So the hierarchy looks
[15:31:13] like this. The level one the top manager
[15:31:15] is Frank. So this is the level number
[15:31:19] one. And then we go to the level number
[15:31:21] two. So we have those two employees. So
[15:31:24] we have Kevin. So this is the level
[15:31:26] number one. And then we have two
[15:31:28] employees Kevin and Mary at the level
[15:31:31] two. So they work together and their
[15:31:35] boss is Frank. So it's going to look
[15:31:38] like this. And they are at the level
[15:31:41] two. We have then Michael that directly
[15:31:44] reports to who? To Kevin because here we
[15:31:47] have the employee ID two and two. So we
[15:31:50] have one employee here and as well Carol
[15:31:52] is as well at the level three and she
[15:31:55] reports to Mary and both Michael and
[15:31:58] Carol are at the level three. So this is
[15:32:01] what we mean with the level. It can help
[15:32:03] us to identify which employee at which
[15:32:05] level in the organization. If you have
[15:32:08] like hierarchy in your data and you can
[15:32:10] see in one table things are referencing
[15:32:13] each others like here the manager ID is
[15:32:16] actually the employee ID. So it's like
[15:32:18] we are referencing those ID to each
[15:32:20] others. This means there is hierarchy
[15:32:23] and there is a structure in this table
[15:32:25] and you can use the recursive city in
[15:32:27] order to build those levels and to
[15:32:30] navigate as well through the hierarchy.
[15:32:33] All right. So that's all for the
[15:32:34] recursive city and with that we have
[15:32:35] covered all the different types of
[15:32:38] cities that we have in
[15:32:42] SQL. So now let's have a quick recap. So
[15:32:45] we have learned that the CTE the common
[15:32:47] table expression is a temporary named
[15:32:50] result like a virtual table that could
[15:32:52] be used from different places in the
[15:32:54] query and we have a lot of advantages
[15:32:56] for the CTE. The main one is it breaks
[15:32:59] the complexity of query into small
[15:33:01] multiple pieces which makes our query
[15:33:04] much easier to read and as well to
[15:33:06] understand. So it improves readability.
[15:33:08] Another advantage of the city is that
[15:33:10] those small multiple pieces they are
[15:33:13] really easy to manage and to develop. So
[15:33:15] those pieces are like self-contained
[15:33:17] which makes our queries more modular. So
[15:33:20] it introduces modularity inside our
[15:33:23] queries. And we also learned that the
[15:33:24] CTE help us to reduce the redundancy
[15:33:27] inside our queries where it makes the
[15:33:30] result of one query usable in multiple
[15:33:33] places inside our query. So it makes our
[15:33:36] code smaller and reduce redundancy. And
[15:33:38] one more advantage of the city is that
[15:33:40] it help us to do looping and iterating
[15:33:43] in SQL by using the recursive CTE. And
[15:33:46] we have understood as well that we can
[15:33:48] treat the CTE result as any other
[15:33:51] physical table inside our database. So
[15:33:53] we can treat it and handle it like any
[15:33:55] other tables. Only one exception that
[15:33:57] this table lives only in one query. So
[15:34:00] we cannot query the CTE from an external
[15:34:03] query. Now we have learned that the
[15:34:05] result of the CTE could be used from the
[15:34:07] main query. This is the classical one.
[15:34:10] But not only we can use it in the main
[15:34:12] query but also we can use it in another
[15:34:15] CTE query which leads to having nested
[15:34:18] cities. And of course we have learned as
[15:34:20] well we can use the result of the CTE
[15:34:23] within itself which makes the CTE
[15:34:25] recursive and allows for looping and
[15:34:27] iterating. And I can only keep
[15:34:29] recommending to not use more than five
[15:34:32] CTEs in one query. Otherwise you're
[15:34:34] going to get the exact opposite and
[15:34:36] benefits from cdes where your code going
[15:34:38] to be really hard to understand and to
[15:34:40] read and even to extend. Okay my friends
[15:34:42] with that we have covered this amazing
[15:34:44] and very important technique in SQL the
[15:34:46] common table expressions the city. Now
[15:34:49] in the next step we're going to talk
[15:34:50] about a new type of objects that you can
[15:34:52] use in databases. We don't have only
[15:34:54] tables we have as well views. And views
[15:34:57] are amazing in order to give you dynamic
[15:34:59] and flexibility in your project. So
[15:35:01] let's talk about
[15:35:06] views. Now a view is not like a query
[15:35:09] that we can use in SQL. It is an object
[15:35:11] that we can find in the database. So
[15:35:14] before we jump immediately to the view,
[15:35:15] I would like to give you the big
[15:35:17] picture, the whole structure of the
[15:35:19] database. So let's go. We have like
[15:35:22] hierarchy structure and the highest
[15:35:24] level of this hierarchy is the SQL
[15:35:26] server. The SQL server manages multiple
[15:35:29] databases. It's like the control center
[15:35:31] that keep everything running and
[15:35:33] accessible. Now inside the SQL server,
[15:35:36] we have multiple databases. So a
[15:35:38] database is collection of informations
[15:35:40] that are stored in structured way. It's
[15:35:43] where all your data is kept and
[15:35:45] organized in different tables and
[15:35:47] objects. And each database is separated
[15:35:49] from others and it has its own data. Now
[15:35:52] inside each database we can find
[15:35:54] multiple schemas. A schema is like a
[15:35:57] logical way on how you group up related
[15:35:59] objects like tables and views together
[15:36:02] within a database. Like for example, if
[15:36:04] you have a database called sales, we can
[15:36:06] group up different tables about the
[15:36:08] orders underneath the schema orders. And
[15:36:11] maybe we have like multiple views and
[15:36:13] tables about the customers where we can
[15:36:15] put it in the schema customers. So if
[15:36:17] you find like multiple tables and views
[15:36:19] that are describing the same object, the
[15:36:22] same topic, we put them all together
[15:36:24] underneath one schema. So again, a
[15:36:26] database could be like the sales
[15:36:28] database and the HR database. They are
[15:36:30] completely different types of data. And
[15:36:33] underneath the sales, we can have like
[15:36:35] different sections. We have the sections
[15:36:36] about the orders and sections about the
[15:36:38] customers. And now moving on, what we
[15:36:40] can find inside the schema, we can find
[15:36:42] tables. A table is where actually your
[15:36:45] data is stored. It contains multiple
[15:36:47] columns and rows. So it is where the
[15:36:49] data physically lives. And now inside
[15:36:51] the schemas, we have another type of
[15:36:53] object. We call it view. And of course
[15:36:56] in this section, we are focusing on the
[15:36:57] views. So a view is like a virtual table
[15:37:00] that has a structure and everything but
[15:37:03] inside it we don't have any data. So the
[15:37:06] view does not store any data and in
[15:37:08] order to see the data we have to execute
[15:37:10] the query behind the view and only after
[15:37:13] that we're going to see some data but it
[15:37:15] is not like the tables it doesn't store
[15:37:16] the data permanently. Now inside the
[15:37:18] tables we can define multiple stuff like
[15:37:21] columns and as well keys and the same
[15:37:23] thing for the views. Inside the views we
[15:37:25] can define multiple columns and one last
[15:37:28] level for each column we have like a
[15:37:30] name and a data type. So as you can see
[15:37:32] the databases are really organized and
[15:37:34] we have like hierarchy where the top
[15:37:36] node is the SQL server and the lowest
[15:37:39] node is the columns and rows. So this is
[15:37:42] what we call the database structure. Now
[15:37:44] in order for you to build and manage
[15:37:46] this structure we have set of commands
[15:37:49] we call it DDL the shortcut of data
[15:37:52] definition language. So the detail is a
[15:37:55] set of commands that allow us to define
[15:37:58] and manage the structure of the
[15:38:00] database. So we have commands like
[15:38:02] create where it help us to create
[15:38:05] databases, schemas, tables, views.
[15:38:07] Another command called alter. Of course
[15:38:09] after you create something you would
[15:38:11] like maybe later to do changes and
[15:38:13] updates and of course we have the drop
[15:38:14] in order to remove any database object
[15:38:17] like dropping a schema, dropping a
[15:38:19] database, tables, views. So as you can
[15:38:21] see the DDL commands can help us to
[15:38:23] manage the database structure. So from
[15:38:25] this picture we have understood that we
[15:38:27] can create views inside schemas in the
[15:38:30] database. So now if you check the client
[15:38:32] and the object explorer you can find the
[15:38:35] exact hierarchy. So it start with the
[15:38:37] SQL server. This is our local server
[15:38:39] that's run at our machine and then we
[15:38:41] can find inside it multiple databases
[15:38:44] and one of them is our sales DB that you
[15:38:46] have installed together with other
[15:38:47] database like the adventure works. So
[15:38:49] now if you go to the sales DB over here
[15:38:52] you can go and drill to the next level
[15:38:54] and now we can find here a lot of
[15:38:55] objects and one of them that you know we
[15:38:57] have tables and views and now you might
[15:38:59] say okay but between the database and
[15:39:01] tables we have schemas so where are the
[15:39:04] schemas well actually if you go inside
[15:39:06] the tables you're going to find our
[15:39:08] tables customers employees and so on but
[15:39:10] before it we have a name called sales
[15:39:13] doc customers and you can find it
[15:39:15] everywhere sales doc customers sales do
[15:39:17] employees and so on the sales is the
[15:39:20] schema that bring all those tables
[15:39:22] together underneath one logical schema.
[15:39:25] So we have a database called sales DB.
[15:39:28] We have a schema called sales and we
[15:39:30] have a table called customers. And now
[15:39:32] if you would like to see all the schemas
[15:39:34] inside this database, what you can do?
[15:39:36] You can go to the securities over here
[15:39:37] and then here we have like a folder
[15:39:39] called schemas. If you go over there,
[15:39:41] you will find the list of all schemas
[15:39:43] that we have in this database. You might
[15:39:45] say, but we didn't create all those
[15:39:46] stuff. If we have only the sales that we
[15:39:48] know. Well, as you create a database in
[15:39:51] SQL server, you will get a lot of other
[15:39:53] system default schemas that the server
[15:39:56] can create. One of them is the
[15:39:58] information schema where it holds a lot
[15:40:00] of views about the catalog and the
[15:40:02] metadata where you can find the list of
[15:40:04] columns, tables, views and so on. So
[15:40:06] here we have only one schema that we
[15:40:08] have created for the user. It is the
[15:40:09] sales. So let's go back. Now if you go
[15:40:12] inside one of those tables you will find
[15:40:14] here multiple stuff like we have
[15:40:16] columns, keys, constraints and so on.
[15:40:19] And if you go to the columns you will
[15:40:21] end up at the lowest level of the
[15:40:23] hierarchy. And here we have the columns
[15:40:25] like the customer ID and we have some
[15:40:27] extra informations like the data type
[15:40:29] length and so on. So this is the
[15:40:31] structure and the hierarchy of
[15:40:32] databases.
[15:40:37] Now I would like you to understand a
[15:40:39] fundamental concept on the database in
[15:40:41] order to understand the views the
[15:40:43] three-level architecture of the
[15:40:44] database. This architecture can describe
[15:40:47] the different levels of data
[15:40:49] abstractions in a database. So let's see
[15:40:52] what this means. So the architecture is
[15:40:54] divided into three levels. The first
[15:40:56] level is the physical level. Then we
[15:40:58] have the logical level and the third one
[15:41:00] is the view level. Now let's understand
[15:41:03] each level what it means. So now the
[15:41:05] physical level it is the lowest level of
[15:41:07] the database where the actual data is
[15:41:10] stored in a physical storage and usually
[15:41:12] who has access to this layer are the
[15:41:15] database administrators because they are
[15:41:17] the experts and they have to manage the
[15:41:19] access and the security of this layer
[15:41:21] because they are the expert that have to
[15:41:24] manage a lot of stuff like optimizing
[15:41:25] the performance making sure that
[15:41:27] everything is secure and managing the
[15:41:30] backup and recovery and to do all the
[15:41:32] configurations and many other tasks. So
[15:41:34] at the physical layer we have to deal
[15:41:36] with a lot of stuff like the data files,
[15:41:39] partitions, logs, cataloges, blocks and
[15:41:42] caches and many other stuff that each
[15:41:44] database needs in order to store your
[15:41:46] data. So as you can see this layer is
[15:41:49] very complicated and you need to be
[15:41:51] really an expert of databases in order
[15:41:54] to be able to manage all those stuff. So
[15:41:56] we call this layer a physical layer or
[15:41:59] sometimes we call it an internal layer.
[15:42:01] So now let's move to the next level. we
[15:42:03] have the logical level. So the logical
[15:42:05] layer it is less complicated than the
[15:42:07] physical layer. Here at this level you
[15:42:09] have to deal on how to organize your
[15:42:11] data and normally we have here like an
[15:42:14] application developer or we have like
[15:42:16] data engineers that access the logical
[15:42:19] level in order to define the structure
[15:42:21] of your data. So those developers can
[15:42:23] focus on how to structure your data
[15:42:25] rather than how the data is exactly
[15:42:28] storing the data physically at the
[15:42:29] storage. So they don't have to deal with
[15:42:31] all those details. they leave it for the
[15:42:33] database administrator and they can
[15:42:35] focus only on how to structure the data.
[15:42:38] That's why we need for this kind of role
[15:42:41] an abstraction level for them which is
[15:42:44] the logical level. So now what actually
[15:42:46] the developers are doing at this level?
[15:42:48] Well, they are like creating tables and
[15:42:51] defining the relationships between those
[15:42:53] tables or they can go and define views.
[15:42:56] they can create indexes on the tables in
[15:42:58] order to optimize the performance of the
[15:43:00] tables or maybe they are creating stored
[15:43:03] procedures and functions and some other
[15:43:05] codes in order to manage those tables.
[15:43:08] So as you can see they are building the
[15:43:09] data model they are structuring your
[15:43:11] data but they don't care at all where
[15:43:14] are those data stored physically in the
[15:43:17] database. So as you can see here things
[15:43:19] are less complicated than the physical
[15:43:21] layer and it is perfect abstraction for
[15:43:23] developers to build projects. So we call
[15:43:25] this the logical layer or sometimes we
[15:43:28] call it the conceptual layer. Okay. So
[15:43:30] now moving on to another level of
[15:43:32] abstraction. We have the view level. So
[15:43:35] the view level is the highest level of
[15:43:38] abstraction in the database and it is
[15:43:40] what the end users and applications can
[15:43:43] access and can see. So for example, you
[15:43:46] could have like one view for business
[15:43:48] analyst. So you prepare and customize a
[15:43:51] views that are suitable only for the
[15:43:54] business analyst and you might say you
[15:43:55] know what let's prepare another set of
[15:43:58] views that are suitable for data
[15:44:00] visualizations and reporting like you
[15:44:03] can go and connect for example a PowerBI
[15:44:05] in order to create dashboards. So they
[15:44:08] are fully customized and prepared views
[15:44:10] in order to be connected with the
[15:44:13] PowerBI reports and you can keep doing
[15:44:15] that by creating multiple set of views
[15:44:18] that are suitable for specific purpose
[15:44:21] and use case. So as you can see at this
[15:44:23] level we are exposing our data for
[15:44:26] multiple users and multiple
[15:44:27] applications. So now the question is
[15:44:29] what do we have to deal at the view
[15:44:31] level? Well, you have their only views
[15:44:33] that holds only the relevant
[15:44:35] informations for the use case or users.
[15:44:38] So the users at this level have only
[15:44:40] views. They don't have to deal with the
[15:44:42] tables, indexes, store procedures, any
[15:44:45] files, logs, partitions or anything.
[15:44:48] This is the highest level of abstraction
[15:44:51] because the focus of this layer is to
[15:44:53] make it friendly for the end users and
[15:44:55] easy to consume. So we call this layer
[15:44:57] the view layer or sometimes we call it
[15:45:00] an external layer. So this is the
[15:45:02] three-level architecture of the
[15:45:04] databases or we call it the three
[15:45:06] abstraction levels of the database. So
[15:45:08] the physical layer has the highest
[15:45:10] complexity, the lowest abstraction and
[15:45:13] the view layer has the highest
[15:45:15] abstraction. So this is one more reason
[15:45:17] why the views are very important concept
[15:45:19] in SQL
[15:45:21] [Music]
[15:45:24] databases. Okay. So with that we have
[15:45:26] enough fundamentals in order to start
[15:45:28] talking about the views. So the question
[15:45:29] is what are views? A view is a virtual
[15:45:33] table in SQL that is based on the result
[15:45:36] of a query without actually storing the
[15:45:39] data in the database. So in short this
[15:45:41] means views are stored or persisted SQL
[15:45:46] query in the database. So let's
[15:45:48] understand what this exactly means. Now
[15:45:50] so far what you have learned we have
[15:45:51] like database table and all what you
[15:45:53] have done we create a select query in
[15:45:55] order to retrieve the data from this
[15:45:57] table. So once we execute our query we
[15:45:59] will get the result back. Now if you are
[15:46:01] talking about views they have as well
[15:46:04] like the structure of the table but
[15:46:06] without any data inside it. And for each
[15:46:08] view there is like a query attached to
[15:46:11] it. So there is no data but we have like
[15:46:13] a query in order to get data. We call
[15:46:16] the normal table as a physical table and
[15:46:18] the view we call it a virtual table. So
[15:46:21] now how exactly we're going to get the
[15:46:22] data. So now if you go and write query
[15:46:25] by selecting data from the view not from
[15:46:27] the table from the view what going to
[15:46:29] happen SQL going to go and trigger the
[15:46:32] queue that is attached to the view and
[15:46:34] this query is responsible to query the
[15:46:37] physical table and then the result going
[15:46:40] to fill the structure of the view and we
[15:46:42] will get back of course the results. So
[15:46:44] we are directly querying a view but
[15:46:46] actually we are indirectly querying a
[15:46:49] physical table. So the view is like
[15:46:51] between us and the data. So that means
[15:46:53] my real data is stored inside the
[15:46:55] database tables and the views are like
[15:46:58] an abstraction layer between me and my
[15:47:00] real data. And of course the data will
[15:47:03] not be stored inside the view. Each time
[15:47:05] I'm querying the view what's going to
[15:47:07] happen the SQL query behind the view
[15:47:09] going to be executed again. So it's
[15:47:11] going to go and retrieve the data and
[15:47:13] get it back to the view and then I will
[15:47:14] see it in the output. So this is what we
[15:47:17] mean with SQL
[15:47:21] view. So now let's have a quick
[15:47:23] comparison between tables and views.
[15:47:25] Tables stores the actual data physically
[15:47:28] at a database. So the tables where the
[15:47:30] data is persisted with in the other hand
[15:47:32] the views they are virtual tables and
[15:47:35] they do not store any data inside the
[15:47:37] database but they present the data from
[15:47:39] the underlying tables. So that means
[15:47:41] views don't persist any data physically.
[15:47:44] Now the tables are hard to maintain and
[15:47:46] as well hard to change. So it needs a
[15:47:49] lot of efforts in order to do any change
[15:47:51] like adding columns and moving columns
[15:47:53] always requires a lot of efforts for the
[15:47:55] migration especially if you have large
[15:47:57] tables. But in the other hand the views
[15:48:00] are way easier to maintain and very
[15:48:02] flexible to change. All what you have to
[15:48:04] do is only to change the query of the
[15:48:06] view. So that means you can very quickly
[15:48:08] change stuff in the views compared to
[15:48:10] the tables. But if you are talking about
[15:48:12] performance, tables are faster than
[15:48:14] views. For example, if you go and do a
[15:48:16] simple select on the table, you will get
[15:48:18] the data back as soon as the database
[15:48:21] fetches the data. But if you are
[15:48:22] selecting something from the view, it is
[15:48:24] actually two queries. The query that
[15:48:27] comes from the user and as well the
[15:48:29] second query is the view query. and the
[15:48:31] query of the view could be very
[15:48:33] complicated in order to extract the data
[15:48:36] from the underlying table. So selecting
[15:48:38] something from the view is always slower
[15:48:40] than selecting something from a table.
[15:48:42] Now if you have a table you can read
[15:48:44] from the table and as well you can write
[15:48:46] to a table but the views are read only
[15:48:49] as the name says it is only a view. You
[15:48:51] cannot go and write something to the
[15:48:52] database using the view. Okay. So those
[15:48:55] are the big differences between views
[15:48:57] and tables.
[15:49:02] All right. So with that we have a clear
[15:49:03] understanding what are views. But now
[15:49:05] you might ask me why do we need views?
[15:49:08] That's why now what we're going to do
[15:49:09] we're going to deep dive into multiple
[15:49:11] scenarios and use cases that you might
[15:49:13] encounter in your SQL projects. So let's
[15:49:16] start with the first use case. The first
[15:49:18] use case and the core reason why we use
[15:49:20] views in our data projects is to store
[15:49:23] central logic from a complex query in
[15:49:26] the database so that everyone can access
[15:49:28] it and with that we improve reusability
[15:49:31] between multiple queries and we reduce
[15:49:33] as well the complexity of the overall
[15:49:36] projects. So let's understand what this
[15:49:37] means. So now in our project we have
[15:49:39] like two tables in the database orders
[15:49:41] and customers and we have learned
[15:49:43] previously that if we have like a
[15:49:45] complex query we can go and use the
[15:49:47] city. So for example in our city we are
[15:49:49] joining tables and doing some
[15:49:51] aggregations using the sum and the city
[15:49:53] going to store the data in an
[15:49:54] intermediate results and then we have
[15:49:56] the main query. For example we are doing
[15:49:58] the step two where we are ranking the
[15:50:01] data. So the whole thing is in one query
[15:50:03] and let's say that a financial analyst
[15:50:05] was doing this type of analyszis. Now
[15:50:08] what could happen is that you might have
[15:50:10] another user for example a budget
[15:50:12] analyst where he is doing exactly the
[15:50:14] same first step. So he has as well a
[15:50:17] city query where first the data are
[15:50:19] joined and then aggregated using the
[15:50:21] sum. But the last step in the main query
[15:50:23] he's not doing ranking he's just doing
[15:50:25] like max and min. And not only that, we
[15:50:28] have a third user, the risk analyst,
[15:50:30] where as well doing the same initial
[15:50:33] step using the CTE, joining the tables
[15:50:35] and doing the summarization. But here
[15:50:37] the risk analyst in this scenario, he's
[15:50:40] just comparing the data at the last step
[15:50:42] in the main query. So now if you sit
[15:50:43] back and look to this, you can see all
[15:50:45] those three data workers, all of them
[15:50:47] are doing the same first step. So all of
[15:50:49] them are doing the same CTE. They are
[15:50:51] joining the data and then doing
[15:50:53] summarization. And of course this is a
[15:50:55] complete waste of time that each one of
[15:50:57] them has to create first the city from
[15:50:59] the scratch in order to do some
[15:51:01] analyszis. So it is complete redundancy
[15:51:04] and makes no sense. So this is exactly
[15:51:06] the disadvantage of only using cities in
[15:51:09] the projects. Now what we can do instead
[15:51:11] of that those three data workers going
[15:51:13] to decide to say you know what let's put
[15:51:15] the first step as view in the database.
[15:51:18] So instead of using CTE each time we're
[15:51:21] going to take this script and put it in
[15:51:23] the database. So we have now a central
[15:51:26] logic that is stored in the database
[15:51:28] where everyone can use it. So we have
[15:51:30] this query this logic only once and
[15:51:33] everyone can benefit from it. So now the
[15:51:36] financial analyst instead of going
[15:51:38] directly to the physical tables they can
[15:51:40] go to the view. So thus means she needs
[15:51:43] only to write one script the rank
[15:51:45] script. Same thing goes for the budget
[15:51:47] analyst. he has only to write the query
[15:51:49] for the max and min and as well for the
[15:51:51] risk analyst he just need to compare the
[15:51:53] data. So as you can see all those
[15:51:55] queries are reduced and they can only
[15:51:57] focus on the analyzes. So this is
[15:51:59] exactly the magic of views in data
[15:52:02] analytics. This logic this knowledge can
[15:52:04] be centralized in the database and this
[15:52:07] is way faster and better than having
[15:52:09] this logic written each time someone
[15:52:11] want to do any analyzes. So this is why
[15:52:14] we need views in data projects.
[15:52:20] So now if you compare views with CTE,
[15:52:22] the CTE are used in order to reduce the
[15:52:25] redundancy within one single query. So
[15:52:28] it improves the reusability within one
[15:52:31] query. Where in the other hand in the
[15:52:33] views we are reducing the redundancies
[15:52:36] from multiple queries. So we are
[15:52:38] reducing the complexity of the whole
[15:52:40] project. So the views are improving the
[15:52:43] reusability in multiple queries. Now
[15:52:46] think about it like this. We use views
[15:52:48] in order to persist a logic in the
[15:52:51] database. So the logic is so important
[15:52:54] that we want to persist it in the
[15:52:55] database. It's like in the tables we
[15:52:57] persist data but with the views we are
[15:52:59] persisting logic. But in the other hand
[15:53:02] in the CTE the logic is not persisted.
[15:53:05] It is temporary and going to be
[15:53:06] calculated only on the fly within the
[15:53:09] scope of one query. So this logic is
[15:53:12] important only in this scenario and it
[15:53:14] is not important for any other queries.
[15:53:17] That's why it makes no sense to persist
[15:53:19] it using the views. So you have to
[15:53:21] decide is this logic is very important
[15:53:24] then take it away from the city and put
[15:53:26] it in the view. But if you think you
[15:53:28] know what this logic is not really
[15:53:30] important and only important in this one
[15:53:32] query then stay with the city because
[15:53:34] creating views always needs some extra
[15:53:37] steps in order to maintain the view. You
[15:53:40] have to create the view. You have to
[15:53:41] drop the view if you don't need it. But
[15:53:43] the CTE, there is almost no maintenance
[15:53:45] for it. The database going to do
[15:53:47] automatically the cleanup once the query
[15:53:49] is done. So there is no extra activity
[15:53:52] to drop a city or something. That's why
[15:53:55] CTE is easier to use than views. So
[15:53:57] those are the big difference between the
[15:53:59] views and
[15:54:03] cities. Okay. So now let's check quickly
[15:54:05] the syntax of a view. So now we have a
[15:54:08] query like select from where. So this is
[15:54:10] a query a simple select statement. But
[15:54:13] now in order to create a view an object
[15:54:15] in database we have to go and use a DDL
[15:54:18] command create. So we're going to say
[15:54:20] create view cuz we want to create a view
[15:54:23] then the name of the view and then it's
[15:54:25] like the CTE we say as and then double
[15:54:28] parenthesis. So as you can see it's very
[15:54:30] simple and we call this a DDL command
[15:54:33] where we are telling the database go and
[15:54:35] create a view and the logic of the view
[15:54:37] comes from this query. So it's very
[15:54:39] simple. This is how you can create views
[15:54:41] in database. Okay. So now let's have the
[15:54:43] following task and it says find the
[15:54:45] running total of sales for each month.
[15:54:47] I'm going to start this task by solving
[15:54:49] it using the CTE. So first I'm going to
[15:54:51] go and do few aggregations on the top of
[15:54:53] the month. So let's go and select. So
[15:54:56] now what do we need? We need the order
[15:54:58] dates but we need it as a month. I'm
[15:55:00] going to go and use the date truncate
[15:55:03] like this and say okay I would like to
[15:55:05] have the date as the granularity of
[15:55:08] month. So let's go and call it order
[15:55:12] month. And now after that we're going to
[15:55:13] do a few aggregations like for example
[15:55:15] let's go and get the sum of sales and
[15:55:19] we're going to call it total sales. And
[15:55:21] that's it for the start. So now let's go
[15:55:23] and call it from the table sales orders
[15:55:26] and group by and we are grouping up by
[15:55:30] by the month. So something like this.
[15:55:32] Let's go and execute it. And now for
[15:55:34] this we get for each month the total
[15:55:36] sales. And now the next step that we
[15:55:37] have to go and calculate the running
[15:55:39] total for the sales. This is of course
[15:55:41] not the running total. So that means
[15:55:43] either we can go and use subqueries. So
[15:55:45] this means this is our first step and we
[15:55:47] need a second step. So either use
[15:55:48] queries or cities. I will go with the
[15:55:50] city over here. So I'm going to say with
[15:55:53] city and
[15:55:56] monthly summary and we're going to
[15:55:59] define it like this. And now what we're
[15:56:01] going to do, we're going to go and
[15:56:02] define the main query. So the main query
[15:56:04] going to be simple. So select and let's
[15:56:07] go and get the order month. And now we
[15:56:10] have to build the running total. So
[15:56:12] we're going to go and use the window
[15:56:13] function. So sum total sales. And then
[15:56:17] we're going to say over we don't have to
[15:56:19] partition the data. We will just sort it
[15:56:22] by the order
[15:56:24] month and we can leave it ascending. So
[15:56:27] this is the running
[15:56:30] total and we have to go and select of
[15:56:33] course our CTE from here. So let's go
[15:56:36] and execute it and with that we are
[15:56:38] getting the running total. Of course we
[15:56:39] can go and add the total sales in the
[15:56:41] output in order to understand the
[15:56:43] results. So here in the output we are
[15:56:44] just building accumulative sales. So for
[15:56:47] this scope everything is fine. and we
[15:56:49] are using the CTE. But now imagine that
[15:56:51] this logic is important for multiple
[15:56:54] queries. So it's really nice to have
[15:56:56] such a report where we are aggregating
[15:56:58] the data at the level of the month and
[15:57:00] this could be used from different users
[15:57:02] and different queries. So now we say how
[15:57:04] about to put this logic in one view so
[15:57:07] that everyone can access it and we don't
[15:57:09] have to repeat the same aggregations
[15:57:11] over and over. And now before we put it
[15:57:13] in view, someone comes and say how about
[15:57:15] to add one more aggregation so that not
[15:57:17] only the total sales we can add. So now
[15:57:19] before we put it as view maybe some
[15:57:21] other user says you know what we would
[15:57:23] like to have one more aggregation not
[15:57:24] only the total sales let's make the
[15:57:26] scope a little bit bigger so that
[15:57:28] everyone can believe it. So for example
[15:57:29] we can go over here and say you know
[15:57:31] what let's go and add the total number
[15:57:33] of orders. So we can go over here and
[15:57:35] say counts and let's get the order ID
[15:57:38] and say this is the total orders or
[15:57:41] maybe some other says let's get the
[15:57:43] quantities as well. So we can go and
[15:57:46] summarize the quantity like this and we
[15:57:50] call it total
[15:57:51] quantities. So with that we are like
[15:57:54] doing a lot of aggregations on the month
[15:57:56] level. Let's go and execute only the
[15:57:58] CTE. So now we have really nice report
[15:58:00] that is based on the months and can be
[15:58:03] used from many different queries. So now
[15:58:05] what we're going to do, we're going to
[15:58:06] take this and put it in a view. Let's go
[15:58:08] and select only this logic and create a
[15:58:11] new query. And now what we're going to
[15:58:13] do, we're going to put our query here
[15:58:14] and we have to create now the DDL in
[15:58:16] order to create a view. So it's going to
[15:58:19] be like this. Create view. Let's give it
[15:58:21] the name maybe starts with the V
[15:58:23] underscore and this going to be the
[15:58:25] monthly summary. So this is the name of
[15:58:28] the view and as then we put everything
[15:58:32] in parenthesis. It's like you are
[15:58:34] building a CTE. So we have here our
[15:58:36] logic and here is our DDL query in order
[15:58:40] to create the view. So now let's go and
[15:58:42] execute it. Now as you can see in the
[15:58:44] output it says only that the command is
[15:58:46] completed because this is not a select
[15:58:48] query. This is a DDL command. So the SQL
[15:58:51] going to tell you okay either I created
[15:58:53] it successfully or not. So now the
[15:58:55] question is where do I find now my view?
[15:58:58] Well, if you go to the object explorer,
[15:59:00] you can see over here underneath our
[15:59:02] database sales DB, we have here
[15:59:05] something called tables where we are
[15:59:06] used to query those tables. But beneath
[15:59:09] it, we have as well our views. So if you
[15:59:12] check the views and expand it, now we
[15:59:14] are not seeing M view because we just
[15:59:16] created the view here. So go over here
[15:59:18] and refresh. And once you do that, you
[15:59:21] will see the newly created view. So this
[15:59:24] is the one that we just created. So now
[15:59:26] what we can do, we can go and create a
[15:59:27] new query and let's go and just query
[15:59:30] the view. So select star from so v month
[15:59:35] monthly summary. Let's go and execute
[15:59:39] it. And now as you can see we are
[15:59:40] getting now the result of the view and
[15:59:42] I'm accessing now this logic from
[15:59:45] completely external query. So now I can
[15:59:47] think about the view as any other table
[15:59:49] that we have in the database. And again
[15:59:51] the big differences between the views
[15:59:53] and the tables. The tables has data has
[15:59:56] actual data and everything there is
[15:59:58] persisted but the view is just an
[16:00:00] abstraction for me and behind it there
[16:00:03] is like a query that goes to the table
[16:00:05] and query the tables in order to present
[16:00:08] the results. But for me I don't care
[16:00:09] about all those details. I can go
[16:00:11] immediately to the query over here and
[16:00:13] start querying. So now in order to
[16:00:15] create the total running sales I don't
[16:00:17] have to create the CTE and sub queries.
[16:00:20] I just go and get for example our main
[16:00:23] query. Let's go back over here. So now
[16:00:26] instead of using the CTE I can go
[16:00:28] directly and access the view. So as you
[16:00:32] can see now my query is very simple. I'm
[16:00:34] doing immediately the step two without
[16:00:37] having to prepare the data first. So if
[16:00:39] I go and execute it I will get exact
[16:00:41] results. And now if you compare the
[16:00:43] query on top of the view like this with
[16:00:45] the city query you can see that the CTE
[16:00:48] has more steps and it is like little bit
[16:00:51] more complicated than the query on top
[16:00:53] of the view and this is exactly the
[16:00:55] benefit of the view. We reduce the
[16:00:57] complexity and it is very easy to
[16:00:59] consume from the point of view of users.
[16:01:01] So this is how you can put your logic in
[16:01:04] central place using views and with that
[16:01:07] we have learned how we create a view.
[16:01:09] Now one more thing about the schemas. If
[16:01:11] you check our tables over here, they
[16:01:13] have all one schema. So we have sales
[16:01:15] dot customers, sales do employees,
[16:01:18] orders and so on. Our new view has the
[16:01:20] schema of DBO. If you create any object
[16:01:23] whether it's table or view and you don't
[16:01:26] specify a schema in a default schema
[16:01:28] called DBO. And now let's go back to our
[16:01:30] DDL scripts. So as you can see over
[16:01:32] here, we didn't specify any schema. We
[16:01:34] just said okay, this is the view name.
[16:01:37] And now in order to put our view in the
[16:01:39] correct schema we don't want it to be in
[16:01:41] the defaults. You have to go and specify
[16:01:43] the schema name in the DDL. And now in
[16:01:45] order to do that we go to the name of
[16:01:47] the view and we write the schema name
[16:01:50] and then separated with a dot. So the
[16:01:52] first one is the schema name and the
[16:01:54] second one is the view name. So now
[16:01:57] let's go and execute it. Now if you
[16:01:59] check over here you don't see anything
[16:02:00] new. But if you refresh you will find
[16:02:03] another view in the correct schema. So
[16:02:05] we have sales dot vmon monthly summary
[16:02:08] and this is exactly what we want. So
[16:02:10] this is how you can assign a view or
[16:02:12] even a table to the correct schema if
[16:02:15] you don't want to use the default one
[16:02:16] the view. All right. So now the next
[16:02:18] step is that you say you know what I
[16:02:19] would like to clean up. I don't need
[16:02:20] those two views in my database. So how
[16:02:23] to delete a view? We can go and use the
[16:02:25] command drop. It is very simple. If you
[16:02:27] go and create a new query and you say
[16:02:30] drop and then you say what you want to
[16:02:32] drop. you want to drop a view and then
[16:02:33] you have to specify the name and schema
[16:02:36] of the view. But now since it is the
[16:02:38] default schema DBU, I don't have to
[16:02:40] write it down. So we can start
[16:02:42] immediately with the view name. So V
[16:02:45] monthly summary. So that's it. It's very
[16:02:48] simple. So now we go and execute it. It
[16:02:51] says it's completed but as you can see
[16:02:53] nothing has changed. We go and refresh.
[16:02:55] And now we can see that the database did
[16:02:57] remove the view with the schema DBU. So
[16:03:00] it's very simple. This is how you can
[16:03:02] drop a view in SQL. Okay. So now to the
[16:03:05] next step. Let's go back to our DDL of
[16:03:07] creating the view sales monthly summary.
[16:03:09] And now you say you know what I would
[16:03:11] like to change the logic inside the
[16:03:13] view. So how we can update this content?
[16:03:16] How I can update my query? If you say
[16:03:18] let's go and for example delete this
[16:03:20] column. I need only three columns. So
[16:03:23] and you go execute it. The database say
[16:03:25] I cannot do it for you because we have
[16:03:27] already such a view. So SQL will not go
[16:03:30] and replace stuff going to say no we
[16:03:33] have the same name and I cannot do
[16:03:35] anything for it. So how we can update
[16:03:37] the view? Well in other databases like
[16:03:39] ocris for example it's very simple. You
[16:03:42] can go over here and say create or
[16:03:45] replace view. So it's like you are
[16:03:47] telling the database create this view or
[16:03:49] if it already exists then replace it and
[16:03:52] you will not get error in the postcress.
[16:03:54] But in the SQL server it is little bit
[16:03:57] more complicated. we don't have this
[16:03:58] command. So here you have two ways.
[16:04:00] Either you go over here and say you know
[16:04:02] what let's first drop the view. So you
[16:04:06] go with the same name over
[16:04:08] here and then what you're going to do
[16:04:10] you're going to go and mark the drop
[16:04:12] view. So if you execute it like this the
[16:04:14] view going to be dropped and then we
[16:04:16] recreate the view like this. So what we
[16:04:18] have done we destroy the view and then
[16:04:20] we recreate it using the new logic. Or
[16:04:22] you say you know what I would like to
[16:04:24] have everything in one go like I don't
[16:04:26] want to do it in two steps. I would like
[16:04:27] to have everything in one command and
[16:04:29] for that you have to use in SQL server
[16:04:31] the TSQL the transacts SQL it is like an
[16:04:35] extension for SQL only in SQL server
[16:04:37] well it's like programming where you can
[16:04:39] go and add variables or you can all go
[16:04:41] and add checks we will not do a deep
[16:04:43] dive in this language but I would like
[16:04:44] to show you how to do it for the views
[16:04:46] so just follow me with that I'm going to
[16:04:48] go and replace the whole thing and then
[16:04:51] we're going to say if and now we are
[16:04:53] checking the system catalog if the
[16:04:55] object ID
[16:04:57] And now we go and specify the view name.
[16:05:00] So let's go and copy the whole thing
[16:05:01] with the schema as well. And then we're
[16:05:03] going to say for SQL this is a view. So
[16:05:07] if this object exists so we are saying
[16:05:09] is not null. So that means it exist in
[16:05:12] the catalog then what SQL should do?
[16:05:15] Should drop this view. So we're going to
[16:05:17] say drop view and it's like we have done
[16:05:19] it first and then semicolon and then we
[16:05:23] say for scale go and with that we are
[16:05:25] saying for SQL the tscale is done. So
[16:05:28] the logic is done and after that we have
[16:05:31] the DDL for our view. So again what we
[16:05:33] are doing we are checking before
[16:05:36] creating the view whether the view
[16:05:38] exist. If it exist then we are telling
[16:05:40] the scale go and drop it and if it
[16:05:42] doesn't exist that means we haven't
[16:05:44] created this view yet. it is completely
[16:05:47] brand new view then this step going to
[16:05:49] be skipped so that there is nothing to
[16:05:51] drop. So now if you go and execute the
[16:05:53] whole thing it will work and of course
[16:05:54] if you go and refresh over here you
[16:05:56] still see the view. So SQL did destroy
[16:06:00] the table first and then recreated. So
[16:06:02] if you execute it again. So this is how
[16:06:04] you replace your logic in view in SQL
[16:06:07] server. And with that we have learned
[16:06:09] all possible scenarios. How to create a
[16:06:11] view, how to drop a view and how to
[16:06:14] update the logic of a
[16:06:19] view. Now back to our database
[16:06:21] architecture and let's understand how
[16:06:23] the database executes views. So now
[16:06:26] let's say that the data engineer is
[16:06:28] creating view called top end. So the
[16:06:30] query going to be sent to the database
[16:06:32] engine and once the database engine
[16:06:34] understand this is a view this is not a
[16:06:36] table. So now the database engine going
[16:06:38] to go to the disk storage and to the
[16:06:40] catalog and it will stores not only the
[16:06:43] metadata about the view also the SQL
[16:06:47] that is responsible for the view. So
[16:06:49] it's going to take the SQL statements
[16:06:50] that you have defined in the create view
[16:06:53] and place it as well in the catalog. So
[16:06:55] if you compare to the tables we have in
[16:06:57] tables only metadata but in the views we
[16:07:00] have both the metadata and as well the
[16:07:02] query of the view and as well you can
[16:07:04] see that the database engine will not
[16:07:07] create a table in the user's data. So
[16:07:10] there is nowhere data stored inside the
[16:07:12] disk or the cache. So the actual data
[16:07:15] the physical data will not be stored
[16:07:16] anywhere. We are storing only metadata
[16:07:19] and the query inside the system catalog.
[16:07:22] So now we tell our data analyst okay we
[16:07:24] have a new view and the data analyst can
[16:07:26] go and write a query in order to
[16:07:28] retrieve the data from the view. So he
[16:07:30] going to say and say select from the
[16:07:32] view and execute it. The database engine
[16:07:34] going to take it and understand okay now
[16:07:36] we are talking about view. So the
[16:07:37] database first has to retrieve not the
[16:07:40] data going to retrieve the query from
[16:07:43] the catalog in order to understand what
[16:07:45] do we have now to execute. Then the
[16:07:47] database going to execute the query of
[16:07:49] the view first and the data for this
[16:07:52] query comes from a physical table called
[16:07:54] orders. So now the database engine is
[16:07:56] querying the order to retrieve the data
[16:07:58] so that we have a data for the end user
[16:08:01] and then it's going to be executed and
[16:08:02] the result going to be sent back to the
[16:08:05] data analyst. So as you can see there is
[16:08:06] like two queries. The SQL engine first
[16:08:09] has to execute the query from the view
[16:08:12] and only after that the database engine
[16:08:15] can execute the query that comes from
[16:08:17] the user. So actually the data comes
[16:08:19] always from a physical table but we are
[16:08:21] not providing the data analyst an access
[16:08:23] to the table. We are just providing an
[16:08:26] access to the view. So this can happen
[16:08:28] each time an end user selecting data
[16:08:30] from the view. Always the database
[16:08:32] engine going to grab the query from the
[16:08:34] catalog, execute it first in order to
[16:08:36] get the data and then execute what the
[16:08:39] end user wants. And now if the data
[16:08:40] engineer says no, let's go and drop the
[16:08:42] view. So she writes a query in order to
[16:08:45] drop the view. And the database engine
[16:08:46] going to go to the system catalog and
[16:08:48] delete both the metadata and the query.
[16:08:51] So as you can see, if you are dropping a
[16:08:52] view, you are not losing the actual
[16:08:55] data. So there will be no user data lost
[16:08:57] at all. So don't worry about it. What
[16:09:00] you are losing is only the query and the
[16:09:02] metadata about your view. It's only if
[16:09:04] you drop a physical table like the
[16:09:07] orders, you will lose your data. So
[16:09:09] dropping views is not that bad like
[16:09:11] dropping a database table. So this is
[16:09:13] how the database works with the views
[16:09:15] behind the scenes.
[16:09:20] Now moving on to the second scenario to
[16:09:22] the next use case of using views in
[16:09:24] projects is that we use views in order
[16:09:26] to hide complexity and to improve
[16:09:29] abstraction. In many scenarios we work
[16:09:31] with a very large and complex databases
[16:09:34] and we can use views in order to reduce
[16:09:37] the complexity and make things easier
[16:09:39] for the users. So let's understand what
[16:09:41] this means. Now I'm going to explain for
[16:09:42] you a scenario that happens almost in
[16:09:45] each project. Like if you get an access
[16:09:46] to a database where you want to do
[16:09:48] analyzes, you will be in scenario and
[16:09:50] this can happen a lot where you're going
[16:09:52] to find a large database where the
[16:09:54] tables are very complex to understand.
[16:09:56] They have a lot of columns. They have
[16:09:58] like technical and cryptical names and
[16:10:00] how tables are connected to each others
[16:10:02] and relationship between them. It's
[16:10:04] almost impossible to understand. then
[16:10:06] you have to be deeply involved with the
[16:10:08] data models with documentations and with
[16:10:11] experts until you understand how to
[16:10:14] query this database. So if you are not a
[16:10:16] developer and from end user perspective
[16:10:19] it can be nightmare where you are trying
[16:10:21] to do multiple joins in order to make
[16:10:23] simple analyzes and of course from the
[16:10:25] database perspective this data model is
[16:10:28] good enough for one application but if
[16:10:30] you are opening your database for
[16:10:32] multiple data analyszis projects this
[16:10:34] can be a nightmare because you have to
[16:10:36] go and explain for each user how to
[16:10:38] query the data. So what we usually do
[16:10:41] instead of giving a direct access to
[16:10:43] such technical and hard to understand
[16:10:46] data model we go as developers creating
[16:10:49] multiple views since we are the expert
[16:10:51] of the data model and these new views
[16:10:53] going to be an abstraction of the
[16:10:56] complexity that I have in my database
[16:10:58] and we have to make sure that those
[16:11:00] views are providing objects that are
[16:11:03] friendly. So they have like a full
[16:11:05] English name that makes sense and as
[16:11:08] well the columns are friendly and we try
[16:11:10] to not offer a lot of views so the user
[16:11:12] don't have to do all the joins. So we
[16:11:14] provide like few views that are friendly
[16:11:18] and has a lot of informations that the
[16:11:20] users needs for the analyzes. So with
[16:11:22] that the users can have an access to
[16:11:24] something more friendly and easy to
[16:11:26] consume and then they can write simple
[16:11:28] queries in order to do analyzes on top
[16:11:30] of these friendly views. And this is
[16:11:33] what we can give a name like we are
[16:11:35] providing a data product from my complex
[16:11:39] physical database. So here again how
[16:11:41] important are the views to provide an
[16:11:44] abstraction and easy to consume objects
[16:11:46] for the users and with that I can hide
[16:11:49] all my complexity and the script of the
[16:11:52] view going to be developed from the
[16:11:54] experts and only once so that the users
[16:11:57] don't have to understand or to write
[16:11:59] these complex SQL joins and with that
[16:12:02] you can make your data projects way
[16:12:04] easier than before. So this is another
[16:12:06] important use case for the views where
[16:12:08] we can use it in order to provide
[16:12:10] abstraction and as well easy and
[16:12:12] friendly objects for the end users.
[16:12:14] Okay. So now let's have the following
[16:12:15] task and it says provide view that
[16:12:18] combines details from orders, products,
[16:12:22] customers and employees. So now instead
[16:12:24] of having all those tables from our
[16:12:26] database, we have to provide one
[16:12:29] combined view that has everything well
[16:12:32] almost everything. So now let's see how
[16:12:33] we can create such a view. So let's
[16:12:35] start first by the table orders. I'm
[16:12:37] going to go and
[16:12:39] select first star from sales orders and
[16:12:44] let's go and execute it. This is the
[16:12:46] central table that connects everything.
[16:12:47] You can see here we have the order ID,
[16:12:49] product ID, sales, customers and so on.
[16:12:52] So it is a great start point. So now
[16:12:54] we're going to go and be picky about the
[16:12:55] columns. I would not show all the
[16:12:57] columns but I would say let's go and
[16:12:59] show for example the order ID. This is
[16:13:01] essential. It's nice to have a unique
[16:13:04] identifier. Now the product ID, I will
[16:13:06] not show it but I will just list it over
[16:13:08] here. The same for the customer ID,
[16:13:11] saleserson ID. Those stuff I would like
[16:13:14] to replace later. So I will just make it
[16:13:17] as comment so I don't forget about it
[16:13:19] because it makes no sense to show the
[16:13:21] product ID and customer ids and so on.
[16:13:23] We would like to show the details about
[16:13:26] each object because instead of having
[16:13:28] the product ID, I would like to show for
[16:13:30] example the product name itself and some
[16:13:32] other informations from the table
[16:13:34] products. And with that we are reducing
[16:13:36] the complexity. So now what else we can
[16:13:38] get from the table orders? We can go and
[16:13:40] get the order date. I will put it here.
[16:13:42] And maybe we can go and get stuff like
[16:13:45] sales and
[16:13:46] quantity. So like this. Of course, we
[16:13:50] can go and put all the columns. But for
[16:13:52] now, I will go with those informations.
[16:13:54] Now, it's important since we're going to
[16:13:55] have a lot of tables. Let's go and make
[16:13:57] sure we are using aliases. So, now we're
[16:14:00] going to have the O for each of those
[16:14:03] columns. All right. Fine. So, now we
[16:14:05] have four details from the table orders.
[16:14:08] Now, what is next? We have the product
[16:14:10] ID. So, let's go and get the
[16:14:11] informations from the products. What
[16:14:14] we're going to do, we're going to use a
[16:14:15] left join just to make sure to not miss
[16:14:17] any order. If you go with the inner
[16:14:20] join, you might miss some orders. So I
[16:14:22] will not do that. So let's join it with
[16:14:24] the products like this.
[16:14:27] And so now we have to go and join the
[16:14:30] tables. So we can use the keys product
[16:14:34] ID equal the order product ID. All
[16:14:38] right. So now the question is which
[16:14:40] informations we want to show for the
[16:14:42] users. Let's go to the table orders. So
[16:14:45] we have the product and category and the
[16:14:47] price. I would say let's go and get the
[16:14:49] product and category. That's enough. So
[16:14:51] now instead of the ID I'm going to have
[16:14:54] it like this. So it's going to be the
[16:14:56] product and the
[16:14:58] category. Now let's go and test it. I'm
[16:15:00] going to execute it. Now as you can see
[16:15:02] we don't have a product ID. We have the
[16:15:04] product name which is more friendly. So
[16:15:06] we have now those two columns from the
[16:15:07] orders and those two from the products
[16:15:10] and the last two as well from the
[16:15:11] orders. So it looks really nice and
[16:15:13] friendly and with that the user don't
[16:15:15] need extra table called products. We
[16:15:18] have everything in one. Now let's go and
[16:15:19] do the same for the customers. So let's
[16:15:21] go
[16:15:22] and do the same thing. So let's join
[16:15:26] sales customers see and as well join
[16:15:30] them using the key customer ID equal to
[16:15:33] the customer ID. Now we have to go and
[16:15:36] grab a few columns from the customers.
[16:15:38] Let's go and check. So we have a first
[16:15:40] name, last name and country and score. I
[16:15:42] would say I would go with the names and
[16:15:44] the countries but instead of having
[16:15:46] first name and last name I'm going to
[16:15:48] put everything in one. So we have to go
[16:15:50] and concatenate the informations. So
[16:15:53] we're going to get the first
[16:15:54] name then plus then empty between the
[16:15:58] first name and the last name and then
[16:16:01] the last name like
[16:16:03] this. Now we will not call it a name.
[16:16:06] We're going to go and call it the
[16:16:07] customer name because later we're going
[16:16:09] to have as well an employee name. All
[16:16:11] right. So next we want to get the
[16:16:14] country and we have to say this is the
[16:16:16] country from the customers. So we're
[16:16:18] going to call it customer country and
[16:16:21] that's it. Let's go and execute it. Now
[16:16:23] we can see we have again our orders
[16:16:26] products and now we have the
[16:16:27] informations from that customer. But
[16:16:29] here we have issue that we have some
[16:16:31] nulls and that's because there is no
[16:16:32] last name. So what we're going to do,
[16:16:34] we're going to go and handle the nulls
[16:16:36] for the last name and as well for the
[16:16:38] first name. So we're going to use the
[16:16:40] kowalis. If the last name is null then
[16:16:44] make an empty string and the same thing
[16:16:47] for the first name. So first name. All
[16:16:50] right. So now let's go and execute it.
[16:16:52] So with that we are getting as well the
[16:16:55] first name if the last name is missing
[16:16:57] or if the first name is missing we can
[16:16:59] get the last name. So looks good. So it
[16:17:01] looks good with that. We have the
[16:17:02] customer's details. The last thing we
[16:17:04] have to go and get the employees. So the
[16:17:07] employee here is called salesperson ID
[16:17:09] which we can connect it directly to the
[16:17:11] table employees. So if you go to the
[16:17:13] employees over here, which columns do we
[16:17:15] need? We have the first name, last name,
[16:17:17] department and so on. I would say let's
[16:17:19] go get the names and the departments. So
[16:17:21] first let's go and join it. So lift join
[16:17:25] sales
[16:17:27] employees and we're going to join it
[16:17:29] using the employee ID. and we're going
[16:17:31] to join it with the sales person ID that
[16:17:36] comes from the order table. So now
[16:17:38] instead of the person ID we're going to
[16:17:40] have as well the same thing. So I will
[16:17:42] just go and copy paste this. So instead
[16:17:45] of the alias we're going to have E and
[16:17:47] as well E over here and we're going to
[16:17:49] call it sales name and as well what we
[16:17:52] going to have we're going to have the
[16:17:54] department. So
[16:17:55] department and that's it. Let's go and
[16:17:59] execute it. So now we have a lot of
[16:18:01] informations in our view. So we have the
[16:18:03] first columns from the orders then from
[16:18:06] the products and here we have from
[16:18:08] customers and those two from the
[16:18:10] employees and the last two again from
[16:18:12] the orders. So that we have combined now
[16:18:14] all the relevant informations from
[16:18:16] multiple tables in our database in only
[16:18:19] one view. This result is relative big
[16:18:21] but still we have all the informations
[16:18:23] in one and it is more friendly for the
[16:18:26] users in order to consume our data
[16:18:28] instead of going and joining like all
[16:18:30] those four tables together. So now the
[16:18:32] next step we're going to put the result
[16:18:34] of this query in view in our database so
[16:18:37] that our end users can start consuming
[16:18:39] it. So how we going to do it? This is
[16:18:41] our combined query and now we're going
[16:18:45] to write the DDL for it. So create view
[16:18:47] and now we're going to give it the name
[16:18:49] order details and then as and we're
[16:18:53] going to put the whole thing in two
[16:18:54] parenthesis. So at the start and at the
[16:18:57] end and of course don't forget the
[16:18:59] schema. So our schema is sales sales dot
[16:19:04] then we have the view name just in order
[16:19:06] to have it in the correct schema and not
[16:19:08] in dbo. So everything is ready. Let's go
[16:19:11] ahead and execute it. So now let's go
[16:19:13] and check our database. So if you go and
[16:19:15] refresh, you will find our second view
[16:19:18] order details. So now let's go and test
[16:19:20] it. We're going to say select star
[16:19:24] from sales v order details. Let's go and
[16:19:30] execute it. And with that we are getting
[16:19:32] now a combined view that are showing all
[16:19:35] important informations from the
[16:19:36] database. So this is what the users can
[16:19:38] see. And with that the users don't care
[16:19:41] about how many tables do we have in the
[16:19:43] tables and how to join all those tables.
[16:19:46] We have only one view and we can start
[16:19:48] working on it. This is a very common use
[16:19:50] case for the
[16:19:54] views. Okay. Moving on to the next
[16:19:56] scenario to the next use case. We use
[16:19:58] SQL views in order to implement security
[16:20:01] and to protect our data in the database.
[16:20:03] In many scenarios, we have sensitive
[16:20:05] informations in our data and we cannot
[16:20:07] go and share it with everyone. So one of
[16:20:09] the best practices is to create views in
[16:20:12] order to protect your data before
[16:20:14] sharing it with the users. So let's
[16:20:16] understand what this means. So now let's
[16:20:18] understand first the scenario without
[16:20:20] views only tables. So now let's say that
[16:20:22] you have the table orders four columns
[16:20:24] and three rows and then you have like
[16:20:26] for example a manager that has an access
[16:20:28] directly to the database and start
[16:20:30] writing some queries in order to
[16:20:32] retrieve data. But in your project you
[16:20:34] have multiple people that has an access
[16:20:36] to your database like for example a data
[16:20:38] analyst and as well she is writing a
[16:20:40] script in order to retrieve data from
[16:20:42] the orders and as well you have maybe a
[16:20:44] students that has an access to your
[16:20:45] database and querying the data like any
[16:20:48] other role like a manager and data
[16:20:49] analyst. So as you can see you have now
[16:20:51] different rules in your project and all
[16:20:53] of them having the same rights by
[16:20:56] accessing directly your table. So a
[16:20:58] manager or data analyst or a student
[16:21:01] they are seeing the whole table all rows
[16:21:04] and all columns. And of course in the
[16:21:06] real projects this is a big problem.
[16:21:08] Sometimes the data are sensitive and you
[16:21:10] cannot give an access for everyone. And
[16:21:12] of course if you are using only tables
[16:21:14] this going to be a nightmare because you
[16:21:15] can go and create multiple tables but
[16:21:17] it's going to be really hard to make all
[16:21:19] those tables in sync. But instead of
[16:21:21] that we have views. So what you can do
[16:21:23] you can go and remove all accesses to
[16:21:25] the physical table but instead you can
[16:21:28] go and create multiple views for each
[16:21:31] role. For example you can go and create
[16:21:33] a view called orders managers and maybe
[16:21:36] you can give all the data and all the
[16:21:37] columns because the managers are allowed
[16:21:40] to see let's say sensitive data but
[16:21:42] still it's nice to create a view maybe
[16:21:43] you change your mind later and you go
[16:21:45] and remove something. Now let's say that
[16:21:47] for the data analyst you want to offer
[16:21:49] all the data but there is only one
[16:21:51] column that is very sensitive. So what
[16:21:53] you can do you can go and create another
[16:21:55] view called orders analyst. So in the
[16:21:57] view only three columns are available
[16:21:59] ABC and then you give access to all data
[16:22:02] analyst and with that you have protected
[16:22:05] this sensitive information. So we call
[16:22:07] this column level security. And now we
[16:22:09] come to our poor students. And here we
[16:22:11] create another view where we are not
[16:22:13] only protecting the column D but also we
[16:22:16] are protecting few rows like for example
[16:22:19] the row number three because we want to
[16:22:21] offer only few informations to the
[16:22:23] students. So we are protecting the
[16:22:24] columns and as well the rows and for
[16:22:26] that we can create another dedicated
[16:22:28] view called for example orders students
[16:22:31] and we can offer it to the students and
[16:22:33] with that we are doing column level
[16:22:35] security and as well row level security.
[16:22:38] So we are offering multiple views very
[16:22:40] easily without having to worry how to
[16:22:42] load the data from one table to another.
[16:22:45] So creating those views are really easy
[16:22:47] and provide us a perfect tool in order
[16:22:50] to manage the security of our data. So
[16:22:53] this is one very common use case of
[16:22:55] using views in data projects. All right.
[16:22:57] So now let's have the following task and
[16:22:59] it says provide a view for EU sales team
[16:23:02] that combines details from all tables
[16:23:04] and excludes data related to the USA. So
[16:23:08] the first part of the task is similar to
[16:23:10] what we have already done but we cannot
[16:23:12] offer all data for the user. So this
[16:23:15] time we are providing a view that is
[16:23:17] specifically created for a team the
[16:23:19] sales team. So the first part we have
[16:23:21] already done it where we are combining
[16:23:23] all details in one view. But the problem
[16:23:25] with the view that we have created that
[16:23:27] it shows all data. But now the
[16:23:29] requirement change we cannot show all
[16:23:30] data. We have to go and exclude the USA
[16:23:33] data from our details. So let's see how
[16:23:35] we can do that. It's very simple. We're
[16:23:37] going to go and grab the same query. We
[16:23:38] will not repeat that. So we have as well
[16:23:41] here joining tables and prepare
[16:23:43] everything. But instead of showing all
[16:23:46] data, what we're going to do, we're
[16:23:47] going to go and filter the data based on
[16:23:49] the customer country. So it's very
[16:23:51] simple. At the ends we will have a work
[16:23:54] clause where the C country is not equal
[16:23:59] to
[16:24:01] USA. So we have now a filter. Let's go
[16:24:04] and execute it. And with that, as you
[16:24:06] can see in the output, we are getting
[16:24:08] the orders that are not from USA. And
[16:24:11] with that we are protecting the data of
[16:24:12] the USA and the EU sales teams can
[16:24:16] access only their data. So it looks nice
[16:24:18] and protected. And with that we are
[16:24:20] doing now role level security. That
[16:24:22] means we are hiding now all the orders
[16:24:24] all the rows that are not allowed to be
[16:24:26] seen and consumed from this group of
[16:24:29] users. So now what is the next step? It
[16:24:31] is very simple. We're going to go and
[16:24:32] put everything in one view. So with that
[16:24:34] we have the query ready and we can go
[16:24:36] and create the new view. So we're going
[16:24:39] to call it create view. Then we need the
[16:24:42] schema and the name going to be almost
[16:24:45] the same. So order details but EU. And
[16:24:49] then we have to have as punch
[16:24:51] parenthesis like this. So everything is
[16:24:54] ready. Let's go and execute it. And now
[16:24:56] we can go and refresh in order to see
[16:24:58] our new view. If you still don't see it,
[16:25:00] you can go to the views over here and
[16:25:02] refresh as well to the folder. So with
[16:25:04] that I can see we have our new view.
[16:25:06] Now, of course, the next step we go and
[16:25:08] test it. So, let's create a new query.
[16:25:11] Select star
[16:25:13] from sales and v order details EU. So,
[16:25:19] let's test it. And with that, as you can
[16:25:21] see, we are getting the combined view
[16:25:23] only for the data that is relevant for
[16:25:25] the EU sales team. So, I'm not seeing
[16:25:28] here any USA records. So, with that, we
[16:25:30] are providing view that protects few
[16:25:33] rows like the orders from USA. So as you
[16:25:35] can see views are really great in order
[16:25:38] to provide security to our data whether
[16:25:40] we are protecting the columns or the
[16:25:42] rows. For example in our view we can say
[16:25:44] not only I want to remove the USA orders
[16:25:48] but let's say the department information
[16:25:50] is sensitive information and I would
[16:25:52] like to hide it from the view. So you
[16:25:53] can just simply remove it from the
[16:25:55] select and with that you are doing
[16:25:56] column level security. So now I have two
[16:25:58] options that I can provide to the users.
[16:26:01] The first option doesn't has any like
[16:26:03] role level security. It is the first
[16:26:05] view the order details. We don't have
[16:26:07] there any filters. So it's going to show
[16:26:09] all the orders. So here we give access
[16:26:11] only to people that are allowed to see
[16:26:12] all data. And we have another option the
[16:26:15] details with the EU. It doesn't show all
[16:26:17] data. It shows only a subset that is
[16:26:19] relevant for the EU team. So now it's
[16:26:21] really easy to control the security of
[16:26:23] my data using the views. And this is
[16:26:26] very important use case for the
[16:26:28] [Music]
[16:26:31] views. Okay. Okay, so moving on to the
[16:26:33] next use case for the views, we can use
[16:26:35] it in order to have more dynamic and
[16:26:37] flexibility in our projects. So let's
[16:26:40] understand what this means. If you have
[16:26:41] a table and you have multiple users
[16:26:43] accessing this table, now what can
[16:26:45] happen? you might change your mind about
[16:26:47] the design and the data model of your
[16:26:49] database where you can say you know what
[16:26:51] instead of having one table I'm going to
[16:26:53] go and split it into two tables or maybe
[16:26:55] another decision you say you know what
[16:26:57] I'm going to go and rename a table or in
[16:26:59] another day you decide you know what
[16:27:00] let's go and rename few columns or maybe
[16:27:03] add a column remove column so you are
[16:27:05] doing changes to your physical data
[16:27:06] model and you are changing stuff in the
[16:27:08] tables you know what's going to happen
[16:27:10] all those users that are accessing the
[16:27:12] tables going to scream because all of
[16:27:15] them having a complex SQL queries and
[16:27:18] your small changes at the tables are
[16:27:20] breaking everything in their queries and
[16:27:22] what this means this means escalations
[16:27:25] and you don't have anymore the freedom
[16:27:27] to change anything in your database
[16:27:29] without talking before to 100 people
[16:27:32] before doing any change. So we don't do
[16:27:34] that instead of that we use views. So
[16:27:36] what's going to happen? You create a
[16:27:38] view and you tell the users, okay, take
[16:27:40] this view and consume it and leave me
[16:27:42] alone. And now you have again your
[16:27:44] freedom to do any changes you want. So
[16:27:46] you go to your tables and do splitting,
[16:27:48] renaming and changing everything you
[16:27:50] want as long as you are updating the
[16:27:53] query between the table and the view to
[16:27:56] make sure that the users are not
[16:27:58] noticing any change. So for example, if
[16:28:00] you go and split the table into two
[16:28:01] tables, then you have to put in the view
[16:28:03] a join or union in order to reconstruct
[16:28:07] the same structure that the users are
[16:28:09] used to. And if you would like to rename
[16:28:11] something in your database, like instead
[16:28:13] of ID, you are now calling it a key. All
[16:28:15] what you have to do now is to go to the
[16:28:17] query of the view and rename it back
[16:28:20] from a key to an ID. So no one going to
[16:28:22] notice that you are doing changes to the
[16:28:24] physical tables. So using views and
[16:28:27] offering it to users is a gamecher for
[16:28:29] you because giving the users views kind
[16:28:32] of gives you more freedom dynamic and
[16:28:34] flexibility to change anything in your
[16:28:36] data model and the tables without
[16:28:39] getting any headache. So this is amazing
[16:28:41] use case for the
[16:28:45] views. Okay, moving on. We have a lot of
[16:28:47] use cases for the views. They are just
[16:28:49] amazing. So the next one is we can use
[16:28:52] views in order to introduce a second
[16:28:54] version of my data model in another
[16:28:56] language. So we could offer multiple
[16:28:58] languages to the users. Let's understand
[16:29:00] what this means. So now we have the
[16:29:02] following scenario. We have again our
[16:29:03] table orders where the data is persisted
[16:29:06] and everything in English and of course
[16:29:08] what happens sometimes you have like
[16:29:10] international team that are accessing
[16:29:13] your data. So you have team in USA and
[16:29:15] maybe you have team from Germany that as
[16:29:17] well are end users that want to access
[16:29:19] the data. Of course it depend on the
[16:29:22] number of users that are using your
[16:29:23] database. But if you have a lot of users
[16:29:26] that come from Germany and as well from
[16:29:28] India, it might make sense that you go
[16:29:30] and translate your data and the table
[16:29:33] structure into another language. So for
[16:29:36] example, instead of giving access to the
[16:29:37] table orders, we can create another view
[16:29:41] called bishong. That's the order in
[16:29:43] German. But not only you are giving a
[16:29:45] new name for the object, you could go as
[16:29:48] well and rename all the columns inside
[16:29:50] the view. Then the German users going to
[16:29:52] access the German view and it's going to
[16:29:55] be for them easier to understand the
[16:29:57] content of your database. The same thing
[16:29:59] for the Indian team. And for the Indian
[16:30:01] users, you can go and provide a view in
[16:30:04] Hindi. I'm not sure whether I'm
[16:30:05] pronouncing the word correct, but this
[16:30:08] is the first word that I said in Hindi.
[16:30:10] I don't promise that I'm going to learn
[16:30:12] the Hindi language because it's enough
[16:30:14] to learn Germany. So I'm trying as well
[16:30:16] to write this word Adish. I hope it is
[16:30:20] correct. And to be honest, it is really
[16:30:22] interesting how you write this word in
[16:30:24] Hindi. So now back to the topic. As you
[16:30:26] can see now we are using like the views
[16:30:29] in order to provide a translation for
[16:30:31] our database by just giving a new name
[16:30:34] for the views and as well for the
[16:30:35] columns. So this is another nice use
[16:30:37] case that I usually use as well in my
[16:30:39] projects in order to provide multi-
[16:30:41] languages for the data model that I have
[16:30:43] and I can do that with the power of
[16:30:49] views. Now we come to my favorite use
[16:30:52] case for the views and that I personally
[16:30:54] recommend in each project that we can
[16:30:56] use views as a virtual data ms in a data
[16:31:00] warehouse. So now why this is my
[16:31:02] favorite? Because I'm specialist in data
[16:31:03] warehouses and data leaks and this topic
[16:31:06] is very important decision in each
[16:31:08] project like this. So let's understand
[16:31:10] what this means. So now a classical data
[16:31:12] warehouse architecture based on the
[16:31:14] approach of enmon is going to look like
[16:31:16] this. We have multiple source systems
[16:31:18] where our data are spreaded and now we
[16:31:20] would like to go and extract all our
[16:31:22] data from these multiple sources and put
[16:31:25] it in one big database called data
[16:31:28] warehouse. And there will be a lot of
[16:31:29] operations on this central database like
[16:31:32] the data going to be first cleaned and
[16:31:34] then maybe integrated together and maybe
[16:31:36] we are building there some historical
[16:31:38] data. So we're going to be doing
[16:31:40] multiple steps in order to prepare the
[16:31:42] data for complex reporting and analyzes.
[16:31:44] And what we usually do in the data
[16:31:46] warehouse, we're going to store all
[16:31:48] those informations as a physical table.
[16:31:50] Now once we have built the data
[16:31:52] warehouse, what's going to happen? We're
[16:31:53] going to have multiple use cases that
[16:31:56] would like to access the data warehouse
[16:31:58] in order maybe to do some different
[16:32:00] reporting. Now, it's going to be very
[16:32:02] complex if we connect immediately like a
[16:32:04] reporting engine like PowerBI directly
[16:32:06] to the data warehouse. But instead of
[16:32:08] this, we try to split the data warehouse
[16:32:11] into multiple subsets like we can split
[16:32:13] it after topic or domain or departments
[16:32:16] and we call those subsets as data marts.
[16:32:19] So a data mart is always specific for a
[16:32:22] use case that's focus on one topic like
[16:32:25] for example we could have a dedicated
[16:32:26] mart for the sales and another data m
[16:32:29] which is dedicated only for finance
[16:32:31] topics but both of them comes from our
[16:32:34] data warehouse. Then the last layer
[16:32:36] going to be like for example the
[16:32:37] reporting and dashboarding maybe you
[16:32:39] have something like powerbi where you
[16:32:40] are creating a dashboard one data m like
[16:32:43] the sales or and as well maybe few stuff
[16:32:45] from other marts. But now the big
[16:32:47] question here in the data mart is how
[16:32:49] should I store the data? Should I store
[16:32:51] the data using tables or should I use
[16:32:54] views? And now the best practice says if
[16:32:56] you are building data marts then use
[16:32:58] views. And we call this virtual data
[16:33:01] marts. And there are many reasons why
[16:33:03] using views at a data mart it's way
[16:33:05] better than using tables. Like for
[16:33:07] example, it is more dynamic and quicker
[16:33:10] to change them cuz usually at the data
[16:33:12] mart you are building a lot of business
[16:33:13] logics and you want to have some
[16:33:15] flexibility and speed and the
[16:33:17] maintenance efforts is very simplified.
[16:33:19] No need to build any ETLs or data loads
[16:33:22] from the data warehouse to the data
[16:33:24] parts and this makes the data warehouse
[16:33:26] as a real single point of truth for your
[16:33:29] data. And once you start copying data
[16:33:31] from one layer to another layer, it's
[16:33:33] going to be really hard to maintain and
[16:33:35] chaotic and you have to have really
[16:33:37] restrict monitoring and data quality. So
[16:33:39] that's why using views you're going to
[16:33:41] always reflect the status of the data
[16:33:43] warehouse and this can help you of
[16:33:45] course with the data consistency which
[16:33:47] is a critical point in each data
[16:33:49] warehouse project. So there are many
[16:33:51] reasons why we build virtual data mart
[16:33:54] and we go with the views in this layer.
[16:33:56] So as you can see how the views are
[16:33:58] playing a very important role in
[16:34:01] building a data warehouse. So this is
[16:34:03] another amazing and very important use
[16:34:05] case of using views in your data
[16:34:11] projects. All right friends, so now
[16:34:13] let's have a quick recap about views. So
[16:34:15] we have learned that views are a virtual
[16:34:17] table that is based on the result of a
[16:34:20] query without actually storing any data
[16:34:23] in the database. So we use views in
[16:34:25] order to presist a complex SQL logic and
[16:34:28] query in the database. And we have
[16:34:30] learned that in some scenarios views are
[16:34:33] better than CTE because it improves the
[16:34:35] reusability and reduce the complexity in
[16:34:39] multiple queries which reduce the
[16:34:41] complexity of the whole projects where
[16:34:43] the CTE only improves the reusability in
[16:34:45] one query. And we have learned that as
[16:34:47] well the views in some scenarios are
[16:34:49] better than tables. We have learned that
[16:34:51] they are very flexible and easier to
[16:34:53] maintain since they don't store any data
[16:34:56] and it's really fast and easy to change
[16:34:58] stuff in the view compared to the
[16:35:00] tables. But as well we have learned that
[16:35:02] the tables are faster than views. Now
[16:35:04] there are like endless use cases for the
[16:35:06] views. But from my experience in
[16:35:08] projects I have choose for you the best
[16:35:10] use cases for the views. The first use
[16:35:11] case is if we find like a common
[16:35:14] repeated logic in SQL queries, we can go
[16:35:18] and store this logic in view in the
[16:35:20] database so that the users don't have to
[16:35:22] keep repeating the logic over and over.
[16:35:24] So we use views in order to have a
[16:35:26] central business logic. Another use case
[16:35:29] is to hide the complexity of your
[16:35:31] physical data model and to offer for the
[16:35:33] users and high abstracted layer. So you
[16:35:36] provide for the user something very
[16:35:37] friendly and you hide all the complex
[16:35:40] technical data model that you have in
[16:35:41] the database because not everyone is
[16:35:43] expert with your data model. One more
[16:35:45] use case we can use views in order to
[16:35:48] implement security and to protect our
[16:35:50] sensitive data in the database. So we
[16:35:52] can offer multiple views in order to
[16:35:54] protect columns or rows in a table.
[16:35:56] Another use case we have learned that we
[16:35:58] can use views in order to have more
[16:36:00] dynamic and flexibility for your
[16:36:03] database where we offer the users a
[16:36:05] table view and then you have the freedom
[16:36:07] to change stuff at your physical data
[16:36:09] model without affecting all users. And
[16:36:12] another nice use case for the views we
[16:36:14] can offer multiple languages from our
[16:36:16] data model. And the last use case we
[16:36:18] have learned how views play an important
[16:36:21] role in a data warehouse system. So
[16:36:23] views are amazing. All right my friends.
[16:36:25] So with that we have learned everything
[16:36:26] about this new objects the views in
[16:36:29] databases. This is amazing for
[16:36:30] flexibility and dynamic in your
[16:36:32] projects. Now in the next one we're
[16:36:34] going to learn how to create tables
[16:36:35] based on query and we will learn about
[16:36:38] the temporary tables. So let's
[16:36:43] go. Okay. So now first let's have a look
[16:36:46] again to the database structure. We have
[16:36:48] learned that in each SQL server there
[16:36:50] are multiple databases and in each
[16:36:52] database there are multiple schemas. And
[16:36:54] now inside each schema we can define
[16:36:57] multiple objects like we can define
[16:36:59] tables and views. And now we will be
[16:37:01] focusing on the object table. And we
[16:37:03] have learned as well we can use the
[16:37:05] language DDL data definition language
[16:37:07] which is a set of SQL commands in order
[16:37:10] to define this database structure. So we
[16:37:12] can use the SQL command create in order
[16:37:14] to define a new table or alter in order
[16:37:17] to update the structure or drop in order
[16:37:20] to drop the whole table. So a table is
[16:37:23] an object in the database structure and
[16:37:25] we have learned as well there is three
[16:37:27] levels of the database architecture and
[16:37:29] we have understood that at the logical
[16:37:31] level the middle one the conceptual
[16:37:33] level we deal as application developer
[16:37:36] or data engineer with the tables. So we
[16:37:39] define tables and relationship between
[16:37:41] them. So if you are an end user or a
[16:37:43] business analyst it's going to be little
[16:37:45] bit more hard to work with the tables.
[16:37:47] You have to be a developer or a data
[16:37:49] engineer. But working with tables is way
[16:37:52] easier than working with the complexity
[16:37:54] of the database at the physical level.
[16:37:57] So you don't have to be a database
[16:37:58] expert or administrator to work with
[16:38:01] tables. So the difficulty here is like
[16:38:03] in the middle. The abstraction is not
[16:38:05] that low but as well not that high. So
[16:38:07] now let's answer the question, what are
[16:38:09] tables? A database table is a structured
[16:38:12] collection of data. It's like a simple
[16:38:14] grid or spreadsheet that you might find
[16:38:16] in Excel. So it has different columns
[16:38:18] like each column represent a field like
[16:38:20] the ID, name, country and the table has
[16:38:23] as well multiple rows and each row
[16:38:26] represent a record or an entry of the
[16:38:28] data. So for example if this table is
[16:38:30] about the employees then each record
[16:38:33] each row is one employee. Now the
[16:38:36] intersect between the rows and columns
[16:38:38] we call it a cell and a cell is a single
[16:38:41] piece of data. Now the whole table going
[16:38:43] to be stored physically in the database
[16:38:45] as database files. So they are in the
[16:38:48] database like multiple files that are
[16:38:51] holding the informations about the table
[16:38:53] and those files are stored physically in
[16:38:56] that disk storage of the database. So
[16:38:58] that means your data inside the tables
[16:39:00] are not stored like a spreadsheet like
[16:39:02] an Excel but they are stored in special
[16:39:05] database files that usual developers and
[16:39:08] end users don't have access to those
[16:39:10] files. So tables again it's like an
[16:39:12] abstraction and representation for the
[16:39:14] actual data that are in the files. So
[16:39:16] actually each time you are querying the
[16:39:18] database table the database has to go to
[16:39:20] those files and fetch the data for you.
[16:39:22] All right. So this is what we mean with
[16:39:24] database
[16:39:28] tables. Okay. So now we have like
[16:39:31] different types of tables in SQL. We
[16:39:33] have tables that stays forever. We call
[16:39:35] it permanent tables. So they stay as
[16:39:38] long as you don't drop them. And you
[16:39:40] have another type of tables they called
[16:39:42] the temporary tables. And those tables
[16:39:45] going to be deleted and dropped once the
[16:39:47] session ends. So now we're going to
[16:39:48] focus first on the first type, the
[16:39:50] permanent tables. And there are two ways
[16:39:52] on how to create them. The first way is
[16:39:55] the classical way where you create table
[16:39:57] from the scratch and then you go and
[16:39:59] insert your data. So we call it create
[16:40:01] insert and the other way called create
[16:40:05] table as select. It's going to create as
[16:40:07] well the table but based on SQL query.
[16:40:10] So let's understand the differences
[16:40:12] between
[16:40:15] them. The create insert method is the
[16:40:18] classical way on how we define and
[16:40:20] create tables in SQL where first we have
[16:40:23] to go and create the table and define
[16:40:25] the structure and after that we insert
[16:40:27] our data into the database table where
[16:40:29] the other method the CTAs create table
[16:40:32] as select. And this one going to create
[16:40:34] a new table as well but this time based
[16:40:37] on the result of SQL query. So let's
[16:40:40] understand what this means. Okay. So now
[16:40:42] to the first method create insert. So
[16:40:44] here we have two steps. The first step
[16:40:46] is we have a DDL statements where we use
[16:40:49] the command create. So once we execute
[16:40:51] the first step what's going to happen
[16:40:52] the database engine going to go and
[16:40:54] create for us an empty table. It is a
[16:40:57] brand new table where we can hold our
[16:40:59] data. So with that we have defined the
[16:41:01] structure of our table but it's still an
[16:41:04] empty table. So now in the next step we
[16:41:06] have to go and insert our data inside
[16:41:08] this new table. So our data can come
[16:41:10] from multiple sources like a CSV file or
[16:41:13] maybe completely from another database
[16:41:15] where we are doing migration or maybe
[16:41:17] you are inserting manually your data or
[16:41:19] maybe it come from an application or you
[16:41:21] are doing data migration from one
[16:41:24] database to another. So at the end once
[16:41:26] you execute insert what's going to
[16:41:27] happen your data going to be inserted in
[16:41:30] this new table. So in this method we
[16:41:32] have like two steps. First we define the
[16:41:35] structure of the table and the second
[16:41:37] step we take care of inserting our data
[16:41:40] inside the table. And now this new table
[16:41:42] and your data going to be persisted
[16:41:44] permanently. Now let's check the other
[16:41:46] method the CTIS. Here it's only one step
[16:41:49] where you define a query and once you
[16:41:51] execute this query what going to happen
[16:41:53] the database has to retrieve the data
[16:41:56] from another table. So it might retrieve
[16:41:58] data from our new table that we just
[16:42:00] created using create insert. So once the
[16:42:02] query is executed we will get a result.
[16:42:05] So now what the database going to do
[16:42:07] going to create a new brand table but
[16:42:10] this time the definition and the data of
[16:42:12] this new table it doesn't come from any
[16:42:15] definition that we specify. it comes
[16:42:17] from the result of the query. So
[16:42:20] whatever structure that we have in the
[16:42:22] results, it going to be reflected in our
[16:42:24] new table. So again the definition and
[16:42:27] the data that we see in this new table
[16:42:29] comes one one to one from the result of
[16:42:32] our query. So in this type we don't have
[16:42:34] to define anything or to insert any
[16:42:37] data. We are just writing a query and
[16:42:39] the output of this query going to define
[16:42:41] the table. But in this method as you can
[16:42:43] see it always needs a database table in
[16:42:46] order to execute the query. But the
[16:42:47] create insert method we are creating
[16:42:50] something from the scratch. So these are
[16:42:52] the two different ways on how you create
[16:42:54] tables in SQL and the differences
[16:42:56] between
[16:43:00] them. Okay. So now you might ask you
[16:43:02] know what the CTAs are very similar to
[16:43:05] the views. We have a query and the
[16:43:07] output of this query going to be like an
[16:43:08] object in the database. So what are the
[16:43:11] differences between them? Let's check
[16:43:13] this. Now let's say that in our database
[16:43:15] we have a table that has three columns
[16:43:17] A, B, C. And now what we can do, we can
[16:43:19] go and create view based on a query. So
[16:43:21] you create the DDL statement in order to
[16:43:23] create the view in the database. And of
[16:43:25] course the database going to go and
[16:43:26] store the query in the database and it's
[16:43:29] going to be empty. So there will be no
[16:43:31] data because views does not store any
[16:43:33] data and the query of the view will not
[16:43:35] be yet executed. But now in the other
[16:43:37] hand if you go and create a table using
[16:43:39] CTIS. So here again we have a query
[16:43:42] attached to the object to the table. So
[16:43:44] here what happens the database has to
[16:43:46] execute the query in order to understand
[16:43:49] the structure and as well the data that
[16:43:52] should be inserted inside the table. So
[16:43:54] our SQL query going to be executed and
[16:43:57] the result of the query going to be
[16:43:59] inserted inside the table. So that means
[16:44:01] this new table is storing already the
[16:44:04] result of the query. So now this is the
[16:44:06] first differences between the table and
[16:44:08] view. As you create view the query will
[16:44:10] not be executed and we don't have
[16:44:12] anything about the result of the query
[16:44:15] where in the CTIS we have already result
[16:44:18] of the query stored inside the table and
[16:44:20] everything is prepared. So now let's see
[16:44:22] what's going to happen once the user
[16:44:24] selects something from the view. So now
[16:44:26] the database going to go for the first
[16:44:28] time executing the query of the view in
[16:44:31] order to fetch the data from the
[16:44:33] original table and then presented as a
[16:44:36] result for the user. But now in the
[16:44:37] other hand if the user go and query the
[16:44:40] table that is created from the CTIS. So
[16:44:42] now what can happen? SQL will not
[16:44:44] execute again the query of the CTIS
[16:44:47] because the database already done that
[16:44:49] and prepared everything. So that means
[16:44:51] we are not querying anything from the
[16:44:53] original table and the data can be
[16:44:55] directly fetched from the new table. So
[16:44:58] the user is going to get immediately the
[16:44:59] result from our table that is created
[16:45:01] from the CDIS. So here comes the second
[16:45:04] difference between the tables and views.
[16:45:06] The views are slower than CTIS and
[16:45:09] that's because the database has here an
[16:45:11] extra task. It must execute the query of
[16:45:14] the view in order to get the data. But
[16:45:16] in the CTIS the query going to be faster
[16:45:19] than the view because we have already
[16:45:21] executed everything and prepared it for
[16:45:23] the user. So that's why tables from CTIS
[16:45:26] are way faster than views. And now there
[16:45:28] is another difference and perspective
[16:45:30] about this which is from my point of
[16:45:32] view is more important than the
[16:45:34] performance. So now let's say that in
[16:45:36] the next day we are doing data updates
[16:45:38] on the original table like we are doing
[16:45:40] updates on the column C and as well in
[16:45:43] the column P. So now let's see what this
[16:45:44] means for the user if they are using
[16:45:46] views. So the user in the next day is
[16:45:48] executing again the same query and again
[16:45:50] here the database has to execute the
[16:45:52] query of the view in order to fetch the
[16:45:55] data from the original table. So that
[16:45:57] means today in the views we are getting
[16:45:59] different data than yesterday because we
[16:46:02] have a new data and new updates and the
[16:46:04] user in the result going to see as well
[16:46:06] the new updates and the fresh data. So
[16:46:08] the user is seeing exactly the status of
[16:46:12] the data in the original tables. But now
[16:46:14] let's see what going to happen if the
[16:46:15] user go and query the table from the
[16:46:18] CATS. So in the table of the CATS, we
[16:46:21] are still having the data from
[16:46:23] yesterday. All those new updates from
[16:46:25] the original data will not be reflected
[16:46:27] in this new table because once the user
[16:46:29] selects something from this table, the
[16:46:31] database will not go and query or fetch
[16:46:34] the new changes from the original table
[16:46:36] because we have already prepared the
[16:46:38] data from yesterday. So that means our
[16:46:40] user now is getting old data from the
[16:46:43] CTAs table and the only way to get new
[16:46:46] fresh data from the CTIS is to reexecute
[16:46:50] the CTIS query. And of course this is
[16:46:52] another step and it is harder to
[16:46:55] maintain the table from the CTAs and
[16:46:58] this is a big difference for the users
[16:47:00] between the views and the tables from
[16:47:02] the CTAs. Now think about views you are
[16:47:04] ordering a pizza at restaurants. So
[16:47:07] every time you are quering the view you
[16:47:09] are placing an order the chef going to
[16:47:11] go and make a pizza from the scratch
[16:47:13] using the freshest ingredients. So that
[16:47:15] means you are always getting a fresh hot
[16:47:18] pizza. And think about the CTS as like a
[16:47:21] frozen pizza from a grocery store. The
[16:47:23] pizza was prepared earlier and stored in
[16:47:26] the freezer. And if you want to eat it,
[16:47:27] you have to go and heat it up in the
[16:47:29] oven. But it's still not like a fresh
[16:47:31] pizza that is made on the spot and from
[16:47:33] the scratch. Now I made myself hungry
[16:47:35] because I love pizza. So I think I'm
[16:47:37] going to go for a quick break.
[16:47:40] [Music]
[16:47:51] Okay, so now let's check quickly the
[16:47:52] syntax of those two methods. The first
[16:47:54] one is create insert. So first step we
[16:47:56] have to go and create a table using a
[16:47:58] DDL statements. So we use the command
[16:48:00] create and then we have to tell SQL are
[16:48:03] we creating a table or view. In this
[16:48:05] scenario we are creating a table and
[16:48:07] then we specify the name of the table.
[16:48:09] Then after that we have two parenthesis
[16:48:11] and inside them we make a list of all
[16:48:13] columns that we need inside this table.
[16:48:15] So we have two columns the ID and the
[16:48:17] name. And after that we are defining the
[16:48:19] data type of those columns and maybe as
[16:48:21] well the length. There are a lot of
[16:48:23] options that we can add to this syntax
[16:48:25] but now we are just checking the
[16:48:27] simplest form of creating a table. Now
[16:48:29] the next step is that we need an insert
[16:48:31] statement. So we are saying insert into
[16:48:34] our new table the following values. we
[16:48:36] are inserting the id number one and the
[16:48:39] value for the name going to be frank. So
[16:48:41] this is a classical way on creating new
[16:48:43] table and inserting data to it. Now
[16:48:45] let's move to the second method the cas.
[16:48:47] Now this time we have an SQL query like
[16:48:50] select from where and some extra logic.
[16:48:53] So this is our query and then we're
[16:48:55] going to go and put our query inside a
[16:48:58] DDL statement. It's like we have done it
[16:49:00] in the views. It's exactly like we have
[16:49:02] done it in the views but this time
[16:49:05] instead of saying view we're going to
[16:49:06] say table. So again we have the create
[16:49:08] command and we are creating a table then
[16:49:11] the name of the table and then we say as
[16:49:14] and then we have two parenthesis and
[16:49:16] inside them we have our query and this
[16:49:18] is where the name come from create table
[16:49:22] as select cas. So it is very simple in
[16:49:26] one statement you have everything you
[16:49:28] are creating a new table and as well you
[16:49:31] are inserting the data that comes from
[16:49:33] this query. Now this syntax is used in
[16:49:36] databases like MySQL, Postgress and
[16:49:39] Oracle. But in MySQL we have like a
[16:49:41] shorter way on how to do it. Again we
[16:49:44] have our query select from where. But
[16:49:47] now in SQL server we can insert a
[16:49:49] command between the select and from like
[16:49:52] this. So we are saying select the
[16:49:55] following columns into new table. So we
[16:49:59] have this keyword into then the table
[16:50:01] name and then you continue after that
[16:50:03] with your query from where aggregations
[16:50:05] and so on. So here it's like the DDL is
[16:50:08] inside your query itself but in the
[16:50:10] other databases you can have like the
[16:50:12] query is separated from the DDL
[16:50:14] statements. Personally, I prefer this
[16:50:16] syntax than having this into because if
[16:50:19] you have like big complex query, this
[16:50:21] can be really hard to see and to miss
[16:50:23] the column selection. So, this is the
[16:50:25] syntax of creating a new table from a
[16:50:28] query the CTAs in different
[16:50:30] [Music]
[16:50:33] databases. Okay. So, now we're going to
[16:50:35] check the scenarios and use cases where
[16:50:37] it makes sense to use. So, let's start
[16:50:40] with the first one. Now we have learned
[16:50:42] before it makes sense to have a complex
[16:50:44] logic stored inside the database so that
[16:50:47] our end users don't have to keep
[16:50:49] repeating the same logic over and over
[16:50:51] and it's as well maybe complicated for
[16:50:53] some users. So that's why we have used
[16:50:55] views and the result of the view going
[16:50:57] to be used from our users. So everything
[16:50:59] can stay easy and friendly to consume
[16:51:01] for our users. But now what might happen
[16:51:04] is that the logic of the view could be
[16:51:06] very complicated and needs a lot of time
[16:51:09] to be executed from the database. So it
[16:51:12] takes really long time until we get the
[16:51:14] intermediate result from the database.
[16:51:16] So that means if it's going to takes 30
[16:51:18] minutes then each users has to wait 30
[16:51:21] minutes until the query is executed and
[16:51:23] none of your users going to be happy
[16:51:25] with this situation. In this scenario,
[16:51:27] if this happens, you have to try maybe
[16:51:29] to optimize the query. But if you cannot
[16:51:32] do anything about that, you have to
[16:51:34] switch the view to CTAs table. So now
[16:51:37] what you have to do, you have to take
[16:51:38] the same logic and then put it
[16:51:41] in so that the intermediate results are
[16:51:44] stored in a table. And of course at the
[16:51:46] moment of creating the table, it will
[16:51:48] take 30 minutes. It will take long time
[16:51:50] because it is the same query and the
[16:51:52] database going to need the time until
[16:51:54] creating the intermittent results. But
[16:51:56] the big advantage is that once
[16:51:58] everything is prepared maybe at the
[16:52:00] night at the morning once your users are
[16:52:03] like online and start querying the data
[16:52:06] they have everything prepared. So the
[16:52:08] user is going to go and start selecting
[16:52:10] and analyzing the intermediate result
[16:52:12] but this time using the table that you
[16:52:15] have created from the CTAs and the
[16:52:17] response time going to be for all users
[16:52:19] again normal and fast. So if you have a
[16:52:21] scenario where your views are very slow
[16:52:24] you have to go and prepare the data at
[16:52:25] the night using the CTIS and prepare the
[16:52:28] tables to be analyzed from the end
[16:52:30] users. So this is the most common use
[16:52:32] case for the CTIS and this scenario
[16:52:35] happens a lot in projects where you
[16:52:37] decide to go instead of views to go with
[16:52:40] the CTIS in order to have persistence
[16:52:42] data and you gain performance. Okay, so
[16:52:44] finally back to SQL let's go and create
[16:52:47] a table using now we're going to go and
[16:52:49] create a table that shows the total
[16:52:51] number of orders for each month. Let's
[16:52:53] go and do it. So first what do we need?
[16:52:55] We need a query. So let's write it.
[16:52:58] select. I'm going to go with the date
[16:53:00] name in order to get the name of the
[16:53:02] month from our order dates and we're
[16:53:06] going to call it order month. And then
[16:53:09] we're going to go and aggregate the data
[16:53:11] by counting the order ID for total
[16:53:16] orders from our table sales orders. Uh
[16:53:20] don't forget to group by our month. So
[16:53:24] something like this. Let's go and
[16:53:27] execute it. So the result is very
[16:53:28] simple. We have the order month and the
[16:53:30] total orders. So we have two columns and
[16:53:33] three rows. So we have our query and of
[16:53:35] course we didn't create anything yet.
[16:53:37] Now in SQL server in order to create a
[16:53:39] table from the query what we're going to
[16:53:42] do exactly before the from we're going
[16:53:45] to write into and now we have to specify
[16:53:47] the schema and the table name. I'm going
[16:53:49] to stay with the schema sales and I'm
[16:53:51] going to call it monthly
[16:53:54] orders like this. So that means we have
[16:53:57] our query and the DDL is exactly between
[16:54:00] the from and select. So now if I go and
[16:54:02] execute this what going to happen we
[16:54:05] will not see here the result of the
[16:54:06] query. We're going to get here like
[16:54:08] three rows affected because this is a
[16:54:10] DDL statement. It is not anymore a query
[16:54:13] and the database is telling us I have
[16:54:14] created now a table with three rows. So
[16:54:17] now if you check our tables we don't see
[16:54:18] it yet. Let's go and refresh and check
[16:54:20] again the tables. Now we can see our
[16:54:22] table here sales monthly orders. Now of
[16:54:25] course we have to go and check whether
[16:54:27] everything is fine. So let's go and
[16:54:29] select the rows from our new table sales
[16:54:33] monthly
[16:54:35] orders. So let's go select it first and
[16:54:39] execute. And now we can see again the
[16:54:42] result of our query. But we are not
[16:54:44] writing here the query. We are just
[16:54:45] selecting it from the table. So our data
[16:54:47] is stored in our table. And we can go
[16:54:50] and check the structure of this table.
[16:54:52] So if you go to the columns you can see
[16:54:54] we have here the order month and the
[16:54:56] total orders and those informations
[16:54:58] comes from our query. So SQL is saying
[16:55:00] here the order month is a var which is
[16:55:02] correct because here we have the names
[16:55:04] of the month. So SQL is able to define
[16:55:07] the data type of the table from our
[16:55:09] query and the second column the total
[16:55:11] orders it is an integer and that's
[16:55:13] because we have here numbers. So as you
[16:55:15] can see SQL is defining the structure of
[16:55:17] the table based on the result of our
[16:55:20] query over here. And of course the data
[16:55:22] inside the table comes as well from the
[16:55:24] query. And the result of this table
[16:55:26] going to stay like this as long as you
[16:55:28] don't change anything. So if you go and
[16:55:30] close this and open it after one year
[16:55:32] it's going to show exact same results.
[16:55:34] So it's going to live in the database as
[16:55:36] long as you don't drop this table. But
[16:55:38] if things change in the table orders,
[16:55:41] this table will not be updated
[16:55:42] automatically like we have learned in
[16:55:44] the views. So now if you want to say you
[16:55:46] know what I would like to go and drop
[16:55:47] this table well it is very simple just
[16:55:50] go and say drop table and the table name
[16:55:53] over here. So make sure you select it
[16:55:55] and execute it. And now if you go over
[16:55:57] here and refresh. So let's check the
[16:55:59] tables. You can see here the table is
[16:56:05] dropped. And now if you say you know
[16:56:07] what let's go and refresh the table that
[16:56:10] come from the CTAs every day so that we
[16:56:13] always get refresh data inside this
[16:56:15] table. So now let's go and execute again
[16:56:18] our CIS. And with that if we go and
[16:56:20] refresh we're going to find again our
[16:56:22] table inside it. Now if you go and
[16:56:24] execute it one more time in order to
[16:56:26] refresh the data of the table what you
[16:56:28] going to get? You're going to get an
[16:56:29] error. The database going to tell you we
[16:56:31] have already this table so we cannot
[16:56:32] recreate it. So now the question is how
[16:56:35] we can update the the content of this
[16:56:37] table. Well, we have to go and drop it
[16:56:39] first and then recreate it. And if you
[16:56:41] want to put everything in one statement,
[16:56:43] we have to go and use the TSQL. It is
[16:56:46] transacts SQL. It's like extension where
[16:56:48] you can do some programming inside SQL.
[16:56:51] So in order to do that, what we're going
[16:56:53] to do, we're going to go at the start
[16:56:54] over here and we're going to make an if
[16:56:57] logic. So we're going to go and search
[16:56:58] for the objects. So we're going to say
[16:57:01] if the object ID and now we have to go
[16:57:03] and specify the name of this object
[16:57:06] together with the schema. Make sure to
[16:57:07] select everything sales monthly order
[16:57:10] and put it inside here. And then we have
[16:57:12] to define the type of this object. And
[16:57:15] here we're going to go with you. It is
[16:57:17] userdefined table. So we are saying if
[16:57:21] the object sales monthly orders is not
[16:57:24] null. So that means it exist. So what
[16:57:27] you want to do? we have to go and drop
[16:57:29] it. I'm going to take the statement from
[16:57:31] here and then we're going to put it
[16:57:33] after the if over here. So we are saying
[16:57:36] if this table exist then drop the table
[16:57:39] otherwise don't do anything because we
[16:57:41] don't have any new table and the query
[16:57:43] going to work and at the end of the TSQL
[16:57:46] we have go in order to say the TSQL is
[16:57:49] done and then our usual query after all
[16:57:51] that. So let's go and execute the whole
[16:57:53] thing and as you can see it is working.
[16:57:56] So what happens? The database did find
[16:57:58] this table and drop it and then executed
[16:58:01] our query. So if you keep executing
[16:58:03] this, you are just refreshing the
[16:58:05] content of this table. So this is how we
[16:58:07] work with the CTAs in
[16:58:13] SQL. All right, moving on to another
[16:58:15] common use case for the CTAs that I
[16:58:17] usually use as well in my projects. We
[16:58:19] use CDS in order to create a persistent
[16:58:22] snapshot of the data at specific time in
[16:58:25] order to analyze data quality issue. So
[16:58:28] let's understand what this means. Now in
[16:58:29] some scenarios you have like a table and
[16:58:31] you are analyzing an issue. So there is
[16:58:33] like a data quality issue at your data
[16:58:35] and you are analyzing this scenario in
[16:58:37] order to understand why it happens. But
[16:58:39] the problem is that at the same time
[16:58:41] there will be updates on the table and
[16:58:44] your data is changing. So there will be
[16:58:46] updates maybe on some fields or you are
[16:58:48] getting new records and everything is
[16:58:50] getting mixed up and you will not be
[16:58:52] able to analyze the scenario where the
[16:58:54] data quality issue happened. So now it's
[16:58:57] almost impossible to find the ro cause
[16:58:59] of your issue. But instead of that what
[16:59:02] we do if we have like an issue of the
[16:59:03] data we go and create a fixed persisted
[16:59:06] snapshot of the data in a separate table
[16:59:09] using CTS so that we make sure nothing
[16:59:11] is changing and everything is fixed. And
[16:59:14] with that I can keep doing my analysis
[16:59:16] on the same data without the worry that
[16:59:19] data are getting changed. So this is
[16:59:21] another way why we use CTS in projects
[16:59:23] to make sure that we have snapshot of
[16:59:25] the data to ensure that our analyzes are
[16:59:28] done on the same scenario that caused
[16:59:30] the buck and going to be used as a
[16:59:32] foundation for finding the problem and
[16:59:35] fixing
[16:59:39] it. All right, moving on to another use
[16:59:41] case of the CTAs. We can use it in order
[16:59:44] to create our data m to make it physical
[16:59:47] data m instead of virtual data ms using
[16:59:50] views. So let's understand what this
[16:59:52] means now. As we learned before, if you
[16:59:53] have a data warehouse system, our data
[16:59:56] warehouse layer going to store the data
[16:59:57] inside tables. But for the second layer,
[17:00:00] the data m, we can go and use views in
[17:00:02] order to have dynamic and flexibility in
[17:00:04] order to generate multiple data ms. And
[17:00:07] we called it the virtual layer. But now
[17:00:10] in some scenarios if things get
[17:00:12] complicated your data m and reports
[17:00:14] going to be slow because there for each
[17:00:16] action you are generating a query. So
[17:00:19] the powerbi reports and dashboards are
[17:00:21] creating queries in your data marts and
[17:00:23] your data marts have always to go to the
[17:00:25] data warehouse in order to retrieve the
[17:00:28] data for the reports and the whole thing
[17:00:30] could take minutes or maybe sometimes
[17:00:32] hours. So in these scenarios we cannot
[17:00:34] stay using views because they are
[17:00:36] slowing everything down. But instead of
[17:00:38] that we have to convert our data mart to
[17:00:41] a physical layer. That means instead of
[17:00:43] using views we have to go and use
[17:00:45] tables. And one very common way in order
[17:00:47] to generate the tables of the data marts
[17:00:49] on daily basis is to use queries between
[17:00:53] the data warehouse layer and the data
[17:00:55] mart layer. It's still going to take
[17:00:56] maybe 30 minutes. That's why you can go
[17:00:58] and prepare the data at the night. But
[17:01:00] at the reporting layer where things and
[17:01:02] the performance really matters, the
[17:01:04] performance going to be better because
[17:01:05] the response time from the tables is way
[17:01:08] faster than views and the reports don't
[17:01:10] have always to waste time waiting for
[17:01:12] the data marts to get data from the
[17:01:14] warehouse. So this is another use case
[17:01:16] where you use CTAs where the views at
[17:01:18] the data marts are slow and we have to
[17:01:20] go and replace them with stables using
[17:01:22] CTAs to speed up things. But still my
[17:01:25] recommendation here is that start first
[17:01:27] with the views. So create a virtual data
[17:01:29] mart using views because the
[17:01:31] implementation going to be very dynamic
[17:01:33] and fast and you are always getting
[17:01:35] fresh data from the warehouse but maybe
[17:01:37] later if you notice okay some data ms
[17:01:39] and models are complex then maybe go and
[17:01:42] replace few marts from views to tables
[17:01:45] using cis. So this is another use case
[17:01:47] for the and it is nice workaround for
[17:01:50] your data warehouse system. All right
[17:01:52] friends, so with that we have covered
[17:01:53] now the first type of the tables that we
[17:01:55] have in databases. The permanent tables
[17:01:58] where you create a table and it's going
[17:01:59] to live forever until you go and drop
[17:02:01] it. Now we're going to talk about
[17:02:03] another type of tables in databases. We
[17:02:05] have the temporary tables. So let's
[17:02:07] understand what are temporary
[17:02:12] tables. So temporary tables or sometimes
[17:02:15] you call them as a shortcut temp tables.
[17:02:18] They store intermediate results in a
[17:02:20] temporary storage in the database during
[17:02:22] a session and the database automatically
[17:02:25] drop these tables after the session
[17:02:28] ends. So let's understand what this
[17:02:30] means. Now we have learned in the CIS we
[17:02:32] could use a query in order to retrieve
[17:02:35] data from one table and then it puts the
[17:02:37] intermediate results in brand new table
[17:02:40] in the database. So with that we are
[17:02:42] creating another table based on a query.
[17:02:44] The same thing for the temporary tables.
[17:02:47] We have as well a query that goes and
[17:02:49] retrieves the data from a table and as
[17:02:51] well the database going to go and create
[17:02:53] new brand table in the database that has
[17:02:55] the structure and the data from the
[17:02:58] result of the query. So it is exactly at
[17:03:00] the CTIS. What is the difference here?
[17:03:02] Well, it is about the lifetime of the
[17:03:05] table. Now the database tables that you
[17:03:07] have created using create insert or CTIS
[17:03:10] those tables going to stay permanent and
[17:03:12] they're going to live in the database as
[17:03:14] long as you don't drop them. So even if
[17:03:17] the system is completely offline the
[17:03:19] data going to stay at the database once
[17:03:21] it is online again but the temporary
[17:03:23] tables going to get deleted and dropped
[17:03:26] from the database automatically once the
[17:03:29] session ends. So what session means like
[17:03:32] once you open the client and you connect
[17:03:34] to the database and you are start doing
[17:03:36] queries we call the time between
[17:03:38] connecting ourself to the database and
[17:03:40] disconnecting from the database we call
[17:03:43] this a session. So that means once you
[17:03:45] close the client and you disconnect from
[17:03:46] the database and maybe shut down your PC
[17:03:48] and do something else. What going to
[17:03:49] happen? The database going to go and
[17:03:51] destroy and delete all the temporary
[17:03:54] tables that you have created during the
[17:03:56] session. So that mean the table going to
[17:03:58] live as long as you have a session and
[17:04:00] you can access during this time the
[17:04:02] table as you are accessing any other
[17:04:04] permanent table. So this is what we mean
[17:04:06] with temporary tables or sometimes we
[17:04:08] call it as a shortcut temp
[17:04:10] [Music]
[17:04:13] tables. Okay. So now let's check the
[17:04:16] easiest syntax ever. So for the
[17:04:18] temporary table the syntax going to look
[17:04:19] like this. you're going to have like a
[17:04:21] query select from where and as we
[17:04:23] learned in the CTIS if you go and say
[17:04:26] into then the table name it's going to
[17:04:28] go and create a physical new table but
[17:04:30] now if you want it as a temporary table
[17:04:32] what you going to do you're going to
[17:04:34] just put hash before the name of the
[17:04:36] table then SQL can understand okay now
[17:04:38] we are talking about temporary table and
[17:04:40] the database going to store it in that
[17:04:42] temporary storage so it is very simple
[17:04:45] this is the syntax of that temporary
[17:04:47] tables so so far we have learned that we
[17:04:49] have a database called sales DB and
[17:04:51] inside it we can find the tables that we
[17:04:53] have created the customers, employees,
[17:04:56] orders and so on. Those are our tables
[17:04:58] and they are always there like if you go
[17:05:00] and close everything and then start it
[17:05:02] or in the next day you're going to find
[17:05:03] always those tables with the same data.
[17:05:06] So they're going to exist as long as we
[17:05:07] are not dropping them. Now the question
[17:05:10] is where do we find the temporary
[17:05:12] tables? Well, as we learned, if you go
[17:05:14] over here at the system databases, you
[17:05:16] will find multiple databases from the
[17:05:19] SQL server and normally only the
[17:05:21] database administrator has an access to
[17:05:23] this and one of those databases called
[17:05:25] temp DB, temporary database. So, let's
[17:05:28] go inside it. Now, we can find multiple
[17:05:30] objects and one of them we can find here
[17:05:32] the temporary tables. And now, of
[17:05:34] course, we don't have anything inside it
[17:05:36] because we didn't create anything. So,
[17:05:38] let's go and create one. We have already
[17:05:40] an open session and active session with
[17:05:41] the SQL server. As you can see here, we
[17:05:43] are connected to the database and we can
[17:05:45] start creating temporal tables. So now
[17:05:47] what is the plan? I would like now to do
[17:05:49] few modifications on the table orders.
[17:05:52] But I will not do it directly at the
[17:05:54] table orders. I would like to take a
[17:05:56] copy from the sales DB and create from
[17:05:59] it a temporary table. So let's go and do
[17:06:01] that. What do we need first? We need a
[17:06:04] query. So I would like to select
[17:06:06] everything all the columns all the rows
[17:06:08] from the table orders. So from sales
[17:06:12] orders. So this is my query. Now so far
[17:06:16] nothing is created. We have only select
[17:06:17] statements. But now in order to create a
[17:06:20] temporary table what we're going to do
[17:06:21] we're going to put a statement between
[17:06:23] the select and from. So exactly before
[17:06:26] the from go over here and say into then
[17:06:29] in order to make sure it is a temporary
[17:06:31] table we use hash and then the table
[17:06:33] name. So we're going to call it orders.
[17:06:35] So that's it. We have our query and in
[17:06:38] between we have the into and make sure
[17:06:40] you are using hash in order to be a
[17:06:42] temporary table. So let's go and execute
[17:06:44] it. And now we can see that 10 rows are
[17:06:46] affected and we don't have any error.
[17:06:48] And now of course we cannot see it yet
[17:06:50] because we have to go and refresh the
[17:06:52] object explorer. So let's go and do
[17:06:53] that. And now let's expand it. And now
[17:06:56] we can see our temporary tables. As you
[17:06:58] can see it is at the schema dbo because
[17:07:01] we haven't defined any schema. And this
[17:07:03] is the default one from the database. So
[17:07:05] nice. Now we have the table and let's go
[17:07:07] and check few stuff. So let's go and
[17:07:09] select the table itself. So select star
[17:07:12] from and make sure to say hash orders.
[17:07:16] Let's go and select it. And now we are
[17:07:18] getting the data from the temporary
[17:07:20] table and not from the original table.
[17:07:22] The orders in the database sales DB. So
[17:07:25] all those informations comes from the
[17:07:27] temporary table. Now, of course, you can
[17:07:29] do whatever you want to this temporary
[17:07:30] table because it's not that important
[17:07:32] and it's anyway going to get deleted.
[17:07:34] So, let's say that I would like to
[17:07:36] delete all the orders where the order
[17:07:39] status equal to delivered. So, let's go
[17:07:41] and do that. What we're going to do
[17:07:43] delete from our hash orders. So, make
[17:07:47] sure we are selecting the temporary
[17:07:49] table and then where we're going to say
[17:07:51] the
[17:07:53] order status equal to what I say
[17:07:57] delivered. Yeah, delivered. So delivered
[17:08:00] like this. Let's go and execute it.
[17:08:02] Okay, with that it says five rows are
[17:08:04] affected. Let's go and select it again.
[17:08:07] So
[17:08:08] select from
[17:08:11] orders and let's check that. So as you
[17:08:14] can see now we don't have all orders. We
[17:08:16] have only the orders where the status
[17:08:18] equal to shipped. So all delivered
[17:08:19] orders are removed. And now we can do
[17:08:22] whatever we want to this copy. We can
[17:08:23] analyze it. We can modify it. We can go
[17:08:26] and insert a new data. So we can do
[17:08:28] whatever manipulation we want on this
[17:08:30] copy. And now if you say, you know what,
[17:08:31] I like this result and I would like to
[17:08:33] have it not only during the session.
[17:08:36] Maybe I'm going to need it for tomorrow
[17:08:37] or something. So now what we're going to
[17:08:39] do, we're going to do the exact
[17:08:40] opposite. We're going to now store the
[17:08:42] result of the temporary table back to
[17:08:44] our database so that we don't lose this
[17:08:47] intermediate result. So in order to do
[17:08:49] that, we're going to say into and then
[17:08:52] make sure to specify the sales dot
[17:08:54] because we want to select the correct
[17:08:56] schema and then let's say it is orders
[17:08:59] and I'm going to call it test like this.
[17:09:02] So let's go and execute it. So it says
[17:09:04] five rows are affected. Now we have to
[17:09:07] see those informations in the sales DB.
[17:09:09] We still don't have this table over
[17:09:10] here. So right click on the DB and then
[17:09:13] refresh it. So let's go again to the
[17:09:15] tables. And now you can see we have our
[17:09:17] new table orders test. So it is amazing
[17:09:20] right? What we have done is we have took
[17:09:22] a copy from the original table orders to
[17:09:25] a temporary space. We have done some
[17:09:28] modifications and play with the data and
[17:09:30] we have done some analyzes and then the
[17:09:32] end result of our temporary table. We
[17:09:35] have loaded back to another new table
[17:09:38] called orders test in order maybe in the
[17:09:40] next day to keep working on it. So it is
[17:09:43] really nice way to do changes in place
[17:09:45] where you say you know what it is
[17:09:47] temporary and whatever mistakes you
[17:09:48] makes it's okay it is like playground.
[17:09:51] So now we still have an active session
[17:09:53] with the database and our temporary
[17:09:55] table going to be always here. Now let's
[17:09:57] see what going to happen if we end our
[17:09:58] session. So in order to do that let's go
[17:10:01] and just close everything. So I will
[17:10:04] just close and we'll not store anything.
[17:10:07] So with that we have now ended the
[17:10:09] session. Let's go and start it again and
[17:10:11] see whether we still have the temporary
[17:10:13] table. So we have now again to connect
[17:10:15] to the SQL server and now we have
[17:10:18] another session. So that means the old
[17:10:20] session is already lost. Let's go to the
[17:10:22] databases to the system databases to the
[17:10:25] temp DB and let's go to the temporary
[17:10:27] tables. As you can see the database
[17:10:30] already cleaned up everything and this
[17:10:32] space is again empty for any new
[17:10:34] temporary table that I'm going to
[17:10:35] create. So as you can see once you close
[17:10:38] the session everything going to get
[17:10:39] lost. Now let's go back to our sales DB
[17:10:41] over here to the tables. We can see the
[17:10:44] table that we have created orders test
[17:10:46] it is still living here and still has
[17:10:48] like the data that we have created. So
[17:10:50] this is how things works with the
[17:10:52] temporary tables in
[17:10:56] SQL. Now let's see how the database
[17:10:59] server executed that temporary SQL. So
[17:11:02] now let's say that you are as a data
[17:11:04] analyst. You have created a query and
[17:11:05] then you say into in a temporary table.
[17:11:07] Now the database engine going to
[17:11:09] identify the query and first it's going
[17:11:11] to go and execute the query and then
[17:11:13] it's going to go and execute it and
[17:11:15] maybe we're going to get the data from
[17:11:16] the table orders and after the query is
[17:11:19] executed the database engine now has the
[17:11:21] results. Now two things can happen.
[17:11:23] First the database engine going to go
[17:11:25] and store the metadata informations in
[17:11:28] the system catalog. And now the second
[17:11:30] thing the database engine going to
[17:11:31] create a table but this time not in the
[17:11:34] users but in the temporary storage in
[17:11:37] the disk. So the table going to live
[17:11:38] there for a short time. And now what you
[17:11:40] can do you can write multiple SQL
[17:11:42] queries that are doing maybe multiple
[17:11:44] analysis on top of this table. So each
[17:11:46] time you select something the database
[17:11:48] engine has to go to the temporary
[17:11:50] storage and fetch the data from there.
[17:11:53] And now once you are finished and let's
[17:11:54] say you close your client the session
[17:11:57] between you and the database going to
[17:11:58] ends and now the database going to
[17:12:00] understand okay there is no more
[17:12:02] connection to this user and it going to
[17:12:04] go and clean up now the temporary
[17:12:07] storage with any tables that are created
[17:12:10] from this session. So that means the
[17:12:12] database is automatically cleaning up
[17:12:14] the storage maybe for other sessions. So
[17:12:16] this is how the database engine works
[17:12:18] with the temporary tables.
[17:12:24] So now the question is why do we need
[17:12:26] temporary tables? Let's see the
[17:12:27] following scenario. Now let's say that
[17:12:29] in our source database we have a table
[17:12:31] called orders and now we would like to
[17:12:33] go and load the table in our data
[17:12:34] warehouse. We have to do several
[17:12:36] transformations in order to prepare the
[17:12:38] data for the analyzes in the data
[17:12:40] warehouse. So maybe you have one query
[17:12:41] to remove the duplicates and another one
[17:12:43] to handle the nulls and maybe you are
[17:12:45] doing filtering and cleaning up and the
[17:12:48] last step you would like to aggregate
[17:12:49] the data. And now of course those
[17:12:51] queries those transformations want to
[17:12:53] change the content of the table orders
[17:12:56] and there is no scenario where you can
[17:12:58] do that directly on the source database
[17:13:00] and of course this is not allowed.
[17:13:02] That's why in data warehousing we have
[17:13:04] to go and get our own copy of the data
[17:13:06] and then on top of this data we can do
[17:13:08] our transformations. Now one way to do
[17:13:10] this using the temporary tables. So you
[17:13:13] have one script in order to extract the
[17:13:15] data from the table orders and put it in
[17:13:17] temporary table as an intermediate
[17:13:19] results and then you come with the
[17:13:21] transformations and all those queries
[17:13:23] and they start manipulating and changing
[17:13:25] the data of this extra copy in the
[17:13:28] temporary table and the last step you
[17:13:30] have the load where you go and load the
[17:13:32] final version of the intermediate
[17:13:34] results in the database. This is if you
[17:13:36] would like to do the whole ETL before
[17:13:39] inserting the data to the database. So
[17:13:41] now the orders table and the final table
[17:13:43] in the data warehouse both of them are
[17:13:45] tables. So they are permanent tables and
[17:13:48] they will stay there as long as we don't
[17:13:50] drop them. So they are very important
[17:13:52] tables. But now for the intermediate
[17:13:54] results it is not that important. It is
[17:13:56] just an intermediate step that we have
[17:13:58] done in order to have our extra copy of
[17:14:01] the data to manipulate it and so on in
[17:14:03] order to prepare it to be inserted in
[17:14:05] the data warehouse. So after we loaded
[17:14:08] it in the data warehouse, this copy of
[17:14:10] the data is not anymore important. It
[17:14:12] shouldn't stay like for a long time.
[17:14:14] That's why in this scenario, maybe we
[17:14:16] can go and use the temporary tables
[17:14:18] instead of normal tables for the
[17:14:20] intermediate results. And that's because
[17:14:22] only of one advantage is that the
[17:14:24] database going to go and do an automatic
[17:14:27] clean up after the host session ends. So
[17:14:30] it comes out of the box automatically
[17:14:31] from the database. So that means I don't
[17:14:34] have to deal with the dropping mechanism
[17:14:36] of this table for the next load. If
[17:14:38] there is like something wrong in the
[17:14:40] data warehouse, you would like always to
[17:14:41] check the copy where the transformations
[17:14:44] are done in order to debug and find
[17:14:46] issues. So I don't normally use
[17:14:47] temporary tables in these scenarios, I
[17:14:49] use just normal tables. But for other
[17:14:52] small projects, maybe this makes sense.
[17:14:53] So this is one use case on when to use
[17:14:56] the temporary tables in your projects.
[17:14:58] We use it in order to store intermediate
[17:15:00] results temporary until we are done with
[17:15:03] the session and then once we are done
[17:15:04] the database can go and drop that
[17:15:07] temporary
[17:15:11] table. All right guys, now a quick talk
[17:15:13] about the temporary tables. To be
[17:15:15] honest, I never use this in my projects.
[17:15:17] If I need an intermediate results in one
[17:15:20] query, I can go and use the CTEs. And if
[17:15:23] my intermediate results is very
[17:15:25] important then I put it in either view
[17:15:27] or CTIS but it is nice technique to
[17:15:30] learn maybe you can utilize it in one of
[17:15:32] your
[17:15:36] projects. All right guys so now let's
[17:15:38] have a quick summary about tables.
[17:15:39] Tables in database are like spreadsheet
[17:15:42] or grid that contains columns and rows
[17:15:44] and your actual data are stored in these
[17:15:47] tables. And we have learned there are
[17:15:48] two types of tables. We have permanent
[17:15:50] tables and temporary tables. Permanent
[17:15:53] tables lives in the database forever as
[17:15:55] long as you don't drop them. But in the
[17:15:57] other hand that temporary tables they
[17:15:59] have short lifetime. They will be
[17:16:01] dropped from the database once you end
[17:16:03] the session. Now we have learned as well
[17:16:05] there are two methods on how to create
[17:16:07] tables in databases. The first method is
[17:16:10] create insert. This method involves two
[17:16:12] steps. The first one is defining and
[17:16:15] creating the table and the second step
[17:16:17] is by inserting the data inside this new
[17:16:20] table. So you are creating something
[17:16:21] from the scratch. And the second method
[17:16:23] we call it CTAs. It create as well brand
[17:16:27] new table but based on the result of a
[17:16:30] query. So this type is done with only
[17:16:32] one step but it always needs another
[17:16:35] existing table. And we have learned as
[17:16:36] well the difference between tables and
[17:16:38] views where the main advantage of using
[17:16:40] tables created from CTIS is that to
[17:16:43] ensure the performance is fast enough at
[17:16:46] the end of the users or your reporting
[17:16:48] system. So we use CIS instead of views
[17:16:52] if the logic of the view is very complex
[17:16:54] and takes a lot of time to be executed
[17:16:56] in the database. And one more nice use
[17:16:58] case for the CIS is that we can go and
[17:17:01] persist a snapshot of the data in order
[17:17:03] to analyze a bug and data quality issue
[17:17:06] and to ensure that we have the exact
[17:17:08] data in order to find a solution for the
[17:17:10] bug and the issue. Now we have learned
[17:17:12] as well that we can use temporary tables
[17:17:14] in order to store intermediate results
[17:17:16] in a temporary storage and the main
[17:17:18] advantage of the temporary table is the
[17:17:21] database automatically drops all that
[17:17:23] temporary tables when the session ends
[17:17:26] and that's because for you the
[17:17:28] intermediate results are not that
[17:17:29] important to live long
[17:17:34] time. Hey my friends. So we have learned
[17:17:37] that in real data projects if you have a
[17:17:40] database there will be a lot of
[17:17:42] analytical use cases that want to access
[17:17:44] your data and do analytics. And what
[17:17:46] going to happen? They're going to write
[17:17:48] complex queries because in many
[17:17:50] scenarios they are doing complex
[17:17:52] analyzes. And if you don't do anything
[17:17:54] about it in your projects, you're going
[17:17:55] to face a lot of challenges like
[17:17:57] complexity and a lot of redundancy of
[17:18:00] the same complex logic but from multiple
[17:18:02] users and maybe performance and security
[17:18:05] issues. And we have learned we have five
[17:18:07] amazing techniques in order to solve
[17:18:10] those problems. We have learned the
[17:18:11] subqueries and cities and as well how to
[17:18:14] create objects like views, CTAs and
[17:18:17] temporary tables. So now what we're
[17:18:19] going to do, we're going to go and
[17:18:20] compare them side by side in order to
[17:18:22] have a big picture about the advantages
[17:18:24] and the disadvantages of each method. So
[17:18:27] let's go and compare them. Okay. So now
[17:18:29] we have our five methods and the first
[17:18:31] criteria that I would like to compare
[17:18:32] them is the storage type. We have
[17:18:35] learned that if you are using subqueries
[17:18:37] and CTE, what can happen? and the
[17:18:39] database going to put the result of
[17:18:41] those two techniques in the memory in
[17:18:43] the cache so that later the main query
[17:18:46] has a fast access to those intermediate
[17:18:48] results. But in the other hand if you
[17:18:50] are using temporary tables or tables
[17:18:52] from CDS the new created table can be
[17:18:55] stored inside the disk storage. And now
[17:18:57] for the views as we understood there
[17:18:59] will be no data storage and that means
[17:19:02] we are not using any storage from the
[17:19:04] database. Now if you are talking about
[17:19:06] the lifetime so that means how long the
[17:19:08] object going to live or persist in the
[17:19:10] database. Now our three techniques sub
[17:19:12] queries CTE and temporary tables all of
[17:19:15] them going to live a short time in the
[17:19:17] database. So all of them are temporary.
[17:19:20] But now if you are talking about
[17:19:21] creating objects using CIS and views
[17:19:24] those two going to be permanent. So that
[17:19:26] means they're going to live in the
[17:19:27] database as long as you don't drop them.
[17:19:30] Now we're going to compare them with
[17:19:31] something similar is when the database
[17:19:34] going to go and drop or delete those
[17:19:36] objects. Now we have learned that the
[17:19:38] subqueries and the cities have a short
[17:19:40] time. They going to live only during the
[17:19:43] execution of the query. So once the
[17:19:45] query ends the database going to go to
[17:19:47] the cache and delete everything. But for
[17:19:50] the temporary tables they live little
[17:19:52] bit longer as long as you are in the
[17:19:54] session. But once you end the session,
[17:19:56] the database as well going to go and
[17:19:58] drop and delete your table. Now for the
[17:20:00] objects that comes from the CIS and
[17:20:03] views as we learned they are persistent
[17:20:05] and permanent and the database can only
[17:20:07] delete them if you ask the database to
[17:20:09] do that by using the DDL command drop.
[17:20:12] So the database will not delete anything
[17:20:14] for these two. So now the next one is
[17:20:16] the query scope like how we can access
[17:20:19] those objects. Now for the subquery and
[17:20:21] the CTE the scope is here very small. It
[17:20:24] is accessed only from one single query.
[17:20:26] The query itself where you write the
[17:20:28] city and subquery. So you cannot access
[17:20:31] it from external queries. But we have
[17:20:33] learned that the temporary tables cis
[17:20:35] and views you can access all those
[17:20:38] objects from multiple queries. So that
[17:20:40] means you can access those objects from
[17:20:43] multiple external queries. Now the next
[17:20:45] one if you are thinking about the
[17:20:47] reusability if you look to the
[17:20:49] subqueries they are very limited. the
[17:20:51] subquery going to be used only in one
[17:20:53] query and only in one place. So if you
[17:20:56] need it in multiple places, you have to
[17:20:58] go and repeat the same logic. So
[17:21:00] subqueries are the worst with their
[17:21:02] reusability. But now if you are talking
[17:21:04] about the CTE, it is little bit better.
[17:21:06] You still can access it only from one
[17:21:08] single query but you can access it in
[17:21:10] the same query from multiple places. So
[17:21:13] you can access it multiple times from
[17:21:15] different joins and you don't have to
[17:21:17] repeat the same logics over and over.
[17:21:19] But still it is limited because you have
[17:21:21] only one query that is using the logic.
[17:21:24] Now if you think about the temporary
[17:21:25] tables I could say the reusability here
[17:21:28] is medium and that's because you can
[17:21:30] access the data by multiple queries but
[17:21:33] only during this session. So once the
[17:21:35] session is ended you cannot access it
[17:21:38] anymore which means you have to recreate
[17:21:40] it in order to reuse it again. So it is
[17:21:42] more reusable than the city and the
[17:21:44] subqueries but not that good like the
[17:21:46] CTAs and views. Those techniques can
[17:21:49] offer the highest reusability for you.
[17:21:52] So they are always there for multiple
[17:21:54] users from multiple queries. So it can
[17:21:57] eliminate a lot of redundancies and you
[17:21:59] have to do the job only once. Now moving
[17:22:01] into the next one. If you are thinking
[17:22:03] about the intermediate result of those
[17:22:04] techniques, the question is how fresh is
[17:22:07] the data? Is the data from these objects
[17:22:10] always up to date? Now for the
[17:22:11] subqueries and the cities they are
[17:22:14] always up to date because the SQL is
[17:22:16] executing the logic on the fly and
[17:22:18] storing the data in the memory and
[17:22:20] immediately after that going to come the
[17:22:22] main query and get the data. So always
[17:22:24] the intermediate results in the memory
[17:22:26] are up to date. But now if you think
[17:22:28] about that temporary tables and the CTIS
[17:22:30] the query is only executed once and if
[17:22:33] there is like any update and changes on
[17:22:35] the original table you will not find
[17:22:38] those changes in those objects and
[17:22:40] that's because SQL executed once and
[17:22:42] that's all. So if you query those tables
[17:22:44] there is no guarantee that the data are
[17:22:46] up to date. So if you want fresh data
[17:22:49] you have always to drop the table and
[17:22:51] create it again from the query. Now if
[17:22:53] you are talking about the views they are
[17:22:55] amazing they are always up to date
[17:22:57] because views does not store any data.
[17:22:59] So each time you ask the views for data
[17:23:02] what's going to happen the database
[17:23:03] going to go to the original table and
[17:23:05] fetch the data to the view. So your data
[17:23:07] are always fresh and up to date. So this
[17:23:09] is a big picture about the behavior of
[17:23:11] those advanced techniques that you can
[17:23:13] use in SQL projects. And if you ask my
[17:23:16] opinion my favorite is going to be the
[17:23:17] views in the first place. Then in the
[17:23:20] second in my list is the city. They are
[17:23:22] amazing, but don't use more than five
[17:23:24] CTEs in one query. Otherwise, it's going
[17:23:26] to be really annoying and hard to read.
[17:23:28] And then I'm going to say in the third
[17:23:29] place, the sub queries. And then the
[17:23:32] CDIS. I use CIS if the views are slow.
[17:23:35] If that's a scenario, I'm jump to the
[17:23:37] CDIS and create a permanent physical
[17:23:39] tables from my query. And the last one
[17:23:41] that I rarely use is the temporary
[17:23:43] tables. So, this is how I rank those
[17:23:46] techniques in my skill projects.
[17:23:52] Now I would like to show you as well a
[17:23:53] big picture on how things works in my
[17:23:55] projects in order to see all those
[17:23:57] different techniques and possibilities
[17:23:59] that you can use. It's like a big
[17:24:01] picture and recap. So story time. So you
[17:24:04] have a database and things starts where
[17:24:06] you have a database administrator or
[17:24:08] let's say a data engineer that is
[17:24:10] creating a new table from the scratch.
[17:24:12] So he going to write a DDL statement in
[17:24:14] order to create one physical table at
[17:24:16] our database. And now our database table
[17:24:18] is empty. That's why in the second step
[17:24:21] he going to go and write an insert
[17:24:23] statement in order to fill our new table
[17:24:25] with data. Now once we have a table
[17:24:27] we're going to give the access maybe to
[17:24:29] a data scientist or data analyst in
[17:24:31] order to start writing SQL queries. So
[17:24:33] now the first thing that could happen
[17:24:35] that the logic is complex and she has to
[17:24:37] do that in two steps. So the first step
[17:24:40] is a query that prepares the data in
[17:24:42] order to execute the second step. So
[17:24:45] that's why she going to go and use the
[17:24:46] subquery and the main query going to go
[17:24:48] and retrieve the data from the
[17:24:50] intermediate results in order to prepare
[17:24:52] the final results for the analyst. Now
[17:24:54] what could happen is that there will be
[17:24:56] an SQL logic in the query where it keep
[17:24:59] repeating the scripts. So now instead of
[17:25:01] writing another subquery for that she
[17:25:03] going to go and put this logic in CTE
[17:25:05] and now she going to go to the main
[17:25:07] query and use the result of the CTE in
[17:25:10] multiple places in the same query. So
[17:25:12] all those stuff the sub queries and the
[17:25:14] city queries the main queries all those
[17:25:16] stuff happens in one single query and
[17:25:19] now what could happen is that she is
[17:25:21] writing an amazing code. So instead of
[17:25:23] using it only in her query what's going
[17:25:26] to happen she going to go and persist
[17:25:28] this logic in the database. So she going
[17:25:30] to put it as a view in the database so
[17:25:32] that all other users and analysts can
[17:25:35] benefit from this logic and they don't
[17:25:37] have to write it again. So instead
[17:25:39] they're going to go and query the view
[17:25:41] and this going to makes the life easier.
[17:25:43] And of course our data analyst can as
[17:25:45] well use this view in the main query.
[17:25:47] And now one more thing she has as well
[17:25:49] another logic that is really complex and
[17:25:52] as well everyone can benefit from it.
[17:25:54] But the issue this query is very slow.
[17:25:56] So now she has to decide do I put it in
[17:25:59] view or do I create a new table based on
[17:26:01] the query using CTAs. Now of course
[17:26:03] because of the performance and the view
[17:26:05] takes around 30 minutes to be executed.
[17:26:07] She decided to execute the query using
[17:26:09] the CTIS where she generate a physical
[17:26:12] table so that all other analysts as well
[17:26:14] can access this new table in order to
[17:26:17] reuse the results and of course she can
[17:26:19] use it in her main query and with that
[17:26:22] now you have experience how things works
[17:26:24] in real projects. It is not simple
[17:26:26] select query from table it is like this
[17:26:29] people are creating subquery CTE views
[17:26:32] temporary tables CTAs for different
[17:26:35] purposes. All right my friends. So
[17:26:37] that's all about the CTIS and the
[17:26:39] temporary tables. And with that we have
[17:26:40] learned all the techniques on how to
[17:26:42] organize our complex projects. Now next
[17:26:44] we're going to start talking about
[17:26:45] something completely different. We're
[17:26:47] going to talk about the stored
[17:26:49] procedures on how to put our code inside
[17:26:51] the database. This is all about that
[17:26:53] programmability and how to add stuff
[17:26:55] like parameters, variables, error
[17:26:58] handling. So it's like programming. So
[17:27:00] let's go. So let's uncover this word of
[17:27:02] the s procedures and let's go.
[17:27:08] Now think about store procedures like
[17:27:10] this. Every time you go to a coffee
[17:27:12] shop, you say, "I would like a large
[17:27:14] coffee with a coconut milk, no sugar,
[17:27:17] and extra whipped cream." And you repeat
[17:27:19] this over and over each time you go to
[17:27:22] this coffee shop. And now, if you are
[17:27:24] working with stored procedures, it's
[17:27:25] going to be like this. Whenever you go
[17:27:27] to the coffee shop, you just say, "Give
[17:27:29] me my usual." and the barista know
[17:27:31] exactly what you mean behind that and
[17:27:33] you will get exactly your order without
[17:27:35] specifying and repeating everything word
[17:27:37] by word and this is exactly what's going
[17:27:39] to happen if you work with stored
[17:27:40] procedures so let's have some coffee
[17:27:44] right all right so now we can continue
[17:27:47] all right so now let's start again from
[17:27:48] the scratch we have always these two
[17:27:50] sides we have the client side and the
[17:27:52] server side of the database and what we
[17:27:54] have learned we have like a database and
[17:27:55] you as a user you can go and create like
[17:27:58] different SQL statements Like for
[17:28:00] example, you can create like an SQL
[17:28:02] select statements in order to retrieve
[17:28:04] data from the database or another SQL
[17:28:07] statements where you are inserting data
[17:28:09] to the database and another one let's
[17:28:11] say that you are updating the content of
[17:28:13] your tables and so on. So you have like
[17:28:16] different statements in order to
[17:28:17] interact with the database. Now let's
[17:28:19] say that what you are doing is not only
[17:28:21] one time job you are keep repeating
[17:28:23] those steps over and over. So you are
[17:28:26] always like doing an insert then an
[17:28:28] update and then a select and you keep
[17:28:30] repeating that day after day. So now
[17:28:33] imagine that you are doing something
[17:28:34] crazy where you go in vacation but the
[17:28:37] job should be done. So what you do you
[17:28:38] hand over all those select statements to
[17:28:41] your colleagues and they have to do it
[17:28:43] every day as well as you are gone. So
[17:28:45] you go and give them all those SQL
[17:28:47] scripts and you tell them okay you have
[17:28:49] to execute the first query then the
[17:28:51] second query and then the third query.
[17:28:53] This is of course not a good way on how
[17:28:55] to do things because of course there
[17:28:57] will be some human errors where like the
[17:29:00] execution of the script is not correct
[17:29:02] like first updating then inserting and
[17:29:04] things can go wrong and that's exactly
[17:29:07] why we have stored procedures in SQL. So
[17:29:10] what we can do we can put all those SQL
[17:29:13] statements together in one frame in one
[17:29:16] program and we call it start procedure.
[17:29:19] And now once you do that all your SQL
[17:29:21] statements will not stay at the client
[17:29:23] side they will be stored now in the
[17:29:26] server side of the database. So that
[17:29:28] means in store procedures we are storing
[17:29:30] our SQL statements inside the database.
[17:29:33] So you don't have to go and hand over
[17:29:35] your SQL statements to your colleagues.
[17:29:37] And now all what you have to do in order
[17:29:39] to interact with your SQL statements is
[17:29:42] to go and execute the store procedure.
[17:29:44] So you write very simple command called
[17:29:47] execute SP for example. So with that you
[17:29:50] are calling your stored procedure that
[17:29:52] is stored inside the server. And once
[17:29:54] you execute this what can happen the
[17:29:56] database going to go to the stored
[17:29:57] procedure and start executing all the
[17:30:00] SQL statements that you have inside the
[17:30:03] store procedure and it's going to do it
[17:30:05] exactly in the order that you have
[17:30:06] defined. So from top to bottom. So now
[17:30:09] once the database went through all your
[17:30:11] SQL statements, it's going to return
[17:30:13] back to the user the data that we have
[17:30:15] from the selects. And with that things
[17:30:17] are really easy and you can tell your
[17:30:19] colleagues okay just execute this third
[17:30:21] procedure and the rest can be done from
[17:30:24] the database. So with that you minimize
[17:30:26] the human errors and you make sure that
[17:30:28] everything can be executed as you wish
[17:30:30] and as well as you are back from your
[17:30:32] vacation things are easier. You have to
[17:30:34] just go and execute the third procedure.
[17:30:36] So this is what we mean with start
[17:30:38] procedure. You can store inside it
[17:30:40] multiple SQL statements in specific
[17:30:42] order and you can save it inside the
[17:30:44] database and each time you need your SQL
[17:30:46] statements you can go and simply execute
[17:30:52] them. So now let's have a quick
[17:30:54] comparison between a normal query normal
[17:30:56] SQL statements compared to a stored
[17:30:58] procedure. So a normal SQL query you
[17:31:01] have like select from where and so on.
[17:31:04] This is like one-time transaction. You
[17:31:06] are asking the database for one thing
[17:31:08] and the database is answering. So it is
[17:31:11] like one-time request. But now in the
[17:31:13] other hand in the stored procedures you
[17:31:15] have multiple SQL statements and once
[17:31:17] you execute the stored procedure there
[17:31:19] will be many interactions with the
[17:31:21] database in one go. So that means you
[17:31:23] will have multiple transactions that is
[17:31:25] happening in your store procedure. So an
[17:31:28] SQL query it is like a simple request.
[17:31:30] You need one thing and you are getting
[17:31:32] it. But on the other hand in the start
[17:31:34] procedure it is like a program. As you
[17:31:37] are writing a code in any programming
[17:31:39] languages it is more than one request it
[17:31:41] has a lot of stuff like for example you
[17:31:44] can go and build looping logic where we
[17:31:46] go and iterate through something or you
[17:31:49] can go and build a control flow where
[17:31:51] you have a logic like the FL statements.
[17:31:54] So there are like different paths in
[17:31:56] your code and as well in programming we
[17:31:58] have like parameters and variables in
[17:32:01] order to make our code dynamic and
[17:32:03] flexible and as well we can build error
[17:32:05] handling on our code in order to
[17:32:08] customize what can happen if there is
[17:32:09] like an issue. So the store procedure it
[17:32:12] is like having a code like for example
[17:32:14] in Python. So that means you can do more
[17:32:16] complicated stuff compared to a simple
[17:32:19] query where you have only like one
[17:32:21] request. So in the stored procedures you
[17:32:23] are doing like programming and coding
[17:32:25] and it is more advanced than only just
[17:32:28] having a query. So that means if you are
[17:32:30] working with stored procedures things
[17:32:32] going to get more complicated and
[17:32:33] advanced but of course you will get a
[17:32:35] lot of flexibility and reusability
[17:32:38] compared to a simple
[17:32:39] [Music]
[17:32:42] query. So now there is like another
[17:32:45] alternative to stored procedures. Well,
[17:32:47] you can go and put all your SQL
[17:32:49] statements in a Python code and things
[17:32:52] can work as well. So, either you put
[17:32:53] your SQL statements inside the stored
[17:32:56] procedure or in a Python code. But now
[17:32:58] the big question is what are the
[17:33:00] differences between them? Well, there is
[17:33:01] like a disadvantage if you having Python
[17:33:03] in different server because you have to
[17:33:05] go and build a connection between your
[17:33:08] server and the database server and
[17:33:10] connection means always networking and
[17:33:12] you might get slightly worse
[17:33:14] performance. So this is one advantage
[17:33:16] for the start procedure. Another
[17:33:18] advantage for the search procedure that
[17:33:20] all the scripts that you're going to
[17:33:21] store inside the store procedure in the
[17:33:23] database going to be pre-ompiled. So
[17:33:26] pre-ompiled means the SQL database
[17:33:28] servers knows already about your SQL
[17:33:31] statements and there was already a check
[17:33:33] whether all the syntaxes are correct and
[17:33:35] the database as well going to be
[17:33:37] preparing everything to execute the
[17:33:39] stored procedure like maybe preparing
[17:33:41] the execution plans and a lot of stuff.
[17:33:43] So if you store your skill statements
[17:33:45] inside store procedure in the database,
[17:33:47] it is very close to the database and the
[17:33:49] database knows everything about your
[17:33:51] scripts and it is ready to execute it.
[17:33:53] But if you put all your SQL statements
[17:33:55] outside of the database, of course, the
[17:33:57] database has no chance to understand
[17:34:00] what is coming. So it cannot go and
[17:34:02] compile anything until Python sends the
[17:34:05] code to database. So this is another
[17:34:07] advantage for the stored procedure. But
[17:34:09] now if you build your SQL statements in
[17:34:12] Python, you will get a lot of
[17:34:13] advantages. Like for example, you can go
[17:34:15] and build very flexible Python codes
[17:34:18] where you can use Python features
[17:34:20] together with the SQL and with that you
[17:34:22] open the door of many possibilities and
[17:34:24] flexibility. Another thing with Python,
[17:34:26] you can make great version control. So
[17:34:29] everything is integrated in Python
[17:34:31] tools. And one more advantage is that if
[17:34:33] you have a complex requirement in your
[17:34:35] projects, it's going to be really hard
[17:34:36] to implement it in stored procedures.
[17:34:38] it's going to cost you a lot of lines of
[17:34:40] code and things going to be not
[17:34:42] comfortable. But if you are implementing
[17:34:44] a complex logic in Python, things going
[17:34:47] to be way easier. So with Python, you
[17:34:49] can implement complex logics very easily
[17:34:52] compared to the stored procedure. So
[17:34:54] those are the big differences between
[17:34:55] the stored procedure and Python. Now I
[17:34:57] have to be honest with you about having
[17:34:59] your code in store procedure or in
[17:35:01] Python. Well, if you are working
[17:35:03] together in a data project, I will never
[17:35:05] recommend you to use stored procedure if
[17:35:07] you have the possibility to have your
[17:35:09] code in Python. And that's because I saw
[17:35:11] a lot of projects using stored procedure
[17:35:13] and most of them ends in chaos. It is
[17:35:16] really hard to debug. It is really hard
[17:35:18] to test. It's like catastrophic. So
[17:35:20] really don't use in your projects any
[17:35:23] store procedures. Especially if you have
[17:35:25] like a big project and you have a lot of
[17:35:27] data and tables and so on. You can
[17:35:29] manage everything perfectly using
[17:35:31] Python. Especially if you have platform
[17:35:33] like data bricks or snowflakes then of
[17:35:36] course the best way to control your data
[17:35:38] projects is using Python. But of course
[17:35:40] if you don't have this possibility and
[17:35:42] you have only a database server and you
[17:35:44] can only work with this then you don't
[17:35:46] have any other option. You have to work
[17:35:47] with the store procedures. But if you
[17:35:49] have this possibility to put your
[17:35:51] project inside Python and to run your
[17:35:53] scripts from there, then it is way
[17:35:55] better than having stored procedure.
[17:35:56] Well, this is my opinion. I'm just
[17:35:58] talking about working in projects in big
[17:36:00] projects. But if you have like small
[17:36:02] projects, few tables and so on, then
[17:36:04] it's fine to stay with the store
[17:36:05] procedure. But never build a big project
[17:36:07] using stored procedures because I tell
[17:36:10] you it will never work. So try to always
[17:36:13] to think about to have the right
[17:36:14] platform in order to run your projects.
[17:36:17] And now I'm thinking about it. Maybe I
[17:36:19] should have put this tip at the end of
[17:36:20] the video, not in the middle. So
[17:36:22] whatever. If you still want to learn
[17:36:24] store procedures, we're going to
[17:36:25] continue on that. And I'm going to have
[17:36:27] like a really nice example about how to
[17:36:29] build store procedures step by step like
[17:36:31] having a mini projects. So why not
[17:36:33] learning both of them. So let's
[17:36:38] go. Okay. So now let's have a quick look
[17:36:40] to the syntax of the store procedure. It
[17:36:42] is very simple. So it has always two
[17:36:44] parts. First we have to define the start
[17:36:46] procedure. So we can do it like this.
[17:36:48] Create procedure. Then we have to define
[17:36:50] the procedure name and then we say as
[17:36:52] and then we have begin and end. It's
[17:36:54] very important for SQL to understand
[17:36:56] when that definition starts and when it
[17:36:58] ends. And then between the begin and end
[17:37:00] we're going to have a set of SQL
[17:37:02] statements. So here you can insert
[17:37:04] whatever you want. Insert update queries
[17:37:06] anything. And once you have defined the
[17:37:08] sort procedure the next step is that
[17:37:10] we're going to go and execute it. So the
[17:37:12] syntax is very simple. We're going to
[17:37:13] say execute and then the procedure name.
[17:37:15] So that's it with that SSQL going to go
[17:37:17] to the S procedure and start executing
[17:37:19] all the SQL statements that you have in
[17:37:21] the definition. So this is the syntax of
[17:37:23] the S procedure. As I said it is very
[17:37:25] simple. All right guys. So now let's do
[17:37:27] it step by step. The first step is that
[17:37:29] we're going to go and write a query. So
[17:37:31] let's say that we have a very simple
[17:37:32] task and it says for US customers find
[17:37:35] the total number of customers and the
[17:37:38] average score. So let's go and do it.
[17:37:40] It's very simple. So select count star
[17:37:44] total customers and then the average of
[17:37:47] scores as average score from our table
[17:37:52] sales customers and then since it says
[17:37:55] US customers we have to go and filter
[17:37:57] the data based on the column country is
[17:38:00] equal to USA. So that's it. This is our
[17:38:03] query. Let's go and execute it. So we
[17:38:05] have a very quick nice report about the
[17:38:07] total number of customers and the
[17:38:08] average score. So now let's say that I
[17:38:10] have a weekly meeting and I have to
[17:38:12] represent this reports over and over. So
[17:38:15] that means I have to go and execute this
[17:38:17] query like frequently in weekly basis in
[17:38:20] order to get the data for the reports.
[17:38:22] So now what this means I have to go and
[17:38:24] save this query in order to use it later
[17:38:27] that each time I have to rewrite it. So
[17:38:29] that means I have to store this text
[17:38:30] somewhere that I don't go and rewrite
[17:38:33] the query over and over. So what I
[17:38:35] usually do, let's go and we copy the
[17:38:37] whole query and then we create a new
[17:38:40] text and let's say it's going to be my
[17:38:41] weekly query and it's going to be SQL.
[17:38:45] So I'm going to go and edit it and here
[17:38:48] I'm going to save my query and each time
[17:38:50] I need this query I have to go and copy
[17:38:52] it, go back to my SQL and then I'm going
[17:38:55] to go and paste it in order to execute
[17:38:58] it. So either going to write it each
[17:38:59] time or copy and paste it. Well, we
[17:39:02] don't have to do that. we have start
[17:39:04] procedures. So that means we're going to
[17:39:06] go to the step two where we're going to
[17:39:07] turn this query into a store procedure.
[17:39:10] So let's do that. It's very simple. So
[17:39:12] we're going to say create procedure. And
[17:39:15] now we have to go and give it a name. So
[17:39:16] it's going to be get customer summary.
[17:39:19] And then after that we're going to say
[17:39:21] as and then we need the begin and end.
[17:39:23] And in between we're going to put our
[17:39:25] query. So let's go and copy our query
[17:39:28] and just put it in between. So that's
[17:39:31] it. Let's go and execute it. And with
[17:39:33] that we have created our store
[17:39:35] procedure. And now in order to see our
[17:39:37] store procedure we can go to the object
[17:39:39] explorer to our database sales DB. And
[17:39:41] then here we have a folder called
[17:39:43] programmability. So let's go inside it.
[17:39:45] And here we have a lot of stuff like
[17:39:47] functions, triggers and we have stored
[17:39:49] procedures. So let's go inside it. And
[17:39:51] we can see over here this is our new
[17:39:53] created stored procedure. So we are
[17:39:56] almost there. The next step is that
[17:39:58] we're going to go and call our store
[17:39:59] procedure. And this is the easiest part.
[17:40:02] So it's going to be execute the stored
[17:40:04] procedure. And the syntax is very
[17:40:05] simple. So execute and then the name of
[17:40:08] the stored procedure. So get customer
[17:40:11] summary. So let's go and execute it. And
[17:40:14] with that as you can see we get the
[17:40:16] result of our query. So as you can see
[17:40:18] it is very simple. In just few steps we
[17:40:21] created a store procedure. And then in
[17:40:23] the future you don't need the whole
[17:40:24] thing. You just go and execute the store
[17:40:27] procedure. I don't have to store the
[17:40:29] query locally at my PC or to copy and
[17:40:32] paste anything. If I want this report
[17:40:35] now, I just have to execute the store
[17:40:37] procedure like this and I will get the
[17:40:43] results. Okay. So now let's keep moving.
[17:40:45] Now we're going to talk about the
[17:40:47] parameters inside stored procedures. So
[17:40:49] what is a parameter? It is like a
[17:40:52] placeholder where you can pass in
[17:40:53] information from you into the store
[17:40:56] procedure while running it and using
[17:40:58] parameters in store procedure it's going
[17:41:00] to make it flexible reusable and
[17:41:03] dynamic. So let's understand what this
[17:41:05] means. Let's say that you got a new
[17:41:07] task. So it says for German customers
[17:41:10] find the total number of customers and
[17:41:12] the average score. So that means now we
[17:41:14] have like to generate two reports one
[17:41:16] for USA and one for Germany. And in both
[17:41:18] of them you are doing the same
[17:41:19] aggregation. And again we have to go and
[17:41:21] start writing the query. It's going to
[17:41:23] be very similar to the one that we have
[17:41:24] in the previous example. So we are doing
[17:41:26] the same stuff same aggregations but the
[17:41:29] only change here is that we're going to
[17:41:31] use another value to filter the data. So
[17:41:33] instead of USA we're going to go and say
[17:41:35] here Germany. So let's go and execute
[17:41:38] this one over here. And with that we can
[17:41:39] see we have total number of customers
[17:41:41] too. So this is the report that we have
[17:41:43] to provide like in weekly basis. And
[17:41:45] again in order not to go and copy paste
[17:41:47] stuff we're going to go and create a
[17:41:49] store procedure for that. At the end
[17:41:51] we're going to have an end. But now of
[17:41:53] course we cannot have like the same
[17:41:54] names we're going to go and say here
[17:41:56] Germany. So let's go and execute it. And
[17:42:00] the next step we have to go and execute
[17:42:02] the store procedure. So like this. Let's
[17:42:06] go and execute it. And the whole logic
[17:42:08] now stored inside the database. Let's go
[17:42:10] and refresh on the explorer over here.
[17:42:12] And you can see now we have two stored
[17:42:14] procedures. But now you have to feel
[17:42:16] there is something wrong. Always in
[17:42:18] programming and coding. If you find
[17:42:20] yourself repeating the same task over
[17:42:22] and over then there is always a smarter
[17:42:25] way on how to optimize that. Repeating
[17:42:28] stuff in coding is always bad thing. So
[17:42:30] now clearly we are repeating the same
[17:42:32] query in two different store procedure.
[17:42:34] And now if you compare them you see it's
[17:42:36] because of the value. So we have here
[17:42:38] the value for the filter once Germany
[17:42:40] and one USA. And those values are static
[17:42:43] values. So it's always going to stay
[17:42:45] inside the store procedure as USA. But
[17:42:47] instead of that we can replace those
[17:42:49] static values with a parameter. And then
[17:42:52] you decide as you are executing the
[17:42:54] stored procedure for which country you
[17:42:57] want to execute the store procedure. So
[17:42:59] let's go and do that. I'm just going to
[17:43:01] remove everything from here and focus
[17:43:03] only on the first store procedure. Now
[17:43:05] what we're going to do after giving the
[17:43:07] name of our store procedure we have to
[17:43:10] define our parameter. So it start with
[17:43:13] at and with that SQL understandhuh now
[17:43:15] we are talking about parameters and we
[17:43:17] need now the name of the parameter. So
[17:43:19] it's going to be country. It could be
[17:43:21] any name that you want and after that we
[17:43:23] have to define for SQL the data type.
[17:43:25] It's like when you are creating a table
[17:43:27] and you define columns you assign a data
[17:43:29] type for each column. The same thing
[17:43:31] here you have to assign as well a data
[17:43:33] type for each parameter. So we're going
[17:43:35] to use the data type in var and for the
[17:43:38] countries it's enough to have the length
[17:43:39] of 50. So with that we are telling SQL
[17:43:42] for this third procedure we can pass an
[17:43:45] information to the store procedure and
[17:43:47] this information and value going to be
[17:43:49] used inside this parameter. So now after
[17:43:51] we defined this parameter over here we
[17:43:54] can go and use it anywhere inside our
[17:43:56] query. And of course we want to go and
[17:43:58] use it instead of this static value. So
[17:44:00] now we're going to remove this static
[17:44:02] value and instead we're going to have
[17:44:04] the parameter. So now we are saying
[17:44:06] you're going to filter the table based
[17:44:08] on the value that comes from the user
[17:44:10] and not anymore static with a USA. And
[17:44:12] as I said you can use this parameter
[17:44:15] everywhere like even here in the select
[17:44:16] statements. So it is a value that could
[17:44:18] be used everywhere in your query. So
[17:44:20] that's it. We have defined our new
[17:44:22] parameter and we have used this
[17:44:25] parameter in our query. So now we have
[17:44:26] to go and update the store procedure. We
[17:44:29] cannot leave it as create. Instead of
[17:44:31] that, we're going to say alter. So we
[17:44:32] are saying alter procedure and with the
[17:44:35] new informations. Let's go and execute
[17:44:37] it. And now we have to go and execute
[17:44:40] it. So now what we're going to do, we're
[17:44:41] going to say execute get customer
[17:44:43] summary. But now our store procedure is
[17:44:45] expecting a value from you from the
[17:44:47] input. So we're going to do it exactly
[17:44:48] like we done in the name over here. So
[17:44:51] we're going to say the parameter country
[17:44:53] is equal
[17:44:54] to Germany. So that means the value of
[17:44:58] this parameter come from me come from
[17:45:00] the input and this information going to
[17:45:02] be passed to my query to the store
[17:45:04] procedure. So let's go and execute it.
[17:45:06] And with that as you can see we are
[17:45:08] getting the report of customers for
[17:45:10] Germany. And now if you say okay let's
[17:45:12] go and generate the report for USA. All
[17:45:14] what you have to do is replace the
[17:45:17] parameter. So in the value instead of
[17:45:19] Germany we're going to say USA. So let's
[17:45:22] go and execute it. Great. Now we are
[17:45:24] getting as well the report for us
[17:45:26] customers. So that seems my friends for
[17:45:29] those two reports I just need one store
[17:45:31] procedure and with the help of the
[17:45:32] parameter I made my store procedure now
[17:45:35] more flexible and professional. So this
[17:45:37] is exactly the power of the parameters
[17:45:40] it makes everything reusable and
[17:45:43] dynamic. And now of course we don't need
[17:45:45] the store procedure for Germany. So what
[17:45:47] we can do we can go and drop it. So
[17:45:49] we're going to say drop procedure and it
[17:45:53] was like this Germany. So we don't need
[17:45:56] this store procedure and we're going to
[17:45:58] stay with only one dynamic store
[17:46:00] procedure. So this is how to use
[17:46:02] parameters in store procedure and why
[17:46:04] it's important. Okay. So now to the next
[17:46:06] step is that we can go and add default
[17:46:08] values for the parameters. So let's say
[17:46:11] that I execute very frequently this
[17:46:13] report where I say the country equal to
[17:46:14] USA and I don't want each time to define
[17:46:17] the parameter value equal to USA. So if
[17:46:20] you are using a value very frequently
[17:46:22] you can add it as a default inside the
[17:46:24] definition of the store procedure and it
[17:46:26] is very simple. So if you go to the
[17:46:28] definition again over here after the
[17:46:30] parameter and you say equal to USA. So
[17:46:34] now it's very important to understand
[17:46:35] that the country will not be always
[17:46:38] equal to USA. It is just you are saying
[17:46:40] if I don't get from the user any value
[17:46:43] then as a default I'm going to go and
[17:46:45] use the USA. So let's go and again
[17:46:48] change the definition of our stored
[17:46:50] procedure using alter. So execute and
[17:46:52] now we can go to our store procedure and
[17:46:55] I can skip the whole thing over here and
[17:46:57] execute it. So now as a default I'm
[17:46:59] getting the report of USA without
[17:47:02] passing an information to the store
[17:47:03] procedure because I know it is as a
[17:47:05] default USA. But if you need it as a
[17:47:08] Germany of course you have to go and
[17:47:10] define it. So you say execute the store
[17:47:12] procedure where the country equal to
[17:47:14] Germany. So if you execute it like this
[17:47:16] SQL still going to use your value. So
[17:47:18] the value that comes as an input from
[17:47:20] the user has more priority of course as
[17:47:22] the defaults. And with that we are
[17:47:24] getting the Germany reports. So as you
[17:47:26] can see it's really nice right using
[17:47:27] parameters in store
[17:47:32] procedure. All right moving on to the
[17:47:34] next step. Now we can work with multiple
[17:47:36] queries inside one stored procedure. And
[17:47:39] this is what we have learned at the
[17:47:40] start. We can have multiple SQL
[17:47:42] statements in one stored procedure. And
[17:47:44] now we have a new report and query to
[17:47:46] generate. It says find the total number
[17:47:49] of orders and the total sales. So let's
[17:47:51] do it quickly. We can write it like
[17:47:53] this.
[17:47:54] Select counts order ID. This is the
[17:47:58] total orders and then the sum of sales.
[17:48:03] Total sales from our table sales orders.
[17:48:07] And of course we are always creating a
[17:48:09] report based on specific country. So
[17:48:11] that means we have to go and join it
[17:48:12] with the customers table in order to
[17:48:15] filter the data. So on customer ID equal
[17:48:19] to the customer id. And now we're going
[17:48:22] to go and filter the data. So country
[17:48:25] equal to USA. So something like this.
[17:48:28] Let's go and execute it. And with that
[17:48:31] for the US customers, we have six orders
[17:48:33] and the total sales 180. And of course,
[17:48:36] the same thing we're going to do for
[17:48:37] Germany. So now, of course, we will not
[17:48:39] go and create an extra store procedure.
[17:48:41] For this, we're going to go and put
[17:48:42] everything in one store procedure. So
[17:48:44] let's go and copy the whole thing and
[17:48:47] put it here inside. So after the first
[17:48:50] report we're going to have the second
[17:48:51] report and now the best practice here if
[17:48:53] you have multiple queries in store
[17:48:55] procedure go and add at the end of each
[17:48:58] query a semicolon. It is just easier to
[17:49:00] understand how now this is the end of
[17:49:02] this query especially if you have like a
[17:49:04] big complex queries where you have CTE
[17:49:07] union and so on. It's going to be really
[17:49:09] hard to understand that we are talking
[17:49:11] now about completely new query but it is
[17:49:13] not like something the database requires
[17:49:15] it but it's just easier to read. So just
[17:49:18] add semicolons at the end of each query.
[17:49:21] So now let's go and execute the whole
[17:49:23] thing in order to change the definition
[17:49:25] of our query. And one more thing of
[17:49:27] course don't forget we don't need static
[17:49:29] values over here. We're going to go and
[17:49:30] add our nice
[17:49:32] parameters. So add country. So I think
[17:49:36] with that we have everything is ready to
[17:49:38] be executed. So let's go and change the
[17:49:40] definition of our store procedure. And
[17:49:42] now let's go and start with the defaults
[17:49:44] where the country equal to USA. So let's
[17:49:47] go and execute it. And now in the output
[17:49:49] as you can see we have two results. And
[17:49:51] that's because we have two queries. So
[17:49:53] the first report is for the first query
[17:49:55] and the second one for the new one that
[17:49:57] we just created. And the same thing if
[17:49:59] you go and execute the store procedure
[17:50:01] for Germany we will get as well two
[17:50:03] results. And here we can see we have
[17:50:04] four orders and 200 of total sales for
[17:50:07] Germany. So as you can see it's very
[17:50:09] simple. You can go now and add multiple
[17:50:11] SQL statements not only queries you can
[17:50:14] go and update you can do an insert
[17:50:16] delete any kind of SQL statements you
[17:50:19] can just go and add it inside your
[17:50:21] program. And as usual SQL going to
[17:50:23] execute it from the top to the bottom.
[17:50:25] So since this is the first SQL statement
[17:50:27] it's going to execute it first and then
[17:50:29] after that it's going to go to the next
[17:50:31] one. So this is how you can add multiple
[17:50:33] SQL statements to your store
[17:50:39] procedure. All right everyone. So now
[17:50:40] we're going to talk about the variables.
[17:50:42] So what is a variable? It is like a
[17:50:44] placeholder where you store inside it a
[17:50:46] value in order to use it later inside
[17:50:49] your stored procedure. So that means
[17:50:51] variable holds like a value inside the
[17:50:53] memory and you can reuse it everywhere
[17:50:55] you want inside your stored procedure
[17:50:57] but it's not like the parameters.
[17:50:59] Parameters are something like outside
[17:51:01] the store procedure. It's an input from
[17:51:03] the one that is executing the store
[17:51:05] procedure and the store procedure has to
[17:51:07] adapt with the parameter. But a variable
[17:51:10] it's something that lives inside the
[17:51:12] store procedure and we use it as a
[17:51:14] developers in order to make our code
[17:51:15] dynamic and to move a value from one
[17:51:17] place to another. So let's have a very
[17:51:19] simple example now. Let's say that we
[17:51:21] don't want our report here about the
[17:51:23] total customers as a query. So I don't
[17:51:25] want it as a result in the output. Let's
[17:51:27] say I'm generating a report always like
[17:51:29] this. We are saying the total customers
[17:51:31] from Germany equal to two and the
[17:51:33] average score from Germany is equal to
[17:51:35] 425. So I need it as a text not as a
[17:51:38] table like here. So in order to do that
[17:51:40] we can use the TSQL print in order to
[17:51:43] give a message after executing the store
[17:51:45] procedure. So the syntax of print is
[17:51:47] very simple. So we can go over here and
[17:51:50] say print and then we have single quotes
[17:51:53] and let's go and get the whole message
[17:51:54] from here without the comments and then
[17:51:57] the semicolon and we can repeat that for
[17:52:01] the second message. So for the average
[17:52:04] score and we put it over here as well a
[17:52:07] semicolon. Now if you do it like this
[17:52:10] this message going to be always static.
[17:52:12] So we will have always like two for the
[17:52:14] total customers and the average score
[17:52:16] going to always be like this even though
[17:52:18] that the data is changing. So we cannot
[17:52:20] have it static like this. We have to
[17:52:22] make it dynamic and especially if we are
[17:52:24] calling this function for USA. So we
[17:52:26] cannot have it here as a Germany. So
[17:52:28] let's see how we can make this dynamic.
[17:52:30] Now let's start with the easy stuff.
[17:52:32] Instead of the Germany over here we can
[17:52:34] go and put our parameter right. So
[17:52:36] instead of this so we're going to say at
[17:52:39] country but now the problem is it is
[17:52:41] part of the whole string we cannot do
[17:52:43] that so we're going to stop the text and
[17:52:46] you can see the coloring is changing and
[17:52:47] then have a plus in order to have
[17:52:49] concatenations. So this text comes first
[17:52:52] then the value from the country and then
[17:52:55] we're going to have as well the double
[17:52:56] point as a static text and again a
[17:52:59] concatenation and then we have the two
[17:53:00] we can talk about later. So let's do the
[17:53:02] same stuff over here. So we're going to
[17:53:04] say plus add country caring is not
[17:53:07] changing because of this code. So let me
[17:53:09] just remove it and then afterward plus
[17:53:12] make it static again plus and remove the
[17:53:15] final quotes. So with that in the
[17:53:17] message we have now dynamic where we get
[17:53:19] the value of the country from the
[17:53:21] parameter. And now we come to the
[17:53:23] interesting part. We have here an issue
[17:53:25] those two values they come from this
[17:53:27] query. And of course we cannot use a
[17:53:29] parameter for that. We have to use now
[17:53:31] the variables. Now in order to make a
[17:53:33] variables we have three steps. The first
[17:53:35] step is that we have to tell SQL about
[17:53:38] our new variable. So SQL can prepare and
[17:53:40] make like placeholder for it in the
[17:53:42] memory. So we have to tell and prepare
[17:53:44] it with our new variables. Now usually
[17:53:47] we do all the declarations of our
[17:53:49] variables at the start of the store
[17:53:51] procedure immediately after begin. So
[17:53:53] that means we're going to go over here
[17:53:54] and say declare and now after that it's
[17:53:58] like the parameters. It's very simple.
[17:53:59] So at total customers. So this is the
[17:54:03] name of the variable. And after that we
[17:54:04] have to define the data type. Of course
[17:54:06] you have to understand the data type
[17:54:08] from the query. Since we are saying
[17:54:10] count star then the output going to be
[17:54:12] an integer. That's why we're going to
[17:54:13] write it like this. So integer. And now
[17:54:15] we need another one for the average. So
[17:54:18] what we're going to do we're going to
[17:54:19] make a comma. Now we are declaring
[17:54:21] another variable. So at average score
[17:54:24] and the data type of this one going to
[17:54:26] be float because we have an average. So
[17:54:29] that's it for the first step. We are
[17:54:30] telling SQL we have two variables and
[17:54:32] SQL going to go and create an empty
[17:54:34] placeholder. So now in the second step
[17:54:36] we have to give our variables a value.
[17:54:39] So where we going to get the values?
[17:54:40] We're going to get it from the query. So
[17:54:42] let's do that. Now let's start with the
[17:54:44] first column. As you can see we have
[17:54:45] here the count star. And as we learned
[17:54:47] anything that we write on the right
[17:54:49] side, it going to be like an alias for
[17:54:51] the column. But in SQL if you go and
[17:54:53] write something before it, it going to
[17:54:55] be the variable. So we can do it like
[17:54:58] this. at total customers and then equal.
[17:55:02] So now we are saying whatever value this
[17:55:04] query returns it should be stored inside
[17:55:06] my new variable so that I'm assigning
[17:55:09] values to my variable. But here there is
[17:55:12] one thing that we cannot have any more
[17:55:13] aliases because our query will not
[17:55:16] return any results. Our query have now
[17:55:19] only one task to assign values to my
[17:55:21] variables. So that's why we cannot have
[17:55:23] it like this. We have to remove the
[17:55:24] alias. And the same thing we're going to
[17:55:25] do it for the average. So at average
[17:55:29] score equal to the average score and we
[17:55:32] have to remove the alias. So that's it.
[17:55:34] Now our query having different purpose.
[17:55:36] It is not for returning result. It is to
[17:55:39] assign values to our variables. So now
[17:55:42] we have values in the next step we have
[17:55:43] to go and use it. And we can use our
[17:55:45] variables everywhere inside our store
[17:55:47] procedure. So it could be in the print,
[17:55:49] it could be in the next query. So in any
[17:55:51] select statements in any place.
[17:55:53] Sometimes we use variables in order to
[17:55:55] pass an information from one query to
[17:55:58] another one. But in this example, we
[17:55:59] want to use our variables inside the
[17:56:02] prints. So it is very simple. We now
[17:56:03] we're going to go and replace the static
[17:56:06] number and it's like the parameter.
[17:56:07] We're going to say at total customers
[17:56:11] and the same thing for the average at
[17:56:14] average score. So that's it. It's very
[17:56:17] simple. So again the step one we have to
[17:56:19] declare them to define it for SQL and
[17:56:21] with that we're going to get an empty
[17:56:22] variable. The second step we have to add
[17:56:25] values to those variables and the last
[17:56:27] step we have to go and use those
[17:56:28] variables. So it makes sense right now
[17:56:31] if you check our message over here you
[17:56:33] can see that everything is dynamic and
[17:56:35] we don't have any static values but
[17:56:37] there is one more thing that's in the
[17:56:39] print everything should be as a string.
[17:56:41] So we cannot have dates numbers floats
[17:56:43] and so on. So that's why you have to
[17:56:45] make check if you're adding any
[17:56:46] parameter and variables all of them
[17:56:48] should be string. So the country it is
[17:56:51] okay because we have the data type of
[17:56:53] varchar but the total number and the
[17:56:55] average score this is not really good
[17:56:58] because they have different data type
[17:56:59] and we have to go and now cast those
[17:57:01] data types to another one. So we're
[17:57:04] going to say cast and we're going to say
[17:57:05] here as invar so that we don't get any
[17:57:09] errors from SQL. So cast as well here as
[17:57:12] in
[17:57:14] vchar like this. All right. So I think
[17:57:17] we are ready. Let's go and change the
[17:57:20] definition of our store procedure in
[17:57:21] order to test. So let's go and execute.
[17:57:23] Perfect. And now let's go and test. So
[17:57:26] let's start with the defaults where we
[17:57:27] have the parameter as USA. So now as you
[17:57:30] can see we are getting one result and
[17:57:32] this is from the second query. So the
[17:57:34] first query is not returning anything
[17:57:36] anymore in the output. But if you go to
[17:57:38] the messages over here, you can see we
[17:57:40] have a new message. It says total
[17:57:42] customers from USA is equal to three and
[17:57:45] the average score from USA is equal to
[17:57:48] 825. And this is exactly what we wanted
[17:57:51] for our reports. Now let's go and
[17:57:53] execute the parameter equal to Germany.
[17:57:56] Again, we have only one result. And in
[17:57:58] the messages, we're going to get total
[17:57:59] customers from Germany is equal to two
[17:58:01] and the average score from Germany is
[17:58:03] equal to 425. So this is exactly how we
[17:58:06] work with the variables. We use it in
[17:58:08] order to hold one information in one
[17:58:10] place in order to reuse it later in
[17:58:13] different place. So that's it for
[17:58:15] [Music]
[17:58:18] variables. All right everyone. Now we're
[17:58:20] going to talk about how to control the
[17:58:21] flow in your store procedure and we're
[17:58:24] going to learn how to do that using the
[17:58:25] if else statements. So now let's have
[17:58:27] the following scenario. Now if you check
[17:58:29] our query over here we are doing the
[17:58:31] average of score and if you check the
[17:58:33] data you can see that in the scores we
[17:58:36] have nulls and nulls are really bad for
[17:58:38] aggregations. So we usually have to
[17:58:40] clean up our data before doing any
[17:58:42] aggregations. And in this scenario we
[17:58:45] can understand null as a zero. And how
[17:58:47] we going to clean up and handle the
[17:58:48] data? We're going to go and make an
[17:58:50] update on our table where we say if
[17:58:52] there is like a null then make it as a
[17:58:55] zero. And we will do this as a pre-step
[17:58:57] inside our store procedure. So that
[17:58:59] means first we have to clean up the data
[17:59:02] and then afterward we're going to
[17:59:03] generate the reports. And this is what
[17:59:05] we usually do inside SQL projects. So
[17:59:08] the logic going to be very simple. We
[17:59:10] have to check first do we have nulls
[17:59:11] inside the score. If the answer is yes
[17:59:14] then we have to go and update the null
[17:59:16] values to zero. But if the answer is no,
[17:59:18] we don't have any values then we can
[17:59:20] skip everything. So now we're going to
[17:59:22] go and build this logic inside our store
[17:59:24] procedure in order to clean up and
[17:59:26] prepare the data. So let's go. Okay. So
[17:59:28] now this part we're going to call it
[17:59:31] generating
[17:59:33] reports and we're going to have another
[17:59:35] part called prepare and clean up data.
[17:59:40] So now let's prepare first the structure
[17:59:42] of the if statements. So the syntax
[17:59:44] going to look like this. So if and then
[17:59:46] begin and end. So this is the block of
[17:59:50] the if and we're going to do the same
[17:59:51] thing for the else. So we have else and
[17:59:54] we have begin and end. Let me just
[17:59:56] separate them. So now how this works? We
[17:59:58] have to create a condition. If the
[18:00:00] condition is met then the if statement
[18:00:03] going to be executed. But if the
[18:00:04] condition is not fulfilled and we have
[18:00:06] false then the else statement going to
[18:00:08] be executed. So what is the condition?
[18:00:10] We have to check whether there is null
[18:00:12] inside the scores. So let's write a very
[18:00:15] simple query. It's going to say select
[18:00:17] one from sales
[18:00:21] customers where score is null and always
[18:00:25] we have to check the country equal to
[18:00:28] let's say USA. So let's go and execute
[18:00:31] this one over here. So now we are
[18:00:32] getting in the output a results. If we
[18:00:34] are getting a results that means
[18:00:36] somewhere there are nulls. But if you go
[18:00:39] for example and say here Germany and
[18:00:41] execute the same query in the output you
[18:00:44] see that we don't have any results. That
[18:00:46] means for the German customers we don't
[18:00:48] have any nulls in their scores. So if
[18:00:51] this query returns something we have
[18:00:53] nulls. If it didn't return anything then
[18:00:55] there is no nulls. And we're going to
[18:00:57] use exactly this query as a condition.
[18:01:00] So we're going to take our check and say
[18:01:02] if exists and then two parenthesis and
[18:01:05] then we put our query. So what we are
[18:01:07] saying if exist if this query return
[18:01:10] anything then go and execute the next
[18:01:12] block and if it is not exist that means
[18:01:15] it is not returning anything then go and
[18:01:17] execute the second block. So it's a
[18:01:19] logic right it's very simple now of
[18:01:20] course instead of having a static value
[18:01:22] over here we can use our parameter so at
[18:01:25] country and now we have to tell SQL what
[18:01:27] to do if it exists. So in between we can
[18:01:31] have like an update statement. So update
[18:01:33] sales customers and we're going to set
[18:01:36] the score equal to zero. But very
[18:01:39] important we have to go and use where
[18:01:41] condition otherwise it going to go and
[18:01:43] update everything. The score is
[18:01:47] null and the country equal to our
[18:01:50] parameter country. So with that we are
[18:01:53] updating exactly the nulls for specific
[18:01:55] country. And let's have a semicolon at
[18:01:57] the end. And at the start maybe I'm
[18:02:00] going to say just to have a nice message
[18:02:01] in the output print and we can have a
[18:02:04] message updating null scores to zero and
[18:02:09] as well a semicolon at the end. So if
[18:02:11] there is any nulls then execute the
[18:02:13] whole thing print the message and update
[18:02:15] the table. So now the next step is that
[18:02:17] we're going to go and tell SQL what can
[18:02:19] happen if the condition is not
[18:02:20] fulfilled. That means we don't have any
[18:02:22] nulls. Well we don't have to update the
[18:02:24] table at all because we don't have to
[18:02:26] clean up anything. But I'm going to go
[18:02:28] and make print over here. So print and
[18:02:31] we're going to give the message no null
[18:02:34] scores found. And at the last end I'm
[18:02:38] going to go and put a semicolon. So
[18:02:40] that's it. This is our logic. We are
[18:02:42] checking our condition and then we
[18:02:44] execute if the condition is met where we
[18:02:46] update the table with zero instead of
[18:02:48] null and if the condition is not met
[18:02:50] then don't do anything. Just print a
[18:02:52] message. Now you might say you know what
[18:02:54] why you are doing this? we just can use
[18:02:56] this update statements and we don't need
[18:02:57] the whole if else statements. So why we
[18:02:59] are checking in the first place? I can
[18:03:01] like each time I run this store
[18:03:02] procedure I go and update all the nulls
[18:03:05] if they exist to a zero. Well, this is
[18:03:07] not really professional because you are
[18:03:09] wasting resources. So each time you run
[18:03:11] an update statement like this. So
[18:03:14] imagine that you have a big table and
[18:03:15] each time you run your store procedure,
[18:03:17] SQL have to go and check whether there
[18:03:19] is any nulls and so on. And this is of
[18:03:22] course consume resources. It's way
[18:03:24] better if you go and check first whether
[18:03:26] it's really needed. So that's why we are
[18:03:28] doing this logic. Now as you can see our
[18:03:30] store procedure is getting bigger and
[18:03:32] bigger. So we have like two parts. The
[18:03:34] first part is preparing and cleaning up
[18:03:37] the data. And the second part we are
[18:03:39] generating reports. Let's go and update
[18:03:41] the whole thing and execute it. And now
[18:03:44] we have to do it step by step. So let's
[18:03:46] check our query over here. And you can
[18:03:47] see we have here null for USA customers.
[18:03:50] So let's go first execute it for the USA
[18:03:53] as a defaults. And now let's go and
[18:03:55] check the messages. It's saying updating
[18:03:57] null scores to zero. That means the
[18:04:00] first block is executed because SQL did
[18:04:03] find a customer with a null. And with
[18:04:06] that the average of scores going to be
[18:04:08] different than previously. So we have
[18:04:10] now more accurate average in our
[18:04:13] reports. So if you go and check our
[18:04:15] query again, you can see now we have a
[18:04:16] zero instead of null. Let's go and
[18:04:19] execute it for Germany like this. And
[18:04:21] let's go and check the messages. It says
[18:04:23] no null scores found. And that is
[18:04:25] correct because for Germany we don't
[18:04:28] have any nulls. So with that we have
[18:04:30] created a control flow using the FL
[18:04:33] statements. And as you can see we are
[18:04:34] not doing any more like simple queries.
[18:04:36] We are creating like a mini program. And
[18:04:39] now it's like an ETL where first we
[18:04:41] prepare the data and second we generate
[18:04:43] reports. And you can imagine a real
[18:04:45] project how big those stored procedures
[18:04:47] going to get where you have a lot of
[18:04:49] tables and a lot of things to
[18:04:54] do. Okay. So now we're going to talk
[18:04:56] about the error handling in store
[18:04:58] procedure. Error handling it is like
[18:05:00] essential things to do while programming
[18:05:02] because it gives you the control on what
[18:05:04] can happen once you have an error. And
[18:05:07] there's a lot of things that you can do
[18:05:08] like maybe deleting data, printing a
[18:05:11] very structured like message or maybe
[18:05:13] doing some logging and so on. So you
[18:05:15] have a full control on what to do if
[18:05:18] there is an error and of course we can
[18:05:19] do that in the store procedure. So now
[18:05:21] let's check the quickly the syntax. It
[18:05:23] is usually has two parts. The first part
[18:05:26] is the try part. So the syntax is like
[18:05:28] this begin try end try. So you are
[18:05:31] defining the boundaries of the try and
[18:05:33] in between you going to have all your
[18:05:35] SQL statements and your code and the
[18:05:37] second part going to be the catch parts.
[18:05:39] So you say begin catch and end catch. So
[18:05:42] you are defining the boundaries and then
[18:05:44] in between you can tell SQL what to do
[18:05:46] if there is like an error. So what is
[18:05:48] try and catch? Like the word it says try
[18:05:51] it's like you are attempt to do
[18:05:52] something that might fail. So you are
[18:05:54] telling SQL try to execute this code. So
[18:05:57] the SQL going to go and try to execute
[18:06:00] your codes. And if any error happens
[18:06:02] while executing your codes, the SQL
[18:06:04] going to jump to the second block and
[18:06:06] start doing whatever you have defined in
[18:06:08] the catch. But if there is no errors at
[18:06:11] all, this part will not be executed. So
[18:06:14] the catch is like your backup plan. If
[18:06:16] something goes wrong here, then go to
[18:06:18] the plan B and do something. So let's
[18:06:20] see the workflow of the try catch. So
[18:06:22] first the SQL going to go and execute
[18:06:24] the try and then it going to check is
[18:06:26] there any error. If we don't have any
[18:06:28] error then everything ends and that's
[18:06:30] it. But while execution if the SQL face
[18:06:33] any error what going to happen it going
[18:06:35] to go and execute the catch. So as you
[18:06:37] can see the workflow is very simple and
[18:06:39] this is what we mean with try and catch.
[18:06:41] So let's go back to SQL to have some
[18:06:43] example. All right. So now back to our
[18:06:45] store procedure. Let's go and introduce
[18:06:47] an error inside our code. So let's go
[18:06:49] over here and maybe in our query we're
[18:06:52] going to go and divide by zero which is
[18:06:55] of course a problem. So we have this
[18:06:57] error over here and let's go and update
[18:07:00] the logic of our store procedure. And
[18:07:03] now if you go and execute it. So let's
[18:07:05] go and do that. We will get an error
[18:07:07] saying yeah you cannot divide by zero.
[18:07:10] But now what I would like to do I would
[18:07:11] like to have something else where we
[18:07:12] have customized message when error
[18:07:14] happens. So I would like to have the
[18:07:16] control on which information should be
[18:07:18] displayed if there is an issue. And in
[18:07:20] order to do that we have to use the try
[18:07:22] and catch. So it's going to be very
[18:07:23] simple. Now this is my whole code. So
[18:07:26] the whole thing from preparing to
[18:07:28] generate the report the whole thing is
[18:07:30] my code and we have to put the whole
[18:07:32] thing in a try. So how to do that?
[18:07:34] Exactly after the first begin we're
[18:07:37] going to have another begin but for the
[18:07:39] try. And now what we're going to do,
[18:07:40] we're going to go to the last end over
[18:07:42] here and have an end try. So with that
[18:07:46] we put now the whole code inside the
[18:07:48] try. And after that we're going to
[18:07:50] introduce the catch. So begin catch and
[18:07:54] end catch. And now in between we have to
[18:07:56] tell SQL what can happen if we encounter
[18:07:59] an error. And here we can do many stuff
[18:08:01] but I would like now to focus on
[18:08:03] customizing the error message. Let's
[18:08:05] start with the first one. So I'm going
[18:08:06] to say print let's say an error
[18:08:11] accord. This is the first thing. Then on
[18:08:14] the next line I'm going to print more
[18:08:16] informations. And now we're going to say
[18:08:17] the error message. So error message
[18:08:21] double point space. And now we can go
[18:08:24] and use some predefined functions from
[18:08:26] SQL like for example the error
[18:08:29] message. This function going to return
[18:08:31] the description of the error like the
[18:08:33] one we have here divide by zero error
[18:08:35] encountered and we can go and keep
[18:08:37] adding stuff the way that we need like
[18:08:41] maybe the error
[18:08:43] number. So we can have it like this and
[18:08:45] for that we have as well a function
[18:08:46] called error number and I think we have
[18:08:49] to cast this one because it is a number
[18:08:51] and in the messages we have to have only
[18:08:54] vchar. So this going to be as int var
[18:08:58] like this and we can keep adding stuff
[18:09:00] to our message like for example let's
[18:09:03] take the error line and for that we have
[18:09:05] as well a function so it's going to be
[18:09:07] the error
[18:09:09] line like this and we have to cast
[18:09:12] it because it is as well a number and as
[18:09:16] well what is really important is the
[18:09:19] name of the stored procedure. So error
[18:09:23] procedure and we have a function for
[18:09:25] that error procedure like this. It's
[18:09:29] going to be a string. So that's why I
[18:09:31] don't have to cast it. So now with that
[18:09:32] we have defined for SQL what to do if
[18:09:35] there is like an error in our code. So
[18:09:37] let's go and execute the whole thing.
[18:09:40] And now let's go and execute our stored
[18:09:42] procedure. So let's go and do that. So
[18:09:44] now as you can see in the output we are
[18:09:46] not getting any results and it is not
[18:09:48] giving an error. But if you go to the
[18:09:50] messages, you will see a very nice
[18:09:52] message. So it says an error is
[18:09:54] occurred. The error message is divided
[18:09:56] by zero and we have the error number in
[18:09:59] which line and as well the stored
[18:10:01] procedure name. So as you can see it's
[18:10:02] amazing. This is how we use the try and
[18:10:05] catch in order to have more options on
[18:10:07] to control what can happen if there is
[18:10:09] an
[18:10:13] error. Now the next step what I'm going
[18:10:15] to do, we have to go and organize our
[18:10:17] store procedure. As you can see,
[18:10:18] everything is getting bigger. So now
[18:10:19] what we usually do, we use tab in order
[18:10:21] to make spaces between each section. So
[18:10:24] now the first section is between the
[18:10:26] first begin and the last end. So we have
[18:10:29] to go and mark everything and hit once a
[18:10:32] tab. So now it is easier to read. Now
[18:10:35] the whole thing is our codes. So now the
[18:10:37] next level is the block of the try. So
[18:10:39] the whole thing over here is the try. So
[18:10:43] let's go and do that. I'm just going to
[18:10:45] mark everything until here and then hit
[18:10:48] tab. So now we can see it better, right?
[18:10:50] And the same thing for the catch. I
[18:10:52] think I have already done that. So it's
[18:10:54] already pushed. Now we go to the next
[18:10:55] level. So between this begin and end,
[18:10:58] everything is pushed. So this looks
[18:11:00] nice. The same thing over here. It's
[18:11:02] pushed as well. And then we don't have
[18:11:03] here any begin and end. So it looks
[18:11:06] okay. And the same thing over here. So
[18:11:08] all our begin and end is now sorted
[18:11:11] correctly. Now the next step is that we
[18:11:12] can go and improve the comments a little
[18:11:14] bit. So we can split our code into
[18:11:16] multiple sections. So what we're going
[18:11:18] to do, we're going to go over here and
[18:11:19] say this is step one. And what I like to
[18:11:22] do is to go and add separation using the
[18:11:25] equals or any special character that you
[18:11:27] like and as well here. So with that we
[18:11:31] have the first step. We are preparing
[18:11:32] the data. And then let's go and copy the
[18:11:34] whole thing and go over here and say
[18:11:37] this is the step two. And we're going to
[18:11:39] say this is
[18:11:41] generating summary reports and something
[18:11:45] like this. And of course below that we
[18:11:47] can say what is this report about. So
[18:11:51] calculate total
[18:11:53] customers and average score for
[18:11:58] specific country. And as well we can go
[18:12:01] over here and add as well a comment.
[18:12:04] calculate total number of orders and
[18:12:09] total sales for specific country. And of
[18:12:14] course we have to go and remove this
[18:12:16] error over here otherwise we'll get an
[18:12:18] error and we can go and add something
[18:12:20] about the catch where we can say like
[18:12:21] this again few
[18:12:23] comments we're going to say error
[18:12:28] handling. So let's go and execute it
[18:12:31] again in order to make sure we have the
[18:12:33] newest version. And with that we are
[18:12:35] done. We have a really nice stored
[18:12:36] procedure with multiple steps and we
[18:12:38] have it professional where we have error
[18:12:40] handling inside it and everything looks
[18:12:42] well organized and easy to read. So this
[18:12:45] is how we build stored procedures. All
[18:12:47] right my friends. So that's all about
[18:12:48] the store procedures. That was an
[18:12:50] amazing feature in SQL to add
[18:12:52] programmability in SQL. Now in the next
[18:12:54] step we're going to cover quickly the
[18:12:56] topic of the triggers. So let's
[18:13:02] go. All right. So previously we have
[18:13:04] understood that we can put all our SQL
[18:13:07] statements in one stored procedure and
[18:13:09] you have to go and manually execute the
[18:13:12] store procedure. So that means in order
[18:13:14] to trigger the start procedure, you have
[18:13:16] manually to execute it and this is of
[18:13:19] course a problem. How about to do that
[18:13:21] automatically? So triggers in SQL they
[18:13:24] are special stored procedure that
[18:13:26] automatically runs or let's say fired in
[18:13:29] response to a specific event that
[18:13:31] happens on a table. So what this exactly
[18:13:34] means? So now let's say that we have a
[18:13:36] table in our database and now something
[18:13:38] could happen to this table like
[18:13:40] inserting data, deleting, updating data,
[18:13:43] all those stuff that is happening we
[18:13:45] call them events. And now what we can do
[18:13:47] we can go and attach like a trigger on
[18:13:50] top of this table and each time an event
[18:13:52] happened like insert update delete
[18:13:54] something else going to be triggered
[18:13:56] like maybe going and inserting data
[18:13:58] somewhere else in another table or doing
[18:14:01] a check whether we are allowed to delete
[18:14:03] the data in the first place or maybe
[18:14:05] sending a warning message or something.
[18:14:07] So based on any changes to the table we
[18:14:10] can trigger another events and we can do
[18:14:12] that using the SQL triggers and for the
[18:14:15] SQL triggers we have like multiple types
[18:14:17] like the DML triggers and this type of
[18:14:20] trigger going to respond once we have
[18:14:22] like insert update delete statements.
[18:14:25] Another type of triggers we have the DDL
[18:14:27] triggers like you can make a trigger to
[18:14:29] respond to any schema changes like
[18:14:32] creating altering or dropping a table or
[18:14:35] even view by the way not only tables.
[18:14:37] And the third type of triggers we have
[18:14:38] the login trigger. So the trigger can
[18:14:40] respond to login events. Now in this
[18:14:43] tutorial we're going to focus on the DML
[18:14:45] triggers the insert update delete. And
[18:14:47] for the DML triggers we have two types.
[18:14:50] We have after triggers and as well we
[18:14:52] have instead of triggers. So as the name
[18:14:55] suggest if you use after so it can be
[18:14:58] executed after the event and the other
[18:15:00] type that instead of it's something that
[18:15:02] cannot wait until everything happens. So
[18:15:05] this time the trigger going to be
[18:15:06] executed during the event not after it.
[18:15:10] So now in order to understand all of
[18:15:11] this we're going to have really nice use
[18:15:17] case. And now the use case is about
[18:15:19] maintaining an audit logs. So what we
[18:15:22] mean with that? Let's have for example
[18:15:23] the table employees. The employee data
[18:15:26] are usually very sensitive informations
[18:15:28] because there we can see which employees
[18:15:31] are added, the salary updates, the
[18:15:33] employee terminations and this makes the
[18:15:35] table very important because we would
[18:15:38] like to track all those changes that is
[18:15:40] happening to this table. So each time we
[18:15:42] are inserting, updating, deleting, we
[18:15:44] would like to maintain a log about all
[18:15:46] those changes in order to analyze it
[18:15:48] later. It is of course very important
[18:15:50] such a logs for the compliance and the
[18:15:53] auditors and in case there is like a
[18:15:55] problem we can go to the logs to
[18:15:57] understand when this happened who made
[18:15:59] the changes and what exactly changed and
[18:16:02] now in order to maintain logs we're
[18:16:04] going to use the power of triggers. So
[18:16:06] what we're going to do we're going to go
[18:16:07] and attach like a trigger on the table
[18:16:10] employees and each time we insert new
[18:16:13] data to the employees we are triggering
[18:16:16] another events. So what can happen this
[18:16:18] new employee going to be inserted in the
[18:16:21] audit logs in order to have a record
[18:16:23] about this activity in the logs. So that
[18:16:26] means each time you are inserting data
[18:16:28] to the table employees you are
[18:16:30] automatically inserting data inside the
[18:16:32] logs and this is really amazing use case
[18:16:34] for the triggers. So let's go and
[18:16:36] implement it. Okay. So now let's check
[18:16:38] quickly the syntax of the triggers. So
[18:16:40] we start with the usuals create trigger
[18:16:42] then the trigger name and then we have
[18:16:43] to specify on which table this trigger
[18:16:46] going to be built in. So now we are
[18:16:48] attaching like a trigger on top of one
[18:16:50] table and after that we have to define
[18:16:52] for SQL when this trigger going to
[18:16:55] happen. So what is actually triggering
[18:16:57] the trigger and here you can define
[18:16:59] after or instead then you have to define
[18:17:02] the operator. So first you have to
[18:17:04] define like after or instead of and then
[18:17:06] we have to define the operation. So
[18:17:09] insert, update, delete or one of them.
[18:17:11] And with that you are telling SQL when
[18:17:13] exactly this should happen. And now
[18:17:14] after that we have to tell SQL what
[18:17:17] going to happen if the trigger is
[18:17:19] triggered. So here we have like begin
[18:17:21] and end. And then we have like several
[18:17:23] skill statements that's going to
[18:17:24] describe what's going to happen once we
[18:17:26] have the trigger. So that's it. As you
[18:17:28] can see the syntax is very simple. Okay.
[18:17:30] So now let's do it step by step. First I
[18:17:32] would like to create a table where we're
[18:17:34] going to store the logs information. So
[18:17:36] it's going to be very simple table.
[18:17:38] We're going to say create table. Then
[18:17:40] we're going to call it sales employee
[18:17:43] logs and we're going to have the
[18:17:44] following columns inside it. So let's
[18:17:47] start with the primary key. It's going
[18:17:48] to be the log ID and the data type int
[18:17:50] and then we're going to have like a
[18:17:51] sequence. So we're going to have
[18:17:53] identity and this is the primary key.
[18:17:56] Let's go to the next one. It's going to
[18:17:57] be the employee ID and the data type
[18:18:00] going to be ins. The next one is going
[18:18:01] to be the log message. So let's have it
[18:18:04] as a vchar and I'm going to have it like
[18:18:07] 255 and then to the next one we're going
[18:18:09] to have the lock dates and then we're
[18:18:11] going to have like let's say a date or a
[18:18:13] date time. So that's it. Let's go and
[18:18:15] execute it and with that we have a new
[18:18:17] table inside our database. Now the next
[18:18:19] step is that we're going to go and
[18:18:21] create our trigger. So we're going to
[18:18:23] say create trigger and I'm going to call
[18:18:26] it like this trg. This is just a prefix
[18:18:28] to indicate this is a trigger. And I'm
[18:18:30] just going to call it after insert
[18:18:33] employee. And now we have to define the
[18:18:36] table. So it's going to be on sales
[18:18:39] employee. So now with that we are saying
[18:18:41] we have now a trigger on the table
[18:18:43] employees. And now we have to define the
[18:18:45] logic. So we're going to use after
[18:18:47] insert. So that means after we insert
[18:18:50] any record to the table employees the
[18:18:52] following things should happen. So we're
[18:18:54] going to say as and then begin and end
[18:18:58] and in between we can have our logic. So
[18:19:00] what can happen after a new record is
[18:19:02] inserted to the employees. We're going
[18:19:04] to go and insert a new record to the
[18:19:06] employee logs. So we're going to have
[18:19:08] insert into sales employee
[18:19:12] logs and we're going to have here the
[18:19:14] three columns employee
[18:19:16] ID the log
[18:19:18] message and the log dates. So now which
[18:19:22] value is going to be inserted? it going
[18:19:23] to be like from a query. So we're going
[18:19:25] to say select and we're going to say as
[18:19:27] well employee ID and for the log message
[18:19:30] we can have customized one like let's
[18:19:32] say new employee added and it's going to
[18:19:37] be equal to the employee ID. So in order
[18:19:40] to have the employee ID it's going to be
[18:19:41] like
[18:19:42] this. So that's it. Now to the next one
[18:19:46] we need the log date. It's going to be
[18:19:48] get date. And now you might say okay but
[18:19:51] where this employee ID is coming from?
[18:19:54] Well, it going to come from the table
[18:19:56] from inserted. So what is actually
[18:19:58] inserted? It is like special virtual
[18:20:00] table that holds all the new inserted
[18:20:03] data to our table employees. So anything
[18:20:06] we are inserting inside the employees
[18:20:08] will be available inside this table. And
[18:20:11] of course this is only available during
[18:20:14] the execution of this trigger. So you
[18:20:16] cannot go now outside of this query and
[18:20:18] start querying the table inserted
[18:20:20] because you will not find anything. This
[18:20:21] is only like a virtual table that
[18:20:23] contains anything that you are doing to
[18:20:26] the table employees and you find a lot
[18:20:28] of informations like the salary, the age
[18:20:30] and so on. So that's it for the
[18:20:32] inserted. Now we have to make sure that
[18:20:35] in our message we have everything as a
[18:20:37] string because the employee ID is an
[18:20:39] integer. So we have to cast it. So cast
[18:20:42] and then we're going to say as far char
[18:20:46] like this otherwise we'll get an error.
[18:20:48] So I think we have our trigger ready. We
[18:20:50] have a new trigger on the table
[18:20:52] employees. And now the first question is
[18:20:55] when this trigger going to happen? Well
[18:20:57] it can happen after inserting data to
[18:21:00] the employees. And then the second
[18:21:01] question what's going to happen? Well,
[18:21:04] once we have this event, the whole thing
[18:21:06] here going to be executed where we are
[18:21:08] saying insert to the logs, the employee
[18:21:11] ID, the message and as well the date
[18:21:13] when this happens. And we can get all
[18:21:15] those informations from the table, the
[18:21:17] virtual table inserted. So I think we
[18:21:20] are ready. Let's go and execute it. And
[18:21:22] now if you go to the object explorer to
[18:21:24] our database, let's go to our table
[18:21:26] employees and then to the triggers. So
[18:21:29] if you refresh over here you can see our
[18:21:31] new trigger that we just created. So
[18:21:34] with that we have to find our trigger
[18:21:35] and we are ready. Now the next step is
[18:21:37] that we're going to go and trigger our
[18:21:39] trigger. So let's go and do that. Let's
[18:21:41] have a new query. But first I'm going to
[18:21:43] have a look to our
[18:21:45] logs. So sales employee logs. So let's
[18:21:50] query this one. And as you can see our
[18:21:52] logs is empty because we didn't insert
[18:21:54] anything to the table employees. Let's
[18:21:57] go and do that. Let's trigger our
[18:21:58] trigger. So what we're going to do,
[18:21:59] we're going to say insert into sales
[18:22:02] employees and we're going to have the
[18:22:04] following values. So we are at the
[18:22:06] counter, I think six. Let's have the
[18:22:09] first name
[18:22:10] Maria. The last name an then we're going
[18:22:14] to have the position. It's going to be
[18:22:15] the HR for example. The birth date,
[18:22:18] let's pick something. I don't know. We
[18:22:21] have a female
[18:22:23] here. And the salary. Let's go and get
[18:22:26] this salary and the hierarchy it can be
[18:22:29] for example three. So let's go and
[18:22:31] execute it. And with that as you can see
[18:22:33] we have inserted a new data to the
[18:22:34] employees. Let's check now the logs. So
[18:22:38] let's query it. So we have here nice log
[18:22:40] about the employee number six. And we
[18:22:42] have here nice message and when this did
[18:22:45] happen. Of course you can go and insert
[18:22:47] another employee let's say seven with
[18:22:50] the same data. So let's do that and
[18:22:52] check the logs. And with that we have
[18:22:55] another log for the new employee. So
[18:22:57] this is really amazing use case in order
[18:22:58] to maintain a log to your data and you
[18:23:01] can go and make like some analyzes on
[18:23:03] how many inserted happens and of course
[18:23:06] not only on the insert you can have it
[18:23:08] on the update delete. So as you can see
[18:23:10] it is very simple. This is how we create
[18:23:12] the triggers in SQL. All right my
[18:23:13] friends. So that's all about the
[18:23:14] triggers with that with with that we
[18:23:17] have covered now with that we have
[18:23:19] covered now all the concepts and topics
[18:23:21] that you have to learn about SQL. Now in
[18:23:23] the next chapter it's going to be about
[18:23:25] the performance. So as you start writing
[18:23:27] queries and so on you will start
[18:23:29] noticing some queries are really slow.
[18:23:31] Now what we're going to do in this
[18:23:32] chapter we're going to learn different
[18:23:34] techniques on how to optimize the
[18:23:35] performance. And the first and the very
[18:23:37] famous one is to go and build indexes in
[18:23:40] databases. So let's understand what this
[18:23:46] means. So what is an index? An index is
[18:23:49] a data structure that provides a quick
[18:23:52] access to the rows to improve the speed
[18:23:55] of your queries. So an index is like a
[18:23:58] guide for your database in order to
[18:24:00] speed up the process of searching for
[18:24:02] data especially if you have like big
[18:24:04] tables. So now in order to understand
[18:24:06] what are indexes, imagine you have huge
[18:24:08] book and you want to find a specific
[18:24:10] topic or a chapter. Instead of flipping
[18:24:13] each single page in order to find the
[18:24:15] topic that you are searching for, you
[18:24:16] would use the index at the back of the
[18:24:18] book in order to jump straight to the
[18:24:21] right page. And that's exactly what
[18:24:23] index does but for your data. Another
[18:24:26] analogy that I use in order to
[18:24:27] understand indexes is think about the
[18:24:30] indexes as a big hotel. Now let's say
[18:24:33] that in the hotel we don't have any
[18:24:34] guide and you would like to find the
[18:24:36] room number let's say 5001. Now what you
[18:24:40] going to do? You're going to go and
[18:24:41] search for your room floor by floor and
[18:24:44] checking each room until you find your
[18:24:46] room. But instead of that, thankfully
[18:24:48] hotels have a numbering system. And you
[18:24:51] can ask for a map from the reception in
[18:24:53] order to understand in which building in
[18:24:55] which floor you can find your room. So
[18:24:58] by just following the map and maybe some
[18:25:00] signs, it's going to be very quickly to
[18:25:02] locate and find your room in such a big
[18:25:04] hotel. And that's exactly what each
[18:25:06] database needs. It needs an index in
[18:25:08] order to help the database finding and
[18:25:11] locating the right data without having
[18:25:13] to scan
[18:25:18] everything. And now let's say that you
[18:25:20] ask me, you know what, I have this big
[18:25:22] table and I would like to speed up the
[18:25:24] queries using indexes. And my first
[18:25:27] question going to be, what are you
[18:25:28] exactly doing with this table? Are you
[18:25:30] using this table to search for text or
[18:25:33] are you doing like complex analyszis
[18:25:35] with this table? And the reason why I'm
[18:25:37] asking this is that we have different
[18:25:40] indexes in databases for different
[18:25:42] purposes. So now let's have a quick look
[18:25:43] to the different types of indexes that
[18:25:46] we have in database. I divide the
[18:25:48] indexes in databases into three
[18:25:50] categories. The first one is by the
[18:25:52] structure how the database is organizing
[18:25:55] and referencing the data. And here we
[18:25:57] have two types. The clustered index and
[18:26:00] the non-clustered index. Those are very
[18:26:02] important to understand. Now we have
[18:26:04] another category for the indexes. We can
[18:26:07] divide them by the storage. And in this
[18:26:09] category we are talking about how the
[18:26:11] data is stored physically in the
[18:26:14] database. So we have two types. We have
[18:26:16] the row store index and the column store
[18:26:19] index. And the third type is the
[18:26:21] functions and here we have two types. We
[18:26:24] have the unique index and the filtered
[18:26:26] index. Now each index type has its own
[18:26:29] strings but as well there is always a
[18:26:32] tradeoff. Some might improve their read
[18:26:34] performance. The other one might improve
[18:26:36] the insert and update operations. So
[18:26:39] it's all about choosing the right type
[18:26:41] of index for the job. So now what we're
[18:26:43] going to do, we're going to go and deep
[18:26:44] dive into each of those types in order
[18:26:46] to understand how they work and how we
[18:26:49] can create them. And we will start with
[18:26:50] the first category, the structure. We
[18:26:53] have the clustered index and the
[18:26:54] nclustered index.
[18:27:00] Now before we dive into how the indexes
[18:27:02] works in databases, let's understand
[18:27:04] first what happens to the database
[18:27:07] tables if you don't use any index. When
[18:27:10] you create a new table in your database
[18:27:12] like for example the customers table
[18:27:14] where you have let's say 20 customers
[18:27:16] inside this table. What you're going to
[18:27:18] see at the client side is like
[18:27:20] spreadsheets like a table with rows and
[18:27:23] columns but behind the scenes the
[18:27:24] database store it a bit differently.
[18:27:27] It's going to store the data in a data
[18:27:29] file on the disk and inside this file
[18:27:32] the data can be stored inside blocks
[18:27:34] called pages. So it's not like rows and
[18:27:37] columns that are stored inside data
[18:27:39] files and inside the data files we have
[18:27:42] pages. So what is a page? A page is the
[18:27:46] unit of data storage in a database and
[18:27:49] it is a fixed size of 8 kilobyt where
[18:27:52] the SQL database can store anything
[18:27:55] inside it. It can store inside it the
[18:27:57] rows of your tables or columns metadata
[18:28:00] indexes and every time you are
[18:28:02] interacting with your data the SQL is
[18:28:05] reading and writing to those pages. So
[18:28:08] as you can see the SQL is not storing
[18:28:10] the data inside like rows and columns.
[18:28:13] So if you are running a query the SQL is
[18:28:15] not like selecting a specific column it
[18:28:18] always fetch a data page in order to
[18:28:21] read the rows inside this page. And the
[18:28:24] main two types that we're going to learn
[18:28:25] is the data page and the index page. So
[18:28:28] how the data page looks like it is
[18:28:30] divided into multiple sections. The
[18:28:32] first section is the page header where
[18:28:34] the database can store key informations
[18:28:36] about the metadata like the page ID and
[18:28:39] it has the following format. It start
[18:28:41] with the file ID like one and then we
[18:28:44] have a unique number for each page. So
[18:28:46] for example 150. So the page header is a
[18:28:49] fixed size of 96 bytes. Now to the next
[18:28:51] section, we're going to have a variable
[18:28:53] size. This is where your data row is
[18:28:56] going to be stored. So your actual data
[18:28:58] and row is going to be stored in this
[18:28:59] section. And the SQL going to try and
[18:29:01] fits as many rows as it can in one
[18:29:04] single page. And this of course depends
[18:29:06] on the size of each row. So if you have
[18:29:09] like a large table where the rows are
[18:29:11] really big, so SQL can fit only few rows
[18:29:14] in one single page. And now moving on to
[18:29:16] the last section in the data page, we
[18:29:19] have the offset array. This is like a
[18:29:21] quick index for the rows stored inside
[18:29:24] this page. It keeps track of where each
[18:29:27] rows begins so that the SQL can easily
[18:29:30] locate a specific row without having SQL
[18:29:33] like scanning the entire page in order
[18:29:35] to find a row. So this is the structure
[18:29:37] of the data page and this is exactly how
[18:29:40] the SQL stores data inside the
[18:29:43] databases. So now back to our example
[18:29:44] where we have the customers table and 20
[18:29:47] rows. So let's see how SQL going to be
[18:29:49] creating those pages. Now if you are not
[18:29:51] using any index in this table. So now
[18:29:53] what going to happen? SQL going to
[18:29:55] insert the data inside those pages as
[18:29:57] you are inserting the data inside the
[18:30:00] customers. So maybe first you are
[18:30:01] inserting the customers like 12 5 6 7
[18:30:05] and SQL going to insert it to the data
[18:30:07] pages exactly like that. So that means
[18:30:10] SQL is just inserting the data as you
[18:30:12] insert it to the table. So let's say
[18:30:14] each data page is like fitting only five
[18:30:17] rows. So after we insert five customers,
[18:30:19] SQL going to go and create another data
[18:30:21] page for the next rows. So in the next
[18:30:24] page, the SQL going to insert the next
[18:30:26] five customers. And once it's full, it's
[18:30:29] going to create another data page in
[18:30:30] order to start adding the next customer
[18:30:33] until we have like for example four
[18:30:35] pages for that 20 customers. So now if
[18:30:38] you check the customers inside those
[18:30:39] four pages you see that they are not
[18:30:42] sorted at all and that's because in this
[18:30:44] scenario we are not using any index. So
[18:30:47] we call this structure as a heap
[18:30:49] structure. So a heap table is a table
[18:30:52] without a clustered index. That means
[18:30:54] the rows are stored randomly without any
[18:30:57] particular order. This is not a really
[18:31:00] bad because it's going to be very quick
[18:31:02] to insert data inside this table. But of
[18:31:05] course finding something from this table
[18:31:07] going to be very slow. So this is the
[18:31:09] first tradeoff. You have a very fast
[18:31:12] writes but a very bad reads. Think about
[18:31:15] it like you are throwing all your papers
[18:31:17] in a drawer without organizing them. So
[18:31:20] you can toss things very quickly in this
[18:31:22] drawer. But if you want to search for
[18:31:24] specific paper later, it's going to be
[18:31:26] very long process until you find it
[18:31:28] because nothing's in order. So now let's
[18:31:31] see how the SQL going to handle if you
[18:31:33] read something from this table. Let's
[18:31:35] say that you are searching for the
[18:31:36] customer with the ID 14. So now SQL has
[18:31:39] totally no idea where to find this
[18:31:41] customer. So SQL going to start fetching
[18:31:43] each data page and start scanning each
[18:31:46] row. So it's going to start with the
[18:31:47] first data page and start scanning.
[18:31:49] Well, SQL will not find 14 here. So SQL
[18:31:52] going to go to the next page and start
[18:31:54] scanning as well. Searching for the ID
[18:31:57] 14 and nothing going to be found. The
[18:31:59] same thing for the third page as well.
[18:32:00] SQL will not find 14. So SQL going to go
[18:32:03] to the last data page and there after
[18:32:05] scanning four rows in this data page
[18:32:08] finally SQL going to find the customer
[18:32:10] number 14 and it's going to return it
[18:32:12] for the clients. So as you can see in
[18:32:14] order to find one customer SQL did read
[18:32:18] four different pages and scanned like 19
[18:32:21] rows in order to find the customer and
[18:32:23] this process we call it full table scan.
[18:32:27] So the full table scans means SQL is
[18:32:29] scanning the entire table page by page
[18:32:32] and row by row in order to find specific
[18:32:35] row. And of course for this table maybe
[18:32:37] it's not a big deal. But if you have
[18:32:39] like a big table where you have like
[18:32:41] hundred of thousands or maybe millions
[18:32:43] of rows searching through the heap
[18:32:45] structure going to be very painful and
[18:32:47] slow in order to locate one row. And
[18:32:49] here exactly why we need indexes in SQL
[18:32:52] databases. So let's understand the first
[18:32:54] type of indexes the clustered
[18:33:00] index. All right. So now let's
[18:33:02] understand what can happen if you create
[18:33:04] clustered index in your table. So say
[18:33:06] you create a clustered index on the ID
[18:33:09] column of the customers. So the first
[18:33:11] thing that's going to happen SQL going
[18:33:12] to physically sort all the data based on
[18:33:16] the column ID. So the rows going to
[18:33:18] rearranged in each data page from the
[18:33:21] lowest to the highest. So in the first
[18:33:23] page we're going to have the first
[18:33:25] customer ID number one then 2 3 4 5
[18:33:28] until we reach in the last page the last
[18:33:31] customer number 20. So as you can see
[18:33:33] the first page has the lowest value and
[18:33:36] the last page has the highest value. So
[18:33:39] that's not all. The next step is that
[18:33:41] SQL going to go and start structuring
[18:33:44] and building the B tree. So what is a B
[18:33:48] tree? A B tree short for balance tree.
[18:33:50] It is hierarchal structure that store
[18:33:53] the data as a tree upside
[18:33:56] [Music]
[18:34:02] down. It start with the root the root
[18:34:05] node and then it keep branching out
[18:34:07] until we reach eventually the leaves.
[18:34:10] Between the leaf nodes and the root
[18:34:12] nodes we call this section the
[18:34:14] intermediate nodes. So it could be like
[18:34:16] one level or multiple levels between the
[18:34:18] root and the leaves. And once SQL
[18:34:21] construct the B tree, it's going to be
[18:34:23] very easy for SQL to navigate through
[18:34:25] the B tree in order to find specific
[18:34:27] information. So let's see how SQL is
[18:34:30] building the B tree for the clustered
[18:34:32] index. Now very important to understand
[18:34:34] that the leaves the leaf nodes and the B
[18:34:37] tree for the clustered index contain the
[18:34:40] actual data the data pages. So all your
[18:34:43] nice sorted data pages and your data is
[18:34:46] stored at the leaf level. Then after
[18:34:48] that SQL going to start building the
[18:34:50] intermediate nodes and here the database
[18:34:52] going to use different type of pages. We
[18:34:56] have the index page. So in the index
[18:34:58] page we cannot find the actual data the
[18:35:01] entire rows but instead the index page
[18:35:03] stores a key value that contain a
[18:35:06] pointer to another index page or to a
[18:35:09] data page. So for example we have here
[18:35:11] the value one the key and then the value
[18:35:14] going to be the ID of the data page. So
[18:35:16] here we don't have like the whole row
[18:35:18] about the data we have here only a
[18:35:21] pointer to another data page. So here we
[18:35:23] are telling the scale if you are
[18:35:25] searching for ids between 1 and five you
[18:35:28] can locate it at the data page ID
[18:35:32] 1.100 and then we can store in this
[18:35:34] index page another pointer where we can
[18:35:36] tell SQL if you are searching between 6
[18:35:38] and 10 then you can locate it at the
[18:35:41] second data page. So this is the
[18:35:44] structure of the index page it contains
[18:35:45] only pointers to another page and the
[18:35:48] same thing for the second two pages. The
[18:35:51] SQL going to create another index page
[18:35:53] where it's going to says if you are
[18:35:54] searching for IDs between 11 and 15, you
[18:35:57] can find it at the third page 1 double
[18:36:00] point 10002. And for the last group
[18:36:02] between 16 and 20, we have another
[18:36:04] pointer to the last page to the page
[18:36:06] number
[18:36:08] one3. So as you can see inside those
[18:36:10] index pages, we have like a pointer for
[18:36:13] each group of ids for each cluster. So
[18:36:16] for the group of customers between 1 and
[18:36:18] five we have one pointer and for the
[18:36:21] second group between six and 10 we have
[18:36:22] another pointer. So that means we don't
[18:36:25] have here a pointer for each row. We
[18:36:28] have a pointer for each group for each
[18:36:30] cluster. That's why we call it clustered
[18:36:33] index. And now once SQL is done building
[18:36:35] the intermediate nodes, SQL going to go
[18:36:37] and build the last node, the root node
[18:36:40] where it says if you are searching for
[18:36:42] customers between 1 and 10, then go to
[18:36:46] the index page with the ID
[18:36:48] 1.200. So that means the route node here
[18:36:51] is pointing to another index page, not
[18:36:54] directly to the data page. And the same
[18:36:56] thing, we need another pointer for the
[18:36:59] second index page. So the customers
[18:37:01] between 11 and 20 go to the index page
[18:37:04] with the ID
[18:37:06] 1.201 and this is exactly what going to
[18:37:08] happen if you create a clustered index
[18:37:11] in SQL. First it going to go and
[18:37:14] physically sort all your data in the
[18:37:16] databases. So if it's from the first
[18:37:18] time sorted randomly SQL has to arrange
[18:37:22] everything and sort the data from the
[18:37:24] scratch. And then it's going to go and
[18:37:25] build this structure where you have in
[18:37:27] the root node and index page in the
[18:37:30] intermediate nodes the index pages but
[18:37:32] at the leaf level at the leaves we have
[18:37:36] the actual data the data pages. So now
[18:37:38] let's see what going to happen if you
[18:37:40] query the table where you search for the
[18:37:42] ID number 14. So it's going to check
[18:37:44] which pointer to use since 14 is in the
[18:37:47] group between 11 and 20. It's going to
[18:37:49] go and use the second pointer to the
[18:37:52] index page with the ID one double point
[18:37:54] 2011. And here the SQL going to open
[18:37:56] this index page and check the pointers.
[18:37:59] So since 14 is between 11 and 15 it
[18:38:03] going to go and use the pointer to the
[18:38:04] data page one point 102 and with that
[18:38:08] SQL located the correct data page the
[18:38:11] third page and now SQL going to open
[18:38:13] this data page and find the customer ID
[18:38:17] number 14. So as you can see it was very
[18:38:19] fast for SQL to locate the correct data
[18:38:21] page with only three jumps from the root
[18:38:24] node to the intermediate node. The SQL
[18:38:26] were able to find fast the correct data
[18:38:28] page. And here SQL needs only to read
[18:38:31] one data page instead of reading as we
[18:38:33] saw in the heap structure four different
[18:38:36] data pages. And of course you might say
[18:38:37] but still here we are reading like three
[18:38:39] pages. Well, reading an index page is
[18:38:42] very fast compared to the data page
[18:38:44] because reading a data page is always
[18:38:46] slower than reading an index page. So,
[18:38:49] as you can see, this P3 structure, the
[18:38:51] clustered index structure did help the
[18:38:53] SQL and the database to locate the right
[18:38:56] data in the right
[18:38:59] [Music]
[18:39:02] databases. And this is exactly how that
[18:39:05] clustered index works in the SQL
[18:39:07] database.
[18:39:12] All right. So now we're going to move to
[18:39:13] the second type and we're going to
[18:39:14] understand how exactly SQL build and
[18:39:17] create the nonclustered index. So let's
[18:39:20] go. So now we are back to the heap
[18:39:22] structure where our table don't have any
[18:39:24] index and our data are stored randomly
[18:39:26] inside the data pages. And now if you go
[18:39:28] and create a non-clustered index on the
[18:39:31] customer ID, what can happen? And here's
[18:39:33] the big difference that SQL will not
[18:39:36] touch or change anything on the physical
[18:39:40] actual data on the databases. So the
[18:39:42] database is going to stay as it is and
[18:39:45] nothing going to be changed and the SQL
[18:39:47] start immediately building the B
[18:39:50] structure. So it's going to start
[18:39:51] immediately building an index page and
[18:39:53] this index page is a little bit
[18:39:55] different than the one that we have
[18:39:56] learned previously. So since it's index
[18:39:58] page, it's going to store pointers. But
[18:40:00] this time SQL going to store in the key
[18:40:02] the customer ID. So one is the customer
[18:40:05] ID and now the value the pointer it will
[18:40:08] not be the data page ID. We will be more
[18:40:11] specific. So we're going to have like an
[18:40:12] address where exactly the row is stored.
[18:40:16] So it's going to start with the file ID,
[18:40:18] the page number because the customer ID
[18:40:21] one is stored in the page
[18:40:24] one2. But SQL gonna go add as well the
[18:40:28] offset number of the row where exactly
[18:40:30] in the page we can find this ID and the
[18:40:33] whole thing we can call it an air ID the
[18:40:36] row identifier. So now let's see quickly
[18:40:38] how the index page is pointing exactly
[18:40:41] to the row inside the data page. So the
[18:40:43] first part of the row identifier is
[18:40:46] mapping to the data page ID and then
[18:40:49] from the 96 it's going to take us to the
[18:40:51] offset and that's exactly the location
[18:40:54] of the row number one. So 96 is the part
[18:40:57] where we're going to start finding the
[18:40:59] row number one and that's going to takes
[18:41:01] us exactly to the place where we can
[18:41:04] read the information about the row ID
[18:41:07] number one. So this is how the index
[18:41:09] page is locating the exact place of the
[18:41:13] rows. So SQL going to go and continue
[18:41:15] and assign for each customer ID a
[18:41:18] pointer to the exact location. So as you
[18:41:21] can see now in the index page we don't
[18:41:22] have like a pointer for each group of
[18:41:25] customers like we have learned in the
[18:41:27] clusters index. We have now a pointer
[18:41:30] for each ID and this type of index page
[18:41:33] we call it roator page. So now SQL going
[18:41:36] to go and continue and map a pointer for
[18:41:39] each customer ID that we have inside our
[18:41:41] table. So we will have multiple index
[18:41:43] pages pointing to our data page. So as
[18:41:46] you can see we have a lot of pointers
[18:41:48] and the data inside the index page is of
[18:41:51] course sorted but inside the data pages
[18:41:54] it left as it is. And now those index
[18:41:57] pages that has the row identifier going
[18:42:00] to be stored at the leaf level of the B
[18:42:03] tree. So at the leaf level we don't have
[18:42:05] the actual data the data pages we have
[18:42:08] index pages where we have pointers then
[18:42:11] to the actual data and then it's going
[18:42:13] to go and start building the
[18:42:14] intermediate nodes. It's exactly like
[18:42:16] the clustered index where it's going to
[18:42:18] point to another index page. So between
[18:42:21] one and five customers it's going to be
[18:42:23] in the index page number 200. So the
[18:42:26] next step is going to go and build the
[18:42:27] intermediate nodes. It's going to be
[18:42:29] exactly like the clustered index.
[18:42:31] Nothing going to be changed. is like the
[18:42:33] same structure. So it is an index page
[18:42:35] pointing to another index page but this
[18:42:38] time for a group of customers and then
[18:42:40] we're going to have as well the root
[18:42:41] node. So again we call this structure as
[18:42:44] a B tree structure where they point to
[18:42:47] another databases but the databases are
[18:42:49] not part of the B tree. So now let's say
[18:42:52] if we are searching for the customer ID
[18:42:55] number 14, what's going to happen? It's
[18:42:57] going to start again from the root node
[18:42:59] and then it's going to find the pointer
[18:43:01] to the intermediate node and then jump
[18:43:02] to the next step to the intermediate
[18:43:04] node and then it's going to find the
[18:43:05] pointer to the index page between 11 and
[18:43:08] 15 and then it's going to go and scan
[18:43:11] this index page and find okay for the
[18:43:13] customer ID number 14 we have the
[18:43:15] following address. So it's going to go
[18:43:17] and locate the exact database and as
[18:43:20] well the exact place of the row. So it
[18:43:23] can go and jump immediately to the row
[18:43:25] without scanning anything else. So here
[18:43:27] this time with the nclustered index the
[18:43:30] SQL did read three different index
[18:43:32] pages. And finally the one data page in
[18:43:34] order to find the data. So if you
[18:43:37] compare to the clustered index you can
[18:43:38] see that we have here one extra layer
[18:43:41] one extra index page to be scanned in
[18:43:44] order to find the right place of the
[18:43:46] row. And this is how SQL creates the B
[18:43:48] tree for the nonclustered index and how
[18:43:51] it scans it in order to find the
[18:43:57] information. All right. So now when I
[18:43:59] think about the clustered index and the
[18:44:00] non-clustered index, I think about a
[18:44:03] book. You can think of the clustered
[18:44:05] index like the table of contents at the
[18:44:08] front of the table. So the table of
[18:44:10] contents kind of tells you where to find
[18:44:12] each chapter and the chapters are
[18:44:15] exactly sorted like the table of
[18:44:17] contents and this is exactly what the
[18:44:19] clustered index does. But now in the
[18:44:21] other hand think about the nclustered
[18:44:23] index as the index that you can find at
[18:44:26] the end of the book. The index of the
[18:44:28] book is a very detailed list of topics,
[18:44:32] terms and keywords where it points
[18:44:35] exactly to the location where you can
[18:44:37] find it in the book. And the content and
[18:44:39] the topic of the book is not sorted like
[18:44:42] the index of the book. And this is
[18:44:44] exactly what the noncluster index does.
[18:44:46] It is coexisting with the data. It is an
[18:44:49] extra list where it can point exactly
[18:44:51] where we can find the data inside our
[18:44:54] table. All right. Right. So now let's
[18:44:55] put those two indexes side by side to
[18:44:57] understand the differences between them.
[18:44:59] So the structure of the cluster the
[18:45:00] index is a B tree where it start with
[18:45:03] the root node where we have an index
[18:45:05] page. This index page is pointing to the
[18:45:08] intermediate nodes where we have as well
[18:45:10] index pages and those index pages are
[18:45:13] pointing to the actual data to the data
[18:45:15] pages. So at the leave level of the
[18:45:18] clustered index we have the data pages
[18:45:20] the actual data. What's special about
[18:45:22] the clustered index is that it
[18:45:24] physically sort the data inside those
[18:45:27] pages. So everything here is physically
[18:45:29] rearranged and sorted. Now if you are
[18:45:32] talking about the nclustlustered index
[18:45:34] we have as well a bit tree. So the same
[18:45:36] thing at the root node we have an index
[18:45:38] page pointing to an intermediate index
[18:45:41] page but this time the intermediate
[18:45:43] nodes are pointing to another index
[18:45:46] page. They are not pointing like the
[18:45:48] clustered index to a data page. they are
[18:45:50] pointing to index page. So now if you
[18:45:53] check this structure you can see that at
[18:45:54] the leaf level for the clustered index
[18:45:57] we have the actual data the data pages
[18:45:59] but on the other side at the leaf level
[18:46:01] for the nclustered index we don't have
[18:46:03] the actual data we have index pages but
[18:46:06] those index pages are pointing to the
[18:46:09] actual data to the data pages but the
[18:46:12] big difference of that the data pages
[18:46:14] are not part of the B3 the B3 of the
[18:46:17] nclustlustered index is just a separate
[18:46:19] structure that does not involve any
[18:46:22] data. So we have only index pages and it
[18:46:25] just points to the data pages without
[18:46:27] changing anything physically with your
[18:46:30] data. But in reality what happen is that
[18:46:33] you can have those two types of indexes
[18:46:35] the clustered and the nclustered indexes
[18:46:38] in one table. So one can happen the leaf
[18:46:41] level of the nclustered index going to
[18:46:44] be pointing to the data pages of the
[18:46:46] clustered index because those index
[18:46:48] pages don't care whether those pages are
[18:46:51] sorted or not. It's just going to go and
[18:46:54] point to the correct page and to the
[18:46:56] correct row. So that means we have now
[18:46:58] like two different B3 structures that
[18:47:01] are pointing to the data. And here there
[18:47:03] is like one thing that you have to
[18:47:04] understand that that you can create only
[18:47:08] one clustered index on a table. And this
[18:47:11] rule really makes sense because you can
[18:47:12] sort the data only in one way in SQL.
[18:47:15] And that's of course makes sense because
[18:47:17] you can sort the data physically only
[18:47:19] once. And that's why in SQL databases
[18:47:21] you are allowed to create only one
[18:47:24] clustered index because physically the
[18:47:26] data can be sorted only in one way. But
[18:47:29] in the other hand in the non-clustered
[18:47:30] index you can create as many
[18:47:33] nonclustered index you need. So you can
[18:47:35] create three four and all of them are
[18:47:37] pointing to the same data pages because
[18:47:40] in the B tree of the non-clustered index
[18:47:42] you don't store any data pages. We store
[18:47:44] only pointers to the data and you could
[18:47:46] have like multiple pointers. So this is
[18:47:48] the most important and the main
[18:47:50] difference between those two indexes.
[18:47:52] Now if you put it side by side, we have
[18:47:54] learned that the clustered index going
[18:47:56] to go and physically sorts and stores
[18:47:59] the rows at the B tree. But the
[18:48:01] nclustered index is going to go and
[18:48:03] create a separate p structure with
[18:48:06] pointers to the actual data. And by the
[18:48:08] way, the clustered index we call it the
[18:48:11] main index that we could use in each
[18:48:14] table. So the clustered index is the
[18:48:16] main one, the most important one that
[18:48:18] you can go and use in each table in your
[18:48:21] database. Now as we learned if you are
[18:48:22] talking about the number of indexes you
[18:48:25] can create maximum one index for each
[18:48:27] table but for the nclustered index there
[18:48:30] is no limitations you can go and create
[18:48:32] multiple indexes for each table. And now
[18:48:35] if you go and compare them about the
[18:48:37] read performance how fast we can get
[18:48:39] data using clustered index. Well it is
[18:48:42] faster than the nclustlustered index.
[18:48:44] And that's because in the nonclass and
[18:48:46] index we have this extra layer at the
[18:48:49] leaf node from the B tree and because of
[18:48:52] this having extra layer that means SQL
[18:48:54] has to do extra job in order to find the
[18:48:56] data that's why clustered index is
[18:48:59] faster than the nonclustered index but
[18:49:01] now in the other hand if we are talking
[18:49:03] about the right performance how fast we
[18:49:06] can insert data to the tables well
[18:49:09] writing data to a table with a clustered
[18:49:11] index is slower than the nclustered
[18:49:14] index. And that's because as you are
[18:49:16] inserting data to the table, SQL has
[18:49:18] always to check the databases is
[18:49:21] everything sorted correctly and if not
[18:49:23] SQL has to go and start physically
[18:49:26] sorting the data again in order to have
[18:49:28] the correct order. So there is a lot of
[18:49:30] stress in order to sort the data with
[18:49:32] the clustered index. But in the other
[18:49:34] hand in the non-clustered index we don't
[18:49:36] have this. So the physical data going to
[18:49:38] stay as it is. We are just creating nice
[18:49:41] new pointers. So if you are writing to a
[18:49:43] table where you have a clustered index,
[18:49:45] it's going to be slower than writing to
[18:49:47] a table where you have nclustered index.
[18:49:49] And of course the fastest way to write
[18:49:52] data to a table is to not have indexes
[18:49:55] at all. So a heap structure. So SQL just
[18:49:58] go and start inserting data inside those
[18:50:01] databases without creating any extra
[18:50:03] structures. So as you can see it's like
[18:50:05] always a tradeoff. You can read fast but
[18:50:08] you're going to write slower. So you
[18:50:10] cannot have like everything. Now we are
[18:50:12] talking about the storage efficiency.
[18:50:14] The clustered index going to be better
[18:50:16] with the storage than the nonclustered
[18:50:18] index and that's because of the same
[18:50:20] reason with the nonstructured index. We
[18:50:22] have this extra layer of index pages and
[18:50:25] index pages needs storage and that's why
[18:50:27] they can waste more storage than the
[18:50:29] clustered index. Now if you're talking
[18:50:31] about the use cases when to use
[18:50:33] clustered index. Well, if you have like
[18:50:35] a column this column has to have few
[18:50:37] criteria in order to be good candidate
[18:50:39] for the clustered index. First, it's
[18:50:41] going to be good if the values inside
[18:50:43] the columns are unique. And second, and
[18:50:45] it is way more important than that, the
[18:50:48] values of this column should not change
[18:50:50] a lot because if this column having a
[18:50:53] lot of update operators and the data is
[18:50:55] keep changing, that means each time SQL
[18:50:58] going to go and start sorting the data
[18:51:00] again left and right. So having a column
[18:51:02] that is frequently changing, it's not
[18:51:05] good for clustered index. And that's why
[18:51:08] the primary keys of tables are a perfect
[18:51:11] candidate because first they are unique
[18:51:13] and second we will never go and update a
[18:51:15] primary key value. We always append a
[18:51:18] new primary key value and that's why
[18:51:20] primary keys are perfect for clustered
[18:51:23] index. And one more thing where I go and
[18:51:25] use clustered index is that to optimize
[18:51:27] the performance of a range query. If you
[18:51:30] are quering the data between one value
[18:51:32] and another one clusters index works
[18:51:34] really well. Now in the other hand if we
[18:51:36] are talking about the non-clustered
[18:51:37] index we could use it on coms that are
[18:51:40] used in the search conditions or if you
[18:51:42] are joining tables without using the
[18:51:44] primary keys then you can go and apply
[18:51:46] the nclustered index in order to have
[18:51:48] faster joins or you can go and use it to
[18:51:50] optimize the performance if you are
[18:51:52] searching for an exact value exact
[18:51:55] match. So those are the main and
[18:51:57] important differences between the
[18:51:59] clustered and the nclustered indexes.
[18:52:05] All right. So now before we go to SQL
[18:52:07] and start practicing, I would like to
[18:52:09] show you the syntax of the index. So
[18:52:12] it's very very simple. It start with
[18:52:14] create and then we can define whether it
[18:52:17] is clustered or nonclustered and then
[18:52:19] the keyword index. But this section is
[18:52:22] optional. So if you don't define
[18:52:24] anything, the default going to be the
[18:52:26] nonclustered. So if you say create index
[18:52:29] the SQL server going to go and create
[18:52:31] nclustered index. Then after that we
[18:52:33] have to go and define the name of the
[18:52:35] index and then we have to tell SQL which
[18:52:38] table we have to create the index in on
[18:52:40] table name and then we can go and define
[18:52:44] one column or multiple columns for the
[18:52:46] index and we call an index with multiple
[18:52:49] columns as composite index. So for
[18:52:51] example we can go and create a clustered
[18:52:53] index using this command create
[18:52:56] clustered index the index name and then
[18:52:58] we specify the table and the ID. So we
[18:53:01] are saying create clustered index based
[18:53:04] on this column the ID from the table
[18:53:06] customers. And if you want to create a
[18:53:08] nclustered index you say create
[18:53:10] nclustered index and the same thing. So
[18:53:12] so far we are using one column in the
[18:53:14] index but we can go and create a
[18:53:16] composite index with multiple columns
[18:53:18] like the following example. So we can
[18:53:20] say create an index and as you can see
[18:53:22] we skipped here defining the type and
[18:53:25] that's because the default going to be
[18:53:27] nonclustered index. And now here we are
[18:53:29] specifying two columns the last name and
[18:53:31] the first name. And as you can see we
[18:53:33] specifying as well for SQL how to sort
[18:53:36] the data. So we are saying last name
[18:53:38] should be sorted inside the data page
[18:53:41] ascending lowest to the highest but the
[18:53:44] first name should be the way around from
[18:53:46] the highest to the lowest. So you can
[18:53:48] control how the data going to be sorted
[18:53:50] physically in the data page. So as you
[18:53:52] can see it is very simple. This is the
[18:53:54] syntax for creating index in SQL. All
[18:53:57] right. So back to SQL and the first
[18:53:59] question is where do we find indexes in
[18:54:01] the database? Well you can go and
[18:54:03] explore it. If you go to the object
[18:54:05] explorer over here and check any tables
[18:54:06] from our sales DB for example the
[18:54:08] customers and here you have a folder
[18:54:10] called indexes. So if you expand it you
[18:54:13] will find here an index. I didn't create
[18:54:16] any of those indexes in the database.
[18:54:18] But in SQL server, if you define any of
[18:54:20] the columns as a primary key, the SQL
[18:54:23] server going to go by default creating a
[18:54:26] clustered index for the primary key
[18:54:29] because it makes always sense to create
[18:54:30] a clustered index on the primary key. So
[18:54:33] this one is created as a default and as
[18:54:35] you can see at the start we have like a
[18:54:36] key primary key customer and then it is
[18:54:39] clustered. Now I would like to start
[18:54:40] from the scratch. That's why I would
[18:54:42] like to go and create a new table
[18:54:44] without any indexes. So what we're going
[18:54:46] to do, we're going to go and load the
[18:54:48] table customers into a new table. So how
[18:54:50] we going to do that? We're going to go
[18:54:51] and say select star from sales
[18:54:56] customers and before the from we're
[18:54:58] going to say into a new table. So it's
[18:55:01] going to be TB customers. So like this.
[18:55:05] Let's go ahead and execute it. So now if
[18:55:07] you go to the left side and refresh the
[18:55:09] tables you can find we have now a new
[18:55:11] table called DB customers. Now let's go
[18:55:14] and check whether we have any indexes
[18:55:15] inside it. So indexes it is empty. So we
[18:55:19] don't have anything no clustered index
[18:55:21] or anything else. And this table has the
[18:55:23] structure of heap structure. So the data
[18:55:27] are inserted there randomly. It is not
[18:55:29] sorted. And if I go over here and for
[18:55:31] example, let's say I'm going to select
[18:55:34] from this new
[18:55:35] table where customer ID equal one and I
[18:55:40] execute it. The SQL server did a full
[18:55:43] scan on the table in order to find this
[18:55:46] customer ID. So our new table DB
[18:55:48] customers is heap cluster. But let's go
[18:55:52] and change that. What we're going to do,
[18:55:53] we're going to go and create a new
[18:55:55] clustered index. So we're going to say
[18:55:57] create
[18:55:59] clustered index and then we're going to
[18:56:01] go and give it a name for the index. We
[18:56:04] usually follow the following index. So
[18:56:06] we have index as prefix and then after
[18:56:08] that we specify the table name. So DB
[18:56:11] customers and then the key for the
[18:56:14] index. So the column that we are using
[18:56:16] in order to index the table. This is
[18:56:18] important to stick with the same naming
[18:56:20] convention for the index name because
[18:56:22] later as you are monitoring your
[18:56:24] indexes, it's going to be really easy to
[18:56:25] understand. Okay, this index is for the
[18:56:28] table DB customers and we are using the
[18:56:30] customer ID to index. So now after that
[18:56:33] we're going to go specify on which table
[18:56:35] we are doing the index. So on sales DB
[18:56:40] customers and then we're going to
[18:56:42] specify the column name. So we are
[18:56:44] saying build for me a clustered index
[18:56:46] based on the customer ID. So now let's
[18:56:49] go and execute it. So as you can see
[18:56:51] it's very fast because we have only five
[18:56:53] rows. So the database just switched all
[18:56:55] the data pages very fast. Now let's go
[18:56:58] and check our new index. So let's go and
[18:57:00] refresh and let's go inside it. And now
[18:57:02] we can see that we have our new index
[18:57:04] clustered index based on the customer
[18:57:06] ID. Now as we learned we cannot create
[18:57:08] multiple clustered index. But let's go
[18:57:10] and test that. So I will just take the
[18:57:13] whole thing and let's say I would like
[18:57:15] to create a class index based on the
[18:57:17] first
[18:57:18] name as well
[18:57:22] here. So let's go and execute it. So as
[18:57:25] you can see saying you cannot create
[18:57:28] more than one clustered index on this
[18:57:30] table. That means we can create only one
[18:57:32] clustered index. And let's say that
[18:57:35] after you created the index you chose
[18:57:37] the wrong column and you would like to
[18:57:39] change it to the first name. So what
[18:57:42] we're going to do, we have to go and
[18:57:43] drop the index. So we say drop index and
[18:57:48] then you need the index name. It was
[18:57:50] this one. And then you have to specify
[18:57:52] which table. So it's going to be sales
[18:57:55] DB
[18:57:57] customers like this. So if I do it like
[18:58:01] this and let's go and refresh again. You
[18:58:03] can see that we don't have any indexes
[18:58:05] anymore and the table is packed as a hip
[18:58:08] structure. And now you can go and create
[18:58:10] the correct clustered index for this
[18:58:12] table. But to be honest, I'm going to
[18:58:14] stick with the customer ID. So I will
[18:58:16] not create a clustered index on the
[18:58:18] first name because the first name of
[18:58:19] course is not unique. You can have like
[18:58:21] maybe multiple customers having the same
[18:58:23] name. And as well updates could happen
[18:58:26] on the first name and that's going to be
[18:58:27] very expensive. So that means I'm going
[18:58:29] to stick with my index on the customer
[18:58:32] ID. Let's go and execute it. And now I
[18:58:34] have again my index on my table. Now
[18:58:38] let's say that that I have the following
[18:58:40] select statements from our tables. So
[18:58:44] customers and I'm searching for the last
[18:58:48] name where let's say we are searching
[18:58:50] for brown. So let's go and execute it.
[18:58:53] So let's say that we are getting more
[18:58:54] and more customers and our table is
[18:58:56] getting bigger and I frequently use this
[18:58:59] query. So I'm searching for specific
[18:59:01] customers using the last name. So what
[18:59:04] we can do, we can go and create a
[18:59:06] nonclustered index for the last name in
[18:59:08] order to improve the performance of this
[18:59:10] query. So let's go and create that. So
[18:59:12] we're going to say create
[18:59:14] nonclustered index. And now we're going
[18:59:17] to give it the name using the naming
[18:59:18] convention. So DB customers and we're
[18:59:22] going to use the last name for this
[18:59:23] index. So on
[18:59:26] sales DB customers and we will use the
[18:59:30] column last name for the index. So let's
[18:59:33] go and execute it. And now if you go to
[18:59:36] our indexes and refresh, we will find
[18:59:39] our new index over here. And as you can
[18:59:42] see, it says it is nonclustered and as
[18:59:45] well non-unique. We will talk about the
[18:59:47] uniqueness later. So as you can see,
[18:59:48] it's very easy. We have just created a
[18:59:50] uncclustered index on the last name. And
[18:59:52] now as we learned, we can go and create
[18:59:54] multiple nonclustered index on the same
[18:59:57] table. Let's say for example, now we our
[19:00:00] query looks like this. We are searching
[19:00:01] for the first name using for example the
[19:00:04] value Anna. And now this query happens a
[19:00:07] lot and maybe slow. So we can go and
[19:00:09] create new nonclustered index. So let me
[19:00:12] just have it like this. And for the
[19:00:14] nonclustered index you don't have to
[19:00:16] specify always like nonclustered index.
[19:00:19] As default it's going to be
[19:00:20] nonclustered. So we can skip that. And
[19:00:23] here let's call it first name. And the
[19:00:26] column that we are using is the first
[19:00:29] name. So let's go and create this index.
[19:00:33] And now let's go and refresh our
[19:00:35] indexes. And as you can see, SQL did
[19:00:37] create a nonclustered index for the
[19:00:40] first name. So if you don't specify the
[19:00:42] type of the index, it's going to be as a
[19:00:44] default nonclustered
[19:00:49] index. All right. So now let's talk
[19:00:51] about the composite index. It is an
[19:00:53] index that has multiple columns inside
[19:00:56] the same index. So far we have used only
[19:00:59] one column in the index but we can go
[19:01:02] and specify multiple columns and that's
[19:01:04] because sometimes our wear conditions
[19:01:07] are complicated and based on multiple
[19:01:09] columns. So for example let's say that
[19:01:11] we are searching for country equal to
[19:01:15] USA and at the same time we are saying
[19:01:18] the score should be higher than 500. So
[19:01:21] that means in this condition we are
[19:01:23] using two columns and we would like to
[19:01:25] speed up this query. So how we going to
[19:01:27] do it? So we're going to go and create
[19:01:29] let's say an index and give it a name DB
[19:01:33] customers and let's say country score on
[19:01:39] sales DB customers. And now it is very
[19:01:43] important to do the following thing. Now
[19:01:45] we have to go and define a list of
[19:01:48] columns that we want to be included in
[19:01:50] this index. And it is very crucial and
[19:01:53] important that you get the same order as
[19:01:56] your query. So your query start with the
[19:01:59] country and then the score. You have to
[19:02:02] do it the same thing in the index. So
[19:02:04] the first column it's going to be the
[19:02:06] country and then the score. So it must
[19:02:09] be the same order as your query. So
[19:02:12] let's go and create this index. And if
[19:02:15] you go to the indexes over here, you can
[19:02:16] see that we have created our new index.
[19:02:19] So now once you create such a index and
[19:02:22] your table going to be like always
[19:02:24] updating this index you have to be
[19:02:25] committed and responsible. So in your
[19:02:28] queries if you want to filter the data
[19:02:29] using country and score always start
[19:02:32] with the country then the score in order
[19:02:34] to be able to use the index optimizer.
[19:02:38] So if you do it like this the index
[19:02:40] going to be working but if you go and
[19:02:42] query the way around. So you start with
[19:02:45] the score and then the country the SQL
[19:02:48] will not be using your index. So either
[19:02:51] you adjust your queries or you have to
[19:02:54] go and recreate the index based on this
[19:02:57] switch. So be very careful with the
[19:02:59] composite indexes. The order is very
[19:03:02] crucial. So you're going to have it
[19:03:04] exactly like the query. And now you
[19:03:07] might say you know what now we have like
[19:03:08] a nice index for those two columns. What
[19:03:11] going to happen if I go and use in my
[19:03:14] query only one of them like for example
[19:03:16] the country. So now the question is if I
[19:03:18] go and execute this query is the SQL is
[19:03:22] using this index even though that I
[19:03:24] don't have the score. Well yes because
[19:03:27] it follows the leftmost prefix rule. So
[19:03:30] this means SQL can use the index if you
[19:03:33] are using always the lift columns. So
[19:03:36] here in our index country is on the left
[19:03:39] that's why it is working over here. But
[19:03:42] if you go and skip the lift column it
[19:03:44] will not work. So if you go over here
[19:03:46] for example and say let's go and select
[19:03:49] only the
[19:03:51] score and it is like higher than 500.
[19:03:55] What we have done, we have skipped the
[19:03:57] country in this query and that's why it
[19:04:00] will not be working. So as long as you
[19:04:02] are including the left columns, it will
[19:04:05] work even though it is only one column.
[19:04:07] So in this scenario, the first query
[19:04:09] going to use the index, the second one
[19:04:12] will not be using it. So now let me give
[19:04:13] you a very simple example in order to
[19:04:15] understand how this works. So let's say
[19:04:17] that we have an index using four columns
[19:04:20] A, B, C, D. Now in your query if you go
[19:04:22] and target the column A the index going
[19:04:24] to be used. Now the same thing going to
[19:04:26] happen if you go and use A and P. So if
[19:04:29] you're using those two columns you will
[19:04:31] be using the index. So those are where
[19:04:34] the index will be used. So now let's
[19:04:38] have the scenarios where the index wants
[19:04:42] be used. So for example if you go and
[19:04:44] just jump immediately to the column B.
[19:04:47] So you are not using the left column the
[19:04:49] A that's why you will not be using the
[19:04:52] index and as well in your query if you
[19:04:54] are using A and you are skipping the P.
[19:04:56] So you have A and then C you will not be
[19:05:00] using the index. So you have always to
[19:05:02] use always the lift columns. So here if
[19:05:05] you are using A B C you will be using
[19:05:08] the index. And let's see here you are
[19:05:09] using A B and then you jump and skip to
[19:05:12] the D you will not be using the index.
[19:05:14] So this is what we mean with the
[19:05:15] leftmost prefix rule by using the
[19:05:18] composite index. So if you're using
[19:05:20] multiple columns inside one index, be
[19:05:22] careful with the order of the columns
[19:05:24] that you are defining. All right. So
[19:05:26] that's all for this category, clustered
[19:05:28] and uncclustered index. Now we're going
[19:05:30] to move to the second category where we
[19:05:32] talk about the indexes by the storage,
[19:05:35] the row store and the column store.
[19:05:41] So now let's say that we have a table we
[19:05:43] have multiple rows and multiple columns.
[19:05:45] Now if we use a row store index this is
[19:05:48] the classical one. What going to happen?
[19:05:50] Our table going to be splitted into
[19:05:52] multiple rows. And as we learned each
[19:05:54] group of rows going to be stored inside
[19:05:56] a data page. So that means we are
[19:05:58] organizing the data row by row which
[19:06:01] means all the columns for each row going
[19:06:04] to be stored together. This is the
[19:06:06] traditional way on how the databases
[19:06:09] organize their data where the
[19:06:11] informations are stored row by row. But
[19:06:13] now in the other side if you use column
[19:06:15] store index the SQL going to go and
[19:06:17] split your table into multiple separate
[19:06:19] columns and then SQL going to go and
[19:06:21] store the values of one column together
[19:06:24] in data page. So that means if you go
[19:06:26] and open a data page you will find only
[19:06:29] the values of one column. You will not
[19:06:31] find the entire row. So if it's like the
[19:06:34] first name you will see only the first
[19:06:36] name informations you will not see the
[19:06:37] last name information in this data page.
[19:06:39] So if you compare them the row store
[19:06:41] index stores the data row by row the
[19:06:44] column store index stores the data
[19:06:46] column by column. So this is a very high
[19:06:48] level representation on how the column
[19:06:50] store index is stored. As you know me we
[19:06:53] go in details in order to understand
[19:06:55] exactly how SQL works with the column
[19:06:58] store index. So let's go.
[19:07:03] All right. So now let's say that we have
[19:07:05] a table for the customers. We have three
[19:07:07] columns ID, name and status. And as well
[19:07:10] we have around 2 million rows, 2 million
[19:07:12] customers. And as we learned as a
[19:07:14] default, the table going to be built as
[19:07:16] a heap structure where the rows are
[19:07:18] stored row by row inside data pages. But
[19:07:20] now we go and create a column store
[19:07:23] index on top of this table. So now once
[19:07:25] you do that SQL going to go through a
[19:07:28] process in order to build the column
[19:07:30] store. So the first step is SQL going to
[19:07:32] go and divide the data the rows into row
[19:07:35] groups. Now in SQL server each row group
[19:07:38] can contain around like 1 million row.
[19:07:40] So in this example our table going to be
[19:07:42] splitted into two row groups. The first
[19:07:45] one million row in one group and the
[19:07:47] second one in another row group. Now you
[19:07:49] might ask me we are talking about
[19:07:50] columns. Why we are splitting the rows?
[19:07:53] Well, this is just a pre-step in order
[19:07:55] just to optimize the performance and to
[19:07:57] do parallel processing. And of course,
[19:07:59] the data will not be stored like this
[19:08:01] because we have the second step. Now, in
[19:08:03] the next step, SQL going to go and
[19:08:05] segment the columns. So now, SQL will go
[19:08:08] for each row group and start splitting
[19:08:10] the data by the columns. And that's why
[19:08:13] we call it a column store because we are
[19:08:15] separating the columns from each others.
[19:08:17] So that means we have one segment for
[19:08:19] the ID, another one for the name and a
[19:08:22] third one for the status. And this can
[19:08:24] happen for each row group. And now it's
[19:08:26] going to move to the third step in this
[19:08:28] process. We have the data compression.
[19:08:31] And this is the most important step in
[19:08:33] this process because it is the reason
[19:08:36] why column store is very fast compared
[19:08:38] to the ro store. So in this process
[19:08:41] there are like different techniques on
[19:08:42] how to do data compression and the most
[19:08:45] famous one is that it's going to go and
[19:08:47] create like a dictionary. Let's take for
[19:08:49] example the column status the status of
[19:08:52] the customer whether it is active or
[19:08:54] inactive. So the word active and
[19:08:56] inactive going to be repeated like 2
[19:08:59] million times because we have 2 million
[19:09:01] customers and since it is like string it
[19:09:03] is like taking a lot of space and
[19:09:05] storage. But now instead of that we're
[19:09:07] going to go and compress the data. So
[19:09:09] first it's going to go and create a
[19:09:10] dictionary by replacing the value active
[19:09:14] and inactive into smaller values like
[19:09:16] one and two. So we have like a mapping
[19:09:19] between the long value to a small value.
[19:09:22] And after that SQL going to store like a
[19:09:25] data stream where we have like only two
[19:09:27] values one two one two. So we're going
[19:09:29] to have like a big stream of 2 million
[19:09:31] rows. So it's going to go and do this
[19:09:33] for each column and with that the size
[19:09:35] of each column going to be changed
[19:09:37] depends of course on how much different
[19:09:39] values you have in each column. So this
[19:09:41] step is very important in order to
[19:09:43] reduce the size of the data and as well
[19:09:45] to increase the performance. So now once
[19:09:48] everything is organized and compressed,
[19:09:50] SQL going to go and start storing the
[19:09:53] results in databases. But TSQL will not
[19:09:56] use the standard databases that we have
[19:09:58] learned previously. But instead going to
[19:10:00] use a special database called LOB large
[19:10:04] object page. So now let's quickly
[19:10:06] compare the structure of the normal
[19:10:07] database that we have learned in the row
[19:10:09] store with the new one, the column
[19:10:11] store, the LOB data page. So as usual
[19:10:14] each page has a header. This is same as
[19:10:16] any data page. But the next section is
[19:10:18] going to be the segment header. It has
[19:10:20] like metadata informations about the
[19:10:23] column segment that is stored in this
[19:10:25] page. Like we have the segment ID, the
[19:10:28] row group ID, the column ID and it has
[19:10:31] as well very important information the
[19:10:33] ID to the dictionary page. So the
[19:10:35] dictionary page is as well a type of
[19:10:38] pages in SQL. It has as well a header
[19:10:41] but inside it we have like a mapping. So
[19:10:43] it maps the original value, the long
[19:10:45] one, the inactive to the smaller version
[19:10:48] of this value, for example, one. And
[19:10:50] that's all for the dictionary page. It
[19:10:52] has the mapping between the original
[19:10:54] values and the smaller values. And
[19:10:56] beneath the segment header, we can have
[19:10:58] now the important place where our data
[19:11:00] can be stored. We have the data stream.
[19:11:03] So it is like sequence of ids from the
[19:11:05] dictionary that represents the values of
[19:11:08] the columns side by side. And of course,
[19:11:11] we cannot fit the whole 1 million rows
[19:11:13] inside this data stream. We're going to
[19:11:15] have like multiple LOP databases. So
[19:11:18] this is how exactly the SQL stores your
[19:11:21] data. If you decided to go with the
[19:11:23] column store, so let's go back to the
[19:11:25] process. So back to the process. As you
[19:11:27] can see, SQL is storing the data as LO
[19:11:30] data storage. So this is the last step
[19:11:32] and with that SQL did convert your table
[19:11:35] into a column store. So now we cannot
[19:11:37] just create a column store without
[19:11:39] defining whether it is clustered index
[19:11:42] or non-clustered index. So let's start
[19:11:44] with the first one the clustered column
[19:11:46] store index. So if you create such a
[19:11:48] index SQL of course will not be building
[19:11:51] a B3 structure. SQL going to use exactly
[19:11:54] this structure the column store
[19:11:56] structure. So as we learned the cluster
[19:11:58] index is a complete makeover of your
[19:12:00] table. when you apply it then SQL going
[19:12:02] to format everything column-wise and it
[19:12:05] is fully replacing the old row based
[19:12:08] table structure that we have at the
[19:12:09] start. So once you apply the clustered
[19:12:12] column store index it will not leave
[19:12:14] anything behind and your table going to
[19:12:16] be completely structured as a column
[19:12:18] store and one more thing which is makes
[19:12:20] sense of course all the columns from the
[19:12:23] original table going to be converted to
[19:12:24] a column store. So it is not leaving
[19:12:27] anything behind it. But in the other
[19:12:29] hand, if you are using non-clustered
[19:12:31] column store index, as we learned, it is
[19:12:34] like a companion to your existing table.
[19:12:37] So it coexist with the table and it will
[19:12:39] not replace anything. So the column
[19:12:42] store index can be an additional thing
[19:12:44] that is stored beside your table. So
[19:12:47] that means the original table will not
[19:12:49] be deleted at all like the clustered
[19:12:51] column store index. The first one is in
[19:12:54] the old row based storage. the regular
[19:12:56] table, the first one, and your data
[19:12:58] going to be as well stored in a separate
[19:13:01] structure in the column store index. And
[19:13:03] of course, in the non-clustered column
[19:13:05] store index, since we are creating an
[19:13:08] extra index outside of your original
[19:13:10] table, you can go and define which
[19:13:12] column should be included in this
[19:13:13] process. It must not be all the columns.
[19:13:16] You can go for example with only the
[19:13:17] status. So that means you build a column
[19:13:20] store index only for one column for the
[19:13:22] status of the customers. So this is what
[19:13:24] we mean with the clustered column store
[19:13:26] index and the nclustered column store
[19:13:32] index. All right friends, so now you
[19:13:35] might ask me why we are doing all those
[19:13:36] stuff. Why I would split my data by the
[19:13:39] columns? Well, it's all because of
[19:13:42] analytics. Because in analytics we have
[19:13:44] like big complex query where we have a
[19:13:46] lot of data aggregations and stuff on
[19:13:48] big tables. And the roster index is
[19:13:51] perfectly designed in order to improve
[19:13:54] the performance of such big queries. And
[19:13:56] that's why SQL databases like SQL server
[19:13:59] and as well BI tools like Tableau and
[19:14:01] PowerBI did adopt this methods in order
[19:14:05] to offer fast platform for data
[19:14:07] analyzes. So now let's understand
[19:14:09] exactly why the column store index is
[19:14:12] way faster for data analyzes than the
[19:14:14] row store index. So let's go. So again
[19:14:16] we have the customers tables and let's
[19:14:18] say we have like five customers where we
[19:14:20] have ID, name and status and as we
[19:14:22] learned before if we are using roster
[19:14:24] index the data can be stored in multiple
[19:14:26] databases and in each database we're
[19:14:28] going to have the whole record the whole
[19:14:30] information about one customer. So for
[19:14:33] this example we're going to have like
[19:14:34] three databases but if you are using the
[19:14:36] column store index it's going to be
[19:14:38] stored little bit differently. So the
[19:14:40] first column the id going to be stored
[19:14:41] in one data page and here the SQL will
[19:14:43] not go and build a dictionary because
[19:14:45] the ids are already short. So we're
[19:14:47] going to have like one data stream with
[19:14:49] all ids and now for the next column name
[19:14:51] is going to be stored in separate data
[19:14:53] page where we're going to have an extra
[19:14:55] dictionary page where each name going to
[19:14:57] be mapped to one small value. So the
[19:14:59] data going to be compressed and we're
[19:15:00] going to save storage. Now the database
[19:15:02] going to create for the third column the
[19:15:04] status one more data page and the
[19:15:06] dictionary here going to be very small.
[19:15:08] So for active we're going to have one
[19:15:09] and for the inactive we're going to have
[19:15:11] two and in the data stream we will be
[19:15:13] storing only the ids of the dictionary.
[19:15:15] So now let's understand why the column
[19:15:17] store is faster. Let's have the
[19:15:19] following query. We want to find the
[19:15:21] total number of customers that are
[19:15:23] active. So we have the query select
[19:15:26] count star from customers and we're
[19:15:28] going to filter the data by the status
[19:15:30] where it is equal to active. So now if
[19:15:32] we query the table with the row store
[19:15:34] what can happen? SQL have first to go
[19:15:36] and collect the data. So it's going to
[19:15:38] go to the first data page and collect
[19:15:39] the first two customer then to the
[19:15:41] second to the third and so on. And as
[19:15:43] you can see SQL here is reading
[19:15:45] everything the whole row the ID the name
[19:15:48] the status even though that for the
[19:15:50] query we actually we don't need all
[19:15:52] those informations we just need to count
[19:15:54] how many customers we need with the
[19:15:56] status active but still cannot go and
[19:15:59] selectively only reading the status has
[19:16:03] to read the whole record. So after SQL
[19:16:05] has all the data it's going to go and
[19:16:07] filter the data. So it's going to go and
[19:16:08] remove the inactive rows and then SQL
[19:16:11] going to do the aggregate operation and
[19:16:13] with that we're going to get three rows.
[19:16:16] So that's why the total count of active
[19:16:18] customers going to be three. But now
[19:16:19] let's see how SQL going to query the
[19:16:22] column store. So SQL first have to
[19:16:23] analyze okay which columns do I need
[19:16:26] actually for this query. Well, we need
[19:16:28] only the status. So SQL will not go and
[19:16:31] open all three data pages and read it.
[19:16:33] SQL will target only one data page the
[19:16:36] database where we have the column
[19:16:37] status. So it's going to take this very
[19:16:39] simple data stream and then it's going
[19:16:41] to go and understand the dictionary and
[19:16:43] it going to go and remove all the values
[19:16:45] where it is equal to two. So without in
[19:16:47] the output we have only three values and
[19:16:49] SQL going to go and do a very quick
[19:16:51] count for those values. So in the output
[19:16:53] we will get as well three total number
[19:16:55] of active customers. So now if you
[19:16:57] compare this intermediate result sets
[19:17:00] from the row store and the column store
[19:17:02] you can see that in the row store we
[19:17:04] have fetched and retrieved a lot of
[19:17:06] unnecessary informations for this query
[19:17:08] and this of course going to make the
[19:17:10] speed of the query very slow but in the
[19:17:13] column store reads exactly what it needs
[19:17:15] for this aggregation and we didn't read
[19:17:18] any extra informations about the names
[19:17:20] of the customers the ids it didn't like
[19:17:23] open any extra data pages it exactly
[19:17:25] gets the data that it needs for the
[19:17:28] aggregation and that's exactly why the
[19:17:30] performance of queries where we have
[19:17:32] aggregations and data analyzes is going
[19:17:35] to be very fast if you are using column
[19:17:37] store compared to the row store. So
[19:17:40] that's why we use column store for big
[19:17:42] data and data analytics. All right. So
[19:17:44] now let's summarize the differences
[19:17:46] between the row store and the column
[19:17:47] store indexes side by side. So let's
[19:17:50] start by the definition. The row store
[19:17:52] going to go and organize and store the
[19:17:53] data row by row. It is really nice
[19:17:55] method if you need a lot of columns in
[19:17:57] one row. But in the other hand, the
[19:17:59] column store index going to go and store
[19:18:01] the data and organize it column by
[19:18:03] column which is really great if you're
[19:18:05] focusing on specific column. Now if you
[19:18:07] are talking about the storage
[19:18:08] efficiency, the row store index going to
[19:18:10] take more space compared to the column
[19:18:13] store index and that's because as we
[19:18:14] learned the column store going to go and
[19:18:16] compress the data which going to save a
[19:18:18] lot of storage if you have large tables.
[19:18:20] Now to the next point which is more
[19:18:22] important about the performance. The
[19:18:24] read and write optimizations we can say
[19:18:27] for the row store things are more
[19:18:28] balanced. So you will get a decent speed
[19:18:31] for both write and read operations but
[19:18:33] things in the column store is different.
[19:18:36] It is fast for reading especially if you
[19:18:38] are doing data analytics but writing
[19:18:40] data like inserting and updating it is
[19:18:43] slower because as we learned there are
[19:18:45] like multiple steps until the data is
[19:18:48] written in the pages. So in one hand you
[19:18:50] are optimizing the speed of your
[19:18:52] analytical queries but in the other hand
[19:18:55] changing data it is slower than the
[19:18:57] roster index. Now let's talk about the
[19:18:59] next point input and output efficiency.
[19:19:01] Well the roster index it's not really
[19:19:03] good because you are retrieving a lot of
[19:19:05] columns. So a lot of data should be read
[19:19:08] from the disk storage in order to answer
[19:19:10] your queries. But in the other hand for
[19:19:12] the column store it is lower and that's
[19:19:15] because it targets exactly the data and
[19:19:18] columns that is needed for the query. So
[19:19:20] there will be generally less data that
[19:19:22] is read from the disk storage and of
[19:19:24] course that's why we are getting fast
[19:19:26] read performance. So now if you are
[19:19:28] thinking which systems are best for ro
[19:19:30] store index well the roster index is
[19:19:32] very suitable for the OLTB systems
[19:19:35] online transactional systems like
[19:19:37] banking and commerce systems where the
[19:19:40] full records are accessed very
[19:19:42] frequently but in the other hand the
[19:19:44] column store index is great for OLAP.
[19:19:46] All app systems are online analytical
[19:19:49] processing where you have like data
[19:19:51] warehouses, data league, business
[19:19:52] intelligence. You are building reports
[19:19:54] and analyzes. You have large data sets
[19:19:57] and very complicated aggregated queries.
[19:20:00] So if you have such a project then the
[19:20:01] column store index is the way to go. So
[19:20:04] that means the use case for the row
[19:20:06] store index if you have high frequency
[19:20:08] transactions where the system has to
[19:20:10] quickly access records and the use case
[19:20:13] for the column store is big data
[19:20:15] analytics where the SQL has to scan
[19:20:17] large data sets. So those are the main
[19:20:19] differences between the row store index
[19:20:22] and the column store
[19:20:27] index. All right. So now let's check the
[19:20:29] syntax of the column store index. Well,
[19:20:31] it is really easy what we're going to
[19:20:32] do. we can just put a column store
[19:20:34] keyword between the clustered or
[19:20:37] nonclustered and the index. So once you
[19:20:40] specify that then you are telling SQL
[19:20:42] you want to create a column store index
[19:20:44] and the rest is going to stay as it is.
[19:20:46] Now if you want to create row column
[19:20:48] store then you don't have to specify
[19:20:50] anything. There is no keyword for the
[19:20:52] row store. So as we learned before we
[19:20:54] can go and create a nonclustered index
[19:20:56] and cluster on the index and both of
[19:20:57] those syntax is going to tell SQL we are
[19:21:00] creating row store index but if you go
[19:21:03] and use the column store keyword then
[19:21:06] you are telling SQL that you want to
[19:21:07] create either clustered or nclustered
[19:21:10] column store index and here there is
[19:21:11] like a syntax rule if you are creating a
[19:21:14] clustered column index then you must not
[19:21:17] specify anything for the columns. So you
[19:21:19] cannot go and specify anything like an
[19:21:21] ID or country or any columns over here
[19:21:24] because it makes no sense once you say
[19:21:26] cluster column store then all the
[19:21:28] columns going to be included in the new
[19:21:31] structure. So this is the syntax of the
[19:21:34] column store index. All right. So back
[19:21:36] to scale let's check how we can create
[19:21:37] column store index. Now if you check our
[19:21:39] table here DB customers that we have
[19:21:41] created previously and we go to the
[19:21:43] indexes you can see that we have created
[19:21:46] few indexes and one of them is the
[19:21:48] clustered index. This one is a row store
[19:21:52] index. So our table is splitted by the
[19:21:55] rows. Now let's go and change that.
[19:21:57] Let's make our table splitted by the
[19:21:59] columns using the column store. So we're
[19:22:01] going to say
[19:22:02] create
[19:22:04] clustered column store index and we're
[19:22:08] going to give it the name index DB
[19:22:11] customers and it's going to be on the
[19:22:13] table sales DB customers and here if you
[19:22:18] go and specify a column it's going to be
[19:22:20] a mistake. So let's go and check that.
[19:22:23] So if you go and execute it says it
[19:22:25] fails because key lists or the columns
[19:22:28] is not allowed. So we cannot have this.
[19:22:30] So let's remove it. And now we have the
[19:22:32] correct syntax. Let's execute it again.
[19:22:35] We will get another error because it
[19:22:37] says in one table you cannot have more
[19:22:40] than one clustered index. We have
[19:22:42] already one. You have to decide do you
[19:22:44] want to split your table by columns or
[19:22:46] by rows. That's why we have to go and
[19:22:48] drop the previous index. So we're going
[19:22:50] to do it like this. Drop index. And I
[19:22:54] need the name of the index like this.
[19:22:56] And then we have to specify the table
[19:23:01] name. So that's it. Let's drop the
[19:23:04] index. Now if you refresh, we cannot see
[19:23:07] anymore our clustered index and our
[19:23:09] query should be working. So let's do
[19:23:11] that. Now let's check the indexes again.
[19:23:14] And now as you can see, we got a new
[19:23:17] clustered index, but this time it is
[19:23:19] column store. Now you can see at the
[19:23:21] start we have like an icon. This looks
[19:23:23] like a bar chart or like analytics and
[19:23:26] reports and that's because the main
[19:23:27] purpose of creating com store is to have
[19:23:30] a bar chart. So now of course we cannot
[19:23:32] go and create multiple clustered column
[19:23:34] index. We can have maximum only one. So
[19:23:37] now if you say you know what let's go
[19:23:38] and create for the first name another
[19:23:41] index but this time it's going to be a
[19:23:42] column store. So if I go and copy the
[19:23:45] whole thing over here and let's say it
[19:23:47] is none clustered column index and let's
[19:23:51] call it for example first
[19:23:53] name and we define over here the first
[19:23:58] name. So that's it. Let's go and execute
[19:24:01] it. You will see that we will get an
[19:24:03] error where SQL tells us you cannot
[19:24:06] create multiple column store indexes.
[19:24:09] That means you can create only one
[19:24:11] column store index for each table and
[19:24:14] you have to decide whether it is a
[19:24:16] clustered or non-clustered and you
[19:24:18] cannot create like the row store
[19:24:20] multiple non-clustered index. So you are
[19:24:23] allowed only with one column store index
[19:24:26] but this limitation is only here in the
[19:24:28] SQL server. In other databases I know
[19:24:30] that is allowed to use multiple column
[19:24:32] store indexes like in the Azure SQL
[19:24:36] server you can do that. So now in order
[19:24:37] to practice and you would like to create
[19:24:39] a nonclustered column store index, you
[19:24:42] can drop the first one and you can go
[19:24:43] and create the one that you need as a
[19:24:46] nclustered index. So actually let's go
[19:24:48] and do that. Let's drop the first one.
[19:24:50] So drop index and this is our index on
[19:24:55] this table. Let's do that. And once you
[19:24:58] execute the nonclustered column store
[19:25:00] index is going to work. And if you
[19:25:02] refresh over here, you will see that we
[19:25:04] have a non-clustered column store index
[19:25:07] for the first name. Okay. So now as we
[19:25:09] learned that the column store going to
[19:25:11] go and compress the data and the storage
[19:25:13] that is needed for the entire table
[19:25:15] going to be less than the row store. So
[19:25:18] let's see whether that is really true.
[19:25:20] Now in order to check this I will not do
[19:25:22] that in the database sales DB because
[19:25:24] everything here is already small. We're
[19:25:26] going to go and use another database. We
[19:25:28] have the adventure works DW2022 and if
[19:25:32] you have a newer version that's okay. So
[19:25:34] now what is the plan? We're going to go
[19:25:36] and create three identical copies of one
[19:25:38] table and we're going to have different
[19:25:40] structures. So the first one going to be
[19:25:41] the heap structure. The second one going
[19:25:43] to be row store structure and the third
[19:25:46] one going to be column store structure
[19:25:48] and then we're going to go and compare
[19:25:49] the storage of those three. So now we
[19:25:52] have to go and pick one of those tables.
[19:25:54] We need one big table. So for example
[19:25:56] the fact internet sales. So let's see
[19:25:58] how we can do that. Let's start with the
[19:25:59] heap structure. We're going to say
[19:26:01] select star into a new table. So it's
[19:26:05] going to be the
[19:26:06] fact internet sales and underscore hp
[19:26:11] for the heap. And we're going to get it
[19:26:13] from the table fact internet sales. So
[19:26:18] like this. And here it's very important
[19:26:19] if you are switching databases you have
[19:26:21] to go and use the database. So it's
[19:26:23] going to be use adventure work DW 2022.
[19:26:27] So execute this at the starts to make
[19:26:30] sure that you are switching to the new
[19:26:32] database. And now let's go and execute
[19:26:34] our heap structure. So with that we have
[19:26:36] created heap table as you can see 60,000
[19:26:39] rows. And since we didn't define any
[19:26:41] clustered index this table going to be
[19:26:44] heap structure. Now let's go and create
[19:26:46] another table where we use clustered row
[19:26:49] store index. So what we're going to do,
[19:26:51] we're going to copy the whole thing over
[19:26:53] here and we're going to call this row
[19:26:55] store and we're going to go of course
[19:26:57] change the name to RS but still we are
[19:26:59] targeting the same table. So let's go
[19:27:02] and execute this at the start. But now
[19:27:04] in order to make it as clustered row
[19:27:06] store we have to go and create an index.
[19:27:08] So it going to be like this create
[19:27:12] clustered index. We don't have to
[19:27:14] specify the row store because it is as a
[19:27:17] default. It's going to be ro store. So
[19:27:18] let's call it
[19:27:20] index facts
[19:27:23] internet sales RS and then the primary
[19:27:27] key. So B key and now we need the
[19:27:31] table fact internet sales RS and now we
[19:27:36] need the columns the primary key well
[19:27:39] actually I don't know what is the
[19:27:40] primary key so let's go and check that
[19:27:42] so it is a composite primary keys so
[19:27:45] it's going to be the sales order number
[19:27:49] and sales order line number like this.
[19:27:53] So let's go and execute this. And with
[19:27:56] that we have clustered row index. I'm
[19:27:58] going to go and check what do we have
[19:28:00] over here. So let's go and refresh
[19:28:01] everything. So we have now two tables
[19:28:04] the heap and the row store. So let's
[19:28:07] extend it and check the indexes. And as
[19:28:09] you can see we have the clustered index.
[19:28:12] Now we need the third table. It's going
[19:28:14] to be the column store index. I'm just
[19:28:16] going to go and copy the whole thing
[19:28:17] over here. So this is the column store
[19:28:22] going to be here CS and CS and of course
[19:28:27] we don't need any columns for the column
[19:28:29] store and don't forget to add the column
[19:28:32] store keyword. So create cluster column
[19:28:35] store index and we have to rename as
[19:28:37] well over here. So let's go and execute
[19:28:40] our new stuff. So we create first the
[19:28:42] table and then we convert it to a column
[19:28:45] store index. So let's go and do that and
[19:28:48] we have to go and refresh and check our
[19:28:50] tables. So this is our third table and
[19:28:53] let's go and check the indexes and we
[19:28:55] have clustered column store. All right.
[19:28:57] So now we are done. We have our three
[19:28:59] different tables. Now let's go and check
[19:29:01] the stoages of those three tables. So
[19:29:04] now let's go and check our first table
[19:29:05] the heap table. So right click on it and
[19:29:08] go to the properties. And now we can see
[19:29:10] here a lot of informations about our
[19:29:12] table. But we are interested on the
[19:29:13] storage. So click here on the page for
[19:29:16] the storage. And now we can see here few
[19:29:18] informations about the storage and one
[19:29:20] of them is the data space. It is around
[19:29:22] 9 MB and the index space is almost
[19:29:25] nothing. So we don't have anything over
[19:29:27] here. So this is the storage of the heap
[19:29:30] structure. We don't have any indexes.
[19:29:31] Let's go now to the row store. So we're
[19:29:34] going to go to the RS and properties.
[19:29:37] Then let's go to the storage. And now as
[19:29:39] you can see the data space is exactly
[19:29:41] the same. And that's because whether it
[19:29:43] is heap or row store index, we're going
[19:29:45] to store the data in data pages as rows.
[19:29:48] So the size of the data itself will not
[19:29:50] change. It will be sorted differently.
[19:29:52] But what changed here is the size of the
[19:29:55] index. Now we are consuming more storage
[19:29:58] for the index. So that means the overall
[19:30:01] storage of the table with a cluster draw
[19:30:03] store index it is more than the heap
[19:30:06] structure. Let's go and check now our
[19:30:08] column store index. So to the CS and
[19:30:11] let's go to the properties. And now it
[19:30:13] is interesting to see whether our table
[19:30:15] is getting smaller. So let's go to the
[19:30:17] storage. And as you can see the data
[19:30:19] space is around 1 mgabyte compared to
[19:30:22] the 9 mgabyte. I know those are small
[19:30:25] numbers but still it is massively
[19:30:28] reduced space because everything is
[19:30:30] compressed and of course we are not
[19:30:32] using any index spaces because we don't
[19:30:35] have this B3 structure in the column
[19:30:38] store. So as you can see if you compare
[19:30:39] to the others it is the winner. This
[19:30:42] table that is using the column store is
[19:30:44] consuming way less storage than the
[19:30:47] others. So now if you want to rank it
[19:30:49] based on the storage the best one is the
[19:30:52] column store index table. Then the next
[19:30:55] one is the table with the he structure
[19:30:57] and the worst one is the table with the
[19:31:01] row store clustered index. So that's
[19:31:03] true. column store index is consuming
[19:31:06] less space than the other type of
[19:31:12] indexes. All right. So now what is
[19:31:14] unique index? Unique index is a special
[19:31:16] type of indexes that going to make sure
[19:31:19] no duplicates in your data. And there
[19:31:22] are a couple of reasons why is it
[19:31:23] important to have a unique index. The
[19:31:25] first one and the most obvious reason is
[19:31:28] to have data integrity. So the unique
[19:31:30] index going to go and enforce uniqueness
[19:31:32] in your data and that is very helpful.
[19:31:35] For example, if you have a column like
[19:31:37] an email address or a product ID. Having
[19:31:41] duplicate in such a columns can mess up
[19:31:43] your data very badly. So having a unique
[19:31:46] index on a column like an email going to
[19:31:48] make sure there are no sneaky duplicates
[19:31:50] inside your data. And the second
[19:31:52] important reason why unique index is
[19:31:55] important is to improve the performance.
[19:31:57] So for example, if you are searching for
[19:31:59] specific email, the SQL going to start
[19:32:01] searching for the email value and once
[19:32:04] the SQL find the value, the SQL will
[19:32:06] stop searching because we are sure that
[19:32:08] there is no duplicates in the data. So
[19:32:10] with that you are improving the
[19:32:12] performance of your queries. So if you
[19:32:14] are creating an index and you know this
[19:32:16] column is unique then make sure to make
[19:32:18] the index as unique index. So now if you
[19:32:21] have a look again to our clustered index
[19:32:23] where we have the B structure if you
[19:32:25] make this index as unique then you are
[19:32:28] giving an extra task for the SQL that's
[19:32:31] going to go and make sure that all those
[19:32:34] ids of the customer going to be unique.
[19:32:37] So SQL has to guarantee that there are
[19:32:40] no duplicates at all inside your data in
[19:32:42] the databases. So now since we are
[19:32:45] giving SQL an extra task to prove the
[19:32:48] uniqueness of the data building the
[19:32:50] clustered index going to be little bit
[19:32:52] slower. So that means inserting new data
[19:32:55] writing data going to be slower as the
[19:32:58] normal clustered index. But now if you
[19:33:00] are talking about the read performance
[19:33:02] the performance of our query it's going
[19:33:04] to be optimized a little bit faster than
[19:33:06] a normal clustered index. So again this
[19:33:09] tradeoff we are making writing data
[19:33:12] slower but we are gaining more speed on
[19:33:15] the query performance. So this is what
[19:33:17] we mean with unique index. Okay. So
[19:33:20] let's keep extending the syntax of the
[19:33:22] index. So now in order to tell whether
[19:33:23] it is unique or not we can specify it
[19:33:26] exactly at the start. So we say create
[19:33:29] unique is just before the clustered or
[19:33:31] nonclustered and then afterward the cl
[19:33:33] store and nothing changed for the rest.
[19:33:35] So we can specify this keyword to TSQL,
[19:33:38] it should be unique. And if you don't
[19:33:40] write anything before the clustered
[19:33:42] index, it's going to be not unique. So
[19:33:45] for example, this one says create an
[19:33:47] index. So we didn't specify anything
[19:33:50] here, duplicates are allowed in the
[19:33:52] index. But if you go and specify a
[19:33:54] unique index, then the duplicates are
[19:33:57] not allowed. So it is very simple. Okay.
[19:33:59] So now let's go and create unique
[19:34:01] cluster. Now let's go and target the
[19:34:03] table products. Let's go and first
[19:34:05] select the data from the table. So sales
[19:34:10] products and execute it. Now let's see
[19:34:12] that I'm going to go and create a unique
[19:34:14] index on the column category. Let's go
[19:34:17] and try it. So create unique
[19:34:21] nonclustered index and let's give it the
[19:34:24] name index products
[19:34:29] category on the table sales products and
[19:34:33] we are targeting the column category. So
[19:34:37] let's go and execute it. Now we will get
[19:34:40] an error because the category has
[19:34:43] duplicates. So if you go and query again
[19:34:45] our table, you can see we have here
[19:34:47] duplicate values and the SQL cannot go
[19:34:50] and create unique index for this table.
[19:34:53] It's too late. But you still can create
[19:34:55] this index if the table is empty and SQL
[19:34:57] will not allow you to insert any
[19:34:59] duplicates about the categories. And of
[19:35:02] course it makes no sense to have unique
[19:35:04] index on the categories because of
[19:35:06] course we're going to get duplicates
[19:35:07] here. But maybe you say, you know what,
[19:35:09] my products are unique. The product name
[19:35:12] should be unique and we are not allowed
[19:35:14] to have in this table two products with
[19:35:16] the same name. So if you have such a
[19:35:18] rule at your business, you can go and
[19:35:20] define a unique index for the products.
[19:35:22] So let's go and do that. Now we're going
[19:35:24] to go and replace the category with the
[19:35:27] products and the same thing over here.
[19:35:30] So we are targeting the column products.
[19:35:32] Let's go and execute it. As you can see
[19:35:35] now it is working because we don't have
[19:35:37] any duplicates inside the table
[19:35:39] products. And if you go and check the
[19:35:41] indexes over here, we can see our new
[19:35:44] index. And as you can see at the start
[19:35:46] here, it says it is unique non-clustered
[19:35:48] index. Now let's go and try the data
[19:35:51] integrity. Are we allowed not to add any
[19:35:54] duplicate to this table? So let's go and
[19:35:56] try that out. Let's have an insert
[19:35:57] statement. Let's say insert into sales
[19:36:01] products. And I would like only to
[19:36:03] insert the product ID and the product
[19:36:08] name. and we're going to insert two
[19:36:11] values. Values, let's say we're going to
[19:36:14] have a new ID 106, but we're going to go
[19:36:16] and insert duplicate for the product
[19:36:19] name. So, we're going to say caps. We
[19:36:22] have already a product called caps over
[19:36:24] here. So, we are now inserting
[19:36:26] duplicates. Let's go and try it. Now,
[19:36:28] you will get an error saying you cannot
[19:36:30] insert duplicates to this table because
[19:36:33] we have unique index. So as you can see
[19:36:35] this index is now helping us and
[19:36:38] improving the quality of my table. So
[19:36:40] this is how we work with the unique
[19:36:42] index in
[19:36:46] SQL. Okay. So now what is a filtered
[19:36:49] index? A filtered index is a regular
[19:36:51] index but with a twist. It only includes
[19:36:55] rows that meet specific condition. So
[19:36:58] let's understand what this means. So
[19:36:59] again we have our nonclustered index and
[19:37:02] the B3 structure. So now at the leaf
[19:37:04] nodes we will get only the ids the data
[19:37:08] that fulfill a specific condition. So
[19:37:10] for example if we are saying we want
[19:37:12] only the active customers this is the
[19:37:14] condition. So that means on the leaf
[19:37:16] nodes we will have only the customer ids
[19:37:19] that are active and any inactive
[19:37:22] customer will not be included at all at
[19:37:25] the data page and at the nodes. So that
[19:37:27] means our B structure going to be little
[19:37:30] bit smaller as usual because we have
[19:37:32] less data included in the structure. So
[19:37:35] our index going to be smaller than the
[19:37:37] regular nclustered index. So now the
[19:37:40] question is why is it important to have
[19:37:42] a filtered index? Well the biggest
[19:37:44] benefit is we going to have targeted
[19:37:47] optimizations. So for example if our
[19:37:49] analyzes always focuses on the active
[19:37:52] users and the inactive users are totally
[19:37:54] unrelevant. So that means having only
[19:37:57] relevant subset of data in the index
[19:38:00] going to make the whole index much
[19:38:02] smaller which leads to faster
[19:38:05] performance. So it's going to be faster
[19:38:07] to query this filtered B3 structure. So
[19:38:11] that means we are doing targeted
[19:38:13] optimizations and we are improving the
[19:38:15] query performance. Now the second
[19:38:18] benefit if you think about the storage
[19:38:20] since the size of the B structure going
[19:38:22] to be smaller that means we're going to
[19:38:24] need less storage space in order to
[19:38:26] store the index which is great thing if
[19:38:29] you have large tables in your database.
[19:38:32] So the filter the index going to make
[19:38:34] the structure of the index smaller which
[19:38:36] going to improve the speed and the
[19:38:38] performance and as well reduce the
[19:38:40] storage that is needed for your index.
[19:38:43] Okay. So now let's check the syntax of
[19:38:44] the filtered index. It's very simple.
[19:38:46] It's like any query you can go and add
[19:38:49] at the end of creating the index the
[19:38:52] wear clause and then the condition as
[19:38:54] you are doing in any select statements.
[19:38:56] But the SQL server is very restrictive
[19:38:59] using this type of index. So you cannot
[19:39:01] use filtered index on a clustered index.
[19:39:05] So it is only allowed for the nclustered
[19:39:08] index because it makes no sense. If you
[19:39:11] create a clustered index, the entire
[19:39:13] table should be reorganized and ordered.
[19:39:16] So it will not work for only subset of
[19:39:19] data and as well you cannot create a
[19:39:21] filtered index on a column store. So it
[19:39:24] is only allowed if you are using row
[19:39:27] store but you can go and combine the
[19:39:29] unique index together with the filtered
[19:39:31] index. There's no restrictions. So it's
[19:39:33] going to be like this. Create unique
[19:39:35] nonclustered index on the table and then
[19:39:38] you specify the wear condition. So this
[19:39:40] is the syntax of the filtered index and
[19:39:42] we have these restrictions. All right.
[19:39:44] So now let's say that we have the
[19:39:45] following query where we are selecting
[19:39:47] data from customers but always in our
[19:39:49] program or in our report we are
[19:39:52] selecting only the customers from USA.
[19:39:55] So we have the following condition. It
[19:39:57] says where country equal to USA and
[19:40:01] execute. So this is the basics of many
[19:40:04] queries that we have in our project and
[19:40:06] we are always filtering the customers
[19:40:08] based on the country. So in one query we
[19:40:10] are finding maybe the top customers and
[19:40:13] another query we are finding the average
[19:40:15] of scores and so on. But we are always
[19:40:17] filtering the data like this where
[19:40:19] country equal to USA. So now since we
[19:40:22] are using this column a lot and our
[19:40:24] table may be getting like million of
[19:40:26] records we can go and create
[19:40:28] nonclustered index on this column. So
[19:40:30] the usual way we go over here and say
[19:40:32] create
[19:40:34] nonclustered index and we call it like
[19:40:37] this index
[19:40:39] customers country and then it's going to
[19:40:42] be on the
[19:40:43] table sales customers and we select the
[19:40:47] column country like this. So if you do
[19:40:50] it like this SQL going to go and create
[19:40:52] a nclustered index for all customers not
[19:40:55] only from USA but for everything. So
[19:40:58] even if the customers come from Germany
[19:41:00] which is not really necessary because in
[19:41:02] our project we only focus on the
[19:41:05] customers from USA. So instead of that
[19:41:08] we can go and include the wear condition
[19:41:11] inside our cluster. So it's very simple
[19:41:13] we're going to go and say where country
[19:41:16] equal to USA exactly like our query. So
[19:41:20] now the index that's going to be built
[19:41:22] it will be focused and targeted only for
[19:41:25] subset of data only for the data that
[19:41:27] fulfill this condition. So now let's go
[19:41:30] and create our filtered index and it is
[19:41:33] working. Let's go and check our indexes
[19:41:35] on the customers. So let's go to the
[19:41:37] indexes over here and refresh. Now we
[19:41:40] can see our index over here. It says it
[19:41:42] is not unique because we didn't define
[19:41:44] anything at the start. So duplicates are
[19:41:46] allowed of course which is what we
[19:41:48] defined here. And as well it is
[19:41:50] filtered. So it doesn't contain all the
[19:41:53] rows from your table. It contains only
[19:41:55] the rows that fulfill our condition. So
[19:41:58] that means now if I go and execute this
[19:42:00] query, the index going to be used
[19:42:03] because the rows of this query is
[19:42:05] included in the index. But if I go over
[19:42:07] here and say Germany and execute the
[19:42:10] query, it's going to be slower because
[19:42:12] all those rows inside the query is not
[19:42:16] part of our index. So this index will
[19:42:18] not be used at all in order to improve
[19:42:21] the query. So this is how we work with
[19:42:23] the filtered index in
[19:42:28] SQL. All right. So now we're going to
[19:42:30] summarize and talk quickly about how to
[19:42:32] use the right index. So when to use
[19:42:34] which type? Let's start with the first
[19:42:36] one. We have the heap structure. So as
[19:42:38] we learned it is a table without any
[19:42:41] index. So in which scenario we don't
[19:42:43] have to use any indexes in case you want
[19:42:46] to have fast inserts. So if you want to
[19:42:48] have a fast write performance then don't
[19:42:51] take any index. So you stay with the
[19:42:53] default with the he structure of your
[19:42:55] table and we usually use it in not very
[19:42:57] important tables like the staging tables
[19:43:00] or temporary tables where we want to
[19:43:02] insert the data fast and then get rid of
[19:43:05] the data later. So here there is no need
[19:43:07] to utilize any index. Now if you are
[19:43:09] talking about the clustered index, we
[19:43:12] usually use the clustered index for
[19:43:14] primary keys. It is even a default from
[19:43:16] the database. If you create any primary
[19:43:18] keys, then SQL going to go and create a
[19:43:20] clustered index. So this is the main
[19:43:23] usage of the clustered index, you use it
[19:43:25] in the primary keys. And if there's like
[19:43:27] no primary key in your table, then you
[19:43:29] can go and pick another column where
[19:43:31] sorting the data is important like for
[19:43:33] example a date column. So it could be a
[19:43:35] good candidate for your clustered index.
[19:43:37] Now moving on to another type we have
[19:43:39] the column store index. So when I said
[19:43:42] here clustered index I mean clustered
[19:43:44] row store index of course. But now the
[19:43:46] question is when do we use the column
[19:43:48] store index. If you have like big
[19:43:50] complex analytical queries where you are
[19:43:52] aggregating a lot of data doing data
[19:43:54] aggregations then go for the column
[19:43:56] store index because it going to give you
[19:43:58] amazing performance. And as well if you
[19:44:00] are struggling with the size of tables
[19:44:02] so if you have a super large table you
[19:44:05] can go and use the column store index
[19:44:07] because it can go and compress the data
[19:44:09] and reduce the size of the whole table.
[19:44:11] So for those scenarios we use the column
[19:44:13] store index. So again for the row store
[19:44:16] clustered index we use it usually for
[19:44:18] the old TB systems where you have a lot
[19:44:20] of transactions and so on but for the
[19:44:23] column store we use it usually for the
[19:44:25] OLAP systems where you have a data
[19:44:26] warehouse reporting system business
[19:44:28] intelligence and so on. Now moving on to
[19:44:30] another type we have the nonclustered
[19:44:32] index. We usually use this index for non
[19:44:35] primary key columns. So that means the
[19:44:37] rest of the columns of your tables could
[19:44:39] be candidate for the nonclustered index.
[19:44:41] And there are a lot of reasons why you
[19:44:43] would do that. For example, for the
[19:44:45] foreign keys or using it on the columns
[19:44:47] that are used in order to join two
[19:44:50] tables and another place where you can
[19:44:52] use the nonclustered index for the
[19:44:54] columns that are used for the work
[19:44:56] clause. So there are like many scenarios
[19:44:58] where we can use the nonclustered index
[19:45:00] but not for the primary keys. Now moving
[19:45:02] on to another type, we have the filtered
[19:45:04] index. We use it in order to target a
[19:45:07] subset of data. So if in our query and
[19:45:09] analyzes we are only focusing on a
[19:45:12] subset of data all time, it makes no
[19:45:14] sense to have one big index for all
[19:45:16] data, we can use the filtered index to
[19:45:18] have focused index. And of course if the
[19:45:21] size of the index is a problem then you
[19:45:24] can use a filtered index in order to
[19:45:26] reduce the overall size of the storage
[19:45:28] of the index. And then to the last type
[19:45:30] we have the unique index. you can go and
[19:45:33] use the unique index in order to ensure
[19:45:35] data integrity of your table and as well
[19:45:37] it might prove slightly the performance
[19:45:39] of your query and that's because SQL has
[19:45:42] less task to do if the index is unique
[19:45:44] once SQL finds a match it going to skip
[19:45:46] the search so this is a quick summary
[19:45:48] and guide on when to use which index
[19:45:51] type that usually help me finding the
[19:45:54] right
[19:45:58] index all right friends so now let's say
[19:46:00] that you have created your index ES in
[19:46:02] your database and your query is
[19:46:04] optimized and you have fast performance
[19:46:07] but the job is not done yet.
[19:46:10] No
[19:46:11] god no god please no no no no
[19:46:17] because over the time the indexes get
[19:46:19] fragmented outdated unused and this
[19:46:22] going to lead to a poor performance in
[19:46:24] your queries and as well going to
[19:46:26] increase the storage costs and the
[19:46:29] overall performance of your database
[19:46:31] going to drop down. So indexes like
[19:46:33] having a car it need maintenance. So you
[19:46:36] need to change the oil and the tire of
[19:46:38] the car. And the same thing goes for the
[19:46:40] indexes. You have to maintain them. They
[19:46:42] need attention to keep everything
[19:46:44] running smoothly. So now I'm going to
[19:46:46] show you how I manage, maintain, and
[19:46:49] monitor the indexes of my SQL projects.
[19:46:51] So let's
[19:46:55] go. The first and the most important
[19:46:58] task is to monitor the usage of your
[19:47:00] indexes. So of course the first question
[19:47:02] we have to ask ourself over the time are
[19:47:05] we using really the indexes that we have
[19:47:07] created are they really helping
[19:47:09] improving the speed of my queries or was
[19:47:12] it just a good idea at the start of the
[19:47:14] project and later no one used those
[19:47:16] indexes. This is very crucial because if
[19:47:19] you are having an unused index you are
[19:47:21] consuming unnecessary storage space and
[19:47:24] as well the right performance in the
[19:47:26] tables going to be slow which is
[19:47:28] completely unnecessary if you are not
[19:47:30] using the index. So now our task is to
[19:47:33] find out the usage of each index that
[19:47:36] you have in the projects. So let's see
[19:47:37] how we can do that. So now previously we
[19:47:39] have created like multiple indexes on
[19:47:41] the table DB customers. So if you go to
[19:47:44] the DB customers and to the indexes, you
[19:47:46] can see that we have four indexes. Now
[19:47:48] we can go and show those informations by
[19:47:50] using a special stored procedures from
[19:47:52] the SQL server called SP help index.
[19:47:55] Let's go and do that. So SP help index.
[19:47:59] So it is a system stored procedure that
[19:48:02] comes with the database. So this stored
[19:48:04] procedure needs only one value and that
[19:48:06] is the table name. So we have it over
[19:48:09] here sales DB customers. Let's go and
[19:48:12] query it. So we have four indexes. Then
[19:48:14] we have a nice description of the index.
[19:48:16] So it says it is nonclustered index and
[19:48:19] whether it is column store. And it say
[19:48:21] where it is located. So it says it
[19:48:23] located on primary. Primary is the name
[19:48:26] of the file group where the data is
[19:48:28] stored. And as a default it can be
[19:48:29] stored as primary. And now the next
[19:48:31] information we have the index keys. It
[19:48:33] is nice information to understand which
[19:48:35] keys are used or which columns are used
[19:48:37] for the index. So the first one you can
[19:48:40] see we have two columns that means it is
[19:48:41] a composite index and of course for the
[19:48:44] column store we don't have any columns
[19:48:46] and then we have the first name last
[19:48:47] name. So this is a really nice quick
[19:48:49] store procedure in order to see
[19:48:50] information about our index. Okay. So
[19:48:52] now let's focus on our task on how to
[19:48:54] monitor the usage of the indexes. Now in
[19:48:57] databases we have a lot of schemas and
[19:48:59] tables that protocol the metadata of our
[19:49:03] database. And in SQL Server, we have a
[19:49:05] special schema called CIS where you can
[19:49:07] find a lot of metadata information about
[19:49:10] the SQL server. Metadata like the
[19:49:12] description of the tables, views,
[19:49:14] columns and as well the indexes. So now
[19:49:17] let's check what we can find inside the
[19:49:19] table indexes. So let's going to do it.
[19:49:21] Select star from CIS. This is the schema
[19:49:24] name. And then as you can see we have a
[19:49:26] list of many informations but we want to
[19:49:29] focus on the indexes. Now let's go and
[19:49:32] execute it. Now we get a huge list of
[19:49:34] all indexes that we have and a lot of
[19:49:37] informations for each index. We don't
[19:49:40] have to go and understand now each
[19:49:41] column. But I'm going to go and select
[19:49:43] the main important informations from
[19:49:45] this table. So what do we need? The
[19:49:47] object ID. This is the table ID. So the
[19:49:50] object
[19:49:51] ID and we have the name. It is the index
[19:49:55] name. And then here we have a nice
[19:49:58] information whether it is clustered or
[19:50:00] nonclustered. So let's go and select it
[19:50:03] type disk as so let's call it index type
[19:50:06] and we can go and check whether it is
[19:50:08] primary key or not. So let's get this
[19:50:10] information as well is primary key. I
[19:50:13] will go and just rename it is primary
[19:50:16] key and what else do we need whether it
[19:50:19] is unique. So it is as well nice
[19:50:20] information to have. So is
[19:50:23] [Music]
[19:50:26] unique. So of course you can go and grab
[19:50:28] a lot of stuff. It depends really on
[19:50:30] what you are monitoring. So for example,
[19:50:32] I'm going to go and check whether it is
[19:50:33] disabled or not. So is
[19:50:36] disabled and I'll just rename
[19:50:40] it. So with that I have like focus
[19:50:43] monitoring. I don't have to have all
[19:50:44] those informations. So let's go and
[19:50:46] execute. But now I would like to go and
[19:50:47] change few stuff like for example I
[19:50:49] don't want the object ID. I would like
[19:50:51] to have the full name of the table. And
[19:50:53] as well there is a lot of indexes that
[19:50:56] is unrelevant for my database. So now in
[19:50:58] order to do that we have to go and get
[19:51:00] the informations from another metadata
[19:51:02] table. So let's go and call this index
[19:51:04] and let's go and join it with another
[19:51:07] metadata table. It's called tables. So
[19:51:11] tbl and we're going to go and join it
[19:51:13] using the so the index object
[19:51:17] ID equal to the table object ID. And now
[19:51:22] if you like to see the content of this
[19:51:24] table we can go and create separately.
[19:51:26] So select star from our new table. So
[19:51:30] let's see the content of this table. So
[19:51:32] you can see we have the name which is
[19:51:34] the table name. And I think that's all
[19:51:35] what we need. We have a lot of other
[19:51:37] informations about the table. Well, I
[19:51:39] just need the table name. So let's go
[19:51:41] and do it at the start. tbl name as
[19:51:45] table
[19:51:46] name and I don't need anymore the object
[19:51:49] ID. But of course we have to go and use
[19:51:51] the alias for each of those informations
[19:51:53] in order to understand those
[19:51:54] informations comes from the index. So
[19:51:56] let's go and do that. All right. So my
[19:51:59] query is ready. Let's go and execute it
[19:52:01] again. So now as you can see we are
[19:52:02] getting the table name and the list is
[19:52:04] very short because it is only focusing
[19:52:06] on the tables that you have in the
[19:52:08] database. And this filter happens
[19:52:10] because of the inner join. But one more
[19:52:12] thing I would like to go and sort the
[19:52:13] data. So I'm going to say order by I
[19:52:15] would like to sort it by the table name
[19:52:18] and then the index name. All right. So
[19:52:21] now let's go and check for example the
[19:52:23] table customers. You can see that we
[19:52:25] have two non-clustered index and one of
[19:52:27] them is column store index. Those two we
[19:52:29] have created from the previous tutorial
[19:52:31] and we have an index on the primary key
[19:52:33] as you can see here is primary key equal
[19:52:35] to one and this is as well unique. So
[19:52:37] with that we have a really nice list of
[19:52:39] all indexes that we have in our
[19:52:41] database. But we are not there yet
[19:52:43] because our task is how to monitor the
[19:52:47] usage of the index. Now in order to get
[19:52:49] the usage for each of those indexes, we
[19:52:52] have to go to a special view called
[19:52:54] dynamic management view. And there the
[19:52:56] SQL server going to provide a lot of
[19:52:58] statistics about the usage for that
[19:53:00] index. And we can find it as well in the
[19:53:01] same schema. So let's go and query this
[19:53:04] table. So it's going to be select star
[19:53:06] from. So the same schema says
[19:53:09] dodm
[19:53:11] db_ind index usage stats. So let's go
[19:53:15] and explore this table and execute it.
[19:53:18] Now in those statistics we can find the
[19:53:20] usage of two indexes the index number
[19:53:23] three and one. And we can see there are
[19:53:25] like three usage informations of the
[19:53:27] index number one. And next we have like
[19:53:29] user seeks user scans and user lookups.
[19:53:32] So this is how many times the index is
[19:53:34] used as seeks or scans or lookups. We
[19:53:37] will understand those informations as we
[19:53:39] learn about the execution plan. And here
[19:53:41] we have a very nice information about
[19:53:43] how many time our index got updated. So
[19:53:46] as you can see here is zero because I
[19:53:48] didn't add any new data after creating
[19:53:50] the index. But of course all those
[19:53:52] numbers might be different at your site
[19:53:54] because it depends whether you are doing
[19:53:55] more queries and practicing. And you can
[19:53:57] find here more informations about when
[19:54:00] was exactly the last usage of those
[19:54:02] indexes and many many nice informations.
[19:54:05] So now let's go and integrate this view
[19:54:08] with our query. So now what I'm going to
[19:54:10] do, I'm going to do a lift join because
[19:54:12] if I do an inner join, I will only find
[19:54:15] the used indexes. But I don't want that
[19:54:18] because I want to see a full build of
[19:54:20] all my indexes in the database. So left
[19:54:22] join and we're going to go and get our
[19:54:26] view and call it S. And then we have to
[19:54:28] join it on the keys. So S on. So I would
[19:54:31] say let's go and grab the object ID
[19:54:33] equal to the index object ID. And of
[19:54:37] course we have to join on the index ID.
[19:54:40] So it's going to be the index ID equal
[19:54:42] to the index ID like this. Now we have
[19:54:46] to go and select few informations from
[19:54:48] this view. So I'm going to go and select
[19:54:50] like all those number of usage. So s
[19:54:54] let's get the user
[19:54:57] seeks as the user
[19:55:00] scans and the
[19:55:02] lookups and maybe as well the user
[19:55:05] updates and it is really nice
[19:55:07] information to understand when it was
[19:55:09] the last time used. So last user
[19:55:12] seek and the
[19:55:14] last user scan. Let me just correct it
[19:55:17] over here. And actually I can go and put
[19:55:19] those two dates in one date because if
[19:55:23] it's like the last seek it's going to be
[19:55:24] null over here or the opposite. And now
[19:55:27] what we can do we can go and put those
[19:55:28] two together actually in one column
[19:55:30] because when we have a value over here
[19:55:32] it's going to be null and vice versa. So
[19:55:34] we can do that using the null function
[19:55:37] kowalis like this and we can get this
[19:55:40] over here and we can call the whole
[19:55:42] thing last
[19:55:44] update. So like this and maybe I'm going
[19:55:46] to go and rename all those
[19:55:48] [Music]
[19:55:54] stuff. All right. So now we are done.
[19:55:57] Let's go and execute it. Okay. So let's
[19:55:59] go and check our new report over here.
[19:56:01] So this is our query and let's start
[19:56:04] with the first table for example the
[19:56:05] customers and go to the right side. And
[19:56:07] now we can see that we have three
[19:56:09] indexes and from these two indexes we
[19:56:11] have only one index that is not used at
[19:56:14] all. So we can see over here that the
[19:56:17] nclustered index on the country is not
[19:56:20] being used and that's because we have
[19:56:22] another index about the country that
[19:56:24] comes from the column store. So it could
[19:56:26] be like this that you are quering the
[19:56:27] table using the country but the SQL
[19:56:31] saying I would like to go and use this
[19:56:32] index instead of the first one. So we
[19:56:34] can say okay this one is not really
[19:56:37] useful maybe we can go and drop it right
[19:56:39] and for the rest you can see okay this
[19:56:41] column store index is used twice and the
[19:56:43] next one is once again the numbers at
[19:56:45] your side might be different and if we
[19:56:47] have a look to all other tables we have
[19:56:49] a lot of nulls so that means all those
[19:56:51] indexes that you have created on the DB
[19:56:54] customers let me check only one is used
[19:56:56] but now you might say you know what I've
[19:56:58] used the index but why I'm not seeing
[19:57:00] here any numbers about it well that's
[19:57:02] because those numbers will not live
[19:57:04] forever and we are using now the express
[19:57:07] edition locally at our PC. So each time
[19:57:09] you shut down your PC and you close the
[19:57:11] client the database going to shut down
[19:57:13] as well and those statistics going to be
[19:57:15] lost because they are in the memory. But
[19:57:17] in real projects the numbers going to be
[19:57:19] totally different than here and of
[19:57:20] course you're going to get realistic
[19:57:22] numbers. Now let's try to target one of
[19:57:24] those not used indexes. So for example
[19:57:27] let's go with this index. It is not
[19:57:29] clustered index on the product. So let's
[19:57:31] go and query that. Currently it is
[19:57:33] completely not used. So if I go and
[19:57:35] select it. So select star from sales
[19:57:40] products where
[19:57:43] product equal to caps. So with that we
[19:57:46] have used the index I think. Let's go
[19:57:49] back and query again and let's go to our
[19:57:52] index and check whether it is used. Well
[19:57:54] it is correct. So our query did use this
[19:57:57] index and we can see here it is used
[19:57:59] once. And now you can go and analyze in
[19:58:01] your project all the indexes that you
[19:58:03] have on your tables and you can see
[19:58:05] whether you are really using it with
[19:58:07] your queries or not. And if you are not
[19:58:09] using the query of course you have to
[19:58:11] make a decision about it. Maybe if you
[19:58:13] are working a team to ask about it who
[19:58:15] did create it and why. Maybe there is
[19:58:17] like one task in the database that is
[19:58:20] not frequently used. Maybe it's
[19:58:22] something that is run like once a month
[19:58:24] or something like that. So the index is
[19:58:25] needed but not that frequently. But
[19:58:27] still now we have like insights about
[19:58:29] what is going on with those indexes and
[19:58:31] whether we need them or not. And if you
[19:58:34] don't need them, go and drop them. All
[19:58:36] right, my friends. So here is the secret
[19:58:38] that 90% of SQL developers don't do
[19:58:41] that's going to make you in 1 minute the
[19:58:43] hero of the projects. So once I join a
[19:58:46] project and after saying hello to
[19:58:47] everyone, I open the database of the
[19:58:50] project and do one query. I checked the
[19:58:52] usage of the indexes of the projects and
[19:58:55] I can tell you after working 15 years
[19:58:57] with SQL that 90% of indexes created in
[19:59:01] projects are totally untouched and
[19:59:03] unused. So I collect all unused indexes
[19:59:06] and discuss it with the team. And if we
[19:59:09] don't find real usage for those indexes,
[19:59:11] we go and drop them. So after dropping
[19:59:14] all those unused indexes, you have done
[19:59:16] two great things for the projects.
[19:59:19] First, you have saved a lot of storage
[19:59:21] in the database. And second, which is
[19:59:23] way more important, you have improved
[19:59:25] and optimized the right performance on
[19:59:28] the database. So in your first day with
[19:59:30] one query, you have optimized the
[19:59:33] performance of the database. You have
[19:59:34] save storage and you're going to shine
[19:59:36] like an expert in your project. So if
[19:59:38] you haven't done that, do that
[19:59:43] now. All right. And now moving on to the
[19:59:46] next one. As we learned, identifying an
[19:59:48] unused index is an important task. But
[19:59:51] in the other hand, identifying a missing
[19:59:54] index is as well very important to
[19:59:57] improve the performance of your queries.
[19:59:59] So in SQL server, you can get
[20:00:01] recommendations from the database itself
[20:00:03] about missing indexes for your query. So
[20:00:06] let's see where we can find those
[20:00:08] recommendations. All right. So now let's
[20:00:09] say that you are doing multiple queries
[20:00:11] and you are doing analyszis and so on.
[20:00:13] For example, I have this query over
[20:00:15] here. It is query on the database
[20:00:17] adventure works DW and I'm joining just
[20:00:20] two tables the fact with the dimension
[20:00:22] and then filtering the data based on the
[20:00:25] colors and as well on the date key where
[20:00:28] I have like a range over here. So once I
[20:00:30] executed I got the following
[20:00:32] informations. It could be any query that
[20:00:34] you are doing while practicing and
[20:00:35] analyzing and so on. So now if you have
[20:00:37] like slow query and so on you can go and
[20:00:39] check the recommendations from the
[20:00:41] database about missing indexes. So in
[20:00:43] order to do that we can go and check
[20:00:45] again the metadata from the database
[20:00:47] system to see the recommendations about
[20:00:49] the missing indexes. So let's go and do
[20:00:51] that. So we're going to go and select
[20:00:53] from and now we have to go and target
[20:00:55] the dynamic management views and it is
[20:00:57] like this dm
[20:01:00] db
[20:01:02] missing index details. So let's go and
[20:01:06] explore the content over here. And don't
[20:01:08] forget that those informations going to
[20:01:10] be inside the cache of the server and if
[20:01:13] there's like a restart or something in
[20:01:15] the server you will lose all those
[20:01:16] informations. So now from my query there
[20:01:19] is few suggestions and recommendations
[20:01:21] from the database. Let's go and check
[20:01:22] it. So we can see here there are four
[20:01:24] recommendations about missing indexes
[20:01:27] from the database. So now let's go and
[20:01:29] check the first recommendation over
[20:01:30] here. You can go and check the table
[20:01:32] name from the object ID or you can find
[20:01:34] it here in the statements. So here the
[20:01:36] database is suggesting an index for the
[20:01:38] table dimension product and it is
[20:01:41] recommending us to make an index for the
[20:01:43] column color and that's because if you
[20:01:45] check our query we have like here a
[20:01:47] filter the wear condition where we are
[20:01:48] seeing the color equal to black and
[20:01:50] since we don't have an index on the
[20:01:52] color SQL is just suggesting to use an
[20:01:54] index for the color and of course in
[20:01:56] this situation we can go and use an
[20:01:58] uncclustered index. Now after that we
[20:02:00] have three recommendations for the same
[20:02:02] table fact internet sales. So for
[20:02:04] example here it is suggesting to make an
[20:02:06] index on the order date K because we are
[20:02:09] using it in the filter over here and as
[20:02:11] well suggesting to make an index for the
[20:02:14] product key since we are using it for
[20:02:16] the join. So this is really nice report
[20:02:19] about missing indexes in the database
[20:02:21] and it could assist you to find out
[20:02:23] things that you didn't thought about.
[20:02:25] But here my recommendation is evaluate
[20:02:27] those informations very carefully. Don't
[20:02:30] go and create like an index for each
[20:02:32] suggestions from the database. You still
[20:02:34] have to think about it. Is it really
[20:02:36] necessary? Do we really use this query
[20:02:38] very frequently and so on? So don't go
[20:02:41] blindly creating indexes for each
[20:02:43] recommendations from the database. So
[20:02:45] this is really nice tool and assistant
[20:02:47] for you in order to make a good strategy
[20:02:49] for your indexing. So this is how you
[20:02:51] find the recommendations of missing
[20:02:53] indexes from SQL
[20:02:59] database. Okay. Okay. So now to the next
[20:03:01] step, we have to go and monitor the
[20:03:03] duplicates in indexing. If you are
[20:03:06] working in team with multiple developers
[20:03:08] and you are working parallely in order
[20:03:10] to optimize the performance of the
[20:03:12] queries, what might happen is that
[20:03:14] different developers creating different
[20:03:17] indexes for the same column in the same
[20:03:20] table. But of course, this must not
[20:03:21] happen if you have a clean and solid
[20:03:23] review process in the project. But we
[20:03:26] are human and those things could happen.
[20:03:28] So that's why you have to monitor
[20:03:30] whether there are like duplicates. So
[20:03:32] the mission is to find whether there is
[20:03:34] a column that is involved in multiple
[20:03:36] indexes. So let's see how we can monitor
[20:03:38] that in SQL. Okay. So now it's very
[20:03:40] simple in order to find the duplicates
[20:03:42] of indexes inside your database. So we
[20:03:45] have learned before that we can find the
[20:03:46] list of all indexes in this table
[20:03:48] indexes in the system schema and then we
[20:03:51] join it with the tables in order to get
[20:03:53] the table name and then we have another
[20:03:55] table in order to find the columns that
[20:03:58] are involved in the index. Those
[20:04:00] informations we can find it inside the
[20:04:02] index columns and now in order to get
[20:04:04] the full name of the columns we're going
[20:04:06] to go and join it with the columns
[20:04:08] table. So it's very simple and makes
[20:04:10] sense. Let's go and execute the whole
[20:04:12] query. Now as you can see it is sorted
[20:04:14] by the table name and the column name
[20:04:16] and that's because we can find then
[20:04:18] easier the duplicate. So let's go and
[20:04:20] check the first table. So the country is
[20:04:22] part of this index where we have the
[20:04:25] column store nonclustered and again the
[20:04:27] country is involved in another index
[20:04:29] where we have the customer's country and
[20:04:32] this is a row store nonclustered index.
[20:04:35] So this is of course bad thing. We have
[20:04:36] to go and decide now do we want it as a
[20:04:38] column store or row store. And if we
[20:04:41] check as well this table, we can find
[20:04:43] the first name in two different clusters
[20:04:46] the same story. And that's because we
[20:04:47] were practicing and creating those
[20:04:49] indexes. And that's it. But now if you
[20:04:51] have like large schema and a lot of
[20:04:53] indexes, I would go and make like a flag
[20:04:55] in order to understand whether we have a
[20:04:57] duplicate or not. And that's by
[20:04:58] calculating the number of rows of unique
[20:05:01] table name and index name. And we can do
[20:05:03] that very easily using the window
[20:05:05] functions. So let's have new row. And
[20:05:07] we're going to go and use the function
[20:05:09] count since we want to find the number
[20:05:11] of rows over. Then we're going to go and
[20:05:14] partition
[20:05:15] by we need the table name and as well
[20:05:19] the column name. Our expectation of this
[20:05:22] column should be one. If we have more
[20:05:24] than one then there is an issue and that
[20:05:27] means the column is inside two different
[20:05:30] indexes. And now let's go and sort it by
[20:05:32] the column name and descending. So let's
[20:05:36] go and execute it. And now we have here
[20:05:38] a nice flag where we can see how many
[20:05:40] rows we have for a specific column in a
[20:05:42] table. So if it's one like those
[20:05:44] columns, they are fine. Those columns
[20:05:47] are involved only once in one index. But
[20:05:49] for the first four rows, we have here an
[20:05:52] issue because we count here two columns.
[20:05:54] That means we have two indexes for the
[20:05:56] same column. So as you can see the query
[20:05:58] is very simple and with that we have a
[20:05:59] nice report about the duplicates of
[20:06:02] indexes inside our database.
[20:06:08] Okay, one more thing in order to
[20:06:10] maintain our indexes is by updating the
[20:06:12] statistics. The database engines usually
[20:06:15] use statistics in order to understand
[20:06:18] which index should be used for our
[20:06:20] query. And if these statistics are not
[20:06:22] up to date, SQL going to make wrong
[20:06:24] decisions. So let's understand what this
[20:06:26] means. Now let's say that you have
[20:06:28] created a table and you start inserting
[20:06:30] data to this new table. Now the database
[20:06:33] engine going to go and create your new
[20:06:34] table and insert the data. Behind the
[20:06:36] scenes the database engine going to go
[20:06:38] and create for your new table
[20:06:40] statistics. It's like metadata
[20:06:42] informations about your data and that's
[20:06:45] like a report or insights about your
[20:06:47] table where you can find a lot of
[20:06:49] informations like the number of rows
[20:06:52] that distribution of values in a column
[20:06:55] and as well we can find the number of
[20:06:57] distinct values and histogram and
[20:07:00] patterns and many other informations
[20:07:02] about your table. So now of course the
[20:07:04] question is why do we have those
[20:07:06] informations in the database? Now
[20:07:08] imagine that you are doing select from
[20:07:10] where what going to happen the database
[20:07:12] engine has to go and create an execution
[20:07:15] plan. We're going to learn about this
[20:07:17] later in details. It is just a road map
[20:07:19] on how to execute this query. So here
[20:07:22] for example in order to load the data
[20:07:24] from the table there are like different
[20:07:26] ways on how to do it. So there is like a
[20:07:28] table scan, index scan, index seek. So
[20:07:31] that means the database engine has here
[20:07:33] three different ways on how to do it.
[20:07:36] And now in order for the database to
[20:07:38] decide which way to use, it's going to
[20:07:41] go and read the statistics of the table.
[20:07:43] So it's going to go and collect
[20:07:45] informations. Okay, how many rows do we
[20:07:47] have? Are the informations are unique?
[20:07:49] How is the distribution of the data and
[20:07:51] so on. And now based on those statistics
[20:07:53] and numbers, the database can now make a
[20:07:56] good decision about which methods to use
[20:07:59] in order to load the data. So for
[20:08:01] example, here the index scan is the best
[20:08:03] way to load our table. So this is
[20:08:06] exactly why the database needs the
[20:08:08] statistics in order to make the correct
[20:08:11] decision and to use the correct index.
[20:08:13] So now you might ask okay this is
[20:08:15] something internal for the database why
[20:08:17] do we have to care about it? Well there
[20:08:19] is an issue. Now for example in our
[20:08:21] table we have 50 rows and let's say that
[20:08:23] in the next day you went and inserted to
[20:08:26] this table like around 1 million row.
[20:08:28] Now the issue that could happen is that
[20:08:30] the statistics will not get updated
[20:08:33] about this table and the statistics can
[20:08:35] still say that we have only 50 rows. So
[20:08:38] that means the statistics of this table
[20:08:40] is now outdated. And the big issue that
[20:08:43] once you query this table, the SQL
[20:08:45] engine don't know at all about the 1
[20:08:48] million row that you have inserted in
[20:08:50] the table because it's going to go and
[20:08:52] ask the statistics and it's going to
[20:08:54] answer with only 50 rows and the
[20:08:56] database going to say okay this is very
[20:08:58] small table and let's maybe skip an
[20:09:00] index or something. So that means the
[20:09:02] database going to make wrong decisions
[20:09:04] because the statistics are outdated. And
[20:09:08] now your task is to monitor those
[20:09:10] statistics and to keep updating them. So
[20:09:13] let's see how we can do that. Okay. So
[20:09:15] now the first thing that we have to do
[20:09:16] is to find out whether our statistics
[20:09:18] are up to date or outdated. In order to
[20:09:21] do that we have as well to access the
[20:09:23] metadata about our database. And for
[20:09:25] that as well we have tables and dynamic
[20:09:27] management functions in the system
[20:09:30] schema where we can find a lot of
[20:09:32] details about the statistics. And in
[20:09:34] order to monitor the statistics, I have
[20:09:36] prepared a query like this. So here I'm
[20:09:38] using a table called stats uh where here
[20:09:41] you're going to get a list of all
[20:09:42] statistics inside our database and the
[20:09:45] name of the statistics and then I'm
[20:09:47] joining it with the tables in order to
[20:09:48] get the table name and what is very
[20:09:51] important is the dynamic management
[20:09:53] function. So here we're going to get
[20:09:54] very important informations like the
[20:09:56] last updates and the number of rows and
[20:09:59] the number of modifications. So let's go
[20:10:02] and query it. So here we can see
[20:10:04] informations like the table name, the
[20:10:06] statistics name and now it's very
[20:10:08] important when the last time the
[20:10:10] statistics get updated. So now let's go
[20:10:12] and check our table DB customers. We can
[20:10:15] see here the statistics name and what is
[20:10:18] very important is the last update. So
[20:10:20] this tells us how old is the statistics.
[20:10:23] So for me it is like 4 days. And then we
[20:10:25] can find the total number of rows in
[20:10:27] this table. And now what is very
[20:10:29] important is the number of modifications
[20:10:31] that have been done on the table. So
[20:10:34] after updating the statistics on the
[20:10:36] 19th of October, there were around 15
[20:10:39] rows that got modificated. This could be
[20:10:41] an insert, update, delete. So any
[20:10:44] operation of the table considered to be
[20:10:46] a modification. So that you can see
[20:10:48] there were a lot of modifications. So
[20:10:50] these statistics should be updated. So
[20:10:52] now for the table customers, you can see
[20:10:54] that the statistics are up to date. So
[20:10:57] we have here zero as a modifications and
[20:10:59] there will be no need to update the
[20:11:01] statistics. So this is how you can go
[20:11:03] and check the statistics informations
[20:11:05] inside your database in order to make a
[20:11:07] decision should I update the statistics
[20:11:09] or not. So now let's say that I would
[20:11:11] like to go and update the statistics of
[20:11:13] our table DB customers. Now as you can
[20:11:16] see we have here multiple statistics. So
[20:11:18] over here we have this statistics on
[20:11:20] this table and as well we have the
[20:11:22] statistics on the index. So as you can
[20:11:24] see we have here multiple statistics in
[20:11:26] one table. One for the table itself and
[20:11:29] one for each index that we have in this
[20:11:31] table. So now let's say that I would
[20:11:33] like to go and update the statistics
[20:11:35] only for one. I don't want to update
[20:11:37] everything in this table only for one
[20:11:39] statistics. Let's go and do that. So
[20:11:42] it's going to be very simple update
[20:11:44] statistics. And then we have to go and
[20:11:46] mention the name. So it's going to be
[20:11:47] sales DB customers. And then we have to
[20:11:50] specify the name of the statistics. So
[20:11:52] let's go and get this over here and
[20:11:55] let's go and execute it. So it was very
[20:11:58] fast. Let's go and reexecute our query
[20:12:01] and check the data. So now let's go and
[20:12:04] find it. It was exactly this one. And as
[20:12:07] you can see it just got updated and the
[20:12:09] number of rows is five and the number of
[20:12:11] notifications is zero. So we have now an
[20:12:14] upto-date statistics for this table. But
[20:12:17] let's say that I would like to go and
[20:12:19] update the rest but I don't want to do
[20:12:20] it one by one. So what we can do we can
[20:12:23] just copy the same thing over here but
[20:12:26] we don't specify any name of the
[20:12:28] statistic. So we are saying update
[20:12:30] statistics and then only the table name.
[20:12:33] So let's go and execute it. So now what
[20:12:35] going to happen is still going to go and
[20:12:36] update all the statistics that belongs
[20:12:38] to this table. So let's go and check our
[20:12:41] query again. Now you can see everything
[20:12:43] disappeared and the DB customer is
[20:12:46] completely up to date with no
[20:12:48] modifications problem. So this is how
[20:12:50] you can go and update your table and you
[20:12:53] can do then for the rest as well. But
[20:12:55] now there is like one more thing where
[20:12:57] you can go and update the statistics of
[20:12:59] the whole database. But beware this
[20:13:03] might take really long time and we're
[20:13:05] going to do that by executing a special
[20:13:07] store procedure. So execute SP update
[20:13:11] stats. This one over here. Let's go and
[20:13:15] do that. And now it is done. And we have
[20:13:17] here a pretty long log. It was fast
[20:13:20] because we don't have a big database. It
[20:13:22] is very small database. So it's not
[20:13:23] compared to any real databases. So now
[20:13:26] we can see over here that SQL is going
[20:13:29] through everything that you have in the
[20:13:31] database and trying to update the
[20:13:32] statistics. So in many situations it's
[20:13:35] going to be not necessary because there
[20:13:36] is nothing to update. There were no
[20:13:38] modifications and so on. That's why the
[20:13:40] database is smart enough to say no it is
[20:13:42] not required and it go and skip it. So
[20:13:44] now how I usually do it in my project is
[20:13:46] that I have like a job on the weekend
[20:13:49] where it's going to go and update the
[20:13:51] whole database statistics. So with that
[20:13:54] I make sure all my tables and indexes
[20:13:57] having up to-date statistics. Of course
[20:14:00] if you have small database you can run
[20:14:01] this like every day but if this takes
[20:14:04] long time then you can schedule it in
[20:14:06] the weekend. And as well if I know in
[20:14:08] the project that there will be in one
[20:14:10] day a lot of new incoming data. So we
[20:14:13] are doing some kind of data migrations.
[20:14:15] So I go and update the statistics after
[20:14:17] the data migration is done just to make
[20:14:19] sure we have up-to-date statistics. So
[20:14:21] this is how we monitor and update the
[20:14:23] statistics of the
[20:14:24] [Music]
[20:14:28] database. Okay. Okay, so now moving on
[20:14:30] to the final task that I usually do in
[20:14:32] order to monitor and manage the indexes
[20:14:34] is to monitor the index fragmentations.
[20:14:37] Over the time as your data is inserted,
[20:14:40] updated, deleted into your tables,
[20:14:42] indexes can become fragmented. So what
[20:14:45] is fragmentation? It means like there is
[20:14:48] unused spaces in your databases and the
[20:14:50] database is not filling them or your
[20:14:53] data is not anymore sorted correctly in
[20:14:55] the index and this of course leads to
[20:14:58] inefficient use of the storage and as
[20:15:00] well going to slow down your
[20:15:07] queries and in SQL in order to get
[20:15:10] everything organized again we have two
[20:15:12] methods the first method is reorganize
[20:15:15] so it's going to go and def fragment the
[20:15:17] leaf level of the index in order to get
[20:15:20] it organized and sorted again with the
[20:15:22] logical order. So it is very light
[20:15:25] operation and it will not block the user
[20:15:27] from using your table. And the second
[20:15:29] method called rebuild this is
[20:15:31] heavyweight operation. It going to go
[20:15:34] and drop the whole index and recreate it
[20:15:37] from the scratch. And this means of
[20:15:39] course not only the data going to get
[20:15:40] sorted again but as well the
[20:15:43] fragmentations inside your databases and
[20:15:45] the index going to be eliminated. So
[20:15:47] let's see how we can do that in SQL.
[20:15:49] Okay. So now back to our database and
[20:15:51] the first question that you have to ask
[20:15:53] do we have an issue with the
[20:15:54] fragmentations in our indexes. So we
[20:15:57] have to check the health of our indexes
[20:15:59] in the database. And in order to do
[20:16:01] that, we have again to go to the system
[20:16:04] metadata that we have and we're going to
[20:16:06] check their dynamic management
[20:16:08] functions. So there is like a special
[20:16:10] functions in order to get an answer in
[20:16:12] the SQL server. Let's go and do that. So
[20:16:14] we're going to go and select star from
[20:16:16] the function. So it is sis dot so it's
[20:16:19] going to be sis dot dm db index physical
[20:16:24] states this one. And this is a function
[20:16:26] that we have to pass few parameters. We
[20:16:28] will not go in details just follow me
[20:16:30] with this. So we have to give it the DB
[20:16:32] ID and a null another null and a third
[20:16:37] null and the last one going to be
[20:16:39] limited. So we have to do it like this.
[20:16:41] So let's go and query it. Now what do we
[20:16:43] find? We have the object ID. We have the
[20:16:45] index ID and few other informations but
[20:16:48] the most important one is the average
[20:16:50] fragmentation in percent. So this
[20:16:52] columns gives us the degree of the
[20:16:54] fragmentations in a word index. If it is
[20:16:57] zero then it is perfect. We have no
[20:16:59] fragmentation in the index and our index
[20:17:02] is very healthy. But if it is like 100
[20:17:05] then that means it is completely out of
[20:17:07] order and we have to do something about
[20:17:09] it. And now you might say you know what
[20:17:11] I don't know which object it does and
[20:17:13] which index. Well you have to go and
[20:17:15] join few tables like the cy.ts and
[20:17:18] cis.index in order to get those
[20:17:20] informations. So we have to go and do
[20:17:22] that like we have done at the first
[20:17:23] query. So okay so offline I have done
[20:17:25] that. So I joined with the tables and
[20:17:27] the indexes and I'm sorting the data by
[20:17:30] the average fragmentations and
[20:17:32] percentage descending in order to get
[20:17:34] the problems at the start because we are
[20:17:36] interested where we have high
[20:17:37] percentage. So let's go and execute
[20:17:39] this. And now since it is practicing
[20:17:41] database I didn't insert any data and so
[20:17:44] on. But in real projects you will get
[20:17:45] here different numbers. And here is my
[20:17:48] recommendations about the percentage. If
[20:17:50] the fragmentation is between like zero
[20:17:52] and 10 that means everything is like
[20:17:54] okay and you don't have to do anything
[20:17:56] about it. But if the percentage is
[20:17:58] between like 10 and 30 then here we have
[20:18:01] to do something about it. So here I
[20:18:03] recommend to use the reorganize method
[20:18:05] in order to sort the data again
[20:18:07] correctly. But if you have more than 30%
[20:18:10] then here my recommendation is to go and
[20:18:12] rebuild the whole index because not only
[20:18:15] the data is in wrong order but as well
[20:18:18] there is a new spaces in your data page
[20:18:20] in the index. So you have to do
[20:18:21] something about it. So now let's go and
[20:18:23] imagine one of those indexes for example
[20:18:25] this one over here has fragmentation of
[20:18:28] 15%. So now what we have to do is to go
[20:18:30] and reorganize this index. Let's see how
[20:18:32] we can do that. So let's go over here
[20:18:35] and say the following. alter index and
[20:18:38] then we need the index name. So let's go
[20:18:40] and get it from here and then you have
[20:18:42] to mention the table name where the
[20:18:44] index exists. So we have it from the
[20:18:46] customers. So from sales customers so
[20:18:49] now we are editing the index and we have
[20:18:51] to tell SQL what to do now. So we just
[20:18:53] want to reorganize the index. So you go
[20:18:55] and use the keyword reorganize. So
[20:18:58] reorganize and that's it. This is very
[20:19:01] simple. So let's go and do that. And as
[20:19:03] you can see it is completed and it was
[20:19:05] very fast because we have small
[20:19:06] database. But sometimes it take little
[20:19:08] more time if you have a big index and
[20:19:10] big table. So after reorganizing you can
[20:19:12] go and again check the table over here
[20:19:14] and see the results and it should be
[20:19:17] like here is zero. Now let's see that we
[20:19:19] have another index where the
[20:19:20] fragmentation around like 50%. So let's
[20:19:23] go and copy it and this time instead of
[20:19:26] reorganize we're going to do rebuild. So
[20:19:29] I'm going to take the whole thing and
[20:19:32] this time we're going to go and rebuild
[20:19:34] this index over here on the same table
[20:19:36] and instead of reorganize we're going to
[20:19:38] say rebuild. So let's go and execute
[20:19:41] that. And with that SQL did drop the
[20:19:44] whole index and create it from the
[20:19:46] scratch. And this is usually takes more
[20:19:48] time than reorganize of course. And the
[20:19:51] next step of course is to go and check
[20:19:52] again the fragmentations and so on. So
[20:19:55] that's all about how to make your index
[20:19:57] healthy and remove the fragmentations
[20:20:00] from your index. All right, my friends.
[20:20:03] So as you can see, improving the
[20:20:04] performance of your queries doesn't end
[20:20:06] by creating them. It's all about staying
[20:20:09] proactive. So monitor the usage of the
[20:20:12] indexes, check whether there are any
[20:20:14] missing indexes, and always make sure
[20:20:16] the statistics of the database are up to
[20:20:19] date and keep your eyes on the
[20:20:20] fragmentations and make sure you have
[20:20:22] healthy indexes. So with that you have
[20:20:25] learned how I manage and monitor the
[20:20:27] indexes once I create them and I really
[20:20:29] recommend you to follow those
[20:20:34] steps. All right friends, so now let's
[20:20:36] say that you have a large complex
[20:20:38] analytical SQL query and it involves a
[20:20:42] lot of joins and aggregations and so on
[20:20:44] but it is slow and of course you want to
[20:20:47] go and optimize the performance of your
[20:20:48] query by maybe using indexes. And now
[20:20:51] the big question is where exactly I'm
[20:20:54] going to go build this index on which
[20:20:55] table on which columns. So that means
[20:20:58] you have to understand where exactly the
[20:20:59] problem is. Is it by joining tables or
[20:21:02] sorting data or by the aggregations? Now
[20:21:05] in order to answer all those questions
[20:21:08] we have something called execution plan.
[20:21:10] So what is that? The execution plan
[20:21:13] going to show you how the database
[20:21:15] exactly process your query step by step.
[20:21:19] And this is what we need. It's going to
[20:21:21] show us where exactly we have a
[20:21:23] performance issue. So in other words,
[20:21:25] the execution plan it's like your window
[20:21:28] on how the SQL database thinks and once
[20:21:31] you understand that then you're going to
[20:21:33] make a right decision on building an
[20:21:35] index. So let's understand exactly what
[20:21:37] this means. Okay. So now let's imagine
[20:21:39] that you are doing a query like
[20:21:41] selecting from table and then joining
[20:21:43] the data with another table. So now once
[20:21:45] you execute this query the database
[20:21:47] engine will not go immediately and start
[20:21:49] fetching data from the disk but instead
[20:21:51] of that first the SQL has to make a
[20:21:54] plan. So it's like you are planning a
[20:21:56] trip where you check the Google map in
[20:21:58] order to find the best route in order to
[20:22:01] reach the destination and the execution
[20:22:03] plan is exactly the same thing. The
[20:22:05] database has first to plan how to
[20:22:08] execute your query and it's going to
[20:22:09] build this plan step by step based on
[20:22:12] your query and as well the statistics.
[20:22:15] So the first step for example how to get
[20:22:17] the data from the tables and there are
[20:22:19] like multiple ways like scan index or
[20:22:22] full table scan and then after that it
[20:22:24] need to decide which type of joins going
[20:22:26] to be done like is it hash join or a
[20:22:28] loop join and then at the end of this
[20:22:30] plan it's going to be the select
[20:22:32] statements. So once the execution plan
[20:22:34] is ready the database engine going to
[20:22:36] start implementing the steps. So it's
[20:22:38] going to go and start reading your
[20:22:40] tables for example from the disk and
[20:22:41] then after that it's going to join the
[20:22:43] tables and then select the columns and
[20:22:45] send at the end the results to the end
[20:22:47] user. And now once everything is done
[20:22:50] the database engine going to do one more
[20:22:51] thing where it's going to go and take
[20:22:53] this execution plan and store it at the
[20:22:56] cache. And that's because the database
[20:22:58] engine can go and reuse this plan if we
[20:23:02] have a similar query. So for example, if
[20:23:04] you go and execute the same query again,
[20:23:06] the database engine here going to
[20:23:08] understand ah this is the same query. We
[20:23:10] have already built an execution plan for
[20:23:13] that. So it going to go and check the
[20:23:15] cache and it is way faster to get it
[20:23:17] immediately from the cache instead of
[20:23:19] building it. So in this scenario, the
[20:23:20] database engine doesn't have to make any
[20:23:22] decisions or something like that. going
[20:23:24] to go and get the plan from the cache
[20:23:26] and start immediately by executing the
[20:23:29] plan. And of course, the database engine
[20:23:31] will not hide the execution plan from
[20:23:33] the users. You can go and check it
[20:23:35] because you can go and check how the
[20:23:37] database loaded the data, how they are
[20:23:39] joined and so on. And then you can make
[20:23:41] a correct decision on how to optimize
[20:23:43] your query maybe by adding indexes. So
[20:23:46] let's go back to SQL and see how we can
[20:23:48] do that.
[20:23:52] Okay, so now we're going to work with
[20:23:54] the database Adventure Works DW2022. And
[20:23:57] now we're going to go to our tables and
[20:23:59] we're going to focus on the fact fact
[20:24:02] reseller sales. Now let's go and check
[20:24:04] the type of this table. So if you go
[20:24:06] inside it and go to the indexes, you can
[20:24:08] see that we have an index on the primary
[20:24:11] key. So we have a clustered roster
[20:24:14] index. So that means the data is
[20:24:16] structured in this P tree. So now what
[20:24:18] we're going to do, we're going to go and
[20:24:18] create a mirror of this table but
[20:24:20] without any indexes. So it's going to be
[20:24:23] very simple. Select star from our fact
[20:24:26] reseller sales and we're going to insert
[20:24:29] it in a new table. So
[20:24:32] into fact
[20:24:34] reseller sales and I'm going to call it
[20:24:37] HP for heap. So let's go and execute it.
[20:24:40] And now you can see we have inserted in
[20:24:41] the new table around 60,000 rows. So now
[20:24:44] we can go and refresh our tables in
[20:24:46] order to find our new table. So it is
[20:24:48] over here factory seller sales and if
[20:24:51] you check the indexes you will not find
[20:24:53] any. So that means it is a heap table.
[20:24:55] Now let's go and do a very simple query
[20:24:57] on top of our new table. So select star
[20:25:01] from the factory seller HP like this. So
[20:25:04] let's go and execute it and we got the
[20:25:07] results. So now the question is I would
[20:25:09] like to see the execution plan of this
[20:25:11] query. Now in order to see the execution
[20:25:13] plan we're going to go to the toolbar
[20:25:15] over here and we have three things. The
[20:25:18] first one is says display estimated
[20:25:21] execution plan and we have another one
[20:25:24] says include actual execution plan and a
[20:25:27] third one says include live query
[20:25:30] statistics. So now the question is what
[20:25:32] are the differences between them? Let's
[20:25:33] start with the first one displayed
[20:25:35] estimated execution plan. So here what's
[20:25:37] going to happen? SQL going to go and
[20:25:39] guess the execution plan without
[20:25:42] executing the query. So it's just an
[20:25:44] estimation. So this is only a guess an
[20:25:46] estimation. The second one is the actual
[20:25:49] one. So this going to show you the
[20:25:51] execution plan that is used in order to
[20:25:54] process your query. So after executing
[20:25:57] your query, SQL going to show for you
[20:25:59] which plan is used. So that means the
[20:26:01] estimated plan it is something before
[20:26:04] executing your query and the actual plan
[20:26:07] is something after executing your query.
[20:26:10] And the third one is while executing the
[20:26:13] query. So you're going to get a realtime
[20:26:15] execution of your query and you can see
[20:26:17] how your execution plan is working. So
[20:26:20] now we can go and try that. Let's go and
[20:26:22] activate the estimated execution plan.
[20:26:24] Now we can see over here we have a new
[20:26:26] output where you can see like few boxes.
[20:26:29] So this is an estimated execution plan
[20:26:31] without executing your query. But now if
[20:26:34] you go over here and switch it to the
[20:26:36] actual execution plan nothing going to
[20:26:38] happen because first you have to execute
[20:26:40] your query. So let's go and do that. So
[20:26:42] once we have executed we got the result
[20:26:45] the messages and here we have a new tab
[20:26:47] called execution plan. So if you go over
[20:26:49] here you will find the real execution
[20:26:52] plan that is used to process your query.
[20:26:54] And let's go and try the third one. And
[20:26:57] let's go and
[20:26:58] execute. It was pretty fast because the
[20:27:01] query is very fast. But here we can see
[20:27:03] how the data and the plan is working
[20:27:06] during the execution. So this is the
[20:27:08] live execution plan. And of course we
[20:27:10] have the last one which is the current
[20:27:13] execution plan. So those are the
[20:27:15] differences between those stuff. Now you
[20:27:17] might ask why do we have this estimated
[20:27:20] and actual execution plans? Well, it is
[20:27:23] really nice tool to understand whether
[20:27:25] everything like is healthy at your
[20:27:27] database because if the guessing is
[20:27:29] something else at the actual execution
[20:27:32] plan that means this is an indicator
[20:27:34] that something is wrong at the
[20:27:36] statistics or the index at your
[20:27:38] database. So if they are matching the
[20:27:40] estimated and the actual then everything
[20:27:42] looks good. But now we're going to focus
[20:27:44] only on one type of those execution
[20:27:46] plans. We're going to stick with the
[20:27:48] actual execution plan. So now what we're
[20:27:50] going to do, we're going to go and open
[20:27:52] two queries side by side and one going
[20:27:54] to be from the clustered index and
[20:27:56] another one is from the heap structure.
[20:27:59] So it's going to be like one to one.
[20:28:00] Let's go and query both of them. And now
[20:28:03] let's go and try to read the execution
[20:28:05] plan. But make sure that you are
[20:28:07] activating the actual execution plan. So
[20:28:10] we have here now two plans. So now we
[20:28:13] are at the he table and we don't have
[20:28:15] any indexes. So now the question is how
[20:28:17] to read this execution plan? Well, now
[20:28:20] the plan is very simple because we have
[20:28:21] a very simple query but we read it from
[20:28:24] the right to the left. So the first
[20:28:26] operation is the table scan and then we
[20:28:29] have here a very small arrow to the next
[20:28:31] one where we have the select. So from
[20:28:33] right to left. So now of course the
[20:28:35] first operator is how to read your data
[20:28:37] inside the table and here we have
[20:28:39] different types of scans and one of them
[20:28:41] is the table scan. So table scan
[20:28:43] actually is scanning the entire table.
[20:28:46] So it's going to go and scan all the
[20:28:48] rows inside your tables in order to
[20:28:50] execute this query. Now if you go and
[20:28:52] mouse hover on the table scan, you will
[20:28:55] find a lot of details about what is
[20:28:58] happening during loading the data or
[20:29:00] scanning the table. But it is little bit
[20:29:02] annoying better than that. If you go
[20:29:04] right click on it and then go to
[20:29:06] properties, you will get in the right
[20:29:08] side the same details but it is easier
[20:29:10] to read. So the first thing that we have
[20:29:12] to read is the number of rows that has
[20:29:15] been read. So we can see that we have
[20:29:17] read all the rows inside the table which
[20:29:19] is not really good and we have another
[20:29:21] important informations about the
[20:29:22] resources and the cost. So we have the
[20:29:24] CPU cost and the input output costs and
[20:29:27] what is interesting is the logical
[20:29:29] operator the table scan and we can see
[20:29:32] some nice informations about the
[20:29:33] storage. It says it is row store. Now
[20:29:35] let's go and check the execution plan of
[20:29:38] this other table where we have a
[20:29:40] clustered index. So let's go to the
[20:29:42] execution plan. And now you can see that
[20:29:44] we have on the right side something
[20:29:46] else. We don't have table scan. We have
[20:29:48] something called clustered index scan.
[20:29:51] It is either scanning the entire table
[20:29:53] again or only a range or a part of the
[20:29:57] index. And of course in the details we
[20:29:59] can see whether it read all the
[20:30:00] informations or not. Now if you go and
[20:30:03] check the number of rows again the whole
[20:30:05] index is read in order to get this
[20:30:07] results. So again we have here the total
[20:30:09] number of rows inside our table. And as
[20:30:11] well you can see over here the logical
[20:30:13] operation it is clustered index scan. So
[20:30:16] it is not table scan. Now of course we
[20:30:18] have to go and check the CPU and the
[20:30:20] input output costs whether we are
[20:30:21] consuming the same efforts or not. So we
[20:30:24] can go and compare stuff. So here we
[20:30:26] have like
[20:30:27] 0.07. And if you go over here you can
[20:30:30] see we didn't gain like a lot of
[20:30:32] information having an index on this
[20:30:34] table. And that's of course logical
[20:30:35] because this query is not using any
[20:30:38] indexes. It is just like selecting
[20:30:40] everything from the whole
[20:30:44] table. So now let's go and extend it
[20:30:47] where we're going to sort the data by
[20:30:48] the primary key sales order number. So
[20:30:53] let's go and get this one and as well
[20:30:55] for the heap structure. So let's go and
[20:30:58] execute it and check the execution plan
[20:31:01] and the same thing for our cluster
[20:31:04] table. Now let's check first the heap
[20:31:07] structure. As you can see here, we have
[20:31:08] like two steps. First, it's going to go
[20:31:11] and scan the whole table and then we
[20:31:14] have sort operator in order to go and
[20:31:17] sort all the data in order to present it
[20:31:19] in the output. And at the end, we have
[20:31:21] the select which is not really
[20:31:23] important. So here we have like two
[20:31:24] operators. But now if you go to our
[20:31:26] clustered index, you can see that we
[20:31:29] have only like two steps. There is no
[20:31:30] sort step, right? And that's because the
[20:31:33] clustered index is only sorted and SQL
[20:31:37] don't have to go and sort the data
[20:31:39] again. So it doesn't have to go and sort
[20:31:41] anything. The data is already sorted. So
[20:31:43] this is the first win that you have if
[20:31:46] you have an index. So everything is
[20:31:48] already sorted and if you have an order
[20:31:50] by on this column then SQL don't have to
[20:31:53] do it during the query. So now if you
[20:31:55] want to go and compare the cost you can
[20:31:56] see here we still have the same cost for
[20:31:58] the CPU and the input output in the h
[20:32:01] structure without any index we have here
[20:32:04] like double cost. The first cost is for
[20:32:06] the table scan. It is the exact same
[20:32:09] amount of CPU and input output like the
[20:32:12] clustered but as well on top of it we
[20:32:14] have high cost for sorting the data. So
[20:32:17] we are consuming more CPU and input
[20:32:19] output. And if you summarize those cost
[20:32:21] of course this query going to be slower
[20:32:24] and bad compared to the clustered index.
[20:32:28] So with that in the execution plan you
[20:32:30] can understand exactly the benefit of
[20:32:32] your index. And one more thing about
[20:32:34] this plan if you go over here. So if you
[20:32:36] go to the objects and let me just extend
[20:32:39] it like this. You can see the name of
[20:32:42] the index that has been used for your
[20:32:44] query. So it says the index is B key for
[20:32:48] primary key. And then we have the whole
[20:32:50] thing. So now if you go to our table on
[20:32:52] the left side, check the indexes, it
[20:32:54] going to be exactly this index. So in
[20:32:57] the execution plan you can find as well
[20:33:00] which index has been used in your query.
[20:33:02] And this is very important to check. If
[20:33:04] you create a new index then run your
[20:33:07] query and check whether the database is
[20:33:09] using your new created index. And if not
[20:33:12] then you are making the wrong decisions
[20:33:14] about your index. So each time you
[20:33:15] create a new index, make sure to check
[20:33:17] whether in the execution plan the
[20:33:19] database is using your new
[20:33:21] [Music]
[20:33:24] index. Okay, so now let's keep going.
[20:33:27] Now instead of using the primary key,
[20:33:29] I'm going to go and filter the data
[20:33:30] based on one of those columns that we
[20:33:32] have in this table. So let me check the
[20:33:34] results and let's take for example the
[20:33:37] carrier tracking number. So carrier
[20:33:40] tracking number and let's go and pick a
[20:33:43] value. the first one here like this and
[20:33:46] let's do the same thing for the heap
[20:33:48] table and execute it. And now in the
[20:33:51] execution plan you see we still have a
[20:33:53] table scan and on this table let's see
[20:33:56] the execution plan with the clustered
[20:33:58] index. Now let's say that I would like
[20:34:00] to go and create a nclustered index for
[20:34:03] this column. So let's go and do it. So
[20:34:05] create
[20:34:07] nonclustered index and I'm going to call
[20:34:09] it index fact reseller and then the
[20:34:13] column name. So on our table fact
[20:34:18] reseller and the column going to be
[20:34:20] carrier tracking number. So I'm going to
[20:34:22] take it from here and let's go and
[20:34:25] create it. Now let's see whether our
[20:34:27] query going to use this index. So let's
[20:34:29] go and execute it and let's go to the
[20:34:31] execution plan. Now things looks
[20:34:34] completely different than before. So
[20:34:36] what is going on? We can see that we
[20:34:38] have now something new. We don't have a
[20:34:40] clustered index. We have something
[20:34:42] called index seek. Index seek is an
[20:34:45] amazing sign in your execution plan
[20:34:47] because it tells us that SQL server did
[20:34:50] find a way to use the index in order to
[20:34:53] find the exact data that we need without
[20:34:55] scanning a lot of stuff. So that means
[20:34:58] now we have like three types of scans.
[20:35:00] We have the table scan where the SQL
[20:35:02] going to go and scan the whole table and
[20:35:04] this can happen in the heap structure
[20:35:06] and the second one we have the index
[20:35:09] scan and here we don't know whether it
[20:35:11] is scanning the whole index or a part of
[20:35:13] the index and the last one we have the
[20:35:16] index seek where the database is able to
[20:35:19] find directly the data without scanning
[20:35:21] a lot of stuff. So the worst type is the
[20:35:24] table scan. Then we have the index scan
[20:35:27] and the best one is the index seek. So
[20:35:30] if you check here the details you can
[20:35:32] see the number of rows that has been
[20:35:34] read is only 12. This is amazing. Let's
[20:35:37] go and check the heap scan over here. So
[20:35:40] to the execution plan and if you go over
[20:35:42] here you can see that we are reading
[20:35:45] around 60,000 rows in order to get 12.
[20:35:49] But with the index we are reading only
[20:35:52] 12 in order to get 12 and this is
[20:35:54] amazing and very fast of course and of
[20:35:57] course the cost of this is very very
[20:35:59] small. So if you check the CPU and the
[20:36:01] input output you can see those numbers
[20:36:03] are nothing and of course if you go to
[20:36:05] the object over here you can see which
[20:36:07] index has been used and this is exactly
[20:36:09] the index that we have just created. So
[20:36:12] that means it was a really good decision
[20:36:15] to create this index and the SQL was
[20:36:17] very happy about it and used it in order
[20:36:19] to fast find our data. So now let's go
[20:36:22] and check the rest of the plan. And now
[20:36:24] you can see over here we have key
[20:36:25] lookup. The key lookup is an operation
[20:36:28] that we need in order to get the rest of
[20:36:30] the columns because from this index we
[20:36:33] are getting the data of only one column
[20:36:35] the carrier tracking number. But since
[20:36:37] in our query we are saying select star
[20:36:40] that means we have a lot of columns and
[20:36:42] those columns are not part of the index.
[20:36:44] So in this index is called don't know
[20:36:46] anything about the rest. That's why has
[20:36:49] to go and search for the other columns
[20:36:52] and of course it is called a lookup not
[20:36:54] a scan or something like that and that's
[20:36:56] why we have here as well only 12 rows
[20:36:59] but from this step we will get the rest
[20:37:02] of the columns. So and now the next step
[20:37:05] is that SQL going to go and join those
[20:37:07] two informations. So we have from the
[20:37:10] first one the carrier tracking number
[20:37:12] and the second one we have the rest of
[20:37:14] course SQL has to go and merge all those
[20:37:16] stuff in one in order to have it as a
[20:37:18] results. And now this operation called a
[20:37:20] nested loops. Behind the scenes there
[20:37:23] are different types of joins not the one
[20:37:25] that we know the inner lift and so on
[20:37:27] but there is another types of joints. We
[20:37:29] have the nested loop. We have the merge
[20:37:32] join and the hash join. The nested loop
[20:37:34] is very good for small stuff. If you
[20:37:37] have large tables, then the merge and
[20:37:39] the hash joints are way better than the
[20:37:42] nested loop. So that means if you are
[20:37:44] getting here a lot of data from the
[20:37:46] index and the lookups and you seek is
[20:37:49] using a nested loop, this is not good.
[20:37:52] But for now it is okay because we are
[20:37:54] getting only 12 rows and the operation
[20:37:56] going to be fast enough. And now one
[20:37:58] more thing that we can see inside our
[20:37:59] execution plan is the cost in
[20:38:01] percentage. So from checking this plan
[20:38:03] you can see the select is almost costing
[20:38:06] nothing. The cost of the nested loop is
[20:38:09] as well like 0%. And then we have like
[20:38:11] 6% of the index seek. That's because it
[20:38:14] is pretty fast and the most expensive
[20:38:17] operation that done in our query is the
[20:38:20] key lookups of course because it's going
[20:38:22] to go and get all the columns. And now
[20:38:25] if you go and compare to the heap
[20:38:27] structure even though that the execution
[20:38:29] plan of the heap structure looks very
[20:38:31] small doesn't mean that is faster than
[20:38:34] the indexes that we have. Still if you
[20:38:36] go and add up all those numbers it is
[20:38:39] way way faster than the heap structure.
[20:38:42] Now I would like to show you one more
[20:38:43] thing. If you want to get rid of this
[20:38:45] key lookup and in your query you have
[20:38:48] only selecting the carrier tracking
[20:38:50] number. Let's go and execute it and go
[20:38:52] to the execution plan. As you can see
[20:38:54] there is no need for the lookup because
[20:38:57] we have only one column and this data we
[20:38:59] can get it completely from our index. So
[20:39:02] as you can see it is interesting to
[20:39:04] understand how SQL is working with your
[20:39:06] table and with your index and this is
[20:39:08] how to validate whether you are making
[20:39:11] correct decisions about your
[20:39:16] indexes. Okay. So now let's go and add
[20:39:18] more stuff where we are doing
[20:39:20] aggregations joins and so on. Let's
[20:39:22] extend our query. So I'm going to go and
[20:39:24] join it with another dimension like for
[20:39:26] example the dim products and the join
[20:39:31] going to be on the product key. So
[20:39:33] product key and equal to as well product
[20:39:37] key. Now after that we're going to go
[20:39:39] and aggregate few stuff. So we're going
[20:39:40] to aggregate by the product name. So I'm
[20:39:43] going to take the product name. So it's
[20:39:45] going to be the English product name and
[20:39:48] let's go and call it product name. And
[20:39:51] let's go and aggregate the sales. So sum
[20:39:55] and we're going to get it from the fact
[20:39:56] table. It's going to be sales amount. So
[20:40:00] as
[20:40:01] total sales and of course we have to go
[20:40:04] and do group by and not French name.
[20:40:08] It's going to be the English
[20:40:09] name. So let's group up by the product
[20:40:13] name. And that's it. Let's go and
[20:40:15] execute it. Now we have a nice list of
[20:40:17] products and total sales. But let's go
[20:40:19] and check the execution plan. And oh my
[20:40:22] god, we have a lot of stuff. So let's
[20:40:24] start from the right side. So let's do
[20:40:27] it quickly from the right to the left.
[20:40:28] So the first thing is that it's going to
[20:40:30] go and get the data from the fact. So it
[20:40:33] is using the clustered index. And then
[20:40:35] after that it's going to go and do a
[20:40:37] hashmatch for the aggregation. And after
[20:40:40] that it's going to go and sort the data
[20:40:42] because it is doing later a merge join.
[20:40:45] So all those steps are preparing the
[20:40:47] fact table. And then we have another
[20:40:49] cluster scan for the dimension. So it
[20:40:52] going to go and as well select the
[20:40:54] informations from the dimension. And we
[20:40:55] have here like not a lot of rows. So it
[20:40:58] is very small table 600 rows. And now of
[20:41:00] course the result of the cluster scan is
[20:41:03] as well sorted right and of course as we
[20:41:05] learned the cluster the index going to
[20:41:06] go and sort the data. So we have here a
[20:41:09] sorted output together with another
[20:41:11] sorted output. So we have like two data
[20:41:14] sets that are sorted and SQL here
[20:41:16] decided to go with the merge join which
[20:41:18] is a good join in order to join two
[20:41:20] sorted data sets. It is way faster than
[20:41:23] joining using the nested loop. So
[20:41:25] everything is fine and then the data
[20:41:27] going to be sorted and presented at the
[20:41:29] output. And now if you are checking this
[20:41:31] plan you can see the most expensive
[20:41:33] thing happened at the fact table. So
[20:41:36] 71% of the total cost happened in this
[20:41:39] step. Now let's say that the query is
[20:41:41] slow and I would like to go and optimize
[20:41:43] it. We have learned that if you are
[20:41:45] doing aggregations on big tables then
[20:41:48] the column store index is a good idea.
[20:41:52] So let's go and find whether that is
[20:41:54] true. So I'm going to go to our other
[20:41:56] table. So our sales table was with the
[20:41:59] heap structure. And now you say you know
[20:42:01] what let's go and convert this he
[20:42:04] structure to a column store. So let's go
[20:42:06] and do that. So we're going to say
[20:42:07] create
[20:42:09] clustered column store index and we're
[20:42:13] going to call it index and then the
[20:42:14] whole name fact
[20:42:17] reseller sales HP and we don't have to
[20:42:20] specify any columns. So it's going to be
[20:42:23] our table on and that's it. Let's go and
[20:42:26] execute it. So now our table is not
[20:42:29] anymore heap structure. It should be a
[20:42:32] column store. So if you go and check the
[20:42:34] informations we can see we have like
[20:42:36] clustered column stored index on it. So
[20:42:39] now let's go and do the same query and
[20:42:41] check whether we have a better
[20:42:42] performance. Let's go and execute it.
[20:42:44] And of course you have to go and
[20:42:45] activate the execution plan. So I'm
[20:42:47] going to and now let's go and check from
[20:42:50] the right again. So this is our fact
[20:42:53] table and as you can see already it is
[20:42:56] costing only 6%. Interesting. So let's
[20:42:59] go and compare what happened to our fact
[20:43:00] table. First of all, we can see that the
[20:43:02] physical operation is a column store
[20:43:05] index scan. And if you go to the objects
[20:43:07] over here, you can see that the SQL did
[20:43:10] use the column store. And that is of
[20:43:12] course going to happen because the whole
[20:43:13] data is stored only in the index. So
[20:43:16] there is no way around it. So it can go
[20:43:18] and of course and use the index. But now
[20:43:19] what is interesting maybe we have to go
[20:43:21] and compare the CPU costs. So if we
[20:43:24] check over here, it is like
[20:43:27] 0,000.67 almost the same thing for the
[20:43:29] input output. Let's go to the previous
[20:43:32] plan where we don't have a column store
[20:43:34] and check our facts. So as you can see
[20:43:36] here it is way more expensive reading
[20:43:39] the fact table than the column store and
[20:43:42] as well we have reduced the input output
[20:43:44] costs. So as you can see we went from
[20:43:46] 71% of total cost for the fact table to
[20:43:51] only 6%. And the resources that is used
[20:43:54] to execute the query it is way less than
[20:43:58] a normal clustered res store. And this
[20:44:00] is exactly the power of this index, the
[20:44:02] column store index. You can use it in
[20:44:05] big tables like the fact tables like we
[20:44:07] are doing here in this query, you will
[20:44:09] be getting amazing performance for this
[20:44:12] scenario. So of course you can go and
[20:44:14] compare the execution plan by moving
[20:44:16] left and right. So as you can see if I
[20:44:18] click over here and I just switch to the
[20:44:20] other tab, I can like quickly compare
[20:44:22] the numbers. But there is another way on
[20:44:24] how to compare execution plans and that
[20:44:27] is if you go to the execution plan and
[20:44:29] right click on it then go to save
[20:44:31] execution plan as and then you have to
[20:44:34] go and give it a name for example query
[20:44:36] pro store. So let's go and save it and
[20:44:40] then you can go to the second query
[20:44:42] where we have the row store and then
[20:44:43] right click on the execution plan and
[20:44:46] say compare show plan. So once you click
[20:44:49] on that then you have to go and select
[20:44:51] the one that you want to compare with.
[20:44:53] So open and now on top you have your
[20:44:56] query and at the bottom you have the
[20:44:58] execution plan that you have saved and
[20:45:00] then you have here a lot of informations
[20:45:03] where they compare both of the execution
[20:45:06] plan and with that you can go in more
[20:45:08] details in order to understand which
[20:45:10] plan is better. All right friends so as
[20:45:12] you can see having the execution plan is
[20:45:14] is amazing. We can see how the SQL is
[20:45:17] working behind the scenes and we can
[20:45:19] understand how SQL is processing my
[20:45:21] query step by step. How much resources
[20:45:24] it is consuming, whether my indexes are
[20:45:26] useful or useless and I can go and
[20:45:29] experiment stuff. I can go and add like
[20:45:31] an index then test and check whether I
[20:45:33] gained like few performance or not. And
[20:45:35] we can go and compare like multiple
[20:45:37] execution plans before and after until
[20:45:40] you get the right index for the right
[20:45:42] table and the right column. So the
[20:45:44] execution plan are amazing in order to
[20:45:47] help us understanding whether our
[20:45:50] indexing strategy is correct or
[20:45:56] not. All right friends, so so far we
[20:45:58] have learned that the SQL server going
[20:46:00] to make its own decisions on how to
[20:46:03] execute your queries and the SQL make
[20:46:05] those plans based on the statistics. But
[20:46:07] sometimes the plan that you are getting
[20:46:09] from the database might be not the best
[20:46:12] one for your query and there could be
[20:46:14] many reasons why this could happen.
[20:46:16] Maybe the statistics are not up to date
[20:46:19] or you have a lot of indexes and the
[20:46:21] database engine get confused and here
[20:46:24] exactly where we need the SQL hints. So
[20:46:27] you can use the SQL hints in order to
[20:46:30] command to force the SQL database on how
[20:46:34] exactly your SQL query should be
[20:46:36] executed. So you can intervene and
[20:46:38] change the steps in the execution plan.
[20:46:41] So let's see how we can do that. All
[20:46:43] right. So now let's have a very simple
[20:46:45] query. We are just joining the table
[20:46:46] orders with the customers and we are
[20:46:48] showing like few columns. Now if you go
[20:46:50] and execute it and we go and check the
[20:46:53] execution plan, we can see in this plan
[20:46:55] that it is using the clustered index in
[20:46:57] order to read the data from the orders
[20:46:59] and the customers and then it is using
[20:47:01] the nested loop in order to do the
[20:47:03] joins. Now let's say that our tables are
[20:47:06] really big but still the SQL is using
[20:47:08] the nested loops and of course this is
[20:47:10] not good for large tables and maybe the
[20:47:13] SQL was confused with the indexes and
[20:47:15] statistics and so on and it decided to
[20:47:18] use the nested loops. So now in order to
[20:47:20] force the SQL to use another type of
[20:47:23] join, we can go and give a hint in our
[20:47:25] query for the SQL to use different types
[20:47:28] for the join. So let's go and do that.
[20:47:31] We're going to go at the end of our
[20:47:32] query and we're going to say option and
[20:47:36] inside it we're going to say use the
[20:47:38] hash join like this. So that's it. This
[20:47:42] is our query and at the end we are
[20:47:44] giving the database a hint for the
[20:47:46] execution plan. So let's go and try that
[20:47:48] out. So let's check the execution plan.
[20:47:50] And now as you can see is using
[20:47:53] different type of join. So with that we
[20:47:55] are intervening in the execution plan
[20:47:58] and we are making choices. So with that
[20:48:00] we have changed the technicality on how
[20:48:03] the SQL is joining those two tables. All
[20:48:05] right. So now let's go and change
[20:48:07] something else like for example instead
[20:48:08] of having index scan I would like to
[20:48:12] have an index seek. So if you have the
[20:48:14] right index in your table, you can go
[20:48:16] and tell SQL how to read your data in
[20:48:20] the table. So let's go and do that.
[20:48:22] Currently here we have an index scan on
[20:48:25] the table customers. So we can go over
[20:48:27] here near the table and we're going to
[20:48:29] say with and inside it we're going to
[20:48:31] say for SQL force seek. So we are
[20:48:34] forcing SQL to use the seek index. So we
[20:48:37] can use those keywords near the table in
[20:48:41] order to specify for SQL how to load the
[20:48:43] data. If you are not specifying anything
[20:48:45] like here with the orders, we don't have
[20:48:47] here any hints. That means we are
[20:48:49] counting on the execution plan that is
[20:48:52] generated from the SQL. But if you don't
[20:48:55] want the recommendations, you can go and
[20:48:57] specify which one should be used. So now
[20:49:00] let's go and execute it. Now we got an
[20:49:02] error because the SQL is not able to
[20:49:05] process what we are asking for and I
[20:49:08] think maybe we are using the force
[20:49:09] command and as well the hash join. Let
[20:49:11] me just uncomment this and let's go and
[20:49:13] give it another try and now it is
[20:49:16] working. So let's go to the execution
[20:49:17] plan. So you can see we got again the
[20:49:19] nested loop. And now if you go to the
[20:49:21] customers table you can see now it is
[20:49:24] using the index seek. So it is not using
[20:49:26] anymore the index scan. So as you can
[20:49:29] see again we are intervening and forcing
[20:49:31] SQL to use the method that might be
[20:49:34] better for our query. Now if you are
[20:49:36] creating a lot of indexes in one table
[20:49:38] and the SQL is still not targeting the
[20:49:41] right index. So if you check the object
[20:49:43] you can see it is targeting specific
[20:49:45] index. But if you have a better index
[20:49:48] than that you can give a hint for the
[20:49:50] SQL to use a specific index. And we can
[20:49:53] do that like this. If you go over here
[20:49:56] and remove the force seek and you say
[20:49:58] use index and then we have to go and
[20:50:01] specify the index name. So let's go and
[20:50:04] get again the primary key over here. Now
[20:50:06] I'm telling SQL you have to go and use
[20:50:08] this index in order to scan the table
[20:50:11] customers. So let's go and try this out.
[20:50:13] And if you go to the execution plan you
[20:50:16] can see it is as well targeting this
[20:50:18] index. So not only you can force SQL for
[20:50:21] a specific type of loading or joining,
[20:50:25] you can force SQL to use a specific
[20:50:27] index that you created. All right
[20:50:29] friends, so as you can see, SQL hands
[20:50:31] are very powerful, but we have to be
[20:50:33] very careful with them because I really
[20:50:35] had a bad experience using them in my
[20:50:38] projects. So here are my recommendations
[20:50:40] and what happens. So what could happen
[20:50:41] is that you are optimizing the
[20:50:43] performance in the development database
[20:50:46] and you start using the hints and the
[20:50:48] speed was really good and once you roll
[20:50:50] that out to another database the
[20:50:53] production database this hint will not
[20:50:56] be working correctly. The same hint that
[20:50:58] you are using might not improve the
[20:51:00] performance and one reason is that
[20:51:02] sometimes the productive database has
[20:51:05] like large data compared to the
[20:51:07] development database. So you have really
[20:51:09] to test the hint in each database that
[20:51:12] you have. So if your hint is working in
[20:51:15] one environment that doesn't mean it
[20:51:17] going to work in the other one. So
[20:51:19] always make sure to test. And the second
[20:51:21] recommendation is that don't use the
[20:51:23] hint as a permanent fix for your
[20:51:25] queries. So what this means? Let's say
[20:51:27] that you are working in the project and
[20:51:29] one of your queries are very slow. Now,
[20:51:31] if it's not clear why the execution plan
[20:51:33] is really bad, you can go and use the
[20:51:36] hints as a workaround in order to speed
[20:51:38] up your query again, but it's still as a
[20:51:41] workaround temporary. You still have to
[20:51:43] invest and spend time in order to
[20:51:45] analyze what was the road cause. So
[20:51:48] maybe it is an old statistics or you
[20:51:50] have wrong indexing and so on. So use
[20:51:52] hints only to work around and speed up
[20:51:55] your queries, but don't use it as a
[20:51:57] permanent fix. So friends, SQL hints are
[20:52:00] really amazing in order to control the
[20:52:02] execution plan, but use it very
[20:52:04] carefully and only if there is like an
[20:52:06] emergency.
[20:52:11] All right friends, so now for each SQL
[20:52:14] data project, we have to make sure that
[20:52:16] we create a clear guidance about the
[20:52:19] index strategy and everyone in the team
[20:52:22] has to commit and follow the strategy in
[20:52:24] order to make sure that each index that
[20:52:27] is created in the project to fulfill a
[20:52:29] purpose and that's because without a
[20:52:31] clear strategy about the indexing, I'm
[20:52:33] going to promise you there will be a lot
[20:52:35] of redundancy, unused indexes, uh waste
[20:52:38] of storage and the whole system of your
[20:52:41] project is going to be slow and bad. So
[20:52:43] now what we're going to do, I'm going to
[20:52:44] show you my indexing strategy that I
[20:52:46] usually follow in my projects. But I'm
[20:52:48] going to tell you from now there is like
[20:52:49] not one strategy that can fit any
[20:52:52] project and any scenario. That's why the
[20:52:54] team of each project should brainstorm
[20:52:56] in order to make their own strategy. So
[20:52:59] now let's have a look to my indexing
[20:53:01] strategy.
[20:53:05] And now if I have to pick only one
[20:53:07] recommendation from me to you in this
[20:53:10] indexing tutorial, I'm going to have
[20:53:12] this advice for you. Avoid overindexing.
[20:53:15] Overindexing is the biggest mistake and
[20:53:18] trap that a lot of developers do where
[20:53:21] they think adding more indexes. That
[20:53:23] sounds like we are speeding up things
[20:53:25] and our queries can be fast. But I have
[20:53:28] to tell you this exactly lead to the
[20:53:30] opposite. And here's why. As we learned,
[20:53:33] each time you add a new data to your
[20:53:35] table, your index has to get updated,
[20:53:37] sorted, rearranged. That means having
[20:53:40] too many indexes, what's going to
[20:53:42] happen? Your insert, update, delete
[20:53:44] operations going to be slow. And this
[20:53:46] means your database is slower and not
[20:53:49] faster. And one more very important
[20:53:51] reason why overindexing is bad is you
[20:53:54] make the database confused while
[20:53:56] creating the execution plan. As we
[20:53:58] learned, the SQL database has to create
[20:54:01] the best execution plan for your query.
[20:54:04] And if you have a lot of indexes in your
[20:54:06] database, it's going to make the process
[20:54:08] of creating an execution plan
[20:54:10] complicated for the database, which
[20:54:12] makes it of course for database harder
[20:54:14] to choose the best path and index. And
[20:54:17] as well, you open the door for bad
[20:54:19] execution plans. And this means it's
[20:54:21] going to slow the query because first
[20:54:23] the database has to create the execution
[20:54:25] plan before executing your query. So
[20:54:27] again it has a bad effect for the
[20:54:29] performance and as well there is another
[20:54:31] bad thing. It can make it harder for the
[20:54:33] database to decide what is the best
[20:54:36] execution plan for a query and having
[20:54:38] too many indexes might make the SQL
[20:54:41] database choosing a really bad execution
[20:54:44] plan. So overindexing confuse the
[20:54:46] execution plan and as well makes the
[20:54:49] query slower. So that's why I call this
[20:54:51] a golden rule and you have to commit to
[20:54:53] it. Just avoid overindexing because it
[20:54:56] is double-edged sword and exactly you
[20:54:58] have to have the mindset of less is
[20:55:01] more. So having a few effective indexes
[20:55:04] is way better than having a lot of
[20:55:06] indexes. So keep it in mind and write it
[20:55:09] in your development guideline for the
[20:55:11] team with big statement avoid
[20:55:13] overindexing. So this is the first
[20:55:15] statement in your indexing strategy. So
[20:55:17] now let's check the
[20:55:18] [Music]
[20:55:21] rest. All right. So now we can split the
[20:55:24] indexing strategy into four phases and
[20:55:27] each phase has multiple steps. So now
[20:55:29] the first step is we're going to go and
[20:55:30] create an initial indexing strategy. So
[20:55:33] now once you start a new SQL project you
[20:55:36] have to define the objectives of the
[20:55:38] projects very clearly. So that means we
[20:55:40] have to make it clear what we are
[20:55:41] focusing on what we want to achieve and
[20:55:43] in order to define the goal of your
[20:55:46] indexing strategy you have to understand
[20:55:48] your system. We have mainly two types of
[20:55:51] databases. In one hand we have OLAB
[20:55:54] databases. It stands for online
[20:55:56] analytical processing. The purpose of
[20:55:58] this database is for data analytics and
[20:56:00] an example for that is the data
[20:56:02] warehouse. So in data warehousing we go
[20:56:05] and extract the data from multiple
[20:56:07] sources and then we prepare it and
[20:56:09] transform it and put it in one big
[20:56:11] storage and we call this process an ETL
[20:56:13] process. And then the front end we have
[20:56:16] like reports and dashboards where the
[20:56:18] data is summarized and aggregated and
[20:56:20] presented for the end user. And these
[20:56:22] reports could be used from users in
[20:56:24] order to analyze and have insights about
[20:56:26] the data. And now in order to generate
[20:56:28] those reports there will be like heavy
[20:56:31] reading on the data warehouse database.
[20:56:34] So that means there will be huge queries
[20:56:36] that's going to access the database in
[20:56:38] order to aggregate and prepare the data
[20:56:40] for the visualization. But now in the
[20:56:42] other hand we have the OLTP systems
[20:56:45] online transactional processing. It is
[20:56:47] like an e-commerce finance banking where
[20:56:50] you have at the back end a database
[20:56:52] where the data is stored and on the
[20:56:54] front end we have like an applications
[20:56:56] for the end users. So now as the users
[20:56:58] are interacting with the app this can
[20:57:00] cause write operations on the database.
[20:57:03] So inserting new data or changing data
[20:57:05] and as well there will be read
[20:57:07] operations on the database in order to
[20:57:09] show the data in the app. So we have
[20:57:11] both write and read. So now of course we
[20:57:13] have to ask ourself what is the goal
[20:57:15] what do we want to achieve and here
[20:57:17] mainly there is like two strategy either
[20:57:19] you want to improve the read performance
[20:57:22] or the right performance. Now if you are
[20:57:24] looking to the OLAP system here it's
[20:57:26] really you have to understand the
[20:57:27] project where is the struggle sometimes
[20:57:29] it could be like the ATL process itself
[20:57:32] it's slow and mainly the ATL is writing
[20:57:34] data from the sources in the data
[20:57:36] warehouse and maybe you have scenario
[20:57:38] where it takes like every day 10 hours
[20:57:40] and 10 hours is of course a problem
[20:57:42] because you cannot wait so long in order
[20:57:45] to get a new data fresh data to the
[20:57:47] report every day. So you can make the
[20:57:49] goal of the project is to optimize the
[20:57:52] right performance. You want to speed up
[20:57:54] the ETL. But actually most of those
[20:57:56] projects having another issue. Well, it
[20:57:59] is the read operation on the database
[20:58:01] because data warehouses normally have
[20:58:03] really big data sets and at the front
[20:58:06] end the reports generate large complex
[20:58:08] queries on the database. So that means
[20:58:11] the rate process going to be the pain
[20:58:13] point in each OLAP system. So normally
[20:58:16] the big goal in each OLAP system going
[20:58:18] to be how to optimize the read
[20:58:21] performance. But now in the right hand
[20:58:23] with the OLTB we have different nature
[20:58:25] of database and scenario. What going to
[20:58:28] happen? You will not have like big
[20:58:29] queries from the apps. You're going to
[20:58:31] have like many query many transactions
[20:58:34] happening between the application and
[20:58:36] the database. So you're going to have
[20:58:38] like massive amount of read and write
[20:58:41] transactions. So the whole time we are
[20:58:43] reading, writing, reading, writing and
[20:58:45] so on. But with the OL app we have like
[20:58:47] something bigger and slower because in
[20:58:49] the ATL we usually run it only once.
[20:58:52] That means we are writing only once new
[20:58:54] data to the database and this happen
[20:58:56] usually at the night but on the
[20:58:58] transactional systems you have a lot of
[20:59:00] readrs all time. Again depend on the
[20:59:02] project but usually the main pain point
[20:59:05] in the OLTP is the right operation. So
[20:59:08] it could be like this. If you are
[20:59:10] building OTP system, the main goal is to
[20:59:13] optimize the right performance. Now of
[20:59:15] course the question is how to do that?
[20:59:17] How we going to optimize that? Well,
[20:59:19] again we have to understand the nature
[20:59:20] of the database. What do we have in the
[20:59:22] OLAP systems is usually like a data
[20:59:25] model where you have a very big fact
[20:59:28] tables and around the fact we have like
[20:59:30] multiple dimensions that are connected
[20:59:32] to the facts. So those fact tables are
[20:59:34] really big tables in the database and
[20:59:37] each time they are used in order to
[20:59:39] build a report and the report going to
[20:59:41] be using all time those facts in order
[20:59:44] to prepare the data for the
[20:59:45] visualizations and a lot of aggregations
[20:59:48] query going to be done on the facts and
[20:59:50] now of course you have to answer now the
[20:59:52] question which type of index should we
[20:59:54] use in this scenario. Well we have a
[20:59:56] perfect one called a column store index.
[20:59:59] So the best practice here is and you can
[21:00:01] make it as a strategy for the whole
[21:00:03] project that we make all fact tables as
[21:00:06] a column store index because this is
[21:00:08] what we are doing in the OLAP. We are
[21:00:10] aggregating large data sets but now the
[21:00:13] data model and the scenario is
[21:00:15] completely different at the right side
[21:00:16] here. We're going to have like a lot of
[21:00:18] tables and they have like different
[21:00:20] sizes and so on and there are like a lot
[21:00:23] of relationship between all those
[21:00:25] tables. So it is completely connected.
[21:00:27] So you have a lot of like primary keys
[21:00:29] and foreign keys relationships between
[21:00:31] them and normally those tables are
[21:00:33] completely normalized table. So they are
[21:00:36] like small pieces but on the left side
[21:00:38] we have denormalized tables as a facts.
[21:00:41] So here is like one strategy that we can
[21:00:43] follow in the indexing of the ALTB is
[21:00:45] that we create clustered index for each
[21:00:48] primary key of our tables. This of
[21:00:51] course can improve a lot of stuff like
[21:00:52] searching, sorting and as well joining
[21:00:55] tables together. But of course since we
[21:00:57] are focusing on optimizing the right
[21:00:59] performance on the OLTP you have to be
[21:01:01] more sensitive by adding new indexes
[21:01:04] compared to the OLAP because each index
[21:01:06] you add it could be a reason why the
[21:01:09] data is written very slowly. So in the
[21:01:12] OLTB you have to be way more careful
[21:01:14] adding indexes. So now as you can see
[21:01:16] you have to understand the nature of
[21:01:18] your project. You have to understand
[21:01:20] what is the main issue. Once you
[21:01:22] understand your project, you can go and
[21:01:24] define like a goal for optimizing the
[21:01:27] system. So either read or write or maybe
[21:01:30] both of them and with that you are
[21:01:32] making like the initial strategy of
[21:01:34] indexing your
[21:01:35] [Music]
[21:01:39] system. All right. So with that we have
[21:01:41] an initial strategy for our indexing and
[21:01:44] we have a rough plan. Now in the next
[21:01:47] phase we have usage patterns indexing.
[21:01:50] So now we're going to do a deep dive
[21:01:51] into our project. And the first thing
[21:01:53] that we have to do is that we have to
[21:01:55] identify the frequently used tables and
[21:01:57] columns. So that means you have to go
[21:01:59] and check the queries used in your
[21:02:01] project in order to understand okay what
[21:02:03] is the most important table that is used
[21:02:06] in many queries. Like for example here
[21:02:08] we have the fact internet sales. It is
[21:02:11] used like in many many queries in our
[21:02:13] scripts. So here you are like developing
[21:02:15] a feeling about what are the most
[21:02:18] important frequently used tables and not
[21:02:20] only that you can go and check how we
[21:02:22] are filtering the data on those queries.
[21:02:24] So for example we have over here we are
[21:02:26] filtering by the order date key is this
[21:02:29] kind of filtering is used like in
[21:02:31] multiple queries. So as you can see we
[21:02:32] have like here a couple of queries where
[21:02:34] we are doing always the same where we
[21:02:36] are filtering the data by the dates. So
[21:02:39] with that we understand there is like a
[21:02:40] pattern inside our projects where this
[21:02:43] column is used mainly on filtering and
[21:02:45] as well for aggregating. So that means
[21:02:47] you do a deep dive in order to
[21:02:49] understand what are the most and
[21:02:51] frequently used tables and columns
[21:02:53] inside your scripts. And now of course
[21:02:55] what I usually do I go and use the help
[21:02:57] of the AI and IBT where I give it my
[21:02:59] code and then ask questions about it.
[21:03:02] For example, this prompt, it says, "Anal
[21:03:03] analyze the following SQL queries and
[21:03:05] generate a report on table and column
[21:03:07] usage statistics. And for each table,
[21:03:09] provide the total number of times the
[21:03:11] table is used across all queries. A
[21:03:14] breakdown for each column in the table
[21:03:16] showing the number of times each column
[21:03:18] appears. And I would like to see as well
[21:03:20] the primary usage of each column,
[21:03:22] filtering, joining, grouping, and so on.
[21:03:24] And in the output, as you can see, we
[21:03:25] got like nice statistics about my
[21:03:27] scripts. So as you can see the most used
[21:03:30] fact table is fact internet sales. It is
[21:03:33] like 13 times used in the projects and
[21:03:35] then we can see like statistics about
[21:03:37] each column that is inside these facts.
[21:03:39] So most of the time is the sales is used
[21:03:42] for aggregating and as we saw the order
[21:03:45] date key is used like five times for
[21:03:47] filtering and the other keys is used for
[21:03:50] joining tables. So as you can see it's
[21:03:52] amazing right now we can identify which
[21:03:55] tables are important which columns as
[21:03:57] well are important and we can like based
[21:03:59] on those informations maybe derive our
[21:04:02] indexing for our database. So with that
[21:04:04] we have identified our frequently used
[21:04:06] tables and columns and now the next step
[21:04:09] we have to go and choose the right index
[21:04:12] type and as we learned before we have
[21:04:14] multiple types of indexes and that's
[21:04:16] really depend on the usage and the
[21:04:18] scenario. So for examples, if your
[21:04:20] columns are primary keys, then go with
[21:04:21] the clustered index. And if you are
[21:04:23] using columns that are not primary key
[21:04:25] where you are doing joining filtering,
[21:04:27] then think about the non-clustered
[21:04:29] index. And of course, if the table is
[21:04:31] very big, as we said, you can go and use
[21:04:32] the column store index. And if you are
[21:04:34] targeting always like a subset of data
[21:04:37] only like one year informations, then
[21:04:39] you can think about the filtered index.
[21:04:41] And the last one, if you have like a
[21:04:43] unique column where you don't have any
[21:04:44] duplicates, then you can go and apply a
[21:04:46] unique index. So it depends on the
[21:04:48] scenario and the usages. You have to
[21:04:50] choose the right index. And of course
[21:04:52] the last step in this phase is that you
[21:04:54] have to go and test your index whether
[21:04:56] everything is working
[21:05:01] fine. So that's all for the phase two.
[21:05:04] Then we go to phase three scenario-based
[21:05:06] indexing. So here we have to tackle and
[21:05:08] focus on specific issues to specific
[21:05:12] pain points. So that means we have first
[21:05:14] to identify the slow queries. So it
[21:05:17] could be reported from users or the team
[21:05:19] is doing like analyzing on the logs and
[21:05:22] to understand which queries are causing
[21:05:24] like performance issues. And now once
[21:05:27] you get a list of slow queries then you
[21:05:29] have to analyze them one by one and it
[21:05:32] is time to dig into the execution plans.
[21:05:35] So as we learn we can check how SQL is
[21:05:37] implementing our queries and start
[21:05:39] looking for areas for example where the
[21:05:41] SQL is doing a full scan of the tables
[21:05:44] or maybe using expensive operations like
[21:05:47] nested loop joins and so on. So once you
[21:05:49] understood where is exactly the pain
[21:05:52] point the next step is that you have to
[21:05:54] go and choose the right index. So which
[21:05:56] type of indexes we're going to use in
[21:05:58] order to optimize the query. And once
[21:06:00] you go and create the index, the last
[21:06:03] step is that you have to go and test it.
[21:06:05] So you're going to run again the
[21:06:06] execution plan in order to make sure
[21:06:08] that your query is using the index that
[21:06:11] you have just created. So that means you
[21:06:13] have to go and compare the execution
[21:06:15] plans before and after. And if you see
[21:06:17] that there is no benefit, then something
[21:06:20] is wrong. That means you have to go and
[21:06:22] investigate more and analyze the
[21:06:23] execution query and maybe choose a
[21:06:26] better index way. And you have to do
[21:06:28] this process for each slow query until
[21:06:31] you get all your queries fast. But of
[21:06:33] course, don't forget indexing is not the
[21:06:35] only methods on how to optimize the
[21:06:37] speed of queries. So as you can see
[21:06:40] through these three phases, we went from
[21:06:42] a very generic methods on how to index
[21:06:45] our system to something very specific
[21:06:48] and scenario based. So as you can see as
[21:06:50] we moving in the phases, we are doing
[21:06:52] more deep dive into our projects.
[21:06:59] All right. So now moving to the last
[21:07:00] phase, we have the monitoring and
[21:07:02] maintenance of our indexes. As we
[21:07:05] learned, the job doesn't stop by just
[21:07:07] creating and implementing indexes. We
[21:07:09] have to be responsible by keeping eye on
[21:07:12] the health of our indexes. And here the
[21:07:14] databases offers a lot of statistics and
[21:07:17] metadata about your data that you could
[21:07:19] use in this phase. So the first step is
[21:07:22] to monitor the usage of the indexes. And
[21:07:25] as we learned, we can use the dynamic
[21:07:27] management views or functions that we
[21:07:30] can find in the system schema where we
[21:07:32] can see the number of usage of each
[21:07:34] index and when the last time our queries
[21:07:37] did use the indexes. So with that we can
[21:07:40] go and find out all those indexes that
[21:07:42] we have created and never been used in
[21:07:45] our projects. And now the next step is
[21:07:46] that we can go and monitor the missing
[21:07:48] indexes. So here we can go and check
[21:07:51] what are the recommendations from the
[21:07:52] database where the database is reporting
[21:07:56] missing indexes from the execution plan
[21:07:58] and again we can go and use those
[21:08:00] dynamic management views or functions in
[21:08:02] order to see more details and as well we
[21:08:05] can go and monitor whether we have
[21:08:07] duplicates in the indexing. It happens a
[21:08:09] lot if you have like a lot of developers
[21:08:11] in your team. So it could be that they
[21:08:13] are working parallelly to optimize the
[21:08:15] performance of slow queries and then go
[21:08:18] and create multiple indexes for the same
[21:08:20] column. So this is something that we can
[21:08:22] go and check whether we have duplicates
[21:08:24] in our indexes and if you have
[21:08:25] duplicates then you have to go and find
[21:08:27] how you can go and consolidate them.
[21:08:29] Then the next step we have to go and
[21:08:31] update the statistics. So as we learned
[21:08:34] statistics are very important for the
[21:08:36] execution plan because the database
[21:08:38] engine use those informations to decide
[21:08:41] the best execution plan for your query
[21:08:43] and if the statistics are old then the
[21:08:45] database going to make wrong decisions
[21:08:47] about how to execute your query which
[21:08:50] might lead to bad performance. So here
[21:08:52] again we have like special functions in
[21:08:54] order to monitor the statistics but here
[21:08:56] my recommendation that each weekend have
[21:08:59] a job that go and create all the
[21:09:01] statistics of your database. And the
[21:09:03] last step we don't have to forget about
[21:09:04] monitoring the fragmentations as we
[21:09:06] learned over the time as you are doing
[21:09:08] modifications on the tables. What could
[21:09:10] happen the order of the databases could
[21:09:12] get wrong or there are like free spaces
[21:09:15] on the database that are not used. So we
[21:09:17] have like fragmentations in the index
[21:09:20] and the same thing we have to monitor
[21:09:21] the fragmentations of each tables and
[21:09:24] here if the percentage is between 0 and
[21:09:26] 10 then there is no issue but if the
[21:09:28] fragmentation is between 10 and 30 then
[21:09:30] we have to go and reorganize the index
[21:09:33] and if it's more than 30 then this is
[21:09:35] alerting you have to go and rebuild the
[21:09:38] whole index and usually for the
[21:09:40] monitoring I go and build like automated
[21:09:43] dashboard in PowerBI or Tableau where I
[21:09:45] go and extract all those metad data and
[21:09:47] create a nice dashboards in order to
[21:09:50] monitor the health of the database or
[21:09:51] you can go and buy some other tools that
[21:09:54] are advanced in order to do those
[21:09:59] stuff. All right. So this is my indexing
[21:10:02] strategy that I usually follow in my
[21:10:03] projects. And as you can see, each phase
[21:10:05] builds upon the previous one. Moving
[21:10:08] from a general strategy to more
[21:10:10] targeted, refined, specific strategy
[21:10:13] where we define first the goal of the
[21:10:15] indexing strategy of the projects. And
[21:10:17] as we move with the phases, we're going
[21:10:19] to be targeting more specific scenarios.
[21:10:22] And this cycle keep repeating. It's not
[21:10:24] only one time. So you have to keep
[21:10:26] discussing is the goal still suitable
[21:10:28] for the projects. You have to keep
[21:10:30] analyzing the frequently used tables and
[21:10:33] columns and keep searching and finding
[21:10:35] those slow queries and always keep an
[21:10:38] eye monitoring the indexes and of course
[21:10:41] I can only keep repeating this avoid
[21:10:44] overindexing. All right my friends so
[21:10:46] that's all about the indexes that was a
[21:10:48] lot of informations and a lot of
[21:10:50] technique. So now you know everything
[21:10:51] about indexing in SQL. Now in the next
[21:10:53] one there is another important
[21:10:55] techniques on how to optimize the
[21:10:57] performance. So we're going to talk
[21:10:58] about the partitions. So how to divide
[21:11:00] our data in order to optimize the
[21:11:02] performance. So let's
[21:11:07] go. All right. So what is SQL
[21:11:09] partitioning? It's a technique in order
[21:11:11] to divide a large table into small
[21:11:14] pieces and each piece we call it a
[21:11:16] partition. Well, this sounds like we are
[21:11:18] dividing one big table into smaller
[21:11:20] tables but it's not like that. We are
[21:11:22] just dividing one table into smaller
[21:11:25] partitions. So we going to see it in the
[21:11:27] database still as one solid table but
[21:11:30] behind the scenes it is splitted into
[21:11:32] multiple partitions. So now let's go and
[21:11:34] understand what this means. Okay. So now
[21:11:36] let's say that you have a table at your
[21:11:38] database and over the time this table is
[21:11:41] getting bigger and bigger where you have
[21:11:43] like hundreds of millions of rows. Now
[21:11:45] once you have such a big table what's
[21:11:46] going to happen everything going to be
[21:11:48] slow. So for example, if you are reading
[21:11:50] the table and the execution plan is
[21:11:52] doing full scan of the table, this can
[21:11:55] take SQL long time until all the rows
[21:11:57] are fetched. And if you decide to make
[21:11:59] like an index for this table, what's
[21:12:02] going to happen? SQL going to go and
[21:12:03] build a very big B tree index where
[21:12:06] there are a lot of branches and files
[21:12:08] and so on. And having a big index is not
[21:12:11] always a good thing because if you do
[21:12:13] operations like delete rows, update rows
[21:12:16] or inserting rows, these operations
[21:12:19] going to need long time to process. So
[21:12:21] having a big index doesn't mean that you
[21:12:24] can have a good performance for your big
[21:12:26] table. So that means having a big table
[21:12:28] is a problematic because everything
[21:12:30] going to be slow. So now what we can do
[21:12:32] in order to optimize the performance of
[21:12:35] this big table? Well, we can use SQL
[21:12:38] partitioning and in order to do that, we
[21:12:40] have to understand the behavior and the
[21:12:43] transactions that are happening on our
[21:12:45] table and what usually happen with that
[21:12:47] the table grows over the time. So, you
[21:12:50] can have like subset of data that
[21:12:51] belongs to 2023 and another one that is
[21:12:54] created and updated in 2024 and then you
[21:12:57] have something like more current in
[21:12:59] 2025. So that means we have like in our
[21:13:02] table old data and as well new data and
[21:13:05] we usually interact with the new data
[21:13:07] more often than the old data. So maybe
[21:13:10] for example for 2023 there is like only
[21:13:12] one read transaction and for the data in
[21:13:15] 2024 we have done like two reads and one
[21:13:18] rights. So it is little bit more than
[21:13:20] 2023 but for the new data for the
[21:13:23] current year there will be heavy
[21:13:25] transactions. So we're going to have a
[21:13:27] lot of reads a lot of rights. We are
[21:13:29] updating, inserting, reading. So a lot
[21:13:31] of things are going on for the new data.
[21:13:33] So that means we are accessing
[21:13:35] frequently the big table only to
[21:13:37] interact with the new data and we rarely
[21:13:40] need the old data. So what we can do, we
[21:13:42] can go and divide this big table and we
[21:13:45] usually divide it by like a date. So
[21:13:48] that means we can go and split this
[21:13:49] table by the year and we put each year
[21:13:52] in one partition. So at the end we're
[21:13:54] going to have like three partitions. And
[21:13:55] now it's really important to understand
[21:13:57] that that those are three partitions.
[21:13:59] They are not three tables. So that means
[21:14:01] at the client side the users can see
[21:14:04] only one table but behind the scenes we
[21:14:07] have like three partitions. Now let's
[21:14:08] say that you have a query in order to
[21:14:10] read the data from 2025. And now what
[21:14:13] going to happen? SQL will not go and
[21:14:14] scan all the data from the table. It's
[21:14:16] going to go and only target one
[21:14:18] partition the 2025. So that means SQL is
[21:14:22] only scanning the relevant informations
[21:14:24] the relevant partition and not the
[21:14:26] entire table. And now we have another
[21:14:28] benefits of having partitions. Let's say
[21:14:30] that you're using a modern database and
[21:14:33] normally they support parallel
[21:14:34] processing. So if you have the
[21:14:36] infrastructure for that what can happen
[21:14:37] the database engine can process each
[21:14:40] partition independently and parallelly.
[21:14:43] So whether you are reading or writing
[21:14:45] data. So what's going to happen? SQL
[21:14:47] going to process your queries parallelly
[21:14:49] which of course can reduce the overall
[21:14:52] execution time. So that means if you
[21:14:54] have a modern infrastructure like maybe
[21:14:56] for example the Azure Synapse and so on
[21:14:58] go with the partitions because the
[21:15:00] partition then could be stored in
[21:15:02] different servers and this helps of
[21:15:04] course the SQL engine to use all the
[21:15:07] resources at once. So that means
[21:15:09] partitions allow scalability and as well
[21:15:12] parallel processing. partitions going to
[21:15:14] make the indexing more efficient. So
[21:15:17] instead of having one very big index for
[21:15:20] the whole table, if you put an index on
[21:15:22] a partition table, what's going to
[21:15:23] happen? Each partition going to get its
[21:15:26] own index, which means the size of the
[21:15:28] indexes going to be smaller. And of
[21:15:30] course, this helps a lot with searching
[21:15:33] for data or as well extending the index
[21:15:35] itself. So for example, if you are
[21:15:37] inserting data to the partition 2025,
[21:15:40] the SQL will not go and change anything
[21:15:42] on the other indexes, it's going to go
[21:15:44] and only change the index of the
[21:15:46] partition 2025. So that you can see the
[21:15:48] power of the partitioning. It improves
[21:15:51] significantly the performance of your
[21:15:53] table whether you are reading or writing
[21:15:55] data to this big table. So this is what
[21:15:58] we mean with partitioning and why we
[21:15:59] need
[21:16:03] it. All right, friends. So now we're
[21:16:05] going to go to the process of creating
[21:16:07] partitions in SQL. At the start it might
[21:16:10] sounds a little bit complicated but
[21:16:11] we're going to do it step by step and I
[21:16:13] have a sketch for that. So we have like
[21:16:15] four steps because we have in the
[21:16:17] database like multiple layers. So let's
[21:16:19] see how we can do that. Let's go. So the
[21:16:22] first step is that we're going to go and
[21:16:23] define the partition function. So what
[21:16:25] is that? We're going to go and define
[21:16:27] here in the function the logic on how to
[21:16:30] divide the table into partitions. And
[21:16:32] this can be based on the partition key.
[21:16:35] So that means we need a column in order
[21:16:37] to define the logic. And we usually use
[21:16:39] columns with the dates like for example
[21:16:41] the order dates or in other scenarios we
[21:16:44] can use the region or country and so on.
[21:16:46] But the most famous one is the dates and
[21:16:48] that's because our tables like get
[21:16:51] bigger over the time and there are like
[21:16:53] multiple types of functions. We're going
[21:16:55] to focus on the range function. So how
[21:16:57] it going to work? We're going to have
[21:16:58] like a range of dates and then we have
[21:17:00] to define like boundary values and let's
[21:17:03] say that I would like to make a
[21:17:05] partition for each year and in order to
[21:17:08] do that we have to define the partition
[21:17:10] boundary. So it is like a value the
[21:17:12] boundary of the years could be like the
[21:17:14] first day of the year or the last day of
[21:17:16] the year. So here in this example we're
[21:17:18] going to take for the boundary the last
[21:17:20] day of the year. So the last day of
[21:17:22] 2023, 2024 and 2025. So we call those
[21:17:26] values the boundary of our function. Now
[21:17:30] between the boundaries we going to have
[21:17:31] our partitions. So for examples all the
[21:17:34] rows for 2025 and earlier years is going
[21:17:38] to be the partition one. So between the
[21:17:40] boundary and everything before is one
[21:17:42] partition and after that between the two
[21:17:44] boundaries we have partition two. So
[21:17:46] this partition going to be for all rows
[21:17:48] of 2024. And then we have another
[21:17:51] section the partition three where we
[21:17:53] have all rows of 2025 and then between
[21:17:56] the last boundary and everything onwards
[21:17:59] is going to be partition 4 and here
[21:18:00] we're going to have all the rows from
[21:18:02] 2026 onward. So with that we have now a
[21:18:05] logic we are telling SQL how to divide
[21:18:08] our data into multiple partitions and
[21:18:10] here there is like two methods the left
[21:18:12] and the right. So what are those two
[21:18:15] methods? So again we have our boundary
[21:18:17] and now the big question to which
[21:18:19] partition does this boundary belongs to
[21:18:22] is it partition one or partition two and
[21:18:24] that's why we have those two methods. If
[21:18:26] you say it is left that mean the
[21:18:28] boundary belongs to the partition number
[21:18:30] one. But in the other hand if you say it
[21:18:32] is right then the boundary going to be
[21:18:34] part and belongs to the partition number
[21:18:37] two. So you have to decide whether the
[21:18:39] boundaries belongs to the left partition
[21:18:40] or to the right partition. And with that
[21:18:42] in the partition one, we're going to
[21:18:44] have all the rows of 2023 including the
[21:18:47] last day of 2023 because in the
[21:18:50] partition 2 we only focus on 2024. So
[21:18:53] it's just the boundary belongs to the
[21:18:56] left partition. It's very simple. Now
[21:18:57] let's go and implement that in SQL. So
[21:19:00] let's do it. The syntax is very simple.
[21:19:02] We're going to say create partition
[21:19:05] function and then we have to give it a
[21:19:07] name. So it's going to be
[21:19:09] partition by year since we are dividing
[21:19:12] the data by the year. And after that we
[21:19:14] have to define the data type. So we are
[21:19:17] splitting the data by a date. So it's
[21:19:19] going to be date. And after that we have
[21:19:21] to define the partition function type.
[21:19:24] So in our example we are using the
[21:19:26] range. And now we have to define whether
[21:19:28] it is left or right. We're going to
[21:19:31] stick with the left. And now comes the
[21:19:33] very important step. We have to define
[21:19:35] the boundaries. So we're going to say
[21:19:38] for
[21:19:39] values and we're going to enter here
[21:19:41] three boundaries like in our example for
[21:19:43] each year we're going to define a date.
[21:19:45] So
[21:19:47] 2023 and the last day of the year. Same
[21:19:50] goes for
[21:19:52] 2024 and for the last one
[21:19:56] 2025. So with that we have defined the
[21:19:59] logic the range we have defined the
[21:20:02] boundaries and we tell SQL the
[21:20:04] boundaries are a date. So let's go and
[21:20:06] execute our function. Okay, so that's
[21:20:09] it. As you can see, it's very simple. We
[21:20:11] just created a function that split the
[21:20:14] data by the date using the range lift.
[21:20:16] And of course, this function is not yet
[21:20:18] attached to any tables or anything. It
[21:20:21] is just a logic that is stored in the
[21:20:23] database. All right. So now since our
[21:20:24] partition function is stored inside the
[21:20:26] database, we will have metadata about
[21:20:29] those functions stored in the system
[21:20:31] schema. So we have there a dedicated
[21:20:33] table called partition functions and
[21:20:35] there we're going to find informations
[21:20:37] about all functions that we have inside
[21:20:39] our database. So let's go and execute
[21:20:41] it. And as you can see we find now our
[21:20:43] new created partition function. So
[21:20:46] partition by year it is a range and it
[21:20:48] has an ID and so on. And I really
[21:20:50] recommend you to check it before
[21:20:52] creating any new partition function.
[21:20:54] Maybe you have already one in the
[21:20:59] projects. Okay. Okay. So now let's check
[21:21:01] the next step in our process. We're
[21:21:03] going to go and build now the file
[21:21:05] groups. So what is a file group? It is
[21:21:08] like a logical container of one or more
[21:21:11] data files. So it's very simple. It's
[21:21:13] like folders. We're going to go and
[21:21:15] create now like multiple folders. So
[21:21:17] later we can insert inside them files.
[21:21:20] And this is really nice because it gives
[21:21:22] us like freedom and flexibility where we
[21:21:25] can go and decide how the data files are
[21:21:27] organized for each partition. So what we
[21:21:30] usually do, we go and create for each
[21:21:32] partition a file group. So we're going
[21:21:34] to have like four folders or four file
[21:21:36] groups for 2023, 2024 and so on. So now
[21:21:39] let's go back to SQL in order to do
[21:21:41] that. All right. So now let's go and
[21:21:43] create those file groups. The syntax is
[21:21:45] very simple. So it's going to say alter
[21:21:47] database. And now we have to tell the
[21:21:49] database where these file groups should
[21:21:51] be stored in which database. So I'm
[21:21:53] going to stay with the sales DB. And
[21:21:55] then we have to tell okay add file group
[21:21:59] and after that we have to define the
[21:22:01] name of the file group. So the first one
[21:22:02] going to be for
[21:22:03] 2023. So the syntax is very simple.
[21:22:06] Let's go and do it for the other years.
[21:22:09] So we need
[21:22:11] 2024 5 and six. Okay. So that's all. We
[21:22:14] can just select everything and execute.
[21:22:17] So as you can see it's very simple. We
[21:22:18] have just created four file groups and
[21:22:21] they are empty. So we don't have
[21:22:23] anything inside those containers. Now
[21:22:25] let's say that you have made mistake
[21:22:26] with the namings and so on and you would
[21:22:28] like to drop one of them. So the syntax
[21:22:30] is as well very easy. So it's going to
[21:22:33] say alter database sales DB and instead
[21:22:35] of add you're going to say remove. So
[21:22:38] once you execute this file group will be
[21:22:41] dropped but we need it. So let's go and
[21:22:44] recreate it. Now as usual after creating
[21:22:46] stuff let's check whether everything is
[21:22:48] created correctly and whether we have
[21:22:51] any duplicate or anything wrong. So with
[21:22:53] that we have as well a file group table
[21:22:56] inside the system schema and let's go
[21:22:58] and execute it. So I'm just filtering
[21:23:00] with the type FG for file group. So
[21:23:03] let's execute it. And now we can see in
[21:23:05] our database we have four file groups.
[21:23:08] Now four of those file groups we just
[21:23:10] created it right. So we have the 2023 24
[21:23:13] and so on. But we have something called
[21:23:16] primary file group. This is the default
[21:23:18] file group that is created for each
[21:23:21] database. So it is a container for all
[21:23:23] data files in your database. And as you
[21:23:25] can see we have here a flag saying it is
[21:23:27] a default. So it's default and we have
[21:23:29] it one and for the rest they are not the
[21:23:31] defaults. So this is really nice to see
[21:23:33] all the file groups inside your database
[21:23:35] to check that you don't have duplicate
[21:23:37] and so
[21:23:41] on. Okay. Now moving on to the third
[21:23:43] step where things going to get more
[21:23:45] physically. So so far we have like a
[21:23:47] function the file group and all those
[21:23:48] stuff are logical stuff. We don't have
[21:23:50] data yet. In order to have data, we have
[21:23:53] to go and create data files. So, as we
[21:23:55] learned before, data files going to
[21:23:57] contain our actual data and they're
[21:23:59] going to be stored physically in the
[21:24:00] database. So, you can go and assign for
[21:24:02] each file group like one or multiple
[21:24:05] data files. And the file format here is
[21:24:08] MDF. It is secondary data files. We have
[21:24:11] like primary and secondary. But in the
[21:24:13] partitions, we usually go with this
[21:24:15] format, the NDF. So again the file
[21:24:17] groups are illogical containers and the
[21:24:19] data files are physical files where our
[21:24:22] actual data going to be stored inside
[21:24:23] it. So now let's go back to SQL in order
[21:24:25] to create some data files. Okay. So now
[21:24:27] we're going to come to the little bit
[21:24:29] annoying part where we're going to go
[21:24:30] and create files. But the syntax is as
[21:24:32] well very simple. So we're going to say
[21:24:34] the same things alter database and our
[21:24:37] database is sales DB. And then this time
[21:24:39] we're going to say add file. And now we
[21:24:42] have to give SQL not only the name but
[21:24:44] the physical place of the files. So
[21:24:46] let's do it step by step. We're going to
[21:24:48] open new two parenthesis. So first we
[21:24:50] have to define for SQL the logical name.
[21:24:53] It is not the file name. It is the
[21:24:55] logical name of the file. So let's give
[21:24:57] it a name for example B 2023 and then
[21:25:00] comma. So this is the logical name. And
[21:25:04] now the next one is we're going to give
[21:25:05] the physical name of the file together
[21:25:08] with the path. So we're going to say
[21:25:09] file name equal and now we have to
[21:25:12] define for SQL the complete path of the
[21:25:15] file in SQL server there is like a
[21:25:17] default path where the data going to be
[21:25:19] stored and I'm going to go and use the
[21:25:21] same path and the path really depends on
[21:25:24] the version and as well the type of the
[21:25:26] SQL server that you are using. So for
[21:25:28] the current version that I'm using for
[21:25:30] this tutorial we can find it over here
[21:25:32] in this path. So if you go to the C then
[21:25:34] program files Microsoft SQL Server MSSQL
[21:25:38] and the version for me is 16 SQL Express
[21:25:41] and then inside MSSQL data and so on. So
[21:25:44] we're going to go inside this folder and
[21:25:46] now we can see over here all the
[21:25:48] database files. So we can see for
[21:25:50] example here the sales DB the sales DB
[21:25:53] logs and we have here the adventure
[21:25:55] works and so on. So you're going to see
[21:25:56] all the files of your database. And what
[21:25:58] we're going to do, we're going to put as
[21:25:59] well our partitions files inside the
[21:26:02] default folder. But for real project,
[21:26:04] you have to ask the database
[21:26:05] administrators about the exact location
[21:26:08] where you can put your partitions. So
[21:26:10] let's go back to SQL and I'm going to
[21:26:12] put this path over here. And then we
[21:26:14] have to specify the file name. So it's
[21:26:17] going to be P 2023 dot. And now we have
[21:26:20] to specify the file name. So, NDF and
[21:26:23] with that we have now a complete path
[21:26:25] with the file name. So, we are almost
[21:26:27] there but we are not done yet. We have
[21:26:29] to tell SQL where to put this file in
[21:26:31] which container in which file group. So,
[21:26:34] we're going to go over here and we're
[21:26:35] going to say to file group and here make
[21:26:39] sure to select the correct one. So, FG
[21:26:42] 2023. All right. So, that's all. Let's
[21:26:45] go and execute it. So, let's do it. And
[21:26:48] with that we have created a file inside
[21:26:50] a file group. I will not be creating
[21:26:52] like multiple files inside one file
[21:26:54] group. It's going to be like one to one.
[21:26:56] So now what we're going to do we're
[21:26:57] going to go and create the other files
[21:26:58] for each file group for each year. So we
[21:27:01] just have to copy and paste and just
[21:27:03] change the names. So for
[21:27:06] 2024 going to be like
[21:27:08] this. So that's it. And the same thing
[21:27:11] for 2025.
[21:27:17] And for the last one 20
[21:27:24] 26 and we can go and select now
[21:27:27] everything and execute it. So that's it
[21:27:30] with that we have created now four
[21:27:32] different files and we have mapped as
[21:27:34] well each file to the correct file group
[21:27:37] and I usually don't create like a lot of
[21:27:39] files. I just create like one for each
[21:27:41] year or maybe for bunch of years. So you
[21:27:44] don't have to go and make for each day
[21:27:46] like partition or something like that.
[21:27:48] Okay. As usual after creating stuff we
[21:27:50] have to go and check the metadata. Now I
[21:27:52] have here prepared a query where we
[21:27:54] query the file groups together with the
[21:27:56] files. So all the data informations
[21:27:58] could be found inside the table master
[21:28:00] files and then we join those tables and
[21:28:02] select our database. So let's go and
[21:28:05] query this one. And now we're going to
[21:28:07] get a list of all files inside your
[21:28:09] database. So we see over here we have
[21:28:12] the primary for the database itself and
[21:28:15] you can see the path of the file and as
[21:28:17] well the size of it and we can see over
[21:28:19] here we have four files and the file
[21:28:21] group that is assigned to and the
[21:28:23] complete path of each file and you can
[21:28:26] monitor over here of course how the size
[21:28:28] of each file is growing over the time.
[21:28:31] Maybe one of them is getting like really
[21:28:33] big and then you can think about let's
[21:28:35] go and split it to multiple files. So
[21:28:37] that's it about how to create data
[21:28:39] files.
[21:28:43] All right. So now we're going to move to
[21:28:45] the last step where we're going to go
[21:28:46] and define the function scheme. Now if
[21:28:48] you have a look to this picture, you see
[21:28:50] that there is something missing. From
[21:28:52] one side, we have defined how to divide
[21:28:54] our data into multiple partitions. And
[21:28:57] from the other side, we have repaired
[21:28:58] all the files and the file groups and so
[21:29:01] on. And now what is missing is the
[21:29:03] connection. How to connect those
[21:29:05] partitions to the file groups. And we
[21:29:08] can do that by using the partition
[21:29:09] scheme. So all what we are doing now is
[21:29:12] just defining which partition belongs to
[21:29:14] which file group. So for example, we're
[21:29:16] going to go and map the partition one to
[21:29:18] the file group 2023. And with that all
[21:29:21] the data of 2023 and earlier going to go
[21:29:24] to the file group 2023. And of course we
[21:29:28] have to go and map each partition to a
[21:29:30] file group. If you don't do that, you
[21:29:32] will get error in SQL. And once we build
[21:29:34] the partition scheme then we can have
[21:29:36] all the component ready in order to have
[21:29:39] partition table. So now let's have a
[21:29:41] quick summarize. The partition function
[21:29:44] going to decide on how to split your
[21:29:46] data into multiple partitions. The
[21:29:48] partition scheme going to go and map the
[21:29:50] partitions to a file group. And the file
[21:29:53] groups are like folders in order to
[21:29:55] organize your files. And each file group
[21:29:57] has one or more data files where your
[21:30:00] actual data going to be stored
[21:30:01] physically. add these files at the
[21:30:04] start. It might be confusing, but now as
[21:30:06] you understand each layer, then it's
[21:30:08] going to make it easier for you to build
[21:30:09] partitions. So now let's go back to SQL
[21:30:12] in order to build the partition scheme.
[21:30:14] Okay, so now we have the easiest part
[21:30:16] where we're going to connect everything
[21:30:17] together. So the syntax as well very
[21:30:19] simple. It's going to say create
[21:30:21] partition scheme and now we have to give
[21:30:24] it a name. So let's go with like scheme
[21:30:28] partition by year. And now we have to
[21:30:31] map the partition function with the file
[21:30:33] groups. So first we're going to say as
[21:30:35] and then we define here the partition
[21:30:38] function. So as partition and now we
[21:30:41] need the partition function that we have
[21:30:43] created. So as
[21:30:44] partition by year and then after that
[21:30:48] we're going to map it to the file
[21:30:49] groups. And here it is very important to
[21:30:52] map it in the correct order. So the
[21:30:54] order is very important. So the first
[21:30:57] one was file group 2023. The second one
[21:31:01] 2024 and we have 2025 and the last one
[21:31:07] 2026. So again the order is very
[21:31:10] important and as well it's going to be a
[21:31:11] little bit tricky. So sometimes as you
[21:31:13] are creating like the functions maybe
[21:31:15] you make mistake that you don't know how
[21:31:17] much partitions are going to create like
[21:31:19] in our example we have three boundaries
[21:31:21] and SQL going to create four partitions.
[21:31:23] So it happens sometimes that you think
[21:31:25] okay I have three boundaries and then
[21:31:28] I'm going to get three partitions which
[21:31:30] is not really correct. So for example
[21:31:32] let me just remove one of those and
[21:31:34] let's say I have only three five groups
[21:31:37] and let's go and execute this one over
[21:31:39] here. Now we are getting error. It says
[21:31:41] the partition function generates more
[21:31:44] partitions than the five groups. And
[21:31:46] that is really correct because our
[21:31:48] definition of the logic can split the
[21:31:50] data into four partitions. And now we
[21:31:52] are giving SQL only three five groups
[21:31:55] which is not correct. So we have to go
[21:31:56] and add the plus one. And one more thing
[21:31:59] SQL will not go and check whether you
[21:32:01] are mapping things correctly to the five
[21:32:03] groups because it doesn't really care
[21:32:05] about the naming of those five groups.
[21:32:07] So for example, if you go and put this
[21:32:09] one at the end, what's going to happen?
[21:32:11] It's going to be a big problem. So all
[21:32:13] the years of 2023 going to be stored
[21:32:15] inside 2024, 2024 going to be in 2025.
[21:32:19] So everything going to be mixed and the
[21:32:21] skill can do it like you tell it. So
[21:32:23] that's why make sure you have the
[21:32:25] correct sorts. So that's it. Let's go
[21:32:27] and create our scheme. So it is working.
[21:32:30] This is very simple. We just map now the
[21:32:33] partitions to the five groups. And as
[21:32:35] usual we check things after creating and
[21:32:38] I have prepared here like really nice
[21:32:40] query from the metadata in order to see
[21:32:42] the whole thing the functions the file
[21:32:44] groups the schemes you can of course add
[21:32:46] to it the data files but I'm just going
[21:32:49] to stick with this over here. So again
[21:32:51] in SQL server we have a dedicated table
[21:32:53] for the partition schemes. Then I'm just
[21:32:55] joining it with the functions and then
[21:32:57] with the destination data spaces in
[21:32:59] order to get the partition number and
[21:33:01] the file groups. So let's go and execute
[21:33:03] it. And now we can see very nicely the
[21:33:05] scheme that we have created and the
[21:33:07] function name of the partition. And then
[21:33:09] we can see the partition number and the
[21:33:12] file group name. So we can see how
[21:33:14] things are mapped together. So if you
[21:33:16] get it like this then so far everything
[21:33:18] is
[21:33:22] good. All right. So so far what you have
[21:33:24] done we have prepared all the layers. So
[21:33:27] we have the setup is ready to be used in
[21:33:30] any table. So we have the functions, the
[21:33:32] files, the file groups and schema and
[21:33:34] everything is ready. But still we are
[21:33:36] not using it. The logic just exist and
[21:33:38] the files are empty. So now what we're
[21:33:40] going to do we're going to go and create
[21:33:41] a table but not a normal one a partition
[21:33:44] table. So let's go and do that. It's
[21:33:46] very simple as well. So create table and
[21:33:48] we have to give it a name. So let's get
[21:33:50] it as well in the schema sales orders
[21:33:53] and I'm just going to give it the name
[21:33:54] partitions. So now we have just to
[21:33:56] define like few columns inside this
[21:33:58] table. So let's get an order ID and data
[21:34:03] type int. And let's go and get an order
[21:34:05] date. We call it dates with the data
[21:34:08] type dates. And maybe just one more
[21:34:10] called sales and a data type in. So this
[21:34:13] is very normal table that we create in
[21:34:15] databases. But it's still not yet
[21:34:17] partitioned. Now in order to use
[21:34:19] everything that we have defined, we're
[21:34:21] going to go do the following. We're
[21:34:22] going to say on and now we have to tell
[21:34:25] SQL only the name of the partition
[21:34:28] scheme. So everything else is like
[21:34:30] connected and mapped together because
[21:34:32] the scheme is mapping the function with
[21:34:34] the file groups. The file groups are
[21:34:36] mapped to the data files and everything
[21:34:38] is like connected together. And here in
[21:34:40] the table we have just to give the name
[21:34:42] of the scheme. So the name of the
[21:34:43] partition scheme is scheme
[21:34:46] partition by year. And now it's very
[21:34:49] important to give a column. And since
[21:34:52] the whole logic and the function is
[21:34:54] based on a date, we cannot go and
[21:34:56] specify here for example the order ID or
[21:34:58] sales because it makes no sense. We're
[21:35:00] going to go and pick the order date and
[21:35:02] put it over here. And with that, we have
[21:35:04] created a partition table. So now what
[21:35:06] we're going to do, we're going to go and
[21:35:07] start inserting that out of our table.
[21:35:09] So let's go and do that. We're going to
[21:35:10] say insert into sales order
[21:35:15] partitioned and we're going to pick
[21:35:17] values like this. So one and then let's
[21:35:20] get any dates like 2023 like for example
[21:35:24] my the mid of the month and the sales
[21:35:26] could be anything like let's say 100. So
[21:35:30] let's go and execute this and let's go
[21:35:32] query our
[21:35:34] table. So it is this one over
[21:35:38] here. All right. So now we have one
[21:35:40] record inside our partition table. And
[21:35:42] now the big question is in which
[21:35:44] partition in which data file did SQL
[21:35:47] store this record. So we have to test
[21:35:49] whether everything is working fine. So
[21:35:51] in order to do that I have prepared as
[21:35:53] well a query. So we are again asking the
[21:35:55] table partitions with the destination
[21:35:57] data spaces where we're going to get the
[21:35:59] number of rows in each partition and
[21:36:01] then we have the file group and we are
[21:36:03] focusing on our table orders partitions.
[21:36:06] So let's go and execute this one. And
[21:36:07] now we can see very easily we have the
[21:36:09] four partitions. our new record is
[21:36:11] inserted in the correct place in 2023
[21:36:15] file group and in the correct partition.
[21:36:17] So with that we make sure our function
[21:36:20] and the whole logic that we have built
[21:36:22] is working correctly. So now let's go
[21:36:24] and add more records. I'm just going to
[21:36:26] go and duplicate it. Record number two.
[21:36:28] And I'm just going to pick a date in
[21:36:31] 2024. And this one going to be like 20.
[21:36:35] Let's just change the value. So 50.
[21:36:38] Let's go and execute it.
[21:36:40] And now we have a second row inside our
[21:36:42] table. And again the big question is
[21:36:45] whether it is working. So let's go and
[21:36:47] execute this again. And now we can see
[21:36:49] our record is inserted in the partition
[21:36:51] 2 in the file group 2024 which is
[21:36:55] correct. Now let's go and check the
[21:36:57] boundaries whether it is working
[21:36:59] correctly. So I'm going to go and here
[21:37:01] in the third row I'm going to say the
[21:37:04] last day of 2025. So it's going to be
[21:37:07] month 12 and the last day. So 20. Let's
[21:37:12] go and insert it and check our table. So
[21:37:15] we have a new record. And now let's go
[21:37:18] and check. My expectation here that this
[21:37:21] row is going to be inserted in the file
[21:37:23] group
[21:37:24] 2025. So let's go and execute. And that
[21:37:27] is correct. As you can see the record is
[21:37:30] inserted in the correct partition. And
[21:37:32] this is really important to test the
[21:37:33] boundaries whether they are working
[21:37:35] correctly because it's a little bit
[21:37:36] tricky. You have this range left right
[21:37:39] and boundaries and so on. So you can do
[21:37:41] it like this to check whether the
[21:37:42] expectation of your logic is working
[21:37:44] correctly. And the last one I'm just
[21:37:46] going to do it very fast. So let's do it
[21:37:49] 2026. And I'm going to pick the first
[21:37:51] day of this
[21:37:54] year. So let's go and insert it. And now
[21:37:57] what is the expectation? I think it is
[21:37:59] pretty simple. So let's go and query.
[21:38:01] And the first day of this year is
[21:38:02] inserted in the partition number four.
[21:38:04] So I can say everything is working
[21:38:06] correctly. If you get it like this then
[21:38:08] you have created successfully a
[21:38:10] partition table and you have prepared
[21:38:12] all the layers of this partition
[21:38:14] correctly. I know this is a lot of work
[21:38:16] but to be honest it is fun because for
[21:38:19] the first time in database you feel like
[21:38:20] you are controlling stuff. Usually in
[21:38:22] database everything like behind the
[21:38:24] scenes and you don't know exactly where
[21:38:26] the files are stored of your tables and
[21:38:28] so on. There is a lot of abstraction in
[21:38:30] databases but here like we are getting
[21:38:33] deep in databases and we are controlling
[21:38:35] and managing all those files which is
[21:38:37] sometimes it's nice to have this freedom
[21:38:39] and flexibility. All right one quick
[21:38:41] thing that I would like to show you that
[21:38:42] if you go to the database in the
[21:38:44] explorer then let's go to the storage
[21:38:46] over here. So let's expand it and here
[21:38:48] you can find easily informations about
[21:38:50] the partitions. So over here we can find
[21:38:52] our partition scheme and as well the
[21:38:54] partition function that we have created.
[21:38:56] it is just a quick access instead of
[21:38:58] like querying the
[21:39:02] metadata. So now let's have a quick
[21:39:04] summarize how everything is connected
[21:39:06] together. So we have a table and then we
[21:39:08] specify for scale that is connected to a
[21:39:11] partition scheme and in the partition
[21:39:13] scheme we have everything connected. It
[21:39:15] is linked to a specific partition
[21:39:17] function and there we have the
[21:39:19] partitions and at the same time it is
[21:39:21] connected to file groups and the file
[21:39:24] groups are connected to the data files.
[21:39:26] So as you can see all those layers and
[21:39:28] elements are connected together. Now
[21:39:30] let's see how this works. So we have
[21:39:32] inserted the last day of 2025 and now
[21:39:34] the first thing that's going to happen
[21:39:36] the partition function going to decide
[21:39:38] to which partition it belongs. So as you
[21:39:40] can see it is a boundary value and since
[21:39:42] we have defined it as a lift it going to
[21:39:45] target the left partition the partition
[21:39:47] three and then the partition scheme
[21:39:49] going to connect it to the right file
[21:39:51] group and in this scenario it's going to
[21:39:52] be the file group 2025 and we have here
[21:39:55] only one file so it going to as well go
[21:39:57] to the correct data file and in this
[21:39:59] file the SQL going to store this row so
[21:40:02] it is pretty
[21:40:06] easy and now we come to very important
[21:40:08] part where we can understand how the
[21:40:10] partitions are really improving the
[21:40:12] performance of my query and of course we
[21:40:14] can do that by checking the execution
[21:40:16] plan. So now in order to compare like
[21:40:18] the behavior with and without the
[21:40:20] partition what we have to do is to
[21:40:22] create a mirror table without partition.
[21:40:24] So we have our table here the
[21:40:25] partitioned one what I'm just going to
[21:40:27] do I will go over here and say into and
[21:40:29] we're going to call it sales
[21:40:32] orders no partition. So we are taking
[21:40:35] the data and the structure from the
[21:40:38] orders partitions and of course it will
[21:40:40] not be partitioned. So let's go and
[21:40:42] execute it. Now if you go over here we
[21:40:45] can see that we have two tables. We have
[21:40:47] the no partition and the partitioned
[21:40:49] one. So now what we're going to do we're
[21:40:50] going to write a query on both tables
[21:40:52] and then compare the execution plan. So
[21:40:54] first let's start with the no partition.
[21:40:56] also from and and now in order to see
[21:40:59] the effect of the partition what we're
[21:41:00] going to do we're going to say where
[21:41:03] order dates equal to and now we're just
[21:41:05] going to pick a value like 2026 the 1st
[21:41:08] of January so let's go and query it and
[21:41:11] we're going to do the same thing a new
[21:41:13] query but this time for the partitions
[21:41:17] so now in order to see the execution
[21:41:20] plan make sure to activate it so we go
[21:41:22] to the action bar over here and we're
[21:41:24] going to say include the actual
[21:41:25] execution plan. So let's click on it and
[21:41:28] execute. And with that we have here an
[21:41:30] execution plan. And let's do the same
[21:41:32] thing for the no partitions. So execute
[21:41:35] and we have here execution plan. So now
[21:41:37] let's check what we have in execution
[21:41:39] plan. We're going to focus on this one
[21:41:40] over here. So right click on it and then
[21:41:42] go to properties. And now we can see a
[21:41:44] lot of details about the execution plan.
[21:41:46] But what is interesting is the number of
[21:41:48] rows. So as you can see we are reading
[21:41:50] four rows. That means the whole table.
[21:41:52] And of course we have here the CPU and
[21:41:54] the other costs. Now let's go and check
[21:41:56] the partition. So let's click over here.
[21:41:59] So now if you check over here, you can
[21:42:00] see that the total number of rows is
[21:42:03] one. So SQL didn't read all four rows.
[21:42:06] It reads only row and that's because we
[21:42:08] have in this partition only one row. And
[21:42:10] as you can see the number of partitions
[21:42:12] that is used is as well only one. So as
[21:42:14] you can see using partition we have
[21:42:16] reduced the number of rows that is
[21:42:18] retrieved from the files. Now let's go
[21:42:20] and retrieve like two data from two
[21:42:23] different partitions and check the
[21:42:25] execution plan. So let's target 2025 the
[21:42:28] last day of the year like this. So let's
[21:42:32] go and execute it. And the same thing
[21:42:34] for the other
[21:42:37] query. So let's check the without
[21:42:40] partition. We still we are reading like
[21:42:42] four rows. But now if you go to the
[21:42:44] other one, if you check the execution
[21:42:46] plan and check the table scan, you can
[21:42:49] see we are reading only two rows and
[21:42:51] this time the number of partitions that
[21:42:53] are involved in this query is two and
[21:42:56] that's because we have partition for
[21:42:57] 2025 and 2026. So as you can see it's
[21:43:00] worth the efforts. We have optimized our
[21:43:02] queries and this has a great impact on
[21:43:05] big tables. The number of resources and
[21:43:07] the number of reads going to be reduced
[21:43:09] massively. All right my friends. So
[21:43:11] that's all about the partitions in SQL.
[21:43:13] It is amazing and you can use it as well
[21:43:14] not only in databases but as well in
[21:43:17] many other data platforms and tools
[21:43:19] where you always can divide your data in
[21:43:21] order to optimize the performance. Now
[21:43:22] in the next step what I have prepared
[21:43:24] for you after 15 years working in real
[21:43:26] projects using SQL. I have a lot of best
[21:43:29] practices and tips for you. So I have
[21:43:31] collected everything that I know and now
[21:43:33] I'm going to show you the best practices
[21:43:34] and tips and tricks that I can give you
[21:43:36] in order to optimize the performance in
[21:43:38] SQL. So let's go.
[21:43:44] And now before we deep dive into the 30
[21:43:47] best practices, I'm going to give you
[21:43:49] the golden rule. The SQL optimizer
[21:43:51] responds differently for different sizes
[21:43:53] of tables. So that means if you have
[21:43:55] small and medium tables like hundred of
[21:43:57] thousands, you might not notice any
[21:43:59] performance differences if you are
[21:44:00] following the best practices. And that's
[21:44:02] because the size of the data is small.
[21:44:04] But if you have like million or hundred
[21:44:06] of millions of records in tables, you
[21:44:08] will immediately notice how things can
[21:44:10] be faster if you follow the best
[21:44:12] practices. And here is my golden rule.
[21:44:14] If you get any best practice from me or
[21:44:17] let's say you are reading something in
[21:44:18] the internet, always you have to test
[21:44:20] using the execution plan. So for
[21:44:22] example, if you have like two queries
[21:44:24] are returning the same result of the
[21:44:25] data, I'm going to recommend you here to
[21:44:27] check the execution plan. And if you
[21:44:29] notice there is no differences between
[21:44:31] them in the execution plan then pick the
[21:44:34] one that you see it is easier to read
[21:44:36] and to understand because sometimes if
[21:44:38] you are following the best practices for
[21:44:40] the performance your query might be like
[21:44:42] little bit more complicated. So always
[21:44:44] write the query to be understandable and
[21:44:47] only optimize it if you notice it is
[21:44:49] slow. So the golden rule here is always
[21:44:52] test. If you find you are optimizing the
[21:44:54] performance with the new query then pick
[21:44:56] that and if there is no gain in the
[21:44:58] performance then focus on making your
[21:45:00] queries readable. So this is the golden
[21:45:03] rule always test test test using
[21:45:05] execution plan. So let's deep dive into
[21:45:07] best practices and we're going to start
[21:45:09] by optimizing the performance of our
[21:45:15] queries. All right let's start with the
[21:45:17] easy stuff. The first step is select
[21:45:19] only what you need. What I usually see
[21:45:21] in many queries is that the developers
[21:45:23] just go and select all the columns from
[21:45:25] one table and I can tell you I cannot
[21:45:27] think of one scenario where you need all
[21:45:28] the columns of one table in one query.
[21:45:31] So for sure in the result we will get
[21:45:33] like unnecessary columns and of course
[21:45:35] reading unnecessary informations going
[21:45:37] to make your query slower. So this is
[21:45:40] usually a bad practice. Don't use select
[21:45:42] star but instead of that go list all the
[21:45:44] columns that you need for your query. So
[21:45:47] make sure that you only select what you
[21:45:49] need. Don't go and select all the
[21:45:50] columns from one table and with that you
[21:45:52] don't risk reading unnecessary
[21:45:54] informations from the database. So
[21:45:56] always make sure that you select exactly
[21:45:58] what you need for a query don't go with
[21:46:00] a star. Okay. Tip number two avoid
[21:46:03] unnecessary distinct and order by. I
[21:46:05] have noticed that many developers as
[21:46:07] they are writing a lot of queries they
[21:46:09] tend by default adding always distinct
[21:46:12] and order by for each query. And as we
[21:46:15] review the code and discuss it with the
[21:46:16] developer, we see that we really don't
[21:46:19] need to remove any duplicates in the
[21:46:21] query because there are no duplicates
[21:46:22] and it was only a habit to remove the
[21:46:25] duplicates using distincts. And the same
[21:46:26] thing for the order by in many
[21:46:28] situations there is no need to sort the
[21:46:30] data at all. And those operations, the
[21:46:32] distinct removing the duplicate and
[21:46:34] sorting the data, they are very
[21:46:36] expensive operations in your execution
[21:46:38] plan. So they're going to take a lot of
[21:46:39] resources and slow down your query. So
[21:46:42] this considered as a bad practice if you
[21:46:44] always go and use distinct even though
[21:46:46] it's not needed or you are using the
[21:46:48] order by in order to sort the data when
[21:46:50] it is not necessary. So the best
[21:46:52] practice here is to avoid them. Don't
[21:46:54] use distinct or order by only if it is
[21:46:56] necessary. Okay. The next one for
[21:46:59] exploration purposes limit the rows. So
[21:47:02] sometimes especially if you are working
[21:47:03] with a new database you would like to
[21:47:05] explore the tables just to have a quick
[21:47:07] peek in order to see the content of the
[21:47:09] tables. And if your database has a lot
[21:47:11] of big tables with millions of rows and
[21:47:13] so on, you will be consuming a lot of
[21:47:15] resources. If you just select the data
[21:47:17] like this. So now imagine that the
[21:47:19] orders has like 100 million. As you run
[21:47:21] this query, the database has to fetch
[21:47:24] all the 100 million for you. And usually
[21:47:26] for exploration, it's enough to see like
[21:47:28] 10 rows and that's going to be enough.
[21:47:30] That's why it is considered as a bad
[21:47:32] practice if you are exploring the tables
[21:47:34] to not have a limit or top. So a good
[21:47:37] practice would be to say select top 10
[21:47:39] and then have the same query. So if you
[21:47:41] go over here you will get only 10 rows
[21:47:44] and the database will not fetch 100
[21:47:46] million. It can fetch only 10 rows. And
[21:47:48] now if you are exploring a lot of tables
[21:47:51] you will not consume a lot of resource
[21:47:53] from the database. So if you are
[21:47:55] exploring always limit the number of
[21:47:57] rows that you are
[21:48:01] retrieving. All right. Right. So now
[21:48:03] we're going to talk about how to
[21:48:04] optimize the filtering in SQL. So the
[21:48:07] tip here is to create an uncclustered
[21:48:09] index on frequently used columns in wear
[21:48:12] clause. So now of course you have to
[21:48:13] check your queries and so on. And if you
[21:48:15] see that you are frequently filtering
[21:48:17] the data using the order status then it
[21:48:20] makes sense to create a non-clustered
[21:48:22] index for this column in order to
[21:48:24] improve the performance of your query.
[21:48:26] So for this situation I'm going to go
[21:48:27] and create then a nonclustered index for
[21:48:30] the table sales order for the order
[21:48:32] status. So once you create it then you
[21:48:36] improving now the performance of your
[21:48:38] query. Okay. The next one is avoid
[21:48:40] applying functions to columns in the
[21:48:42] works. So in many cases what we usually
[21:48:45] do is that we go and transform the
[21:48:47] columns before like filtering the data.
[21:48:49] Like for example here I'm applying the
[21:48:51] function lower on the order status
[21:48:53] because I'm searching for the value
[21:48:55] delivered and I'm not sure about the
[21:48:56] values in the table whether they have
[21:48:58] like a camel case or uppercase or
[21:49:01] anything but in order to make sure that
[21:49:03] I'm going to find the value I'm going to
[21:49:05] go and say lower the order status and
[21:49:07] then give here a lower value and of
[21:49:10] course it's going to work. So if we go
[21:49:11] and search for it and as you can see we
[21:49:13] have here the status delivered and the
[21:49:15] value is different than the one I used
[21:49:16] because here we have like a capital
[21:49:18] first character but here we have a
[21:49:20] problem we have an index on the order
[21:49:22] status and now if you use any functions
[21:49:24] like for example here the lower the SQL
[21:49:27] will not use the index so that means the
[21:49:30] whole index is now useless and the SQL
[21:49:33] is not using it and that's why we
[21:49:35] consider it as a bad practice to use
[21:49:37] functions for the wear clause and
[21:49:40] Instead of that the good practice is
[21:49:42] that to not use any function and to
[21:49:44] write exactly the value that is used
[21:49:46] inside your data and with that the SQL
[21:49:48] going to be happy and use the index that
[21:49:51] you have created. Okay, let's have
[21:49:52] another example about this rule and here
[21:49:54] we are selecting all the customers where
[21:49:56] the first name start with the A. So with
[21:49:58] that we can go and use the function
[21:50:00] substring in order to get the first
[21:50:03] character of the first name and once you
[21:50:05] match it with a then you will get the
[21:50:07] result and here we have Anna. And this
[21:50:08] is again bad if you have an index on the
[21:50:10] first name and that's because we are
[21:50:12] applying a function on the column. So
[21:50:15] this considered to be a bad practice and
[21:50:17] instead of that we can go and use the
[21:50:18] help of the like. So we can go and
[21:50:21] search for this pattern where it start
[21:50:22] with the A and then we have a white
[21:50:24] card. We don't care about the rest. So
[21:50:26] it must start with a. So if you go and
[21:50:28] execute it you will get the same
[21:50:30] results. So try as much as you can to
[21:50:32] avoid the functions in the wear clouds
[21:50:34] in order to hit and get the index
[21:50:37] working. And in many scenarios, we have
[21:50:38] a workaround in order to use the
[21:50:40] function without transformations. So try
[21:50:43] your best to avoid using functions if
[21:50:45] your columns having an index. All right,
[21:50:47] one more example that you see a lot on
[21:50:49] queries that you filter by the year. So
[21:50:52] we are searching for the orders that
[21:50:54] happens in 2025 and we usually go and
[21:50:56] use the year order dates. And now if you
[21:50:59] have an index on the order dates, this
[21:51:01] again will not be working because you
[21:51:03] are using a function year. So this
[21:51:05] considered to be a bad practice. Instead
[21:51:07] of using the year function, you can go
[21:51:09] and use between. So we don't apply a
[21:51:12] function on the order date and we say
[21:51:13] the order date is between the boundaries
[21:51:16] of the year. Of course, now our query is
[21:51:18] not looking really cool and easy like
[21:51:20] the first one. But still with the second
[21:51:22] one, we are hitting the index. So again
[21:51:25] while you are filtering, try to not use
[21:51:27] functions on the columns because it is
[21:51:29] really waste if you have an index and
[21:51:31] you are not using it. and most of the
[21:51:33] cases you have like a workound for your
[21:51:35] function. So those are the three
[21:51:36] examples that I wanted to show you about
[21:51:38] this tip. All right, moving on to a
[21:51:40] similar one. It says avoid leading wild
[21:51:42] cards as they prevent index usage. So
[21:51:45] this is a similar one. Let's say for
[21:51:47] example I'm searching for the word gold
[21:51:49] inside the last name. And here we have
[21:51:51] to be careful what we are searching for.
[21:51:53] Should the gold exist somewhere in the
[21:51:56] last name or only we are searching for
[21:51:58] the last name that start with gold? If
[21:52:01] it's like that we are searching only the
[21:52:03] last name that starts with gold then we
[21:52:05] are doing it here wrong. And in SQL if
[21:52:07] you're using the leading wild card then
[21:52:10] the SQL will not be using the index. But
[21:52:12] if you are using the wild card at the
[21:52:14] end and the trailing this one is fine
[21:52:16] and will not avoid using the index. So
[21:52:19] this considered as a bad practice
[21:52:21] because you will not be hitting the
[21:52:23] index. Better than that to not use the
[21:52:25] white card as a leading and if that's
[21:52:27] enough for your search then with that
[21:52:29] you are hitting and using the index.
[21:52:32] Okay, moving on to the next one. It says
[21:52:34] use in instead of multiple or or
[21:52:37] operator is very evil for performance
[21:52:40] and try to avoid using it. It really
[21:52:42] kills your performance whether it is in
[21:52:44] the filters or joins and so on. So now
[21:52:46] we want to show the orders where the
[21:52:47] customers is equal to one or two or
[21:52:50] three. And of course this is considered
[21:52:52] to be bad practice and hard to read and
[21:52:54] so on. Please don't do that. Instead we
[21:52:56] have the in operator and we are saying
[21:52:58] if the customer is one of those values
[21:53:01] then show the orders. So if you go and
[21:53:03] run it you will get the exact results
[21:53:06] and it's not only looks nicer than the
[21:53:08] first query but it has as well a better
[21:53:11] performance. So if you find out writing
[21:53:13] a lot of ors think about the inoperator.
[21:53:16] So those are the best practices for
[21:53:17] filtering data to improve the
[21:53:19] performance.
[21:53:24] Okay, so now we're going to focus on how
[21:53:26] to optimize joining tables in SQL. So
[21:53:28] the first tip here is to understand the
[21:53:31] speed of joins and to use inner join
[21:53:34] when it's possible. Well, as we learned
[21:53:35] before, we have like different types of
[21:53:37] joins. We have the inner, left, right,
[21:53:39] and outer join. And if we talk about the
[21:53:41] performance, the best performance you
[21:53:43] will get from the inner join. And that's
[21:53:45] because SQL going to work only on the
[21:53:47] matching rows. That means the effort and
[21:53:49] the processing time is better than the
[21:53:52] other joins. Now in the next one in
[21:53:54] ranking we have the left and right
[21:53:56] joins. They are slightly slower than the
[21:53:58] inner join because usually they process
[21:54:01] more data and more rows than the inner
[21:54:03] join because SQL will work not only with
[21:54:06] the matching rows as well with the
[21:54:08] unmatching rows. So for right and left
[21:54:10] SQL has to do more stuff than the inner
[21:54:12] join. And now the worst type of joins we
[21:54:15] have the outer join. And that and that's
[21:54:17] because this type works with the biggest
[21:54:20] number of rows compared to the other
[21:54:21] types. It's going to present unmatching
[21:54:23] rows from the left and from the right
[21:54:25] tables. So that means SQL has a lot of
[21:54:28] to-do and that's why this join has the
[21:54:30] worst performance. So here my advice is
[21:54:33] always try to use the inner join if it's
[21:54:36] enough to work with the matching rows
[21:54:38] and if the matching rows is not enough
[21:54:40] then go with the lift join maybe. But
[21:54:42] try your best always to bring the inner
[21:54:44] join instead of lift join. But don't
[21:54:46] forget inner join filters the data.
[21:54:49] Okay. The next one it says use explicit
[21:54:51] join the unzi join instead of implicit
[21:54:55] join. Well it is considered as a bad
[21:54:57] practice if you join tables like this
[21:54:59] the implicit join or the nonzi join.
[21:55:02] It's better to use the normal modern
[21:55:04] join where you use the inner join for
[21:55:06] example. about the performance. There is
[21:55:08] like no differences between them. And
[21:55:10] for this scenario, it's very simple. But
[21:55:12] if you have like a complex query, then
[21:55:14] joining table like this might be very
[21:55:16] confusing and really hard to read and as
[21:55:19] well complex to optimize. That's why the
[21:55:21] best practice says go with the normal
[21:55:23] inner join. So go with the anzi join
[21:55:26] instead of the nonzi join. Okay. To the
[21:55:29] next tip. Make sure to index the columns
[21:55:31] used in the on clause. So we have to go
[21:55:34] and make sure that both of those columns
[21:55:36] has an index because indexes speed up
[21:55:39] the lookup process. Without an index,
[21:55:41] the SQL might go and do a full table
[21:55:44] scan. Without an index on those columns,
[21:55:46] the database might go and scan the
[21:55:48] entire tables in order to find a match.
[21:55:51] And that is really slow if you have big
[21:55:53] tables. So now if you go to the
[21:55:54] customers over here and then to the
[21:55:56] indexes, we can see that we have an
[21:55:59] index, a clustered index for the
[21:56:01] customer ID. But if you check the
[21:56:03] customer ID in the orders, we don't have
[21:56:06] an index for that. So this one doesn't
[21:56:08] have an index. So in order to fix that,
[21:56:10] we're going to go and create an
[21:56:11] uncclustered index on the table orders
[21:56:14] for the customer's ID since it is a
[21:56:16] foreign key. So once we do that, we have
[21:56:19] now an index for both of those columns
[21:56:21] and with that our join going to be
[21:56:23] faster. Okay. So now we come to a tip
[21:56:25] where we say really it depends on there
[21:56:28] is like not one clear way on how to do
[21:56:30] it. But let's say if you have a big
[21:56:32] tables, it is better to filter data
[21:56:34] before joining. And here we have like
[21:56:36] three different scenarios that going to
[21:56:38] deliver the same results. But of course
[21:56:40] the question is which one is the best
[21:56:41] for performance. So now let's have a
[21:56:43] look to them. What we are doing here we
[21:56:45] are just joining two tables and then we
[21:56:47] are filtering the result based on the
[21:56:49] order status that comes from the orders.
[21:56:52] So in the first query what we are doing
[21:56:53] we are first joining tables and at the
[21:56:56] ends we are using where clause in order
[21:56:58] to filter the data. So by looking to
[21:57:00] this we are just filtering the data
[21:57:01] after joining the tables. But there is
[21:57:03] another way on how to do it. You can go
[21:57:05] and join the tables but on the join
[21:57:08] condition you can go and add this order
[21:57:11] status equals to delivered. So we are
[21:57:13] matching the data by the customer ID and
[21:57:15] at the same time we are filtering the
[21:57:17] data by the order status since we are
[21:57:20] using the inner join. So the filtering
[21:57:22] is happening during the join or you can
[21:57:24] do it like this where we have here more
[21:57:26] stuff to be added where we don't join
[21:57:28] the table directly with the orders. We
[21:57:30] first prepare the table orders before
[21:57:33] joining it with the customers. And here
[21:57:35] our preparation is we are just selecting
[21:57:37] the columns that we need and we are
[21:57:39] already filtering the data before doing
[21:57:42] the join using the subquery. But if you
[21:57:44] run all those queries you will get the
[21:57:46] exact same results. And of course there
[21:57:48] is another way on how to do it. you can
[21:57:50] go and prepare the data not in subquery
[21:57:53] you can go and use a CTE and then join
[21:57:55] the result of the CTE with the table
[21:57:57] customers. So now about the performance
[21:57:59] if your query is like small not that
[21:58:02] complex and as well you don't have a big
[21:58:04] data inside your tables all those three
[21:58:07] queries going to deliver the same
[21:58:09] performance. I know it might sounds
[21:58:11] weird because here we are like filtering
[21:58:13] after joining or here we are filtering
[21:58:15] during the join. Normally in databases
[21:58:18] the SQL optimizers are now very smart
[21:58:20] can understand that there is a filter
[21:58:22] here and decide on the best execution
[21:58:25] plan for you. So actually wherever you
[21:58:27] put your filter after, during or before
[21:58:30] the SQL is smart enough to do it
[21:58:32] correctly. So if you don't have complex
[21:58:34] query and you don't have like big
[21:58:36] tables, go with the one that suits you.
[21:58:38] And I really recommend you to go with
[21:58:40] the first one because it's logical and
[21:58:42] easier to understand. But if you have
[21:58:44] big tables and complex queries, the best
[21:58:46] practices says try always to prepare the
[21:58:49] data before joining it. So try to
[21:58:51] isolate and abstract the pre-step in a
[21:58:54] subquery or in a CTE before joining it
[21:58:57] with any other tables. And in many
[21:58:59] scenarios in my project where I have a
[21:59:01] big table, this did help where the
[21:59:03] execution plan was better if I isolate
[21:59:05] and prepare the data before joining it.
[21:59:07] So if you have small or medium tables,
[21:59:10] go with the normal way, use the wear
[21:59:11] clause. But if you have complex big
[21:59:13] tables, prepare the data in subquery or
[21:59:16] CTE and then join it with the tables.
[21:59:19] Okay. And now moving on to tip number
[21:59:21] 12. It is similar to the previous one
[21:59:24] but this time it says aggregate data
[21:59:26] before joining tables and again it is
[21:59:28] special case to improve the performance
[21:59:30] of big tables. So now we have the
[21:59:32] following scenario where we are joining
[21:59:34] the orders and the customers and we are
[21:59:36] aggregating the data by the customer ID
[21:59:39] but we are just joining the table
[21:59:40] customers because we need the first
[21:59:42] name. So as a result we have the
[21:59:44] customer ID, the first name and the
[21:59:46] order count. So the standard way is to
[21:59:48] join the tables and then do a group by
[21:59:51] in order to summarize the data. Now if
[21:59:53] you look to this query, we actually
[21:59:54] don't need the join in order to do the
[21:59:56] aggregations. We can do first the
[21:59:59] aggregation like preparing the orders
[22:00:01] with the aggregated data and then join
[22:00:04] the result with the customers in order
[22:00:06] to get the first name. So again we
[22:00:09] prepare first and then we do the join
[22:00:11] and we can do that using either the
[22:00:13] subqueries or using the CTE. So in this
[22:00:16] scenario first we are doing the group by
[22:00:18] we are aggregating the data and the
[22:00:20] result of this is joined with the
[22:00:22] customers tables in order to get the
[22:00:23] first name. Now of course there are like
[22:00:25] many ways on how to do it like for
[22:00:26] example as well using the correlated
[22:00:28] queries where we can go and use the
[22:00:31] subquery in the select statements and
[22:00:33] then use the where condition over here
[22:00:35] to make the correlated query. Now all
[22:00:37] those three going to deliver the same
[22:00:39] results but the question here again
[22:00:41] which one has the best performance?
[22:00:43] Well, I can go immediately and tell you
[22:00:45] that correlated subqueries are the worst
[22:00:48] one. Always avoid using correlated
[22:00:51] subqueries. They has really bad
[22:00:52] performance. And that's because SQL
[22:00:55] going to go and do the aggregations for
[22:00:57] each customer individually. So it's
[22:01:00] going to go like for each row and doing
[22:01:01] aggregation then to the next row and so
[22:01:03] on. So it takes long time. So this is
[22:01:06] bad practices. Don't use it. Now we are
[22:01:08] left again with the first option and the
[22:01:11] second option. And here my tip going to
[22:01:13] be like the previous one. I'm going to
[22:01:14] say if you have small to medium size of
[22:01:17] tables then go with this one because it
[22:01:19] is easier to read and to understand and
[22:01:22] you will gain exactly the same
[22:01:23] performance as this subquery. But if
[22:01:26] your tables are big the best practices
[22:01:28] is to prepare first the data to group up
[22:01:31] the data to filter the data and to
[22:01:33] isolate it in a subquery or a CTE before
[22:01:36] joining it with the final table in the
[22:01:39] final query. But again here only for big
[22:01:41] tables and always test check the
[22:01:43] execution plan whether you are really
[22:01:45] getting any benefits from it. All right.
[22:01:47] So if you have big tables try to prepare
[22:01:49] the data first in city subquery and then
[22:01:51] join. Okay moving on to the next tip. It
[22:01:54] says use union instead of or operator in
[22:01:57] joins. So what this means sometime let's
[22:02:00] say that you are joining two tables the
[22:02:02] customers and the orders. And now about
[22:02:04] the join key, you can see over here it
[22:02:06] says the customer ID should be equal to
[22:02:08] the customer ID from the orders or the
[22:02:11] customer ID should be equal to the
[22:02:12] saleserson's ID. If one of these two
[22:02:15] conditions is fulfilled, then we have a
[22:02:17] match. And I can tell you the or
[22:02:19] operator over here is a performance
[22:02:21] killer. It has really bad performance.
[22:02:24] So try to avoid it. Don't use ore in the
[22:02:26] joins. It has a lot of problems like it
[22:02:29] avoid indexes, it create like loop joins
[22:02:32] and so on. That's why we consider it as
[22:02:35] a bad practice. And now in order to get
[22:02:37] the same results, we can go and split
[22:02:40] the joins. So we can go and have two
[22:02:42] queries. The first query is joining the
[22:02:44] data based on the customer ID and the
[22:02:47] second query based on the saleserson and
[22:02:50] then we go and merge those two results
[22:02:52] using the union. It sounds like bigger
[22:02:54] and too much for the SQL but with this
[22:02:57] you will get better performance than
[22:02:59] using this simple or operator. So again
[22:03:02] if you have big tables try to avoid
[22:03:04] using or and instead of that go and use
[22:03:07] union. Okay the next tip says check for
[22:03:10] nested loops and use SQL hints. Now
[22:03:13] imagine that we have like big tables and
[22:03:15] we are joining tables. So now if you are
[22:03:17] checking the execution plan you have to
[22:03:19] check always the join type. So for
[22:03:21] example here it is using the nested
[22:03:23] loops which is of course is okay because
[22:03:26] we have small tables but if you have big
[22:03:28] tables and still SQL is using for some
[22:03:30] reason the nested loops then this is
[22:03:33] alerting. So in order to change this
[22:03:35] what we can do we can go and use the SQL
[22:03:37] hints in order to force SQL to use the
[22:03:40] hash join. Hash join is really good if
[22:03:42] you have a big table like for example
[22:03:44] the orders that is joins with a small
[22:03:47] table like the customers. So now what we
[22:03:49] can do at the end we can write over here
[22:03:50] option hash join. So let's go and
[22:03:53] execute it and let's check the execution
[22:03:55] plan and with that we have forced SQL to
[22:03:58] use the hash join or hash match. Again
[22:04:01] you have here really to evaluate your
[22:04:02] tables. If you have like small tables
[22:04:05] don't bother with that. But if you have
[22:04:07] big tables and SQL still doing the
[22:04:09] nested loops, nested loops are usually
[22:04:11] very slow because you have a lot of
[22:04:13] iterations and so on and with the hash
[22:04:15] join that small table going to be stored
[22:04:17] in the memory and then you have really a
[22:04:19] quick matching between the two tables.
[22:04:22] So those are all the best practices and
[22:04:24] tips on how to optimize joining tables
[22:04:26] in SQL. All right, so now we're going to
[22:04:28] talk about union and here is the best
[22:04:29] practices. It says use union all instead
[22:04:32] of using union if duplicates are
[22:04:35] acceptable. So it's very simple. If the
[22:04:37] duplicates are acceptable or let's say
[22:04:39] that there is no duplicates then don't
[22:04:42] go with the union because it needs more
[22:04:44] time to be executed. SQL has to go and
[22:04:46] check row by row whether we have
[22:04:48] duplicates or not and this usually takes
[22:04:51] longer time than using the union all. So
[22:04:54] if duplicates are acceptable or you
[22:04:55] don't have any duplicates in your data
[22:04:57] go with the union all just have to go
[22:05:00] and merge all the data without checking
[22:05:02] anything and the performance going to be
[22:05:04] faster. All right, the next one is
[22:05:05] little bit tricky. So it says use union
[22:05:08] all together with the distinct instead
[22:05:10] of using union if the duplicates are not
[22:05:13] acceptable. So you want to remove the
[22:05:15] duplicates. So we have learned that in
[22:05:17] order to do that we're going to go and
[22:05:18] use the union. It's going to go and
[22:05:20] merge the data and as well remove the
[22:05:22] duplicates which is really okay to use
[22:05:24] it if you have like smaller data or
[22:05:26] medium. But let's say that you have like
[22:05:28] millions of row which is really okay if
[22:05:30] you have like medium and small tables.
[22:05:32] But again here if you have huge tables
[22:05:34] big tables hundreds of millions the best
[22:05:37] practice says go with the union all and
[22:05:40] afterwards use a distincts. So in the
[22:05:43] sub query we are using union all but in
[22:05:46] order to remove the duplicates we use
[22:05:48] the distincts. But again here you have
[22:05:50] to test it to check the execution plan.
[22:05:52] If you are getting benefit then go with
[22:05:54] this version. But if your data is not
[22:05:56] really big you have hundred of
[22:05:57] thousands. So go just with the normal
[22:06:00] union. the code is smaller and you will
[22:06:02] get the same effects but only for large
[22:06:04] tables you can go with this best
[22:06:06] practice. So that's all what I have for
[22:06:08] you for the
[22:06:09] [Music]
[22:06:12] union. Okay. So now let's talk about
[22:06:14] aggregations and here the tip says use
[22:06:17] column store index for aggregations on
[22:06:19] large tables like for example fact
[22:06:21] tables and that's because column store
[22:06:23] index going to compress the data. So the
[22:06:25] size of the data going to be smaller and
[22:06:27] as well the aggregation is super fast
[22:06:30] because we are selecting only the
[22:06:32] relevant informations only the relevant
[22:06:34] columns. So it makes it a perfect setup
[22:06:37] for aggregating large tables. And now
[22:06:39] let's say that we have hundreds of
[22:06:40] millions of orders and we have this
[22:06:42] query over here. So the best practice
[22:06:44] says convert this table to a clustered
[22:06:47] column store index. So if you go and
[22:06:49] create this clustered index over here,
[22:06:52] the whole table going to have amazing
[22:06:54] performance for aggregations like this.
[22:06:56] All right. So to the next one, it says
[22:06:59] pre-agregate data and store it in a new
[22:07:02] table for reporting. So let's say that
[22:07:04] we have like a big query where we are
[22:07:06] aggregating the data and so on. And this
[22:07:08] query takes really long time. Let's say
[22:07:10] like 5 minutes or something like that.
[22:07:12] But now the problem with that I would
[22:07:14] like to show the results as a report
[22:07:16] maybe to my manager or let's say during
[22:07:18] a meeting it's going to be really bad if
[22:07:20] everyone have to wait until the query is
[22:07:23] done. So the best practice here if you
[22:07:25] have like a query that runs very slow
[22:07:28] what you can do you can go and store the
[22:07:30] results in a table. So if I go over here
[22:07:33] and say into sales summary what going to
[22:07:36] happen going to store the result inside
[22:07:39] this table. So let's go and execute it.
[22:07:41] And now with that we have a nice table
[22:07:43] where everything is prepared. So all
[22:07:44] that you have to do is to go and query
[22:07:47] this table. And of course it's going to
[22:07:48] be very fast because it's only select
[22:07:50] statements. And with that you have like
[22:07:52] prepared and pre-agregated the data to
[22:07:55] have like fast reports. So don't forget
[22:07:58] about this. If you have a big query you
[22:08:00] can insert the result of this query in a
[22:08:02] new table in order later to use it for
[22:08:04] reporting. But one thing that you have
[22:08:06] to make sure that you have always to
[22:08:08] update this table. So if we have new
[22:08:10] orders, it will not be presented inside
[22:08:12] the sales summary. You have to go and
[22:08:15] run this query again in order to get new
[22:08:17] data inside the sales summary. So those
[22:08:19] are the tips on how to improve the
[22:08:21] performance of your aggregations in
[22:08:27] SQL. So now what is happening here? I
[22:08:29] would like to show the orders but only
[22:08:31] from customers from USA. So if you check
[22:08:34] this query over here, we are joining the
[22:08:36] tables order and customers but mainly we
[22:08:39] are showing only the orders information
[22:08:41] and that means we are using the
[22:08:43] customers only to filter the table
[22:08:45] orders and there are like multiple ways
[22:08:47] on how to do this task. So it's not only
[22:08:50] the joins you can go and use the exist
[22:08:52] as a subquery and as well you can go and
[22:08:55] use the in operator in the subquery. And
[22:08:58] now comes the old but gold question.
[22:09:02] Which one is better? Should we join or
[22:09:04] use exist or in? And oh my god, if you
[22:09:07] go to the forums, you will see people
[22:09:09] fighting about which one is the best.
[22:09:14] Clean tech. Come on, do that again. Do
[22:09:16] that again. I dare you.
[22:09:19] Okay,
[22:09:21] bring it. Oh, you can't say you can't
[22:09:24] say one point. Two point. Now, about the
[22:09:27] best practices, everyone agrees that's
[22:09:30] don't go and use the in operator. So
[22:09:32] this is the bad practice. So
[22:09:36] bad practice avoid it. Don't use it. And
[22:09:39] of course I'm always speaking about big
[22:09:41] tables, okay? Not small tables. So we
[22:09:44] don't go and use this in order to filter
[22:09:47] one table based on the result of another
[22:09:49] table. So don't use any operator in this
[22:09:52] scenario. Now here comes the conflicts.
[22:09:54] We have join and exist. Well, about the
[22:09:57] performance of those two, they are very
[22:09:59] similar for medium tables. like I'm
[22:10:01] speaking about hundred or thousand and
[22:10:03] so on. But still you have to test it.
[22:10:05] You have to go and compare the execution
[22:10:07] plans and if you are getting like
[22:10:09] identical results and both of them are
[22:10:12] having the same speed then I prefer to
[22:10:15] go with the join and that's because to
[22:10:17] be honest it is easier to write than
[22:10:19] writing that exists. So I'm going to say
[22:10:21] from my point of view this is best
[22:10:24] practice if the
[22:10:27] performance
[22:10:29] equal to exist. But now what happens for
[22:10:32] me is that sometimes I get better
[22:10:34] performance using exists. So I'm going
[22:10:37] to say from my point of view the best
[22:10:39] practice
[22:10:40] here. And now you might ask why we are
[22:10:43] getting with the exist better
[22:10:44] performance than in the inner join. And
[22:10:46] that's because SSQL has only to check
[22:10:48] the existence of data from the subquery.
[22:10:51] But in the other hand with the inner
[22:10:52] join SQL has to go and start doing
[22:10:55] matching between two tables. So it can
[22:10:57] go and evaluate all matching records and
[22:11:00] so on. It is not evaluating whether it
[22:11:03] exist or not. And as well sometimes SQL
[22:11:05] has to deal with more rows because you
[22:11:08] might introduce duplicates as you are
[22:11:10] joining tables. And this will not happen
[22:11:12] using exists. So for some scenarios if
[22:11:14] you are using exist you might get better
[22:11:17] performance than using join but everyone
[22:11:20] agrees to not use the end operator. Okay
[22:11:22] the next tip is to avoid redundant logic
[22:11:25] in your query. This happens a lot if you
[22:11:28] have a lot of sub queries and if you
[22:11:30] analyze it you might find sometimes
[22:11:32] there is like redundancy. So for example
[22:11:34] this query I would like to have like a
[22:11:36] tag for each employee whether the salary
[22:11:39] is above the average or below the
[22:11:40] average. So now we might do it like
[22:11:42] this. we say okay let's get the data for
[22:11:44] employees where the salary is higher
[22:11:46] than the average and you go and
[22:11:48] calculate the average in a subquery. So
[22:11:50] if it's higher then you write here above
[22:11:53] average and now we say okay let's go for
[22:11:55] the below average. So we do a union all
[22:11:57] and the condition going to be salary is
[22:11:59] less than the average. And now by
[22:12:01] checking this you see that there's a
[22:12:03] problem. First of all we are querying
[22:12:06] the employees like four times. We have 1
[22:12:08] 2 3 4. So we are scanning the table
[22:12:12] employees four times and as well we have
[22:12:14] the same logic over here. So we are
[22:12:16] calculating the average of salary at
[22:12:18] twice. So this is of course I can say a
[22:12:21] bad practice and there is like many ways
[22:12:23] on how to do it better than that. For
[22:12:25] example, you can go and put this
[22:12:26] subquery in CTE and then use it multiple
[22:12:29] times. But there is like better solution
[22:12:31] using the window function. So if you
[22:12:33] check this, it is very simple. Let's me
[22:12:34] execute it. We are reading the table
[22:12:36] employees only once and then we are
[22:12:39] using the case statements. If the salary
[22:12:41] is higher than the window function. So
[22:12:43] we are calculating the average on top of
[22:12:45] the whole table employees. If it's
[22:12:48] higher then write above average. If it's
[22:12:50] lower then below average. So as you can
[22:12:52] see it is easier to read and it is
[22:12:54] smaller and the performance here is way
[22:12:56] better than reading four times the
[22:12:58] employees and repeating the same logic.
[22:13:00] So here you have always to look to your
[22:13:02] queries and if you see that you are
[22:13:04] repeating the same things over and over
[22:13:06] then you are writing a bad query. Think
[22:13:08] about alternatives like CTE window
[22:13:11] functions and I'm sure you will find a
[22:13:12] better way than reading the table
[22:13:15] several times or repeating the same
[22:13:17] logic several times. So as you can see
[22:13:18] optimizing the queries is not always
[22:13:20] about using indexes and partitions. It's
[22:13:23] all about using best practices. All
[22:13:25] right guys, so with that we have covered
[22:13:27] a lot of best practices on how to
[22:13:29] optimize the performance of your query.
[22:13:31] And as you can see it's not always
[22:13:33] creating indexes, right? In many
[22:13:34] scenarios it's about how you write the
[22:13:40] query. And now in the next section I'm
[22:13:42] going to show you the best practices on
[22:13:45] how to create tables. So the best
[22:13:47] practices of DDL data definition
[22:13:50] language. If you have a poor definition
[22:13:52] of your tables, this has a great impact
[22:13:54] on the performance of your queries. All
[22:13:56] right. So now we have here like a DDL in
[22:13:58] order to create a table customer info
[22:14:01] and it is not really following best
[22:14:02] practices. So let's go through it one by
[22:14:05] one. The first tip is try to avoid the
[22:14:07] data types varchar and text if it's
[22:14:10] possible. The vchart and text they are
[22:14:13] like one of the worst data types for
[22:14:15] performance because they consume a lot
[22:14:17] of resources whatever you do like for
[22:14:19] example if you are sorting the data by a
[22:14:21] column that is var or text it is very
[22:14:23] expensive operation the same thing if
[22:14:25] you go like and create an index on top
[22:14:27] of such a column it's going to be as
[22:14:28] well expensive and they cause a lot of
[22:14:30] problems with the data fragmentations
[22:14:32] and many issues. So try as much as you
[22:14:35] can to skip those data type if it's
[22:14:37] possible. So now let's go and review all
[22:14:39] those columns in order to see whether we
[22:14:40] can change something about it because it
[22:14:43] has a lot of bar charts. So the first
[22:14:44] one over here we have is var because it
[22:14:46] is the first name. Well, it is okay. Now
[22:14:49] moving on to the next one. We have the
[22:14:50] last name as a text which is not really
[22:14:52] good because text is worse than vchar.
[22:14:55] So it's better to use var than a text.
[22:14:57] So here we have to fix it. So var and
[22:15:00] I'm going to go with the links 50. Now
[22:15:02] moving on to the countries. So the
[22:15:04] country is going to be vartar. We cannot
[22:15:06] change that. that contain characters. So
[22:15:08] the next one is the score of the
[22:15:10] customer. H here we can do something
[22:15:12] about it because scores are only
[22:15:14] numbers. So that's why we can go and
[22:15:16] skip this one. So let's remove it and
[22:15:19] say you are integer and with that we
[22:15:21] have avoided using the varchar. And the
[22:15:23] same thing goes for the birthday. The
[22:15:25] birthday is a date and here we have it
[22:15:27] as a vchar. Well this is not really good
[22:15:29] and we can skip that by having this
[22:15:31] column as a date. So date is way better
[22:15:34] than having a vchar. All right. And the
[22:15:36] next one is integer. So with that we
[22:15:38] have fixed few stuff. So we have fixed
[22:15:40] the score and the birthday. And with
[22:15:42] that we have saved some storage. If we
[22:15:44] have an index on the score it's going to
[22:15:45] be way better than having a var. And if
[22:15:48] you are filtering the data based on the
[22:15:50] birthday it's going to be faster. So
[22:15:51] again try your best to avoid the vchar
[22:15:54] and the text. I have seen in many
[22:15:55] projects that a lot of developers tend
[22:15:57] to use the vchar and I understand it is
[22:16:00] easier to make everything as a vchar
[22:16:02] than deciding whether it is an integer,
[22:16:04] date, float and so on because you can
[22:16:06] fit everything in the vchar and text but
[22:16:09] this is lazy. Take time to understand
[22:16:11] the content of this column and try to
[22:16:13] assign it to the correct data type
[22:16:15] because this has really impact on the
[22:16:17] performance. Okay, to the next one it
[22:16:19] says avoid using max or overly large
[22:16:22] lengths. So now we have to keep our eyes
[22:16:24] on the links of each data type
[22:16:26] especially the bar charts. Not only it
[22:16:28] going to waste like a lot of storage.
[22:16:30] It's also going to like mislead the SQL
[22:16:33] by creating large indexes which is
[22:16:36] totally unnecessary because the data
[22:16:38] itself is small but because you have
[22:16:40] defined like a large length SQL going to
[22:16:43] check those informations and make
[22:16:44] decision to make a big index and large
[22:16:47] indexes are always problematic because
[22:16:49] they're going to slow everything down by
[22:16:50] sorting the data by retrieving data by
[22:16:53] updating the index. So it is really bad
[22:16:55] practices if you go blindly and define
[22:16:58] everywhere max or
[22:17:00] 255. Again give it a chance to think
[22:17:03] about each column and predict a length
[22:17:05] for it. So for example if you check over
[22:17:07] here we are saying first name v chart
[22:17:09] max. Well most of the first names are
[22:17:11] short. So we don't need like the maximum
[22:17:14] size of a v chart to fit a first name.
[22:17:17] So here we can go easily instead of max
[22:17:19] with the 50. And the same thing goes for
[22:17:22] the column country. We don't need 255
[22:17:24] characters for the country name. We can
[22:17:27] go with something more realistic like
[22:17:29] around 50. I think you can even go
[22:17:31] smaller, but it's fine to have 50. So,
[22:17:33] the best practice here is to analyze
[22:17:35] your data and to predict the size of
[22:17:38] each column. And don't be lazy by just
[22:17:40] defining max everywhere. I know it's
[22:17:42] faster, but it's bad for performance.
[22:17:44] Okay. What do you have else? Use the
[22:17:47] constraint nutnull as much as possible.
[22:17:50] The nutnull is amazing. It has a lot of
[22:17:52] advantages. Of course, the biggest
[22:17:53] advantage is that's the data integrity
[22:17:55] of your table. So with that, you make
[22:17:57] sure no nulls are inserted in specific
[22:18:00] column. But it is as well good practices
[22:18:02] to use it for improving the performance
[22:18:05] because if you are creating an index,
[22:18:06] you're going to get a better index
[22:18:08] performance since SQL knows there is no
[22:18:10] nulls inside my tree inside the index.
[22:18:13] And in the other side, if you are
[22:18:14] writing query, we tend to use a filter
[22:18:17] where we say a specific column should
[22:18:19] not be null. But if you make sure that
[22:18:21] in the DDL it is not null then you can
[22:18:23] skip this filter and with that you are
[22:18:25] reducing the size of your query. So what
[22:18:27] we're going to do we're going to go
[22:18:28] through all the columns and decide
[22:18:29] whether it is not null and null. So for
[22:18:31] example the first name and the last name
[22:18:33] they should not be null. So that's why
[22:18:35] I'm going to say not null and the same
[22:18:37] thing for the last name not null. For
[22:18:39] the customer ID we're going to talk
[22:18:40] about it soon because we're going to
[22:18:41] convert it to primary key and primary
[22:18:43] keys are usually not null. So now for
[22:18:45] the country we make have it in the
[22:18:47] business that it should not be null. So
[22:18:49] we go and make a constraint about it.
[22:18:51] Now about the total purchases and
[22:18:53] scores. If it is new customer, maybe we
[22:18:55] can have a null inside our data. So
[22:18:57] we're going to leave it empty. And I
[22:18:58] think birthday is going to be usually
[22:19:00] optional. So we're going to leave it as
[22:19:01] well. And whether the customer is
[22:19:03] employee or not. This could be as well a
[22:19:06] null. So with that we have found out
[22:19:08] like three columns where we can have a
[22:19:10] constraint about the not null. And if we
[22:19:11] go and create like an index on the
[22:19:14] country, it's going to be a better
[22:19:15] index. Okay. Moving on to the next one.
[22:19:17] It says make sure that all your tables
[22:19:19] inside the database have a clustered
[22:19:22] primary key and as well it can help you
[22:19:24] building the relationship between tables
[22:19:26] where you have primary keys and foreign
[22:19:27] keys and you can join tables then very
[22:19:30] easily and as well a primary key has
[22:19:32] importance for the performance and
[22:19:34] incale server the default going to be a
[22:19:36] clustered index which is really good to
[22:19:38] have an index on the primary key because
[22:19:40] sometimes you are doing like an update
[22:19:42] operations or delete operations it's
[22:19:44] going to help up by the lookups of
[22:19:46] joining tables. So there are a lot of
[22:19:48] performance benefits of having a primary
[22:19:50] key and make sure that all your tables
[22:19:52] having a primary key. So as you can see
[22:19:54] the issue of our table we don't have a
[22:19:56] primary key and our primary key going to
[22:19:58] be the customer ID. So let's go and do
[22:19:59] that primary key and as I said as a
[22:20:03] default it can be clustered but I'm
[22:20:05] going to write it down in case if you
[22:20:07] are working with different databases
[22:20:08] make sure it is clustered. Okay moving
[22:20:11] on to the next one. It's not only about
[22:20:12] the primary key we have to take care of
[22:20:14] our foreign keys. So the best practice
[22:20:16] says create non-clustered index for the
[22:20:20] foreign keys if they are frequently
[22:20:22] used. The foreign keys are usually
[22:20:24] important in order to connect and join
[22:20:26] two tables and usually we frequently use
[22:20:28] it and not only that we use it sometimes
[22:20:30] in order to filter the data and if you
[22:20:33] create a nonclustered index for that it
[22:20:35] can improve the speed. So what we can do
[22:20:37] it's very simple we're going to go and
[22:20:38] create a nclustered index on our table
[22:20:41] customers info for the foreign key
[22:20:44] employee ID. So how to do it is very
[22:20:46] simple. We're going to go and say create
[22:20:48] nonclustered index on our table the
[22:20:50] customer's info on our foreign key the
[22:20:52] employee ID. But again make sure that
[22:20:54] this is an important foreign key that is
[22:20:57] used frequently from your queries. All
[22:20:59] right friends so as you can see there
[22:21:01] are a lot of best practices on how to
[22:21:02] improve and optimize the DDL. Having a
[22:21:05] healthy DDL can improve the performance
[22:21:07] of your queries. Now in the next section
[22:21:09] I'm going to show you the best practices
[22:21:11] and tips and tricks about indexing. So
[22:21:13] let's go.
[22:21:18] All right, the fifth best practices and
[22:21:20] the most important one is avoid
[22:21:22] overindexing because too many index is
[22:21:24] going to slow down the insert, update,
[22:21:26] delete operations and it's going to
[22:21:28] confuse as well the execution plan about
[22:21:30] choosing the right index and the
[22:21:32] performance of the whole system going to
[22:21:33] go down. And another tip is to monitor
[22:21:35] the usage of the indexes and I can tell
[22:21:38] you 90% of the indexes that is being
[22:21:41] created usually are not used at all. So
[22:21:43] they are taking a lot of space slowing
[22:21:46] down everything. So go and drop those
[22:21:48] unused indexes in your system. The next
[22:21:51] best practice is to have a regular job
[22:21:53] like maybe a weekly job. So first you
[22:21:56] have to update the statistics regularly
[22:21:58] as you are inserting new data and
[22:22:00] modifying data inside your database. The
[22:22:02] statistics and the metadata of your
[22:22:04] tables might get outdated and this is
[22:22:06] really bad because you will not get an
[22:22:08] optimal execution plan for your queries
[22:22:10] and this can slow down your queries of
[22:22:12] course. So regularly make sure that all
[22:22:15] the statistics are updated in order to
[22:22:17] have an optimal execution plan. And what
[22:22:19] else we can do in this weekly job is
[22:22:21] that we can go and rebuild and
[22:22:23] reorganize our indexes. And that is to
[22:22:26] make sure that we are preventing data
[22:22:28] fragmentations in our indexes. Data
[22:22:31] fragmentations in your indexes is really
[22:22:33] bad because there will be a lot of
[22:22:34] unused spaces. The order of your
[22:22:36] clustered index will not be correct. So
[22:22:39] make sure that at least weekly you are
[22:22:41] rebuilding and reorganizing all your
[22:22:43] indexes. So those are the best practices
[22:22:45] of improving the performance and
[22:22:47] optimizing your indexing. If you are
[22:22:49] struggling with very large tables in
[22:22:51] your projects like having fact tables,
[22:22:54] then go and use SQL partitioning in
[22:22:57] order to divide these tables into
[22:22:59] smaller pieces which can improve the
[22:23:02] performance whether you are reading data
[22:23:04] from the table or writing data. And of
[22:23:06] course you can go and mix things where
[22:23:09] you can go and apply a column store
[22:23:11] index on this partition table then you
[22:23:14] will get the best performance if you are
[22:23:17] having large
[22:23:21] tables. All right friends so that's all
[22:23:24] those are the best practices tips and
[22:23:26] tricks that I've collected in the many
[22:23:28] years working with SQL. And now my final
[22:23:31] thought about this is that try always to
[22:23:33] focus on making clear queries. Make it
[22:23:36] like easy to read and easy to understand
[22:23:38] and try to optimize the performance only
[22:23:41] if it's needed. So if you have like
[22:23:42] small database don't worry a lot about
[22:23:44] the performance because the SQL
[22:23:46] optimizer going to pick the best plan
[22:23:48] for you and focus only on having simple
[22:23:50] queries and if there is like performance
[22:23:53] problem always test using the execution
[22:23:55] plan. It should be your judge. So if you
[22:23:57] are applying any index or you are
[22:23:59] rewriting your queries always compare
[22:24:02] before and after using the execution
[22:24:04] plan. And if you are gaining more
[22:24:05] performance then adopt the new query or
[22:24:08] the new index. All right my friends. So
[22:24:10] that's all the tips and tricks best
[22:24:11] practices that I have for you in order
[22:24:13] to optimize the performance. And with
[22:24:15] that we have covered now everything
[22:24:16] about this chapter the performance
[22:24:17] optimization. Now in the next chapter
[22:24:19] I'm going to show you how I use AI in
[22:24:22] order to assist me while I'm using SQL.
[22:24:24] So let's
[22:24:28] go. All right. Right. So now I would
[22:24:30] like to share something important with
[22:24:32] you especially as a future developer
[22:24:34] that is working with AI. One of the best
[22:24:36] ways in order to truly build skill and
[22:24:39] to grow as a developer is by working on
[22:24:41] complex task and issue on your own. So
[22:24:44] when you are stuck on complex task and
[22:24:46] you are pushing yourself to find a
[22:24:48] solution for it and you are writing your
[22:24:50] code in yourself here the magic happens
[22:24:53] and the real learning can happen. And if
[22:24:56] you jump too quickly and ask the AI for
[22:24:58] a solution, what you are doing, you are
[22:25:00] skipping an essential step in order to
[22:25:02] become an expert. And more important
[22:25:04] than that, you won't develop skills in
[22:25:07] order to understand when and where the
[22:25:09] AI was wrong. So my recommendation here
[22:25:12] is to have a discipline. Always try to
[22:25:14] solve the task on your own and only turn
[22:25:16] to AI if you don't have any more ideas
[22:25:18] on how to solve the task. So that's my
[22:25:21] opinion and my advice for you.
[22:25:27] So quickly what is shippet? It is an AI
[22:25:29] program that is developed by open AI
[22:25:32] that is trained to understand questions
[22:25:34] and provide humanlike answers. So what
[22:25:37] GPT stands for? The G stands for
[22:25:39] generative. So that means the data model
[22:25:41] can generate a new content new text and
[22:25:45] P stands for pre-trained. The data model
[22:25:47] is already trained on huge amount of
[22:25:50] data. And the T stands for transformer.
[22:25:52] It is type of neural network
[22:25:54] architecture that processes your
[22:25:56] sentences in the prompts in order to
[22:25:58] understand the context behind it very
[22:26:01] fast and accurate. And in the other hand
[22:26:03] we have the GitHub copilot. It is
[22:26:05] developed by the GitHub and as well
[22:26:07] using the same data models from the open
[22:26:10] AAI. So that means both shad and copilot
[22:26:14] both of them are using the same language
[22:26:16] model that is developed from OpenAI. So
[22:26:19] the GitHub copilot did train on tons of
[22:26:22] codes that is available in GitHub. So
[22:26:24] how it works as you are writing a code
[22:26:26] in the code editor like for example
[22:26:28] visual studio it going to provide
[22:26:30] realtime suggestions as you are writing
[22:26:33] and typing your code. So now if we
[22:26:35] compare those two shad and the copilot
[22:26:37] we can say that the shajibet is a
[22:26:39] standalone application where you can
[22:26:42] interact with it using a website or an
[22:26:44] app where you go and start a
[22:26:45] conversation with the AI where in the
[22:26:48] other hand the copilot is directly
[22:26:50] integrated in your code editor like for
[22:26:52] example the visual studio code this is
[22:26:55] way better than shibility because you
[22:26:57] have realtime interaction with the AI
[22:26:59] this is a great advantage for the
[22:27:01] copilot because everything in one place
[22:27:03] so with the copilot pilot you are
[22:27:05] getting realtime assistant during your
[22:27:07] coding. So the main purpose of the ship
[22:27:10] is to have a conversation with the AI
[22:27:12] for any topic that you like not limited
[22:27:15] only for software developments but in
[22:27:18] the other hand a copilot focuses only on
[22:27:21] assisting the software development where
[22:27:23] you as a developer as you are writing
[22:27:25] your code you are getting auto
[22:27:27] completion of the code or maybe a block
[22:27:29] of code as a suggestion. So these are
[22:27:31] the key differences between shad and
[22:27:34] copilot.
[22:27:38] Now if you are doing software
[22:27:40] developments or you are working with
[22:27:42] data projects and of course it depends
[22:27:44] on your role in the projects there will
[22:27:46] be many different types of tasks and
[22:27:48] activities that should be done in the
[22:27:50] project like there will be a lot of
[22:27:52] brainstormings about new ideas and
[22:27:54] coding solutions debugging generating
[22:27:56] documentations discussing the different
[22:27:58] types of architecture doing road cause
[22:28:01] analyzes. So the spectrum of activities
[22:28:03] and tasks in each projects usually is
[22:28:06] very huge. And of course we can go and
[22:28:08] use the help of different AI tools to
[22:28:10] assist us with those tasks and
[22:28:12] activities and there is like not one AI
[22:28:14] tool that can cover all those stuff. I
[22:28:17] tend to jump between co-pilots and
[22:28:18] something like Shajbet. Okay. So now I'm
[22:28:20] going to go and map those different
[22:28:21] tasks to either sht or copilot. So now
[22:28:24] let's focus on the shibbet. The first
[22:28:26] one is brainstorming and ideas. So now
[22:28:29] if we have in our project a big task or
[22:28:31] let's say a big issue that we want to
[22:28:33] find solution for it. I tend to use of
[22:28:35] course tools like shad in order to have
[22:28:38] a discussion about the topic in order to
[22:28:41] explore and discuss multiple ideas and
[22:28:43] then start evaluating all those ideas.
[22:28:45] The next one where I found myself using
[22:28:47] shbt is doing the project planning. So
[22:28:49] it is as well something high level. You
[22:28:51] can go and discuss with the shaj GBT
[22:28:53] about the design of your projects and
[22:28:55] you can as well discuss the milestones
[22:28:58] the road map of the projects. The next
[22:29:00] thing that I find myself using shajbt is
[22:29:02] for learning knowledge and research. If
[22:29:04] you are working with big data projects
[22:29:06] you will be overwhelmed with the amount
[22:29:08] of cloud services and AI analytics
[22:29:11] tools. So and of course you can go and
[22:29:13] learn new stuff gather informations and
[22:29:15] knowledge using shajibb. Okay, moving on
[22:29:18] to the next task. We have generating
[22:29:19] documentations. Writing documentations
[22:29:21] is always painful process and consumes a
[22:29:24] lot of time and I tend to use tools like
[22:29:26] shibbit in order to generate those
[22:29:28] documentations. But of course, I always
[22:29:30] review the documentations and make it
[22:29:32] short. Okay, moving on to another topic
[22:29:34] where I use shadet is that to discuss
[22:29:36] architecture. Of course, if you are
[22:29:37] starting new projects, they will be like
[22:29:39] different types of architecture in order
[22:29:41] to implement the projects. And of
[22:29:43] course, you can discuss with the
[22:29:44] shajibility about the different types of
[22:29:46] architecture and if you give the
[22:29:47] specifications about your projects then
[22:29:49] you can discuss with the shajibility
[22:29:51] which architecture is suitable for the
[22:29:53] project. And another task that I find
[22:29:55] myself always like researching is
[22:29:58] exploring the best practices, tips and
[22:30:00] tricks. So you can have a discussion
[22:30:01] with the SHP about the recommendations,
[22:30:04] what are the best practices, what are
[22:30:06] the common pitfalls in order to make
[22:30:08] sure that your code and your solution is
[22:30:10] always up to date with the best
[22:30:12] practices. And one more thing, if
[22:30:14] there's like in the projects a very
[22:30:16] complex task, then I tend to have a
[22:30:18] discussion with a tool like Shajibet in
[22:30:20] order to break this complex task into
[22:30:22] small pieces and start finding the
[22:30:24] solution for each piece. And now in the
[22:30:26] other hand, I'm using copilot in order
[22:30:28] to solve different type of tasks. So
[22:30:30] here where I get my hand dirty in the
[22:30:32] code. So while I'm coding I'm using
[22:30:34] alltime co-pilot in order to assist me
[22:30:37] because it provide directly inline
[22:30:39] suggestions and help me to code faster
[22:30:41] and reduce the human error that I might
[22:30:43] make. So while I'm writing a code or
[22:30:45] debugging I tend to use copilot and I
[22:30:48] don't find myself going to shy GBT to
[22:30:50] ask about code or syntax. We can do it
[22:30:53] directly in the copilot. And one task
[22:30:55] that is very famous in any software
[22:30:57] developments we have the refactoring. So
[22:30:59] if you have like a code that is slow and
[22:31:01] bad designs and you want to refactor the
[22:31:03] whole codes, you can do it directly in
[22:31:05] your code together with the copilot in
[22:31:06] order to find optimizations. And I use
[22:31:09] as well copilot in order to add inline
[22:31:11] comments. So I don't find myself going
[22:31:13] to ship and asking to add comments to my
[22:31:15] codes. You can do it directly in your
[22:31:18] code using cilot. And of course if
[22:31:20] everything is working perfectly, I have
[22:31:22] the best practices, the good
[22:31:23] performance, I have the comments, it's
[22:31:24] still you have to maintain nice style
[22:31:26] and format of your code. And of course
[22:31:28] now we can do that directly using the
[22:31:30] copilot. We don't have to go and jump to
[22:31:31] shajbt in order to style and format your
[22:31:33] code. And as you can see I'm currently
[22:31:35] using both of them for different types
[22:31:37] of tasks. So again if I have the feeling
[22:31:39] that I have to discuss something I go to
[22:31:41] shbt. But once the idea is very clear
[22:31:44] and I know the solution then I start
[22:31:47] using copilot in order to write the code
[22:31:49] and with the help of the copilot I can
[22:31:52] deliver clean and professional code. So
[22:31:54] this is how I currently use both
[22:31:56] Shajbuty and
[22:32:01] Copilot. Okay friends, so now what we're
[22:32:03] going to do, I'm going to show you a
[22:32:04] quick guide about the GitHub copilot in
[22:32:07] the Visual Studio Code. Once you create
[22:32:09] a profile and connect it to your Visual
[22:32:11] Studio, you will get a new icon for the
[22:32:13] copilot. So once you go there, you can
[22:32:15] see quickly the status and as well you
[22:32:18] can go and disable the copilot. So if
[22:32:20] you have it like this, that's means your
[22:32:22] co-pilot is active. So now once you have
[22:32:24] everything up and running, what you have
[22:32:26] to do is very simple. Just go and start
[22:32:28] writing your code. So start typing any
[22:32:31] select statements. And now you can see
[22:32:33] that we have a gray text. This gray text
[22:32:36] called the ghost text. It is an auto
[22:32:38] completion from the copilot. And now it
[22:32:40] says select star from table. And now as
[22:32:43] you can see as I mouse hover on it, we
[22:32:46] can see that I can go and switch between
[22:32:48] different suggestions. So here we have
[22:32:50] like three suggestions. One, two, three.
[22:32:52] And I'm going to go with the third one.
[22:32:54] So now here as it says if you want to
[22:32:56] accept the suggestion all what you have
[22:32:58] to do is to press tab. So let's go and
[22:33:00] do it. So you are accepting the whole
[22:33:02] thing. But now if you say you know what
[22:33:04] I'm going to accept only part of the
[22:33:06] code. So let's go again and write
[22:33:08] select. So this time we're going to be
[22:33:10] selective. In order to do that hold
[22:33:12] control and then with the right arrow
[22:33:14] and with that we are accepting part of
[22:33:17] the ghost not everything. But of course
[22:33:19] if you are accepting the whole thing
[22:33:20] just go with the tab. And now there is
[22:33:22] another way in order to trigger the
[22:33:24] ghost text and that's by defining first
[22:33:27] a comments. For example we want to
[22:33:28] select the top three customers based on
[22:33:30] the score. So now once you start writing
[22:33:32] the query the co-pilot going to go and
[22:33:34] write a query that is relevant for the
[22:33:37] comments. So now as you can see we are
[22:33:38] getting top three from customers because
[22:33:41] we want the top three customers and here
[22:33:42] we have like two suggestions like over
[22:33:45] here we have the order buy or without
[22:33:46] it. So I will go with order by and hit a
[22:33:48] tap. And now here another suggestion
[22:33:50] which is correct. In order to solve the
[22:33:51] data from the highest to the lowest. All
[22:33:53] right moving on to the next one. As we
[22:33:55] learned in SQL in order to solve a task
[22:33:58] there could be like multiple solutions
[22:34:00] and multiple variants of queries that
[22:34:02] solving the same task. So let's say that
[22:34:04] we have this task rank customers based
[22:34:06] on their total order sales. So what you
[22:34:08] can do if you start writing the query we
[22:34:10] are getting now the ghost text. But now
[22:34:12] what we can do we can go and hit ct
[22:34:13] controll enter. So now what happens on
[22:34:15] the right side you will get different
[22:34:17] suggestions and here we have like nine
[22:34:19] suggestions on how to solve this task in
[22:34:21] scale. So now what you have to do is to
[22:34:23] go through all those suggestions and
[22:34:25] pick one. For example I can go with the
[22:34:27] suggestion number three and say accept
[22:34:29] suggestion and you will get it in your
[22:34:31] code editor. So this is what we mean
[22:34:32] with the copilot autocomp completion and
[22:34:35] integrating the AI directly as you are
[22:34:38] developing and writing a code. Now in
[22:34:39] the co-pilot, not only using the ghost
[22:34:41] text and the autoco compilation, we can
[22:34:43] go and interact with the AI using inline
[22:34:46] shots. So it's something like shimity.
[22:34:48] Now in order to trigger the shot, what
[22:34:50] you're going to do, you're going to go
[22:34:51] and hit control I and then you're going
[22:34:53] to get a place in order to ask the
[22:34:55] copilot any question like for example
[22:34:57] join the query with the
[22:35:00] table sales orders. So let's go and hit
[22:35:04] it.
[22:35:06] And now as you can see we got a full
[22:35:08] query where the customers is joined with
[22:35:10] the orders and it is totally correct how
[22:35:13] the table are joins. So that means
[22:35:14] copilot knows already all the tables
[22:35:17] that I have in the database and as well
[22:35:20] the columns and how to join them. This
[22:35:22] is amazing. So if you like it you go and
[22:35:24] accept it of course and this is way
[22:35:26] faster than having shajibbd because in
[22:35:28] shajibity you have to introduce your
[22:35:30] database your columns and stuff before
[22:35:32] even asking anything. This is exactly
[22:35:35] the power of copilot. Now what else we
[22:35:38] can do with that? We can go and
[22:35:39] highlight part of our codes and then
[22:35:41] start again the shots and here we can
[22:35:43] say replace this column with an
[22:35:46] aggregation of the sales. So let's go
[22:35:50] and hit okay. Now as you can see it
[22:35:52] replaced it with an aggregate function.
[22:35:53] And one thing that is very important the
[22:35:55] code is not changed yet. So it is
[22:35:58] highlighted and showing you a suggestion
[22:36:00] and now you have to accept it or discard
[22:36:02] it. If you discard, nothing going to
[22:36:04] change in your codes. But once you say
[22:36:06] accept, it's going to go and replace
[22:36:07] your original codes. So if you go and do
[22:36:10] that, now your code is replaced with the
[22:36:12] AI suggestion. Okay. Another thing about
[22:36:14] the copilot, it's try to fix issues that
[22:36:17] you have in your codes. So for example,
[22:36:19] we have here an error. If you go and
[22:36:20] mouse hover it, you can see a menu from
[22:36:22] the copilot in order to view the error
[22:36:24] or to fix it. And another way to do
[22:36:26] that, if you right click on it, you go
[22:36:28] to the copilot. And here you can see we
[22:36:30] can explain or fix. So if you go and
[22:36:33] explain, you will get another window
[22:36:34] where you get an explanation about the
[22:36:36] issue in your code. And once you
[22:36:38] understand it, you can go and ask the
[22:36:39] copilot in order to fix it. So let's go
[22:36:42] over here and go to
[22:36:44] fix. And with that, the copilot did fix
[22:36:46] the issue. It was all about the order of
[22:36:49] the select statements. So first you have
[22:36:50] to do the group by then order by. So it
[22:36:52] helps you to find issues and to fix it
[22:36:54] as well. And now, as you might already
[22:36:56] noticed, as we are writing the code and
[22:36:58] interacting with the Visual Studio, you
[22:37:00] will often get a sparkle, this little
[22:37:03] yellow sparkle on the left side. So, you
[22:37:05] will see this icon each time the copilot
[22:37:07] thinks it can help. So, if you go and
[22:37:09] click on it, you will get a menu of
[22:37:11] different stuff that the copilot can do
[22:37:13] for you, like fixing, explaining,
[22:37:15] modifying, and so on. Well, my friends,
[22:37:17] that's it. This is the copilot, and it
[22:37:19] is very simple, but yet very powerful
[22:37:22] for developers. And of course, not only
[22:37:24] for SQL, for anything like for Python
[22:37:26] and so on. Everything is integrated in
[22:37:28] one place. I don't have to jump to
[22:37:29] Shajibbet and ask stuff. It is live and
[22:37:32] I can do it directly as I'm writing my
[22:37:34] code. So that's all for Copilot. All
[22:37:36] right friends. So now let's switch to
[22:37:38] Shajibet. So let's start first by
[22:37:40] understanding the structure and the
[22:37:42] basic components of Shajbet prompts.
[22:37:49] So the first component and the most
[22:37:50] important one we have the tasks. You
[22:37:53] have to be very clear by defining what
[22:37:55] the AI should do and without having a
[22:37:57] clear tasks the AI will not understand
[22:38:00] what to do. So this is mandatory in each
[22:38:03] prompt and then after that you have to
[22:38:05] provide some context. So you give some
[22:38:07] background informations like for example
[22:38:09] you say I am students or I am a data
[22:38:11] engineer and so on. And another
[22:38:13] components we have to add
[22:38:15] specifications. So in the task you give
[22:38:17] the main task what the AI should do but
[22:38:20] with the specifications you go in
[22:38:22] details like for example which topic
[22:38:24] should be added or maybe excluded the
[22:38:26] number of word counts. So here you are
[22:38:29] specifying a lot of wishes and small
[22:38:31] details and specifications in order to
[22:38:33] get an answer that meet your
[22:38:35] expectations. So both of the context and
[22:38:37] specifications they are important. And
[22:38:39] then after that we have some nice to
[22:38:41] have components like for example
[22:38:43] specifying a rule. So here you give the
[22:38:45] AI a role like for example you tell it
[22:38:48] to act as an expert as a teacher
[22:38:51] interviewer. So you are setting the AI
[22:38:53] to play a role and the last component
[22:38:56] that you can add as as well the tone.
[22:38:58] Here you are defining like the voice of
[22:39:00] the answer in order just to make the
[22:39:02] answer like more friendly and easy to
[22:39:04] read and engaging. So the role and the
[22:39:06] tone they are nice to have and if you go
[22:39:08] and use all those components you will
[22:39:10] get a better results from the AI. So
[22:39:13] let's take for example the following
[22:39:15] prompts explain SQL window functions. So
[22:39:18] this is very simple and very short and
[22:39:20] here we have only one component the
[22:39:22] task. So here you are not giving any
[22:39:24] context whether it is for data analytics
[22:39:27] or for data engineering. So you leave it
[22:39:29] up to the AI and maybe the answer that
[22:39:31] you will get will not meet the
[22:39:33] expectation that you have. And now if
[22:39:35] you want to shape it in the way that you
[22:39:37] want you have to add more components
[22:39:39] like for example this prompt you are
[22:39:40] saying you are a senior SQL expert. So
[22:39:43] here we are defining the rule for the
[22:39:46] AI. So the AI should act now as an SQL
[22:39:49] expert. And then the next section we are
[22:39:51] adding a context to the prompts. So we
[22:39:53] are saying I'm data analyst working on
[22:39:55] SQL projects using SQL server. So now
[22:39:58] the answer that you will get from the AI
[22:40:00] going to use the syntax of the SQL
[22:40:02] server and focus on the topic of
[22:40:03] analytics. That's why the context is
[22:40:05] very important and then we go specify in
[22:40:08] the prompt the task the main task. So we
[22:40:10] say explain the concept of SQL window
[22:40:12] functions and do the following. And now
[22:40:14] we go and give more fine details about
[22:40:17] what the AI should provide. We are
[22:40:19] saying explain each window function and
[22:40:22] show the syntax. describe why they are
[22:40:24] important and when to use them and list
[22:40:26] the top three use cases. So you are now
[22:40:29] specifying what you are expecting from
[22:40:31] the AI and after that of course it is
[22:40:33] nice to have we specify the tone of the
[22:40:36] explanation. So we say the tone should
[22:40:38] be conversational and direct as if you
[22:40:41] are speaking to me onetoone so that it
[22:40:43] is not like you are reading a document
[22:40:45] you are reading something that is
[22:40:47] engaging. So I know this prompt is
[22:40:49] really big but still you will get way
[22:40:51] better results than only saying explain
[22:40:54] the concepts. So those are the main
[22:40:56] components that I usually use if I'm
[22:40:58] starting like a conversation and a
[22:41:00] discussion with the
[22:41:05] shajuti. Okay. Next I'm going to show
[22:41:07] you the frequently used prompts that I
[22:41:09] use in my projects. Now little bit
[22:41:11] awareness about using shajib in
[22:41:13] companies. If you are working in new
[22:41:15] company, make sure to ask about the
[22:41:17] rules of using Shia Gibbt because some
[22:41:19] companies offer their own chatbots for
[22:41:22] few security reasons. So make sure
[22:41:24] always to check with the rules before
[22:41:26] jumping immediately to sht. All right.
[22:41:28] So let's start with the first prompts.
[22:41:30] We can use shad in order to solve an SQL
[22:41:33] task that you have in the project. So
[22:41:35] let's see this prompts. It start first
[22:41:37] with the context. So I'm telling that I
[22:41:40] have an SQL server database and we have
[22:41:42] like two tables. So now I have to
[22:41:44] explain for shad the database that I
[22:41:46] have. So I'm saying we have a table
[22:41:48] called orders and we have the following
[22:41:50] columns and we have another table called
[22:41:52] customers and here are the columns for
[22:41:53] the customers. So that I gave shy a
[22:41:56] context about the tables that I have in
[22:41:58] my database and as well I was precise
[22:42:01] about the database. It is SQL server.
[22:42:03] Now after we have the context the next
[22:42:05] step is that I'm going to tell SQL what
[22:42:07] to do. So I'm telling the AI do the
[22:42:09] following. write a query to rank
[22:42:11] customers based on their sales and then
[22:42:13] I'm detailing what I'm expecting to have
[22:42:15] at the output. So the result should
[22:42:17] include customer ID, full name, country,
[22:42:19] total sales and so on. And here I'm
[22:42:21] adding like more tasks. It's not enough
[22:42:23] to have a query. I would like as well to
[22:42:24] have a comments. So I'm saying include
[22:42:27] comments but avoid commenting on obvious
[22:42:29] parts because if you tell just include
[22:42:32] comments, you will get a lot of
[22:42:34] unnecessary comments. Now of course in
[22:42:36] square there is like not one solution
[22:42:37] for a task. there is always like
[22:42:40] different variants on how to achieve the
[22:42:42] same task. So usually I would like to
[22:42:44] understand what are my options. That's
[22:42:45] why I'm telling Shaji write three
[22:42:48] different versions of the query to
[22:42:50] achieve this task and then I would like
[22:42:51] to evaluate each of those versions and
[22:42:54] that's why I'm giving the task for the
[22:42:55] AI to evaluate those versions and to
[22:42:58] focus on two things. It is easy to read
[22:43:00] and as well has good performance. Okay.
[22:43:03] So let's see what shajivity going to
[22:43:04] give us the results. So we can see the
[22:43:06] first solution over here where shadivity
[22:43:08] is using the CTE. So we can see in the
[22:43:11] CT over here that the table first are
[22:43:13] joined and then we have like a group by
[22:43:15] in order to aggregate the sales. In the
[22:43:18] step two we can see over here we have
[22:43:20] the rank window function in order to
[22:43:22] rank the sales. So of course you can do
[22:43:24] that. Let's check the version number two
[22:43:27] over here. So they I used the subquery
[22:43:30] and it is as well a nice solution where
[22:43:33] the shad first prepared the data. So
[22:43:36] first done the aggregation before
[22:43:38] joining the data. Let's get the last
[22:43:40] solution over here. So we have here
[22:43:42] single query using window function which
[22:43:45] is as you can see it is the smallest
[22:43:47] one. We don't have CTE we don't have any
[22:43:49] sub queries. So first it is joining the
[22:43:52] tables and doing together the group by
[22:43:54] together with the window function and
[22:43:56] after that we get an evaluation from the
[22:43:59] AI where where as you can see it focus
[22:44:01] on two things the readability and the
[22:44:03] performance. So it is saying with the
[22:44:05] CTE the readability is really high
[22:44:08] compared to the sub query and to the
[22:44:10] last version where you have the group by
[22:44:12] together with the window function. So I
[22:44:13] totally agree with the shajibbity the
[22:44:15] first version was the best one for the
[22:44:17] readability. Now checking the
[22:44:18] performance. You can see the performance
[22:44:20] is moderate. The second one, the
[22:44:22] subquery is good. And the last one is
[22:44:25] the best for the performance. But of
[22:44:27] course, always test with the execution
[22:44:28] plan. So as you can see, there is like a
[22:44:30] trade-off between the readability and
[22:44:32] the performance. If the priority is
[22:44:34] readability, then go with the version
[22:44:35] one. But if the priority is the
[22:44:37] performance, then go with the version
[22:44:39] three. As you can see, we got three
[22:44:41] solutions for our one task. And you can
[22:44:43] now evaluate which one you want to use.
[22:44:46] And this is really amazing, right? All
[22:44:48] right, moving on to the next one that I
[22:44:49] frequently use. We have
[22:44:52] impromptability. As you are creating an
[22:44:54] SQL query for a complex task, you might
[22:44:57] end up writing a lot of CTE, sub
[22:44:59] queries. You might end up having a lot
[22:45:01] of joins, sub queries, CTE, hundreds of
[22:45:05] lines, and you might lose the big
[22:45:07] picture. So what I always do, I give the
[22:45:09] query to the SHBT and ask it to optimize
[22:45:12] it in order to be more readable and to
[22:45:14] find any redundancy in my query in order
[22:45:17] to consolidate it. So now let's check
[22:45:19] the prompt. It says the following SQL
[22:45:21] server query is long and hard to
[22:45:23] understand. And then we're going to give
[22:45:24] the AI tasks. So the first task is to
[22:45:27] improve its readability and the next one
[22:45:29] is to detect any redundancy in the code
[22:45:32] in order to remove it and to consolidate
[22:45:34] the query. So to make our query compact
[22:45:36] and small and of course to include some
[22:45:38] comments and not to comment the obvious
[22:45:40] parts and now always if there is like
[22:45:42] some optimizations there should be a
[22:45:44] learning process. So I'm asking now the
[22:45:46] AI to explain each improvement to
[22:45:49] understand the reasons behind it so that
[22:45:51] next time I'm writing the queries I can
[22:45:53] avoid those mistakes and of course you
[22:45:55] have to go and give the query to the AI.
[22:45:57] All right. So now let's check the answer
[22:45:59] from the ship for my prompt. So as you
[22:46:02] can see we have a really long query and
[22:46:04] here we have now from the result the
[22:46:06] improved query. So we can see that we
[22:46:09] have only one city. Well that is crazy.
[22:46:12] We had before like five six cities and
[22:46:14] we can see here that the team managed to
[22:46:17] put everything in one city and then do
[22:46:20] all the aggregations and the window
[22:46:21] function and then we have here the final
[22:46:24] select. Well this is huge improvement to
[22:46:26] the previous query. Let's check here the
[22:46:28] explanation. So it says it consolidated
[22:46:30] the cities so combined all the cities
[22:46:33] into one and many other stuff like there
[22:46:36] were a lot of unnecessary joins and so
[22:46:38] on. And here a small improvement where
[22:46:40] it uses the concat instead of the plus
[22:46:43] because concat is standards for multiple
[22:46:46] databases. And here we have a final
[22:46:48] benefits. So we have shorter query
[22:46:50] instead of five CDs we have only one and
[22:46:53] combining the logic you can reduce the
[22:46:55] number of scans of the tables which is
[22:46:57] correct. So as you can see it is the
[22:46:59] magic of the AI. It found the issues in
[22:47:01] my code, improved the readability and
[22:47:03] reduced all the redundancy and
[22:47:05] unnecessary joints and so on in the SQL
[22:47:08] script. Okay, moving on to the next
[22:47:10] prompt. It is about optimizing the
[22:47:12] performance of my query. And if you are
[22:47:14] working in big projects where you have
[22:47:16] like millions of data in your tables, it
[22:47:18] can be an issue if you are writing
[22:47:19] queries that are not following the best
[22:47:21] practices for performance. So that's why
[22:47:24] I go and double check with the AI
[22:47:26] whether my script is following the best
[22:47:28] practices for the performance. So as
[22:47:30] usual in the prompt we have to go and
[22:47:31] give the context. So the following SQL
[22:47:34] server query is slow and then we start
[22:47:36] giving the AI some tasks. So propose
[22:47:38] optimizations to improve its performance
[22:47:40] and provide me then the improved SQL
[22:47:43] query and I would like always to
[22:47:45] understand the reason why it's better to
[22:47:47] write it in another way so that by the
[22:47:49] next time I improve while I'm writing
[22:47:51] the query. So explain each improvement
[22:47:54] to understand the reasoning behind it
[22:47:56] and then at the end we go and give our
[22:47:58] query. Okay. So now let's write the
[22:48:00] prompts on the following query over
[22:48:02] here. So on this query we have a lot of
[22:48:04] bad practices like for example doing
[22:48:06] aggregations using correlated subquery.
[22:48:09] We are using a lot of functions inside
[22:48:11] the work clause which is not really good
[22:48:13] for indexing and we are using a lot of
[22:48:15] or operators and here we have again a
[22:48:18] subquery. So let's check whether shad
[22:48:20] going to find all those bad practices.
[22:48:23] So let's check the results from the
[22:48:24] shad. And as you can see now we have an
[22:48:26] optimized query. It is little bit longer
[22:48:29] but I think we have here better
[22:48:30] practices. So we have here a lot of
[22:48:32] changes. Let's check what did. So first
[22:48:35] it replaced the lower in the query. It
[22:48:37] says it's not really good to use
[22:48:39] functions in the works so that the index
[22:48:43] can work. So it replaced the lower with
[22:48:46] the order status without the function.
[22:48:48] the next one. So it is avoiding the
[22:48:50] correlated subquery. So instead of that
[22:48:53] it is using a lift join. So it is
[22:48:55] joining the table normally without doing
[22:48:57] any correlated queries and as well it is
[22:49:00] avoiding the function year in the works
[22:49:02] and instead of that it is using the
[22:49:04] range using between and the next one it
[22:49:07] is using exist better than in which is
[22:49:09] better for the performance of course. So
[22:49:11] as you can see you can use the AI in
[22:49:13] order to optimize the performance of
[22:49:14] your query and to convert it to a script
[22:49:17] that is following the best practices. Of
[22:49:20] course my recommendations always don't
[22:49:22] go blindly with all changes that is
[22:49:24] suggested from the shajibity. Always
[22:49:26] take each recommendation one by one.
[22:49:28] Test it and evaluate it using your
[22:49:30] knowledge. Okay to the next one. It is
[22:49:32] interesting one. We can use
[22:49:35] [Music]
[22:49:37] impromptution plan. So now the execution
[22:49:39] plans usually are advanced. So you need
[22:49:42] a lot of knowhow and experience in order
[22:49:44] to understand and read the execution
[22:49:46] plan and if you have a big query it's
[22:49:48] going to be really nightmare in order to
[22:49:50] understand the flow and where is exactly
[22:49:52] the issue. But now we are not alone. We
[22:49:54] have assistant the AI in order to help
[22:49:57] us understanding this complex stuff. So
[22:49:59] what we can do we can take a screenshot
[22:50:01] of the execution plan and upload it to
[22:50:03] Shajib and we say the image is execution
[22:50:06] plan of SQL server query and now we give
[22:50:09] the following task to say describe the
[22:50:11] execution plan step by step after that
[22:50:13] I'm going to tell SQL to identify the
[22:50:15] performance bottlenecks and where is
[22:50:17] exactly the issue what makes my query
[22:50:20] slow this is of course the hardest part
[22:50:22] of reading an execution plan and once it
[22:50:25] identify the performance issues I'm
[22:50:27] going to ask it to suggest ways to
[22:50:28] improve improve the performance and
[22:50:30] optimize the execution plan. So first
[22:50:33] understand the execution plan identify
[22:50:35] the issues and how to optimize it. Okay.
[22:50:37] So now after uploading the photo and
[22:50:39] asking the AI we have the following
[22:50:41] results. So now we can see a detailed
[22:50:44] explanation about the execution plan and
[22:50:46] there is like a lot of details. I will
[22:50:48] not go through everything. So we start
[22:50:50] with the table scans then the cluster
[22:50:52] scan and the nested loops. So we have
[22:50:54] several nested loops and then the
[22:50:56] aggregation and the final step. So that
[22:50:58] now we have like a nice explanation what
[22:51:00] is SQL is doing behind the scenes for my
[22:51:03] query and you don't have to be an expert
[22:51:05] understanding the execution plan. You
[22:51:07] can ask the AI about it. Now what is
[22:51:09] very important is to understand where
[22:51:11] are the bottlenecks what are the
[22:51:13] problems. So let's see what's we have
[22:51:15] here. So let's say the first one we have
[22:51:17] a table scan which is really bad. That
[22:51:19] means this table the orders archive does
[22:51:22] not has any index. So it says the table
[22:51:24] scan indicates a lake of useful index on
[22:51:26] the table which forces the engine to
[22:51:28] scan the whole table or rows. And now
[22:51:31] what is very important is the nested
[22:51:33] loops in the joins. This is really bad
[22:51:36] if you have big tables. So here it's
[22:51:38] saying it's fine if you have like small
[22:51:40] data sets but it going to be really
[22:51:42] problematic if you have many rows. So as
[22:51:45] you can see we are getting more
[22:51:46] knowledge about the issues that we have
[22:51:48] from our execution plan. And the last
[22:51:50] step it is the suggestions. So the first
[22:51:53] one and the most obvious one is to add
[22:51:55] an index to the orders archive. The
[22:51:57] nonclustered index. Well, if there's no
[22:51:59] index at all, I would go first with a
[22:52:01] clustered index, not immediately with a
[22:52:03] nonclustered index. And then some other
[22:52:05] best practices, but I think this one is
[22:52:08] very relevant is to change the join
[22:52:10] type. So you can use the hints in order
[22:52:12] to use a merge join or a hash join. So
[22:52:15] now we understand how it works, where
[22:52:17] are the issues and what the suggestions
[22:52:19] to fix it. All right, the next prompt is
[22:52:21] about debugging. As you are writing a
[22:52:23] complex SQL query, you might get from
[22:52:25] the database an error when you execute
[22:52:27] it and sometimes it is challenging to
[22:52:30] find the root cause of the issue. So we
[22:52:32] have the following prompts. First the
[22:52:33] context is going to say the following
[22:52:35] SQL server query causing this error.
[22:52:38] Then we can paste the error message that
[22:52:40] we are getting and then we ask the AI to
[22:52:42] do the following stuff. First explain
[22:52:44] the error message. So I would like to
[22:52:45] have better understanding of the error.
[22:52:47] And then we ask the AI to find the root
[22:52:50] cause of the issue from my scripts. And
[22:52:52] after finding the problem and the issue,
[22:52:54] we're going to ask the AI to suggest how
[22:52:56] to fix it. And of course, we have to
[22:52:58] give in the prompt as well our SQL
[22:53:00] query. All right. So now I have the
[22:53:02] following query and if I execute it, I'm
[22:53:04] getting the following error. It says the
[22:53:06] column sales.order dot sales in invalid
[22:53:10] in the select list because it is not
[22:53:12] contained in the aggregations and so on.
[22:53:14] So I'm not really understanding what's
[22:53:15] going on. Let's ask the AI about it. So
[22:53:18] let's check what shity did answer. When
[22:53:20] you are using group by every column in
[22:53:22] the select must be used in the group by
[22:53:25] as well. And it says in your query you
[22:53:27] are selecting few columns which is this
[22:53:29] one is valid. The other two as well
[22:53:31] valid but we have one inside the rank
[22:53:34] function. It is invalid. Okay. So now we
[22:53:37] can see here more details about the root
[22:53:39] cause. It is saying when you are using
[22:53:41] window function like the rank it doesn't
[22:53:43] directly work with the aggregate
[22:53:45] functions. So here it's indicate clearly
[22:53:47] that the sales inside the rank function
[22:53:50] is the issue. So let's see the fix over
[22:53:53] here. So since we don't have here sales
[22:53:55] at all you cannot have here sales in the
[22:53:57] partition. That's why the fix here is to
[22:54:00] use the sum of sales because we have it
[22:54:02] in the select. And here you have as well
[22:54:04] a nice explanation about the fix. So you
[22:54:07] can see here we have an explanation
[22:54:09] about the error message the road cause
[22:54:11] it's pointing exactly where there's the
[22:54:13] issue suggesting a fix and explaining
[22:54:16] the fix and this is exactly the steps
[22:54:18] that you have to do if you are debugging
[22:54:20] a code all right moving on to the next
[22:54:22] prompt we can use AI to explain the
[22:54:24] result that I'm getting from SQL well
[22:54:26] sometimes you might have an SQL query
[22:54:29] that you have in the project and you are
[22:54:30] not understanding why you are getting
[22:54:32] specific results so as usual we start
[22:54:34] with the context we tell the AI I didn't
[22:54:37] understand the result of the following
[22:54:38] SQL server query and then we ask the AI
[22:54:41] to do the following. First break down
[22:54:43] how SQL processes the query step by step
[22:54:47] and as well I would like to get an
[22:54:48] explanation for each stage and how the
[22:54:51] result is formed. So as you can see here
[22:54:53] I don't need any optimizations. I don't
[22:54:55] need in the output any query. I just
[22:54:58] need an explanation and then at the end
[22:55:00] you're going to go and paste your query.
[22:55:02] Okay. So now we have the following
[22:55:03] query. We have a recursive CTE where we
[22:55:05] are generating like numbers between 1
[22:55:08] and 20. Can tell you recursive CTE are
[22:55:10] usually like complicated to understand.
[22:55:13] So now maybe we are having hard time
[22:55:14] understanding the result of this query.
[22:55:16] After asking the AI about it, we got the
[22:55:19] explanation first about the query
[22:55:21] structure. So it says you are using the
[22:55:23] CTE with the main query. Well, okay. But
[22:55:25] what is very interesting is to
[22:55:27] understand step by step how SQL executed
[22:55:30] this query. So it tells the step one
[22:55:32] it's going to go and execute the anchor
[22:55:33] query and that's why we will get first
[22:55:35] the one and then the next step the
[22:55:37] recursive query going to be executed for
[22:55:39] the first time. So it is saying okay we
[22:55:42] are adding one to the current value. So
[22:55:44] as you can see 1 + 1 we will get two and
[22:55:47] then in the iteration two we will get 2
[22:55:49] + 1 3 and it will keep repeating this
[22:55:53] process until we get all the result from
[22:55:55] 1 to 20. And then as well we have here
[22:55:57] an explanation about the termination of
[22:55:59] the recursive query. So it's saying the
[22:56:01] filter is the way out of the loop. So
[22:56:04] once we reach the 20 it will stop. And
[22:56:07] then a few informations about the main
[22:56:09] query and with that you will get a deep
[22:56:11] knowledge about how works and why you
[22:56:14] are seeing those results. This is really
[22:56:16] amazing use case for the GBT. All right
[22:56:19] friends. So now we're going to talk
[22:56:20] about my favorite prompts. So we can use
[22:56:23] the AI to style and format my code. So
[22:56:26] now once you are done writing a complex
[22:56:28] query to solve a task and everything is
[22:56:30] correct and optimized as well for the
[22:56:33] performance. Now it's time to go and
[22:56:35] review your code in order to style and
[22:56:37] format your script. So we have the
[22:56:39] following prompt. It says the following
[22:56:41] SQL server query is hard to understand.
[22:56:44] So now we ask the AI to do the
[22:56:45] following. Restyle the code to make it
[22:56:47] easier to read. And the next task for AI
[22:56:50] is to align all the columns aliases.
[22:56:52] Sometimes if you are using any tool to
[22:56:54] style and format your code, you will
[22:56:56] find that it is bringing a lot of new
[22:56:58] lines. So I tell he AI, keep it compact,
[22:57:01] do not introduce unnecessary new lines.
[22:57:04] And the last task for the AI is to make
[22:57:06] sure it is following the best practices.
[22:57:09] And of course, what do we need at the
[22:57:10] end? Our query. Okay, so now we have the
[22:57:12] following query. And as you can see, we
[22:57:15] have very annoying query where it is
[22:57:17] really hard to read and that's because
[22:57:19] the format and the styling of the query
[22:57:21] is really bad. I don't want to speak
[22:57:23] about the alignment and so on. But as
[22:57:25] you can see, we have here lower cases,
[22:57:27] we have here uppercase sometimes for the
[22:57:29] keywords. And of course, if you are
[22:57:31] developing and writing codes and you are
[22:57:32] delivering something like this, it is
[22:57:34] really not nice. So let's see how shipy
[22:57:36] can fix it. Okay. So now after executing
[22:57:38] the prompts, as you can see, now my
[22:57:41] query looks way nicer. So first of all
[22:57:43] all the keywords are uppercase and then
[22:57:46] you can see our CTE are really nice to
[22:57:48] read. We have here enough spacing. The
[22:57:50] alignment of everything looks really
[22:57:52] nice and the case is very clear and the
[22:57:55] main query over here is as well easy to
[22:57:59] read. So they done wonderful job styling
[22:58:01] and formatting my code and here you have
[22:58:03] like explanation what did change. So
[22:58:06] first it is saying okay all the keywords
[22:58:08] are capitalized the alignment of the
[22:58:10] aliases and the columns and so on. So
[22:58:12] with that we got a really nice style
[22:58:14] formatted query that we can share with
[22:58:16] others. Okay, moving on to the next one.
[22:58:19] We can use AI in order to generate
[22:58:21] documentations and as well to add
[22:58:23] comments to my code. Creating
[22:58:25] documentations and adding comments to
[22:58:27] code is usually something very annoying
[22:58:29] for the developers. And sadly I see a
[22:58:32] lot of developers that they tend to not
[22:58:34] add any comments or anything to their
[22:58:36] code. And of course, this is really bad
[22:58:38] because you are not thinking about other
[22:58:40] developers that are reading your code.
[22:58:43] No
[22:58:45] god, no god, please no.
[22:58:50] And since this process is annoying and
[22:58:53] takes time, we can use the help of AI to
[22:58:56] improve the speed of creating those
[22:58:57] stuff. So let's check the following
[22:58:59] prompt. It says the following SQL server
[22:59:01] query lakes comments and documentation.
[22:59:04] So we are saying first insert a leading
[22:59:07] comment at the start of the query
[22:59:08] describing its overall purpose. So this
[22:59:12] is what we usually do. We add at the
[22:59:14] start a short description about the
[22:59:16] following code and then it should go and
[22:59:18] add comments only where clarifications
[22:59:21] is necessary and very important it
[22:59:23] should avoid obvious statements. So it's
[22:59:25] like indexing don't over commenting your
[22:59:28] code and usually if you are creating
[22:59:30] query for data analytics it's really
[22:59:32] good to explain the business rules and
[22:59:34] transformations that you are doing
[22:59:36] inside your query and maybe another
[22:59:38] documentations describing how the query
[22:59:41] works. So for now we are asking to add
[22:59:43] comments and documentations and of
[22:59:44] course you have to go and add your
[22:59:46] query. Okay. So now I just used this
[22:59:48] prompt to one of my queries. Let's go
[22:59:50] and check the results. Now the first
[22:59:52] comment is the most important one
[22:59:54] because it gives the overall purpose of
[22:59:56] the whole query. So let's see what it's
[22:59:58] saying. It's saying this query identify
[23:00:00] customers based on their total salaries
[23:00:02] and provide list of customers with their
[23:00:05] total sales and their assigned segments.
[23:00:08] So we have here like customer
[23:00:09] segmentations. We have high value,
[23:00:11] medium value and low value. So with this
[23:00:13] comment we have the overall purpose of
[23:00:15] the query and then we have the inline
[23:00:18] comments like here. So it says it's
[23:00:20] calculate the total sales for each
[23:00:21] customer for the first CTE and now for
[23:00:23] the second CTE we have here a full
[23:00:25] description how the segment is built and
[23:00:28] this is built of course from the
[23:00:30] business rule of the customer segments.
[23:00:32] So it say the high values for total
[23:00:34] sales above like 100 and between and so
[23:00:37] on. Well this case win is really easy.
[23:00:40] So actually you can read it from the
[23:00:42] case win. But if you have like complex
[23:00:44] queries, it's really nice to have the
[23:00:45] full text of the case win and then add
[23:00:48] the main query. You can see here the
[23:00:50] final output and the inline comments. So
[23:00:53] as you can see it's really nice comments
[23:00:55] inside our codes. And now the next one
[23:00:57] we have like a document about the
[23:00:59] business rule. And I totally agree with
[23:01:01] the AI that the business rule is here
[23:01:03] about the customer segmentations. So we
[23:01:06] have here again very nice like short
[23:01:08] documentations about the business rules
[23:01:10] that we have and then we have another
[23:01:13] document about how the query is working.
[23:01:15] Well I think this is too much for small
[23:01:18] query. We can go and ask the shibility
[23:01:20] to make the documentation like shorter.
[23:01:23] So as you can see we have a full
[23:01:24] documentation about our query about our
[23:01:27] business rules and we have really nice
[23:01:29] comments in our code. All right. Now
[23:01:31] moving on to the next prompts. It is
[23:01:33] very important to improve the whole
[23:01:35] project, the whole database. So what
[23:01:37] we're going to do, we're going to go and
[23:01:38] take our DDL scripts and give it to the
[23:01:41] AI and start asking AI to optimize our
[23:01:44] database DDL. So here there is a lot of
[23:01:47] things that you can optimize with the
[23:01:48] database. So let's check this prompts.
[23:01:50] It's going to say the following SQL
[23:01:52] server DDL script has to be optimized
[23:01:54] and we ask the following task from the
[23:01:56] AI. The first one is to check the
[23:01:58] naming. So if you have a database where
[23:02:00] you have a lot of tables and columns and
[23:02:02] so on, you should be always working with
[23:02:04] a specific naming convention. So here
[23:02:07] just to make sure that the naming that
[23:02:09] you are using is correct. Then what is
[23:02:11] very important in DDLs is the data type.
[23:02:14] Data types plays very crucial role in
[23:02:17] optimizing your queries. So we are
[23:02:19] telling the AI to check the data types
[23:02:21] and whether they are optimized as well.
[23:02:23] And now the next point is about the data
[23:02:26] integrity. So if you are building a
[23:02:27] relational database, you will have a lot
[23:02:29] of primary keys and foreign keys and you
[23:02:31] can tell the AI to check the integrity
[23:02:33] of all those keys. The next point is
[23:02:35] about indexes. Here you can tell the AI
[23:02:38] to check the overall indexing that you
[23:02:40] are using in the DDL scripts just to
[23:02:42] make sure that you are not missing
[23:02:43] anything and as well to check whether we
[23:02:46] have duplicates. So it is really great
[23:02:48] check and the last check is that to
[23:02:50] check the normalizations of the table to
[23:02:52] check the data model and whether there
[23:02:54] is like any suggestions about splitting
[23:02:56] tables and normalizing tables or they
[23:02:59] are like some weird redundancy. Okay. So
[23:03:01] now what we're going to do we're going
[23:03:02] to let the chat activity to optimize the
[23:03:05] DDL of the sales DB. So now we have here
[23:03:07] the DDL of the customers employees
[23:03:09] orders and so on. And after running it
[23:03:12] we have the following results. So now we
[23:03:14] have here again the DDL but optimized
[23:03:16] one. And here the AI is adding comment
[23:03:19] about the changes. So here it added the
[23:03:21] auto incremental for the primary key.
[23:03:23] And here for example a check that is not
[23:03:26] a negative score and for the employees.
[23:03:29] Here another check to make sure that the
[23:03:31] birthday is not something in the future.
[23:03:33] So all those constraints in order to
[23:03:35] make sure that the quality of the table
[23:03:37] is good. And here for the gender it is
[23:03:39] restricting the valid values that could
[23:03:42] be used inside this column and many
[23:03:44] other stuff. And at the end we have like
[23:03:47] the key changes. So about the naming
[23:03:49] it's saying that we have to stick with
[23:03:51] one naming convention. So here it did
[23:03:53] understand that we are using the bascal
[23:03:55] case and for those two columns we have
[23:03:57] an issue like for example this product
[23:03:59] it should called product name. And for
[23:04:01] the data types I don't want to go in all
[23:04:02] details. So here for example it says
[23:04:04] don't use the int use a decimal for the
[23:04:06] price and sales for the integrity saying
[23:04:09] go and add foreign keys. I think for the
[23:04:11] orders we don't have any foreign keys
[23:04:14] that is used in the DDL. So the sht did
[23:04:16] go and add all the foreign keys in the
[23:04:19] DDL. So that was good. And now about the
[23:04:22] indexing it says since we have primary
[23:04:24] keys we will get automatically the
[23:04:26] clustered indexing and the foreign keys
[23:04:28] should get as well an index in order to
[23:04:31] improve the queries and so on. So as you
[23:04:33] can see there is a lot of optimizations
[23:04:35] that could be done in our DDL. So now if
[23:04:38] you are working on the project and you
[23:04:39] have a DDL go ask the AI what could we
[23:04:42] optimize I'm sure you will find
[23:04:44] something and this is very critical
[23:04:46] because having a solid and optimized DDL
[23:04:49] improves of course the speed of the
[23:04:51] queries. All right so now we come to
[23:04:53] very useful use case of using AI for
[23:04:55] your SQL projects and that is by using
[23:04:57] AI to generate test data sets. It is
[23:05:00] always really nice to have small data
[23:05:03] sets in order to test the logic of your
[23:05:05] query. Sometimes you are building a
[23:05:07] logic that does not exist yet in your
[23:05:09] database and of course if you are not
[23:05:11] able to test the scenario that you are
[23:05:13] developing it can be really bad and it
[23:05:15] is always very painful process in order
[23:05:17] to generate a data sets for your code
[23:05:19] but of course now it is easier because
[23:05:21] we have the help of AI. So let's check
[23:05:24] the following prompt. It says I need the
[23:05:26] data sets for testing the following SQL
[23:05:28] server DDL. And now next we have to
[23:05:30] specify for the AI different tasks. The
[23:05:32] first one is we have to define the shape
[23:05:35] of the data sets. So how do you want the
[23:05:37] output? Do you want it as an insert
[23:05:39] statements or do you want it as an excel
[23:05:41] or a file and so on. Now the next
[23:05:43] specifications I would like always to
[23:05:44] have a data set that is realistic. So I
[23:05:47] would like to always to have a data set
[23:05:48] that is relevant and realistic not to
[23:05:51] get dummy word data. So again he's like
[23:05:54] only configurations about the data set.
[23:05:56] The next configuration is that I would
[23:05:58] like to have small data sets. Of course,
[23:05:59] you can go and specify for charge the
[23:06:02] exact size of your data sets. You can
[23:06:04] say I would like to have like 100,000
[23:06:06] rows or millions of rows and so on. So
[23:06:08] you can define the size that you want.
[23:06:10] For me, I would like to have like small
[23:06:12] data sets. And now what is very
[23:06:14] important that if you have multiple
[23:06:16] tables in your DDL and those table have
[23:06:19] primary keys and foreign keys, the data
[23:06:21] set should be correct. So the AI should
[23:06:23] generate keys that is joinable. So if
[23:06:26] you go and join data together, you will
[23:06:28] not get weird results. And of course,
[23:06:30] you can go and keep adding
[23:06:31] specifications whether you want to have
[23:06:33] nulls or no nulls inside your data set.
[23:06:36] So here for example, I'm saying don't
[23:06:38] introduce any null values. And of course
[23:06:40] at the end you have to go and give the
[23:06:41] DDL for the AI. It could be one table or
[23:06:44] the whole database. So you could
[23:06:46] generate a data set for one table or
[23:06:48] hundreds of tables. Okay. So now I'm
[23:06:50] asking the SHT to create test data sets
[23:06:53] for two tables. the employees and the
[23:06:55] orders. Let's check the results. So now
[23:06:58] we can see very small nice insert
[23:07:00] statements for the table employees. So
[23:07:02] we have over here like five employees
[23:07:04] with the different informations. And now
[23:07:06] for the table orders we have a lot of
[23:07:08] columns. So as you can see we have four
[23:07:10] orders. And what is very important is
[23:07:12] that the salesperson ID comes from the
[23:07:15] table employees. So as you can see we
[23:07:16] have two and one where we have it
[23:07:18] already in the employees. and the rest
[23:07:20] of the informations we have like here
[23:07:22] fake addresses and stuff. So with that
[23:07:25] we have a very nice test data sets in
[23:07:27] order to be inserted to our database to
[23:07:29] test our queries. Of course we can go
[23:07:31] and ask maybe to extend it maybe instead
[23:07:33] of only four orders we can go with 20
[23:07:35] orders and so on. So we can go and
[23:07:37] change the size of it and here we have
[23:07:39] some notes about the data itself. So it
[23:07:42] is really amazing we are now generating
[23:07:44] this data using our DLS. All right. So
[23:07:46] now we have the following query and of
[23:07:48] course we are using the SQL server and
[23:07:50] let's say that you are migrating from
[23:07:51] SQL server to MySQL. So let's ask
[23:07:54] Shajbet to convert my code to MySQL. All
[23:07:57] right. So after running it as we can see
[23:07:59] now we have the same query but in MySQL.
[23:08:02] So instead of the isnull we are using
[23:08:04] Kawalis and here we are using the
[23:08:06] concatenation instead of the plus
[23:08:08] operator and instead of the get date in
[23:08:11] MySQL we use the now function. And the
[23:08:13] last thing we are using here top 10 but
[23:08:16] in my scale we use limit 10. And here we
[23:08:19] have really nice explanation about the
[23:08:21] transition. So as you can see it is
[23:08:23] amazing and if you are working on
[23:08:25] companies and in projects this might
[23:08:27] happen that there is like decision to
[23:08:29] start migrating from one database to
[23:08:31] another database and then your project
[23:08:33] going to get a big task of migrating the
[23:08:35] data migrating the DDLs and the queries
[23:08:38] and everything and I really recommend
[23:08:40] using the shad in order to help with the
[23:08:42] migration otherwise this big task might
[23:08:45] take really long time. So as you can see
[23:08:47] this is really amazing how shad can
[23:08:49] improve the speed of your
[23:08:54] projects. Okay. Now in the next section
[23:08:56] I'm going to show you the prompts that
[23:08:58] you can use as a student or if you are
[23:09:00] learning any new programming language.
[23:09:02] Okay. So the first thing that you can do
[23:09:04] with Shajibet is that you can ask it to
[23:09:06] generate an SQL course. So you can ask
[23:09:09] the shajibet to guide you step by step
[23:09:11] in your journey learning any programming
[23:09:14] language and you want to do it
[23:09:15] completely onetoone with the AI. So
[23:09:17] first it is very important in creating a
[23:09:20] course is that to give enough context.
[23:09:22] So in this example it is very short I'm
[23:09:24] saying create an SQL course with a
[23:09:26] detailed road map and agenda. But of
[23:09:29] course you can go and give more
[23:09:31] specifications. You can tell about your
[23:09:33] current knowledge. You can specify which
[23:09:35] database type you would like to work
[23:09:37] with MySQL SQL server. So the more
[23:09:39] context and details you give for the AI,
[23:09:42] the better results you're going to get.
[23:09:43] And then you go and configure your
[23:09:45] course. So you can say for example start
[23:09:47] with SQL fundamentals and advance to
[23:09:50] complex topics. And as well we can say
[23:09:52] make it beginner friendly and it is
[23:09:55] important if it is the first time you
[23:09:56] are learning about the topic. And now we
[23:09:58] have to shape the focus of the course
[23:10:00] like I'm saying here include topics that
[23:10:02] is relevant for data analytics because
[23:10:05] SQL is widely used in different topics
[23:10:07] for data engineering data analytics and
[23:10:10] it's really important in each course to
[23:10:12] focus on use cases. So we are saying
[23:10:14] focus on real world data analytics use
[23:10:17] cases and scenarios and of course you
[23:10:19] can go and add more details about your
[23:10:21] course. Okay. So now I just asked the
[23:10:23] shivity in order to make this course. So
[23:10:25] now let's see the road map and the
[23:10:27] structure of our course. So let's start
[23:10:29] with the phase one with the SQL
[23:10:31] fundamentals. So it start with the basic
[23:10:33] select where and so on. Then the next
[23:10:35] section we are talking about order by
[23:10:37] group by and insert update delete. So
[23:10:39] the basic stuff. Now in the road map you
[23:10:41] get the phase two intermediate SQL. So
[23:10:44] here we are talking about inner joins
[23:10:45] few functions about the text the date
[23:10:48] and the case statements and views. And
[23:10:50] now to the phase three we have the
[23:10:52] advanced SQL for analytics. So we have
[23:10:54] the window functions, the CTE and data
[23:10:57] cleaning using the null functions and
[23:10:59] few transformations. Then we go to the
[23:11:02] phase number four. Here in your road map
[23:11:04] you start talking about real world use
[23:11:06] cases. And here you have like multiple
[23:11:08] projects. So as you can see this is
[23:11:10] really solid road map in order to learn
[23:11:13] SQL. And now in the next step what you
[23:11:15] can do you can start deep diving into
[23:11:17] each of those chapters until SQL to
[23:11:19] start okay with the phase number one
[23:11:21] with the week one to give more details.
[23:11:24] All right. So now the next one once you
[23:11:25] have the agenda and the road map
[23:11:27] learning the SQL now you can go and
[23:11:29] focus on specific chapter specific SQL
[23:11:32] concepts. So in this prompt we are
[23:11:33] saying the context first I want detailed
[23:11:36] explanation about SQL window functions
[23:11:38] and now after that we are specifying for
[23:11:40] the AI the exact structure of the
[23:11:43] explanation. So first it should explain
[23:11:45] what are the window functions and maybe
[23:11:47] as well to give an analogy in order to
[23:11:49] understand exactly what is window
[23:11:51] functions and after that it should
[23:11:53] explain why we need them and when to use
[23:11:55] the window functions. So once you
[23:11:57] understand the basics then you can start
[23:11:59] learning about the syntax of the window
[23:12:01] functions and it should provide as well
[23:12:03] few simple examples and at the end the
[23:12:06] AI should show you the best or the most
[23:12:08] frequently use cases used for the SQL
[23:12:11] window functions. So this is the pattern
[23:12:13] that I like in order to learn something
[23:12:15] new. All right. So now let's see how the
[23:12:16] AI going to explain the SQL window
[23:12:18] functions. So as you can see it start
[23:12:19] with the big title understanding SQL
[23:12:21] with the functions. So we have here a
[23:12:24] quick definition and then we have here
[23:12:25] an analogy and the analogy about like a
[23:12:28] teacher grading students. Well that's
[23:12:31] nice because we have the rank function.
[23:12:33] So you have here a nice analogy about
[23:12:34] the window function and then we
[23:12:36] understand why do we need the window
[23:12:38] functions. Well I totally agree in order
[23:12:40] to have row level details with the
[23:12:42] aggregations. So you can do aggregations
[23:12:45] while maintaining the raw level details
[23:12:47] and as well you can do complex
[23:12:49] calculations because you cannot do
[23:12:50] everything with a group I there's
[23:12:52] functions that only work with the window
[23:12:54] and then we have some explanation when
[23:12:56] to use them. So we see here for example
[23:12:58] the syntax of the window function. So it
[23:13:01] divided to a function partition order by
[23:13:03] over and here few explanation about
[23:13:05] that. Then we have few simple examples
[23:13:08] with queries. So explaining the
[23:13:10] different functions but not all of them.
[23:13:12] Of course, you can go and ask the
[23:13:14] schedule to extend the examples for all
[23:13:17] functions. And now we can see the top
[23:13:19] three use cases for the window
[23:13:20] functions. So we use it in order to rank
[23:13:23] the data and as well to build the
[23:13:25] running totals and the moving average.
[23:13:27] And at the end we have a summary. So as
[23:13:29] you can see we have wonderful
[23:13:30] explanation about the concept of the SQL
[23:13:33] window functions. Okay, moving on to the
[23:13:35] next one. And this one I use it very
[23:13:36] frequently in my projects. There is like
[23:13:38] in programming always different concepts
[23:13:41] that are very close to each others and
[23:13:43] sometimes it is confusing and naturally
[23:13:45] clear what are the big differences
[23:13:47] between them. So here I have for you a
[23:13:49] prompt in order to compare different SQL
[23:13:51] concepts. So now the prompt says I want
[23:13:53] to understand the differences between
[23:13:55] SQL window functions and the group by.
[23:13:58] So both of them are used usually to
[23:14:00] aggregate data in SQL and I would like
[23:14:02] to understand more what are the
[23:14:03] differences between them. So we define
[23:14:05] for the AI the following task. Explain
[23:14:07] the key differences between the two
[23:14:09] concepts and then it's really important
[23:14:10] to understand when to use what. So
[23:14:13] describe when to use each concept with
[23:14:15] examples and it's really nice to
[23:14:17] understand as well the advantages and
[23:14:19] the disadvantages of each concept and at
[23:14:21] the end you would like maybe to get a
[23:14:23] quick summarization about the
[23:14:25] differences between those two functions
[23:14:26] side by side in one table. Okay. So now
[23:14:29] let's see how the share GBD can compare
[23:14:31] those two concepts. So first we have
[23:14:32] really nice table in order to see the
[23:14:34] differences between those two. So for
[23:14:36] example the output granularity it says
[23:14:38] the wind function provides calculation
[23:14:41] at the rowle details where the group by
[23:14:43] provides aggregated results at the group
[23:14:46] level detail and if you are talking
[23:14:47] about the functions it allow ranking
[23:14:49] running total moving average and the
[23:14:52] group by it allows only the basic
[23:14:53] aggregations like sum average count. So
[23:14:56] this is really nice overview for the
[23:14:57] differences. Then we have when to use
[23:15:00] which concepts. So it's telling the
[23:15:02] window function it is used if you want
[23:15:04] role level details together with the
[23:15:06] aggregations and here you have like a
[23:15:08] nice example for the group by it says
[23:15:11] you can use it for example when
[23:15:12] summarizing data into categories like
[23:15:15] here grouping up the data by the region
[23:15:17] and then after that we have like pros
[23:15:19] and cons for each concept. So the
[23:15:21] advantage of the window function we get
[23:15:23] all the rows and for the group I it is
[23:15:25] like easier to understand and to use.
[23:15:28] For the disadvantage of the window
[23:15:29] function it is more complex. For the
[23:15:31] group I the disadvantage is it removes
[23:15:34] the details about the rows and at the
[23:15:36] end we have like sideby-side comparison
[23:15:38] between those two concepts. So as you
[23:15:40] can see we have really nice full
[23:15:41] detailed comparison between those two
[23:15:43] SQL concepts. Practicing SQL with the
[23:15:46] AI. Well, it is really not enough to
[23:15:49] just read about something or maybe to
[23:15:51] follow and watch a course in order to
[23:15:54] learn something. You have always to
[23:15:55] practice. And of course, it is really
[23:15:57] hard to find a materials in order to
[23:15:59] practice a new programming language. So,
[23:16:02] we can do it like this. We give a rule
[23:16:04] act as an SQL trainer and then a context
[23:16:06] where we say and help me practice SQL
[23:16:09] window functions and then we go and
[23:16:11] configure this training this practice by
[23:16:13] doing the following. We tell it to make
[23:16:15] it interactive practicing. So the AI
[23:16:17] provide a task and you give a solution.
[23:16:20] And what else is important is that it
[23:16:21] provides you a simple data set and of
[23:16:24] course you can specify which data set
[23:16:26] you want. Is it industrial data set or
[23:16:28] healthcare or anything you want and then
[23:16:30] we tell the AI give SQL task that
[23:16:33] gradually increase in difficulty. So we
[23:16:35] start with the basics until getting
[23:16:37] advanced tasks. And you can tell the AI
[23:16:39] to act as an SQL server and show the
[23:16:42] results of your query. So you would like
[23:16:43] to get as a result not only the correct
[23:16:46] solution or feedback you want to see the
[23:16:48] result of the query that you gives and
[23:16:50] then finally the AI should go and review
[23:16:52] your queries provide a feedback and
[23:16:54] suggest improvements okay so now let's
[23:16:57] start practicing I gave the prompt to
[23:16:58] shity and now we have simple data sets
[23:17:02] so it is very simple we have the sales
[23:17:04] ID employee region sales dates and
[23:17:06] amounts and then we have the first task
[23:17:08] so it says write a query to rank
[23:17:11] employees by their total sales. So here
[23:17:14] you have like an example output and now
[23:17:16] it says your turn. So the shad is
[23:17:18] waiting for your answer. Okay. So now I
[23:17:21] just prepared a query for it. Let's see
[23:17:23] what can happen once I post it. Oh no, I
[23:17:26] got some errors in the query. So let's
[23:17:28] see what we have. So it says error in
[23:17:30] the aggregations. You should use the
[23:17:32] amount instead of sales. And it says
[23:17:35] unnecessary partition by in the rank and
[23:17:37] so on. So let's check the correct query.
[23:17:40] So we have here the group pi and then we
[23:17:42] have to do the window function without
[23:17:45] using partition pi. So that was a
[23:17:47] mistake and the result of this query
[23:17:49] going to be this one. And here I have
[23:17:51] really nice feedback about the first
[23:17:53] task. So now it ask me about the next
[23:17:55] task. So I'm going to say yes. So now we
[23:17:57] have this task number two about the
[23:17:59] running total. We have a task and we
[23:18:00] have the data and we have now to write
[23:18:03] query in order to solve the task. So my
[23:18:05] friends it is nice right interactive and
[23:18:08] not only SQL you can go and practice any
[23:18:10] programming language. Now moving on to
[23:18:12] the last prompt you can use AI in order
[23:18:14] to prepare you for SQL interview. So
[23:18:17] let's say that you are invited to an
[23:18:18] interview and you would like to prepare
[23:18:20] yourself for it. So you can do a quick
[23:18:22] preparation together with the AI. So you
[23:18:24] can say the following act as interviewer
[23:18:26] and prepare me for SQL interview. And
[23:18:29] now you can go and configure the
[23:18:30] interview where you can say ask common
[23:18:32] SQL interview questions and make it
[23:18:35] interactive. So it provide a question
[23:18:37] and then wait for you to answer and then
[23:18:39] you can say gradually progress to
[23:18:41] advanced topics. So from basics to
[23:18:43] advanced and it is very important that
[23:18:45] it evaluates your answer and give you a
[23:18:47] feedback. So it is a really great way to
[23:18:50] prepare for interviews and I really
[23:18:51] recommended to do it and you can prepare
[23:18:53] yourself not only for an SQL interview,
[23:18:55] you can prepare yourself for an SQL
[23:18:57] exam. Okay. Okay. So now let's prepare
[23:18:59] for an ISQL interview. And here we have
[23:19:01] the first question. Shibility says what
[23:19:03] is the difference between where and
[23:19:05] having. So now it is waiting for an
[23:19:07] answer. We can say where filters data
[23:19:12] before
[23:19:13] aggregation and
[23:19:16] having filters data after aggregation.
[23:19:21] So let's check the answer. So here it is
[23:19:23] giving me an example of a very solid
[23:19:26] answer. But in general I have answered
[23:19:28] correctly. So it says the answer is
[23:19:30] correct. But the feedback says here
[23:19:32] maybe the interviewer like needs more
[23:19:34] details not only one sentence about the
[23:19:36] differences. So here it is like
[23:19:38] encouraging me to speak more and to give
[23:19:40] more details as an answer but still the
[23:19:43] answer is correct. So now let's go to
[23:19:45] the next question. What we have here can
[23:19:47] you explain the differences between
[23:19:48] inner join and left join. So I hope you
[23:19:51] know the answer but as you can see it is
[23:19:53] very interactive and nice and I think
[23:19:55] those questions are really relevant. So
[23:19:56] if I'm interviewing someone I'm going to
[23:19:58] go and ask this question. What is the
[23:20:01] difference between where and having and
[23:20:02] as well the differences between the
[23:20:04] joint types. So this is amazing right? I
[23:20:07] really recommend you if you have like an
[23:20:08] interview go and prepare yourself using
[23:20:11] shajbt and you can go and practice and
[23:20:13] prepare yourself before going to the
[23:20:15] interview. All right. So with that you
[23:20:16] have learned how I use AI in order to
[23:20:18] assist me while I'm coding using SQL.
[23:20:21] And now my friends we come to the most
[23:20:23] important chapter from the whole course.
[23:20:25] You have now learned a lot of things
[23:20:27] about SQL. A lot of advanced techniques,
[23:20:29] a lot of functions, how to transform
[23:20:31] data, how to aggregate data. But now
[23:20:33] what you have to do is to take
[23:20:34] everything and to apply it in SQL
[23:20:37] projects. And those projects are not
[23:20:39] only like easy projects. I bought
[23:20:41] projects for you that is very similar to
[23:20:43] the real project that I do in the
[23:20:44] industry. So you will not learn only
[23:20:46] like how to do project in SQL but as
[23:20:49] well what are the main steps and how we
[23:20:51] implement projects in real world. And
[23:20:53] here I have for you three projects data
[23:20:55] warehousing data exploration and
[23:20:57] advanced data analytics. We're going to
[23:20:58] start with the first one the data
[23:21:00] warehousing projects. This one can be
[23:21:02] amazing. So let's go and deep dive in
[23:21:08] that. All right my friends. So now if
[23:21:10] you want to do data analytics projects
[23:21:12] using SQL we have three different types.
[23:21:14] The first type of projects you can do
[23:21:16] data warehousing. It's all about how to
[23:21:18] organize, structure and prepare your
[23:21:21] data for data analyszis. It is the
[23:21:23] foundations of any data analytics
[23:21:25] projects. And in the next step, you can
[23:21:27] do exploratory data analyzes, EDA. And
[23:21:30] all what you have to do is to understand
[23:21:31] and cover insights about our data sets.
[23:21:34] In this kind of project, you can learn
[23:21:36] how to ask the right questions and how
[23:21:38] to find the answer using SQL by just
[23:21:41] using basic SQL skills. Now moving on to
[23:21:43] the last stage where you can do advanced
[23:21:46] analytics projects where you're going to
[23:21:47] use advanced SQL techniques in order to
[23:21:50] answer business questions like finding
[23:21:52] trends over time, comparing the
[23:21:54] performance, segmenting your data into
[23:21:56] different sections and as well generate
[23:21:58] reports for your stakeholders. So here
[23:22:00] you will be solving real business
[23:22:02] questions using advanced SQL techniques.
[23:22:04] Now what we're going to do, we're going
[23:22:05] to start with the first type of projects
[23:22:07] SQL data warehousing where you will gain
[23:22:09] the following skills. So first you will
[23:22:11] learn how to do ETL ELT processing using
[23:22:14] SQL in order to prepare the data. You
[23:22:16] will learn as well how to build data
[23:22:17] architecture, how to do data
[23:22:19] integrations where we're going to merge
[23:22:21] multiple sources together and as well
[23:22:22] how to do data load and data modeling.
[23:22:25] So if I got you interested, grab your
[23:22:26] coffee and let's jump to the
[23:22:31] projects. All right, my friends. So now
[23:22:33] before we deep dive into the tools and
[23:22:35] the cool stuff, we have first to have
[23:22:37] good understanding about what is exactly
[23:22:40] data warehouse why the companies try to
[23:22:42] build such a data management system. So
[23:22:44] now the question is what is a data
[23:22:46] warehouse? I will just use the
[23:22:47] definition of the father of the data
[23:22:49] warehouse bill in a data warehouse is
[23:22:52] subject-oriented integrated time variant
[23:22:55] and nonvolatile collection of data
[23:22:57] designed to support the management's
[23:22:59] decision-making process. Okay, I I know
[23:23:01] that might be confusing.
[23:23:03] Subject-oriented it means that the
[23:23:04] warehouses always focus on a business
[23:23:06] area like the sales, customers, finance
[23:23:09] and so on. Integrated because it goes
[23:23:11] and integrate multiple source systems.
[23:23:13] Usually you build a warehouse not only
[23:23:15] for one source but for multiple sources.
[23:23:18] Time variance it means you can keep
[23:23:20] historical data inside the data
[23:23:22] warehouse. Nonvolatile it means once the
[23:23:24] data enter the data warehouse it is not
[23:23:26] deleted or modified. So this is how
[23:23:29] build inmon defined data warehouse.
[23:23:31] Okay. So now I'm going to show you the
[23:23:32] scenario where your company don't have a
[23:23:34] real data management. So now let's say
[23:23:36] that you have one system and you have
[23:23:38] like one data analyst has to go to this
[23:23:40] system and start collecting and
[23:23:42] extracting the data and then he going to
[23:23:44] spend days and sometimes weeks
[23:23:46] transforming the raw data into something
[23:23:48] meaningful. Then once they have the
[23:23:50] reports they're going to go and share
[23:23:52] it. And this data analyst is sharing the
[23:23:54] report using an Excel. And then you have
[23:23:56] like another source of data and you have
[23:23:58] another data analyst that she is doing
[23:24:00] maybe the same steps collecting the data
[23:24:02] spending a lot of time transforming the
[23:24:04] data and then share at the end like a
[23:24:06] report and this time she is sharing the
[23:24:08] data using PowerPoint and a third system
[23:24:10] and the same story but this time he is
[23:24:12] sharing the data using maybe PowerBI. So
[23:24:15] now if the company works like this then
[23:24:17] there is a lot of issues. First this
[23:24:19] process it take two way long. I saw a
[23:24:22] lot of scenarios where sometimes it
[23:24:23] takes weeks and even months until the
[23:24:26] employee manually generating those
[23:24:28] reports. And of course, what can happen
[23:24:29] for the users? They are consuming
[23:24:31] multiple reports with multiple state of
[23:24:34] the data. One report is 40 days old,
[23:24:36] another one 10 days and a third one is
[23:24:39] like 5 days. So it's going to be really
[23:24:41] hard to make a real decision based on
[23:24:43] this structure. A manual process is
[23:24:45] always slow and stressful and the more
[23:24:47] employees you involved in the process
[23:24:49] the more you open the door for human
[23:24:51] errors and errors of course in reports
[23:24:53] leads to bad decisions and another issue
[23:24:56] of course is handling the big data. If
[23:24:59] one of your sources generating like
[23:25:01] massive amount of data then the data
[23:25:03] analyst going to struggle collecting the
[23:25:05] data and maybe in some scenarios it will
[23:25:07] not be anymore possible to get the data.
[23:25:09] So the whole process can breaks and you
[23:25:11] cannot generate anymore fresh data for
[23:25:14] specific reports. And one last very big
[23:25:16] issue with that. If one of your
[23:25:17] stakeholders asks for an integrated
[23:25:20] report from multiple sources, well good
[23:25:23] luck with that because merging all those
[23:25:25] data manually is very chaotic,
[23:25:27] time-conuming and full of risk. So this
[23:25:29] is just a picture. If a company is
[23:25:31] working without a proper data
[23:25:33] management, without a data leak, data
[23:25:36] warehouse, data lake houses. So in order
[23:25:38] to make real and good decisions, you
[23:25:40] need data management. So now let's talk
[23:25:42] about the scenario of a data warehouse.
[23:25:44] So the first thing that's going to
[23:25:46] happen is that you will not have your
[23:25:48] data team collecting manually the data.
[23:25:51] You're going to have a very important
[23:25:52] component called ETL. ETL stands for
[23:25:55] extract, transform and load. It is a
[23:25:58] process that you do in order to extract
[23:26:00] the data from the sources and then apply
[23:26:02] multiple transformations on those
[23:26:04] sources and at the end it loads the data
[23:26:06] to the data warehouse and this one going
[23:26:08] to be the single point of truth for
[23:26:10] analyzes and reporting and it is called
[23:26:13] data warehouse. So now what can happen
[23:26:15] all your reports going to be consuming
[23:26:18] this single point of truth. So with that
[23:26:20] you create your multiple reports and as
[23:26:22] well you can create integrated reports
[23:26:25] from multiple sources not only from one
[23:26:27] single source. So now by looking to the
[23:26:29] right side it looks already organized
[23:26:31] right and the whole process is
[23:26:33] completely automated. There is no more
[23:26:35] manual steps which of course it reduces
[23:26:38] the human error and as well it is pretty
[23:26:40] fast. So usually you can load the data
[23:26:42] from the sources until the reports in
[23:26:45] matter of hours or sometimes in minutes.
[23:26:48] So there is no need to wait like weeks
[23:26:50] and months in order to refresh anything.
[23:26:52] And of course the big advantage is that
[23:26:54] the data warehouse itself it is
[23:26:56] completely integrated. So that means it
[23:26:58] goes and bring all those sources
[23:27:00] together in one place which makes it
[23:27:02] really easier for reporting and not only
[23:27:05] integrated you can build in the data
[23:27:07] warehouse as well history. So we have
[23:27:09] now the possibility to access historical
[23:27:11] data and what is also amazing is that
[23:27:14] all those reports having the same data
[23:27:16] status. So all those reports can have
[23:27:18] the same status maybe sometimes one day
[23:27:20] old or something. And of course if you
[23:27:22] have a modern data warehouse in cloud
[23:27:24] platforms you can really easily handle
[23:27:27] any big data sources. So no need to
[23:27:29] panic if one of your sources is
[23:27:31] delivering massive amount of data. And
[23:27:33] of course in order to build the data
[23:27:35] warehouse you need different types of
[23:27:36] developers. So usually the one that
[23:27:38] builds the ETL component and the data
[23:27:41] warehouse is the data engineer. So they
[23:27:44] are the one that is accessing the
[23:27:45] sources, scripting the ATLs and building
[23:27:48] the database for the data warehouse. And
[23:27:50] now for the other part, the one that is
[23:27:52] responsible for that is the data
[23:27:54] analyst. They are the one that is
[23:27:56] consuming the data warehouse, building
[23:27:58] different data models and reports and
[23:28:01] sharing it with the stakeholders. So
[23:28:03] they are usually contacting the
[23:28:04] stakeholders, understanding the
[23:28:06] requirements and building multiple
[23:28:08] reports based on the data warehouse. So
[23:28:10] now if you have a look to those two
[23:28:11] scenarios, this is exactly why we need
[23:28:14] data management. Your data team is not
[23:28:17] wasting time and fighting with the data.
[23:28:20] They are now more organized and more
[23:28:22] focused and with like a data warehouse
[23:28:24] and you are delivering professional and
[23:28:26] fresh reports that your company can
[23:28:29] count on in order to make good and fast
[23:28:32] decisions. So this is why you need a
[23:28:34] data management like a data warehouse.
[23:28:36] Think about data warehouse as a busy
[23:28:38] restaurant. Every day different
[23:28:39] suppliers bring in fresh ingredients,
[23:28:42] vegetables, spices, meat, you name it.
[23:28:44] They don't just use it immediately and
[23:28:46] throw everything in one pot, right? They
[23:28:48] clean it, shop it, and organize
[23:28:50] everything and store each ingredients in
[23:28:53] the right place, fridge or freezer. So,
[23:28:55] this is the preparing phase. And when
[23:28:57] the order comes in, they quickly grab
[23:28:59] the prepared ingredients and create a
[23:29:02] perfect dish and then serve it to the
[23:29:04] customers of the restaurant. And this
[23:29:06] process is exactly like the data
[23:29:07] warehouse process. It is like the
[23:29:09] kitchen where the raw ingredients, your
[23:29:11] data are cleaned, sorted and stored. And
[23:29:14] when you need a report or analyzes, it
[23:29:16] is ready to serve up exactly like what
[23:29:19] you
[23:29:22] need. Okay. So now we're going to zoom
[23:29:24] in and focus on the component ETL. If
[23:29:27] you are building such a project, you're
[23:29:28] going to spend almost 90% just building
[23:29:31] this component, the ETL. So it is the
[23:29:34] core element of the data warehouse and I
[23:29:36] want you to have a clear understanding
[23:29:38] what is exactly an ETL. So our data
[23:29:41] exist in a source system. And now what
[23:29:43] we want to do is is to get our data from
[23:29:45] the source and move it to the target.
[23:29:48] Source and target could be like database
[23:29:49] tables. So now the first step that we
[23:29:51] have to do is to specify which data we
[23:29:54] have to load from the source. Of course
[23:29:56] we can say that we want to load
[23:29:58] everything but let's say that we are
[23:29:59] doing incremental loads. So we're going
[23:30:01] to go and specify a subset of the data
[23:30:03] from the source in order to prepare it
[23:30:05] and load it later to the target. So this
[23:30:07] step in the ATL process we call it
[23:30:09] extract. We are just identifying the
[23:30:12] data that we need. We pull it out and we
[23:30:14] don't change anything. It's going to be
[23:30:16] like one to one like the source system.
[23:30:18] So the extract has only one task to
[23:30:20] identify the data that we have to pull
[23:30:23] out from the source and to not change
[23:30:25] anything. So we will not manipulate the
[23:30:26] data at all. It can stay as it is. So
[23:30:29] this is the first step in the ETL
[23:30:31] process, the extract. Now moving on to
[23:30:33] the stage number two. We're going to
[23:30:35] take this extract data and we will do
[23:30:37] some manipulations, transformations and
[23:30:40] we're going to change the shape of those
[23:30:42] data. And this process is really heavy
[23:30:45] working. We can do a lot of stuff like
[23:30:47] data cleansing, data integration and a
[23:30:49] lot of formatting and data
[23:30:51] normalizations. So a lot of stuff we can
[23:30:53] do in this step. So this is the second
[23:30:55] step in the ETL process, the
[23:30:56] transformation. We're going to take the
[23:30:58] original data and reshape it, transform
[23:31:01] it into exactly the format that we need
[23:31:04] into a new format and shapes that we
[23:31:06] need for analyzes and reporting. Now,
[23:31:08] finally, we get to the last step in the
[23:31:10] ATL process. We have the load. So, in
[23:31:12] this step, we're going to take this new
[23:31:14] data and we're going to insert it into
[23:31:16] the target. So, it is very simple. We're
[23:31:18] going to take this prepared data from
[23:31:20] the transformation step and we're going
[23:31:22] to move it into its final destination,
[23:31:24] the target like for example data
[23:31:26] warehouse. So that's ETL in a nutshell.
[23:31:29] First extract the raw data, then
[23:31:31] transform it into something meaningful
[23:31:33] and finally load it to a target where
[23:31:35] it's going to make a difference. So
[23:31:37] that's it. This is what we mean with the
[23:31:39] ETL process. Now in real projects, we
[23:31:41] don't have like only source and targets.
[23:31:43] Our data architecture going to have like
[23:31:45] multiple layers depend on your design
[23:31:48] whether you are building a warehouse or
[23:31:49] a data lake or a data warehouse. And
[23:31:52] usually there are like different ways on
[23:31:53] how to load the data between all those
[23:31:55] layers. And in order now to load the
[23:31:57] data from one layer to another one there
[23:31:59] are like multiple ways on how to use the
[23:32:01] ATL process. So usually if you are
[23:32:03] loading the data from the source to the
[23:32:05] layer number one like only extract the
[23:32:07] data from the source and load it
[23:32:09] directly to the layer number one without
[23:32:11] doing any transformations because I want
[23:32:13] to see the data as it is in the first
[23:32:16] layer. And now between the layer number
[23:32:17] one and the layer number two you might
[23:32:19] go and use the full ETL. So we're going
[23:32:22] to extract from the layer one, transform
[23:32:24] it and then load it to the layer number
[23:32:26] two. So with that we are using the whole
[23:32:28] process the ATL. And now between layer
[23:32:30] two and layer three we can do only
[23:32:32] transformation and then load. So we
[23:32:34] don't have to deal with how to extract
[23:32:36] the data because it is maybe using the
[23:32:38] same technology and we are taking all
[23:32:40] data from layer 2 to layer three. So we
[23:32:43] transform the whole layer 2 and then
[23:32:44] load it to layer three. And now between
[23:32:47] three and four you can use only the LM.
[23:32:49] So maybe it's something like duplicating
[23:32:51] and replicating the data and then you
[23:32:54] are doing the transformation. So you
[23:32:56] load to the new layer and then transform
[23:32:58] it. Of course, this is not a real
[23:32:59] scenario. I'm just showing you that in
[23:33:01] order to move from source to a target,
[23:33:03] you don't have always to use a complete
[23:33:06] ETL. Depend on the design of your data
[23:33:08] architecture. You might use only few
[23:33:10] components from the ETL. Okay. So this
[23:33:13] is how ETL looks like in real projects.
[23:33:16] Okay. So now I would like to show you an
[23:33:17] overview of the different techniques and
[23:33:19] methods in the ETLs. We have wide range
[23:33:22] of possibilities where you have to make
[23:33:24] decisions on which one you want to apply
[23:33:26] to your projects. So let's start first
[23:33:28] with the extraction. The first thing
[23:33:30] that I want to show you is we have
[23:33:31] different methods of extraction. Either
[23:33:33] you are going to the source system and
[23:33:35] pulling the data from the source or the
[23:33:37] source system is pushing the data to the
[23:33:40] data warehouse. So those are the two
[23:33:41] main methods on how to extract data. And
[23:33:44] then we have in the extraction two
[23:33:46] types. We have a full extraction
[23:33:48] everything all the records from tables
[23:33:50] and every day we load all the data to
[23:33:52] the data warehouse or we make more
[23:33:54] smarter one where we say we're going to
[23:33:55] do an incremental extraction where every
[23:33:57] day we're going to identify only the new
[23:34:00] changing data. So we don't have to load
[23:34:02] the whole thing only the new data we go
[23:34:04] extract it and then load it to the data
[23:34:06] warehouse. And in data extraction we
[23:34:08] have different techniques. The first one
[23:34:09] is like manually where someone has to
[23:34:11] access a source system and extract the
[23:34:14] data manually or we connect ourselves to
[23:34:16] a database and we have then a query in
[23:34:18] order to extract the data or we have a
[23:34:20] file that we have to parse it to the
[23:34:22] data warehouse or another technique is
[23:34:24] to connect ourself to API and do their
[23:34:27] calls in order to extract the data or if
[23:34:29] the data is available in streaming like
[23:34:31] in CFKA we can do eventbased streaming
[23:34:34] in order to extract the data. Another
[23:34:36] way is to use the change data capture
[23:34:39] CDC is as well something very similar to
[23:34:41] streaming or another way is by using web
[23:34:44] scrabbing where you have a code that
[23:34:46] going to run and extract all the
[23:34:47] informations from the web. So those are
[23:34:49] the different techniques and types that
[23:34:51] we have in the extraction. Now if you
[23:34:53] are talking on the transformation there
[23:34:55] are wide range of different
[23:34:57] transformations that we can do on our
[23:34:59] data like for example doing data
[23:35:01] enrichment where we add values to our
[23:35:03] data sets or we do a data integration
[23:35:05] where we have multiple sources and we
[23:35:07] bring everything to one data model or we
[23:35:09] derive new columns based on already
[23:35:12] existing one. Another type of data
[23:35:14] transformations we have the data
[23:35:15] normalization. So the sources has values
[23:35:18] that are like a code and you go and map
[23:35:20] it to more friendly values for the
[23:35:22] analyzers which is more easier to
[23:35:24] understand and to use. Another
[23:35:26] transformations we have the business
[23:35:28] rules and logic depend on the business
[23:35:30] you can define different criterias in
[23:35:32] order to build like new columns. And
[23:35:34] what belongs to transformations is the
[23:35:36] data aggregation. So here we aggregate
[23:35:38] the data to a different granularity and
[23:35:41] then we have type of transformation
[23:35:42] called data cleansing. There are many
[23:35:44] different ways on how to clean our data.
[23:35:47] For example, removing the duplicates,
[23:35:49] doing data filtering, handling the
[23:35:51] missing data, handling invalid values or
[23:35:54] removing unwanted spaces, casting the
[23:35:56] data types and detecting the outliers
[23:35:59] and many more. So we have different
[23:36:01] types of data cleansing that we can do
[23:36:03] in our data warehouse and this is very
[23:36:06] important transformation. So as you can
[23:36:07] see we have different types of
[23:36:09] transformations that we can do in our
[23:36:11] data warehouse. Now moving on to the
[23:36:13] load. So what do we have over here? We
[23:36:15] have different processing types. So
[23:36:17] either we are doing patch processing or
[23:36:20] stream processing. Patch processing
[23:36:22] means we are loading the data warehouse
[23:36:24] in one big patch of data that's going to
[23:36:26] run and load the data warehouse. So it
[23:36:29] is only one time job in order to refresh
[23:36:31] the content of the data warehouse and as
[23:36:33] well the reports. So that means we are
[23:36:35] scheduling the data warehouse in order
[23:36:37] to load it in the day once or twice. And
[23:36:39] the other type we have the stream
[23:36:41] processing. So this means if there is
[23:36:43] like a change in the source system,
[23:36:44] we're going to process this change as
[23:36:46] soon as possible. So we're going to
[23:36:48] process it through all the layers of the
[23:36:50] data warehouse once something changes
[23:36:52] from the source system. So we are
[23:36:53] streaming the data in order to have real
[23:36:56] time data warehouse which is very
[23:36:58] challenging things to do in data
[23:36:59] warehousing. And if you are talking
[23:37:01] about the loads we have two methods
[23:37:03] either we are doing a full load or
[23:37:05] incremental load. It's the same thing as
[23:37:07] extraction right? So for the full load
[23:37:09] in databases there are like different
[23:37:11] methods on how to do it like for example
[23:37:13] we truncate and then insert that means
[23:37:15] we make the table completely empty and
[23:37:17] then we insert everything from the
[23:37:19] scratch or another one you are doing an
[23:37:21] update insert we call it upsert. So we
[23:37:24] can go and update all the records and
[23:37:26] then insert the new one and another way
[23:37:28] is to drop create and insert. So that
[23:37:30] means we drop the whole table and then
[23:37:32] we create it from scratch and then we
[23:37:34] insert the data. It is very similar to
[23:37:36] the truncate but here we are as well
[23:37:38] removing and dropping the whole table.
[23:37:40] So those are the different methods of
[23:37:42] full loads. The incremental load we can
[23:37:44] use as well the upserts. So update and
[23:37:46] insert. So we're going to do an update
[23:37:48] or insert statements to our tables. Or
[23:37:50] if the source is something like a log,
[23:37:52] we can do only insert. So we can go and
[23:37:54] append the data always to the table
[23:37:56] without having to update anything.
[23:37:58] Another way to do incremental load is to
[23:38:00] do a merge. And here it is very similar
[23:38:02] to the upsert but as well with a delete.
[23:38:04] So update, insert, delete. So those are
[23:38:07] the different methods on how to load the
[23:38:09] data to your tables. And one more thing
[23:38:10] in data warehousing, we have something
[23:38:12] called slowly changing dimensions. So
[23:38:14] here it's all about the historicizations
[23:38:16] of your table. And there are many
[23:38:18] different ways on how to handle the
[23:38:20] historiizations in your table. The first
[23:38:22] type is sedd0. We say there is
[23:38:24] notoriizations and nothing should be
[23:38:26] changed at all. So that means you are
[23:38:28] not going to update anything. The second
[23:38:30] one which is more famous, it is the sedd
[23:38:33] one. you are doing an overwrite. So that
[23:38:35] means you are updating the records with
[23:38:38] the new informations from the source
[23:38:39] system by overwriting the old value. So
[23:38:41] we are doing something like the upsert.
[23:38:43] So update and insert but you are losing
[23:38:46] of course history. Another one we have
[23:38:47] the sedd2 and here you want to add
[23:38:49] historiizations to your table. So what
[23:38:51] we do each change that we get from the
[23:38:53] source system that means we are
[23:38:55] inserting new records and we are not
[23:38:57] going to overwrite or delete the old
[23:38:59] data. we are just going to make it
[23:39:01] inactive and the new record going to be
[23:39:03] active one. So there are different
[23:39:05] methods on how to do historiizations as
[23:39:07] well while you are loading the data to
[23:39:10] the data warehouse. All right. So those
[23:39:12] are the different types and techniques
[23:39:13] that you might encounter in data
[23:39:15] management projects. So now what I'm
[23:39:16] going to show you quickly which of those
[23:39:18] types we will be using in our projects.
[23:39:20] So now if we are talking about the
[23:39:21] extraction over here we will be doing a
[23:39:24] pull extraction and about the full or
[23:39:26] incremental it's going to be a full
[23:39:28] extraction. And about the technique we
[23:39:30] are going to be parsing files to the
[23:39:32] data warehouse. And now about the data
[23:39:35] transformations. Well, this one we will
[23:39:37] cover everything all those types of
[23:39:39] transformations that I'm showing you now
[23:39:41] is going to be part of the project
[23:39:43] because I believe in each data project
[23:39:45] you will be facing those
[23:39:46] transformations. Now if you have a look
[23:39:48] to the load our project going to be
[23:39:50] patch processing and about the load
[23:39:52] methods we will be doing a full load
[23:39:54] since we have full extraction and it's
[23:39:56] going to be truncate and inserts. And
[23:39:59] now about the historiizations we will be
[23:40:01] doing the sedd one. So that means we
[23:40:04] will be updating the content of the data
[23:40:06] warehouse. So those are the different
[23:40:07] techniques and types that we will be
[23:40:09] using in our ETL process for this
[23:40:11] project. All right. So with that we have
[23:40:13] now clear understanding what is a data
[23:40:15] warehouse and we are done with the
[23:40:17] theory parts. So now the next step we're
[23:40:19] going to start with the projects. The
[23:40:20] first thing that we have to do is to
[23:40:22] prepare our environment to develop the
[23:40:24] projects. So let's start with
[23:40:29] that. All right. So now we go to the
[23:40:32] link in the description and from there
[23:40:34] we're going to go to the downloads and
[23:40:35] you can find all the materials of all
[23:40:37] courses and projects. But the one that
[23:40:39] we need now is the SQL data warehouse
[23:40:41] projects. So let's go to the link and
[23:40:43] here we have bunch of links that we need
[23:40:45] for the projects. But the most important
[23:40:47] one to get all data and files is this
[23:40:49] one download all project files. So let's
[23:40:52] go and do that. And after you do that
[23:40:54] you're going to get a zip file where you
[23:40:56] have there a lot of stuff. So let's go
[23:40:58] and extract it. And now inside it if you
[23:41:00] go over here you will find the
[23:41:01] repository structure from git. And the
[23:41:04] most important one here is the data
[23:41:05] sets. So you have two sources the CRM
[23:41:08] and the ARP. And in each one of them
[23:41:10] there are three CSV files. So those are
[23:41:13] the data set for the projects. For the
[23:41:15] other stuffs don't worry about it. We
[23:41:17] will be explaining that during the
[23:41:18] project. So go and get the data and put
[23:41:21] it somewhere at your PC where you don't
[23:41:22] lose it. Okay. So now what else do we
[23:41:24] have? We have here a link to the get
[23:41:26] repository. So this is the link to my
[23:41:28] repository that I have created through
[23:41:30] the projects. So you can go and access
[23:41:32] it. But don't worry about it. We're
[23:41:33] going to explain the whole structure
[23:41:34] during the projects and you will be
[23:41:36] creating your own repository. And as
[23:41:38] well we have the link to the notion.
[23:41:40] Here we are doing the project
[23:41:42] management. Here you're going to find
[23:41:43] the main steps the main phases of the
[23:41:45] SQL projects that we will do and as well
[23:41:48] all the task that we will be doing
[23:41:49] together during the projects. And now we
[23:41:52] have links to the project tools. So if
[23:41:54] you don't have it already go and
[23:41:55] download the SQL server express. So it's
[23:41:58] like a server that's going to run
[23:41:59] locally at your PC where your database
[23:42:01] going to live. Another one that you have
[23:42:03] to download is the SQL Server Management
[23:42:05] Studio. It is just a client in order to
[23:42:07] interact with the database and there
[23:42:09] we're going to run all our queries and
[23:42:11] then link to the GitHub and as well link
[23:42:13] to the draw AO if you don't have it
[23:42:15] already go and download it. It is free
[23:42:17] and amazing tool in order to draw
[23:42:19] diagrams. So through the projects we
[23:42:21] will be drawing data models the data
[23:42:24] architecture a data lineage. So a lot of
[23:42:26] stuff we'll be doing using this tool. So
[23:42:29] go and download it. And the last thing
[23:42:30] it is nice to have you have a link to
[23:42:32] the notion where you can go and create
[23:42:34] of course free accounts if you want to
[23:42:36] build the project plan and as well
[23:42:38] follow me by creating the project steps
[23:42:41] and the projects tasks. Okay. So that's
[23:42:43] all those are all the links for the
[23:42:45] projects. So go and download all those
[23:42:47] stuff create the accounts and once you
[23:42:49] are ready then we continue with the
[23:42:54] projects. All right. So now I hope that
[23:42:56] you have downloaded all the tools and
[23:42:58] created the accounts. Now it's time to
[23:43:00] move to very important step that almost
[23:43:02] all people skip while doing projects and
[23:43:05] that is by creating the project plan and
[23:43:08] for that we will be using the tool
[23:43:10] notion. Notion is of course a free tool
[23:43:12] and it can help you to organize your
[23:43:14] ideas, your plans and resources all in
[23:43:17] one place. I use it very intensively for
[23:43:20] my private projects like for example
[23:43:21] creating this course and I can tell you
[23:43:23] creating a project plan is the key to
[23:43:25] success. Creating a data warehouse
[23:43:27] project is usually very complex. And
[23:43:29] according to Gartner reports, over 50%
[23:43:32] of data warehouse projects fail. In my
[23:43:35] opinion about any complex project, the
[23:43:37] key to success is to have a clear
[23:43:40] project plan. So now at this phase of
[23:43:41] the project, we're going to go and
[23:43:43] create a rough project plan because at
[23:43:45] the moment we don't have yet clear
[23:43:47] understanding about the data
[23:43:49] architecture. So let's go. Okay. So now
[23:43:51] let's create a new page and let's call
[23:43:52] it data warehouse projects. The first
[23:43:54] thing is that we have to go and create
[23:43:56] the main phases and stages of the
[23:43:59] projects and for that we need a table.
[23:44:00] So in order to do that hit slash and
[23:44:03] then type database in line and then
[23:44:05] let's go and call it something like data
[23:44:08] warehouse epics and we're going to go
[23:44:10] and hide it because I don't like it. And
[23:44:12] then on the table we can go and rename
[23:44:14] it like for example projects epics
[23:44:17] something like that. And now what we're
[23:44:18] going to do we're going to go and list
[23:44:20] all the big task of the project. So an
[23:44:22] epic is usually like a large task that
[23:44:24] needs a lot of efforts in order to solve
[23:44:26] it. So you can call it epics, stages,
[23:44:29] phases of the project, whatever you
[23:44:30] want. So we're going to go and list our
[23:44:32] project steps. So let's start with the
[23:44:35] requirements analyzes and then designing
[23:44:38] data
[23:44:39] architecture and another one we have the
[23:44:42] project
[23:44:44] initialization. So those are the three
[23:44:46] big task in the project first. And now
[23:44:48] what do we need? We need another table
[23:44:50] for the small chunks of the tasks, the
[23:44:52] subtasks and we're going to do the same
[23:44:54] thing. So we're going to go and hit
[23:44:55] slash and we're going to search for the
[23:44:56] table in line and we're going to do the
[23:44:58] same thing. So first we're going to call
[23:45:00] it data warehouse tasks and then we're
[23:45:03] going to hide it and over here we're
[23:45:05] going to rename it and say this is the
[23:45:07] project tasks. So now what we're going
[23:45:09] to do, we're going to go to the plus
[23:45:10] icon over here and then search for
[23:45:12] relation. This one over here with the
[23:45:14] arrow. And now we're going to search for
[23:45:16] the name of the first table. So we
[23:45:18] called it data warehouse eix. So let's
[23:45:21] go and click it and we're going to say
[23:45:22] as well two-way relation. So let's go
[23:45:25] and add the relation. So with that we
[23:45:27] got a field in the new table called data
[23:45:29] warehouse eix. This comes from this
[23:45:31] table and as well we have here data
[23:45:33] warehouse tasks that comes from the
[23:45:35] below table. So as you can see we have
[23:45:37] linked them together. Now what I'm going
[23:45:38] to do I'm going to take this to the left
[23:45:40] side and then what we're going to do
[23:45:41] we're going to go and select one of
[23:45:43] those epics. Like for example let's take
[23:45:45] design the data architecture. And now
[23:45:47] what we're going to do, we're going to
[23:45:48] go and break down this epic into
[23:45:50] multiple tasks. Like for example, choose
[23:45:53] data management approach. And then we
[23:45:57] have another task. What we're going to
[23:45:58] do, we're going to go and select as well
[23:45:59] the same epic. So maybe the next step is
[23:46:02] brainstorm and design the layers. And
[23:46:06] then let's go to another epic for
[23:46:08] example the project initialization. And
[23:46:10] we say over here for example create get
[23:46:13] repo prepare the structure. we can go
[23:46:16] and make another one in the same epic.
[23:46:19] Let's say we're going to go and create
[23:46:20] the database and the schemas. So, as you
[23:46:23] can see, I'm just defining the subtasks
[23:46:25] of those epics. So, now what we're going
[23:46:27] to do, we're going to go and add a
[23:46:28] checkbox in order to understand whether
[23:46:30] we have done the task or not. So, we go
[23:46:32] to the plus and search for check. We
[23:46:34] need a checkbox. And what we're going to
[23:46:36] do, we're going to make it really small
[23:46:39] like this. And with that, each time we
[23:46:41] are done with the task, we're going to
[23:46:42] go and click on it just to make sure
[23:46:44] that we have done the task. Now, there
[23:46:46] is one more thing that is not really
[23:46:47] working nice and that is here. We're
[23:46:49] going to have like a long list of tasks
[23:46:51] and it's really annoying. So, what we're
[23:46:53] going to do, we're going to go to the
[23:46:54] plus over here and let's search for roll
[23:46:56] up. So, let's go and select it. So, now
[23:46:58] what we're going to do, we have to go
[23:46:59] and select the relationship. It's going
[23:47:01] to be the data warehouse task. And after
[23:47:03] that, we're going to go to the property
[23:47:04] and make it as a checkbox. So, now as
[23:47:06] you can see in the first table, we are
[23:47:08] saying how many tasks is closed. But I
[23:47:10] don't want to show it like this. What we
[23:47:12] can do, we're going to go to the
[23:47:12] calculation and to the percent and then
[23:47:15] percent checked. And with that, we can
[23:47:17] see the progress of our project. And now
[23:47:19] instead of the numbers, we can have
[23:47:21] really nice bar. Great. So as well, we
[23:47:23] can go and give it a name like progress.
[23:47:25] So that's it. And we can go and hide the
[23:47:27] data warehouse tasks. And now with that,
[23:47:29] we have really nice progress bar for
[23:47:31] each epic. And if we close all the tasks
[23:47:33] of this epic, we can see that we have
[23:47:35] reached 100%. So this is the main
[23:47:37] structure. Now we can go and add some
[23:47:39] cosmetics and rename stuff in order to
[23:47:41] make things looks nicer. Like for
[23:47:43] example, if I go to the tasks over here,
[23:47:45] I can go and call it tasks and as well
[23:47:48] go and change the icon to something like
[23:47:50] this. And if you'd like to have an icon
[23:47:52] for all those epics, what you're going
[23:47:54] to do, we're going to go to the epic for
[23:47:55] example design data architecture. And
[23:47:57] then if you hover on top of the title,
[23:47:59] you can see add an icon. And you can go
[23:48:01] and pick any icon that you want. So for
[23:48:04] example, this one. And now as you can
[23:48:05] see, we have defined it here in the top.
[23:48:07] And the icon going to be as well in the
[23:48:09] below table. Okay. So now one more thing
[23:48:11] that we can do for the project tasks is
[23:48:13] that we can go and group them by the
[23:48:15] epics. So if you go to the three dots
[23:48:17] and then we go to groups and then we can
[23:48:19] group up by the epics. As you can see
[23:48:22] now we have like a section for each epic
[23:48:24] and you can go and sort the epics if you
[23:48:26] want. If you go over here sort then
[23:48:28] manual and you can go over here and
[23:48:30] start sorting the epics as you want. And
[23:48:33] with that you can expand and minimize
[23:48:35] each task. if you don't want to see
[23:48:37] always all tasks in one go. So this is
[23:48:40] really nice way in order to build like
[23:48:41] data management for your projects. Of
[23:48:43] course, in companies, we use
[23:48:45] professional tools in order to do
[23:48:46] projects like for example Gyra. But for
[23:48:49] private personal projects that I do, I
[23:48:51] always do it like this and I really
[23:48:53] recommend you to do it not only for this
[23:48:55] project, for any project that you are
[23:48:56] doing. Cuz if you see the whole project
[23:48:58] in one go, you can see the big picture
[23:49:00] and closing tasks and doing it like
[23:49:02] this. These small things going to makes
[23:49:04] you really satisfied and keeps you
[23:49:06] motivated to finish the whole project
[23:49:08] and makes you proud. Okay friends, so
[23:49:10] now I just went and added few icons, a
[23:49:13] renamed stuff and as well more tasks for
[23:49:15] each epic and this going to be our
[23:49:17] starting point in the project and once
[23:49:19] we have more informations we're going to
[23:49:20] go and add more details on how exactly
[23:49:23] we're going to build the data warehouse.
[23:49:25] So at the start we're going to go and
[23:49:26] analyze and understand the requirements
[23:49:28] and only after that we're going to start
[23:49:30] designing the data architecture and here
[23:49:33] we have three tasks. First we have to
[23:49:35] choose the data management approach and
[23:49:37] after that we're going to do
[23:49:38] brainstorming and designing the layers
[23:49:40] of the data warehouse and at the end
[23:49:42] we're going to go and draw a data
[23:49:44] architecture. So with that we have clear
[23:49:46] understanding how the data architecture
[23:49:48] looks like and after that we're going to
[23:49:50] go to the next epic where we're going to
[23:49:52] start preparing our projects. So once we
[23:49:54] have clear understanding of the data
[23:49:56] architecture the first task here is to
[23:49:58] go and create detailed project tasks. So
[23:50:01] we're going to go and add more AP and
[23:50:02] more tasks. And once we are done then
[23:50:04] we're going to go and create the naming
[23:50:06] conventions for the project just to make
[23:50:08] sure that we have rules and standards in
[23:50:10] the whole project. And next we're going
[23:50:12] to go and create a repository in the git
[23:50:14] and we're going to prepare as well the
[23:50:15] structure of the repository so that we
[23:50:17] always commit our work there. And then
[23:50:19] we're going to start with the first
[23:50:21] script where we're going to create a
[23:50:22] database and schemas. So my friends this
[23:50:25] is the initial plan for the project. Now
[23:50:27] let's start with the first epic. We have
[23:50:29] the requirements
[23:50:34] analyzes. Now analyzing the requirement,
[23:50:36] it is very important to understand which
[23:50:39] type of data warehouse you're going to
[23:50:40] go and build because there is like not
[23:50:42] only one standard on how to build it.
[23:50:44] And if you go blindly implementing the
[23:50:46] data warehouse, you might be doing a lot
[23:50:48] of stuff that is totally unnecessary and
[23:50:50] you will be burning a lot of time. So
[23:50:53] that's why you have to sit with the
[23:50:54] stakeholders with the department and
[23:50:56] understand what we exactly have to build
[23:50:58] and depend on the requirements you
[23:51:00] design the shape of the data warehouse.
[23:51:03] So now let's go and analyze the
[23:51:04] requirement of this project. Now the
[23:51:06] whole project is splitted into two main
[23:51:08] sections. The first section we have to
[23:51:10] go and build a data warehouse. So this
[23:51:12] is a data engineering task and we will
[23:51:14] go and develop ETLs and data warehouse.
[23:51:17] And once we have done that we have to go
[23:51:20] and build analytics and reporting
[23:51:22] business intelligence. So we're going to
[23:51:24] do data analyszis. But now first we will
[23:51:26] be focusing on the first part building
[23:51:28] the data warehouse. So what do we have
[23:51:30] here? The statement is very simple. It
[23:51:32] says develop a modern data warehouse
[23:51:35] using SQL server to consolidate sales
[23:51:38] data enabling analytical reporting and
[23:51:41] informed decision making. So this is the
[23:51:43] main statements and then we have
[23:51:45] specifications. The first one is about
[23:51:47] the data sources. It says import data
[23:51:49] from two source systems ERB and CRM and
[23:51:52] they are provided as CSV files. And now
[23:51:55] the second task is talking about the
[23:51:57] data quality. We have to clean and fix
[23:51:59] data quality issues before we do the
[23:52:02] data analyzers because let's be real
[23:52:04] there is no raw data that is perfect is
[23:52:06] always messy and we have to clean that
[23:52:08] up. Now the next task is talking about
[23:52:10] the integration. So it says we have to
[23:52:12] go and combine both of the sources into
[23:52:15] one single userfriendly data model that
[23:52:18] is designed for analytics and reporting.
[23:52:21] So that means we have to go and merge
[23:52:22] those two sources into one single data
[23:52:25] model. And now we have here another
[23:52:26] specifications. It says focus on the
[23:52:29] latest data sets. So there is no need
[23:52:31] for historiization. So that means we
[23:52:33] don't have to go and build histories in
[23:52:34] the database. And the final requirement
[23:52:36] is talking about the documentation. So
[23:52:38] it says provide clear documentations of
[23:52:40] the data model. So that means the last
[23:52:42] product of the data warehouse to support
[23:52:45] the business users and the analytical
[23:52:47] teams. So that means we have to generate
[23:52:49] a manual that's going to help the users
[23:52:51] that makes lives easier for the
[23:52:53] consumers of our data. So as you can see
[23:52:55] maybe this is very generic requirements
[23:52:57] but it has a lot of informations already
[23:52:59] for you. So it's saying that we have to
[23:53:01] use the platform SQL server. We have two
[23:53:04] source systems using the CSV files and
[23:53:06] it sounds that we really have a bad data
[23:53:08] quality in the sources and as well it
[23:53:10] wants us to focus on building completely
[23:53:13] new data model that is designed for
[23:53:16] reporting and it says we don't have to
[23:53:18] do historiization and it is expected
[23:53:20] from us to generate documentations of
[23:53:22] the system. So these are the
[23:53:24] requirements for the data engineering
[23:53:26] part where we're going to go and build a
[23:53:28] data warehouse that fulfill these
[23:53:30] requirements. All right. Right. So with
[23:53:31] that we have analyzed the requirements
[23:53:33] and as well we have closed the first
[23:53:35] easiest ebick. So we are done with this.
[23:53:38] Let's go and close it. And now let's
[23:53:39] open another one. Here we have to design
[23:53:42] the data architecture and the first task
[23:53:44] is to choose data management approach.
[23:53:47] So let's
[23:53:50] go. Now designing the data architecture
[23:53:53] it is exactly like building a house. So
[23:53:56] before construction starts, an
[23:53:57] architect's going to go and design a
[23:54:00] plan, a blueprint for the house. How the
[23:54:02] rooms will be connected, how to make the
[23:54:04] house functional, safe and wonderful.
[23:54:07] And without this blueprint from the
[23:54:08] architects, the builders might create
[23:54:10] something unstable, inefficient or maybe
[23:54:13] unlivable. The same goes for data
[23:54:15] projects. A data architect is like a
[23:54:16] house architecture. They design how your
[23:54:19] data will flow, integrate and be
[23:54:21] accessed. So as data architects we make
[23:54:23] sure that the data warehouse is not only
[23:54:25] functioning but also scalable and easy
[23:54:27] to maintain. And this is exactly what we
[23:54:29] will do now. We will play the role of
[23:54:31] the data architect and we will start
[23:54:33] brainstorming and designing the
[23:54:35] architecture of the data warehouse. So
[23:54:37] now I'm going to show you a sketch in
[23:54:39] order to understand what are the
[23:54:40] different approaches in order to design
[23:54:42] a data architecture. And this phase of
[23:54:44] the projects usually is very exciting
[23:54:46] for me because this is my main role in
[23:54:48] data projects. I am a data architect and
[23:54:51] I discuss a lot of different projects
[23:54:53] where we try to find out the best design
[23:54:55] for the projects. All right. So now
[23:54:56] let's
[23:55:00] go. Now the first step of building a
[23:55:02] data architecture is to make a very
[23:55:04] important decision to choose between
[23:55:07] four major types. The first approach is
[23:55:09] to build a data warehouse. It is very
[23:55:11] suitable if you have only structured
[23:55:13] data and your business want to build
[23:55:15] solid foundations for reporting and
[23:55:18] business intelligence. And another
[23:55:20] approach is to build a data leak. This
[23:55:22] one is way more flexible than a data
[23:55:24] warehouse where you can store not only
[23:55:26] structured data but as well semi and
[23:55:29] unstructured data. We usually use this
[23:55:31] approach if you have mixed types of data
[23:55:33] like database tables, logs, images,
[23:55:35] videos and your business want to focus
[23:55:37] not only on reporting but as well on
[23:55:40] advanced analytics or machine learning
[23:55:42] but it's not that organized like a data
[23:55:44] warehouse and data leaks if it's too
[23:55:46] much unorganized and turns into data
[23:55:49] swamp and this is where we need the next
[23:55:51] approach. So the next one we can go and
[23:55:53] build data lakehouse. So it is like a
[23:55:56] mix between data warehouse and data
[23:55:58] lake. You get the flexibility of having
[23:56:00] different types of data from the data
[23:56:02] lake but you still want to structure and
[23:56:04] organize your data like we do in the
[23:56:06] data warehouse. So you mix those two
[23:56:07] words into one and this is a very modern
[23:56:10] way on how to build that architecture
[23:56:12] and this is currently my favorite way of
[23:56:14] building data management system. Now the
[23:56:16] last and very recent approach is to
[23:56:18] build data mesh. So this is a little bit
[23:56:20] different. Instead of having centralized
[23:56:22] data management system the idea now in
[23:56:24] the data mesh is to make it
[23:56:26] decentralized. You cannot have like one
[23:56:28] centralized data management system
[23:56:29] because always if you say centralized
[23:56:31] then it means bottleneck. So instead you
[23:56:34] have multiple departments and multiple
[23:56:35] domains where each one of them is
[23:56:37] building a data product and sharing it
[23:56:39] with others. So now you have to go and
[23:56:41] pick one of those approaches and in this
[23:56:43] project we will be focusing on the data
[23:56:45] warehouse. So now the question is how to
[23:56:47] build the data warehouse. Well there is
[23:56:49] as well four different approaches on how
[23:56:51] to build it. The first one is the
[23:56:53] enimmon approach. So again you have your
[23:56:55] sources and the first layer you start
[23:56:57] with the staging where the row data is
[23:56:59] landing and then the next layer you
[23:57:01] organize your data in something called
[23:57:03] enterprise data warehouse where you go
[23:57:06] and model the data using the third
[23:57:08] normal format. It's about like how to
[23:57:10] structure and normalize your tables. So
[23:57:12] you are building a new integrated data
[23:57:15] model from the multiple sources. And
[23:57:17] then we go to the third layer. It's
[23:57:18] called the data marts where you go and
[23:57:20] take like small subset of the data
[23:57:22] warehouse and you design it in a way
[23:57:25] that is ready to be consumed from
[23:57:27] reporting and it focus on only one topic
[23:57:29] like for example the customers sales or
[23:57:32] products and after that you go and
[23:57:34] connect your BI tool like PowerBI or
[23:57:36] Tableau to the data marts. So with that
[23:57:38] you have three layers to prepare the
[23:57:40] data before reporting. Now moving on to
[23:57:42] the next one we have the Kimple
[23:57:44] approach. He says you know what building
[23:57:46] this enterprise data warehouse it is
[23:57:48] wasting a lot of time. So what we can do
[23:57:50] we can jump immediately from the stage
[23:57:53] layer to the final data because building
[23:57:55] this enterprise data warehouse it is a
[23:57:58] big struggle and usually waste a lot of
[23:58:00] time. So he always want you to focus and
[23:58:02] building the data ms quickly as
[23:58:04] possible. So it is faster approach than
[23:58:06] in but with the time you might get chaos
[23:58:09] in the data MS cuz you are not always
[23:58:10] focusing in the big picture and you
[23:58:12] might be repeating same transformations
[23:58:14] and integrations in different data ms.
[23:58:17] So there is like trade-off between the
[23:58:19] speed and consistent data warehouse. Now
[23:58:22] moving on to the third approach we have
[23:58:23] the data vault. So we still have the
[23:58:25] stage and the data marts but it says we
[23:58:28] still need this central data warehouse
[23:58:30] in the middle but this middle layer
[23:58:32] we're going to bring more standards and
[23:58:34] rules. So it tells you to split this
[23:58:36] middle layer into two layers the row
[23:58:38] vault and the business vault. In the row
[23:58:41] vault you have the original data but in
[23:58:43] the business vault you have all the
[23:58:44] business rules and transformations that
[23:58:46] prepares the data for the data marks. So
[23:58:49] that vault it is very similar to the
[23:58:50] inmon but it brings more standards and
[23:58:53] rules to the middle layer. Now I'm going
[23:58:56] to go and add a fourth one that I'm
[23:58:58] going to call it medallion architecture
[23:59:00] and this one is my favorite one because
[23:59:02] it is very easy to understand and to
[23:59:04] build. So it says you're going to go and
[23:59:06] build three layers bronze, silver and
[23:59:08] gold. The bronze layer it is very
[23:59:10] similar to the stage but we have
[23:59:12] understood with the time that the stage
[23:59:14] layer is very important because having
[23:59:16] the original data as it is it going to
[23:59:18] helps a lot by traceability and finding
[23:59:20] issues. Then the next layer we have the
[23:59:22] silver layer. It is where we do
[23:59:24] transformations data cleansing but we
[23:59:26] don't apply yet any business rules. Now
[23:59:28] moving on to the last layer the gold
[23:59:30] layer. It is as well very similar to the
[23:59:32] data marts but there we can build
[23:59:34] different type of objects not only for
[23:59:36] reporting but as well for machine
[23:59:38] learning for AI and for many different
[23:59:41] purposes. So they are like business
[23:59:43] ready objects that you want to share as
[23:59:45] a data products. So those are the four
[23:59:47] approaches that you can use in order to
[23:59:49] build a data warehouse. So again if you
[23:59:52] are building a data architecture you
[23:59:54] have to specify which approach you want
[23:59:56] to follow. So at the start we said we
[23:59:57] want to build a data warehouse and then
[23:59:59] we have to decide between those four
[24:00:00] approaches on how to build a data
[24:00:02] warehouse and in this project we will be
[24:00:04] using the medallion architecture. So
[24:00:06] this is a very important question that
[24:00:08] you have to answer as the first step of
[24:00:10] building a data architecture. All right.
[24:00:12] So with that we have decided on the
[24:00:13] approach. So we can go and mark it as
[24:00:16] done. The next step we're going to go
[24:00:17] and design the layers of the data
[24:00:20] warehouse.
[24:00:24] Now there is like not 100% standard way
[24:00:28] and rules for each layer. What you have
[24:00:30] to do as a data architects you have to
[24:00:32] define exactly what is the purpose of
[24:00:35] each layer. So we start with the bronze
[24:00:36] layer. So we say it's going to store row
[24:00:39] and unprocessed data as it is from the
[24:00:41] sources. And why we are doing that it is
[24:00:43] for traceability and debugging. If you
[24:00:46] have a layer where you are keeping the
[24:00:47] raw data, it is very important to have
[24:00:49] the data as it is from the sources
[24:00:51] because we can go always back to the
[24:00:53] bronze layer and investigate the data of
[24:00:55] specific source if something goes wrong.
[24:00:58] So the main objective is to have raw
[24:01:01] untouched data that's going to helps you
[24:01:03] as a data engineer by analyzing the root
[24:01:05] cause of issues. Now moving on to the
[24:01:07] server layer. It is the layer where
[24:01:09] we're going to store clean and
[24:01:10] standardized data and this is the place
[24:01:13] where we're going to do basic
[24:01:14] transformations in order to prepare the
[24:01:16] data for the final layer. Now for the go
[24:01:19] layer it's going to contain business
[24:01:20] ready data. So the main goal here is to
[24:01:23] provide data that could be consumed by
[24:01:25] business users and analysts in order to
[24:01:27] build reporting and analytics. So with
[24:01:29] that we have defined the main goal for
[24:01:31] each layer. Now next what I would like
[24:01:33] to do is to define the object types and
[24:01:36] since we are talking about a data
[24:01:37] warehouse in database we have here
[24:01:39] generally two types either a table or a
[24:01:42] view. So we are going for the bronze
[24:01:43] layer and the silver layer with tables
[24:01:45] but for the gold layer we are going with
[24:01:47] the views. So the best practice says for
[24:01:50] the last layer in your data warehouse
[24:01:52] make it virtual using views. It going to
[24:01:54] gives you a lot of dynamic and of course
[24:01:56] speed in order to build it since we
[24:01:58] don't have to make a load process for
[24:01:59] it. And now the next step is that we're
[24:02:01] going to go and define the load method.
[24:02:03] So in this project I have decided to go
[24:02:04] with the full load using the method of
[24:02:06] truncating and inserting. It is just
[24:02:08] faster and way easier. So we're going to
[24:02:10] say for the bronze layer we're going to
[24:02:11] go with the full load. And you have to
[24:02:13] specify as well for the silver layer as
[24:02:15] well. We're going to go with the full
[24:02:16] load. And of course for the views we
[24:02:18] don't need any load process. So each
[24:02:20] time you decide to go with tables you
[24:02:22] have to define the load methods with our
[24:02:24] full load, incremental loads and so on.
[24:02:25] Now we come to the very interesting part
[24:02:27] the data transformations. Now for the
[24:02:29] bronze layer, it is the easiest one
[24:02:31] about this topic because we don't have
[24:02:33] any transformations. We have to commit
[24:02:35] ourself to not touch the data, do not
[24:02:38] manipulate it, don't change anything. So
[24:02:40] it's going to stay as it is. If it comes
[24:02:42] bad, it's going to stay bad in the
[24:02:43] bronze layer. And now we come to the
[24:02:45] silver layer where we have the heavy
[24:02:46] lifting. As we committed in the
[24:02:48] objective, we have to make clean and
[24:02:50] standardized data. And for that we have
[24:02:52] different types of transformations. So
[24:02:54] we have to do data cleansing, data
[24:02:56] standardizations, data normalizations.
[24:02:58] We have to go and derive new columns and
[24:03:01] data enrichment. So there are like bunch
[24:03:03] of transformations that we have to do in
[24:03:06] order to prepare the data. Our focus
[24:03:07] here is to transform the data to make it
[24:03:10] clean and following standards and try to
[24:03:12] push all business transformations to the
[24:03:14] next layer. So that means in the god
[24:03:16] layer we will be focusing on business
[24:03:19] transformations that is needed for the
[24:03:21] consumers for the use cases. So what we
[24:03:23] do here we do data integrations between
[24:03:25] source system we do data aggregations we
[24:03:28] apply a lot of business logics and rules
[24:03:30] and we build a data model that is ready
[24:03:32] for for example business intelligence.
[24:03:34] So here we do a lot of business
[24:03:36] transformations and in the silver layer
[24:03:38] we do basic data transformations. So it
[24:03:40] is really here very important to make
[24:03:43] the fine decisions what type of
[24:03:45] transformations to be done in each layer
[24:03:47] and make sure that you commit to those
[24:03:49] rules. Now the next aspect is about the
[24:03:51] data modeling in the bronze layer and
[24:03:53] the silver layer. We will not break the
[24:03:55] data model that comes from the source
[24:03:57] system. So if the source system deliver
[24:03:59] five tables, we're going to have here
[24:04:00] like five tables and as well in the
[24:04:02] silver layer. We will not go and
[24:04:04] denormalize or normalize or like make
[24:04:06] something new, we're going to leave it
[24:04:08] exactly like it comes from the source
[24:04:09] system because what we're going to do,
[24:04:11] we're going to build the data model in
[24:04:13] the gold layer. And here you have to
[24:04:14] define which data model you want to
[24:04:16] follow. Are you following the star
[24:04:18] schema, the snowflake or are you just
[24:04:20] making aggregated objects? So you have
[24:04:22] to go and make a list of all data models
[24:04:24] types that you're going to follow in the
[24:04:26] gold layer. And at the end, what you can
[24:04:27] specify in each layer is the target
[24:04:29] audience. And this is of course very
[24:04:31] important decision. In the bronze layer,
[24:04:33] you don't want to give access to any end
[24:04:35] user. It is really important to make
[24:04:36] sure that only data engineers access the
[24:04:39] bronze layer. It makes no sense for data
[24:04:41] analysts or data scientists to go to the
[24:04:44] bad data because you have a better
[24:04:46] version for that in the silver layer. So
[24:04:48] in the silver layer of course the data
[24:04:50] engineers have to have an access to it
[24:04:51] and as well the data analysts and the
[24:04:54] data scientists and so on but still you
[24:04:55] don't give it to any business user that
[24:04:58] can't deal with the raw data model from
[24:05:00] the sources because for the business
[24:05:02] users you're going to get a better layer
[24:05:04] for them and that is the go layer. So in
[24:05:06] the gold layer it is suitable for the
[24:05:09] data analyst and as well the business
[24:05:11] users because usually the business users
[24:05:13] don't have a deep knowledge on the
[24:05:14] technicality of the server layer. So if
[24:05:17] you are designing multiple layers you
[24:05:18] have to discuss all those topics and
[24:05:20] make clear decision for each layer. All
[24:05:23] right my friends. So now before we
[24:05:24] proceed with the design I want to tell
[24:05:26] you a secret principle concept that each
[24:05:29] data architect must know and that is the
[24:05:32] separation of concerns. So what is that?
[24:05:34] As you are designing an architecture,
[24:05:36] you have to make sure to break down the
[24:05:38] complex system into smaller independent
[24:05:41] parts and each part is responsible for a
[24:05:44] specific task. And here comes the magic.
[24:05:47] The component of your architecture must
[24:05:49] not be duplicated. So you cannot have
[24:05:51] two parts are doing the same thing. So
[24:05:53] the idea here is to not mix everything.
[24:05:56] And this is one of the biggest mistakes
[24:05:58] in any big projects and I have shown
[24:06:00] that almost everywhere. So a good data
[24:06:03] architects follow this concept this
[24:06:05] principle. So for example if you are
[24:06:07] looking to our data architecture we have
[24:06:09] already done that. So we have defined
[24:06:11] unique set of tasks for each layer. So
[24:06:14] for example we have said in the server
[24:06:16] layer we do data cleansing but in the
[24:06:18] gold layer we do business
[24:06:20] transformations and with that you will
[24:06:22] not be allowing to do any business
[24:06:24] transformations. In the server layer and
[24:06:26] the same thing goes for the gold layer.
[24:06:28] You don't do in the gold layer any data
[24:06:29] cleansing. So each layer has its own
[24:06:32] unique tasks and the same thing goes for
[24:06:34] the bronze layer and the silver layer.
[24:06:36] You do not allow to load data from the
[24:06:38] source systems directly to the silver
[24:06:40] layer because we have decided the
[24:06:42] landing layer. The first layer is the
[24:06:45] bronze layer otherwise you will have
[24:06:46] like set of source systems that are
[24:06:48] loaded first to the bronze layer and
[24:06:50] another set is skipping the layer and
[24:06:52] going to the silver and with that we
[24:06:54] have overlapping. You are doing data
[24:06:56] ingestion in two different layers. So my
[24:06:59] friends, if you have this mindset,
[24:07:01] separation of concerns, I promise you,
[24:07:03] you're going to be a top data architect.
[24:07:05] So think about it. All right, my
[24:07:06] friends. So with that, we have designed
[24:07:08] the layers of the data warehouse. We can
[24:07:10] go ahead close it. The next step, we're
[24:07:12] going to go to DYO and start drawing the
[24:07:14] data
[24:07:18] architecture. So there is like no one
[24:07:20] standard on how to build a data
[24:07:22] architecture. You can add your style and
[24:07:24] the way that you want. So now the first
[24:07:26] thing that we have to show in that
[24:07:27] architecture is the different layers
[24:07:29] that we have. The first layer is the
[24:07:31] source system layer. So let's go and
[24:07:33] take a box like this and make it a
[24:07:35] little bit bigger. And I'm just going to
[24:07:37] go and make the design. So I'm going to
[24:07:38] remove the fill and make the line dotted
[24:07:41] one. And after that I'm going to go and
[24:07:42] change maybe the color to something like
[24:07:44] this gray. So now we have like a
[24:07:46] container for the first layer. And then
[24:07:48] we have to go and add like a text on top
[24:07:50] of it. So what I'm going to do, I'm
[24:07:51] going to take another box. Let's go and
[24:07:53] type inside it sources. And now I'm
[24:07:55] going to go and style it. So I'm going
[24:07:56] to go to the text and make it maybe 24.
[24:07:59] And then remove the lines like this.
[24:08:02] Make it a little bit smaller and put it
[24:08:04] on top. So this is the first layer. This
[24:08:06] is where the data come from. And then
[24:08:08] the data going to go inside a data
[24:08:10] warehouse. So I'm just going to go and
[24:08:12] duplicate this one. This one is the data
[24:08:18] warehouse. All right. So now the third
[24:08:20] layer what it going to be? It's going to
[24:08:22] be the consumers. who will be consuming
[24:08:24] this data warehouse. So I'm going to put
[24:08:27] another box and say this is the consume
[24:08:30] layer. Okay. So those are the three
[24:08:32] containers. Now inside the data
[24:08:34] warehouse, we have decided to build it
[24:08:36] using the medallion architecture. So
[24:08:38] we're going to have three layers inside
[24:08:40] the warehouse. So I'm going to take
[24:08:41] again another box. I'm going to call
[24:08:44] this one. This is the bronze layer. And
[24:08:47] now we have to go and put a design for
[24:08:49] it. So I'm going to go with this color
[24:08:50] over here. And then the text and maybe
[24:08:52] something like 20. And then make it a
[24:08:55] little bit smaller and just put it here.
[24:08:57] And beneath that we're going to have the
[24:09:00] component. So this is just a title of a
[24:09:02] container. So I'm going to have it like
[24:09:03] this. Remove the text from inside it.
[24:09:06] And remove the filling. So this
[24:09:09] container is for the bronze layer. Let's
[24:09:11] go and duplicate it for the next one. So
[24:09:14] this one going to be the silver layer.
[24:09:17] And of course, we can go and change the
[24:09:19] coloring to gray because it is silver.
[24:09:21] And as well the lines and remove the
[24:09:23] filling. Great. And now maybe I'm going
[24:09:25] to make the font as bold. All right. Now
[24:09:28] the third layer going to be the gold
[24:09:31] layer. And we have to go and pick a
[24:09:33] color for that. So style and here we
[24:09:36] have like something like yellow. The
[24:09:38] same thing for the container. I remove
[24:09:40] the filling. So with that we are showing
[24:09:42] now the different layers inside our data
[24:09:44] warehouse. Now those containers are
[24:09:46] empty. What we're going to do, we're
[24:09:47] going to go inside each one of them and
[24:09:48] start adding contents. So now in the
[24:09:50] sources, it is very important to make it
[24:09:52] clear what are the different types of
[24:09:54] source systems that you are connecting
[24:09:56] to the data warehouse because in real
[24:09:58] project there are like multiple types.
[24:09:59] You might have a database, API, files,
[24:10:02] cafka and here it's important to show
[24:10:04] those different types. In other projects
[24:10:06] we have folders and inside those folders
[24:10:08] we have CSV files. So now what you have
[24:10:09] to do we have to make it clear in this
[24:10:11] layer that the input for our project is
[24:10:14] CSV file. So it really depend how you
[24:10:16] want to show that. I'm going to go over
[24:10:18] here and say maybe folder and then I'm
[24:10:20] going to go and take the folder and put
[24:10:21] it here inside and then maybe search for
[24:10:24] file more results and go pick one of
[24:10:26] those icons. For example, I'm going to
[24:10:28] go with this one over here. So I'm going
[24:10:30] to make it smaller and add it on top of
[24:10:32] the folder. So with that we make it
[24:10:34] clear for everyone seeing the
[24:10:35] architecture that the sources is not a
[24:10:38] database is not an API it is a file
[24:10:41] inside the folder. So now very important
[24:10:42] here to show is the source systems. What
[24:10:45] are the sources that is involved in the
[24:10:47] project. So here what we're going to do
[24:10:48] we're going to go and give it a name.
[24:10:49] For example we have one source called
[24:10:51] CRM like this and maybe make the icon
[24:10:54] and we have another source called ERP.
[24:10:56] So we're going to go and duplicate it
[24:10:58] put it over here and then rename it ERP.
[24:11:01] So now it is for everyone clear. We have
[24:11:02] two sources for this project and the
[24:11:04] technology is used is simply a file. So
[24:11:06] now what we can do as well we can go and
[24:11:08] add some descriptions inside this box to
[24:11:10] make it more clear. So what I'm going to
[24:11:12] do, I'm going to take a line because I
[24:11:14] want to split the description from the
[24:11:16] icons something like this and make it
[24:11:18] gray. And then below it, we're going to
[24:11:19] go and add some text and we're going to
[24:11:21] say is CSV file. And the next point and
[24:11:25] we can say the interface is simply files
[24:11:29] in folder. And of course you can go and
[24:11:31] add any specifications and explanation
[24:11:34] about the sources. If it is a database,
[24:11:36] you can say the type of the database and
[24:11:37] so on. So that we made it in the data
[24:11:39] architecture clear what are the sources
[24:11:41] of our data warehouse. And now the next
[24:11:43] step what we're going to do we're going
[24:11:44] to go and design the content of the
[24:11:46] bronze silver and gold. So I'm going to
[24:11:48] start by adding like an icon in each
[24:11:50] container. It is to show about that we
[24:11:52] are talking about database. So what
[24:11:54] we're going to do we're going to go and
[24:11:55] search for database and then more
[24:11:58] result. More results. I'm going to go
[24:12:00] with this icon over here. So let's go
[24:12:03] and make it bigger. Something like this.
[24:12:05] Maybe change the color of dots. So,
[24:12:07] we're going to have the bronze and as
[24:12:09] well here the silver and the gold. So,
[24:12:12] now what we can do, we're going to go
[24:12:13] and add some arrows between those
[24:12:15] layers. So, we're going to go over here.
[24:12:17] So, we can go and search for arrow and
[24:12:18] maybe go and pick one of those. Let's go
[24:12:21] and put it here. And we can go and pick
[24:12:23] a color for that. Maybe something like
[24:12:25] this. And adjust it. So, now we're going
[24:12:27] to have this nice arrow between all the
[24:12:29] layers just to explain the direction of
[24:12:31] our architecture, right? So we can read
[24:12:33] it from left to right and as well
[24:12:36] between the go layer and the consume.
[24:12:38] Okay. So now what I'm going to do next
[24:12:40] we're going to go and add one statement
[24:12:42] about each layer the main objective. So
[24:12:44] let's go and grab a text and put it
[24:12:46] beneath the database and we're going to
[24:12:48] say for example for the bronze layer
[24:12:49] it's going to be the row data. Maybe
[24:12:51] make the text bigger so you are the row
[24:12:54] data. And then the next one in the
[24:12:56] silver you are clean standard data. And
[24:13:01] then the last one for the gold we can
[24:13:03] say business
[24:13:05] ready data. So with that we make the
[24:13:08] objective clear for each layer. Now
[24:13:10] below all those icons what we're going
[24:13:11] to do we're going to have a separator
[24:13:13] again like this. Make it like colored.
[24:13:16] And beneath it we're going to add the
[24:13:17] most important specifications of this
[24:13:19] layer. So let's go and add those
[24:13:21] separators in each layer. Okay. So now
[24:13:24] we need a text below it. Let's take this
[24:13:26] one here. So what is the object type of
[24:13:29] the bronze layer? That's going to be a
[24:13:31] table and we can go and add the load
[24:13:34] methods. We say this is patch
[24:13:36] processing. Since we are not doing
[24:13:38] streaming, we can say it is a full load.
[24:13:41] We are not doing incremental load. So we
[24:13:43] can say here trank and insert. And then
[24:13:48] we add one more section maybe about the
[24:13:50] transformations. So we can say no
[24:13:53] transformations. And one more about the
[24:13:55] data model. We're going to say none as
[24:13:58] is. And now what I'm going to do I'm
[24:14:00] going to go and add those specifications
[24:14:01] as well for the silver and gold. So here
[24:14:03] what we have discussed the object type
[24:14:05] the load process the
[24:14:07] transformations and whether we are
[24:14:08] breaking the data model or not the same
[24:14:11] thing for the gold layer. So I can say
[24:14:13] with that we have really nice layering
[24:14:15] of the data warehouse and what we are
[24:14:16] left is with the consumers over here you
[24:14:19] can go and add the different use cases
[24:14:20] and tools that can access your data
[24:14:22] warehouse like for example I'm adding
[24:14:24] here business intelligence and reporting
[24:14:26] maybe using PowerBI or Tableau or you
[24:14:29] can say you can access my data warehouse
[24:14:30] in order to do at analyzes using the SQL
[24:14:33] queries and this is what we're going to
[24:14:35] focus on the projects after we build the
[24:14:37] data warehouse and as well you can offer
[24:14:39] it for machine learning purposes and of
[24:14:41] course it It's really nice to add some
[24:14:43] icons in your architecture and usually I
[24:14:44] use this nice websites called flat icon.
[24:14:47] It has really amazing icons that you can
[24:14:49] go and use it in your architecture. Now,
[24:14:51] of course, we can go and keep adding
[24:14:53] icons and stuff to explain the data
[24:14:55] architecture and as well the system.
[24:14:56] Like for example, it is very important
[24:14:58] here to say which tools you are using in
[24:15:00] order to build this data warehouse. Is
[24:15:02] it in the cloud? Are using Azure datab
[24:15:04] bricks or maybe snowflake? So we're
[24:15:06] going to go and add for our project the
[24:15:08] icon of SQL server since we are building
[24:15:11] this data warehouse completely in the
[24:15:13] SQL server. So for now I'm really happy
[24:15:14] about it. As you can see we have now a
[24:15:16] plan right. All right guys so with that
[24:15:18] we have designed the data architecture
[24:15:20] using the doyo and with that we have
[24:15:22] done the last step in this epic and now
[24:15:24] with that we have a design for the data
[24:15:26] architecture and we can say we have
[24:15:28] closed this epic. Now let's go to the
[24:15:30] next one. We will start doing the first
[24:15:32] step to prepare our project. And the
[24:15:34] first task here is to create a detailed
[24:15:36] project
[24:15:40] plan. All right, my friends. So now it's
[24:15:43] clear for us that we have three layers
[24:15:44] and we have to go and build them. So
[24:15:46] that means our big epics going to be
[24:15:48] after the layers. So here I have added
[24:15:50] three more epics. So we have build
[24:15:53] bronze layer, build silver layer and
[24:15:55] gold layer. And after that I went and
[24:15:57] start defining all the different tasks
[24:16:00] that we have to follow in the projects.
[24:16:02] So at the start we will be analyzing
[24:16:04] then coding and after that we're going
[24:16:06] to go and do testing and once everything
[24:16:08] is ready we're going to go and document
[24:16:10] stuff and at the end we have to commit
[24:16:12] our work in the get repo. All those
[24:16:14] epics are following the same like
[24:16:16] pattern in the tasks. So as you can see
[24:16:18] now we have a very detailed project
[24:16:20] structure and now things are more
[24:16:22] cleared for us how we're going to build
[24:16:24] the data warehouse. So with that we are
[24:16:26] done from this task and now the next
[24:16:29] task we have to go and define the naming
[24:16:31] convention of the
[24:16:36] projects. All right. So now at this
[24:16:38] phase of the projects we usually define
[24:16:40] the naming conventions. So what is that?
[24:16:42] It is set of rules that you define for
[24:16:45] naming everything in the projects
[24:16:47] whether it is a database, schema,
[24:16:49] tables, stored procedures, folders,
[24:16:52] anything. And if you don't do that at
[24:16:54] the early phase of the projects, I
[24:16:56] promise you chaos can happen because
[24:16:58] what going to happen? You will have
[24:16:59] different developers in your projects
[24:17:01] and each of those developers have their
[24:17:03] own style of course. So one developer
[24:17:05] might name a table dimension customers
[24:17:08] where everything is lowerase and between
[24:17:10] them underscore and you have another
[24:17:12] developer creating another table called
[24:17:14] dimension products but using the camel
[24:17:16] case. So there is no separation between
[24:17:18] the words and the first character is
[24:17:20] capitalized and maybe another one using
[24:17:22] some prefixes like
[24:17:24] dim categories. So we have here like a
[24:17:26] shortcut of the dimension. So as you can
[24:17:28] see there are different designs and
[24:17:30] styles and if you leave the door open
[24:17:32] what can happen in the middle of the
[24:17:33] project you will notice okay everything
[24:17:35] looks inconsistent and you can define a
[24:17:38] big task to go and rename everything
[24:17:41] following a specific rule. So instead of
[24:17:43] wasting all this time at this phase you
[24:17:46] go and define the naming conventions and
[24:17:48] let's go and do that. So we usually
[24:17:50] start with a very important decision and
[24:17:52] that is which naming convention we going
[24:17:55] to follow in the whole project. So you
[24:17:56] have different cases like the camel
[24:17:58] case, the Pascal case, the kebab case,
[24:18:02] and the snake case. And for this
[24:18:04] project, we're going to go with the
[24:18:05] snake case where all the letters of a
[24:18:08] word going to be lowercased. And the
[24:18:10] separation between words going to be an
[24:18:13] underscore. For example, a table name
[24:18:15] called customer info. Customer is
[24:18:17] lowercased. Info is as well lowercased.
[24:18:19] And between them an underscore. So this
[24:18:21] is always the first thing that you have
[24:18:23] to decide for your data projects. The
[24:18:25] second thing is to decide the language.
[24:18:28] So for example, I work in Germany and
[24:18:30] there is always like a decision that we
[24:18:31] have to make whether we use Germany or
[24:18:34] English. So we have to decide for our
[24:18:35] project which language we're going to
[24:18:37] use. And a very important general rule
[24:18:40] is that avoid reserved words. So don't
[24:18:43] use a square reserved word as an object
[24:18:45] name like for example table. Don't give
[24:18:47] a table name as a table. So those are
[24:18:50] the general principles. So those are the
[24:18:52] general rules that you have to follow in
[24:18:54] the whole project. This applies for
[24:18:56] everything for tables, columns, stored
[24:18:58] procedures, any names that you are
[24:19:00] giving in your scripts. Now moving on,
[24:19:02] we have specifications for the table
[24:19:04] names. And here we have different set of
[24:19:06] rules for each layer. So here the rule
[24:19:09] says source system underscore entity. So
[24:19:12] we are saying all the tables in the
[24:19:14] bronze layer should start first with the
[24:19:16] source system name like for example CRM
[24:19:19] or ARB and after that we have an
[24:19:21] underscore and then at the end we have
[24:19:23] the entity name or the table name. So
[24:19:25] for example we have this table name CRM.
[24:19:28] So that means this table comes from the
[24:19:31] source system CRM and then we have the
[24:19:34] table name the entity name customer
[24:19:35] info. So this is the rule that we're
[24:19:37] going to follow in naming all tables in
[24:19:39] the bronze layer. Then moving on to the
[24:19:41] silver layer, it is exactly like the
[24:19:43] bronze because we are not going to
[24:19:45] rename anything. We are not going to
[24:19:47] build any new data model. So the naming
[24:19:50] going to be one one to one like the
[24:19:52] bronze. So it is exactly the same rules
[24:19:54] as the bronze. But if we go to the gold
[24:19:56] here, since we are building new data
[24:19:58] model, we have to go and rename things.
[24:20:01] And since as well we are integrating
[24:20:02] multiple sources together, we will not
[24:20:05] be using the source system name in the
[24:20:07] tables because inside one table you
[24:20:09] could have multiple sources. So the rule
[24:20:11] says all the names must be meaningful
[24:20:13] business aligned names for the tables
[24:20:16] starting with the category prefix. So
[24:20:18] here the rule says it start with
[24:20:20] category then underscore and then
[24:20:22] entity. Now what is category? We have in
[24:20:25] the code layer different types of
[24:20:27] tables. So we could build a table called
[24:20:29] a fact table. Another one could be a
[24:20:31] dimension. A third type could be an
[24:20:33] aggregation or a report. So we have
[24:20:36] different types of tables and we can
[24:20:38] specify those types as a prefix at the
[24:20:41] start. So for example we are saying here
[24:20:44] effect sales. So the category is fact
[24:20:47] and the table name called sales. And
[24:20:49] here I just made like a table with
[24:20:51] different type of patterns. So we could
[24:20:53] have a dimension. So we say it start
[24:20:55] with the dim underscore for example
[24:20:57] dimim customers or products. And then we
[24:21:00] have another type called fact table. So
[24:21:02] it start with fact underscore or
[24:21:04] aggregated table where we have the first
[24:21:06] three characters like aggregating the
[24:21:08] customers or the sales monthly. So as
[24:21:11] you can see as you are creating a naming
[24:21:13] convention you have first to make it
[24:21:15] clear what is the rule describe each
[24:21:17] part of the rule and start giving
[24:21:19] examples. So with that we make it clear
[24:21:21] for the whole team which names they
[24:21:23] should follow. So we talked here about
[24:21:25] the table naming convention. Then you
[24:21:27] can as well go and make naming
[24:21:29] convention for the columns. Like for
[24:21:30] example in the code layer we're going to
[24:21:32] go and have surrogate keys. So we can
[24:21:34] define it like this. The surrogate key
[24:21:36] should start with a table name and then
[24:21:38] underscore a key. Like for example we
[24:21:40] can call it customer key. It is a
[24:21:43] surrogate key in the dimension
[24:21:45] customers. The same thing for technical
[24:21:47] columns. As a data engineer, we might
[24:21:49] add our own columns to the tables that
[24:21:51] don't come from the source system. And
[24:21:53] those columns are the technical columns
[24:21:55] or sometimes we call them metadata
[24:21:57] columns. Now, in order to separate them
[24:21:59] from the original columns that comes
[24:22:02] from the source system, we can have like
[24:22:04] a prefix for that. Like for example, the
[24:22:06] rule says if you are building any
[24:22:08] technical or metadata columns, the
[24:22:10] column should start with DWH underscore
[24:22:13] and then the column name. For example,
[24:22:15] if you want the metadata load dates, we
[24:22:17] can have
[24:22:18] DWH load dates. So with that, if anyone
[24:22:21] sees that column starts with DWH, we
[24:22:24] understand this data comes from a data
[24:22:26] engineer. And we can keep adding rules
[24:22:28] like for example the store procedure
[24:22:30] over here. If you are making an ETL
[24:22:32] script, then it should start with the
[24:22:34] prefix load underscore and then the
[24:22:36] layer. For example, the store procedure
[24:22:38] that is responsible for loading the
[24:22:39] bronze going to be called load bronze.
[24:22:43] and for the silver load underscore
[24:22:45] silver. So those are currently the rules
[24:22:47] for the start procedure. So this is how
[24:22:49] I do it usually in my projects. All
[24:22:50] right my friends. So with that we have a
[24:22:52] solid naming conventions for our
[24:22:54] projects. So this is done and now the
[24:22:56] next step is that we're going to go to
[24:22:57] git and you will create a brand new
[24:22:59] repository and we're going to prepare
[24:23:01] its structure. So let's
[24:23:06] go. All right. Right. So now we come to
[24:23:08] as well important step in any projects
[24:23:10] and that's by creating the G repository.
[24:23:12] So if you are new to Git, don't worry
[24:23:14] about it. It is simpler than it sounds.
[24:23:16] So it's all about to have a safe place
[24:23:18] where you can put your codes that you
[24:23:20] are developing and you will have the
[24:23:21] possibility to track everything happens
[24:23:23] to the codes and as well you can use it
[24:23:25] in order to collaborate with your team
[24:23:27] and if something goes wrong you can
[24:23:29] always roll back. And the best part here
[24:23:31] once you are done with the project you
[24:23:32] can share your repository as a part of
[24:23:34] your portfolio and it is really amazing
[24:23:36] thing if you are applying for a job by
[24:23:38] showcasing your skills that you have
[24:23:40] built a data warehouse by using well
[24:23:42] doumented get repository. So now let's
[24:23:44] go and create the repository of the
[24:23:46] project. Now we are at the overview of
[24:23:48] our account. So the first thing that we
[24:23:50] have to do is to go to the repositories
[24:23:52] over here and then we're going to go to
[24:23:54] this green button and click on new. The
[24:23:56] first thing that we have to do is to
[24:23:57] give the repository name. So let's call
[24:24:00] it SQL data warehouse project and then
[24:24:04] here we can go and give it a
[24:24:05] description. So for example I'm saying
[24:24:08] building a modern data warehouse with
[24:24:09] SQL server. Now the next option whether
[24:24:11] you want to make it public and private.
[24:24:13] I'm going to leave it as a public and
[24:24:15] then let's go and add here a readme
[24:24:17] file. And then here about the license we
[24:24:20] can go over here and select the MIT. MIT
[24:24:23] license gives everyone the freedom of
[24:24:25] using and modifying your code. Okay. So
[24:24:27] I think I'm happy with the setup. Let's
[24:24:29] go and create the repository. And with
[24:24:32] that we have our brand new repository.
[24:24:34] Now the next step that I usually do is
[24:24:36] to create the structure of the
[24:24:38] repository. And usually I always follow
[24:24:40] the same patterns in any projects. So
[24:24:42] here we need few folders in order to put
[24:24:44] our files right. So what I usually do I
[24:24:47] go over here to add file create a new
[24:24:49] file and I start creating the structure
[24:24:51] over here. So the first thing is that we
[24:24:53] need data sets then slash and with that
[24:24:56] the repository going to understand this
[24:24:58] is a folder not a file and then you can
[24:25:00] go and add anything like here
[24:25:03] placeholder just an empty file this just
[24:25:06] going to help me to create the folders
[24:25:07] so let's go and commit so commit the
[24:25:10] changes and now if you go back to the
[24:25:12] main projects you can see now we have a
[24:25:14] folder called data sets so I'm going to
[24:25:16] go and keep creating stuff so I will go
[24:25:19] and create the documents placeholder
[24:25:22] commit the changes and then I'm going to
[24:25:24] go and create the scripts
[24:25:28] placeholder and the final one what I
[24:25:31] usually add is the
[24:25:33] tests something like
[24:25:35] this. So that as you can see now we have
[24:25:38] the main folders of our repository. Now
[24:25:41] what I usually do the next that I'm
[24:25:42] going to go and edit the main readme. So
[24:25:45] you can see it over here as well. So
[24:25:46] what we're going to do, we're going to
[24:25:47] go inside the readme and then we're
[24:25:49] going to go to the edit button here and
[24:25:51] we're going to start writing the main
[24:25:53] information about our project. This is
[24:25:55] really depend on your style. So you can
[24:25:56] go and add whatever you want. This is
[24:25:59] the main page of your repository. And
[24:26:02] now as you can see the file name here is
[24:26:04] MD. It stands for markdown. It is just
[24:26:07] an easy and friendly format in order to
[24:26:10] write a text. So if you have like
[24:26:11] documentations, you are writing a text.
[24:26:13] It is a really nice format in order to
[24:26:15] organize it, structure it and it is very
[24:26:18] friendly. So what I'm going to do at the
[24:26:20] start I'm going to give a few
[24:26:21] description about the project. So we
[24:26:23] have the main title and then we have
[24:26:25] like a welcome message and what this
[24:26:27] repository is about. And in the next
[24:26:29] section maybe we can start with the
[24:26:30] project requirements and then maybe at
[24:26:33] the end you can say a few words about
[24:26:35] the licensing and few words about you.
[24:26:38] So as you can see it's like the homepage
[24:26:39] of the project and the repository. So
[24:26:41] once you are done we're going to go and
[24:26:43] commit the changes. And now if you go to
[24:26:45] the main page of the repository you can
[24:26:48] see always the folder and files at the
[24:26:50] start and then below it we're going to
[24:26:52] see the informations from the readme. So
[24:26:54] again here we have the welcome statement
[24:26:56] and then the projects requirements and
[24:26:59] at the end we have the licensing and
[24:27:00] about me. So my friends that's it. We
[24:27:03] have now a repository and we have now
[24:27:05] the main structure of the project and
[24:27:07] through the projects as we are building
[24:27:09] the data warehouse we're going to go and
[24:27:11] commit all our work in this repository.
[24:27:13] Nice, right? All right. So with that we
[24:27:16] have now your repository ready and as we
[24:27:18] go in the project we will be adding
[24:27:20] stuff to it. So this step is done and
[24:27:23] now the last step finally we're going to
[24:27:25] go to the SQL server and we're going to
[24:27:27] write our first script where we're going
[24:27:28] to create a database and schemas.
[24:27:35] All right. Now the first step is we have
[24:27:36] to go and create a brand new database.
[24:27:39] So now in order to do that first we have
[24:27:41] to switch to the database master. So you
[24:27:43] can do it like this. Use master and
[24:27:46] semicolon. And if you go and execute it
[24:27:48] now we are switched to the master
[24:27:49] database. It is a system database in SQL
[24:27:52] server where you can go and create other
[24:27:54] databases. And you can see here from the
[24:27:55] toolbar that we are now logged into the
[24:27:58] master database. Now the next step we
[24:28:00] have to go and create our new database.
[24:28:02] So we're going to say create database
[24:28:04] and you can call it whatever you want.
[24:28:06] So I'm going to go with data warehouse
[24:28:09] semicolon. Let's go and execute it. And
[24:28:12] with that we have created our database.
[24:28:14] Let's go and check it from the object
[24:28:15] explorer. Let's go and refresh. And you
[24:28:17] can see our new data warehouse. This is
[24:28:19] our new database. Awesome. Right now to
[24:28:22] the next step we're going to go and
[24:28:23] switch to the new database. So we're
[24:28:25] going to say use data
[24:28:28] warehouse and semicolon. So let's go and
[24:28:31] switch to it. And you can see now we are
[24:28:32] logged into the data warehouse database.
[24:28:35] And now we can go and start building
[24:28:36] stuff inside this data warehouse. So now
[24:28:39] the first step that I usually do is I go
[24:28:41] and start creating the schemas. So what
[24:28:44] is schema? Think about it. It's like a
[24:28:46] folder or a container that helps you to
[24:28:48] keep things organized. So now as we
[24:28:50] decided in the architecture we have
[24:28:52] three layers, bronze, silver, gold. And
[24:28:54] now we're going to go and create for
[24:28:55] each layer a schema. So let's go and do
[24:28:58] that. We're going to start with the
[24:28:59] first one. Create schema. And the first
[24:29:02] one is bronze. So let's do it like this.
[24:29:04] And a semicolon. Let's go and create the
[24:29:07] first schema. Nice. So we have new
[24:29:09] schema. Let's go to our database. And
[24:29:12] then in order to check the schemas, we
[24:29:13] go to the security and then to the
[24:29:15] schemas over here. And as you can see,
[24:29:17] we have the bronze. And if you don't
[24:29:19] find it, you have to go and refresh the
[24:29:21] whole schemas. and then you will find
[24:29:23] the new schema. Great. So now we have
[24:29:24] the first schema. Now what we're going
[24:29:26] to do, we're going to go and create the
[24:29:28] others two. So I'm just going to go and
[24:29:29] duplicate it. So the next one going to
[24:29:31] be the silver and the third one going to
[24:29:33] be the gold. So let's go and execute
[24:29:36] those two together. We will get an error
[24:29:38] and that's because we are not having the
[24:29:40] go in between. So after each command,
[24:29:43] let's have a go. And now if I highlight
[24:29:46] the silver and gold and then execute, it
[24:29:49] will be working. the go in SQL it is
[24:29:51] like separator. So it tells SQL first
[24:29:54] execute completely the first command
[24:29:56] before go to the next one. So it is just
[24:29:58] separator. Now let's go to our schemas
[24:30:00] refresh and now we can see as well we
[24:30:02] have the gold and the silver. So with
[24:30:04] that we have now a database. We have the
[24:30:06] three layers and we can start developing
[24:30:09] each layer
[24:30:14] individually. Okay. So now let's go and
[24:30:16] commit our work in the git. So now since
[24:30:18] it is a script and code we're going to
[24:30:20] go to the folder scripts over here and
[24:30:22] then we're going to go and add a new
[24:30:23] file let's call it in it
[24:30:26] database.sql and now we're going to go
[24:30:28] and paste our code over here. So now I
[24:30:30] have done few modifications like for
[24:30:33] example before we create the database we
[24:30:35] have to check whether the database
[24:30:37] exists. This is an important step if you
[24:30:39] are recreating the database otherwise if
[24:30:41] you don't do that you will get an error
[24:30:43] where it's going to say the database
[24:30:45] already exists. So first it is checking
[24:30:47] whether the database exists then it
[24:30:49] drops it. I have added few comments like
[24:30:52] here we are saying creating the data
[24:30:53] warehouse creating the schemas and now
[24:30:56] we have a very important step. We have
[24:30:58] to go and add a header comment at the
[24:31:00] start of each script. To be honest after
[24:31:02] 3 months from now you will not be
[24:31:04] remembering all the details of this
[24:31:06] script. And adding a comment like this
[24:31:08] it is like a sticky note for you later
[24:31:10] once you visit this script again. And it
[24:31:13] is as well very important for the other
[24:31:14] developers in the team because each time
[24:31:16] you open the scripts the first question
[24:31:18] going to be what is the purpose of this
[24:31:20] script because if you or anyone in the
[24:31:23] team open the file the first question
[24:31:25] going to be what is the purpose of this
[24:31:27] scripts why we are doing this stuff. So
[24:31:30] as you can see here we have a comment
[24:31:31] saying this script creates a new data
[24:31:33] warehouse after checking if it already
[24:31:36] exists. If the database exists, it's
[24:31:38] going to drop it and recreate it. And
[24:31:40] additionally, it's going to go and
[24:31:41] create three schemas, bronze, silver,
[24:31:43] gold. So that it gives clarity what this
[24:31:46] script is about. And it makes everyone
[24:31:48] life easier. Now, the second reason why
[24:31:50] this is very important to add is that
[24:31:52] you can add warnings and especially for
[24:31:55] this script, it is very important to add
[24:31:57] these notes because if you run this
[24:31:59] script, what's going to happen? It's
[24:32:00] going to go and destroy the whole
[24:32:02] database. Imagine someone open this
[24:32:04] script and run it. Imagine an admin open
[24:32:07] this script and run it in your database.
[24:32:09] Everything going to be destroyed and all
[24:32:11] the data will be lost and this can be a
[24:32:14] disaster if you don't have any backup.
[24:32:16] So with that we have nice header
[24:32:17] comments and we have added few comments
[24:32:19] in our code and now we are ready to
[24:32:22] commit our code. So let's go and commit
[24:32:24] it. And now we have our script in the
[24:32:27] git as well. And of course if you are
[24:32:29] doing any modifications make sure to
[24:32:31] update the changes in the git. Okay my
[24:32:33] friends. So with that we have an empty
[24:32:35] database and schemas and we are done
[24:32:38] with this task and as well we are done
[24:32:39] with the whole epic. So we have
[24:32:41] completed the project initialization and
[24:32:43] now we're going to go to the interesting
[24:32:45] stuff. We will go and build the bronze
[24:32:47] layer. So now the first task is to
[24:32:49] analyze the source systems. So let's
[24:32:54] go. All right. So now the big question
[24:32:56] is how to build the bronze layer. So
[24:32:59] first thing first we do analyzing. As
[24:33:01] you are developing anything, you don't
[24:33:03] immediately start writing a code. So
[24:33:05] before we start coding the bronze layer,
[24:33:07] what we usually do is we have to
[24:33:09] understand the source system. So what I
[24:33:11] usually do, I make an interview with the
[24:33:13] source system experts and ask them many
[24:33:15] many questions in order to understand
[24:33:18] the nature of the source system that I'm
[24:33:20] connecting to the data warehouse. And
[24:33:22] once you know the source systems, then
[24:33:24] we can start coding. And the main focus
[24:33:26] here is to do the data ingestion. So
[24:33:29] that means we have to find a way on how
[24:33:31] to load the data from the source into
[24:33:33] the data warehouse. So it's like we are
[24:33:35] building a bridge between the source and
[24:33:38] our target system the data warehouse.
[24:33:39] And once we have the code ready, the
[24:33:41] next step is we have to do data
[24:33:43] validation. So here comes the quality
[24:33:45] control. It is very important in the
[24:33:47] bronze layer to check the data
[24:33:48] completeness. So that means we have to
[24:33:50] compare the number of records between
[24:33:53] the source system and the bronze layer
[24:33:56] just to make sure we are not losing any
[24:33:57] data in between. And another check that
[24:33:59] we will be doing is the schema checks
[24:34:01] and that's to make sure that the data is
[24:34:03] placed on the right position. And
[24:34:05] finally we don't have to forget about
[24:34:07] documentation and committing our work in
[24:34:09] the G. So this is the process that we're
[24:34:11] going to follow to build the bronze
[24:34:16] layer. All right my friends. So now
[24:34:19] before connecting any source systems to
[24:34:20] our data warehouse, we have to make very
[24:34:22] important step is to understand the
[24:34:25] sources. So how I usually do it, I set
[24:34:27] up a meeting with the source systems
[24:34:29] expert in order to interview them to ask
[24:34:31] them a lot of stuff about the source.
[24:34:33] And gaining this knowledge is very
[24:34:35] important because asking the right
[24:34:36] question will help you to design the
[24:34:39] correct scripts in order to extract the
[24:34:41] data and to avoid a lot of mistakes and
[24:34:43] challenges. And now I'm going to show
[24:34:44] you the most common questions that I
[24:34:46] usually ask before connecting anything.
[24:34:48] Okay. So we start first by understanding
[24:34:50] the business context and the ownership.
[24:34:53] So I would like to understand the story
[24:34:54] behind the data. I would like to
[24:34:56] understand who is responsible for the
[24:34:57] data, which IT departments and so on.
[24:35:00] And then it's nice to understand as well
[24:35:02] what business process it supports. Does
[24:35:04] it support the customer transactions,
[24:35:06] the supply chain, logistics or maybe
[24:35:09] finance reporting. So with that you can
[24:35:11] understand the importance of your data.
[24:35:12] And then I ask about the system and data
[24:35:15] documentation. So having documentations
[24:35:17] from the source is your learning
[24:35:19] materials about your data. And it's
[24:35:20] going to saves you a lot of time later
[24:35:23] when you are working and designing maybe
[24:35:25] new data models. And as well I would
[24:35:27] like always to understand the data model
[24:35:29] for the source system. And if they have
[24:35:31] like descriptions of the columns and the
[24:35:33] tables, it's going to be nice to have
[24:35:34] the data catalog. This can helps me a
[24:35:36] lot in the data warehouse. How I'm going
[24:35:38] to go and join the tables together. So
[24:35:40] with that you get a solid foundations
[24:35:42] about the business context, the
[24:35:44] processes and the ownership of the data.
[24:35:46] And now in the next step we're going to
[24:35:47] start talking about the technicality. So
[24:35:49] I would like to understand the
[24:35:50] architecture and as well the technology
[24:35:52] stack. So the first question that I
[24:35:54] usually ask is how the source system is
[24:35:56] storing the data. Do we have the data on
[24:35:58] the on-prem like in SQL server, Oracle
[24:36:01] or is it in the cloud like Azure, AWS
[24:36:04] and so on. And then once we understand
[24:36:06] that then we can discuss what are the
[24:36:08] integration capabilities like how I'm
[24:36:11] going to go and get the data. Do the
[24:36:12] source system offer APIs maybe cafka or
[24:36:15] they have only like file extractions or
[24:36:18] they're going to give you like a direct
[24:36:19] connection to the database. So once you
[24:36:21] understand the technology that you're
[24:36:23] going to use in order to extract the
[24:36:24] data then we're going to deep dive into
[24:36:26] more technical questions and here we're
[24:36:28] going to understand how to extract the
[24:36:30] data from the source system and then
[24:36:32] load it into the data warehouse. So the
[24:36:34] first things that we have to discuss
[24:36:35] with the experts can we do an
[24:36:37] incremental load or a full load and then
[24:36:40] after that we're going to discuss the
[24:36:41] data scope the historicizations do we
[24:36:44] need all data do we need only maybe 10
[24:36:46] years of the data are there histories
[24:36:48] already in the source system or should
[24:36:50] we build it in the data warehouse and so
[24:36:52] on and then we're going to go and
[24:36:54] discuss what is the expected size of the
[24:36:57] extracts are we talking here about
[24:36:58] megabytes gigabytes terabytes and this
[24:37:01] is very important to understand whether
[24:37:03] we have the right tools and platform to
[24:37:06] connect that source system and then I
[24:37:08] try to understand whether there are any
[24:37:09] data volume limitations like if you have
[24:37:11] some old source systems they might
[24:37:13] struggle a lot with performance and so
[24:37:15] on. So if you have like an ETL that is
[24:37:17] extracting large amount of data you
[24:37:19] might bring the performance down of the
[24:37:21] source system. So that's why you have to
[24:37:23] try to understand whether there are any
[24:37:25] limitations for your extracts and as
[24:37:27] well other aspects that might impact the
[24:37:30] performance of the source system. This
[24:37:31] is very important. If they give you an
[24:37:33] access to the database, you have to be
[24:37:35] responsible that you are not bringing
[24:37:37] the performance of the database down.
[24:37:39] And of course, very important question
[24:37:40] is to ask about the authentication and
[24:37:43] the authorization like how you going to
[24:37:44] go and access the data in the source
[24:37:46] system. Do you need any tokens, keys,
[24:37:48] password and so on. So those are the
[24:37:50] questions that you have to ask if you
[24:37:52] are connecting a new source system to
[24:37:54] the data warehouse. And once you have
[24:37:56] the answers for those questions, you can
[24:37:58] proceed with the next steps to connect
[24:38:00] the sources to the data warehouse. All
[24:38:02] right, my friends. So with that, you
[24:38:04] have learned how to analyze a new source
[24:38:06] systems that you want to connect to your
[24:38:08] data warehouse. So this step is done and
[24:38:10] now we're going to go back to coding
[24:38:12] where we're going to write scripts in
[24:38:13] order to do the data ingestion from the
[24:38:16] CSV files to the pros
[24:38:20] layer. And let's have a quick look again
[24:38:23] to our bronze layer specifications. So
[24:38:25] we just have to load the data from the
[24:38:27] sources to the data warehouse. We're
[24:38:29] going to build tables in the bronze
[24:38:31] layer. We are doing a full load. So that
[24:38:33] means we are truncating and then
[24:38:34] inserting the data. There will be no
[24:38:36] data transformations at all in the
[24:38:38] bronze layer. And as well we will not be
[24:38:40] creating any data model. So this is the
[24:38:42] specifications of the bronze layer. All
[24:38:45] right. Right now in order to create the
[24:38:46] DDL script for the bronze layer creating
[24:38:48] the tables of the bronze we have to
[24:38:50] understand the metadata the structure
[24:38:52] the schema of the incoming data and here
[24:38:55] either you ask the technical experts
[24:38:57] from the source system about these
[24:38:58] informations or you can go and explore
[24:39:01] the incoming data and try to define the
[24:39:04] structure of your tables. So now what
[24:39:05] we're going to do we're going to start
[24:39:06] with the first source system the CRM. So
[24:39:09] let's go inside it and we're going to
[24:39:11] start with the first table the customer
[24:39:12] info. Now if you open the file and check
[24:39:14] the data inside it, you see we have a
[24:39:16] header information and that is very good
[24:39:18] because now we have the names of the
[24:39:21] columns that are coming from the source
[24:39:22] and from the content you can define of
[24:39:24] course the data types. So let's go and
[24:39:26] do that. First we're going to say create
[24:39:28] table and then we have to define the
[24:39:31] layer. It's going to be the bronze. And
[24:39:32] now very important we have to follow the
[24:39:34] naming convention. So we start with the
[24:39:36] name of the source system. It is CRM
[24:39:38] underscore and then after that the table
[24:39:40] name from the source system. So it's
[24:39:42] going to be the cost underscore info. So
[24:39:45] this is the name of our first table in
[24:39:47] the bronze layer. Then the next step we
[24:39:48] have to go and define of course the
[24:39:50] columns. And here again the column names
[24:39:52] in the bronze layer going to be one to
[24:39:54] one exactly like the source system. So
[24:39:56] the first one going to be the ID and I
[24:39:59] will go with the data type integer. Then
[24:40:01] the next one going to be the key invar
[24:40:04] char and the length I will go with 50.
[24:40:10] [Music]
[24:40:13] And the last one going to be the create
[24:40:16] date. It's going to be date. So with
[24:40:18] that we have covered all the columns
[24:40:20] available from the source system. So
[24:40:22] let's go and check. And yes the last one
[24:40:24] is the create date. So that's it for the
[24:40:26] first table. Now a semicolon of course
[24:40:28] at the end. Let's go and execute it. And
[24:40:30] now we're going to go to the object
[24:40:32] explorer over here. Refresh. And we can
[24:40:34] see the first table inside our data
[24:40:36] warehouse. Amazing right? So now next
[24:40:38] what you have to do is to go and create
[24:40:41] a DDL statement for each file for those
[24:40:44] two systems. So for the CRM we need
[24:40:46] three DDLs and as well for the other
[24:40:49] system the ERP we have as well to create
[24:40:52] three DDLs for the three files. So at
[24:40:54] the end we're going to have in the
[24:40:55] bronze layer six tables six DTLs. So now
[24:40:59] pause the video go create those DDLs. I
[24:41:01] will be doing the same as well and we
[24:41:03] will see you soon.
[24:41:06] [Music]
[24:41:09] All right. So now I hope you have
[24:41:10] created all those details. I'm going to
[24:41:12] show you what I have just created. So
[24:41:13] the second table in the source CRM we
[24:41:16] have the product informations and the
[24:41:18] third one is the sales details. Then we
[24:41:21] go to the second system and here we make
[24:41:23] sure that we are following the naming
[24:41:24] convention. So first the source system
[24:41:26] ERB and then the table name. So the
[24:41:29] second system was really easy. You can
[24:41:31] see we have only here like two columns
[24:41:32] and for the customers like only three
[24:41:35] and for the categories only four
[24:41:36] columns. All right. So after defining
[24:41:38] those stuff of course we have to go and
[24:41:40] execute them. So let's go and do that.
[24:41:43] And then we go to the object explorer
[24:41:44] over here. Refresh the tables. And with
[24:41:47] that you can see we have six empty
[24:41:49] tables in the bronze layer. And with
[24:41:51] that we have all the tables from the two
[24:41:53] source systems inside our database. But
[24:41:56] still we don't have any data. And you
[24:41:58] can see our naming convention is really
[24:41:59] nice. You see the first three tables
[24:42:01] comes from the CRM source system and
[24:42:04] then the other three comes from the ERB.
[24:42:06] So we can see in the bronze layer the
[24:42:08] things are really splitted nicely and
[24:42:10] you can identify quickly which table
[24:42:12] belong to which source system. Now there
[24:42:14] is something else that I usually add to
[24:42:16] the DDL script is to check whether the
[24:42:19] table exists before creating. So for
[24:42:21] example, let's say that you are renaming
[24:42:23] or you would like to change the data
[24:42:25] type of specific field. If you just go
[24:42:27] and run this query, you will get an
[24:42:29] error because the database going to say
[24:42:31] we have already this table. So in other
[24:42:33] databases you can say create or replace
[24:42:35] table. But in the SQL server you have to
[24:42:37] go and build a TSQL logic. So it is very
[24:42:40] simple. First we have to go and check
[24:42:41] whether the object exists in the
[24:42:43] database. So we say if object ID and
[24:42:47] then we have to go and specify the table
[24:42:48] name. So let's go and copy the whole
[24:42:51] thing over here and make sure you get
[24:42:54] exactly the same name as the table name.
[24:42:56] So there you see like space. I'm just
[24:42:57] going to go and remove it. And then
[24:42:59] we're going to go and define the object
[24:43:00] type. So it's going to be the U. It
[24:43:02] stands for user. It is the user defined
[24:43:04] tables. So if this table is not null. So
[24:43:08] that means the database did find this
[24:43:10] object in the database. So what's going
[24:43:12] to happen? We say go and drop the table.
[24:43:16] So the whole thing again and semicolon.
[24:43:18] So again if the table exist in the
[24:43:21] database is not null then go and drop
[24:43:23] the table and after that go and create
[24:43:26] it. So now if you go and highlight the
[24:43:28] whole thing and then execute it it will
[24:43:30] be working. So first drop the table if
[24:43:33] it exist then go and create the table
[24:43:36] from scratch. Now what you have to do is
[24:43:37] to go and add this check before creating
[24:43:40] any table inside our database. So it's
[24:43:42] going to be the same thing for the next
[24:43:44] table and so on. I went and added all
[24:43:47] those checks for each table and what can
[24:43:50] happen if I go and execute the whole
[24:43:52] thing it going to work. So with that I'm
[24:43:54] recreating all the tables in the bronze
[24:43:57] layer from the
[24:44:01] scratch. Now the methods that we're
[24:44:03] going to use in order to load the data
[24:44:05] from the source to the data warehouse is
[24:44:07] the bulk inserts. Pulk insert is a
[24:44:09] method of loading massive amount of data
[24:44:12] very quickly from files like CSV files
[24:44:15] or maybe a text file directly into a
[24:44:18] database. It is not like the classical
[24:44:20] normal inserts where it's going to go
[24:44:22] and insert the data row by row but
[24:44:24] instead the bulk insert is one operation
[24:44:27] that's going to load all the data in one
[24:44:29] go into the database and that's what
[24:44:31] makes it very fast. So let's go and use
[24:44:33] this method. Okay. Okay, so now let's
[24:44:34] start writing the script in order to
[24:44:36] load the first table in the source CRM.
[24:44:39] So we're going to go and load the table
[24:44:41] customer info from the CSV file to the
[24:44:44] database table. So the syntax is very
[24:44:46] simple. We're going to start with saying
[24:44:48] bulk insert. So with that SQL understand
[24:44:50] we are doing not a normal insert, we are
[24:44:52] doing a bulk insert and then we have to
[24:44:54] go and specify the table name. So it is
[24:44:57] bronze dot CRM cost info. So now we have
[24:45:01] to specify the full location of the file
[24:45:04] that we are trying to load in this
[24:45:06] table. So now what we have to do is to
[24:45:08] go and get the path where the file is
[24:45:10] stored. So I'm going to go and copy the
[24:45:12] whole path and then add it to the bulk
[24:45:14] insert exactly like where the data
[24:45:17] exists. So for me it is in CSQL data
[24:45:20] warehouse project data set in the source
[24:45:23] CRM. And then I have to specify the file
[24:45:25] name. So it's going to be like cost
[24:45:27] info. CSV. You have to get it exactly
[24:45:31] like the path of your files otherwise it
[24:45:33] will not be working. So after the path
[24:45:35] now we come to the with clause. Now we
[24:45:38] have to tell the SQL server how to
[24:45:39] handle our file. So here comes the
[24:45:42] specifications. There is a lot of stuff
[24:45:44] that we can define. So let's start with
[24:45:46] the very important one is the row
[24:45:48] header. Now if you check the content of
[24:45:50] our files you can see always the first
[24:45:53] row includes the header information of
[24:45:55] the file. So those informations are
[24:45:58] actually not the data. It's just the
[24:46:00] column names. The actual data starts
[24:46:02] from the second row and we have to tell
[24:46:05] the database about this information. So
[24:46:07] we're going to say first row is actually
[24:46:10] the second row. So with that we are
[24:46:12] telling SQL to skip the first row in the
[24:46:15] file. We don't need to load those
[24:46:16] informations because we have already
[24:46:18] defined the structure of our table. So
[24:46:21] this is the first specifications. The
[24:46:23] next one which is as well very important
[24:46:25] in loading any CSV file is the separator
[24:46:28] between fields. The delimiter between
[24:46:30] fields. So it's really depend on the
[24:46:32] file structure that you are getting from
[24:46:33] the source. As you can see all those
[24:46:35] values are splitted with a comma and we
[24:46:38] call this comma as a file separator or a
[24:46:41] delimter and I saw a lot of different
[24:46:42] CSVs like sometime they use a semicolon
[24:46:45] or a pipe or special character like a
[24:46:47] hash and so on. So you have to
[24:46:49] understand how the values are splitted
[24:46:51] and in this file it's splitted by the
[24:46:53] comma and we have to tell SQL about this
[24:46:55] info. It's very important. So we're
[24:46:57] going to say filled terminator and then
[24:46:59] we're going to say it is the comma and
[24:47:02] basically those two informations are
[24:47:03] very important for SQL in order to be
[24:47:05] able to read your CSV file. Now there
[24:47:08] are like many different options that you
[24:47:10] can go and add. For example, tape lock.
[24:47:13] It is an option in order to improve the
[24:47:15] performance where you are locking the
[24:47:17] entire table during loading it. So as
[24:47:20] SQL is loading the data to this table,
[24:47:22] it going to go and lock the whole table.
[24:47:25] So that's it for now. I'm just going to
[24:47:26] go and add the semicolon and let's go
[24:47:28] and insert the data from the file inside
[24:47:30] our bronze table. Let's execute it. And
[24:47:32] now we can see SQL did insert around
[24:47:34] 80,000 rows inside our table. So it is
[24:47:37] working. We just loaded the file into
[24:47:38] our database. But now it is not enough
[24:47:41] to just write this script. you have to
[24:47:43] test the quality of your bronze table
[24:47:45] especially if you are working with
[24:47:47] files. So let's go and just do a simple
[24:47:49] select. So from our new
[24:47:53] table and let's run it. So now the first
[24:47:56] thing that I check is do we have data
[24:47:58] like in each column? Well yes as you can
[24:48:00] see we have data and the second thing is
[24:48:03] do we have the data in the correct
[24:48:05] column. This is very critical as you are
[24:48:07] loading the data from a file to a
[24:48:09] database. Do we have the data in the
[24:48:11] correct column? So for example, here we
[24:48:12] have the first name which of course
[24:48:14] makes sense and here we have the last
[24:48:15] name. But what could happen and this
[24:48:17] mistakes happens a lot is that you find
[24:48:20] the first name informations inside the
[24:48:22] key and as well you see the last name
[24:48:24] inside the first name and the status
[24:48:27] inside the last name. So there is like
[24:48:29] shifting of the data and this data
[24:48:31] engineering mistake is very common if
[24:48:33] you are working with CSV files and there
[24:48:35] are like different reasons why it
[24:48:36] happens. Maybe the definition of your
[24:48:38] table is wrong or the field separator is
[24:48:41] wrong. Maybe it's not a comma, it's
[24:48:43] something else or the separator is a bad
[24:48:45] separator because sometimes maybe in the
[24:48:47] keys or in the first name there is a
[24:48:50] comma and the SQL is not able to split
[24:48:52] the data correctly. So the quality of
[24:48:54] the CSV file is not really good and
[24:48:56] there are many different reasons why you
[24:48:58] are not getting the data in the correct
[24:49:00] column. But for now everything looks
[24:49:02] fine for us. And the next step is that
[24:49:04] I'll go and count the rows inside this
[24:49:07] table. So let's go and select that. So
[24:49:09] we can see we have
[24:49:12] 18,493. And now what we can do, we can
[24:49:14] go to our CSV file and check how many
[24:49:16] rows do we have inside this file. And as
[24:49:18] you can see we have
[24:49:21] 18,494. We are almost there. There is
[24:49:23] like one extra row inside the file. And
[24:49:25] that's because of the header. the first
[24:49:28] header information is not loaded inside
[24:49:30] our table and that's why always in our
[24:49:32] tables we're going to have one less row
[24:49:34] than the original files. So everything
[24:49:37] looks nice and we have done this step
[24:49:38] correctly. Now if I go and run it again
[24:49:41] what's going to happen we will get
[24:49:43] duplicates inside the bronze layer. So
[24:49:46] now we have loaded the file like twice
[24:49:48] inside the same table which is not
[24:49:50] really correct. The method that we have
[24:49:52] discussed is first to make the table
[24:49:54] empty and then load truncate and then
[24:49:57] insert. In order to do that before the
[24:50:00] bulk inserts, what we're going to do,
[24:50:01] we're going to say truncate table and
[24:50:04] then we're going to have our
[24:50:06] table and that's it with a semicolon. So
[24:50:09] now what we are doing is first we are
[24:50:12] making the table empty and then we start
[24:50:14] loading from the scratch. We are loading
[24:50:16] the whole content of the file inside the
[24:50:19] table and this is what we call full
[24:50:20] load. So now let's go and mark
[24:50:22] everything together and execute. And
[24:50:24] again if you go and check the content of
[24:50:27] the table you can see we have only
[24:50:29] 18,000 rows. Let's go and run it again.
[24:50:32] The count of the bronze layer you can
[24:50:34] see we still have the 18,000. So each
[24:50:36] time you run this script now we are
[24:50:39] refreshing the table customer info from
[24:50:41] the file into the database table. So we
[24:50:44] are refreshing the bronze layer table.
[24:50:46] So that means if there's like now any
[24:50:47] changes in the file, it will be loaded
[24:50:50] to the table. So this is how we do a
[24:50:52] full load in the bronze layer by
[24:50:54] truncating the table and then doing the
[24:50:57] inserts. And now of course what we have
[24:50:58] to do is to pause the video and go and
[24:51:00] write the same script for all six files.
[24:51:04] So let's go and do
[24:51:08] [Music]
[24:51:09] that. Okay, back. So I hope that you
[24:51:12] have as well written all those scripts.
[24:51:14] So I have the three tables in order to
[24:51:16] load the first source system and then
[24:51:18] three sections in order to load the
[24:51:20] second source system. And as I'm writing
[24:51:22] those scripts, make sure to have the
[24:51:23] correct path. So for the second source
[24:51:26] system, you have to go and change the
[24:51:27] path for the other folder. And as well,
[24:51:29] don't forget the table name on the
[24:51:30] bronze layer is different from the file
[24:51:32] name because we start always with the
[24:51:34] source system name with the files. We
[24:51:37] don't have that. So now I think I have
[24:51:38] everything is ready. So let's go and
[24:51:41] execute the whole thing. Perfect.
[24:51:43] Awesome. So everything is working. Let
[24:51:45] me check the messages. So we can see
[24:51:47] from the message how many rows are
[24:51:49] inserted in each table. And now of
[24:51:51] course the task is to go through each
[24:51:53] table and check the
[24:51:58] content. So that means now we have
[24:52:00] really nice script in order to load the
[24:52:03] bronze layer. And we will use this
[24:52:05] script in daily basis. every day we have
[24:52:07] to run it in order to get a new content
[24:52:10] to the data warehouse. And as we learned
[24:52:12] before, if you have like a script of SQL
[24:52:14] that is frequently used, what we can do,
[24:52:17] we can go and create a stored procedure
[24:52:20] from those scripts. So let's go and do
[24:52:22] that. It's going to be very simple.
[24:52:23] We're going to go over here and say
[24:52:25] create or alter procedure. And now we
[24:52:28] have to define the name of the S
[24:52:30] procedure. I'm going to go and put it in
[24:52:31] the schema bronze because it belongs to
[24:52:34] the bronze layer. So then we're going to
[24:52:36] go and follow the naming convention. The
[24:52:38] source procedure start with load
[24:52:39] underscore and then the bronze layer. So
[24:52:42] that's it about the name and then very
[24:52:44] important we have to define the begin
[24:52:45] and as well the end of our skill
[24:52:47] statements. So here is the begin and
[24:52:50] let's go to the end and say this is the
[24:52:53] end. And then let's go highlight
[24:52:55] everything in between and give it one
[24:52:57] push with tab. So with that it is easier
[24:52:59] to read. So now next what we're going to
[24:53:01] do we're going to go and execute it. So
[24:53:02] let's go and create this store
[24:53:04] procedure. And now if you want to go and
[24:53:05] check your store procedure, you go to
[24:53:07] the database and then we have here a
[24:53:08] folder called programmability. And then
[24:53:10] inside it we have start procedure. So if
[24:53:12] you go and refresh, you will see our new
[24:53:14] stored procedure. Let's go and test it.
[24:53:16] So I'm going to go and have a new query.
[24:53:18] And what we're going to do, we're going
[24:53:19] to say execute
[24:53:22] bronze.load bronze. So let's go and
[24:53:25] execute it. And with that, we have just
[24:53:27] loaded completely the bronze layer. So
[24:53:29] as you can see SQL did go and insert all
[24:53:32] the data from the files to the bronze
[24:53:34] layer. It is way easier than each time
[24:53:36] running those scripts of course. All
[24:53:38] right. So now the next step is that as
[24:53:39] you can see the output message it is
[24:53:42] really not having a lot of informations.
[24:53:44] The message of your ETL sold procedure
[24:53:47] it will not be really clear. So that's
[24:53:49] why if you are writing an ETL script
[24:53:51] always take care of the messaging of
[24:53:53] your code. So let me show you a nice
[24:53:55] design. Let's go back to our store
[24:53:57] procedure. So now what we can do we can
[24:53:58] go and divide the message based on our
[24:54:01] code. So now we can start with the
[24:54:03] message for example over here let's say
[24:54:04] print and we say what we are doing with
[24:54:07] this store procedure we are loading the
[24:54:09] bronze liar. So this is the main message
[24:54:12] the most important one and we can go and
[24:54:14] play with the separators like this. So
[24:54:16] we can say print and now we can go and
[24:54:18] add some nice separators like for
[24:54:20] example the equals at the start and at
[24:54:23] the end just to have like a section. So
[24:54:25] this is just a nice message at the
[24:54:26] start. So now by looking to our code we
[24:54:29] can see that our code is splitted into
[24:54:31] two sections. The first section we are
[24:54:33] loading all the tables from the source
[24:54:35] system CRM and the second section is
[24:54:37] loading the tables from the ERP. So we
[24:54:39] can split the prints by the source
[24:54:41] system. So let's go and do that. So
[24:54:43] we're going to say print and we're going
[24:54:45] to say loading CRM tables. This is for
[24:54:49] the first section. And then we can go
[24:54:51] and add some nice separators like the
[24:54:53] one. Let's take the minus. And of
[24:54:56] course, don't forget to add semicolons
[24:54:58] like me. So, we're going to have
[24:55:00] semicolon for each prints. Same thing
[24:55:03] over here. I will go and copy the whole
[24:55:05] thing because we're going to have it at
[24:55:06] the start and as well at the ends. Let's
[24:55:09] go copy the whole thing for the second
[24:55:11] section. So, for the ERP, it starts over
[24:55:13] here. And we're going to have it like
[24:55:15] this. And we're going to call it loading
[24:55:17] ERP. So, with that in the output, we can
[24:55:20] see nice separation between loading each
[24:55:22] source system. Now we go to the next
[24:55:24] step where we go and add like a print
[24:55:26] for each action. So for example here we
[24:55:29] are truncating the table. So we say
[24:55:31] print and now what we can do we can go
[24:55:33] and add two arrows and we say what we
[24:55:36] are doing. So we are truncating the
[24:55:38] table and then we can go and add the
[24:55:40] table name in the message as well. So
[24:55:42] this is the first action that we are
[24:55:43] doing and we can go and add another
[24:55:45] print for inserting the data. So we can
[24:55:48] say inserting data into and then we have
[24:55:53] the table name. So with that in the
[24:55:54] output we can understand what SQL is
[24:55:56] doing. So let's go and repeat this for
[24:55:58] all other tables. Okay. So I just added
[24:56:01] all those prints and don't forget the
[24:56:03] semicolon at the end. So I would say
[24:56:05] let's go and execute it and check the
[24:56:07] output. So let's go and do that and then
[24:56:09] maybe at the start just to have quick
[24:56:11] output execute our stored procedure like
[24:56:14] this. So let's see now if you check the
[24:56:17] output you can see things are more
[24:56:19] organized than before. So at the start
[24:56:21] we are reading okay we are loading the
[24:56:23] bronze layer. Now first we are loading
[24:56:25] the source system CRM and then the
[24:56:27] second section is for the ERP and we can
[24:56:29] see the actions. So we are truncating
[24:56:31] inserting truncating inserting for each
[24:56:33] table and as well the same thing for the
[24:56:35] second source. So as you can see it is
[24:56:37] nice and cosmetic but it's very
[24:56:39] important as you are debugging any
[24:56:41] errors. And speaking of errors, we have
[24:56:43] to go and handle the errors in our store
[24:56:46] procedure. So let's go and do that. It's
[24:56:48] going to be the first thing that we do.
[24:56:50] We say begin try and then we go to the
[24:56:52] end of our script and we say before the
[24:56:55] last end we say end try and then the
[24:56:58] next thing we have to add the catch. So
[24:57:01] we're going to say begin catch and end
[24:57:04] catch. So now first let's go and
[24:57:06] organize our code. I'm going to take the
[24:57:08] whole codes and give it one more push
[24:57:11] and as well the begin try. So it is more
[24:57:14] organized and as you know the try and
[24:57:16] catch going to go and execute the try
[24:57:19] and if there is like any errors during
[24:57:22] executing this script the second section
[24:57:24] going to be executed. So the catch will
[24:57:26] be executed only if the SQL failed to
[24:57:30] run the try. So now what we have to do
[24:57:32] is to go and define for SQL what to do
[24:57:34] if there's like an error in your code.
[24:57:36] And here we can do multiple stuff like
[24:57:38] maybe creating a logging tables and add
[24:57:40] the messages inside this table or we can
[24:57:43] go and add some nice messaging to the
[24:57:45] output like for example we can go and
[24:57:47] add like a section again over here. So
[24:57:50] again some equals and we can go and
[24:57:53] repeat it over here and then add some
[24:57:54] content in between. So we can start with
[24:57:57] something like to say error
[24:58:00] accord during loading bronze layer and
[24:58:04] then we can go and add many stuff like
[24:58:06] for example we can go and add the error
[24:58:10] message and here we can go and call the
[24:58:12] function
[24:58:13] error message and we can go and add as
[24:58:17] well for example the error number. So
[24:58:20] error number and of course the output of
[24:58:22] this going to be a number but the error
[24:58:24] message here is a text. So we have to go
[24:58:26] and change the data type. So we're going
[24:58:28] to do a cast as invar like this and then
[24:58:33] there is like many functions that you
[24:58:34] can add to the output like for example
[24:58:37] the error state and so on. So you can
[24:58:39] design what can happen if there is an
[24:58:41] error in the ETL. Now what else is very
[24:58:43] important in each ATL process is to add
[24:58:46] the duration of each like step. So for
[24:58:49] example, I would like to understand how
[24:58:51] long it takes to load this table over
[24:58:53] here. But looking to the output, I don't
[24:58:55] have any informations how long is taking
[24:58:57] to load my tables. And this is very
[24:58:59] important because as you are building
[24:59:01] like a big data warehouse, the ETL
[24:59:04] process going to take long time and you
[24:59:05] would like to understand where is the
[24:59:07] issue, where is the bottleneck, which
[24:59:09] table is consuming a lot of time to be
[24:59:11] loaded. So that's why we have to add
[24:59:13] those informations as well to the output
[24:59:16] or even maybe to protocol it in a table.
[24:59:18] So let's go and add as well this step.
[24:59:20] So we're going to go to the start and
[24:59:21] now in order to calculate the duration
[24:59:23] you need the starting time and the end
[24:59:25] time. So we have to understand when we
[24:59:28] start loaded and when we ended loading
[24:59:30] the table. So now the first thing is we
[24:59:32] have to go and declare the variables. So
[24:59:35] we're going to say declare and then
[24:59:36] let's make one called start time and the
[24:59:39] data type of this going to be the date
[24:59:40] time. I need exactly the second when it
[24:59:43] started and then another one for the end
[24:59:45] time. So another variable end time and
[24:59:49] as well the same thing date time. So
[24:59:51] with that we have declared the variables
[24:59:53] and the next step is to go and use them.
[24:59:55] So now let's go to the first table to
[24:59:57] the customer info and at the start we're
[24:59:59] going to say set start
[25:00:01] time equal to get date. So we will get
[25:00:05] the exact time when we start loading
[25:00:08] this table. And then let's go and copy
[25:00:10] the whole thing and go to the end of
[25:00:12] loading over here. So we're going to say
[25:00:14] set this time the end time equal as well
[25:00:17] to the get dates. So with that now we
[25:00:20] have the values of when we start loading
[25:00:22] this table and when we completed loading
[25:00:25] the table. And now the next step is we
[25:00:26] have to go and print the duration those
[25:00:29] informations. So over here we can go and
[25:00:31] say print and we can go and have as
[25:00:33] again the same design. So two arrows and
[25:00:35] we can say very simply load duration and
[25:00:38] then double points and a space. And now
[25:00:41] what we have to do is to calculate the
[25:00:43] duration and we can do that using the
[25:00:45] date and time function date diff in
[25:00:47] order to find the interval between two
[25:00:50] dates. So we're going to say plus over
[25:00:52] here and then use date diff. And here we
[25:00:54] have to define three arguments. First
[25:00:56] one is the unit. So here you can define
[25:00:58] second, minute, hours and so on. So
[25:01:00] we're going to go with the second and
[25:01:01] then we're going to define the start of
[25:01:03] the interval. It's going to be the start
[25:01:04] time. And then the last argument it
[25:01:06] going to be the end of the boundary.
[25:01:08] It's going to be the end time. And now
[25:01:10] of course the output of this going to be
[25:01:11] a number that's why we have to go and
[25:01:13] cast it. So we're going to say cast as
[25:01:16] invar and then we're going to close it
[25:01:18] like this and maybe at the end we're
[25:01:20] going to say
[25:01:21] plus space seconds in order to have a
[25:01:24] nice message. So again what we have done
[25:01:26] we have declared the two variables and
[25:01:28] we are using them at the start we are
[25:01:30] getting the current date and time and at
[25:01:33] the end of loading the table we are
[25:01:34] getting the current date and time and
[25:01:36] then we are finding the differences
[25:01:38] between them in order to get the load
[25:01:40] duration and in this case we are just
[25:01:42] printing this information and now we can
[25:01:44] go of course and add some nice separator
[25:01:46] between each table so I'm going to go
[25:01:48] and do it like this just few minuses not
[25:01:51] a lot of stuff so now what we have to do
[25:01:53] is to go and add this mechanism for each
[25:01:56] table in order to measure the speed of
[25:01:59] the ETL for each one of
[25:02:03] [Music]
[25:02:05] them. Okay. So now I have added all
[25:02:08] those configurations for each table and
[25:02:11] let's go and run the whole thing now. So
[25:02:14] let's go and edit the store procedure
[25:02:16] this and we're going to go and run it.
[25:02:19] So let's go and execute. So now as you
[25:02:21] can see we have here one more info about
[25:02:23] the load durations and it is everywhere
[25:02:25] I can see we have zero seconds and
[25:02:28] that's because it is super fast of
[25:02:30] loading those informations we are doing
[25:02:32] everything locally at PC so loading the
[25:02:34] data from files to database going to be
[25:02:36] mega fast but of course in real projects
[25:02:38] you have like different servers and
[25:02:40] networking between them and you have
[25:02:42] millions of rows in the tables of course
[25:02:44] the duration going to be not like 0
[25:02:46] seconds things going to be slower and
[25:02:48] now you can see easily how long it takes
[25:02:51] to load each of your tables. And now of
[25:02:53] course what is very interesting is to
[25:02:55] understand how long it takes to load the
[25:02:58] whole bronze layer. So now your task is
[25:03:00] as well to print at the end informations
[25:03:03] about the whole patch. How long it took
[25:03:05] to load the bronze
[25:03:07] [Music]
[25:03:11] layer. Okay, I hope we are done. Now I
[25:03:14] have done it like this. We have to
[25:03:16] define two new variables. So the start
[25:03:19] time of the batch and the end time of
[25:03:21] the batch. And the first step in the
[25:03:23] start procedure is to get the date and
[25:03:25] time informations for the first
[25:03:26] variable. And exactly at the end the
[25:03:29] last thing that we do in the start
[25:03:31] procedure, we're going to go and get the
[25:03:33] date and time informations for the end
[25:03:36] time. So we say again set get date for
[25:03:38] the patch and time. And then all what we
[25:03:40] have to do is to go and print a message.
[25:03:42] So we are saying loading bronze layer is
[25:03:45] completed and then we are printing total
[25:03:47] load duration and the same thing with a
[25:03:49] date difference between the patch start
[25:03:51] time and the end time and we are
[25:03:53] calculating the seconds and so on. So
[25:03:55] now what we have to do is to go and
[25:03:56] execute the whole thing. So let's go and
[25:03:59] refresh the definition of the start
[25:04:01] procedure and then let's go and execute
[25:04:03] it. So in the output we have to go to
[25:04:05] the last message and we can see loading
[25:04:08] bronze layer is completed and the total
[25:04:10] load duration is as well 0 seconds
[25:04:13] because the execution time is less than
[25:04:15] 1 second. So with that you are getting
[25:04:17] now a feeling about how to build an ETL
[25:04:19] process. So as you can see the data
[25:04:21] engineering is not all about how to load
[25:04:24] the data. It's how to engineer the whole
[25:04:26] pipeline. how to measure the speed of
[25:04:28] loading the data. What can happen if
[25:04:30] there is like an error and to print each
[25:04:32] step in your ETL process and make
[25:04:35] everything organized and cleared in the
[25:04:37] output and maybe in the logging just to
[25:04:39] make debugging and optimizing the
[25:04:41] performance way easier. And there's like
[25:04:43] a lot of things that we can add. We can
[25:04:45] add the quality measures and stuff. So
[25:04:48] we can add many stuff to our ETL script
[25:04:50] to make our data warehouse professional.
[25:04:53] All right, my friends. So with that we
[25:04:54] have developed a code in order to load
[25:04:56] the bronze layer and we have tested that
[25:04:58] as well. And now in the next step we're
[25:05:00] going to go back to draw because we want
[25:05:02] to draw a diagram about the data flow.
[25:05:04] So let's
[25:05:08] go. So now what is a data flow diagram?
[25:05:11] We're going to draw a simple visual in
[25:05:12] order to map the flow of your data where
[25:05:15] it come from and where it ends up. So we
[25:05:18] want just to make clear how the data
[25:05:20] flows through different layers of your
[25:05:22] projects. And that's help us to create
[25:05:24] something called the data lineage. And
[25:05:26] this is really nice especially if you
[25:05:28] are analyzing an issue. So if you have
[25:05:30] like multiple layers and you don't have
[25:05:32] a real data lineage or flow, it's going
[25:05:34] to be really hard to analyze the scripts
[25:05:36] in order to understand the origin of the
[25:05:39] data and having this diagram going to
[25:05:41] improve the process of finding issues.
[25:05:43] So now let's go and create one. Okay. So
[25:05:45] now back to draw and we're going to go
[25:05:47] and build the flow diagram. So we're
[25:05:48] going to start first with the source
[25:05:51] system. So, let's build the layer. I'm
[25:05:53] going to go and remove the fill dot it.
[25:05:55] And then we're going to go and add like
[25:05:57] a box saying sources and we're going to
[25:06:00] put it over here. Increase the size 24
[25:06:04] and as well without any lines. Now, what
[25:06:07] do we have inside the sources? We have
[25:06:09] like folder and files. So, let's go and
[25:06:12] search for a folder icon. I'm going to
[25:06:14] go and take this one over here and say
[25:06:16] you are the CRM. And we can as well
[25:06:18] increase the size. And we have another
[25:06:20] source. We have the
[25:06:23] ERP. Okay. So, this is the first layer.
[25:06:26] Let's go and now have the bronze layer.
[25:06:29] So, we're going to go and grab another
[25:06:31] box. And we're going to go and make the
[25:06:33] coloring like this. And instead of auto,
[25:06:35] maybe take the hatch, maybe something
[25:06:37] like this, whatever, you know. So,
[25:06:40] rounded. And then we can go and put on
[25:06:42] top of it like the title. So, we can say
[25:06:46] you are the bronze layer. and increase
[25:06:49] as well the size of the font. So now
[25:06:51] what we're going to do, we're going to
[25:06:52] go and add boxes for each table that we
[25:06:55] have in the bronze layer. So for
[25:06:56] example, we have the sales details. We
[25:06:58] can go and make it a little bit smaller.
[25:07:00] So maybe 16 and not bold. And we have
[25:07:04] other two tables from the CRM. We have
[25:07:07] the customer info and as well the
[25:07:10] product info. So those are the three
[25:07:12] tables that comes from the CRM. And now
[25:07:15] what we're going to do, we're going to
[25:07:16] go and connect now the source CRM with
[25:07:20] those three tables. So what we're going
[25:07:21] to do, we're going to go to the folder
[25:07:22] and start making arrows from the folder
[25:07:26] to the bronze layer like this. And now
[25:07:28] we have to do the same thing for the ERP
[25:07:31] source. So as you can see the data flow
[25:07:33] diagram shows us in one picture the data
[25:07:36] lineage between the two layers. So here
[25:07:38] we can see easily those three tables
[25:07:40] actually comes from the CRM and as well
[25:07:42] those three tables in the bronze layer
[25:07:44] are coming from the ERP. I understand if
[25:07:46] we have like a lot of tables it's going
[25:07:48] to be a huge mess. But if you have like
[25:07:50] small or medium data warehouse building
[25:07:53] those diagrams going to make things
[25:07:54] really easier to understand how
[25:07:56] everything is flowing from the sources
[25:07:59] into the different layers in your data
[25:08:01] warehouse. All right. So with that we
[25:08:03] have the first version of the data flow.
[25:08:05] So this step is done and the final step
[25:08:07] is to commit our code in the get
[25:08:13] repo. Okay. So now let's go and commit
[25:08:15] our work. Since it is scripts, we're
[25:08:17] going to go to the folder scripts. And
[25:08:18] here we're going to have like script for
[25:08:20] the bronze, silver, and gold. That's why
[25:08:22] maybe it makes sense to create a folder
[25:08:24] for each layer. So let's go and start
[25:08:26] creating the bronze folder. So I'm going
[25:08:28] to go and create a new file. And then
[25:08:30] I'm going to say bronze slash. And then
[25:08:32] we can have the DDL script of the bronze
[25:08:35] layer SQL. So now I'm going to go and
[25:08:37] paste the DDL codes that we have
[25:08:39] created. So those six tables and as
[25:08:42] usual at the start we have a comment
[25:08:44] where we are explaining the purpose of
[25:08:45] this script. So we are saying this
[25:08:47] scripts creates tables in the bronze
[25:08:49] schema. And by running this scripts you
[25:08:51] are redefining the DDL structure of the
[25:08:53] bronze tables. So let's have it like
[25:08:55] that. And I'm going to go and commit the
[25:08:58] changes. All right. So now as you can
[25:09:00] see inside the scripts we have a folder
[25:09:02] called bronze and inside it we have the
[25:09:05] DDL script for the bronze layer and as
[25:09:07] well in the bronze layer we're going to
[25:09:08] go and put our start procedure. So we're
[25:09:10] going to go and create a new file let's
[25:09:12] call it proc load bronze dossql and then
[25:09:17] let's go and paste our script and as
[25:09:19] usual I have put it at the start an
[25:09:21] explanation about the store procedure.
[25:09:23] So we are saying this third procedure
[25:09:25] going to go and load the data from the
[25:09:26] CSV files into the bronze schema. So it
[25:09:29] going to go and truncate first the
[25:09:31] tables and then do a bulk insert. And
[25:09:33] about the parameters, this source
[25:09:35] procedure does not accept any parameter
[25:09:37] or return any values. And here a quick
[25:09:39] example how to execute it. All right. So
[25:09:41] I think I'm happy with that. So let's go
[25:09:44] and commit it. All right. My friends, so
[25:09:47] with that we have committed our code
[25:09:49] into the g. And with that we are done
[25:09:51] building the bronze layer. So the whole
[25:09:53] op is done. Now we're going to go to the
[25:09:55] next one. This one going to be more
[25:09:57] advanced than the bronze layer because
[25:09:59] there will be a lot of struggle with
[25:10:01] cleaning the data and so on. So we're
[25:10:02] going to start with the first task where
[25:10:04] we're going to analyze and explore the
[25:10:06] data in the source systems. So let's
[25:10:11] go. Okay. So now we're going to start
[25:10:13] with the big question. How to build the
[25:10:15] server layer? What is the process? Okay.
[25:10:17] As usual, first things first, we have to
[25:10:19] analyze. And now the task before
[25:10:21] building anything in the server layer we
[25:10:23] have to go and explore the data in order
[25:10:26] to understand the content of our sources
[25:10:28] once we have it what we're going to do
[25:10:29] we will be starting coding and here the
[25:10:32] transformation that we're going to do is
[25:10:33] data cleansing this is usually process
[25:10:35] that take really long time and I usually
[25:10:37] do it in three steps the first step is
[25:10:40] to check first the data quality issues
[25:10:42] that we have in the bronze layer so
[25:10:44] before writing any data transformations
[25:10:46] first we have to understand what are the
[25:10:48] issues and only then I start writing
[25:10:50] think data transformations in order to
[25:10:52] fix all those quality issues that we
[25:10:54] have in the bronze and the last step
[25:10:56] once I have clean results what we're
[25:10:58] going to do we're going to go and insert
[25:11:00] it into the server layer and those are
[25:11:02] the three faces that we will be doing as
[25:11:04] we are writing the code for the silver
[25:11:06] layer and the third step once we have
[25:11:07] all the data in the server layer we have
[25:11:09] to make sure that the data is now
[25:11:11] correct and we don't have any quality
[25:11:13] issues anymore and if you find any
[25:11:15] issues of course what you going to do
[25:11:16] we're going to go back to coding we're
[25:11:18] going to do the data cleansing and again
[25:11:19] object. So it is like a cycle between
[25:11:21] validating and coding. Once the quality
[25:11:24] of the silver layer is good, we cannot
[25:11:26] skip the last phase where we're going to
[25:11:28] document and commit our work in the G.
[25:11:30] And here we're going to have two new
[25:11:31] documentations. We're going to build the
[25:11:33] data flow diagram and as well the data
[25:11:36] integration diagram after we understood
[25:11:38] the relationship between the sources
[25:11:40] from the first step. So this is the
[25:11:42] process and this is how we're going to
[25:11:43] build the server layer.
[25:11:49] All right. So now exploring the data in
[25:11:51] the bronze layer. So why it is very
[25:11:52] important? Because understanding the
[25:11:54] data it is the key to make smart
[25:11:56] decisions in the server layer. It was
[25:11:58] not the focus in the bronze layer to
[25:12:00] understand the content of the data at
[25:12:02] all. We focus only how to get the data
[25:12:04] to the data warehouse. So that's why we
[25:12:06] have now to take a moment in order to
[25:12:09] explore and understand the tables and as
[25:12:11] well how to connect them. what are the
[25:12:13] relationship between these tables and it
[25:12:15] is very important as you are learning
[25:12:17] about the new source system is to create
[25:12:19] like some kind of documentation. So now
[25:12:21] let's go and explore the sources. Okay.
[25:12:24] So now let's go and explore them one by
[25:12:26] one. We can start with the first one
[25:12:27] from the CRM. We have the customer info.
[25:12:30] So right click on it and say select top
[25:12:32] thousand rows. And this is of course
[25:12:33] important if you have like a lot of
[25:12:35] data. Don't go and explore millions of
[25:12:37] rows. Always limit your query. So for
[25:12:39] example here we are using the top
[25:12:40] thousands just to make sure that you are
[25:12:42] not impacting the system with your
[25:12:43] queries. So now let's have a look to the
[25:12:45] content of this table. So we can see
[25:12:46] that we have here customer informations.
[25:12:49] So we have an ID, we have a key for the
[25:12:51] customer, we have first name, last name,
[25:12:53] marital status, gender and the creation
[25:12:56] date of the customer. So simply this is
[25:12:58] a table for the customer information and
[25:13:00] a lot of details for the customers. And
[25:13:02] here we have like two identifiers. one
[25:13:05] it is like technical ID and another one
[25:13:07] it's like the customer number so maybe
[25:13:10] we can use either the ID or the key in
[25:13:12] order to join it with other tables so
[25:13:14] now what I usually do is to go and draw
[25:13:16] like data model or let's say integration
[25:13:19] model just to document and visual what I
[25:13:21] am understanding because if you don't do
[25:13:23] that you're going to forget it after a
[25:13:24] while so now we go and search for a
[25:13:26] shape let's search for a table and I'm
[25:13:28] going to go and pick this one over here
[25:13:30] so here we can go and change the style
[25:13:32] for example we can make it rounded or
[25:13:34] you can go make it sketch and so on. And
[25:13:37] we can go and change the color. I'm
[25:13:39] going to make it blue. Then go to the
[25:13:41] text. Make sure to select the whole
[25:13:42] thing. And let's make it bigger. 26. And
[25:13:46] then what I'm going to do for those
[25:13:48] items, I'm just going to select them and
[25:13:50] go to our range and maybe make it 40.
[25:13:53] Something like this. So now what we're
[25:13:55] going to do, we're going to just go and
[25:13:56] put the table name. So this is the one
[25:13:58] that we are now learning about. And what
[25:14:01] I'm going to do, I'm just going to go
[25:14:02] and put here the primary key. I will not
[25:14:04] go and list all the informations. So the
[25:14:06] primary key was the ID. And I will go
[25:14:09] and remove all those stuff. I don't need
[25:14:11] it. Now, as you can see, the table name
[25:14:12] is not really friendly. So I can go and
[25:14:14] bring a text and put it here on top and
[25:14:17] say this is the customer information.
[25:14:20] Just to make it friendly and to not
[25:14:22] forget about it. And as well going to
[25:14:24] increase the size to maybe 20 something
[25:14:27] like this. Okay. With that, we have our
[25:14:29] first table. and we're going to go and
[25:14:31] keep exploring. So let's move to the
[25:14:33] second one. We're going to take the
[25:14:34] product information, right click on it
[25:14:37] and select the top thousand rows. I will
[25:14:39] just put it below the previous query.
[25:14:41] Query it. Now by looking to this table
[25:14:43] we can see we have product informations.
[25:14:46] So we have here a primary key for the
[25:14:48] product and then we have like key or
[25:14:50] let's say product number and after that
[25:14:52] we have the full name of the product the
[25:14:54] product costs and then we have the
[25:14:56] product line and then we have like start
[25:14:58] and end. Well this is interesting to
[25:15:01] understand why we have start and ends.
[25:15:03] Let's have a look for example for those
[25:15:05] three rows all of those three having the
[25:15:08] same key but they have different ids. So
[25:15:10] it is the same product but with
[25:15:13] different costs. So for 2011 we have the
[25:15:16] cost of 12. Then 2012 we have 14 and for
[25:15:20] the last year 2013 we have 13. So it's
[25:15:23] like we have like a history for the
[25:15:25] changes. So this table not only holding
[25:15:27] the current informations of the product
[25:15:30] but also history informations of the
[25:15:32] product and that's why we have those to
[25:15:34] date start and end. Now let's go back
[25:15:36] and draw this information over here. So
[25:15:38] I'm just going to go and duplicate it.
[25:15:40] So the name of this table going to be
[25:15:42] the BRD info and let's go and give it
[25:15:44] like a short description current and
[25:15:47] history products information something
[25:15:51] like this just to not forget that we
[25:15:53] have history in this table and here we
[25:15:54] have as well the PRD ID and there is
[25:15:57] like nothing that we can use in order to
[25:15:59] join those two tables we don't have like
[25:16:02] a customer ID here or in the other table
[25:16:04] we don't have any product ID okay so
[25:16:06] that's it for this table let's jump to
[25:16:07] the third table and the last one in the
[25:16:09] CRM M. So let's go and select. I just
[25:16:12] made the other queries as well short. So
[25:16:14] let's go and execute. So what do we have
[25:16:15] over here? We have a lot of informations
[25:16:17] about the order, the sales and a lot of
[25:16:20] measures. Order number. We have the
[25:16:22] product key. So this is something that
[25:16:24] we can use in order to join it with the
[25:16:26] product table. We have the customer ID.
[25:16:28] We don't have the customer key. So here
[25:16:30] we have like ID and here we have key. So
[25:16:33] there's like two different ways on how
[25:16:35] to join tables. And then we have here
[25:16:37] like dates. the order date, the shipping
[25:16:40] date, the due date and then we have the
[25:16:42] sales amount, the quantity and the
[25:16:44] price. So this is like an event table.
[25:16:46] It is transactional table about the
[25:16:48] orders and sales and it is great table
[25:16:51] in order to connect the customers with
[25:16:53] the products and as well with the
[25:16:55] orders. So let's document this new
[25:16:57] information that we have. So the table
[25:17:00] name is the sales details. So we can go
[25:17:02] and describe it like this.
[25:17:05] Transactional records about sales and
[25:17:10] orders. And now we have to go and
[25:17:12] describe how we can connect this table
[25:17:14] to the other two. So we are not using
[25:17:16] the product ID. We are using the
[25:17:18] products key. And now we need a new
[25:17:21] column over here. So you can hold
[25:17:22] control and enter or you can go over
[25:17:24] here and add a new row. And the other
[25:17:27] row going to be the customer ID. So now
[25:17:29] for the customer ID it is easy. we can
[25:17:31] go and grab an arrow in order to connect
[25:17:33] those two tables. But for the product
[25:17:34] key, we are not using the ID. So that's
[25:17:37] why I'm just going to go and remove this
[25:17:39] one and say product key. Let's have
[25:17:41] again a check. So this is a product key.
[25:17:44] It's not the product ID. And if we go
[25:17:46] and check the old table, the products
[25:17:47] info, you can see we are using this key
[25:17:50] and not the primary key. So what we're
[25:17:52] going to do now, we will just go and
[25:17:54] link it like this. And maybe switch
[25:17:56] those two tables. So I will put the
[25:17:58] customers below. Just perfect. It looks
[25:18:01] nice. Okay. So, let's keep moving. Let's
[25:18:03] go now to the other source system. We
[25:18:05] have the ARP and the first one is ARB
[25:18:07] cost and we have this cryptical name.
[25:18:10] Let's go and select the data. So, now
[25:18:12] here it's small table and we have only
[25:18:14] three informations. So, we have here
[25:18:16] something called CD and then we have
[25:18:18] something I think this is the birthday
[25:18:20] and the gender information. So, we have
[25:18:22] here male, female and so on. So, it
[25:18:24] looks again like the customer
[25:18:26] informations but here we have like extra
[25:18:27] data about the birthday. And now if you
[25:18:30] go and compare it to the customer table
[25:18:32] that we have from the other source
[25:18:33] system. Let's go and query it. You can
[25:18:35] see the new table from the ARB don't
[25:18:37] have ids. It has actually the customer
[25:18:41] number or the key. So we can go and join
[25:18:43] those two tables using the customer key.
[25:18:46] Let's go and document this information.
[25:18:48] So I will just go and copy paste and put
[25:18:50] it here on the right side. I will just
[25:18:51] go and change the color now since we are
[25:18:53] now talking about different source
[25:18:55] system. And here the table name going to
[25:18:57] be this one. and the key called C ID.
[25:19:01] Now, in order to join this table with
[25:19:03] the customer info, we cannot join it
[25:19:05] with the customer ID. We need the
[25:19:06] customer key. That's why here we have to
[25:19:08] go and add a new row. So, ctrl enter and
[25:19:10] we're going to say customer key. And
[25:19:13] then we have to go and make a nice arrow
[25:19:15] between those two keys. So, we're going
[25:19:17] to go and give it a description,
[25:19:20] customer information. And here we have
[25:19:23] the birth date. Okay. So, now let's keep
[25:19:26] going. We're going to go to the next
[25:19:28] one. We have the ERP location. Let's go
[25:19:32] and query this table. So, what do we
[25:19:34] have over here? We have the CD again.
[25:19:36] And as you can see, we have country
[25:19:38] informations. And this is of course
[25:19:40] again the customer number. And we have
[25:19:42] only this information, the country. So,
[25:19:44] let's go and document this information.
[25:19:46] This is the customer location. Table
[25:19:48] name going to be like this. And we still
[25:19:50] have the same ID. So, we have here still
[25:19:53] the customer ID and we can go and join
[25:19:55] it using the customer key. And we have
[25:19:57] to give it the description location of
[25:20:00] customers and we can say here the
[25:20:02] country. Okay. So now let's go to the
[25:20:05] last table and explore it. We have the
[25:20:07] ERP ex catalog. So let's go and query
[25:20:12] those informations. So what do we have
[25:20:14] here? We have like an ID, a category, a
[25:20:17] subcategory and the maintenance. Here we
[25:20:19] have like either yes and no. So by
[25:20:21] looking to this table we have all the
[25:20:23] categories and the subcategories of the
[25:20:26] products and here we have like special
[25:20:28] identifier for those informations. Now
[25:20:30] the question is how to join it. So I
[25:20:33] would like to join it actually with the
[25:20:34] product informations. So let's go and
[25:20:36] check those two tables together. Okay.
[25:20:38] So in the product we don't have any ID
[25:20:40] for the categories but we have these
[25:20:42] informations actually in the product
[25:20:45] key. So the first five characters of the
[25:20:47] product key is actually the category ID.
[25:20:50] So we can use this information over here
[25:20:53] in order to join it with the categories.
[25:20:55] So we can go and describe this
[25:20:57] information like this and then we have
[25:20:59] to go and give it a name. And then here
[25:21:01] we have the ID and the ID could be
[25:21:03] joined using the product key. So that
[25:21:06] means for the product information we
[25:21:08] don't need at all the product ID the
[25:21:10] primary key. All what we need is the
[25:21:13] product key or the product number. And
[25:21:15] what I would like to do is like to group
[25:21:16] those informations in a box. So, let's
[25:21:20] go grab like any boxes here on the left
[25:21:22] side and make it bigger and then make
[25:21:26] the edges a little bit smaller. Let's
[25:21:28] remove the fill and the line. I will
[25:21:31] make a dotted line. And then let's grab
[25:21:33] another box over here and say this is
[25:21:36] the CRM. And we can go and increase the
[25:21:38] size maybe something like 40 smaller 35
[25:21:42] bold and change the color to blue and
[25:21:44] just place it here on top of this box.
[25:21:46] So with that we can understand all those
[25:21:48] tables belongs to the source system CRM
[25:21:51] and we can do the same stuff for the
[25:21:53] right side as well. Now of course we
[25:21:55] have to go and add the description here.
[25:21:58] So it's going to be the products
[25:22:00] categories. All right. So with that we
[25:22:02] have now a clear understanding how the
[25:22:04] tables are connected to each others. We
[25:22:07] understand now the content of each table
[25:22:09] and of course it can help us to clean up
[25:22:11] the data in the silver layer in order to
[25:22:13] prepare it. So as you can see it is very
[25:22:16] important to take time understanding the
[25:22:18] structure of the tables the relationship
[25:22:20] between them before start writing any
[25:22:22] code. All right. So with that we have
[25:22:24] now clear understanding about the
[25:22:25] sources and with that we have as well
[25:22:27] created a data integration in the draw.
[25:22:30] So with that we have more understanding
[25:22:32] about how to connect the sources. And
[25:22:34] now in the next two task we will go back
[25:22:36] to SQL where we're going to start
[25:22:38] checking the quality and as well doing a
[25:22:40] lot of data transformations. So let's
[25:22:42] go.
[25:22:46] Okay, so now let's have a quick look to
[25:22:48] the specifications of the server layer.
[25:22:50] So the main objective to have clean and
[25:22:52] standardized data. We have to prepare
[25:22:54] the data before going to the gold layer.
[25:22:57] And we will be building tables inside
[25:22:59] the silver layer. And the way of loading
[25:23:01] the data from the bronze to the silver
[25:23:03] is a full load. So that means we're
[25:23:05] going to truncate and then insert. And
[25:23:06] here we're going to have a lot of data
[25:23:08] transformations. So we're going to clean
[25:23:09] the data. We're going to bring
[25:23:11] normalizations, standardizations. We're
[25:23:13] going to derive new columns. We will be
[25:23:15] doing as well data enrichments. So a lot
[25:23:18] of things to be done in the data
[25:23:19] transformation. But we will not be
[25:23:21] building any new data model. So those
[25:23:23] are the specifications and we have to
[25:23:25] commit ourself to this scope. Okay. So
[25:23:27] now building the DDL script for the
[25:23:28] silver layer going to be way easier than
[25:23:31] the bronze because the definition and
[25:23:33] the structure of each table in the
[25:23:34] silver going to be identical to the
[25:23:36] bronze layer. We are not doing anything
[25:23:39] new. So all what you have to do is to
[25:23:40] take the DDL script from the bronze
[25:23:42] layer and just go and search and replace
[25:23:45] for the schema. I'm just using the
[25:23:46] Notepad++ for the scripts. So I'm going
[25:23:49] to go over here and say replace the
[25:23:51] bronze dots with silver dots and I'm
[25:23:53] going to go and replace all. So with
[25:23:55] that now all the DDL is targeting the
[25:23:58] schema silver layer which is exactly
[25:24:01] what we need. All right. Now before we
[25:24:03] execute our new DDL script for the
[25:24:05] silver, we have to talk about something
[25:24:07] called the metadata columns. They are
[25:24:09] additional columns or fields that the
[25:24:11] data engineers add to each table that
[25:24:14] don't come directly from the source
[25:24:16] systems. But the data engineers use it
[25:24:18] in order to provide extra informations
[25:24:21] for each record. Like we can add a
[25:24:23] column called create date is when the
[25:24:25] record was loaded or an update date when
[25:24:28] the record got updated or we can add the
[25:24:31] source system in order to understand the
[25:24:33] origin of the data that we have or
[25:24:36] sometimes we can add the file location
[25:24:38] in order to understand the lineage from
[25:24:40] which file the data come from. Those are
[25:24:43] great tool if you have data issue in
[25:24:45] your data warehouse if there is like
[25:24:46] corrupt data and so on. This can help
[25:24:49] you to track exactly where this issue
[25:24:52] happens and when. And as well it is
[25:24:54] great in order to understand whether I
[25:24:56] have gap in my data especially if you
[25:24:57] are doing incremental loads. It is like
[25:24:59] putting labels on everything and you
[25:25:02] will thank yourself later when you start
[25:25:04] using them in hard times as you have an
[25:25:06] issue in your data warehouse. So now
[25:25:08] back to our DDL scripts and all what you
[25:25:10] have to do is to go and do the
[25:25:11] following. So for example for the first
[25:25:13] table I will go and add at the end one
[25:25:16] more extra column. So it start with the
[25:25:18] prefix TWW as we have defined in the
[25:25:21] naming convention and then underscore
[25:25:23] let's have the create date and the data
[25:25:26] type going to be date time 2 and now
[25:25:28] what we can do is we can go and add a
[25:25:30] default value for it. I want the
[25:25:32] database to generate these informations
[25:25:34] automatically. We don't have to specify
[25:25:36] that in any scripts. So which value?
[25:25:38] It's going to be the get date. So each
[25:25:41] record going to be inserted in this
[25:25:42] table will get automatically a value
[25:25:45] from the current date and time. So now
[25:25:47] as you can see the naming convention it
[25:25:49] is very important. All those columns
[25:25:51] comes from the source system and only
[25:25:53] this one column comes from the data
[25:25:55] engineer of the data warehouse. Okay. So
[25:25:57] that's it. Let's go and repeat the same
[25:25:59] thing for all other tables. So I will
[25:26:01] just go and add this piece of
[25:26:03] information for each
[25:26:06] DDL. All right. So I think that's it.
[25:26:09] All what you have to do is now to go and
[25:26:11] execute the whole DDL script for the
[25:26:13] silver layer. Let's go and do that. All
[25:26:15] right, perfect. There's no errors. Let's
[25:26:17] go and refresh the tables on the object
[25:26:19] explorer. And with that, as you can see,
[25:26:21] we have six tables for the silver layer.
[25:26:23] It is identical to the bronze layer, but
[25:26:25] we have one extra column for the
[25:26:31] metadata. All right. All right. So now
[25:26:33] in the server layer before we start
[25:26:35] writing any data transformations and
[25:26:37] cleansing we have first to detect the
[25:26:39] quality issues in the bronze without
[25:26:41] knowing the issues we cannot find
[25:26:43] solution right we will explore first the
[25:26:45] quality issues only then we start
[25:26:47] writing the transformation scripts. So
[25:26:50] let's
[25:26:56] go. Okay. Okay. So now what we're going
[25:26:58] to do, we're going to go through all the
[25:26:59] tables over the bronze layer, clean up
[25:27:01] the data, and then insert it to the
[25:27:04] server layer. So let's start with the
[25:27:05] first table, the first bronze table from
[25:27:08] the source CRM. So we're going to go to
[25:27:09] the bronze CRM customer info. So let's
[25:27:13] go and query the data over here. Now, of
[25:27:15] course, before writing any data
[25:27:17] transformations, we have to go and
[25:27:19] detect and identify the quality issues
[25:27:22] of this table. So usually I start with
[25:27:24] the first check where we go and check
[25:27:26] the primary key. So we have to go and
[25:27:28] check whether there are nulls inside the
[25:27:30] primary key and whether there are
[25:27:31] duplicates. So now in order to detect
[25:27:34] the duplicates in the primary key what
[25:27:35] we have to do is to go and aggregate the
[25:27:38] primary key. If we find any value in the
[25:27:40] primary key that exist more than once
[25:27:42] that means it is not unique and we have
[25:27:44] duplicates in the table. So let's go and
[25:27:46] write query for that. So what we're
[25:27:48] going to do, we're going to go with the
[25:27:50] customer ID and then we're going to go
[25:27:51] and count and then we have to group up
[25:27:54] the data. So group by based on the
[25:27:56] primary key and of course we don't need
[25:27:58] all the results. We need only where we
[25:28:00] have an issue. So we're going to say
[25:28:02] having
[25:28:04] count higher than one. So we are
[25:28:06] interested in the values where the count
[25:28:09] is higher than one. So let's go and
[25:28:11] execute it. Now as you can see we have
[25:28:13] issue in this table. we have duplicates
[25:28:15] because all those ids exist more than
[25:28:18] one in the table which is completely
[25:28:20] wrong. We should have the primary key
[25:28:21] unique and you can see as well we have
[25:28:23] three records where the primary key is
[25:28:25] empty which is as well a bad thing. Now
[25:28:28] there is an issue here. If we have only
[25:28:30] one null it will not be here at the
[25:28:32] result. So what I'm going to do I'm
[25:28:34] going to go over here and say or the
[25:28:36] primary key is null just in case if we
[25:28:39] have only one null I'm still interested
[25:28:41] to see the results. So if I go and run
[25:28:44] it again, we'll get the same results. So
[25:28:46] this is equality check that you can do
[25:28:47] on the table. And as you can see, it is
[25:28:49] not meeting the expectation. So that
[25:28:51] means we have to do something about it.
[25:28:53] So let's go and create a new query. So
[25:28:56] here what we're going to do, we can
[25:28:57] start writing the query that is doing
[25:28:59] the data transformation and the data
[25:29:01] cleansing. So let's start again by
[25:29:03] selecting the
[25:29:07] data and execute it again. So now what I
[25:29:10] usually do I go and focus on the issue.
[25:29:13] So for example let's go and take one of
[25:29:15] those values and I focus on it before
[25:29:17] start writing the transformation. So
[25:29:19] we're going to say where customer ID
[25:29:21] equal to this value. All right. So now
[25:29:24] as you can see we have here the issue
[25:29:25] where the ID exist three times but
[25:29:28] actually we are interested only on one
[25:29:30] of them. So the question is how to pick
[25:29:32] one of those. Usually we search for a
[25:29:34] time stamp or date value to help us. So
[25:29:36] if you check the creation date over here
[25:29:38] we can understand that this record this
[25:29:41] one over here is the newest one and the
[25:29:44] previous two are older than it. So that
[25:29:46] means if I have to go and pick one of
[25:29:48] those values I would like to get the
[25:29:50] latest one because it holds the most
[25:29:53] fresh information. So what we have to do
[25:29:55] is we have to go and rank all those
[25:29:57] values based on the create dates and
[25:30:00] only pick the highest one. So that means
[25:30:03] we need a racking function and for that
[25:30:05] in scale we have the amazing window
[25:30:07] functions. So let's go and do that. We
[25:30:10] will use the function row number over
[25:30:14] and then partition by and here we have
[25:30:17] to divide the table by the customer ID.
[25:30:20] So we're going to divide it by the
[25:30:22] customer ID and in order now to rank
[25:30:24] those rows we have to sort the data by
[25:30:26] something. So order by and as we
[25:30:28] discussed we want to sort the data by
[25:30:30] the creation date. So create
[25:30:33] date and we're going to sort it
[25:30:35] descending. So the highest first then
[25:30:37] the lowest. So let's go and do that. And
[25:30:39] now we're going to go and give it a name
[25:30:41] flag last. So now let's go and execute
[25:30:44] it. Now the data is sorted by the
[25:30:46] creation date. And you can see over here
[25:30:48] that this record is the number one. Then
[25:30:51] the one that is older is two and the
[25:30:53] oldest one is three. Of course we are
[25:30:55] interested in the rank number one. Now
[25:30:57] let's go and remove the filter and check
[25:30:59] everything. So now if you have a look to
[25:31:01] the table you can see that on the flag
[25:31:03] we have everywhere like one and that's
[25:31:06] because the those primary keys exist
[25:31:08] only one but sometimes we will not have
[25:31:10] one we'll have two three and so on. If
[25:31:12] there's like duplicates we can go of
[25:31:13] course and do a double check. So let's
[25:31:15] go over here and say select
[25:31:18] star from this query we can say where
[25:31:21] flag last is in equal to one. So let's
[25:31:24] go and query it. And now we can see all
[25:31:26] the data that we don't need because they
[25:31:28] are causing duplicates in the primary
[25:31:30] key and they have like an old status. So
[25:31:32] what we're going to do we're going to
[25:31:34] say equal to one. And with that we
[25:31:36] guarantee that our primary key is unique
[25:31:38] and each value exist only once. So if I
[25:31:40] go and query it like this you will see
[25:31:42] we will not find any duplicate inside
[25:31:44] our table. And we can go and check that
[25:31:46] of course. So let's go and check this
[25:31:48] primary key. And we're going to say and
[25:31:50] customer ID equal to this value. And you
[25:31:53] can see it exists now only once and we
[25:31:56] are getting the freshest data from this
[25:31:58] primary key. So with that we have
[25:31:59] defined like transformation in order to
[25:32:02] remove any duplicates. Okay. So now
[25:32:04] moving on to the next one. As you can
[25:32:06] see in our table we have a lot of values
[25:32:08] where they are like string values. Now
[25:32:11] for these string values we have to check
[25:32:13] the unwanted spaces. So now let's go and
[25:32:16] write a query that's going to detect
[25:32:17] those unwanted spaces. So we're going to
[25:32:20] say select this column the first name
[25:32:24] from our table bronze customer
[25:32:27] information. So let's go and query it.
[25:32:29] Now by just looking to the data it's
[25:32:31] going to be really hard to find those
[25:32:33] unwanted spaces especially if they are
[25:32:35] at the end of the word. But there is a
[25:32:37] very easy way in order to detect those
[25:32:39] issues. So what we're going to do we're
[25:32:41] going to do a filter. So now we're going
[25:32:42] to say the first name is not equal to
[25:32:46] the first name after trimming the
[25:32:48] values. So if you use the function trim,
[25:32:50] what it going to do? It's going to go
[25:32:51] and remove all the leading and trailing
[25:32:54] spaces. So the first name. So if this
[25:32:57] value is not equal to the first name
[25:33:00] after trimming it, then we have an
[25:33:02] issue. So it is very simple. Let's go
[25:33:04] and execute it. So now in the result, we
[25:33:06] will get a list of all first names where
[25:33:08] we have spaces either at the start or at
[25:33:11] the end. So again the expectation here
[25:33:15] is no results. And the same thing we can
[25:33:18] go and check something else like for
[25:33:19] example the last name. So let's go and
[25:33:22] do that over here and here. Let's go and
[25:33:25] execute it. We see in the results we
[25:33:27] have as well 17 customers where they
[25:33:30] have like space in their last name which
[25:33:32] is not really good. And we can go and
[25:33:34] keep checking all the string values that
[25:33:37] we have inside the table. So for example
[25:33:38] the gender. So let's go and check
[25:33:41] that and execute. Now as you can see we
[25:33:44] don't have any results. That means the
[25:33:46] quality of the gender is better and we
[25:33:48] don't have any unwanted spaces. So now
[25:33:51] we have to go and write transformation
[25:33:53] in order to clean up those two columns.
[25:33:55] Now what I'm going to do, I'm just going
[25:33:56] to go and list all the columns in the
[25:33:58] query instead of the star. All right. So
[25:34:01] now I have a list of all the columns
[25:34:02] that I need. And now what we have to do
[25:34:04] is to go to those two columns and start
[25:34:06] removing the unwanted spaces. So we will
[25:34:09] just use the trim. It's very
[25:34:11] simple. And give it a name, of course,
[25:34:14] the same name. And we will trim as well
[25:34:17] the last name. So let's go and query
[25:34:19] this. And with that we have cleaned up
[25:34:21] those two columns from any unwanted
[25:34:23] spaces. Okay. So now moving on we have
[25:34:25] those two informations. We have the
[25:34:27] maritalial status and as well the
[25:34:29] gender. If you check the values inside
[25:34:31] those two columns as you can see we have
[25:34:33] here low cardality. So we have limited
[25:34:35] numbers of possible values that is used
[25:34:37] inside those two columns. So what we
[25:34:40] usually do is to go and check the data
[25:34:42] consistency inside those two columns. So
[25:34:45] it's very simple what we're going to do.
[25:34:47] We're going to do the following. We're
[25:34:48] going to say
[25:34:50] distinct and we're going to check the
[25:34:52] values. Let's go and do that. And now as
[25:34:54] you can see we have only three possible
[25:34:56] values either null, f or m which is
[25:34:59] okay. We can stay like this of course.
[25:35:01] But we can make a rule in our project
[25:35:03] where we can say we will not be working
[25:35:05] with data abbreviations. We will go and
[25:35:07] use only friendly full names. So instead
[25:35:10] of having an F, we're going to have like
[25:35:12] a full word female. And instead of m
[25:35:15] we're going to have like male and we
[25:35:16] make it as a rule for the whole project.
[25:35:18] So each time we find the gender
[25:35:20] informations we try to give the full
[25:35:22] name of it. So let's go and map those
[25:35:24] two values to a friendly one. So we're
[25:35:26] going to go to the gender over here and
[25:35:28] say case when and we're going to say the
[25:35:31] gender is equal to f then make it a
[25:35:36] female. And when it
[25:35:39] is equal to
[25:35:41] m then map it to male. And now we have
[25:35:46] to make decision about the nulls. As you
[25:35:48] can see over here we have nulls. So do
[25:35:50] we want to leave it as a null or we want
[25:35:52] to use always the value unknown. So with
[25:35:55] that we are replacing the missing values
[25:35:58] with a standard default value or you can
[25:36:00] leave it as null. But let's say in our
[25:36:02] project that we are replacing all the
[25:36:04] missing value with a default value. So
[25:36:06] let's go and do that. We're going to say
[25:36:09] else I'm going to go with the NA not
[25:36:12] available or you can go with the unknown
[25:36:14] of course. So that's for the gender
[25:36:16] information like this. And we can go and
[25:36:19] remove the old one. And now there is one
[25:36:21] thing that I usually do in this case
[25:36:22] where sometimes what happens currently
[25:36:24] we are getting the capital F and the
[25:36:26] capital M but maybe in the time
[25:36:28] something change and you will get like
[25:36:30] lower M and lower F. So just to make
[25:36:32] sure in those cases we still are able to
[25:36:34] map those values to the correct value.
[25:36:37] What we're going to do we're going to
[25:36:38] just use the function upper just to make
[25:36:41] sure that if you get any lowerase values
[25:36:44] we are able to catch it. So the same
[25:36:47] thing over here as well. And now one
[25:36:49] more thing that you can add as well. Of
[25:36:51] course, if you are not trusting the data
[25:36:53] because we saw some unwanted spaces in
[25:36:55] the first name and the last name, you
[25:36:57] might not trust that in the future. You
[25:36:58] will get here as well unwanted spaces.
[25:37:01] You can go and make sure to trim
[25:37:04] everything just to make sure that you
[25:37:07] are catching all those cases. So that's
[25:37:10] it for now. Let's go and execute. Now,
[25:37:12] as you can see, we don't have an M and
[25:37:14] an F. We have a full word, male and
[25:37:17] female. And if we don't have a value, we
[25:37:19] don't have a null, we are getting here
[25:37:21] not available. Now we can go and do the
[25:37:23] same stuff for the maritial status. You
[25:37:25] can see as well we have only three
[25:37:27] possibilities. The s null and an M. We
[25:37:30] can go and do the same stuff. So I will
[25:37:32] just go and copy everything from here.
[25:37:34] And I will go and use the marital status
[25:37:37] and just remove this one from here. And
[25:37:39] now what are the possible values? We
[25:37:41] have the S. So it's going to be single.
[25:37:44] We have an M for married. And we have as
[25:37:47] well a null and with that we are getting
[25:37:50] the not available. So with that we are
[25:37:52] making as well data standardizations for
[25:37:54] this column. So let's go and execute it.
[25:37:57] Now as you can see we don't have those
[25:37:59] short values. We have a full friendly
[25:38:01] value for the status and as well for the
[25:38:04] gender. And at the same time we are
[25:38:06] handling the nulls inside those two
[25:38:08] columns. So with that we are done with
[25:38:10] those two columns. And now we can go to
[25:38:11] the last one that create date. For this
[25:38:13] type of informations, we make sure that
[25:38:15] this column is a real date and not as a
[25:38:18] string or varchar. And as we defined it
[25:38:20] in the data type, it is a date which is
[25:38:22] completely correct. So nothing to do
[25:38:25] with this column. And now the next step
[25:38:26] is that we're going to go and write the
[25:38:28] insert statement. So how we going to do
[25:38:30] it? We're going to go to the start over
[25:38:31] here and say insert into
[25:38:36] silverm customer info. Now we have to go
[25:38:38] and specify all the columns that should
[25:38:41] be inserted. So we're going to go and
[25:38:43] type it. So something like this. And
[25:38:45] then we have the query over here. Let's
[25:38:47] go and execute it. So let's do that. So
[25:38:50] with that we have inserted clean data
[25:38:52] inside the silver table. So now what
[25:38:54] we're going to do we're going to go and
[25:38:55] take all the queries that we have used
[25:38:58] in order to check the quality of the
[25:38:59] bronze and let's go and take it to
[25:39:01] another query and instead of having
[25:39:03] bronze we're going to say silver. So
[25:39:05] this is about the primary key. Let's go
[25:39:08] and execute it. Perfect. We don't have
[25:39:10] any results. So we don't have any
[25:39:12] duplicates. The same thing for the next
[25:39:14] one. So the silver and it was for the
[25:39:18] first name. So let's go and check the
[25:39:21] first name and run it. As you can see
[25:39:23] there is no results. It is perfect. We
[25:39:25] don't have any issues. You can of course
[25:39:27] go and check the last
[25:39:30] name and run it again. We don't have any
[25:39:33] results over here. And now we can go and
[25:39:35] check those low cardality columns like
[25:39:38] for
[25:39:39] example the gender. Let's go and execute
[25:39:42] it. So as you can see we have the not
[25:39:43] available or the unknown male and
[25:39:46] female. So perfect and you can go and
[25:39:48] have a final look to the table to the
[25:39:50] silver customer info. Let's go and check
[25:39:53] that. So now we can have a look to all
[25:39:54] those columns. As you can see everything
[25:39:56] looks perfect and you can see it is
[25:39:58] working this metadata information that
[25:40:00] we have added to the table definition.
[25:40:03] Now it says when we have inserted all
[25:40:05] those records to the table which is
[25:40:08] really amazing information to have a
[25:40:09] track and audit. Okay. So now by looking
[25:40:11] to this script we have done different
[25:40:13] types of data transformations. The first
[25:40:15] one is with the first name and the last
[25:40:17] name. Here we have done trimming
[25:40:19] removing unwanted spaces. This is one of
[25:40:22] the types of data cleansing. So we
[25:40:24] remove unnecessary spaces or unwanted
[25:40:26] characters to ensure data consistency.
[25:40:29] Now moving on to the next
[25:40:30] transformation. we have this case when
[25:40:32] so what we have done here is data
[25:40:34] normalization or we call it sometimes
[25:40:37] data standardization so this
[25:40:38] transformation is type of data cleansing
[25:40:41] where we're going to map coded values to
[25:40:43] meaningful user friendly description and
[25:40:46] we have done the same transformation as
[25:40:48] well to the gender another type of
[25:40:50] transformation that we have done as well
[25:40:52] in the same case when is that we have
[25:40:54] handled the missing values so instead of
[25:40:57] nulls we going to have not available so
[25:40:59] handling missing data is as type of data
[25:41:02] cleansing where we are filling the
[25:41:03] blanks by adding for example a default
[25:41:06] value. So instead of having an empty
[25:41:08] string or a null we're going to have a
[25:41:10] default value like the not available or
[25:41:12] unknown. Another type of data and
[25:41:13] transformations that we have done in
[25:41:15] this script is we have removed the
[25:41:17] duplicates. So removing duplicates is as
[25:41:19] well type of data cleansing where we
[25:41:21] ensure only one record for each primary
[25:41:24] key by identifying and retaining only
[25:41:27] the most relevant row to ensure there is
[25:41:30] no duplicates inside our data and as we
[25:41:32] are removing the duplicates of course we
[25:41:34] are doing data filtering. So those are
[25:41:36] the different types of data
[25:41:38] transformations that we have done in
[25:41:40] this script.
[25:41:44] All right, moving on to the second table
[25:41:46] in the bronze layer from the CRM. We
[25:41:48] have the product info. And of course, as
[25:41:50] usual, before we start writing any
[25:41:52] transformations, we have to search for
[25:41:54] data quality issues. And we start with
[25:41:56] the first one, we have to check the
[25:41:57] primary key. So we have to check whether
[25:41:59] we have duplicates or nulls inside this
[25:42:01] key. So what we have to do, we have to
[25:42:03] group up the data by the primary key or
[25:42:05] check whether we have nulls. So let's go
[25:42:07] and execute it. So as you can see,
[25:42:09] everything is safe. We don't have
[25:42:10] duplicates or nulls in the primary key.
[25:42:12] Now moving on to the next one, we have
[25:42:14] the product key. Here we have in this
[25:42:16] column a lot of informations. So now
[25:42:18] what we have to do is to go and split
[25:42:20] this string into two informations. So we
[25:42:22] are deriving new two columns. So now
[25:42:24] let's start with the first one is the
[25:42:26] category ID. The first five characters
[25:42:29] they are actually the category ID and we
[25:42:31] can go and use the substring function in
[25:42:33] order to extract part of a string. It
[25:42:36] needs three arguments. The first one
[25:42:38] going to be the column that we want to
[25:42:40] extract from. And then we have to define
[25:42:43] the position where to extract. And since
[25:42:45] the first part is on the left side, we
[25:42:47] going to start from the first position.
[25:42:49] And then we have to specify the length.
[25:42:51] So how many characters we want to
[25:42:52] extract, we need five characters. So 1 2
[25:42:55] 3 4 5. So that's it for the category ID.
[25:42:59] Category ID. Let's go and execute it.
[25:43:02] Now, as you can see, we have a new
[25:43:03] column called the category ID. and it
[25:43:05] contains the first part of the string
[25:43:07] and in our database from the other
[25:43:09] source system we have as well the
[25:43:11] category ID. Now we can go and double
[25:43:13] check just in order to make sure that we
[25:43:16] can join data together. So we're going
[25:43:17] to go and check the ID from the bronze
[25:43:20] table ERP and this canopy from the
[25:43:24] category. So in this table we have the
[25:43:26] category ids and you can see over here
[25:43:29] those are the ids of the category and in
[25:43:31] the code layer we have to go and join
[25:43:33] those two tables. But here we still have
[25:43:35] an issue. We have here an underscore
[25:43:38] between the category and the
[25:43:39] subcategory. But in our table we have
[25:43:42] actually a minus. So we have to replace
[25:43:45] that with an underscore in order to have
[25:43:47] matching informations between those two
[25:43:49] tables. Otherwise we will not be able to
[25:43:50] join the tables. So we're going to use
[25:43:52] the function
[25:43:53] replace. And what we are replacing? We
[25:43:56] are replacing the minus with an
[25:43:59] underscore something like this. And if
[25:44:01] you go now and execute it, we will get
[25:44:04] an underscore exactly like the other
[25:44:06] table. And of course we can go and check
[25:44:08] whether everything is matching by having
[25:44:10] very simple query where we say this new
[25:44:13] information not in. And then we have
[25:44:17] this nice subquery. So we are trying to
[25:44:19] find any category ID that is not
[25:44:22] available in the second table. So let's
[25:44:24] go and execute it. Now as you can see we
[25:44:26] have only one category that is not
[25:44:28] matching. We are not finding it in this
[25:44:30] table which is maybe correct. So if you
[25:44:32] go over here you will not find this
[25:44:35] category. I just make it a little bit
[25:44:36] bigger. So we are not finding this one
[25:44:38] category from this table which is fine.
[25:44:41] So our check is okay. Okay. So that we
[25:44:43] have the first part. Now we have to go
[25:44:45] and extract the second part and we're
[25:44:47] going to do the same thing. So we're
[25:44:48] going to use the substring and the three
[25:44:50] argument the product key but this time
[25:44:53] we will not start cutting from the first
[25:44:54] position we have to be in the middle. So
[25:44:56] 1 2 3 4 5 6 7. So we start from the
[25:45:01] position number seven. And now we have
[25:45:03] to define the length how many characters
[25:45:06] to be extracted. But if you look over
[25:45:07] here you can see that we have different
[25:45:10] length of the product keys. It is not
[25:45:12] fixed like the category ID. So we cannot
[25:45:15] go and here specify number. We have to
[25:45:17] make something dynamic and there is
[25:45:18] trick in order to do that. We're going
[25:45:20] to go and use the length of the whole
[25:45:22] column. With that we make sure that we
[25:45:24] are always getting enough characters to
[25:45:26] be extracted and we will not be losing
[25:45:28] any informations. So we will make it
[25:45:31] dynamic like this. We will not have it
[25:45:33] as a fixed length and with that we have
[25:45:35] the product key. So let's go and execute
[25:45:38] it. As you can see we are now extracting
[25:45:41] the second part from this string. Now
[25:45:44] why we need the product key? We need it
[25:45:46] in order to join it with another table
[25:45:48] called sales details. So let's go and
[25:45:51] check the sales details. So let me just
[25:45:54] check the column name. It is SLS product
[25:45:57] key. So from bronze
[25:46:01] CRM sales. Let's go and check the data
[25:46:04] over here. And it looks wonderful. So
[25:46:08] actually we can go and join those
[25:46:09] informations together. But of course
[25:46:11] we're going to go and check that. So
[25:46:12] we're going to say where and we're going
[25:46:14] to take our new column and we're going
[25:46:15] to say not in the sub query just to make
[25:46:19] sure that we are not missing anything.
[25:46:21] So let's go and execute. So it looks
[25:46:23] like we have a lot of products that
[25:46:26] don't have any orders. Well, I don't
[25:46:28] have a nice feelings about it. Let's go
[25:46:30] and try something like this one here.
[25:46:33] And we say where sld key like this value
[25:46:39] over here. So I'll just cut the last
[25:46:42] three just to search inside this table.
[25:46:44] So we really don't have such a keys. Let
[25:46:47] me just cut the second one. So let's go
[25:46:50] and search for it. We don't have it as
[25:46:52] well. So anything that starts with the F
[25:46:55] key, we don't have any order with the
[25:46:57] product where it starts with the F key.
[25:46:59] So let's go and remove it. But still we
[25:47:02] are able to join the tables, right? So
[25:47:04] if I go and say in instead of not in. So
[25:47:07] with that you are able to match all
[25:47:09] those products. So that means everything
[25:47:11] is fine. Actually it's just products
[25:47:14] that don't have any orders. So with that
[25:47:16] I'm happy with this transformation. Now
[25:47:19] moving on to the next one. We have here
[25:47:21] the name of the product. We can go and
[25:47:23] check whether there is unwanted spaces.
[25:47:26] So let's go to our quality checks. Make
[25:47:28] sure to use the same table and we're
[25:47:31] going to use the product name and check
[25:47:33] whether we find any unmatching after
[25:47:36] trimming. So let's go and do it. Well,
[25:47:38] it looks really fine. So we don't have
[25:47:40] to trim anything. This column is safe.
[25:47:42] Now moving on to the next one. We have
[25:47:44] the costs. So here we have numbers and
[25:47:47] we have to check the quality of the
[25:47:48] numbers. So what we can do? We can check
[25:47:50] whether we have nulls or negative
[25:47:52] numbers. So negative costs or negative
[25:47:55] prices which is not realistic depend on
[25:47:58] the business of course. So let's say in
[25:47:59] our business we don't have any negative
[25:48:02] costs. So it's going to be like this.
[25:48:04] Let's go and check whether it's
[25:48:06] something less than zero or whether we
[25:48:09] have costs that is null. So let's go and
[25:48:14] check those informations. Well, as you
[25:48:15] can see, we don't have any negative
[25:48:17] values, but we have nulls. So we can go
[25:48:19] and handle that by replacing the null
[25:48:21] with a zero. Of course, if the business
[25:48:24] allow that. So in SQL server, in order
[25:48:26] to replace the null with a zero, we have
[25:48:28] a very nice function called is null. So
[25:48:32] we are saying if it is null then replace
[25:48:35] this value with a zero. It is very
[25:48:37] simple like this and we give it a name
[25:48:40] of course. So let's go and execute it.
[25:48:42] And as you can see we don't have any
[25:48:44] more nulls. We have zero which is better
[25:48:46] for the calculations if you are later
[25:48:48] doing any aggregate functions like the
[25:48:50] average. Now moving on to the next one
[25:48:52] we have the product line. This is again
[25:48:55] abbreviation of something and the
[25:48:57] cardinality is low. So let's go and
[25:48:58] check all possible values inside this
[25:49:01] column. So we're just going to use the
[25:49:03] distinct going to be BRD line. So let's
[25:49:06] go and execute it. And as you can see
[25:49:08] the possible values are null M R ST. And
[25:49:11] again those are abbreviations but in our
[25:49:13] data warehouse we have decided to give
[25:49:16] full nice names. So we have to go and
[25:49:18] replace those codes those abbreviations
[25:49:21] with a friendly value. And of course in
[25:49:23] order to get those informations I
[25:49:24] usually go and ask the expert from the
[25:49:27] source system or an expert from the
[25:49:29] process. So let's start building our
[25:49:31] case win. And then let's use the upper
[25:49:34] and as well the trim just to make sure
[25:49:36] that we are having all the cases. So the
[25:49:39] BRD
[25:49:40] line is equal to so let's start with the
[25:49:44] first value the M. Then we will get the
[25:49:48] friendly value it's going to be
[25:49:50] mountain. then to the next one. So I
[25:49:52] will just copy and paste here. If it is
[25:49:55] an R then it is road and another one for
[25:50:00] let me check what do we have here? We
[25:50:03] have M R and then S. The S stands for
[25:50:06] other sales and we have the T. So let's
[25:50:10] go and get the T. So the T stands for
[25:50:14] touring. We have at the end an else for
[25:50:17] unknown not available. So we don't need
[25:50:20] any nulls. So that's it. And we're going
[25:50:22] to name it as before. So product line.
[25:50:24] So let's remove the old one. And let's
[25:50:27] execute it. And as you can see, we don't
[25:50:30] have here anymore those shortcuts and
[25:50:32] the abbreviations. We have now full
[25:50:34] friendly value. But I will go and have
[25:50:37] here like capital O. It looks nicer. So
[25:50:40] that we have nice friendly value. Now by
[25:50:42] looking to this case when as you can see
[25:50:43] it is always like we are mapping one
[25:50:45] value to another value and we are
[25:50:47] repeating all time upper time upper time
[25:50:50] and so on. We have here a quick form in
[25:50:52] the case when if it is just a simple
[25:50:54] mapping. So the syntax is very simple we
[25:50:57] say case and then we have the column. So
[25:51:00] we are evaluating this value over here
[25:51:02] and then we just say when without the
[25:51:05] equal so if it is an M then make it
[25:51:08] mountain. the same thing for the next
[25:51:10] one and so so with that we have the
[25:51:13] functions only once and we don't have to
[25:51:15] go and keep repeating the same function
[25:51:16] over and over and this one only if you
[25:51:18] are mapping values but if you have
[25:51:20] complex conditions you cannot do it like
[25:51:22] this but for now I'm going to stay with
[25:51:24] the quick form of the case when it looks
[25:51:26] nicer and shorter so let's go and
[25:51:28] execute it we will get the same results
[25:51:30] okay so now back to our table let's go
[25:51:31] to the last two columns we have the
[25:51:33] start and end date so it's like defining
[25:51:36] an interval we have start and end so
[25:51:38] Let's go and check the quality of the
[25:51:39] start and end dates. We're going to go
[25:51:41] and say select star from our bronze
[25:51:45] table. And now we're going to go and
[25:51:47] search it like this. We are searching
[25:51:48] for the end date that is smaller than
[25:51:52] the start. So we are key to start dates.
[25:51:56] So let's go and query this. So you can
[25:51:58] see the start is always like after the
[25:52:01] end which makes no sense at all. So we
[25:52:03] have here data issue with those two
[25:52:05] dates. So now for this kind of data
[25:52:07] transformations what I usually do is I
[25:52:09] go and grab few examples and put it in
[25:52:11] Excel and try to think about how I'm
[25:52:13] going to go and fix it. So here I took
[25:52:15] like two products this one and this one
[25:52:17] over here. And for that we have like
[25:52:18] three rows for each one of them. And we
[25:52:20] have this situation over here. So the
[25:52:22] question now how we going to go and fix
[25:52:24] it? I will go and make like a copy of
[25:52:26] one solution where we're going to say
[25:52:28] it's very simple. Let's go and switch
[25:52:29] the start date with the end date. So if
[25:52:32] I go and grab the end date and put it at
[25:52:35] the start, things going to look way
[25:52:37] nicer, right? So we have the start is
[25:52:39] always younger than the end. But my
[25:52:41] friends, the data now makes no sense
[25:52:43] because we say it start from 2007 and
[25:52:46] ends by 2011 the price was 12. But
[25:52:50] between 2008 and 2012, we have 14. which
[25:52:54] is not really good because if you take
[25:52:55] for example the year 2010 for 2010 it
[25:52:59] was 12 and at the same time 14. So it is
[25:53:02] really bad to have an overlapping
[25:53:04] between those two dates. It should start
[25:53:06] from 2007 and end with 11 and then start
[25:53:09] Feb from 12 and end with something else.
[25:53:12] There should be no overlapping between
[25:53:14] years. So it's not enough to say the
[25:53:17] start should be always smaller than the
[25:53:19] ends but as well the end of the first
[25:53:22] history should be younger than the start
[25:53:25] of the next records. This is as well a
[25:53:27] rule in order to have no overlapping.
[25:53:30] This one has no start but has already an
[25:53:32] end which is not really okay because we
[25:53:35] have always to have a start. Each new
[25:53:37] record in historiizations has to has a
[25:53:40] start. So for this record over here this
[25:53:42] is as well wrong. And of course it is
[25:53:44] okay to have the start without an end.
[25:53:47] So in this scenario it's fine because
[25:53:49] this indicate this is the current
[25:53:52] informations about the costs. So again
[25:53:54] this solution is not working at all. So
[25:53:56] now for the solution two what we can say
[25:53:58] let's go and ignore completely the end
[25:54:01] date and we take only the start date. So
[25:54:03] let's go and paste it over here. But now
[25:54:05] we go and rebuild the end date
[25:54:07] completely from the start date following
[25:54:09] the rules that we have defined. So the
[25:54:11] rule says the end of date of the current
[25:54:14] records comes from the start date from
[25:54:17] the next records. So here this end date
[25:54:20] comes from this value over here from the
[25:54:22] next record. So that means we take the
[25:54:24] next start date and put it at the end
[25:54:27] date for the previous records. So with
[25:54:29] that as you can see it is working the
[25:54:30] end date is higher than the start date.
[25:54:34] And as well we are making sure this date
[25:54:36] is not overlapping with the next record.
[25:54:38] But as well in order to make it way
[25:54:40] nicer we can subtract it with one. So we
[25:54:43] can take the previous day like this. So
[25:54:45] with that we are making sure the end
[25:54:47] date is smaller than the next start. And
[25:54:50] now for the next record this one over
[25:54:53] here the end date going to come from the
[25:54:55] next start date. So we will take this
[25:54:57] one for here and put it as an end date
[25:54:59] and subtract it with one. So we will get
[25:55:02] the previous day. So now if you compare
[25:55:04] those two you can see it's still higher
[25:55:07] than the start. And if you compare it
[25:55:08] with the next record this one over here
[25:55:11] it is still smaller than the next one.
[25:55:13] So there is no overlapping. And now for
[25:55:15] the last record since we don't have here
[25:55:17] any informations it will be a null which
[25:55:20] is totally fine. So as you can see I'm
[25:55:22] really happy with this scenario over
[25:55:24] here. Of course you can go and validate
[25:55:25] this with an expert from the source
[25:55:27] system. But let's say I have done that
[25:55:29] and they approved it and now I can go
[25:55:30] and clean up the data using this new
[25:55:33] logic. So this is how I usually
[25:55:34] brainstorm about fixing an issues. If I
[25:55:37] have like a complex stuff, I go and use
[25:55:39] Excel and then discuss it with the
[25:55:40] expert using this example. It's way
[25:55:42] better than showing a database queries
[25:55:45] and so on. It just makes things easier
[25:55:47] to explain and as well to discuss. So
[25:55:49] now how I usually do it, I usually go
[25:55:51] and make a focus on only the columns
[25:55:53] that I need and take only one two
[25:55:55] scenarios while I'm building the logic
[25:55:57] and once everything is ready I go and
[25:55:59] integrate it in the query. So now I'm
[25:56:01] focusing only on these columns and only
[25:56:03] for these products. So now let's go and
[25:56:06] build our logic. Now in SQL if you are
[25:56:08] at specific record and you want to
[25:56:10] access another information from another
[25:56:12] records and for that we have two amazing
[25:56:14] window functions. We have the lead and
[25:56:17] log. In this scenario, we want to access
[25:56:19] the next records. That's why we have to
[25:56:21] go with the function leads. So, let's go
[25:56:23] and build it lead. And then what do we
[25:56:25] need? We need the lead of the
[25:56:27] start date. So, we want the start date
[25:56:30] of the next record. And then we say over
[25:56:33] and we have to partition the data. So,
[25:56:36] the window going to be focusing on only
[25:56:38] one product which is the product key and
[25:56:41] not the product ID. So, we are dividing
[25:56:43] the data by product key. And of course,
[25:56:45] we have to go and sort the data. So
[25:56:47] order by and we are sorting the data by
[25:56:49] the start
[25:56:50] date and ascending. So from the lowest
[25:56:53] to the highest and let's go and give it
[25:56:55] another name. So as let's say test for
[25:56:58] example just to test the data. So let's
[25:57:01] go and execute. And I think I missed
[25:57:03] something here. It is partition by. So
[25:57:05] let's go and execute again. And now
[25:57:07] let's go and check the results for the
[25:57:09] first partition over here. So the start
[25:57:10] is 2011 and the end is 2012. And this
[25:57:14] information came from the next record.
[25:57:17] So this data is moved to the previous
[25:57:19] record over here. And the same thing for
[25:57:21] this record. So the end date comes from
[25:57:24] the next record. So our logic is
[25:57:26] working. And the last record over here
[25:57:28] is null because we are at the end of the
[25:57:30] window and there is no next data. That's
[25:57:32] why we will get null and this is perfect
[25:57:34] of course. So it looks really awesome.
[25:57:36] But what is missing is we have to go and
[25:57:39] get the previous day. And we can do that
[25:57:41] very simply using minus one. we are just
[25:57:43] subtracting one day. So we have no
[25:57:45] overlapping between those two dates and
[25:57:47] the same thing for those two dates. So
[25:57:49] as you can see we have just built a
[25:57:51] perfect end date which is way better
[25:57:53] than the original data that we got from
[25:57:55] the source system. Now let's take this
[25:57:57] one over here and put it inside our
[25:57:59] query. So we don't need the end date, we
[25:58:02] need our new end date. Let's just remove
[25:58:05] that test and execute. Now it looks
[25:58:08] perfect. All right. Now we are not done
[25:58:10] yet with those two dates. Actually we
[25:58:12] are saying all time dates because we
[25:58:14] don't have here any informations about
[25:58:16] the time always zero. So it makes no
[25:58:18] sense to have these informations inside
[25:58:21] our data. So what we can do we can do a
[25:58:23] very simple cast and we make this column
[25:58:26] as a date instead of date time. So this
[25:58:29] is for the first one and as well for the
[25:58:31] next one as date. So let's try that out.
[25:58:34] And as you can see it is nicer. We don't
[25:58:36] have the time informations. Of course,
[25:58:38] we can tell the source systems about all
[25:58:40] those issues. But since they don't
[25:58:42] provide a time, it makes no sense to
[25:58:44] have date and time. Okay, so it was a
[25:58:46] long run, but we have now a cleaned
[25:58:49] product informations. And this is way
[25:58:51] nicer than the original product
[25:58:53] information that we got from the source
[25:58:54] CRM. So if you grab the DDL of the
[25:58:56] server table, you can see that we don't
[25:58:58] have a category ID. So we have product
[25:59:00] ID and product key. And as well those
[25:59:03] two columns, we just changed the data
[25:59:04] type. So it's date time here but we have
[25:59:06] changed that to a date. So that means we
[25:59:09] have to go and do few modifications to
[25:59:11] the DDL. So what we're going to do we're
[25:59:12] going to go over here and say category
[25:59:14] ID and I will be using the same data
[25:59:16] type for the start and the end. This
[25:59:18] time going to be date and not date and
[25:59:20] time. So that's it for now. Let's go and
[25:59:22] execute it in order to repair the DDL.
[25:59:25] And this is what happen in the silver
[25:59:27] layer. Sometimes we have to adjust the
[25:59:29] metadata if the quality of the data
[25:59:31] types and so on is not good or we are
[25:59:33] building new derived informations in
[25:59:35] order later to integrate the data. So it
[25:59:38] will be like very close to the bronze
[25:59:40] layer but with few modifications. So
[25:59:42] make sure to update your DTL scripts.
[25:59:45] And now the next step is that we're
[25:59:46] going to go and insert the data into the
[25:59:48] table. And now the next we're going to
[25:59:49] go and insert the result of this query
[25:59:52] that is cleaning up the bronze table
[25:59:54] into the silver table. So as we done it
[25:59:57] before insert into silver the product
[26:00:00] info and then we have to go and list all
[26:00:02] the columns. I've just prepared those
[26:00:04] columns. So with that we can go and now
[26:00:07] run our query in order to insert the
[26:00:09] data. So now as you can see this did
[26:00:11] insert the data and the very important
[26:00:13] step is now to check the quality of the
[26:00:15] silver table. So we go back to our data
[26:00:17] quality checks and we go switch to the
[26:00:20] silver. So let's check the primary key.
[26:00:22] There is no issues and we can go and
[26:00:24] check for example here the trims there
[26:00:27] is as well no issue and now let's go and
[26:00:29] check the costs it should not be
[26:00:31] negative or null which is perfect let's
[26:00:34] go and check the data standardizations
[26:00:36] as you can see they are friendly and we
[26:00:38] don't have any nulls and now very
[26:00:40] interesting the order of the dates so
[26:00:43] let's go and check that as you can see
[26:00:45] we don't have any issues and finally
[26:00:47] what I do I go and have a final look to
[26:00:50] the silver table and As we can see
[26:00:53] everything is inserted correctly in the
[26:00:55] correct columns. So all those columns
[26:00:57] comes from the source system and the
[26:00:59] last one is automatically generated from
[26:01:01] the DDL indicate when we loaded this
[26:01:04] table. Now let's sit back and have a
[26:01:06] look to our script. What are the
[26:01:07] different types of data transformations
[26:01:09] that we have done here is for example
[26:01:10] over here the category ID and the
[26:01:12] product key we have derived new columns.
[26:01:15] So it is when we create a new column
[26:01:17] based on calculations or transformations
[26:01:20] of an existing one. So sometimes we need
[26:01:22] columns only for analytics and we cannot
[26:01:25] each time go to the source system and
[26:01:26] ask them to create it. So instead of
[26:01:28] that we derive our own columns that we
[26:01:31] need for the analytics. Another
[26:01:33] transformation we have is the is null
[26:01:35] over here. So we are handling here
[26:01:37] missing information. Instead of null
[26:01:39] we're going to have a zero. And one more
[26:01:40] transformation we have over here for the
[26:01:42] product line. We have done here data
[26:01:45] normalization. Instead of having a code
[26:01:47] value we have a friendly value. And as
[26:01:49] well we have handled the missing data.
[26:01:51] For example, over here instead of having
[26:01:53] a null, we're going to have not
[26:01:55] available. All right, moving on to
[26:01:56] another data transformation. We have
[26:01:58] done data type casting. So we are
[26:02:00] converting the data type from one to
[26:02:02] another. And this considered as well to
[26:02:04] be a data transformation. And now moving
[26:02:06] on to the last one. We are doing as well
[26:02:08] data type casting. But what's more
[26:02:10] important, we are doing data enrichment.
[26:02:12] This type of transformation, it's all
[26:02:14] about adding a value to your data. So we
[26:02:17] are adding new relevant data to our data
[26:02:20] sets. So those are the different types
[26:02:21] of data transformations that we have
[26:02:24] done for this
[26:02:28] table. Okay. So let's keep going. We
[26:02:30] have the sales details and this is the
[26:02:32] last table in the CRM. So what do we
[26:02:33] have over here? We have the order number
[26:02:35] and this is a string. Of course we can
[26:02:36] go and check whether we have an issue
[26:02:38] with the unwanted spaces. So we can
[26:02:40] search whether we're going to find
[26:02:42] something. So we can say trim and
[26:02:45] something like this. and let's go and
[26:02:46] execute it. So we can see that we don't
[26:02:48] have any unwanted spaces. That means we
[26:02:50] don't have to transform this column. So
[26:02:51] we can leave it as it is. Now the next
[26:02:53] two columns they are like keys and ids
[26:02:56] in order to connect it with the other
[26:02:58] tables. As we learned before we are
[26:02:59] using the product key in order to
[26:03:02] connect it with the product informations
[26:03:04] and we are connecting the customer ID
[26:03:06] with the customer ID from the customer
[26:03:07] info. So that means we have to go and
[26:03:09] check whether everything is working
[26:03:10] perfectly. So we can go and check the
[26:03:12] integrity of those columns where we say
[26:03:14] the product key not in and then we make
[26:03:17] a subquery and this time we can work
[26:03:19] with the silver layer right so we can
[26:03:21] say the product key from silver dot
[26:03:25] product info so let's go and query this
[26:03:27] and as you can see we are not getting
[26:03:28] any issue that means all the product
[26:03:30] keys from the sales details can be used
[26:03:33] and connected with the product info the
[26:03:35] same thing we can go and check the
[26:03:36] integrity of the customer ID and we can
[26:03:39] use not the product we and go to the
[26:03:41] customer info and the name was CST ID.
[26:03:45] So let's go and query that and the same
[26:03:47] thing we don't have here any issues. So
[26:03:49] that means we can go and connect the
[26:03:50] sales with the customers using the
[26:03:52] customer ID and we don't have to do any
[26:03:54] transformations for it. So things looks
[26:03:56] really nice for those three columns. Now
[26:03:58] we come to the challenging one. We have
[26:04:00] here the dates. Now those dates are not
[26:04:03] actual dates. They are integer. So those
[26:04:05] are numbers and we don't want to have it
[26:04:07] like this. We would like to clean that
[26:04:09] up. we have to change the data type from
[26:04:11] integer to a dates. Now if you want to
[26:04:13] convert an integer to a date, we have to
[26:04:15] be careful with the values that we have
[26:04:17] inside each of those columns. So now
[26:04:19] let's check the quality for example of
[26:04:21] the order dates. Let's say where order
[26:04:23] dates is less than zero for example
[26:04:26] something negative. Well, we don't have
[26:04:28] any negative values which is good. Let's
[26:04:29] go and check whether we have any zeros.
[26:04:32] Well, this is bad. So we have here a lot
[26:04:34] of zeros. Now what we can do? We can
[26:04:36] replace those informations with a null.
[26:04:38] We can use of course the null if
[26:04:40] function like this. We can say null if
[26:04:42] and if it is zero then make it null. So
[26:04:45] let's execute it. And as you can see now
[26:04:47] all those informations are null. Now
[26:04:49] let's go and check again the data. So
[26:04:51] now this integer has the year's
[26:04:53] information at the start then the months
[26:04:55] and then the day. So here we have to
[26:04:57] have like 1 2 3 4 5. So the length of
[26:05:00] each number should be h. And if the
[26:05:02] length is less than eight or higher than
[26:05:04] eight then we have an issue. Let's go
[26:05:06] and check that. So we're going to say or
[26:05:09] length sales order is not equal to h
[26:05:12] that means less or higher. Let's go and
[26:05:14] execute it. Now let's go and check the
[26:05:16] results over here. And those two
[26:05:18] informations they don't look like a
[26:05:20] date. So we cannot go and make from
[26:05:22] these informations a real date. They are
[26:05:24] just bad data quality. And of course you
[26:05:26] can go and check the boundaries of a
[26:05:28] date. Like for example it should not be
[26:05:31] higher than for example let's go and get
[26:05:33] this value 2050 and then any for the
[26:05:36] month and the date. So let's go and
[26:05:37] execute it. And if we just remove those
[26:05:39] informations just to make sure. So we
[26:05:41] don't have any date that is outside of
[26:05:43] the boundaries that you have in your
[26:05:45] business. Or you go for example and say
[26:05:47] the boundary should be not less than
[26:05:49] depend when your business started. Maybe
[26:05:51] something like this. We are getting of
[26:05:53] course those values because they are
[26:05:55] less than null. But if you have values
[26:05:57] around this dates you will get it as
[26:05:59] well in the query. So we can go and add
[26:06:01] the rests. So all those checks like
[26:06:03] validate the column that has a date
[26:06:05] informations and it has the data type
[26:06:07] integer. So again what are the issues
[26:06:09] over here? We have zeros and sometimes
[26:06:11] we have like strange numbers that cannot
[26:06:14] be converted to a dates. So let's go and
[26:06:16] fix that in our query. So we can say
[26:06:18] case when the sales order the order
[26:06:21] dates is equal to zero or of the order
[26:06:25] date is not equal to 8 then null. Right?
[26:06:28] We don't want to deal with those values.
[26:06:30] they are just wrong and they they are
[26:06:31] not real dates otherwise we say else
[26:06:34] it's going to be the order date. Now
[26:06:36] what we're going to do we're going to go
[26:06:37] and convert this to a date. We don't
[26:06:39] want this as an integer. So how we can
[26:06:41] do that? We can go and cast it first to
[26:06:43] a varchar because we cannot cast from
[26:06:46] integer to date in SQL server. First you
[26:06:49] have to convert it to a varchchar and
[26:06:51] then from varchchar you go to a date.
[26:06:53] Well this is how we do it in SQL server.
[26:06:55] So we cast it first to a varchar and
[26:06:58] then we cast it to a date like this.
[26:07:01] That's it. So we have end and we are
[26:07:03] using the same column
[26:07:05] name. So this is how we transform an
[26:07:08] integer to a date. So let's go and query
[26:07:11] this. And as you can see the order date
[26:07:13] now is a real date. It is not a number.
[26:07:16] So we can go and get rid of the old
[26:07:18] column. Now we have to go and do the
[26:07:19] same stuff for the shipping dates. So,
[26:07:21] we can go over here and replace
[26:07:24] everything with the shipping date and
[26:07:26] let's go and query. Well, as you can
[26:07:27] see, the shipping date is perfect. We
[26:07:29] don't have any issue with this column.
[26:07:31] But still, I don't like that we found a
[26:07:32] lot of issues with the order date. So,
[26:07:34] what we're going to do just in case this
[26:07:36] happens for the shipping date in the
[26:07:38] future, I will go and apply the same
[26:07:39] rules to the shipping dates. Oh, let's
[26:07:42] take the shipping
[26:07:43] date like this. And if you don't want to
[26:07:46] apply it now, you have always to build
[26:07:48] like quality checks that runs every day
[26:07:50] in order to detect those issues. And
[26:07:53] once you detect it, then you can go and
[26:07:55] do the transformations. But for now, I'm
[26:07:56] going to apply it right away. So that is
[26:07:58] for the shipping date. Now we go to the
[26:08:00] due date and we will do the same test.
[26:08:04] Let's go and execute it. And as well, it
[26:08:06] is perfect. So still, I'm going to apply
[26:08:09] the same rules. So let's get the due
[26:08:11] date everywhere here in the query. Just
[26:08:13] make sure you don't miss anything here.
[26:08:15] So let's go and execute now. Perfect. As
[26:08:18] you can see, we have the order date,
[26:08:20] shipping date, and due date. And all of
[26:08:21] them are date and don't have any wrong
[26:08:24] data inside those columns. Now, still
[26:08:26] there is one more check that we can do
[26:08:27] and it's that the order date should be
[26:08:30] always smaller than the shipping date or
[26:08:32] the due date because it makes no sense,
[26:08:34] right? If you are delivering an item
[26:08:36] without an order. So first the order
[26:08:38] should happen then we are shipping the
[26:08:40] items. So there is like an order of
[26:08:42] those dates and we can go and check
[26:08:43] that. So we are checking now for invalid
[26:08:45] date orders where we can say the order
[26:08:48] date is higher than the shipping date or
[26:08:52] we are searching as well for an order
[26:08:54] where the order date is higher than the
[26:08:56] due date. So we can have it like this
[26:08:58] due date. So let's go and check. Well,
[26:09:01] that's really good. We don't have such a
[26:09:03] mistake on the data and the quality
[26:09:05] looks good. So the order date is always
[26:09:07] smaller than the shipping date or the
[26:09:10] due date. So we don't have to do any
[26:09:12] transformations or cleanup. Okay
[26:09:13] friends, now moving on to the last three
[26:09:15] columns. We have the sales, quantity and
[26:09:18] the price. All those informations are
[26:09:20] connected to each others. So we have a
[26:09:21] business rule or calculation. It says
[26:09:24] the sales must be equal to quantity
[26:09:26] multiplied by the price. And all sales
[26:09:28] quantity and price informations must be
[26:09:31] positive numbers. So it's not allowed to
[26:09:33] be negative, zero or null. So those are
[26:09:36] the business rules and we have to check
[26:09:38] the data consistency in our table. Does
[26:09:40] all those three informations following
[26:09:42] our rules? So we're going to start first
[26:09:44] with our rule, right? So we're going to
[26:09:46] say if the sales is not equal to
[26:09:50] quantity multiplied by the price. So we
[26:09:53] are searching where the result is not
[26:09:55] matching our expectation. And as well we
[26:09:58] can go and check other stuff like the
[26:09:59] nulls. So for example we can say or
[26:10:01] sales is null or quantity is
[26:10:06] null and the last one for the price and
[26:10:10] as well we can go and check whether they
[26:10:12] are negative numbers or zero. So we can
[26:10:14] go over here and say less or equal to
[26:10:17] zero and apply it for the other columns
[26:10:19] as well. So with that we are checking
[26:10:22] the calculation and as well we are
[26:10:24] checking whether we have null, zero or
[26:10:25] negative numbers. Let's go and check our
[26:10:28] informations. I'm going to have here
[26:10:29] extinct. So let's go and query it. And
[26:10:32] of course we have here bad data. But we
[26:10:35] can go and sort the data by the sales
[26:10:38] quantity and the price. So let's do it.
[26:10:41] Now by looking to the data we can see in
[26:10:43] the sales we have nulls. We have
[26:10:45] negative numbers and zeros. So we have
[26:10:48] all bad combinations and as well we have
[26:10:50] here bad calculations. So as you can see
[26:10:52] the price here is 50, the quantity is
[26:10:54] one but the sales is two which is not
[26:10:56] correct. And here we have as well wrong
[26:10:58] calculations. Here we have to have a 10
[26:11:00] and here nine or maybe the price is
[26:11:02] wrong. And by looking to the quantity
[26:11:04] now you can see we don't have any nulls.
[26:11:06] We don't have any zeros or negative
[26:11:08] numbers. So the quantity looks better
[26:11:10] than the sales. And if you look to the
[26:11:12] prices we have nulls we have negatives
[26:11:14] and yeah we don't have zeros. So that
[26:11:17] means the quality of the sales and the
[26:11:18] price is wrong. The calculation is not
[26:11:21] working and we have these scenarios. Now
[26:11:23] of course how I do it here I don't go
[26:11:24] and try now to transform everything on
[26:11:26] my own. I usually go and talk to an
[26:11:29] expert maybe someone from the business
[26:11:31] or from the source system and I show
[26:11:32] those scenarios and discuss and usually
[26:11:34] there is like two answers either they
[26:11:36] going to tell me you know what I will
[26:11:38] fix it in my source so I have to live
[26:11:40] with it there is incoming bad data and
[26:11:42] the bad data going to be presented in
[26:11:44] the warehouse until the source system
[26:11:46] clean up those issues. And the other
[26:11:48] answer you might get you know what we
[26:11:49] don't have the budget and those data are
[26:11:51] really old and we are not going to do
[26:11:53] anything. So here you have to decide
[26:11:55] either you leave it as it is or you say
[26:11:57] you know what let's go and improve the
[26:11:59] quality of the data. But here you have
[26:12:00] to ask for the experts to support you
[26:12:03] solving these issues because it really
[26:12:05] depend on the rules. Different rules
[26:12:07] makes different transformations. So now
[26:12:09] let's say that we have the following
[26:12:11] rules. If the sales informations are
[26:12:13] null or negative or zero, then use the
[26:12:16] calculation the formula by multiplying
[26:12:18] the quality with the price. And now if
[26:12:20] the prices are wrong, for example, we
[26:12:22] have here a null or zero, then go and
[26:12:24] calculate it from the sales and the
[26:12:26] quantity. And if you have a price that
[26:12:28] is a minus like minus 21, a negative
[26:12:31] number, then you have to go and convert
[26:12:33] it to a 21. So from a negative to a
[26:12:36] positive without any calculations. So
[26:12:38] those are the rules and now we're going
[26:12:39] to go and build the transformations.
[26:12:41] based on those rules. So let's do it
[26:12:43] step by step. I will go over here and
[26:12:45] we're going to start building the new
[26:12:46] sales. So what is the rule says case
[26:12:49] when of course as usual if the
[26:12:52] sales is null or let's say the sales is
[26:12:56] negative number or equal to zero or
[26:12:59] another scenario we have a sales
[26:13:00] information but it is not following the
[26:13:03] calculation. So we have wrong
[26:13:04] information in the sales. So we're going
[26:13:06] to say the sales is not equal to the
[26:13:09] quantity multiplied by the price. But of
[26:13:13] course we will not leave the price like
[26:13:14] this by using the function APS. The
[26:13:17] absolute is going to go and convert
[26:13:19] everything from negative to a positive.
[26:13:21] Then what we have to do is to go and use
[26:13:23] the calculation. So it going to be the
[26:13:26] quantity multiplied by the price. So
[26:13:29] that means we are not using the value
[26:13:31] that's come from the source system. We
[26:13:33] are recalculating it. Now let's say the
[26:13:35] sales is correct and not one of those
[26:13:37] scenarios. So we're going to say else.
[26:13:39] We will go with the sales as it is that
[26:13:41] comes from the source because it is
[26:13:43] correct. It's really nice. Let's go and
[26:13:45] say an end and give it the same name. I
[26:13:47] will go and rename the old one here as
[26:13:50] an old value and the same for the price.
[26:13:53] The quantity will not touch it because
[26:13:56] it is correct. So like this. And now
[26:13:58] let's go and transform the prices. So
[26:14:00] again as usual we go with case when. So
[26:14:03] what are the scenarios? The price is
[26:14:06] null or the price is less or equal to
[26:14:10] zero. Then what we going to do? We're
[26:14:12] going to do the calculation. So it's
[26:14:14] going to be the sales divided by the
[26:14:16] quantity the SLS quantity. But here we
[26:14:20] have to make sure that we are not
[26:14:21] dividing by zero. Currently we don't
[26:14:23] have any zeros in the quantity but you
[26:14:24] don't know in the future you might get a
[26:14:26] zero and the whole code going to break.
[26:14:28] So what you have to do is to go and say
[26:14:30] if you get any zero replace it with a
[26:14:33] null. So null if if it is zero then make
[26:14:36] it null. So that's it. Now if the price
[26:14:38] is not null and the price is not
[26:14:40] negative or equal to zero then
[26:14:42] everything is fine and that's why we're
[26:14:44] going to have now the else it going to
[26:14:46] be the price as it is from the source
[26:14:48] system. So that's it. We're going to say
[26:14:50] end as price. So I'm totally happy with
[26:14:53] that. Let's go and execute it and check
[26:14:55] of course. So those are the old
[26:14:57] informations and those are the new
[26:14:59] transformed cleaned up informations. So
[26:15:00] here previously we have a null but now
[26:15:02] we have two. So two multiplied with one
[26:15:05] we are getting two. So the sales is here
[26:15:07] correct. Now moving on to the next one
[26:15:09] we have in the sales 40 but the price is
[26:15:12] two. So two multiplied with one we
[26:15:14] should get two. So the new sales is
[26:15:16] correct. It is two and not 40. Now to
[26:15:18] the next one over here the old sales is
[26:15:20] zero. But if you go and multiply the
[26:15:22] four with the quantity you will get
[26:15:24] four. So the sales here is not correct.
[26:15:26] That's why in the new sales we have it
[26:15:28] correct as a four. And let's go and get
[26:15:30] a minus. So in this case we have a minus
[26:15:32] which is not correct. So we are getting
[26:15:34] the price multiplied with one. We should
[26:15:36] get here a nine. And this sales here is
[26:15:39] correct. Now let's go and get a scenario
[26:15:41] where the price is null like this here.
[26:15:44] So we don't have here a price but we
[26:15:45] calculated from the sales and the
[26:15:47] quantity. So we divided the 10 by two
[26:15:49] and we have five. So the new price is
[26:15:52] better. And the same thing for the
[26:15:54] minuses. So we have here minus 21 and in
[26:15:56] the output we have 21 which is correct.
[26:15:58] So for now I don't see any scenario
[26:16:01] where the data is wrong. So everything
[26:16:03] looks better than before. And with that
[26:16:05] we have applied the business rules from
[26:16:07] the experts and we have cleaned up the
[26:16:09] data in the data warehouse. And this is
[26:16:12] way better than before because we are
[26:16:13] presenting now better data for analyszis
[26:16:16] and reporting but it is challenging and
[26:16:18] you have exactly to understand the
[26:16:20] business. So now what we're going to do
[26:16:21] we're going to go and copy those
[26:16:22] informations and integrate it in our
[26:16:24] query. So instead of sales we're going
[26:16:27] to get our new calculation and instead
[26:16:30] of the price we will get our correct
[26:16:32] calculation and here I'm missing the
[26:16:35] end. Let's go and run the whole thing
[26:16:37] again. So with that we have as well now
[26:16:40] cleaned sales quantity and price and it
[26:16:43] is following our business rules. So with
[26:16:45] that we are done cleaning up the sales
[26:16:47] details. The next step we're going to go
[26:16:48] and insert it to the sales details. But
[26:16:51] we have to go and check again the DDL.
[26:16:53] So now all what you have to do is to
[26:16:54] compare those results with the DDL. So
[26:16:57] the first one is the order number. It's
[26:16:58] fine. The product key, the customer ID,
[26:17:01] but here we have an issue. All those
[26:17:02] informations now are date and not an
[26:17:04] integer. So we have to go and change the
[26:17:07] data type. And with that we have better
[26:17:09] data type than before. Then the sales
[26:17:11] quantity price it is correct. Let's go
[26:17:13] and drop the table and create it from
[26:17:15] scratch again. And don't forget to
[26:17:17] update your DDL script. So that's it for
[26:17:20] this. And we're going to go now and
[26:17:21] insert the results into our silver table
[26:17:24] sales details. And we have to go and
[26:17:26] list now all the columns. I have already
[26:17:28] prepared the list of all the columns. So
[26:17:31] make sure that you have the correct
[26:17:32] order of the columns. So let's go now
[26:17:34] and insert the data. And with that and
[26:17:37] with that we can see that the SQL did
[26:17:39] insert data to our sales details. But
[26:17:41] now very important is to check the
[26:17:43] health of the silver table. So what
[26:17:45] we're going to do instead here of
[26:17:46] bronze, we're going to go and switch it
[26:17:48] to silver. So let's check over here. So
[26:17:51] here always the order is smaller than
[26:17:53] the shipping and the due date, which is
[26:17:55] really nice. But now I'm very interested
[26:17:58] on the calculations. So here we're going
[26:17:59] to switch it from bronze to silver. And
[26:18:01] I'm going to go and get rid of all those
[26:18:03] calculations because we don't need it
[26:18:06] this. And now let's see whether we have
[26:18:08] any issue. Well, perfect. Our data is
[26:18:11] following the business rules. We don't
[26:18:12] have any nulls, negative values, zeros.
[26:18:14] Now as usual the last step the final
[26:18:17] check we will just have a final look to
[26:18:19] the table. So we have the order number
[26:18:21] the product key the customer ID those
[26:18:23] three dates we have the sales quantity
[26:18:26] and the price and of course we have our
[26:18:29] metadata column. Everything is perfect.
[26:18:31] So now by looking to our code what are
[26:18:33] the different types of data
[26:18:34] transformation that we are doing. So in
[26:18:36] those three columns we are doing the
[26:18:38] following. So at the start we are
[26:18:39] handling invalid data and this is as
[26:18:42] well type of transformation and as well
[26:18:44] at the same time we are doing data type
[26:18:47] casting. So we are changing it to more
[26:18:49] correct data type. And if you are
[26:18:51] looking to the sales over here then what
[26:18:53] we are doing over here is we are
[26:18:55] handling the missing data and as well
[26:18:57] the invalid data by deriving the column
[26:19:00] from already existing one. And it is as
[26:19:03] well very similar for the price. We are
[26:19:05] handling as well the invalid data by
[26:19:08] deriving it from specific calculation
[26:19:10] over here. So those are the different
[26:19:12] types of data transformations that you
[26:19:14] have done in these
[26:19:18] scripts. All right. Now let's keep
[26:19:20] moving to the next system. We have the
[26:19:22] customer AZ2. So here we have like only
[26:19:25] three columns and let's start with the
[26:19:27] ID first. So here again we have the
[26:19:29] customer's informations and if we go and
[26:19:31] check again our model you can see that
[26:19:34] we can connect this table with the CRM
[26:19:36] table customer info using the customer
[26:19:39] key. So that means we have to go and
[26:19:40] make sure that we can go and connect
[26:19:42] those two tables. So let's go and check
[26:19:46] the other table. We can go and check of
[26:19:47] course the server layer. So let's query
[26:19:50] it and we can query both of the tables.
[26:19:53] Now we can see there is here like extra
[26:19:55] characters that are not included in the
[26:19:57] customer key from the CRM. So let's go
[26:20:00] and search for example for this customer
[26:20:02] over here where C ID like so we are
[26:20:08] searching for customer has similar ID.
[26:20:10] Now as you can see we are finding this
[26:20:12] customer but the issue is that we have
[26:20:14] those three characters NAS. There is no
[26:20:17] specifications or explanation why we
[26:20:19] have the NAS. So actually what we have
[26:20:21] to do is to go and remove those
[26:20:22] informations. We don't need it. So let's
[26:20:24] again check the data. So it looks like
[26:20:27] the old data have an NAS at the start
[26:20:29] and then afterward we have new data
[26:20:31] without those three characters. So we
[26:20:33] have to clean up those ids in order to
[26:20:35] be able to connect it with other tables.
[26:20:38] So we're going to do it like this. We're
[26:20:39] going to start with the case when since
[26:20:41] we have like two scenarios in our data.
[26:20:43] So if the C ID is like the three
[26:20:47] characters in as so if the ID start with
[26:20:50] those three characters then we're going
[26:20:52] to go and apply transformation function
[26:20:54] otherwise it's going to stay like it is.
[26:20:58] So that's it. So now we have to go and
[26:21:00] build the transformation. So we're going
[26:21:02] to use substring and then we have to
[26:21:05] define the string. It's going to be the
[26:21:07] CD and then we have to define the
[26:21:09] position where it start cutting or
[26:21:11] extracting. So we can say 1 2 3 and then
[26:21:15] four. So we have to define the position
[26:21:17] number four. And then we have to define
[26:21:19] the string how many characters should be
[26:21:21] extracted. I will make it dynamic. So I
[26:21:24] will go with the length. I will not go
[26:21:26] and count how much. So we're going to
[26:21:27] say the C ID. So it looks good. If it's
[26:21:30] like NAS then go and extract from the CD
[26:21:34] at the position number four the rest of
[26:21:36] the characters. So let's go and execute
[26:21:38] it. And I'm missing here a comma again
[26:21:41] where we don't have any NAS at the
[26:21:43] start. And if you scroll down you can
[26:21:46] see those as well are not affected. So
[26:21:48] with that we have now a nice ID to be
[26:21:51] joined with other table. Of course we
[26:21:53] can go and test it like this where then
[26:21:55] we take the whole thing the whole
[26:21:57] transformation and say not in we remove
[26:22:00] of course the alias name we don't need
[26:22:02] it. And then we make very simple
[26:22:04] substring select
[26:22:06] distinct CST key the customer key from
[26:22:10] the silver table can be silver CRM cost
[26:22:14] info. So that's it. So let's go and
[26:22:17] check. So as you can see it is working
[26:22:19] fine. So we are not able to find any
[26:22:21] unmatching data between the customer
[26:22:23] info from ERB and the CRM. But of course
[26:22:26] after the transformation if you don't
[26:22:28] use the transformation. So if I just
[26:22:30] remove it like this, we will find a lot
[26:22:32] of unmatching data. So this means our
[26:22:34] transformation is working perfectly and
[26:22:36] we can go and remove the original value.
[26:22:38] So that's it for the first column. Okay.
[26:22:41] Now moving on to the next field, we have
[26:22:43] the birthday of the customers. So the
[26:22:45] first thing to do is to check the data
[26:22:46] type. It is a date. So it's fine. It is
[26:22:49] not an integer or a string. So we don't
[26:22:51] have to convert anything. But still
[26:22:53] there is something to check with the
[26:22:55] birth date. So we can check whether we
[26:22:57] have something out of range. So for
[26:22:58] example, we can go and check whether we
[26:23:00] have really old dates at the birth
[26:23:02] dates. So let's take 19, 100, and let's
[26:23:05] say 24 and we can take the first date of
[26:23:08] the month. So let's go and check that.
[26:23:10] Well, it looks like that we have
[26:23:12] customers that are older than 100 year.
[26:23:14] Well, I don't know. Maybe this is
[26:23:16] correct, but it sounds of course strange
[26:23:18] to do the business. Of course.
[26:23:20] Hey, this is Creed and he is in charge
[26:23:24] of something. That is correct. Say hi to
[26:23:27] the kids. Hi kids. Yay. And then we can
[26:23:30] go and check the other boundary where it
[26:23:32] is almost impossible to have a customer
[26:23:35] that the birthday is in the future. So
[26:23:37] we can say birth date is higher than the
[26:23:41] current date like this. So let's go and
[26:23:44] query this information. Well, it will
[26:23:45] not work because we have to have like an
[26:23:47] or between them. And now if we check the
[26:23:49] list over here, we have dates that are
[26:23:52] invalid for the birth dates. So all
[26:23:54] those dates they are all per day in the
[26:23:57] future and this is totally unacceptable.
[26:23:59] So this is an indicator for bad data
[26:24:01] quality. Of course you can go and report
[26:24:03] it to the source system in order to
[26:24:04] correct it. So here it's up to you what
[26:24:06] to do with those dates. Either leave it
[26:24:08] as it is as a bad data or we can go and
[26:24:11] clean that up by replacing all those
[26:24:13] dates with a null or maybe replacing
[26:24:15] only the one that is extreme where it is
[26:24:18] 100% is incorrect. So let's go and write
[26:24:21] the transformation for that. As usual,
[26:24:22] we're going to start with case when
[26:24:24] birth date is larger than the current
[26:24:28] date and time then null. Otherwise, we
[26:24:32] can have an else where we have the birth
[26:24:34] date as it is and then we have an end as
[26:24:38] birth date. So, let's go and execute it.
[26:24:41] And with that, we should not get any
[26:24:43] customer where the birthday in the
[26:24:45] future. So, that's it for the birth
[26:24:47] date. Now, let's move to the next one.
[26:24:49] We have the gender. Now again the gender
[26:24:51] informations is low cardalities. So we
[26:24:53] have to go and check all the possible
[26:24:55] values inside this column. So in order
[26:24:57] to check all the possible values we're
[26:24:59] going to use select distinct gen from
[26:25:02] our table. So let's go and execute it.
[26:25:04] And now the data doesn't look really
[26:25:06] good. So we have here a null, we have an
[26:25:08] f, we have here an empty string, we have
[26:25:11] male, female, and again we have the M.
[26:25:14] So this is not really good. And what
[26:25:15] we're going to do, we're going to go and
[26:25:16] clean up all those informations in order
[26:25:18] to have only three values. Male, female,
[26:25:21] and not available. So, we're going to do
[26:25:23] it like this. We're going to say case
[26:25:25] when and now we're going to go and trim
[26:25:26] the values just to make sure there is
[26:25:28] like no empty spaces. And as well, I'm
[26:25:31] going to go and use the upper function
[26:25:32] just to make sure that in the future if
[26:25:34] we get any lower cases and so on, we are
[26:25:37] covering all the different scenarios. So
[26:25:40] case this is in F or let's say
[26:25:44] female then make it as female and we can
[26:25:48] go and do the same thing for the male
[26:25:51] like this. So if it is an M or a male
[26:25:54] make sure it is capital letters because
[26:25:56] here we are using the upper then it is a
[26:25:58] male otherwise all other scenarios it
[26:26:01] should be not available. So whether it
[26:26:03] is an empty string or nulls and so on.
[26:26:06] So we have to have an end of course as
[26:26:09] gen. So now let's go and test it and
[26:26:10] check whether we have covered
[26:26:11] everything. So you can see the M is now
[26:26:13] male. The empty is not available. The F
[26:26:16] is female. The empty string or maybe
[26:26:19] spaces here is not available. Female
[26:26:21] going to stay as it is. And the same for
[26:26:23] the male. So with that we are covering
[26:26:25] all the scenarios and we are following
[26:26:27] our standards in the project. So I'm
[26:26:29] going to go and cut this and put it in
[26:26:31] our original query over here. So let's
[26:26:34] go and execute the whole thing. And with
[26:26:36] that we have cleaned up all those three
[26:26:39] columns. Now the question is did we
[26:26:40] change anything in the DDL? Well we
[26:26:42] didn't change anything. We didn't
[26:26:44] introduce any new column or change any
[26:26:46] data type. So that means the next step
[26:26:48] is we're going to go and insert it in
[26:26:50] the server layer. So as usual we're
[26:26:52] going to say here insert into silver ERP
[26:26:56] the customer and then we're going to go
[26:26:58] and list all the column names. So C ID
[26:27:01] birth date and the gender. All right. So
[26:27:04] let's go and execute it. And with that
[26:27:06] we can see it inserted all the data. And
[26:27:08] of course the very important step as the
[26:27:10] next is to check the data quality. So
[26:27:12] let's go back to our query over here and
[26:27:15] change it from bronze to silver. So
[26:27:17] let's go and check the silver layer.
[26:27:19] Well of course we are getting those very
[26:27:21] old customers but we didn't change that.
[26:27:24] We only change the birthday that is in
[26:27:26] the future and we don't see it here in
[26:27:28] the results. So that means everything is
[26:27:29] clean. So for the next one, let's go and
[26:27:32] check the different genders. And as you
[26:27:34] can see, we have only those three
[26:27:35] values. And of course, we can go and
[26:27:37] take a final look to our table. So you
[26:27:40] can see the C ID here, the birth date,
[26:27:42] the gender, and then we see our metadata
[26:27:44] column. And everything looks amazing. So
[26:27:47] that's it. What are the different types
[26:27:49] of data transformations that we have
[26:27:50] done? First with the ID, what we have
[26:27:52] done, we have handled invalid values. So
[26:27:55] we have removed this part where it is
[26:27:57] not needed. And the same thing goes for
[26:27:59] the birth dates. We have handled as well
[26:28:02] invalid values. And then for the last
[26:28:04] one, for the gender, we have done data
[26:28:06] normalizations by mapping the code to
[26:28:08] more friendly value. And as well, we
[26:28:10] have handled the missing values. So
[26:28:12] those are the types that we have done in
[26:28:14] this
[26:28:18] code. Okay. Moving on to the second
[26:28:20] table, we have the location
[26:28:22] informations. So we have ERP location
[26:28:25] A101. So now here the task is easy
[26:28:27] because we have only two columns and if
[26:28:29] you go and check the integration model
[26:28:30] we can find our table over here. So we
[26:28:33] can go and connect it together with the
[26:28:35] customer info from the other system
[26:28:36] using a CID with the customer key. So
[26:28:40] those two informations must be matching
[26:28:42] in order to join the tables. So that
[26:28:44] means we have to go and check the data.
[26:28:46] So let's go and select the data CST key
[26:28:50] from let's go and get the silver data
[26:28:52] customer info. So let's go. Now if you
[26:28:54] go and check the result you can see over
[26:28:56] here that we have an issue with the CI
[26:28:59] ID there is like a minus between the
[26:29:02] characters and the numbers but the
[26:29:03] customer ID the customer number we don't
[26:29:05] have anything that splits the characters
[26:29:08] with the numbers. So if you go and join
[26:29:10] those two informations it will not be
[26:29:11] working. So what we have to do we have
[26:29:13] to go and get rid of this minus because
[26:29:15] it is totally unnecessary. So let's go
[26:29:17] and fix that. It's going to be very
[26:29:19] simple. So what we're going to do we're
[26:29:20] going to say CI ID. So we're going to go
[26:29:22] and search for the minus and replace it
[26:29:25] with nothing. It's very simple like
[26:29:27] this. So let's go and query it again.
[26:29:30] And with that things looks very similar
[26:29:31] to each others. And as well we can go
[26:29:33] and query it. So we're going to say
[26:29:35] where our transformation is not in then
[26:29:39] we can go and use this as a subquery
[26:29:41] like this. So let's go and execute it.
[26:29:44] And as you can see we are not finding
[26:29:46] any unmatching data now. So that means
[26:29:48] our transformation is working. And with
[26:29:50] that we can go and connect those two
[26:29:52] tables together. So if I take the
[26:29:54] transformation away you can see that we
[26:29:56] will find a lot of unmatching data. So
[26:29:58] the transformation is okay. We're going
[26:30:00] to stay with it. And now let's speak
[26:30:02] about the countries. Now we have here
[26:30:04] multiple values and so on. What I'm
[26:30:06] going to do this is low cardinality and
[26:30:08] we have to go and check all possible
[26:30:10] values inside this column. So that means
[26:30:12] we are checking whether the data is
[26:30:14] consistent. So we can do it like this.
[26:30:17] distinct the
[26:30:19] country from our table. I'm just going
[26:30:21] to go and copy it like this. And as
[26:30:23] well, I'm going to go and sort the data
[26:30:25] by the country. So, let's go and check
[26:30:28] the informations. Now, you can see we
[26:30:30] have a null. We have an empty string,
[26:30:32] which is really bad. And then we have a
[26:30:34] full name of country and then we have as
[26:30:36] well an abbreviation of the countries.
[26:30:39] Well, this is a mix. This is not really
[26:30:41] good because sometimes we have DE and
[26:30:43] sometimes we have Germany and then we
[26:30:45] have the United Kingdom and then for the
[26:30:47] United States we have like three
[26:30:49] versions of the same information which
[26:30:51] is as well not really good. So the
[26:30:53] quality of the country is not really
[26:30:55] good. So let's go and work on the
[26:30:57] transformation. As usual we're going to
[26:30:58] start with the case win. If trim
[26:31:01] country is equal to D, then we're going
[26:31:06] to transform it to Germany. And the next
[26:31:09] one it's going to be about the USA. So
[26:31:11] if trim country is in. So now let's go
[26:31:15] and get those two values the US and the
[26:31:17] USA. So US and USA then it's going to be
[26:31:23] the United States states. So with us we
[26:31:26] have covered as well those three cases.
[26:31:29] Now we have to talk about the null and
[26:31:30] the empty string. So we're going to say
[26:31:32] when trim country is equal to empty
[26:31:36] string or country is null then it's
[26:31:41] going to be not available otherwise I
[26:31:44] would like to get the country as it is.
[26:31:45] So trim country just to make sure that
[26:31:48] we don't have any leading or trailing
[26:31:50] spaces. So that's it. Let's go and say
[26:31:53] this is the country. So it is working
[26:31:56] and the country information is
[26:31:58] transformed. And now what I'm going to
[26:31:59] do, I'm going to take the whole new
[26:32:00] transformation and compare it to the old
[26:32:03] one. Let me just call this as old
[26:32:07] country and let's go and query it. So
[26:32:09] now we can check those values state as
[26:32:11] before. So nothing did change. The DE is
[26:32:14] now Germany. The empty string is not
[26:32:16] available. The null the same thing and
[26:32:18] the United Kingdom stayed as like it's
[26:32:21] like before. And now we have one value
[26:32:23] for all those information. So it's only
[26:32:26] the United States. So it looks perfect.
[26:32:28] And with that we have cleaned as well
[26:32:30] the second column. So with that we have
[26:32:32] now clean results. And now the question
[26:32:34] did we change anything in the DDL? Well
[26:32:36] we haven't changed anything. Both of
[26:32:38] them are varchar. So we can go now
[26:32:40] immediately and insert it into our
[26:32:42] table. So insert into silver customer
[26:32:46] location. And here we have to specify
[26:32:48] the columns. It's very simple the ID and
[26:32:50] the country. So let's go and execute it.
[26:32:53] And as you can see we got now inserted
[26:32:55] all those values. Of course, as a next,
[26:32:57] we go and double check those
[26:32:59] informations. I would just go and remove
[26:33:01] all those stuff as well here. And
[26:33:03] instead of bronze, let's go with the
[26:33:05] silver. So, as you can see, all the
[26:33:08] values of the country looks good. And
[26:33:10] let's have a final look to the table.
[26:33:12] So, like this. So, we have the ids
[26:33:14] without the separator. We have the
[26:33:16] countries and as well our metadata
[26:33:19] information. So, with that, we have
[26:33:20] cleaned up the data for the location.
[26:33:22] Okay. So now what are the different
[26:33:23] types of data transformation that we
[26:33:25] have done here is first we have handled
[26:33:27] invalid values. So we have removed the
[26:33:30] minus with an empty string and for the
[26:33:32] country we have done data normalization.
[26:33:35] So we have replaced codes with friendly
[26:33:37] values and as well at the same time we
[26:33:40] have handled missing values by replacing
[26:33:42] the empty string and null with not
[26:33:44] available. And one more thing of course
[26:33:46] we have removed the unwanted spaces. So
[26:33:49] those are the different types of
[26:33:50] transformation that we have done for
[26:33:52] this
[26:33:55] table. Okay guys, now keep the energy
[26:33:58] up, keep the spirit up. We have to go
[26:34:00] and clean up the last table in the
[26:34:02] bronze layer. And of course, we cannot
[26:34:04] go and skip anything. We have to check
[26:34:06] the quality and to detect all the
[26:34:08] errors. So now we have a table about the
[26:34:10] categories for the products. And here we
[26:34:12] have like four columns. Let's go and
[26:34:14] start with the first one, the ID. As you
[26:34:16] can see in our integration model, we can
[26:34:18] connect this table together with the
[26:34:20] product info from the CRM using the
[26:34:22] product key. And as you remember in the
[26:34:24] silver layer, we have created an extra
[26:34:26] column for that in the product info. So
[26:34:29] if you go and select those data, you can
[26:34:31] see we have a column called category ID
[26:34:34] and this one is exactly matching the ID
[26:34:36] that we have in this table and we have
[26:34:38] done the testing. So this ID is ready to
[26:34:41] be used together with the other table.
[26:34:43] So there is nothing to do over here. And
[26:34:46] now for the next columns they are
[26:34:47] string. And of course we can go and
[26:34:49] check whether there are any unwanted
[26:34:51] spaces. So we are checking for the
[26:34:52] unwanted spaces. So let's go and check
[26:34:56] select start from and we're going to go
[26:34:58] and get the same table like this here.
[26:35:00] And first we are checking the category.
[26:35:02] So the category is not equal to the
[26:35:05] category after trimming the unwanted
[26:35:07] spaces. So let's go and execute it. And
[26:35:10] as you can see we don't have any
[26:35:12] results. So there are no unwanted
[26:35:14] spaces. Let's go and check the other
[26:35:15] column. For example, the subcategory,
[26:35:18] the next one. So let's get the
[26:35:20] subcategory and run the query as well.
[26:35:23] We don't have anything. So that means we
[26:35:25] don't have unwanted spaces for the
[26:35:27] subcategory. Let's go now and check the
[26:35:29] last column. So I will just copy and
[26:35:31] paste. Now let's get the maintenance and
[26:35:34] let's go and execute. And as well, no
[26:35:36] results. Perfect. We don't have any
[26:35:38] unwanted spaces inside this table. So
[26:35:41] now the next step is that we're going to
[26:35:42] go and check the data standardizations
[26:35:45] because all those columns has low
[26:35:47] cardinality. So what we can do we can
[26:35:49] say
[26:35:49] select distinct let's get the cats
[26:35:53] category from our table. I'll just copy
[26:35:56] and paste it and check all values. So as
[26:35:58] you can see we have the accessories,
[26:36:00] bikes, clothing and components.
[26:36:02] Everything looks perfect. We don't have
[26:36:04] to change anything in this column. Let's
[26:36:06] go and check the subcategory. And if you
[26:36:08] scroll down, all values are friendly and
[26:36:11] nice as well. Nothing to change here.
[26:36:14] And let's go and check the last column,
[26:36:15] the maintenance. Perfect. We have only
[26:36:18] two values, yes and no. We don't have
[26:36:20] any nulls. So my friends, that's means
[26:36:22] this table has really nice data quality
[26:36:25] and we don't have to clean up anything.
[26:36:27] But still, we have to follow our
[26:36:29] process. We have to go and load it from
[26:36:31] the bronze to the silver even if we
[26:36:33] didn't transform anything. So our job is
[26:36:35] really easy. Here we're going to go and
[26:36:37] say insert into silver dot ERP px and so
[26:36:42] on. And we're going to go and define the
[26:36:44] columns. So it's going to be the ID, the
[26:36:46] category,
[26:36:48] subcategory, maintenance. So that's it.
[26:36:50] Let's go and insert the data. Now, as
[26:36:52] usual, what we're going to do, we're
[26:36:53] going to go and check the data. So
[26:36:56] silver
[26:36:58] ERP. Let's have a look. All right. So we
[26:37:01] can see the ids are here, the
[26:37:02] categories, the subcategories, the
[26:37:04] maintenance and we have our meta column.
[26:37:07] So everything is inserted correctly. All
[26:37:10] right. So now I have all those queries
[26:37:12] and the insert statements for all six
[26:37:14] tables. And now what is important before
[26:37:17] inserting any data, we have to make sure
[26:37:19] that we are truncating and emptying the
[26:37:22] table because if you run this query
[26:37:23] twice, what's going to happen? You will
[26:37:25] be inserting duplicates. So first
[26:37:27] truncate the data and then do a full
[26:37:30] load insert all data. So we're going to
[26:37:32] have one step before it's like the
[26:37:34] bronze layer. We're going to say
[26:37:35] truncate table and then we will be
[26:37:37] truncating the silver customer info and
[26:37:39] only after that we have to go and insert
[26:37:42] the data. And of course we can go and
[26:37:43] give this nice information at the start.
[26:37:46] So first we are truncating the table and
[26:37:48] then inserting. So if I go and run the
[26:37:50] whole thing. So let's go and do it. It
[26:37:52] will be working. So if I can run it
[26:37:54] again, we will not have any duplicates.
[26:37:56] So we have to go and add this step
[26:37:58] before each insert. So let's go and do
[26:38:00] that. All right. So I'm done with all
[26:38:03] tables. So now let's go and run
[26:38:06] everything. So let's go and execute it.
[26:38:08] And we can see in the messaging
[26:38:10] everything working perfectly. So with
[26:38:12] that we made all tables empty. And then
[26:38:13] we inserted the
[26:38:18] data. So perfect. With that we have a
[26:38:20] nice script that loads the silver layer.
[26:38:23] But of course like the front layer,
[26:38:25] we're going to put everything in one
[26:38:26] stored procedure. So let's go and do
[26:38:28] that. We'll go to the beginning over
[26:38:30] here and say create or alter procedure
[26:38:34] and we're going to put it in the schema
[26:38:36] silver and using the naming convention
[26:38:38] load silver and we're going to go over
[26:38:40] here and say begin and take the whole
[26:38:42] code end it is long one and give it one
[26:38:45] push with a tab and then at the end
[26:38:47] we're going to say edge. Perfect. So we
[26:38:50] have our stored procedure but we forgot
[26:38:52] here the ass with that we will not have
[26:38:54] any error. Let's go and execute it. So
[26:38:56] the stored procedure is created. If you
[26:38:58] go to the programmability and you will
[26:39:00] find two procedures load bronze and load
[26:39:03] silver. So now let's go and try it out.
[26:39:05] All what you have to do is now only to
[26:39:07] execute the silver load silver. So let's
[26:39:11] execute the start procedure and with
[26:39:13] that we will get the same results. This
[26:39:15] third procedure now is responsible of
[26:39:18] loading the whole silver layer. Now of
[26:39:20] course the messaging here is not really
[26:39:23] good because we have learned in the
[26:39:24] bronze layer we can go and add many
[26:39:26] stuff like handling the error doing nice
[26:39:29] messaging catching the duration time. So
[26:39:32] now your task is to pause the video take
[26:39:34] this start procedure and go and
[26:39:36] transform it to be very similar to the
[26:39:38] bronze layer with the same messaging and
[26:39:41] all the add-ons that we have added. So
[26:39:43] pause the video now. I will do it as
[26:39:44] well offline and I will see you
[26:39:49] [Music]
[26:39:51] soon. Okay. So I hope you are done and I
[26:39:54] can show you the results. It's like the
[26:39:56] bronze layer. We have defined at the
[26:39:57] start few variables in order to catch
[26:40:00] the duration. So we have the start time,
[26:40:02] the end time, patch start time and patch
[26:40:04] end time. And then we are printing a lot
[26:40:06] of stuff in order to have like nice
[26:40:08] messaging in the output. So at the start
[26:40:10] we are saying loading the server layer
[26:40:12] and then we start splitting by the
[26:40:14] source system. So loading the CRM tables
[26:40:17] and I'm going to show you only one table
[26:40:18] for now. So we are setting the timer. So
[26:40:21] we are saying start time get the date
[26:40:23] and time informations to it. Then we are
[26:40:25] doing the usual. We are truncating the
[26:40:27] table and then we are inserting the new
[26:40:29] informations after cleaning it up. And
[26:40:32] we have this nice message. We will say
[26:40:34] load duration where we are finding the
[26:40:36] differences between the start time and
[26:40:38] the end time using the function date
[26:40:40] diff. And we want to show the result in
[26:40:42] the seconds. So we are just printing how
[26:40:45] long it took to load this table. And
[26:40:47] we're going to go and repeat this
[26:40:48] process for all the tables. And of
[26:40:50] course we are putting everything in try
[26:40:52] and catch. So the SQL going to go and
[26:40:55] try to execute the try part. And if
[26:40:57] there are any issues the SQL going to go
[26:41:00] and execute the catch. And here we are
[26:41:02] just printing few information like the
[26:41:04] error message the error number and the
[26:41:06] error states. And we are following
[26:41:07] exactly the same standard at the bronze
[26:41:10] layer. So let's go and execute the whole
[26:41:13] thing. And with that we have updated the
[26:41:15] definition of the third procedure. Let's
[26:41:17] go now and execute it. So execute silver
[26:41:20] dot load silver. So let's go and do
[26:41:23] that. It went very fast like fewer than
[26:41:25] 1 seconds again because we are working
[26:41:27] on local machine loading the server
[26:41:29] layer loading the CRM tables and we can
[26:41:32] see this nice messaging. So it start
[26:41:34] with truncating the table inserting the
[26:41:36] data and we are getting the load
[26:41:38] duration for this table and you will see
[26:41:40] that everything is below 1 second and
[26:41:42] that's because in real projects you will
[26:41:44] get of course more than 1 second. So at
[26:41:47] the end we have load duration of the
[26:41:49] whole silver layer. And now I have one
[26:41:51] more thing for you. Let's say that you
[26:41:53] are changing the design of this store
[26:41:55] procedure for the server layer. You are
[26:41:57] adding different types of messaging or
[26:41:59] maybe you're creating logs and so on. So
[26:42:01] now all those new ideas and redesigns
[26:42:04] that you are doing for the silver layer,
[26:42:05] you have always to think about bringing
[26:42:08] the same changes as well in the other
[26:42:10] store procedure for the pros layer. So
[26:42:12] always try to keep your codes following
[26:42:14] the same standards. Don't have like one
[26:42:16] idea in one store procedure and an old
[26:42:19] idea in another one. Always try to
[26:42:21] maintain those scripts and to keep them
[26:42:23] all up to date following the same
[26:42:25] standards. Otherwise, it can be really
[26:42:27] hard for other developers to understand
[26:42:29] the cause. I know that needs a lot of
[26:42:31] work and commitments, but this is your
[26:42:33] job to make everything following the
[26:42:35] best practices and following the same
[26:42:37] naming convention and standards that you
[26:42:39] put for your projects. So guys, now we
[26:42:42] have very nice two ETL scripts. One that
[26:42:45] loads the bronze layer and another one
[26:42:47] for the server layer. So now our data
[26:42:48] warehouse is very simple. All what you
[26:42:50] have to do is to run first the bronze
[26:42:53] layer and with that we are taking all
[26:42:54] the data from the CSV files from the
[26:42:57] source and we put it inside our data
[26:42:59] warehouse in the bronze layer and with
[26:43:01] that we are refreshing the whole bronze
[26:43:03] layer. Once it's done the next step is
[26:43:06] to run the store procedure of the server
[26:43:08] layer. So once you execute it you are
[26:43:10] taking now all the data from the bronze
[26:43:12] layer transforming it cleaning it up and
[26:43:15] then loading it to the server layer. And
[26:43:18] as you can see the concept is very
[26:43:19] simple. We are just moving the data from
[26:43:21] one layer another layer with different
[26:43:23] tasks. All right guys, so as you can see
[26:43:25] in the server layer we have done a lot
[26:43:27] of data transformations and we have
[26:43:29] covered all the types that we have in
[26:43:31] the data cleansing. So we remove
[26:43:33] duplicates, data filtering, handling
[26:43:35] missing data, invalid data, unwanted
[26:43:37] spaces, casting the data types and so
[26:43:39] on. And as well we have derived new
[26:43:41] columns, we have done data enrichment
[26:43:44] and we have normalized a lot of data. So
[26:43:46] now of course what we have not done yet
[26:43:48] business rules and logic data
[26:43:50] aggregations and data integration. This
[26:43:52] is for the next layer. All right my
[26:43:54] friends. So finally we are done cleaning
[26:43:56] up the data and checking the quality of
[26:43:58] our data. So we can go and close those
[26:44:01] two steps. And now to the next step we
[26:44:03] have to go and extend the data flow
[26:44:05] diagram. So let's
[26:44:09] go. Okay. So now let's go and extend our
[26:44:12] data flow for the silver layer. So, what
[26:44:15] I'm going to do, I'm just going to go
[26:44:16] and copy the whole thing and put it side
[26:44:19] by side to the bronze layer. And let's
[26:44:22] call it silver layer. And the table name
[26:44:25] is going to stay as before because we
[26:44:26] have like one to one like the bronze
[26:44:29] layer. But what we're going to do, we're
[26:44:30] going to go and change the coloring. So,
[26:44:31] I'm going to go and mark everything and
[26:44:33] make it gray like silver. And of course,
[26:44:36] what is very important is to make the
[26:44:38] lineage. So, I'm going to go now from
[26:44:40] the bronze and take an arrow and put it
[26:44:43] to the silver table. And now with that
[26:44:45] we have like a lineage between three
[26:44:47] layers and you are checking this table
[26:44:48] the customer info you can understand aha
[26:44:51] this comes from the bronze layer from
[26:44:53] the customer info and as well this comes
[26:44:55] from the source system CRM so now we can
[26:44:58] see the lineage between different layers
[26:45:01] and without looking to any scripts and
[26:45:03] so on in one picture you can understand
[26:45:05] the whole projects so I don't have to
[26:45:07] explain a lot of stuff by just looking
[26:45:09] to this picture you can understand how
[26:45:11] the data is flowing between sources is
[26:45:14] bronze layer, silver layer, and to the
[26:45:16] gold layer, of course, later. So, as you
[26:45:18] can see, it looks really nice and clean.
[26:45:20] All right. So, with that, we have
[26:45:21] updated the data flow. Next, we're going
[26:45:24] to go and commit our work in the G repo.
[26:45:26] So, let's
[26:45:30] go. Okay. So, now let's go and commit
[26:45:32] our scripts. We're going to go to the
[26:45:34] folder scripts. And here we have a
[26:45:36] server layer. If you don't have it, of
[26:45:38] course, you can go and create it. So,
[26:45:39] first we're going to go and put the DDL
[26:45:41] scripts for the server layer. So let's
[26:45:43] go and I will paste the code over here.
[26:45:46] And as usual, we have this commit as the
[26:45:48] header explaining the purpose of this
[26:45:50] script. So let's go and commit our work.
[26:45:53] And we're going to do the same thing for
[26:45:55] the store procedure that loads the
[26:45:57] server layer. So I'm going to go over
[26:45:59] here. I have already filed for that. So
[26:46:01] let's go and paste that. So we have here
[26:46:04] our stored procedures. And as usual at
[26:46:06] the start, we have as well. So this
[26:46:08] script is doing the ATL process where we
[26:46:10] load the data from bronze into silver.
[26:46:13] So the action is to truncate the table
[26:46:15] first and then insert transformed cleans
[26:46:17] data from bronze to silver. There are no
[26:46:20] parameters at all. And this is how you
[26:46:22] can use the source procedure. Okay. So
[26:46:24] we're going to go and commit our work.
[26:46:27] And now one more thing that we want to
[26:46:29] commit in our project all those queries
[26:46:31] that you have built to check the quality
[26:46:33] of the server layer. So this time we
[26:46:35] will not put it in the scripts. We're
[26:46:36] going to go to the tests and here we're
[26:46:38] going to go and make a new file called
[26:46:40] quality checks silver and inside it
[26:46:42] we're going to go and paste all the
[26:46:44] queries that we have filled. I just here
[26:46:46] reorganize them by the tables. So here
[26:46:49] we can see all the checks that we have
[26:46:51] done during the course and at the header
[26:46:54] we have here nice comments. So here we
[26:46:56] are just saying that this script is
[26:46:57] going to check the quality of the server
[26:46:59] layer and we are checking for nulls,
[26:47:01] duplicates, unwanted spaces, invalid
[26:47:03] date range and so on. So that each time
[26:47:06] you come up with a new quality check,
[26:47:08] I'm going to recommend you to share it
[26:47:09] with the project and with other team in
[26:47:11] order to make it part of multiple checks
[26:47:14] that you do after running the ATL. So
[26:47:17] that's it. I'm going to go and put those
[26:47:18] checks in our repo and in case I come up
[26:47:22] with new check, I'm going to go and
[26:47:23] update it. Perfect. So now we have our
[26:47:25] code in our repository. All right. So
[26:47:28] with that, our code is saved and we are
[26:47:30] done with the whole epic. So we have
[26:47:32] built the silver layer. Now let's go and
[26:47:35] minimize it. And now we come to my
[26:47:37] favorite layer, the code layer. So we're
[26:47:39] going to go and build it. The first step
[26:47:41] as usual, we have to analyze. And this
[26:47:43] time we're going to explore the business
[26:47:44] objects. So let's
[26:47:48] go. All right. So now we come to the big
[26:47:51] question. How we going to build the gold
[26:47:52] layer? As usual, we start with
[26:47:54] analyzing. So now what we're going to do
[26:47:56] here is to explore and understand what
[26:47:58] are the main business objects that are
[26:48:00] hidden inside our source system. So as
[26:48:02] you can see we have two sources six
[26:48:04] files and here we have to identify what
[26:48:06] are the business objects. Once we have
[26:48:07] this understanding then we can start
[26:48:09] coding and here the main transformation
[26:48:11] that we are doing is data integration.
[26:48:13] And here usually I split it into three
[26:48:15] steps. The first one we're going to go
[26:48:17] and build those business objects that we
[26:48:19] have identified. And after we have a
[26:48:21] business objects we have to look at it
[26:48:23] and decide what is the type of this
[26:48:25] table. Is it a dimension? Is it a fact?
[26:48:28] Or is it like maybe a flat table? So
[26:48:30] what type of table that we have built
[26:48:32] and the last step is of course we have
[26:48:34] now to rename all the columns into
[26:48:36] something friendly and easy to
[26:48:38] understand so that our consumers don't
[26:48:40] struggle with technical names. So once
[26:48:41] we have all those steps what we're going
[26:48:43] to do it's time to validate what we have
[26:48:44] created. So what we have to do the new
[26:48:46] data model that we have created it
[26:48:48] should be connectable and we have to
[26:48:50] check that the data integration is done
[26:48:52] correctly and once everything is fine we
[26:48:54] cannot skip the last step. we have to
[26:48:56] document and as well commit our work in
[26:48:58] the g. And here we will be introducing a
[26:49:00] new type of documentations. So we're
[26:49:02] going to have a diagram about the data
[26:49:04] model. We're going to build a data
[26:49:06] dictionary where we're going to describe
[26:49:07] the data model. And of course we're
[26:49:09] going to extend the data flow diagram.
[26:49:11] So this is our process. Those are the
[26:49:13] main steps that we will do in order to
[26:49:15] build the code
[26:49:19] layer. Okay. So what is exactly data
[26:49:22] moduling? Usually the source system
[26:49:23] going to deliver for you row data
[26:49:25] unorganized messy not very useful in its
[26:49:29] current states. But now the data
[26:49:31] modeling is the process of taking this
[26:49:33] row data and then organize it and
[26:49:35] structure it in meaningful way. So what
[26:49:38] we are doing we are putting the data in
[26:49:40] new friendly and easy to understand
[26:49:43] objects like customers, orders,
[26:49:45] products. Each one of them is focused on
[26:49:47] specific information and what is very
[26:49:49] important is we're going to describe the
[26:49:51] relationship between those objects. So
[26:49:54] by connecting them using lines. So what
[26:49:56] you have built on the right side we call
[26:49:58] it logical data model. If you compare to
[26:50:00] the left side you can see the data model
[26:50:02] makes it really easy to understand our
[26:50:03] data and the relationship the processes
[26:50:06] behind them. Now in data modeling we
[26:50:07] have three different stages or let's say
[26:50:09] three different ways on how to draw a
[26:50:11] data model. The first stage is the
[26:50:13] conceptual data model. Here the focus is
[26:50:16] only on the entity. So we have
[26:50:18] customers, orders, products and we don't
[26:50:20] go in details at all. So we don't
[26:50:22] specify any columns or attributes inside
[26:50:24] those boxes. We just want to focus what
[26:50:26] are the entities that we have and as
[26:50:28] well the relationship between them. So
[26:50:30] the conceptual data model don't focus at
[26:50:33] all on the details. It just gives the
[26:50:35] big picture. So the second data model
[26:50:37] that we can build is the logical data
[26:50:39] model. And here we start specifying what
[26:50:41] are the different columns that we can
[26:50:43] find in each entity like we have the
[26:50:45] customer ID the first name last name and
[26:50:48] so on and we still draw the relationship
[26:50:50] between those entities and as well we
[26:50:51] make it clear which columns are the
[26:50:53] primary key and so on. So as you can see
[26:50:55] we have here more details but one thing
[26:50:57] we don't describe a lot of details for
[26:50:59] each column and we are not worry how
[26:51:01] exactly we going to store those tables
[26:51:03] in the database. The third and last
[26:51:05] stage we have the physical data model.
[26:51:08] This is where everything gets ready
[26:51:09] before creating it in the database. So
[26:51:12] here you have to add all the technical
[26:51:13] details like adding for each column the
[26:51:16] data types and the length of each data
[26:51:18] type and many other database techniques
[26:51:21] and details. So again if you look to the
[26:51:23] conceptual data model it gives us the
[26:51:25] big picture and in the logical data
[26:51:26] model we dive into details of what data
[26:51:29] we need and the physical layer model
[26:51:31] prepares everything for the
[26:51:33] implementation in the database. And to
[26:51:35] be honest in my projects I only draw the
[26:51:37] conceptual and the logical data model
[26:51:39] because drawing and building the
[26:51:41] physical data model needs a lot of
[26:51:43] efforts and time and there are many
[26:51:45] tools like in data bricks they
[26:51:46] automatically generate those models. So
[26:51:49] in this project what we're going to do
[26:51:50] we're going to draw the logical data
[26:51:52] model for the gold
[26:51:57] layer. All right. It's now for analytics
[26:51:59] and especially for data warehousing and
[26:52:01] business intelligence. We need a special
[26:52:03] data model that is optimized for
[26:52:05] reporting and analytics and it should be
[26:52:08] flexible, scalable and as well easy to
[26:52:10] understand. And for that we have two
[26:52:12] special data models. The first type of
[26:52:14] data model we have the star schema. It
[26:52:16] has a central fact table in the middle
[26:52:18] and surrounded by dimensions. The fact
[26:52:20] table contains transactions, events, and
[26:52:23] the dimensions contains descriptive
[26:52:25] informations. And the relationship
[26:52:26] between the fact table in the middle and
[26:52:28] the dimensions around it forms like a
[26:52:31] star shape. And that's why we call it
[26:52:33] star schema. And we have another data
[26:52:34] model called snowflake schema. It looks
[26:52:37] very similar to the star schema. So we
[26:52:39] have again the fact in the middle and
[26:52:41] surrounded by dimensions. But the big
[26:52:43] difference is that we break the
[26:52:45] dimensions into smaller subdimensions.
[26:52:48] And the shape of this data model as you
[26:52:50] are extending the dimensions it's going
[26:52:52] to looks like a snowflake. So now if you
[26:52:54] compare them side by side you can see
[26:52:55] that the star schema looks easier right?
[26:52:58] So it is usually easy to understand easy
[26:53:00] to query it is really perfect for
[26:53:02] analyzers but it has one issue with the
[26:53:05] dimension might contain duplicates and
[26:53:07] your dimensions get bigger with the
[26:53:09] time. Now if you compare it to the
[26:53:10] snowflake you can see the schema is more
[26:53:13] complex. You saw you need a lot of
[26:53:15] knowledge and efforts in order to query
[26:53:17] something from the snowflake. But the
[26:53:19] main advantage here comes with the
[26:53:20] normalization as you are breaking those
[26:53:22] redundancies in small tables. You can
[26:53:24] optimize the storage. But to be honest,
[26:53:26] who care about the storage? So for this
[26:53:28] project, I have chose to use the star
[26:53:30] schema because it is very commonly used.
[26:53:32] Perfect for reporting like for example
[26:53:34] if you're using PowerBI and we don't
[26:53:37] have to worry about the storage. So
[26:53:38] that's why we're going to adopt this
[26:53:40] model to build our gold layer.
[26:53:46] Okay. So now one more thing about those
[26:53:47] data models is that they contain two
[26:53:50] types of tables fact and dimensions. So
[26:53:52] when I say this is a fact table or a
[26:53:54] dimension table well the dimension
[26:53:56] contains descriptive informations or
[26:53:58] like categories that gives some context
[26:54:00] to your data. For example a product info
[26:54:02] you have product name, category,
[26:54:04] subcategories and so on. This is like a
[26:54:06] table that is describing the products
[26:54:09] and this we call it dimension. But in
[26:54:11] the other hand we have facts. They are
[26:54:13] events like transactions. They contain
[26:54:15] three important informations. First you
[26:54:18] have multiple ids from multiple
[26:54:20] dimensions. Then we have like date
[26:54:22] informations like when the transaction
[26:54:24] or the event did happen. And the third
[26:54:27] type of information you're going to have
[26:54:28] like measures and numbers. So if you see
[26:54:30] those three types of data in one table,
[26:54:33] then this is a fact. So if you have a
[26:54:35] table that answers how much or how many,
[26:54:38] then this is a fact. But if you have a
[26:54:40] table that answers who, what, where,
[26:54:43] then this is a dimension table. So this
[26:54:45] is what dimension and fact
[26:54:50] tables. All right my friends. So so far
[26:54:52] in the bronze layer and in the silver
[26:54:54] layer we didn't discuss anything about
[26:54:56] the business. So the bronze and silver
[26:54:58] were very technical. We are focusing on
[26:55:00] data ingestion. We are focusing on
[26:55:02] cleaning up the data quality of the
[26:55:04] data. But still the tables are very
[26:55:06] oriented to the source system. Now comes
[26:55:09] the fun part in the god layer where
[26:55:11] we're going to go and break the whole
[26:55:12] data model of the sources. So we're
[26:55:14] going to create something completely new
[26:55:16] to our business that is easy to consume
[26:55:19] for business reporting and analyzes. And
[26:55:22] here it is very important to have a
[26:55:23] clear understanding of the business and
[26:55:25] the processes. And if you don't know it
[26:55:27] already at this phase you have really to
[26:55:29] invest time by meeting maybe process
[26:55:31] experts, the domain experts in order to
[26:55:34] have clear understanding what we are
[26:55:36] talking about in the data. So now what
[26:55:38] we're going to do, we're going to try to
[26:55:39] detect what are the business objects
[26:55:41] that are hidden in the source systems.
[26:55:44] So now let's go and explore that. All
[26:55:46] right. Now in order to build a new data
[26:55:48] model, I have to understand first the
[26:55:50] original data model. What are the main
[26:55:52] business objects that we have? How
[26:55:53] things are related to each others? And
[26:55:55] this is very important process in
[26:55:57] building a new model. So now what I
[26:55:59] usually do, I start giving labels to all
[26:56:02] those tables. So if you go to the shapes
[26:56:03] over here, let's go and search for
[26:56:05] label. And if we go to more icons, I'm
[26:56:08] going to go and take this label over
[26:56:09] here. So, drag and drop it. And then I'm
[26:56:11] going to go and increase maybe the size
[26:56:13] of the font. So, let's go with 20 and
[26:56:16] bold. Just make it a little bit bigger.
[26:56:18] So, now by looking to this data model,
[26:56:20] we can see that we have product
[26:56:22] informations in the CRM and as well in
[26:56:24] the ARP. And then we have like customer
[26:56:26] informations and transactional table.
[26:56:29] So, now let's focus on the product. So,
[26:56:31] the product information is over here. We
[26:56:33] have here the current and the history
[26:56:35] product informations and here we have
[26:56:37] the categories that's belong to the
[26:56:39] products. So in our data model we have
[26:56:41] something called products. So let's go
[26:56:42] and create this label. It's going to be
[26:56:44] the product and let's go and give it a
[26:56:47] color to the style. Let's pick for
[26:56:49] example the red one. Now let's go and
[26:56:52] move this label and put it beneath this
[26:56:54] table over here. And with that I have
[26:56:56] like a label saying this table belongs
[26:56:59] to the objects called products. Now I'm
[26:57:01] going to do the same thing for the other
[26:57:03] table over here. So I'm going to go and
[26:57:05] tag this table to the product as well.
[26:57:07] So that I can see easily which tables
[26:57:09] from the sources does has informations
[26:57:11] about the product business object. All
[26:57:14] right. Now moving on, we have here a
[26:57:15] table called customer information. So we
[26:57:18] have a lot of information about the
[26:57:19] customer. We have as well in the ARP
[26:57:21] customer information where we have the
[26:57:22] birthday and the country. So those three
[26:57:25] tables has to do with the object
[26:57:27] customer. So that means we're going to
[26:57:28] go and label it like that. So let's call
[26:57:30] it customer and I'm going to go and pick
[26:57:33] different color for that. Let's go with
[26:57:35] the green. So I will tag this table like
[26:57:38] this. And the same thing for the other
[26:57:40] tables. So copy tag the second table and
[26:57:44] the third table. Now it is very easily
[26:57:46] for me to see which table to belong to
[26:57:49] which business objects. And now we have
[26:57:51] the final table over here and only one
[26:57:53] table about the sales and orders. In the
[26:57:55] arb we don't have any informations about
[26:57:57] that. So this one going to be easy.
[26:57:59] Let's call it sales. And let's move it
[26:58:03] over here. And as well maybe change the
[26:58:05] color of that to for example this color
[26:58:07] over here. Now this step is very
[26:58:09] important by building any data model in
[26:58:11] the gold layer. It gives you a big
[26:58:13] picture about the things that you are
[26:58:14] going to module. So now the next step is
[26:58:16] that we're going to go and build those
[26:58:18] objects step by step. So let's start
[26:58:19] with the first objects with our
[26:58:21] customers. So here we have three tables
[26:58:23] and we're going to start with the CRM.
[26:58:25] So let's start with this table over
[26:58:26] here. All right. So with that we know
[26:58:28] what are our business objects and this
[26:58:30] task is done and now in the next step
[26:58:32] we're going to go back to scale and
[26:58:34] start doing data integrations and
[26:58:36] building completely new data model. So
[26:58:39] let's go and do
[26:58:43] that. Now let's have a quick look to the
[26:58:45] good layer specifications. So this is
[26:58:47] the final stage. We're going to provide
[26:58:49] data to be consumed by reporting and
[26:58:51] analytics. And this time we will not be
[26:58:53] building tables. We will be using views.
[26:58:56] So that means we will not be having like
[26:58:58] stored procedure or any load process to
[26:59:00] the code layer. All what we are doing is
[26:59:02] only data transformation and the focus
[26:59:04] of the data transformation going to be
[26:59:06] data integration, aggregation, business
[26:59:08] logic and so on. And this time we're
[26:59:10] going to introduce a new data model. We
[26:59:12] will be doing star schema. So those are
[26:59:15] the specifications for the gold layer
[26:59:16] and this is our scope. So this time we
[26:59:19] make sure that we are selecting data
[26:59:21] from the silver layer not from the
[26:59:23] bronze because the bronze has bad data
[26:59:25] quality and the silver is everything is
[26:59:27] prepared and cleaned up. In order to
[26:59:29] build the good layer going to be
[26:59:30] targeting the server layer. So let's
[26:59:33] start with select star from and we're
[26:59:36] going to go to the silver CRM customer
[26:59:39] info. So let's go and hit execute. And
[26:59:41] now we're going to go and select the
[26:59:42] columns that we need to be presented in
[26:59:45] the go layer. So let's start selecting
[26:59:47] the columns that we want. So we have the
[26:59:49] ID, the key, the first
[26:59:55] name. I will not go and get the metadata
[26:59:58] information. This only belongs to the
[27:00:00] silver. Perfect. The next step is that
[27:00:01] I'm going to go and give this table an
[27:00:03] alias. So let's go and call it CI. And
[27:00:06] I'm going to make sure that we are
[27:00:07] selecting from this alias because later
[27:00:10] we're going to go and join this table
[27:00:12] with other tables. So something like
[27:00:14] this. So we're going to go with those
[27:00:15] columns. Now let's move to the second
[27:00:17] table. Let's go and get the birthday
[27:00:19] information. So now we're going to jump
[27:00:20] to the other system and we have to join
[27:00:22] the data by the CID together with the
[27:00:25] customer key. So now we have to go and
[27:00:27] join the data with another table. And
[27:00:29] here I try to avoid using the inner join
[27:00:32] because if the other table doesn't have
[27:00:34] all the information about the customers,
[27:00:36] I might lose customers. So always start
[27:00:38] with the master table and if you join it
[27:00:41] with any other table in order to get
[27:00:43] informations try always to avoid inner
[27:00:45] join because the other source might not
[27:00:48] have all the customers and if you do
[27:00:49] inner join you might lose customers. So
[27:00:51] I tend to start from the master table
[27:00:53] and then everything else is about the
[27:00:56] lift join. So I'm going to say lift join
[27:00:58] silver ERP customer a12. So let's give
[27:01:01] it the alias ca. And now we have to join
[27:01:04] the tables. So it's going to be by CE
[27:01:06] from the first table. It's going to be
[27:01:08] the customer key equal to CA and we have
[27:01:12] the CI ID. Now of course we're going to
[27:01:14] get matching data because we checked the
[27:01:16] server layer. But if we haven't prepared
[27:01:18] the data in the server layer, we have to
[27:01:20] do here preparation step in order to
[27:01:22] join the tables. But we don't have to do
[27:01:23] that because that was a pre-step in the
[27:01:26] server layer. So now you can see the
[27:01:27] systematic that we have in this bronze,
[27:01:30] silver, gold. So now after joining the
[27:01:32] tables we have to go and pick the
[27:01:33] information that we need from the second
[27:01:35] table which is the birth date. So B date
[27:01:38] dates and as well from this table there
[27:01:40] is another nice information it is the
[27:01:43] gender information. So that's all what
[27:01:46] we need from the second table. Let's go
[27:01:48] and check the third table. So the third
[27:01:50] table is about the location information
[27:01:52] the countries and as well we connect the
[27:01:54] tables by the CID with the key. So let's
[27:01:57] go and do that. We're going to say as
[27:01:59] well left join silver ERP location and
[27:02:03] I'm going to give it the name LA and
[27:02:05] then we have to join Y the keys the same
[27:02:07] thing it's going to be CI customer key
[27:02:10] equal to LA CI ID again we have prepared
[27:02:14] those ids and keys in the server layer
[27:02:16] so the join should be working now we
[27:02:18] have to go and pick the data from the
[27:02:20] second table so what do we have over
[27:02:22] here we have the ID the country and the
[27:02:24] metadata information so let's go and
[27:02:26] just get the country Perfect. So now
[27:02:29] with that we have joined all the three
[27:02:31] tables and we have picked all the
[27:02:33] columns that we want in this object. So
[27:02:35] again by looking over here we have
[27:02:37] joined this table with this one and this
[27:02:39] one. So with that we have collected all
[27:02:41] the customer informations that we have
[27:02:43] from the two source systems. Okay. So
[27:02:46] now let's go and query in order to make
[27:02:47] sure that we have everything correct and
[27:02:49] in order to understand that your joints
[27:02:51] are correct you have to keep your eye in
[27:02:54] those three columns. So if you are
[27:02:55] seeing that you are getting data that
[27:02:58] means you are doing the the joints
[27:03:00] correctly but if you are seeing a lot of
[27:03:02] nulls or no data at all that means your
[27:03:05] joints are incorrect but now it looks
[27:03:07] for me it is working and another check
[27:03:10] that I do is that if your first table
[27:03:12] has no duplicates what could happen is
[27:03:14] that after doing multiple joins you
[27:03:16] might now start getting duplicates
[27:03:18] because the relationship between those
[27:03:20] tables is not clear one to one you might
[27:03:23] get like one to many relationship ship
[27:03:24] or many to many relationships. So now
[27:03:26] the check that I usually do at this
[27:03:28] stage is that I have to make sure that I
[27:03:30] don't have duplicates from their
[27:03:32] results. So we don't have like multiple
[27:03:34] rows for the same customer. So in order
[27:03:36] to do that, we go and do a quick group
[27:03:39] by. So we're going to group by the data
[27:03:41] by the customer ID and then we do the
[27:03:44] count from this subquery. So this is the
[27:03:48] whole subquery and then after that we're
[27:03:51] going to go and say group by the
[27:03:54] customer ID and then we say having
[27:03:58] count higher than one. So this query
[27:04:02] actually try to find out whether we have
[27:04:04] any duplicates in the primary key. So
[27:04:07] let's go and execute it. We don't have
[27:04:09] any duplicates and that means after
[27:04:11] joining all those tables with the
[27:04:13] customer info those tables didn't cause
[27:04:16] any issues and didn't duplicate my data.
[27:04:18] So this is very important check to make
[27:04:21] sure that you are in the right way. All
[27:04:23] right. So that means everything is fine
[27:04:25] about the duplicates. We don't have to
[27:04:27] worry about it. Now we have here an
[27:04:29] integration issue. So let's go and
[27:04:30] execute it again. And now if you look to
[27:04:32] the data we have two sources for the
[27:04:34] gender informations. one comes from the
[27:04:36] CRM and another one come from the ERP.
[27:04:39] So now the question is what we're going
[27:04:40] to do with this? Well, we have to do
[27:04:42] data integration. So let me show you how
[27:04:44] I do it. First I go and have a new query
[27:04:47] and then I'm going to go and remove all
[27:04:49] other stuff and I'm going to leave only
[27:04:51] those two informations and use it
[27:04:53] distinct just to focus on the
[27:04:56] integration and let's go and execute it
[27:04:58] and maybe as well to do an order by. So
[27:05:00] let's do one and two. Let's go and
[27:05:02] execute it again. So now here we have
[27:05:04] all the scenarios and we can see
[27:05:06] sometimes there is a matching. So from
[27:05:08] the first table we have female and the
[27:05:10] other table we have as well female but
[27:05:12] sometimes we have an issue like those
[27:05:13] two tables are giving different
[27:05:15] informations and the same thing over
[27:05:17] here. So this is as well an issue
[27:05:18] different informations. Another scenario
[27:05:20] where we have a data from the first
[27:05:22] table like here we have the female but
[27:05:24] in the other table we have not
[27:05:26] available. Well this is not a problem.
[27:05:28] So we can get it from the first table
[27:05:30] but we have as well the exact opposite
[27:05:32] scenario where from the first table the
[27:05:34] data is not available but it is
[27:05:36] available from the second table. And now
[27:05:38] here you might wonder why I'm getting a
[27:05:40] null over here. We did handle all the
[27:05:42] missing data in the silver layer and we
[27:05:44] replace everything with not available.
[27:05:46] So why we are still in getting a null?
[27:05:48] This null doesn't come directly from the
[27:05:50] tables. It just come because of joining
[27:05:53] tables. So that means there are
[27:05:55] customers in the CRM table that is not
[27:05:58] available in the ARB table and if there
[27:06:01] is like no match what going to happen we
[27:06:03] will get a null from SQL. So this null
[27:06:06] means there was no match and that's why
[27:06:08] we are getting this null. It is not
[27:06:10] coming from the content of the tables
[27:06:12] and this is of course an issue. But now
[27:06:14] the big issue what can happen for those
[27:06:16] two scenarios here we have the data but
[27:06:19] they are different. And here again we
[27:06:20] have to ask the experts about it. What
[27:06:23] is the master here? Is it the CRM system
[27:06:26] or the ARP? And let's say from their
[27:06:28] answer going to say the master data for
[27:06:30] the customer information is the CRM. So
[27:06:33] that means the CRM informations are more
[27:06:36] accurate than the ERP information and
[27:06:39] this is only about the customers of
[27:06:40] course. So for this scenario where we
[27:06:42] have female and male then the correct
[27:06:44] information is the female from the first
[27:06:46] source system. The same goes over here
[27:06:48] and here we have like male and female
[27:06:50] then the correct one is the male because
[27:06:53] this source system is the master. Okay.
[27:06:55] So now let's go and build this business
[27:06:57] rule. We're going to start as usual with
[27:06:59] the case win. So the first very
[27:07:01] important rule is if we have a data in
[27:07:03] the gender information from the CRM
[27:07:06] system from the master then go and use
[27:07:08] it. So we're going to go and check the
[27:07:10] gender information from the CRM table.
[27:07:12] So customer gender is not equal to not
[27:07:15] available. So that means we have a value
[27:07:18] male or female. Let me just have here a
[27:07:20] comma like this. Then what's going to
[27:07:22] happen? Go and use it. So we're going to
[27:07:24] use the value from the master. CRM is
[27:07:28] the master for gender info. Now
[27:07:32] otherwise that means it is not available
[27:07:35] from the CRM table. Then go and use and
[27:07:39] grab the information from the second
[27:07:40] table. So we're going to say CA gender.
[27:07:44] But now we have to be careful with this
[27:07:46] null over here. We have to convert it to
[27:07:47] not available as well. So we're going to
[27:07:49] use the
[27:07:50] kis. So if this is a null then go and
[27:07:54] use the not available like this. So
[27:07:57] that's it. Let's have an end. And let me
[27:07:59] just push this over here. So let's go
[27:08:01] and call it new gen for now. Let's go
[27:08:04] and execute it and let's go and check
[27:08:06] the different scenarios. All those
[27:08:08] values over here we have data from the
[27:08:11] CRM system and this is as well
[27:08:13] represented in the new column. But now
[27:08:15] for the second part we don't have data
[27:08:18] from the first system. So we are trying
[27:08:19] to get it from the second system. So for
[27:08:22] the first one is not available and then
[27:08:24] we try to get it from the second source
[27:08:26] system. So now we are activating the
[27:08:27] else. Well it is null and with that the
[27:08:30] kalis is activated and we are replacing
[27:08:32] the null with not available. For the
[27:08:35] second scenario as well, the first
[27:08:36] search system don't have the gender
[27:08:39] information. That's why we are grabbing
[27:08:40] it from the second. So with that we have
[27:08:43] a female. And then the third one the
[27:08:45] same thing we don't have information but
[27:08:46] we get it from the second source system.
[27:08:48] We have the male and the last one it is
[27:08:50] not available in both source systems.
[27:08:53] That's why we are getting not available.
[27:08:55] So with that as you can see we have a
[27:08:56] perfect new column where we are
[27:08:58] integrating two different source system
[27:09:00] in one. And this is exactly what we call
[27:09:03] data integration. This piece of
[27:09:05] information, it is way better than the
[27:09:07] source CRM and as well the source ARP.
[27:09:11] It is more rich and has more
[27:09:13] information. And this is exactly why we
[27:09:15] try to get data from different source
[27:09:17] system in order to get rich information
[27:09:19] in the data warehouse. So with that we
[27:09:21] have a nice logic and as you can see
[27:09:23] it's way easier to separate it in
[27:09:25] separate query in order first to build
[27:09:27] the logic and then take it to the
[27:09:29] original query. So what I'm going to do,
[27:09:31] I'm just going to go and copy everything
[27:09:32] from here and go back to our query. I'm
[27:09:35] going to go and delete those
[27:09:36] informations the gender and I will put
[27:09:39] our new logic over here. So a comma and
[27:09:42] let's go and execute. So with that we
[27:09:44] have our new nice column. Now with that
[27:09:46] we have very nice objects. We don't have
[27:09:48] duplicates and we have integrated data
[27:09:50] together. So we took three tables and we
[27:09:52] put it in one object. Now the next step
[27:09:54] is that we're going to go and give nice
[27:09:57] friendly names. The rule in the gold
[27:09:59] layer that to use friendly names and not
[27:10:01] to follow the names that we get from the
[27:10:03] source system and we have to make sure
[27:10:05] that we are following the rules by the
[27:10:07] naming conventions. So we are following
[27:10:09] the snake case. So let's go and do it
[27:10:11] step by step. For the first one let's go
[27:10:13] and call it the customer ID. And then
[27:10:15] the next one I will get rid of using
[27:10:18] keys and so on. I'm going to go and call
[27:10:19] it customer number because those are
[27:10:22] customer numbers. Then for the next one,
[27:10:25] we're going to call it first name
[27:10:28] without using any prefixes. And the next
[27:10:31] one last name and we have here marital
[27:10:35] status. So I will be using the exact
[27:10:38] name but without the prefix. And here we
[27:10:41] just going to call it gender. And this
[27:10:42] one we're going to call it career date.
[27:10:46] And this one birth date. And the last
[27:10:49] one going to be the country. So let's go
[27:10:52] and execute it. Now as you can see the
[27:10:54] names are really friendly. So we have
[27:10:56] customer ID, customer numbers, first
[27:10:58] name, last name, material status,
[27:11:00] gender. So as you can see the names are
[27:11:02] really nice and really easy to
[27:11:04] understand. Now the next step I'm going
[27:11:06] to think about the order of those
[27:11:07] columns. So the first two it makes sense
[27:11:09] to have it together. The first name,
[27:11:11] last name, then I think the country is
[27:11:13] very important information. So I'm going
[27:11:14] to go and get it from here and put it
[27:11:16] exactly after the last name is just
[27:11:19] nicer. So let's go and execute it again.
[27:11:21] So the first name, last name, country.
[27:11:23] It's always nice to group up relevant
[27:11:25] columns together, right? So we have here
[27:11:27] the status of the gender and so on. And
[27:11:29] then we have the career date and the
[27:11:30] birth date. I think I'm going to go and
[27:11:32] switch the birth date with the career
[27:11:34] date. It's more important than the
[27:11:36] career dates like this. And here not
[27:11:38] forget the comma. So execute again. So
[27:11:41] it looks wonderful. Now comes a very
[27:11:43] important decision about these objects.
[27:11:45] Is it a fact table or a dimension? Well,
[27:11:48] as we learned, dimensions hold
[27:11:50] descriptive informations about an
[27:11:52] object. And as you can see, we have here
[27:11:54] a descriptions about the customers. So
[27:11:56] all those columns are describing the
[27:11:59] customer information. And we don't have
[27:12:00] here like transactions and events. And
[27:12:03] we don't have like measures and so on.
[27:12:04] So we cannot say this object is a fact.
[27:12:07] It is clearly a dimension. So that's why
[27:12:10] we're going to go and call this object
[27:12:12] the dimension customer. Now there is one
[27:12:14] thing that if you are creating a new
[27:12:16] dimension you need always a primary key
[27:12:18] for the dimension. Of course we can go
[27:12:20] over here and depend on the primary key
[27:12:22] that we get from the source system but
[27:12:24] sometimes you can have like dimensions
[27:12:26] where you don't have like a primary key
[27:12:28] that you can count on. So what we have
[27:12:30] to do is to go and generate a new
[27:12:33] primary key in the data warehouse. And
[27:12:35] those primary keys we call it surrogate
[27:12:37] keys. Srogate keys are system generated
[27:12:40] unique identifier that is assigned to
[27:12:42] each records to make the record unique.
[27:12:45] It is not a business key. It has no
[27:12:47] meaning and no one in the business knows
[27:12:50] about it. We only use it in order to
[27:12:52] connect our data model. And in this way
[27:12:54] we have more control on how to connect
[27:12:56] our data model and we don't have to
[27:12:58] depend always on the source system. And
[27:13:01] there are different ways on how to
[27:13:02] generate surrogate keys like defining it
[27:13:05] in the DDL or maybe using the window
[27:13:07] function row number in this data
[27:13:09] warehouse. I'm going to go with a simple
[27:13:11] solution where we're going to go and use
[27:13:12] the window function. So now in order to
[27:13:15] generate a surrogate key for this
[27:13:17] dimension what we're going to do it is
[27:13:19] very simple. So we're going to say row
[27:13:21] number over and here we have to order by
[27:13:25] something. You can order by the create
[27:13:27] date or the customer ID or the customer
[27:13:30] number. whatever you want but in this
[27:13:32] example I'm going to go and order by the
[27:13:34] customer ID. So we have to follow the
[27:13:37] naming convention that all surrogate
[27:13:39] keys with a key at the end as a suffix.
[27:13:41] So now let's go and query those
[27:13:43] informations. And as you can see at the
[27:13:45] start we have a customer key and this is
[27:13:47] a sequence. We don't have here of course
[27:13:49] any duplicates. And now this target key
[27:13:52] is generated in the data warehouse and
[27:13:54] we're going to use this key in order to
[27:13:56] connect the data model. So now with that
[27:13:58] our query is ready and the last step is
[27:14:01] that we're going to go and create the
[27:14:03] object and as we decided all the objects
[27:14:05] in the gold layer going to be virtual
[27:14:07] one. So that means we're going to go and
[27:14:09] create a view. So we're going to say
[27:14:11] create view gold dot dim. So follow the
[27:14:15] naming convention stand for the
[27:14:17] dimension and we're going to have the
[27:14:18] customers and then after that we have
[27:14:20] ass. So with that everything is ready.
[27:14:23] Let's go and execute it. It was
[27:14:25] successful. Let's go to the views now
[27:14:27] and you can see our first objects. So we
[27:14:30] have the dimension customers in the gold
[27:14:32] layer. Now as you know me in the next
[27:14:34] step that we're going to go and check
[27:14:36] the quality of this new objects. So
[27:14:38] let's go and have a new query. So select
[27:14:41] star from our view temp customers. And
[27:14:45] now we have to make sure that everything
[27:14:47] in the right position like this. And now
[27:14:49] we can do different checks like the
[27:14:51] uniqueness and so on. But I'm worried
[27:14:54] about the gender information. So let's
[27:14:56] go and have a distinct of all values. So
[27:14:59] as you can see it is working perfectly.
[27:15:00] We have only female, male and not
[27:15:03] available. So that's it with that. We
[27:15:05] have our first new
[27:15:10] dimension. Okay friends. So now let's go
[27:15:12] and build the second object. We have the
[27:15:15] products. So as you can see product
[27:15:17] information is available in both source
[27:15:19] systems. As usual, we're going to start
[27:15:21] with the CRM informations and then we're
[27:15:23] going to go and join it with the other
[27:15:24] table in order to get the category
[27:15:26] informations. So those are the columns
[27:15:28] that we want from this table. Now we
[27:15:30] come here to a big decision about this
[27:15:33] objects. This object contains historical
[27:15:35] informations and as well the current
[27:15:37] informations. Now of course depend on
[27:15:38] the requirement whether you have to do
[27:15:40] analyszis on the historical
[27:15:42] informations. But if you don't have such
[27:15:43] a requirements we can go and stay with
[27:15:46] only the current informations of the
[27:15:48] products. So we don't have to include
[27:15:49] all the history in the objects and it is
[27:15:51] anyway as we learned from the model over
[27:15:53] here we are not using the primary key we
[27:15:56] are using the product key. So now what
[27:15:58] we have to do is to filter out the
[27:16:00] historical data and to stay only with
[27:16:02] the current data. So we're going to have
[27:16:04] here a wear condition. And now in order
[27:16:06] to select the current data what we're
[27:16:08] going to do we're going to go and target
[27:16:10] the end dates. If the end date is null
[27:16:12] that means it is a current data. Let's
[27:16:14] take this example over here. So you can
[27:16:16] see here we have three records for the
[27:16:18] same product key and for the first two
[27:16:21] records we have here an information in
[27:16:23] the end dates because it is historical
[27:16:25] informations but the last record over
[27:16:28] here we have it as a null and that's
[27:16:29] because this is the current information
[27:16:32] it is open and it's not closed yet. So
[27:16:34] in order to select only the current
[27:16:36] informations it is very simple we can
[27:16:38] say brd in dates is null. So if you go
[27:16:42] now and execute it, you will get only
[27:16:44] the current products. You will not have
[27:16:46] any history. And of course we can go and
[27:16:48] add comment to it. Filter out all
[27:16:51] historical data. And this means of
[27:16:54] course we don't need the end date in our
[27:16:56] selection of course because it is always
[27:16:58] a null. So with that we have only the
[27:17:01] current data. Now the next step is that
[27:17:03] we have to go and join it with the
[27:17:05] product categories from the ERP. And
[27:17:07] we're going to use here the ID. So as
[27:17:10] usual the master information is the CRM
[27:17:13] and everything else going to be
[27:17:14] secondary. That's why I use the lift
[27:17:17] join just to make sure I'm not losing
[27:17:19] I'm not filtering any data because if
[27:17:21] there is no match then we lose data. So
[27:17:24] lift join silver ERP and the category.
[27:17:27] So let's call it PC. And now what we're
[27:17:30] going to do we're going to go and join
[27:17:31] it using the key. So en from the CRM we
[27:17:35] have the category ID equal to PC ID. And
[27:17:39] now we have to go and pick columns from
[27:17:40] the second table. So it's going to be
[27:17:43] the PC. We have the category very
[27:17:45] important PC. We have the
[27:17:48] subcategory and we can go and get the
[27:17:50] maintenance. So something like this.
[27:17:53] Let's go and query. And with that we
[27:17:55] have all those columns comes from the
[27:17:58] first table and those three comes from
[27:18:00] the second. So with that we have
[27:18:01] collected all the product informations
[27:18:04] from the two source systems. Now the
[27:18:06] next step is we have to go and check the
[27:18:08] quality of these results. And of course
[27:18:10] what is very important is to check the
[27:18:13] uniqueness. So what we're going to do
[27:18:14] we're going to go and have the following
[27:18:16] query. I want to make sure that the
[27:18:19] product key is
[27:18:21] unique because we're going to use it
[27:18:23] later in order to join the table with
[27:18:25] the sales. So
[27:18:27] from and then we have to have group by
[27:18:30] product key and we're going to say
[27:18:32] having
[27:18:33] counts higher than one. So let's go and
[27:18:37] check. Perfect. We don't have any
[27:18:38] duplicates. The second table didn't
[27:18:41] cause any duplicates for our join. And
[27:18:43] as well this means we don't have
[27:18:45] historical data and each product is only
[27:18:47] one records and we don't have any
[27:18:49] duplicates. So I'm really happy about
[27:18:52] that. So let's go and query again. Now,
[27:18:54] of course, the next step, do we have
[27:18:56] anything to integrate together? Do we
[27:18:58] have the same information twice? Well,
[27:19:00] we don't have that. The next step is
[27:19:02] that we're going to go and group up the
[27:19:04] relevant informations together. So, I'm
[27:19:06] going to say the product ID, then the
[27:19:09] product key, and the product name are
[27:19:11] together. So, all those three
[27:19:13] informations are together. And after
[27:19:15] that, we can put all the category
[27:19:17] informations together. So, we're going
[27:19:18] to have the category ID, the category
[27:19:21] itself, the subcategory. Let me just
[27:19:23] query and see the results. So we have
[27:19:25] the product ID key name and then we have
[27:19:28] the category ID name and the subcategory
[27:19:31] and then maybe as well to put the
[27:19:33] maintenance after the subcategory like
[27:19:36] this and I think the product cost and
[27:19:38] the line can start could stay at the
[27:19:40] end. So let me just check. So those
[27:19:42] three four informations about the
[27:19:44] category and then we have the cost line
[27:19:46] and the start date. I'm really happy
[27:19:48] with that. The next step we're going to
[27:19:49] go and give nice names, friendly names
[27:19:52] for those columns. So let's start with
[27:19:54] the first one. This is the product ID.
[27:19:57] The next one going to be the product
[27:19:59] number. We need the key for the
[27:20:01] surrogate key later. And then we have
[27:20:04] the product name. And after that we have
[27:20:07] the category ID and the category. And
[27:20:11] this is the subcategory. And then the
[27:20:14] next one going to stay as it is. I don't
[27:20:16] have to rename it. The next one going to
[27:20:18] be the cost and the product line and the
[27:20:22] last one going to be the start stage. So
[27:20:25] let's go and execute it. Now we can see
[27:20:27] very nicely in the output all those
[27:20:29] friendly names for the columns and it
[27:20:32] looks way nicer than before. I don't
[27:20:34] have even to describe those informations
[27:20:36] the name describe it. So perfect. Now
[27:20:38] the next big decision is what do we have
[27:20:40] here? Do we have a fact or dimension?
[27:20:42] What do you think? Well, as you can see
[27:20:44] here again, we have a lot of
[27:20:46] descriptions about the products. So all
[27:20:48] those informations are describing the
[27:20:51] business object products. We don't have
[27:20:53] like here transactions, events, a lot of
[27:20:56] different keys and ids. So we don't have
[27:20:58] really here facts. We have a dimension.
[27:21:01] Each row is exactly describing one
[27:21:03] object, describing one product. That's
[27:21:06] why this is a dimension. Okay. So now
[27:21:08] since this is a dimension, we have to go
[27:21:10] and create a primary key for it. Well,
[27:21:12] actually the surrogate key and as we
[27:21:14] have done it for the customers, we're
[27:21:16] going to go and use the window function
[27:21:18] row number in order to generate it over
[27:21:21] and then we have to sort the data. I
[27:21:22] will go with the start date. So let's go
[27:21:25] with the start dates and as well the
[27:21:28] product key and we're going to give it a
[27:21:31] name products key like this. So let's go
[27:21:35] and execute it. With that, we have now
[27:21:37] generated a primary key for each product
[27:21:40] and we're going to be using it in order
[27:21:42] to connect our data model. All right.
[27:21:44] Now, the next step with that, we're
[27:21:45] going to go and build the view. So,
[27:21:47] we're going to say create view. We're
[27:21:50] going to say gold and dimension products
[27:21:53] and then us. So, let's go and create our
[27:21:56] object. And now, if you go and refresh
[27:21:58] the views, you will see our second
[27:22:00] object, the second dimension. So, we
[27:22:02] have here in the gold layer the
[27:22:04] dimension products. And as usual, we're
[27:22:06] going to go and have a look to this view
[27:22:09] just to make sure that everything is
[27:22:10] fine. So dem products. So let's execute
[27:22:14] it. And by looking to the data
[27:22:16] everything looks nice. So with that we
[27:22:18] have now two
[27:22:23] dimensions. All right friends. So with
[27:22:25] that we have covered a lot of stuff. So
[27:22:27] we have covered the customers and the
[27:22:29] products and we are left with only one
[27:22:31] table where we have the transactions the
[27:22:33] sales and for the sales information we
[27:22:36] have only data from the CRM. We don't
[27:22:37] have anything from the ERP. So let's go
[27:22:39] and build it. Okay. So now I have all
[27:22:41] those informations and now of course we
[27:22:43] have only one table. We don't have to do
[27:22:45] any integrations and so on. And now we
[27:22:47] have to answer the big question. Do we
[27:22:48] have here a dimension or a fact? Well by
[27:22:51] looking to those details we can see
[27:22:53] transactions. We can see events. We have
[27:22:55] a lot of dates, informations. We have as
[27:22:58] well a lot of measures and metrics and
[27:23:00] as well we have a lot of ids. So it is
[27:23:03] connecting multiple dimensions. And this
[27:23:05] is exactly a perfect setup for effect.
[27:23:08] So we're going to go and use those
[27:23:09] informations as a facts. And of course
[27:23:11] as we learned a fact is connecting
[27:23:13] multiple dimensions. We have to present
[27:23:16] in this fact the surrogate keys that
[27:23:18] comes from the dimensions. So those two
[27:23:20] informations the product key and the
[27:23:23] customer ID those informations comes
[27:23:25] from the source system and as we learned
[27:23:27] we want to connect our data model using
[27:23:29] the surrogate keys. So what we're going
[27:23:31] to do we're going to replace those two
[27:23:32] informations with the surrogate keys
[27:23:34] that we have generated and in order to
[27:23:36] do that we have to go and join now the
[27:23:39] two dimensions in order to get the
[27:23:42] surrogate key and we call this process
[27:23:44] of course data lookup. So we are joining
[27:23:46] the tables in order only to get one
[27:23:48] information. So let's go and do that. We
[27:23:50] will go with a lift join of course not
[27:23:52] to lose any transaction. So first we're
[27:23:55] going to go and join it with the product
[27:23:56] key. Now of course in the silver layer
[27:23:58] we don't have any surrogate keys. We
[27:24:01] have it in the gold layer. So that means
[27:24:03] for the fact table we're going to be
[27:24:05] joining the silver layer together with
[27:24:07] the gold layer. So, gold dots and then
[27:24:09] the dimension products and I'm going to
[27:24:12] just call it PR. And we're going to join
[27:24:15] the SD using the product key together
[27:24:18] with the product
[27:24:20] number from the dimension. And now the
[27:24:23] only information that we need from the
[27:24:25] dimension is the key, the surrogate key.
[27:24:28] So, we're going to go over here and say
[27:24:30] product key. And what I'm going to do,
[27:24:32] I'm going to go and remove this
[27:24:34] information from here because we don't
[27:24:36] need it. We don't need the original
[27:24:37] product key from the source system. We
[27:24:39] need the surrogate key that we have
[27:24:41] generated in our own in this data
[27:24:42] warehouse. So the same thing going to
[27:24:44] happen as well for the customer. So gold
[27:24:48] dimension customer again we are doing
[27:24:51] here a lookup in order to get the
[27:24:53] information on SD. So we are joining
[27:24:56] using this ID over here equal to the
[27:25:01] customer ID because this is a customer
[27:25:03] ID. And what we're going to do the same
[27:25:06] thing we need the surrogate key the
[27:25:09] customer key and we're going to delete
[27:25:11] the ID because we don't need it. Now we
[27:25:12] have the surrogate key. So now let's go
[27:25:14] and execute it. And now with that we
[27:25:17] have in our fact table the two keys from
[27:25:19] the dimensions. And now this can help us
[27:25:21] to connect the data model to connect the
[27:25:24] facts with the dimensions. So this is
[27:25:26] very necessary step building the fact
[27:25:28] table. You have to put the surrogate
[27:25:30] keys from the dimensions in the facts.
[27:25:32] So that was actually the hardest part
[27:25:34] building the facts. Now the next step
[27:25:36] all what you have to do is to go and
[27:25:37] give friendly names. So we're going to
[27:25:39] go over here and say order number. Then
[27:25:42] the surrogate keys are already friendly.
[27:25:44] So we're going to go over here and say
[27:25:46] this is the order date. And the next one
[27:25:49] going to be shipping date. And then the
[27:25:53] next one due age and the sales going to
[27:25:56] be I'm going to say sales
[27:25:59] amount the
[27:26:01] quantity and the final one is the price.
[27:26:05] So now let's go and execute it and look
[27:26:07] to the results. So now as you can see
[27:26:09] the columns looks very friendly and now
[27:26:11] about the order of the columns we use
[27:26:13] the following schema. So first in the
[27:26:15] fact table we have all the surrogate
[27:26:17] keys from the dimensions. Then second we
[27:26:19] have all the dates and at the end you
[27:26:22] group up all the measures and the
[27:26:24] metrics at the end of the fact. So
[27:26:26] that's it for the query for the facts.
[27:26:28] Now we can go and build it. So we're
[27:26:30] going to say
[27:26:31] create view gold in the gold layer and
[27:26:35] this time we're going to use the fact
[27:26:37] underscore and we're going to go and
[27:26:38] call it sales and then don't forget
[27:26:40] about the ass. So that's it. Let's go
[27:26:42] and create it. Perfect. Now we can see
[27:26:45] the fact. So with that we have three
[27:26:47] objects in the go there. We have two
[27:26:49] dimensions and one facts. And now of
[27:26:51] course the next step with that we're
[27:26:52] going to go and check the quality of the
[27:26:55] view. So let's have a simple
[27:26:58] select fact sales. So let's execute it.
[27:27:02] Now by checking the result you can see
[27:27:03] it is exactly like the result from the
[27:27:06] query and everything looks nice. Okay.
[27:27:08] So now one more trick that I usually do
[27:27:10] after building effect is try to connect
[27:27:13] the whole data model in order to find
[27:27:14] any issues. So let's go and do that. We
[27:27:16] will do just simple lift join with the
[27:27:19] dimensions. So gold dimension customers
[27:27:23] see and we will use the
[27:27:28] keys and then we're going to say where
[27:27:31] customer key is null. So there is no
[27:27:33] matching. So let's go and execute it.
[27:27:36] And with that as you can see in the
[27:27:37] results we are not getting anything that
[27:27:39] means everything is matching perfectly
[27:27:42] and we can do as well the same thing
[27:27:44] with the products. So left join called
[27:27:48] then products p on product key and then
[27:27:53] we connect it with the fact product key
[27:27:56] and then we going go and check the
[27:27:58] product key from the dimension like
[27:28:00] this. So we are checking whether we can
[27:28:02] connect the fact together with the
[27:28:04] dimension products. Let's go and check
[27:28:06] and as you can see as well we are not
[27:28:08] getting anything and this is all right.
[27:28:09] So with that we have now SQL codes that
[27:28:12] is tested and as well creating the gold
[27:28:15] layer. Now in the next step as you know
[27:28:17] in our requirements we have to make
[27:28:19] clear documentations for the end users
[27:28:21] in order to use our data model. So let's
[27:28:23] go and draw a data model of the star
[27:28:28] schema. So let's go and draw our data
[27:28:31] model. Let's go and search for a table.
[27:28:34] And now what I'm going to do, I'm going
[27:28:35] to go and take this one where I can say
[27:28:37] what is the primary key and what is the
[27:28:40] foreign key. And I'm going to go and
[27:28:41] change a little bit the design. So it's
[27:28:43] going to be rounded. And let's say I'm
[27:28:45] going to go and change to this color.
[27:28:46] And maybe go to the size, make it 16.
[27:28:49] And then I'm going to go and select all
[27:28:51] the columns and make it as well 16 just
[27:28:54] to increase the size. And then go to our
[27:28:57] range and we can go and increase it 39.
[27:29:00] So now let's go and zoom in a little bit
[27:29:02] for the first table. Let's go and call
[27:29:04] it gold dimension customers and make it
[27:29:08] a little bit bigger like this. And now
[27:29:10] we're going to go and define here the
[27:29:11] primary key. It is the customer key. And
[27:29:14] what else we're going to do? We're going
[27:29:15] to go and list all the columns in the
[27:29:16] dimension. It is a little bit annoying
[27:29:18] but the result is going to be awesome.
[27:29:19] So what do we have? The customer ID. We
[27:29:21] have the customer number and then we
[27:29:25] have the first name. Now in case you
[27:29:28] want a new rows so you can hold control
[27:29:31] and enter and you can go and add the
[27:29:33] other columns. So now pause the video
[27:29:35] and then go and create the two
[27:29:36] dimensions the customers and the
[27:29:38] products and add all the columns that
[27:29:40] you have built in the
[27:29:43] [Music]
[27:29:45] view. Welcome back. So now I have those
[27:29:48] two dimensions. The third one going to
[27:29:50] be the fact table. Now for the fact
[27:29:52] table I'm going to go with different
[27:29:54] color. for example, the blue and I'm
[27:29:56] going to go and put it in the middle.
[27:29:58] Something like this. So, we're going to
[27:29:59] say gold fact sales and here for that we
[27:30:04] don't have primary key. So, we're going
[27:30:05] to go and delete it. And I have to go
[27:30:07] and add all the columns of the facts.
[27:30:09] So, order number, products key, customer
[27:30:13] key. Okay. All right. Perfect. Now, what
[27:30:15] we can do, we can go and add the foreign
[27:30:17] key information. So, the product key is
[27:30:19] a foreign key for the products. So,
[27:30:21] we're going to say FK1. And the customer
[27:30:23] key going to be the foreign key for the
[27:30:25] customers. So FK2 and of course you can
[27:30:27] go and increase the spacing for that.
[27:30:30] Okay. So now after we have the tables
[27:30:31] the next step in data modeling is to go
[27:30:34] and describe the relationship between
[27:30:35] these tables. This is of course very
[27:30:37] important for reporting and analytics in
[27:30:39] order to understand how I'm going to go
[27:30:41] and use the data model. And we have
[27:30:43] different types of relationships. We
[27:30:44] have one to one, one to many. And in
[27:30:46] star schema data model the relationship
[27:30:48] between the dimension and the fact is
[27:30:51] one to many. And that's because in the
[27:30:52] table customers we have for a specific
[27:30:55] customer only one record describing the
[27:30:57] customer but in the fact table the
[27:30:59] customer might exist in multiple records
[27:31:01] and that's because customers can order
[27:31:04] multiple times. So that's why in fact it
[27:31:06] is many and in the dimension side it is
[27:31:09] one. Now in order to see all those
[27:31:10] relationships we're going to go to the
[27:31:12] menu to the left side and as you can see
[27:31:13] we have here entity relations and now we
[27:31:16] have different types of arrows. So for
[27:31:18] example we have zero to many, one to
[27:31:20] many, one to one and many different
[27:31:22] types of relations. So now which one we
[27:31:24] going to take? We're going to go and
[27:31:26] pick this one. So it says one mandatory.
[27:31:28] So that means the customer must exist in
[27:31:31] the dimension table. Too many but it is
[27:31:33] optional. So here we have three
[27:31:34] scenarios. The customer didn't order
[27:31:36] anything or the customer did order only
[27:31:38] once or the customer did order many
[27:31:41] things. So that's why in the fact table
[27:31:43] it is optional. So we're going to take
[27:31:45] this one and place it over here. So
[27:31:47] we're going to go and connect this part
[27:31:50] to the customer dimension and the many
[27:31:52] parts to the facts. Well actually we
[27:31:55] have to do it on the customers. So with
[27:31:57] that we are describing the relationship
[27:31:59] between the dimensions and fact with one
[27:32:01] to many. One is mandatory for the
[27:32:03] customer dimension and many is optional
[27:32:06] to the facts. So we have the same story
[27:32:07] as well for the products. So the many
[27:32:10] part to the facts and the one goes to
[27:32:14] the products. So it's going to look like
[27:32:15] this. Each time you are connecting new
[27:32:17] dimension to the fact table, it is
[27:32:19] usually one to many relationship. So you
[27:32:22] can go and add anything you want to this
[27:32:24] model like for example a text like
[27:32:26] explaining something. For example, if
[27:32:27] you have some complicated calculations
[27:32:29] and so on, you can go and write this
[27:32:31] information over here. So for example,
[27:32:33] we can say over here sales calculation,
[27:32:36] we can make it a little bit smaller. So
[27:32:38] let's go with 18. So we can go and write
[27:32:41] here the formula for that. So sales
[27:32:43] equal quantity multiplied with the price
[27:32:47] and make this little bit bigger. So it
[27:32:50] is really nice info that we can add it
[27:32:53] to the data model and even we can go and
[27:32:55] link it to the column. So we can go and
[27:32:57] take this arrow for example put it like
[27:32:59] this and link it to the column and with
[27:33:02] that you have as well nice explanation
[27:33:04] about the business rule or the
[27:33:05] calculation. So you can go and add any
[27:33:07] descriptions that you want to the data
[27:33:09] model. Just to make it clear for anyone
[27:33:11] that is using your data model. So with
[27:33:13] that you don't have only like three
[27:33:15] tables in the database. You have as well
[27:33:17] like some kind of documentations and
[27:33:19] explanation. In one click we can see how
[27:33:21] the data model is built and how you can
[27:33:23] connect the tables together. It is
[27:33:26] amazing really for all users of your
[27:33:27] data model. All right. So now with that
[27:33:29] we have really nice data model. And now
[27:33:32] in the next step we're going to go and
[27:33:33] create quickly a data catalog.
[27:33:39] All right, great. So with that we have a
[27:33:41] data model and we can say we have
[27:33:42] something called a data products and we
[27:33:44] will be sharing this data product with
[27:33:47] different types of users and there is
[27:33:48] something that every data products
[27:33:51] absolutely needs and that is the data
[27:33:53] catalog. It is a document that can
[27:33:55] describe everything about your data
[27:33:57] model. columns, the tables, maybe the
[27:34:00] relationship between the tables as well.
[27:34:02] And with that, you make your data
[27:34:03] product clear for everyone. And it's
[27:34:05] going to be for them way easier to
[27:34:06] derive more insights and reports from
[27:34:09] your data product. And what is the most
[27:34:11] important one? It is time-saving because
[27:34:14] if you don't do that, what's going to
[27:34:15] happen? Each consumer, each user of your
[27:34:17] data product will keep asking you the
[27:34:19] same questions about what do you mean
[27:34:21] with this column? What is this table?
[27:34:23] How to connect the table A with the
[27:34:24] table P? and you will keep repeating
[27:34:26] yourself and explaining stuff. So
[27:34:28] instead of that you prepare a data
[27:34:30] catalog, a data model and you deliver
[27:34:32] everything together to the users and
[27:34:34] with that you are saving a lot of time
[27:34:36] and stress. I know it is annoying to
[27:34:38] create a data catalog but it is
[27:34:40] investments and best practices. So now
[27:34:42] let's go and create one. Okay. So now in
[27:34:44] order to do that I have created a new
[27:34:45] file called data catalog in the folder
[27:34:48] documents. And here what we're going to
[27:34:49] do is very straightforward. We're going
[27:34:50] to make a section for each table in the
[27:34:52] code layer. So for example we have here
[27:34:54] the table dimension customers. What you
[27:34:56] have to do first is to describe this
[27:34:58] table. So we are saying it stores
[27:35:00] details about the customers with the
[27:35:02] demographics and geographics data. So
[27:35:04] you give a short description for the
[27:35:06] table and then after that you're going
[27:35:07] to go and list all your columns inside
[27:35:09] this table and maybe as well the data
[27:35:11] type. But what is way important is the
[27:35:13] description for each column. So you give
[27:35:15] a very short description like for
[27:35:17] example here the gender of the customer.
[27:35:19] And now one of the best practices of
[27:35:21] describing a column is to give examples
[27:35:23] because you can understand quickly the
[27:35:25] purpose of the columns by just seeing an
[27:35:27] example. Right? So here we are saying we
[27:35:29] can find inside the male, female and not
[27:35:32] available. So with that the consumer of
[27:35:33] your table can immediately understand uh
[27:35:35] it will not be an M or an F. It's going
[27:35:38] to be a full friendly value without
[27:35:40] having them to go and query the content
[27:35:41] of the table. They can understand
[27:35:43] quickly the purpose of that column. So
[27:35:45] with that we have a full description for
[27:35:46] all the columns of our dimension. The
[27:35:49] same thing we're going to do for the
[27:35:50] products. So again, a description for
[27:35:52] the table and as well a description for
[27:35:54] each column and the same thing for the
[27:35:56] facts. So that's it. With that you have
[27:35:58] like a data catalog for your data
[27:36:00] products at the code layer. And with
[27:36:02] that the business user or the data
[27:36:04] analyst have better and clear
[27:36:05] understanding of the content of your
[27:36:07] code layer. All right my friends. So
[27:36:09] that's all for the data catalog. In the
[27:36:11] next step we're going to go back to DO
[27:36:13] where we're going to finalize the data
[27:36:15] flow diagram. So let's go.
[27:36:21] Okay. So now we're going to go and
[27:36:22] extend our data flow diagram, but this
[27:36:25] time for the gold layer. So now let's go
[27:36:27] and copy the whole thing from the silver
[27:36:29] layer and put it over here side by side.
[27:36:31] And of course we're going to go and
[27:36:33] change the coloring to the gold. And now
[27:36:35] we're going to go and rename stuff. So
[27:36:38] this is the gold layer. But now of
[27:36:40] course we cannot leave those tables like
[27:36:42] this. We have completely new data model.
[27:36:44] So what do we have over here? We have
[27:36:46] the fact sales, we have dimension
[27:36:50] customers, and as well we have dimension
[27:36:53] products. So now what I'm going to do,
[27:36:55] I'm going to go and remove all those
[27:36:56] stuff. We have only three tables. And
[27:36:58] let's go and put those three tables
[27:37:00] somewhere here in the center. So now
[27:37:02] what you have to do is to go and start
[27:37:03] connecting those stuff. I'm going to go
[27:37:05] with this arrow over here, direct
[27:37:07] connection, and start connecting stuff.
[27:37:10] So the sales details goes to the fact
[27:37:12] table. Maybe put the fact table over
[27:37:14] here. And then we have the dimension
[27:37:16] customer. This comes from the CRM
[27:37:19] customer info. And we have two tables
[27:37:22] from the ERP. It comes from this table
[27:37:24] as well. And the location from the ERP.
[27:37:27] Now the same thing goes for the
[27:37:30] products. It comes from the product info
[27:37:32] and comes from the categories from the
[27:37:35] ERP. Now, as you can see here, we have
[27:37:37] cross arrows. So what you can do, we can
[27:37:38] go and select everything and we can say
[27:37:40] line jumps with a gap. And this makes it
[27:37:43] a little bit like better in the visual
[27:37:45] for the arrows. So now for example if
[27:37:47] someone asks you where the data come
[27:37:49] from for the dimension products you can
[27:37:51] open this diagram and tell them okay
[27:37:53] this comes from the server layer. We
[27:37:55] have like two tables. The product info
[27:37:57] from the CRM and as well the categories
[27:38:00] from the ERP and those several tables
[27:38:02] comes from the bronze layer and you can
[27:38:04] see the product info comes from the CRM
[27:38:06] and the category comes from the ERP. So
[27:38:09] it is very simple. We have just created
[27:38:11] a full data lineage for our data
[27:38:12] warehouse from the sources into the
[27:38:15] different layers in our data warehouse
[27:38:16] and data lineage is this really amazing
[27:38:18] documentation that can help not only
[27:38:20] your users but as well the developers.
[27:38:22] All right. So with that we have very
[27:38:23] nice data flow diagram and a data
[27:38:25] lineage. All right. So we have completed
[27:38:27] the data flow. It's really feel like
[27:38:29] progress like achievements as we are
[27:38:31] clicking through all those tasks. And
[27:38:33] now we come to the last task in building
[27:38:35] the data warehouse where we're going to
[27:38:37] go and commit our work in the get
[27:38:42] repo. Okay. So now let's put our scripts
[27:38:44] in the project. So we're going to go to
[27:38:46] the scripts over here. We have here
[27:38:48] bronze silver but we don't have a gold.
[27:38:49] So let's go and create a new file. We're
[27:38:51] going to have gold/ and then we're going
[27:38:53] to say ddl gold.sql. So now we're going
[27:38:56] to go and paste our views. So we have
[27:38:59] here our three views. And as usual at
[27:39:01] the start we can describe the purpose of
[27:39:03] the views. So we are saying create gold
[27:39:05] views. This script can go and create
[27:39:07] views for the code layer and the code
[27:39:09] layer represent the final dimension and
[27:39:11] fact tables. The star schema each view
[27:39:13] perform transformations and combination
[27:39:15] data from the server layer to produce
[27:39:17] business ready data sets and those views
[27:39:19] can be used for analytics and reporting.
[27:39:21] So that's it. Let's go and commit it.
[27:39:24] Okay. So with that as you can see we
[27:39:26] have the bronze the silver. So we have
[27:39:28] all our ETLs and scripts in the
[27:39:31] repository. And now as well for the code
[27:39:33] layer, we're going to go and add all
[27:39:35] those quality checks that we have used
[27:39:37] in order to validate the dimensions and
[27:39:39] facts. So we're going to go to the test
[27:39:41] over here and we're going to go and
[27:39:42] create a new file. It's going to be
[27:39:44] quality checks gold and the file type is
[27:39:47] SQL. So now let's go and paste our
[27:39:50] quality checks. So we have the check for
[27:39:51] the fact, the two dimensions and as well
[27:39:54] an explanation about the script. So we
[27:39:56] are validating the integrity and the
[27:39:58] accuracy of the go layer. And here we
[27:40:00] are checking the uniqueness of the
[27:40:01] surrogate keys and whether we are able
[27:40:03] to connect the data model. So let's put
[27:40:05] that as well in our git and commit the
[27:40:08] changes. And in case we come up with a
[27:40:09] new quality checks, we're going to go
[27:40:11] and add it to our script here. So those
[27:40:13] checks are really important if you are
[27:40:15] modifying the ATLs or you want to make
[27:40:17] sure that after each those script should
[27:40:19] run and so on. It is like a quality gate
[27:40:22] to make sure that everything is fine in
[27:40:24] the gold layer. Perfect. So now we have
[27:40:26] our code in our repository. Okay
[27:40:28] friends. So now what you have to do is
[27:40:30] to go and finalize the get repo. So for
[27:40:32] example all the documentations that we
[27:40:34] have created during the projects we can
[27:40:37] go and upload them in the docs. So for
[27:40:38] example you can see here the data
[27:40:40] architecture the data flow data
[27:40:42] integration data model and so on. So
[27:40:44] that each time you edit those pages you
[27:40:46] can commit your work and you have like a
[27:40:48] version of that. And another thing that
[27:40:50] you can do is that you go to the readme
[27:40:53] like for example over here I have added
[27:40:54] the project overview some important
[27:40:56] links and as well the data architecture
[27:40:59] and a little description of the
[27:41:00] architecture of course and of course
[27:41:02] don't forget to add few words about
[27:41:04] yourself and important profiles in the
[27:41:06] different social medias. All right my
[27:41:08] friends. So with that we have committed
[27:41:09] our work and as well closed the last
[27:41:12] epic building the god layer and with
[27:41:14] that we have completed all the phases of
[27:41:17] building a data warehouse. Everything is
[27:41:19] 100% and this feels really nice. All
[27:41:22] right my friends. So with that we have
[27:41:23] covered the first type of SQL projects
[27:41:26] that data warehousing projects. This is
[27:41:28] usually a very complex project that you
[27:41:30] can get involved in a company and this
[27:41:32] is really amazing project if you are
[27:41:33] planning to be a data engineer. But of
[27:41:35] course, if you are a data analyst, you
[27:41:37] might end up as well building
[27:41:38] warehouses. So now we have everything
[27:41:40] prepared for the second type of projects
[27:41:42] in SQL. We will deep dive now into the
[27:41:44] exploratory data analyzers. So let's
[27:41:50] go. And now here we're going to cover
[27:41:52] the second type of projects where we're
[27:41:54] going to use our basic SQL skills in
[27:41:56] order to do something called data
[27:41:58] profiling where we're going to try to
[27:41:59] understand all the aspects of our data
[27:42:02] sets using simple aggregations like the
[27:42:04] sum, average, count and as well we will
[27:42:06] be using techniques like some
[27:42:08] [Music]
[27:42:11] queries. All right my friends. So the
[27:42:13] first step in any data project is that
[27:42:15] we need data sets. If you have done the
[27:42:17] previous project where we have built the
[27:42:19] SQL data warehouse, then you have
[27:42:21] everything the data and the database. So
[27:42:23] you don't have to worry about it. But if
[27:42:25] you skip that, which I don't recommend,
[27:42:27] I still have prepared for you the files
[27:42:29] and the database. So let's get the data
[27:42:31] and create our database. All right. So
[27:42:32] now if you go to the link in the
[27:42:34] description, we're going to go to the
[27:42:35] downloads. And of course, you can
[27:42:37] subscribe to my newsletter. And then
[27:42:39] here we have the SQL course materials.
[27:42:41] And here we have a link for data
[27:42:42] analytics projects. Let's go to the
[27:42:44] link. And now here you have some
[27:42:45] important links like downloading the
[27:42:47] server the management studio where we're
[27:42:49] going to write our SQLs and as well
[27:42:51] there is a link to the g repository and
[27:42:53] as well what is very important is to
[27:42:55] download all the project files. So click
[27:42:57] on that and download all the files. Now
[27:42:59] extract the file and put it somewhere
[27:43:01] safe at your PC and now inside it you
[27:43:03] can find all the scripts and the data
[27:43:04] sets. Now there is like three ways on
[27:43:06] how to create the database in SQL
[27:43:08] server. So the first one is by executing
[27:43:11] scripts. If you go to the scripts over
[27:43:12] here, the first one we have a file
[27:43:14] called init database. Just go inside it
[27:43:17] and copy the whole thing and then let's
[27:43:19] go to SQL server. Now make a new query
[27:43:22] and make sure you switch to the master
[27:43:25] database and then paste the whole code.
[27:43:27] So now what you are doing here is we are
[27:43:29] creating a new database. We are creating
[27:43:31] a schema and then three very important
[27:43:34] tables that we're going to use in our
[27:43:36] data analyzes. Now there is like only
[27:43:38] one thing that you have to change in
[27:43:39] this script and that is the path of the
[27:43:42] files. And once you have done that just
[27:43:44] go and execute the whole script. And now
[27:43:47] as you can see everything is done and
[27:43:48] there is like data inserted. Now if you
[27:43:50] go to the left side to the database and
[27:43:52] refresh you can find a new database
[27:43:55] called data warehouse analytics. And if
[27:43:57] you go inside the tables you will find
[27:44:00] our three tables customer products and
[27:44:02] sales. So this is one way on how to
[27:44:04] create the database. The second methods
[27:44:06] is to go to the databases over here.
[27:44:08] Right click on it and say new database.
[27:44:11] And for example, let's call it data
[27:44:13] warehouse analytics. I'm going to call
[27:44:15] it two because I have already one. And
[27:44:17] then click okay. And with that you have
[27:44:19] a new database. So what we're going to
[27:44:21] do now, we're going to right click on it
[27:44:23] and then go to tasks and then import
[27:44:26] flat file. And now what we're going to
[27:44:27] do, we're going to go and import the CSV
[27:44:30] files to our new database. So we can go
[27:44:32] next and then you have to go and locate
[27:44:34] your files. I have them somewhere over
[27:44:37] here. So data set CSV files and we have
[27:44:40] to focus on the gold tables. So I'm
[27:44:43] going to go and select this one and then
[27:44:44] next. Now I'm just getting an overview
[27:44:46] of my data. So next. Now just to make
[27:44:49] sure that you are not getting any error,
[27:44:51] I'm going to go and allow nulls and
[27:44:53] that's all. So next and finish. So
[27:44:56] perfect. The data has been inserted. Now
[27:44:58] let's go to our database tables. And as
[27:45:01] you can see, we have here our new table.
[27:45:03] So you have to go and repeat this three
[27:45:05] times in order to import the data. Well,
[27:45:07] you can use this method if the first
[27:45:09] method didn't work. But I really
[27:45:10] recommend you to use the script in order
[27:45:12] to create the database. The third way is
[27:45:14] to go and restore the database itself.
[27:45:17] Now how we're going to do it? We're
[27:45:18] going to go again to the data sets and
[27:45:20] as you can see we have here a database
[27:45:22] backup. So as you can see we have here a
[27:45:24] PAK file. So now what you have to do is
[27:45:26] to go and copy that and then we're going
[27:45:28] to go to the database location. So it
[27:45:30] really depend where you have installed
[27:45:32] the SQL server. So currently I have it
[27:45:34] here program files Microsoft SQL server
[27:45:36] and then the express MSSQL backup and
[27:45:40] you have to place the file over here. So
[27:45:42] I have it here data warehouse analytics
[27:45:44] backup. And now all what you have to do
[27:45:45] is to right click on the database and
[27:45:47] then say restore database and then we're
[27:45:50] going to go to the device three points
[27:45:52] and we're going to say add. And now you
[27:45:54] can see our database data warehouse
[27:45:56] analytics. Once we say okay and then
[27:45:58] okay and now since I have it already I
[27:46:01] will get an error but once I click okay
[27:46:03] the whole database can be restored
[27:46:05] without running any scripts. So those
[27:46:07] are the three ways on how to create the
[27:46:10] database of the projects and if you have
[27:46:11] built with me the data warehouse
[27:46:13] projects before you don't have to do it
[27:46:14] because we have built that together. So
[27:46:17] pause the video and get the data for the
[27:46:23] projects. All right my friends. So we're
[27:46:25] going to start with a secret, a little
[27:46:27] trick that I usually do by analyzing any
[27:46:30] data sets. So let's start with little
[27:46:31] coffee before we start. H this is really
[27:46:35] hot. Okay. So the secret says as I'm
[27:46:37] looking to any data sets in any
[27:46:39] projects, I see the data always divided
[27:46:41] between dimensions and measures.
[27:46:45] What truth? You take the blue pill, you
[27:46:48] take the red pill. All I'm offering is
[27:46:51] the truth. Nothing more.
[27:46:54] If you see your data like me as
[27:46:55] dimensions and measures, you can
[27:46:58] generate like endless amount of insights
[27:47:01] from any projects from any data sets and
[27:47:03] you will find me through the projects
[27:47:05] that I'm always speaking about measures
[27:47:07] and dimensions. So I'm going to show you
[27:47:09] how I usually do it. So now usually by
[27:47:11] looking to any data sets in any
[27:47:12] projects. So you have like multiple
[27:47:14] columns and rows here I see the data
[27:47:16] always splitted into two categories
[27:47:18] either a dimension or a measure. And now
[27:47:21] of course the question is here is my
[27:47:23] column a dimension or a measure? Well in
[27:47:26] order to assign it to one of those
[27:47:27] categories you have to ask the first
[27:47:30] question is it a numeric value? If it's
[27:47:32] not so you have like string or date or
[27:47:35] any other data type then it is a
[27:47:36] dimension and if it is yes in numeric
[27:47:39] then you have to ask the second question
[27:47:42] does it make sense to aggregate it. So
[27:47:44] if the answer for both questions is yes,
[27:47:46] it is numeric and it makes sense to
[27:47:49] aggregate it then it is a measure
[27:47:51] otherwise it is a dimension. Now let's
[27:47:53] practice and have some examples. So now
[27:47:55] by looking to the values of the column
[27:47:57] category you can see all the values are
[27:47:59] characters. So it is not numeric that
[27:48:02] means this column is a dimension. So it
[27:48:04] is very simple. Let's take another
[27:48:06] column. We have the sales amount. So now
[27:48:08] as you can see the values are numeric
[27:48:09] and as well it makes sense to aggregate
[27:48:12] those values. we can get the total sales
[27:48:14] or the average sales and so on. So it
[27:48:16] fulfill both of the conditions. It is
[27:48:18] numeric and it makes sense to aggregate
[27:48:20] it. That's why we say sales is a
[27:48:22] measure. Now if you're checking the
[27:48:24] values of the product name, you can see
[27:48:26] that all of them are characters and
[27:48:28] names. So it is not numeric. That means
[27:48:30] the product is a dimension. Moving on to
[27:48:32] the next one, we have the quantity. The
[27:48:34] values are numeric and as well it makes
[27:48:36] sense to aggregate it. Can summarize all
[27:48:38] those values to have the total quantity.
[27:48:40] So quantity is a measure. Now if you're
[27:48:42] looking to the values of the birth dates
[27:48:44] you can see this is a date information
[27:48:46] it is not numeric so that means it is a
[27:48:49] dimension right but if you calculate the
[27:48:51] age from the birth dates age of the
[27:48:54] customer going to be in numeric and it
[27:48:56] makes sense to aggregate it for example
[27:48:58] finding the average age of customers. So
[27:49:01] if we derive a numeric value from a
[27:49:04] dimension then we can use it as a
[27:49:06] measure. So age is measure and now we
[27:49:08] come to something really tricky. This is
[27:49:10] the ID. So for example if you are
[27:49:12] checking the customer ID you can see all
[27:49:14] those values are numeric. So the first
[27:49:17] condition is fulfilled. Now the very
[27:49:19] important question does it make sense to
[27:49:21] aggregate the ids? Well those ids are
[27:49:24] unique identifier for a customer and if
[27:49:26] you find like the average of that it is
[27:49:28] not like helpful right I cannot think of
[27:49:31] one use case of aggregating the customer
[27:49:34] ID like having the average of all those
[27:49:36] ids or summarizing the ids. So it makes
[27:49:39] no sense to aggregate it. That's why we
[27:49:42] can consider the ID of a customer as a
[27:49:44] dimension not as a measure. So as you
[27:49:46] can see it is very simple. If it is
[27:49:48] numeric and it makes sense to aggregate
[27:49:50] then it is measure otherwise it is a
[27:49:52] dimension. And this is the foundations
[27:49:55] of any data analytics. If you see your
[27:49:57] data as dimensions and measures you can
[27:50:00] generate a lot of use cases and insights
[27:50:03] from your data sets. Now I totally
[27:50:05] understand if you are still confused
[27:50:06] about dimensions and measures and you
[27:50:08] might be asking why do I need measures
[27:50:10] and dimensions. Well if you are doing
[27:50:12] any type of data analysis or you are
[27:50:14] exploring any data sets you will be end
[27:50:16] up always like grouping up the data by
[27:50:18] something like you are grouping the data
[27:50:20] by countries or grouping the data by for
[27:50:22] example products or categories. So we
[27:50:25] need dimensions to group up our data and
[27:50:28] in the other sides you will be asking
[27:50:30] questions like how much how many what is
[27:50:32] the total of something. So you always
[27:50:34] need to aggregate or calculate something
[27:50:36] right and for that you need the measure.
[27:50:38] So we need the measures in order to
[27:50:40] answer the question how many and how
[27:50:42] much and we need the dimensions in order
[27:50:44] to group up the data by something. So
[27:50:47] that's why almost in any type of data
[27:50:49] analyzes you need dimensions and
[27:50:51] measures and this going to be more clear
[27:50:53] as we progress in the projects. All
[27:50:54] right. So now I'm going to walk you
[27:50:56] through the project road map and I have
[27:50:58] split that into six steps. So we're
[27:51:00] going to do different types of
[27:51:01] explorations like the database
[27:51:03] dimensions, measures, dates and we're
[27:51:05] going to do some basics analyszis like
[27:51:07] the magnitude and the ranking. So let's
[27:51:09] start with the first step in our
[27:51:10] projects. We're going to do database
[27:51:16] exploration. So let's say that you have
[27:51:18] joined a team and you got an access to a
[27:51:20] database. The first thing that I usually
[27:51:22] do is that I explore the structure of
[27:51:24] the database just to have basic
[27:51:26] understandings about the database
[27:51:28] tables, the views, columns. Are we
[27:51:30] talking about like 10 tables, hundreds
[27:51:32] of tables? So it is just a few queries
[27:51:35] in order to say hello to the database.
[27:51:37] So now let's go to SQL and explore the
[27:51:39] database of our projects. So now how we
[27:51:41] going to do it? Either you go to the
[27:51:43] left side over here and start clicking
[27:51:44] the objects of your database and explore
[27:51:47] the tables, views, columns and so on. Or
[27:51:49] a better way that I usually do it that I
[27:51:51] explore the database using a query. So
[27:51:53] what we can do, we can go and select
[27:51:55] data from the system tables because the
[27:51:58] database stores metadata informations
[27:52:01] about our tables and objects. So we're
[27:52:03] going to target an information schema.
[27:52:06] This is an internal schema in the
[27:52:07] database where we have like multiple
[27:52:09] tables and views to explore the metadata
[27:52:12] and the structure of our database. So
[27:52:14] for example, we can go with the tables.
[27:52:16] So let's go and create it. And with that
[27:52:19] you have a list of tables and with that
[27:52:22] you can see multiple informations like a
[27:52:24] catalog, the schema and the table names
[27:52:27] and you can see over here the object
[27:52:28] type whether it is a table or a view. If
[27:52:31] you done the data warehouse project with
[27:52:32] me then you will find a lot of tables.
[27:52:35] But if you are just doing the data
[27:52:37] analyzes you will see only those three
[27:52:40] tables. So customers, products and
[27:52:42] sales. So with that we can see in our
[27:52:44] database there are like around 15 tables
[27:52:47] or three tables. Now in the output you
[27:52:49] can see the database name the schema and
[27:52:52] a list of all tables and of course don't
[27:52:54] forget that you are using the database
[27:52:57] that we created. So with that we have a
[27:52:59] nice quick list with all tables inside
[27:53:01] our database. Now the next step we can
[27:53:03] go and drill down and check what are the
[27:53:05] columns that we have inside our
[27:53:07] database. And for that we can as well
[27:53:08] target the same schema. So select star
[27:53:11] from information schema and it is very
[27:53:14] simple. So we're going to go to the
[27:53:15] table columns. So let's go and execute
[27:53:18] it. And now we will see a lot of
[27:53:20] informations over here. So we can see in
[27:53:22] our database we have around 101 columns.
[27:53:26] So that we can see all the columns
[27:53:28] available in our database. And what I
[27:53:30] usually do with that I go and select the
[27:53:33] columns only for specific table. So we
[27:53:35] can say where are table name
[27:53:38] equal let's get for example the
[27:53:40] dimension customers. So let's query the
[27:53:43] whole thing and with that we can see we
[27:53:46] have 10 columns inside this dimension
[27:53:48] and this is how the columns are sorted
[27:53:50] inside our table or view and we can see
[27:53:52] all the metadata informations about each
[27:53:55] column. So now as you can see we are now
[27:53:57] exploring the structure of our database
[27:54:00] and this is really helpful to get an
[27:54:02] overview of the database and the
[27:54:03] projects. Are we talking about like 20
[27:54:06] tables or hundreds of tables? And we can
[27:54:08] quickly see the naming of the columns,
[27:54:10] the tables. This is really important to
[27:54:13] get a feeling about the projects and
[27:54:15] sets the foundations for exploring the
[27:54:18] data inside those tables. All right
[27:54:20] friends, so with that we have done the
[27:54:21] first step. We have explored the
[27:54:23] database structure and now we can start
[27:54:25] diving into the actual data. The first
[27:54:27] thing that we can explore is the
[27:54:29] dimensions.
[27:54:34] Okay. So what we going to do with the
[27:54:35] dimension exploration? All what we have
[27:54:36] to do is to go and identify the unique
[27:54:39] values of each dimension that we have
[27:54:41] inside our database. This can help us to
[27:54:43] understand what are the categories,
[27:54:45] which countries, what are the product
[27:54:47] types that we have inside our database
[27:54:49] and we have a very simple formula for
[27:54:51] that. So all what you need is the SQL
[27:54:53] keyword distinct together with any
[27:54:55] dimension in your data set like distinct
[27:54:57] country, distinct category. So for
[27:54:59] example if you are checking any column
[27:55:00] that is dimension you can see a lot of
[27:55:02] values and repeating stuff but now once
[27:55:04] you say distinct column what going to
[27:55:06] happen you will get a list of all unique
[27:55:08] values and with that you can understand
[27:55:10] quickly I have three different types so
[27:55:12] I have a bc and this as well going to
[27:55:14] help you to understand the granularity
[27:55:16] of your dimension does the dimension has
[27:55:19] like three values or 100 value so it is
[27:55:21] very simple let's go and analyze our
[27:55:23] dimensions okay so now let's explore the
[27:55:25] dimension values inside our database so
[27:55:27] let's start with the first table the
[27:55:29] customers and if you check those columns
[27:55:31] we have to find an interesting dimension
[27:55:33] like for example the country. So now
[27:55:34] what we can do we can go and explore all
[27:55:36] the countries our customers come from.
[27:55:39] So let's go and do that. It is very
[27:55:41] simple. Select distinct and then we have
[27:55:43] our column the dimension country from
[27:55:45] our table customers. So let's go and
[27:55:48] execute it. And with that we can see in
[27:55:50] the result we have six countries. This
[27:55:52] is really nice in order to understand
[27:55:54] the geographical spread. So we have
[27:55:56] customers for our business that comes
[27:55:58] from six different countries. Germany,
[27:56:00] United States, France, Canada and so on.
[27:56:02] So now with that we have like the first
[27:56:04] little insights about our business. Now
[27:56:06] let's jump to another table the
[27:56:08] products. So what we have to do is to
[27:56:10] explore all the categories inside our
[27:56:12] business the major divisions. So we're
[27:56:14] going to say select distinct category
[27:56:17] from our table products. So let's go and
[27:56:20] execute it. Now in the output you can
[27:56:22] see we have four categories. We have the
[27:56:24] accessories, bikes, clothing and
[27:56:26] components. This is like giving us an
[27:56:28] overview of the product range. What are
[27:56:30] the major divisions inside our business?
[27:56:33] Now the next one I'm digging deeper in
[27:56:35] this information. So not only I want to
[27:56:37] see the categories, I would like as well
[27:56:39] to see the
[27:56:40] subcategories. I'm not starting a new
[27:56:42] query because there is of course
[27:56:44] relationship between the category and
[27:56:45] the subcategory. Let's go now and
[27:56:47] execute it. Now you can see in the
[27:56:48] output our categories are now splitted
[27:56:51] into more specific groups. So for
[27:56:54] example the bikes over here we have
[27:56:56] mountain bikes, road bikes and so on. So
[27:56:58] as you can see the subcategories has
[27:57:00] more details about the products than the
[27:57:03] category. And now in order to get the
[27:57:05] full picture we going to bring now the
[27:57:07] product name. So with that we're going
[27:57:10] to get a big picture in one shot. So now
[27:57:13] you can see the whole hierarchy of our
[27:57:15] products. And of course it is more
[27:57:17] interesting if you go and sort the data
[27:57:19] by those three informations. So let me
[27:57:22] just execute it again. So now if you go
[27:57:23] and explore our data for example we have
[27:57:25] here the category accessories and we
[27:57:27] have a subcategory inside it called
[27:57:29] lights. And in this subcategory we have
[27:57:31] three different products. And if you
[27:57:33] scroll to the end of our table you can
[27:57:35] see that we have around
[27:57:37] 295 products. So you can see the
[27:57:40] granularity of the product name is
[27:57:42] different than the category and the
[27:57:43] subcategory. And all those three
[27:57:45] informations are related to each others.
[27:57:47] So now as you can see after exploring
[27:57:49] those dimensions we have now better
[27:57:51] understanding on how the data is
[27:57:54] organized and this can help us by the
[27:57:55] analyzes if you are aggregating by the
[27:57:58] category you will get only four rows. If
[27:58:00] you are aggregating by the products you
[27:58:02] will get hundreds of rows. So this is
[27:58:04] how we explore the dimensions of our
[27:58:07] database. Okay. So now with that we have
[27:58:09] a clear picture about the dimensions
[27:58:10] inside our data sets. And now in the
[27:58:12] next step we're going to deep dive into
[27:58:13] one special type of dimensions. We have
[27:58:16] the dates. So we're going to explore the
[27:58:18] date
[27:58:21] columns. Okay. So now what we going to
[27:58:23] do with the date exploration? We're
[27:58:25] going to go and explore the boundaries
[27:58:27] of the dates that we have in the data
[27:58:29] sets. What is the earliest and the
[27:58:31] latest dates in my data? We're going to
[27:58:33] understand the time span. Do we have in
[27:58:35] our business 2 years or like 10 years?
[27:58:38] And this is of course very important to
[27:58:40] understand in order later to make
[27:58:41] different types of time analyzes. Now
[27:58:44] the formula for that is very simple. All
[27:58:46] what we need is the min and max
[27:58:48] functions in order to get the earliest
[27:58:50] and the latest dates. And of course
[27:58:51] we're going to apply that on date
[27:58:53] columns, date dimensions. So for
[27:58:55] example, we're going to have like min
[27:58:56] order date, max create date, min birth
[27:58:59] date. So any date that you have in your
[27:59:01] data set. And here if you look to any
[27:59:02] date column inside your data, you will
[27:59:04] find multiple values. But what is
[27:59:06] interesting is to understand what is the
[27:59:08] earliest date like here for example 2018
[27:59:11] and what is the latest date for example
[27:59:14] 2028 and with that we can understand aha
[27:59:17] we have like time span of 10 years using
[27:59:20] the date diff function. So now let's go
[27:59:22] and apply our new formula on our date
[27:59:24] columns. All right. So now let's search
[27:59:26] for date informations inside our
[27:59:28] database. And usually you're going to
[27:59:30] find a lot in the facts. So let's go to
[27:59:32] the fact cells. And here we have like
[27:59:33] multiple dates. the order date, shipping
[27:59:35] date and due dates. Now let's go and
[27:59:37] explore the boundaries of the order
[27:59:39] date. So we have the following task.
[27:59:40] Find the date of the first and last
[27:59:43] order. So how we going to do that? We're
[27:59:45] going to say select and we are targeting
[27:59:47] the order date from our table sales. So
[27:59:51] let's go and execute it. And now we can
[27:59:53] see we have a lot of values inside our
[27:59:56] database. So now in order to find the
[27:59:58] first dates, what we're going to do,
[27:59:59] we're going to go and use the function
[28:00:01] min in order to get the minimum order
[28:00:04] dates. So we're going to go and call it
[28:00:06] first order dates. So let's go and
[28:00:09] execute it. So now we can see the date
[28:00:11] of the first order. It is in December
[28:00:13] 2010. Now let's go and find the date of
[28:00:16] the last order. So we're going to have
[28:00:18] this time the max order date. Uh let's
[28:00:22] go and call it last order date. So let's
[28:00:26] go and explore now the other boundary
[28:00:28] and with that we can see in January 2014
[28:00:31] it is the date of the last order in our
[28:00:34] system. So with that we have explored
[28:00:36] the boundaries of the order dates the
[28:00:38] first and the last and of course we can
[28:00:40] now understand very quickly that we have
[28:00:43] four years of sales inside our business
[28:00:45] but we can go and calculate it. So now
[28:00:47] the task says how many years of sales
[28:00:49] are available. Now in order to find the
[28:00:52] years between those two dates, we have
[28:00:54] another scale function. It's called date
[28:00:56] diff. And now we have to go and subtract
[28:00:58] two dates. Now this function need three
[28:01:00] arguments. The first one you have to
[28:01:02] specify whether it is a year, month and
[28:01:04] day. And we start with the smallest
[28:01:06] date. So it's going to be the min order
[28:01:09] dates. And then the last argument is
[28:01:11] going to be the latest or the highest
[28:01:13] date. And it's going to be the max order
[28:01:15] dates. And we can go and call it order
[28:01:18] range in years. Okay. So let's go and
[28:01:21] execute it. And with that you can see in
[28:01:23] the output we have four years. Of course
[28:01:25] if you want to go and check the months
[28:01:27] you can go over here and say month and
[28:01:29] execute. So between those two dates we
[28:01:31] have 37 months. And of course now we
[28:01:34] have to go and rename it. So with that
[28:01:36] we have explored the dimension order
[28:01:39] dates. But what is more interesting is
[28:01:41] to check the customers and here we have
[28:01:44] the birth date. So now what we can do,
[28:01:46] we can go and find the youngest and the
[28:01:49] oldest customer. So let's go and do
[28:01:51] that. We're going to say select
[28:01:53] minates and with that we are getting the
[28:01:56] oldest birth date and we will get now
[28:01:59] the max birth date and with that we will
[28:02:02] get the youngest birth date from our
[28:02:06] table customers. So let's go and explore
[28:02:09] that. Now we can see the birth date of
[28:02:11] the oldest customer. I hope he or she is
[28:02:14] still alive. So it is more than 100
[28:02:16] years and the youngest customer is
[28:02:19] around like 40 years. So we don't have
[28:02:22] really young customers inside our
[28:02:23] business. And of course if you don't
[28:02:25] want to see the birth dates, you want to
[28:02:27] see the age, what you have to do is
[28:02:29] actually very simple. You're going to
[28:02:30] use as well diff and we want the year
[28:02:33] and then we're going to say min birth
[28:02:36] date with the current date and time. And
[28:02:39] for that we have a function called get a
[28:02:41] date and we're going to call it oldest
[28:02:44] age. So if you go ahead and execute this
[28:02:47] one over here you can see the age of the
[28:02:49] oldest customer it is 109. Of course you
[28:02:52] can do the same thing for the youngest.
[28:02:54] If you just replace this with max and
[28:02:57] here we have the youngest age. So let's
[28:03:00] go and execute it. It is 39. So my
[28:03:03] friends this is how we explore the
[28:03:05] boundaries of a date and by finding the
[28:03:08] first date and the last date and the
[28:03:10] years between them we are having now
[28:03:12] more understanding of the time span of
[28:03:14] our business and that's going to help us
[28:03:16] later by making different type of
[28:03:18] complex analyzers. So this is how we
[28:03:20] explore the dates. All right. So with
[28:03:22] that we have now a clear picture about
[28:03:24] the scope of our projects and the date
[28:03:26] range inside our data sets. Now in the
[28:03:28] next step, we're going to go and explore
[28:03:30] the second type of data, the
[28:03:35] measures. All right. So now what is
[28:03:37] exactly exploring the measures? What
[28:03:39] we're going to do is to calculate and
[28:03:41] find out the key metrics of our
[28:03:44] business, the big numbers, the highest
[28:03:46] level of aggregations of our data. And
[28:03:48] the formula for that is very simple.
[28:03:50] We're going to go and use the aggregate
[28:03:52] functions in SQL like the sum, average,
[28:03:55] count for any measure inside our data
[28:03:57] sets. So for example, we're going to
[28:03:59] find the total sales by summarizing the
[28:04:01] sales value, finding the average price,
[28:04:04] finding the sum of quantity in order to
[28:04:06] have a big number about all sold items.
[28:04:09] So always an aggregate function together
[28:04:11] with a measure. So for example, if you
[28:04:12] have a column where you have a lot of
[28:04:14] values and you go and summarize all
[28:04:16] those values, you will get for example
[28:04:18] 240. So this is a key metric. This is
[28:04:21] the highest level of aggregations and
[28:04:23] the value is not splitted at all. So for
[28:04:25] example, we say this is the total
[28:04:27] revenue of our business. And this is
[28:04:29] exactly what we mean by exploring the
[28:04:31] measures. We will get those big numbers.
[28:04:33] So now let's go and apply those
[28:04:34] aggregate functions to the measures that
[28:04:36] we have inside our data set. Okay. So
[28:04:38] now we're going to go and spotlight on
[28:04:40] the big numbers that matters the most of
[28:04:42] our business. So now based on those
[28:04:44] three tables, I have collected here the
[28:04:47] following questions. So let's go and
[28:04:48] solve them one by one. The first one is
[28:04:50] find the total sales. So we're going to
[28:04:53] go and summarize by using the sum
[28:04:55] function for the sales amount as total
[28:04:59] sales from our table fact sales. So
[28:05:03] let's go and execute it. So this is the
[28:05:06] total amount of sales in our business.
[28:05:08] It is around 29 millions. So this is the
[28:05:12] business total revenue. Now we can go to
[28:05:14] the second one. It says show how many
[28:05:17] items are sold. So this time we need
[28:05:19] another column but from the same table
[28:05:22] from the fact sales. So the question is
[28:05:24] how many items that means we want the
[28:05:26] quantity and we're going to stay with
[28:05:28] the same function. So we are summarizing
[28:05:30] all the values of the quantity and we
[28:05:32] can call it total quantity. Let's go and
[28:05:36] explore that. So we can see our business
[28:05:38] did sold around 60,000 items and these
[28:05:42] 60,000 items did generate around 30
[28:05:44] million. So let's keep going. The next
[28:05:46] question, find the average selling
[28:05:48] price. So that means we are targeting
[28:05:50] the same table. And here we have the
[28:05:52] price informations. So we're going to
[28:05:54] say the price. This time the aggregate
[28:05:56] function going to be the average. And
[28:05:57] we're going to call it average price. So
[28:06:00] let's go and execute it. So the average
[28:06:02] price in our business is 486. So that
[28:06:05] means our business is selling like
[28:06:07] expensive items. Now let's go to the
[28:06:09] next question. It says find the total
[28:06:12] number of orders. And for that we're
[28:06:14] going to go and use the function count
[28:06:16] and we can count the order numbers. So
[28:06:20] order number total orders let's go and
[28:06:24] execute it. So it says we have 60,000
[28:06:27] orders. And now as you are working with
[28:06:28] the count function what I usually do I
[28:06:31] try to count the same thing but using a
[28:06:34] distinct. So distinct order number. So,
[28:06:37] what I'm trying to do here is first
[28:06:40] eliminate any duplicates in the order
[28:06:42] number and then count it. I don't want
[28:06:44] to count the same order twice inside our
[28:06:47] sales. So, let's go and execute that.
[28:06:50] Now, as you can see, we have only 27,000
[28:06:53] orders out of 60,000. So, that means the
[28:06:57] same order is repeating in our database.
[28:07:00] Let's have actually a look. So, select
[28:07:02] star from our table and let's go and
[28:07:06] have a look. Now as you can see from the
[28:07:07] first order over here you can see the
[28:07:09] same order is repeated three times and
[28:07:12] that's because this customer did order
[28:07:14] three things in the same order. So now
[28:07:17] of course what is the definition of
[28:07:19] order? Usually the whole thing is one
[28:07:21] order. That's why in order to get an
[28:07:23] accurate number of orders you have to go
[28:07:25] and use a distinct in order to eliminate
[28:07:28] first all duplicates and then count how
[28:07:30] many orders we have. So in this scenario
[28:07:32] I'm going to say in our business we have
[28:07:34] around 27,000 orders. So that's why it
[28:07:37] is little bit tricky using the count
[28:07:39] function. Always try to compare the
[28:07:41] numbers before and after using distinct.
[28:07:44] So let's keep going to the next one. It
[28:07:46] says find the total number of products.
[28:07:48] So it is very simple. We're going to say
[28:07:50] select count and we're going to say
[28:07:52] product key as total products from the
[28:07:57] table gold products. So let's go and
[28:08:00] execute it. So as you can see we have
[28:08:02] 295 and if you go and make it distinct
[28:08:06] just to check you will get the same
[28:08:09] number. So that means there is no
[28:08:10] duplicates and of course you can go and
[28:08:12] count the product name instead. The
[28:08:14] names of the product is unique. So
[28:08:15] that's why we are as well getting the
[28:08:18] same numbers. So that's it. Let's
[28:08:20] continue find the total number of
[28:08:22] customers. So the same thing select
[28:08:25] count and you can go with a customer key
[28:08:27] for example from called a dimension
[28:08:30] customers and I'm going to call it as
[28:08:33] total customers. So let's go and execute
[28:08:36] it. So we can see in our system we have
[28:08:38] 18,000 registered customers. Now the
[28:08:41] next one it says find the total number
[28:08:43] of customers that has placed an order.
[28:08:45] So that means having a customer inside
[28:08:48] our database doesn't mean that this
[28:08:50] customer did already placed an order.
[28:08:52] Maybe we have customer that just
[28:08:54] registered and didn't order anything. So
[28:08:56] what we're going to do, we're going to
[28:08:57] take the same query, but instead of
[28:08:59] targeting the customers table, we're
[28:09:01] going to target our fact the sales. So
[28:09:04] let's go and execute it. So now, as you
[28:09:06] can see, we are getting 16,000, which
[28:09:08] makes no sense because one customer
[28:09:10] might order multiple stuff. So what
[28:09:12] we're going to do, we're going to say
[28:09:14] distinct and let's query it again. So
[28:09:16] now it is more correct. We are getting
[28:09:18] around 18,000 customers. Now we can go
[28:09:21] and compare them one by one. So as you
[28:09:23] can see we are getting the same numbers.
[28:09:25] So that means all our registered
[28:09:26] customers did already placed an order
[28:09:29] because the numbers are matching. So it
[28:09:31] is very simple. We are just using an
[28:09:33] aggregate functions and that we are
[28:09:35] getting those key values. But what I
[28:09:37] usually do is that I collect all those
[28:09:39] measures in one query in order to have
[28:09:41] an overview of all key numbers in our
[28:09:44] business. So instead of me querying each
[28:09:46] one of them individually, I combine them
[28:09:49] in one go. So now what we're going to
[28:09:50] do, we're going to generate a report
[28:09:52] that shows all key metrics of our
[28:09:54] business. So how I usually do it, I'm
[28:09:56] going to go and get the first query for
[28:09:57] the total sales and put it over here.
[28:09:59] And now I'm going to build only two
[28:10:01] columns. The first one is the name of
[28:10:03] the measure and the second one is the
[28:10:05] value of the measure. So let me show you
[28:10:07] what I mean. Now this one over here, I
[28:10:09] will not call it total sales. I'm going
[28:10:11] to make it like generic. So I'm going to
[28:10:13] say measure value. And before it we're
[28:10:16] going to make another column from a
[28:10:18] static string value is the total sales
[28:10:21] and we're going to call it measure name
[28:10:23] like this. So let's go and just execute
[28:10:26] this one over here. So the measure is
[28:10:28] total sales. So it is not anymore like
[28:10:31] the column name. It is now a value in
[28:10:33] the output and the measure value is like
[28:10:35] around 30 millions. Now what I'm going
[28:10:37] to do I'm going to go and add another
[28:10:39] measure as a second row. And in order to
[28:10:41] do that, we're going to use the union
[28:10:43] all and then copy the whole thing over
[28:10:45] here and say total quantity and we're
[28:10:48] going to change the measure to quantity.
[28:10:51] So now let's select both of them and
[28:10:53] query. And now as you can see we have
[28:10:55] now the two big numbers in one query. So
[28:10:58] the total sales and the total quantity.
[28:11:00] So now what we can do we can go and
[28:11:01] collect all those big numbers and
[28:11:03] measures and put it in one query. So
[28:11:06] with that we have the average price, the
[28:11:07] total number of orders, product,
[28:11:09] customers and as well you can go and
[28:11:11] target different tables because SQL
[28:11:13] cares here only about the number of
[28:11:15] columns and the data type of columns
[28:11:17] must be matching. So now let's go and
[28:11:20] query this and now in single query we
[28:11:23] can see the big numbers the key metrics
[28:11:26] of our business. We can see the total
[28:11:28] sales, total quantity, average price and
[28:11:30] so on. This is a super report where you
[28:11:32] can generate it for any business where
[28:11:34] you have in one go the full big picture
[28:11:37] about the business. So this is how I
[28:11:39] generally do if I'm exploring a new
[28:11:40] database. I put all those big numbers
[28:11:43] and measures in one query to have better
[28:11:46] understanding about the business. All
[28:11:47] right my friends. So with that we have
[28:11:49] now a clear understanding about the
[28:11:51] dimensions and as well the measures of
[28:11:53] our data sets. Now in the next step
[28:11:55] we're going to go and start combining
[28:11:57] stuff together in order to generate
[28:11:58] insights. And we're going to focus now
[28:12:00] in a very basic analyszis. It is the
[28:12:02] magnitude
[28:12:07] analyzis. Okay. So now what is exactly a
[28:12:09] magnitude analyszis? It's all about
[28:12:11] comparing the measure values across
[28:12:13] different categories and dimensions. And
[28:12:15] this can help us of course to understand
[28:12:17] the importance of different categories.
[28:12:20] Now the formula for that going to be
[28:12:21] interesting. So now this time we will be
[28:12:23] mixing stuff together. So first we have
[28:12:25] to go and aggregate a specific measure
[28:12:27] and then we say by dimension. We need
[28:12:30] here the dimension in order to split the
[28:12:32] measure. It sounds complicated but it is
[28:12:34] very simple and basics. So for example
[28:12:36] we can say the total sales by country,
[28:12:39] the total quantity by category, the
[28:12:41] average price by products, the total
[28:12:44] orders by customer and if you follow
[28:12:46] this formula you will be generating
[28:12:48] endless amount of insights by just
[28:12:50] combining any measure with any
[28:12:51] dimension. You can call it it is a new
[28:12:53] insight. So it's going to look like like
[28:12:55] this. If you have one measure that is
[28:12:57] like for example 600 and if you put now
[28:12:59] this measure together with dimension
[28:13:01] what's going to happen this 600 is going
[28:13:03] to be splitted by the dimension values.
[28:13:05] So A going to have like 200, B going to
[28:13:07] have 300 and C 100. And now with that we
[28:13:10] can go and compare those categories
[28:13:12] right. So we can see now that category B
[28:13:15] has the highest measure and the C has
[28:13:17] the lowest. And this help us to compare
[28:13:19] the values of the measure. what is the
[28:13:20] best category and what is the worst
[28:13:22] category. So this is very basics
[28:13:24] analyszis. So let's go and apply this
[28:13:26] formula on our data sets. Okay. So now
[28:13:29] let's go and break all our measures by
[28:13:31] dimensions. So here I have prepared few
[28:13:33] interesting examples where first we're
[28:13:35] going to break the total number of
[28:13:37] customers. As we learned we have 18,000
[28:13:39] by the countries. So the measure is
[28:13:42] total customers and the dimension going
[28:13:44] to be the countries. So let's go and
[28:13:45] write the query for that. So we're going
[28:13:47] to select. So the first thing that we're
[28:13:48] going to go and add is the dimension. So
[28:13:50] it's going to be the country. And then
[28:13:52] we need the measure. It's going to be
[28:13:54] the count of the customer key. So this
[28:13:58] will give us the total customers. And we
[28:14:01] need to select our table. So it's going
[28:14:03] to be the dimension customers. And of
[28:14:05] course we have to go and group up the
[28:14:07] data by the countries. So group up
[28:14:10] country. So let's go and execute it. And
[28:14:13] with that you see again the list of
[28:14:15] countries. So we have our six countries
[28:14:18] and then the total customers for each
[28:14:20] country. So that we can see the
[28:14:22] distribution of customers by the
[28:14:24] country. But what we usually do is that
[28:14:26] we go and sort the data by the measure
[28:14:28] the total customers like this. And we're
[28:14:31] going to sort it by descending. So with
[28:14:33] that we will get first the countries
[28:14:35] with the highest customers. So let's go
[28:14:38] and execute it. So now we can see in the
[28:14:40] results the highest number of customers
[28:14:42] come from United States then Australia,
[28:14:44] United Kingdom
[28:14:46] 337 customers without the country
[28:14:50] informations it is not available. So
[28:14:52] that's it right it is very simple. So
[28:14:54] with that we have splitted the total
[28:14:56] number of customers by a dimension the
[28:14:58] country. Now of course we can go and
[28:15:00] split the data by different type of
[28:15:02] dimension. So for the next one we are
[28:15:04] saying find the total customers by
[28:15:07] gender. So here's the same thing. We
[28:15:08] have the same measure that to other
[28:15:10] customers but we are splitting the data
[28:15:12] by different type of dimension. So just
[28:15:14] copy and paste and now instead of
[28:15:16] countries we just going to switch it to
[28:15:18] gender and over here and that's it. So
[28:15:21] let's go and execute. So now as you can
[28:15:23] see the granularity of the gender over
[28:15:26] here is different than the countries. We
[28:15:27] have here only three values and we can
[28:15:30] see it is almost splitted evenly between
[28:15:32] male customers and female customers. And
[28:15:34] of course this going to help us to
[28:15:36] understand the demography of our
[28:15:38] customers. And as you can see it was
[28:15:40] very simple. We just switch the
[28:15:42] dimension. So you can go and split as
[28:15:44] well by the marital status and so on.
[28:15:46] Now let's go and split the total
[28:15:49] products by the category. Well actually
[28:15:51] the query is going to be very simple as
[28:15:53] well. So select and here we're going to
[28:15:54] have the same aggregate function the
[28:15:56] count products key as total
[28:16:00] products from our table gold dimension
[28:16:04] products and then we're going to group
[28:16:05] up by the dimension the category and
[28:16:08] we're going to order by as well the same
[28:16:10] thing total products distinct from the
[28:16:13] highest to the lowest. So let's go and
[28:16:15] execute it. And with that we can see how
[28:16:17] many products do we have in each of
[28:16:19] those categories. And we can see the
[28:16:21] biggest category the components and
[28:16:23] after that the pikes. And this is
[28:16:25] interesting that we have seven products
[28:16:27] where we have nulls where they don't
[28:16:29] belong to any category. This is really
[28:16:31] nice. Let's go to the next one. What do
[28:16:33] we have over here? What is the average
[28:16:36] costs in each category? So this is like
[28:16:38] different style of question but at the
[28:16:40] ends we're going to have the same thing.
[28:16:41] We have over here the average costs.
[28:16:43] This is the measure and the category is
[28:16:46] our dimension. It's like we are saying
[28:16:48] find average costs by category. So what
[28:16:51] we're going to do, we're going to go and
[28:16:52] copy the same query and the dimension is
[28:16:54] the same. So the categories but the
[28:16:57] measure is different. We are not talking
[28:16:59] about the total products. We are going
[28:17:00] to say average and here we're going to
[28:17:02] have the column costs and let's go and
[28:17:04] rename it average costs. So that's it as
[28:17:07] well for the order by we have to use the
[28:17:10] new measure. So let's go and execute it.
[28:17:12] So now we can see the most expensive
[28:17:14] category is the bikes costs a lot
[28:17:17] compared to the accessories of course.
[28:17:19] So you can see the accessories is only
[28:17:21] 13 and the bikes is 900. So this is as
[28:17:24] well gives us insights about how
[28:17:26] expensive each category is and as you
[28:17:28] can see it is always the same templates.
[28:17:31] We are splitting specific measure by a
[28:17:33] dimension. So let's keep going to the
[28:17:35] next one. It says what is the total
[28:17:38] revenue generated for each category. So
[28:17:40] again here the question is find the
[28:17:43] total revenue by category. So the total
[28:17:46] revenue here is the measure and the
[28:17:48] category again is the dimension. So now
[28:17:50] the total revenue comes from the fact
[28:17:52] and the category comes this time from
[28:17:54] the dimension. So that means we have to
[28:17:56] go and join tables right. So how we
[28:17:58] going to do it? Let's go and start with
[28:17:59] the select star from and I would like
[28:18:02] always to start from the fact table. So
[28:18:05] fact sales f and then we're going to go
[28:18:07] and join it with the dimension and
[28:18:09] usually I go with the left join in order
[28:18:10] to not lose anything because if you use
[28:18:13] an inner join you might lose in the fact
[28:18:15] few orders and few sales I don't want
[28:18:17] that. So lift join with the dimension
[28:18:20] this one going to be the products and
[28:18:22] the key for that going to be very simple
[28:18:24] going to be the product key and the same
[28:18:26] thing for the facts. So with that we
[28:18:28] join the fact table with the dimension.
[28:18:30] So now we have to go and pick what do we
[28:18:32] need? We need from the fact the sales
[28:18:34] right. So sales amount and we need from
[28:18:37] the products the category and we want to
[28:18:40] group up the data by the category. So so
[28:18:43] this part is done. What is missing is of
[28:18:46] course the aggregations. So we are
[28:18:48] aggregating actually the sales. So sum
[28:18:51] sales and we can call it total revenue.
[28:18:54] So like this. And of course we can go
[28:18:57] and order the data by the total revenue
[28:18:59] by our measure and distinct from highest
[28:19:02] to the lowest. So as you can see it is
[28:19:03] exactly like the previous one. But here
[28:19:05] the data doesn't come from only one
[28:19:07] table. Here it comes from two tables. So
[28:19:09] the measure come from the facts and the
[28:19:11] dimension come from the dimension
[28:19:13] products. And this is classic right? The
[28:19:15] dimension has all those descriptions and
[28:19:18] details about the products like the
[28:19:20] categories. And the fact table has all
[28:19:22] those measures and dates that we use in
[28:19:25] order to calculate our measures. So
[28:19:27] that's it. Let's go and execute it. Now,
[28:19:29] as you can see in the output, the
[28:19:31] category bikes is bringing the most of
[28:19:33] revenue. So here it's like in millions
[28:19:36] 28 millions of sales and the accessories
[28:19:39] and the closing is not really bringing a
[28:19:41] lot of like revenue. Both of them are
[28:19:43] below like 1 million. So with that you
[28:19:45] can understand our business is making a
[28:19:47] lot of money selling bikes, right? So my
[28:19:50] friends as we are exploring the data we
[28:19:52] are understanding more and more about
[28:19:54] our business right so let's keep going
[28:19:56] to the next one we have here the
[28:19:57] question what is the total revenue
[28:19:59] generated by each customer so now we
[28:20:01] want to find out the top spender right
[28:20:04] select star and as well we start from
[28:20:06] the fact table and this time we're going
[28:20:10] to lift join it with the customers right
[28:20:12] so the dimension customers and we're
[28:20:15] going to go join the data so we're going
[28:20:17] to use the customer key for the join And
[28:20:19] what we're going to do, we're going to
[28:20:20] go and get maybe the customer key. And
[28:20:23] let's go and get as well the first name,
[28:20:25] maybe few details about the customer and
[28:20:28] as well the last name. So those are the
[28:20:30] columns that we want from the customers.
[28:20:32] And now what do we need? We need the
[28:20:34] aggregation. So it's going to be the
[28:20:35] same thing. Sales amount as total
[28:20:38] revenue. And we have to go and group up
[28:20:41] the data by all those three
[28:20:43] informations. So we're going to go and
[28:20:45] copy paste. And at the end as usual,
[28:20:47] we're going to order by the measure
[28:20:49] total revenue descending. So that's it.
[28:20:52] It is exactly like previous one but with
[28:20:54] different dimensions. So let's go and
[28:20:56] query it. And now we get a full list of
[28:20:59] all our customers, the 18,000s. And we
[28:21:02] can see the total revenue for each
[28:21:04] customer. So we can see Nicole and
[28:21:06] Caitlyn, they are our top spenders and
[28:21:09] the most royal customers that generated
[28:21:12] sales and revenue for our business. This
[28:21:14] is really cool. Right now let's go to
[28:21:16] the next one. It says what is the
[28:21:18] distribution of sold items across
[28:21:21] countries. It is like finding the total
[28:21:23] quantity by countries. So it is very
[28:21:26] simple. I'm going to go and take the
[28:21:27] same query because countries comes from
[28:21:29] the dimension customers and the sold
[28:21:32] items the quantity come from the sales.
[28:21:34] So we are doing the same joints but with
[28:21:36] different dimensions and measures. So
[28:21:38] what do we need from the customers is
[28:21:40] only the country and the measure going
[28:21:42] to be the quantity. And here we're going
[28:21:44] to go and say total sold items and we
[28:21:47] have to change the group by to the
[28:21:50] countries and sorting the data by the
[28:21:52] new measure. That's it. And with that we
[28:21:54] are generating new reports by just
[28:21:57] changing the dimensions and measures. So
[28:21:59] again this is very interesting to
[28:22:00] understand which country is generating
[28:22:02] like good business for us. So my friends
[28:22:04] as you might already noticed if in the
[28:22:06] dimension we have like small number of
[28:22:09] unique values like in the countries we
[28:22:11] have here only seven values in the
[28:22:12] gender we have only three we call those
[28:22:14] dimensions low cardality dimensions
[28:22:17] because we have low number of values
[28:22:19] inside it and in the result we will get
[28:22:21] only here for example seven rows but if
[28:22:23] our dimension is high cardality like by
[28:22:27] the customers we have 18,000 unique
[28:22:30] customers then our measure going to be
[28:22:32] splitted by those 18,000 and in the
[28:22:35] results we will get exactly the same
[28:22:37] number of customers. So the number of
[28:22:39] rows and results really depends on the
[28:22:41] cardality of the dimension. So as you
[28:22:43] can see we can generate a lot of
[28:22:45] different reports by only following this
[28:22:47] formula dividing the measure by a
[28:22:49] dimension. So we just generated eight
[28:22:52] different insights and reports by only
[28:22:54] few measures and dimensions. So now what
[28:22:56] you can do you can pause the video and
[28:22:57] try different dimensions and measures in
[28:23:00] order to have more insights about our
[28:23:02] business. Okay. So as you can see this
[28:23:04] is the basics analyszis that we can do
[28:23:06] in any data set or any domain where we
[28:23:08] are aggregating a measure by dimension.
[28:23:10] Now in the next and last step in our
[28:23:12] projects we will be doing ranking
[28:23:18] analyszis. Okay. So what is ranking
[28:23:20] analyszis? It is very basic. We're going
[28:23:22] to go and order the value of our
[28:23:24] dimension based on a measure in order to
[28:23:27] identify the top performers and as well
[28:23:29] the bottom performers. And the formula
[28:23:31] for that is going to be the following.
[28:23:33] So this time we're going to be ranking
[28:23:34] the dimensions by an aggregated measure.
[28:23:37] So for example, we're going to rank the
[28:23:38] countries by the total sale or we're
[28:23:40] going to find the top five products by
[28:23:42] the sold item, the quantity or the
[28:23:45] bottom three customers by total orders.
[28:23:47] So it's like the magnitude analyzes.
[28:23:49] We're going to have like an ordered list
[28:23:51] of dimensions value. For example, from
[28:23:53] the highest to the lowest in order to
[28:23:55] identify quickly the top performers. And
[28:23:57] of course we can go and filter the data
[28:23:59] by saying I would like to have only the
[28:24:00] top two categories. And with that you
[28:24:03] are removing all other dimensions that
[28:24:05] are not on the top two. And in SQL we
[28:24:07] can use for that the keyword top or we
[28:24:10] can use the ranking window functions
[28:24:12] like rank, dense rank, row number and so
[28:24:15] on. So let's go and apply our formula in
[28:24:17] order to rank our data set. Okay. So now
[28:24:20] let's check our data. We're going to
[28:24:22] start with the first question. Which
[28:24:23] five products generate the highest
[28:24:25] revenue? So we are searching for the
[28:24:27] best performing products in our
[28:24:29] business. So of course the first
[28:24:30] question what is the dimension and
[28:24:32] measure that we have in this question.
[28:24:34] Well the revenue that means we need the
[28:24:36] sales from the facts and the products
[28:24:38] that means we need the dimension
[28:24:40] products. Now in order to write this
[28:24:41] query it's going to be very simple. So
[28:24:43] we can use as well the group by I will
[28:24:45] not write it from the scratch. So I'm
[28:24:47] just going to take this query over here
[28:24:49] where we aggregated the total sales by
[28:24:51] the category. Now what I have to do is
[28:24:53] just to change the dimension. So instead
[28:24:56] of the category we need the product name
[28:24:59] and we are aggregating now the data by
[28:25:01] the product name because we need the top
[28:25:03] five products right. So the revenue is
[28:25:06] the sales amount and with that we have
[28:25:08] like almost everything is ready. So
[28:25:09] let's go and execute it. And now we can
[28:25:11] see we have a list of all products in
[28:25:13] our business and as well we can see the
[28:25:16] total revenue. But the task says here we
[28:25:18] need the top five. So we don't need all
[28:25:21] the products from our database. We have
[28:25:23] to go and select only this subset. Now
[28:25:25] in order to do that in SQL server, it's
[28:25:27] very simple. We're going to go over here
[28:25:28] and say top five and SQL going to go and
[28:25:31] return only the first five rows from the
[28:25:34] results. So let's go and execute it. And
[28:25:36] as you can see now in the results, we
[28:25:38] have only five products with the highest
[28:25:40] sales. And that's it. With that, we have
[28:25:42] solved the task and we can see the top
[28:25:44] five products and all of them are pikes.
[28:25:47] Now let's go and check the other sides.
[28:25:49] We want to find the five worst
[28:25:51] performing products by the same measure,
[28:25:53] the sales. And this is very simple. So
[28:25:56] what we're going to do, we're going to
[28:25:57] go and take the same query over here.
[28:25:59] And now what we're going to do, we're
[28:26:00] going to go and sort the data from the
[28:26:02] lowest to the highest. So instead of
[28:26:05] descending, we're going to remove it.
[28:26:07] And with that, SQL going to use the
[28:26:09] ascending. So let's go and execute it.
[28:26:11] And with that, as you can see, we are
[28:26:13] getting the worst five performing
[28:26:15] products by just sorting the data
[28:26:17] differently. So it is very simple right
[28:26:19] and with that we can see our five best
[28:26:21] sellers and the five worst sellers. And
[28:26:24] now what we can do we can go and just
[28:26:25] change the dimension and generate
[28:26:28] different reports like instead of the
[28:26:29] product name let's go and check the
[28:26:31] subcategories what are the best
[28:26:33] subcategories of our data. So I just
[28:26:35] change the dimension let's go and query.
[28:26:37] So with that we can see the best
[28:26:39] subcategories we have in our business
[28:26:41] and the same thing if you want to go and
[28:26:42] check the worst performing
[28:26:44] subcategories. So generating reports is
[28:26:46] very simple and now my friends in SQL
[28:26:48] there is like two ways on how to create
[28:26:50] ranking. We have a simple one where we
[28:26:53] are using the group by clouds together
[28:26:55] with the keyword top. But if you are
[28:26:57] generating a reports where it's things
[28:26:59] are more complex and you need more
[28:27:01] flexibility, you should use the window
[28:27:03] functions. So let me show you how I can
[28:27:05] solve this task using the window
[28:27:07] function. So now I'm going to go and
[28:27:08] take almost the same query. Let's put it
[28:27:11] over here. I'm going to get rid of the
[28:27:13] top five. And let's see, we are still
[28:27:15] speaking about the products name as well
[28:27:18] with a group I. But now what we're going
[28:27:20] to do, we're going to go and generate a
[28:27:21] rank. So we can go and use for example
[28:27:23] the row number. And in scale there's
[28:27:25] like different types of window functions
[28:27:27] for ranking. One of them is the row
[28:27:29] number or the rank and then we're going
[28:27:31] to say over. Now we're going to go and
[28:27:33] sort the data. It's like we have done in
[28:27:35] the previous one. We have to sort the
[28:27:37] data by the total revenue and the total
[28:27:39] revenue is the sum of sales and
[28:27:41] descending and we're going to call this
[28:27:43] rank products. So let's go and execute
[28:27:47] it. Now as you can see we have created a
[28:27:49] new column where we have like a rank. So
[28:27:52] we have for each products like one rank
[28:27:55] until the last
[28:27:56] products 130. So now what we are
[28:27:59] interested is to go and select the top
[28:28:01] five. Right now in order to do that we
[28:28:04] need a second step. That's why we're
[28:28:05] going to go and use the subquery. So
[28:28:07] we're going to say select star from and
[28:28:09] then we're going to put the whole thing
[28:28:11] in a subquery something like that. And
[28:28:14] all what you have to do is to use the
[28:28:16] new flag that we have created in order
[28:28:18] to filter the data. So we're going to
[28:28:20] say where the rank products is smaller
[28:28:22] or equal to five. And with that we
[28:28:25] should get only the top five products.
[28:28:28] So let's go and execute it. And as you
[28:28:30] can see we are getting the same results.
[28:28:32] Now, of course, with the window
[28:28:33] function, it is more complicated than
[28:28:35] the first one. But with the window
[28:28:36] function, we get more flexibility on
[28:28:38] selecting more columns or adding more
[28:28:41] different types of aggregations and
[28:28:42] details on the query. And as well, we
[28:28:44] can go and use different types of
[28:28:46] ranking functions that handles the tice
[28:28:49] differently. So, if the task is very
[28:28:51] simple like this, I'm going to go with
[28:28:53] the simple group pie. But if you are
[28:28:55] generating like complex reports, I'm
[28:28:57] going to go with the window function. So
[28:28:59] now what you can do, you can go and rank
[28:29:00] the data by different dimensions and
[28:29:02] measures. For example, find the top 10
[28:29:04] customers who have generated the highest
[28:29:06] revenue. And as well, you can go and
[28:29:08] find the three customers with the fewest
[28:29:11] orders placed. So again, we can go and
[28:29:14] reuse the previous queries that we have
[28:29:16] generated. So this query generates the
[28:29:18] customers and their total sales. And all
[28:29:20] what you have to do is to say top 10 and
[28:29:23] then rerun the query. And with that, we
[28:29:25] are getting the top 10 customers. and
[28:29:27] about the lowest three customers. All
[28:29:30] what we have to do is to go and replace
[28:29:32] the measure. So we are counting the
[28:29:36] unique number of orders. So we're going
[28:29:39] to say total orders and as well go
[28:29:42] change the order by not descending
[28:29:44] ascending. And we need the top three. So
[28:29:47] let's go and execute it. So we can see
[28:29:50] the three customers that did order only
[28:29:52] once and they are the three customers
[28:29:54] with the fewest orders. So as you can
[28:29:56] see by just switching the dimensions and
[28:29:58] measures we are generating completely
[28:30:00] new important insights and as you can
[28:30:02] see as we are exploring the data we are
[28:30:04] understanding what are the best products
[28:30:06] what are the top customers that are
[28:30:08] usually very important for reporting.
[28:30:11] All right my friends so with that we
[28:30:12] have covered the last step in our
[28:30:15] projects how to rank our data and with
[28:30:17] that we have covered all the steps of
[28:30:19] the project road map. We have done a lot
[28:30:21] of explorations for the database,
[28:30:22] dimensions, measures. We have combined
[28:30:25] the dimensions and measures in order to
[28:30:27] do magnitude and ranking analyszis.
[28:30:29] Okay, my friends. So that's all about
[28:30:31] the EDA projects. And now in the next
[28:30:33] one, we will do the last type of
[28:30:35] projects, the advanced data analytics.
[28:30:37] So let's
[28:30:42] go. And now the type that we're going to
[28:30:44] cover is advanced analytics projects
[28:30:46] using SQL where we're going to write
[28:30:48] complex SQL queries to answer real
[28:30:50] business questions. So we're going to
[28:30:52] use the advanced window functions, the
[28:30:54] CTE subqueries and we're going to go and
[28:30:56] script two big queries in order to
[28:30:59] generate two reports. So with this type
[28:31:01] of project, you will learn how to solve
[28:31:03] real business questions using advanced
[28:31:05] techniques. All right. So for this
[28:31:06] project as well, we have a road map
[28:31:08] where we're going to progress through
[28:31:10] different type of steps and analyzes. So
[28:31:12] we're going to do many stuff like change
[28:31:13] over time, cumulative analyszis,
[28:31:15] performance, data segmentations and at
[28:31:18] the end reporting and all using SQL. So
[28:31:20] let's start with the first step in the
[28:31:21] road map. We going to analyze the change
[28:31:24] over time. So let's
[28:31:28] go. Okay. So now what is change over
[28:31:31] time? It is a technique in order to
[28:31:33] analyze how a measure evolves over the
[28:31:35] time. And this is very important in
[28:31:37] order to track the trends and as well to
[28:31:40] identify seasonality of your data. And
[28:31:42] the formula for that is very simple.
[28:31:44] We're going to go and aggregate a
[28:31:45] measure but this time based on a date
[28:31:48] dimension. For example, the total sales
[28:31:50] by a year, the average cost by the
[28:31:53] month. So if you combine any aggregated
[28:31:55] measure together with a date column or
[28:31:57] dimension, then all what you are doing
[28:31:59] is you are analyzing the change over
[28:32:01] time. So for example, we're going to go
[28:32:03] and break our measure this time for
[28:32:04] example by the years. And with that we
[28:32:06] can track immediately how our business
[28:32:09] is doing over the time over the years.
[28:32:11] So for example, we can see here the best
[28:32:13] year was 2024 and then we have really
[28:32:15] hard decline in our business in 2025 and
[28:32:18] then slightly it's going up in 2026. So
[28:32:21] with that we can quickly analyze the
[28:32:23] trends of our business. So now let's go
[28:32:25] and check the trends and the changes
[28:32:27] over time in our business. Okay. So now
[28:32:29] let's analyze the trends and changes
[28:32:31] over time in our data and in order to do
[28:32:33] this kind of analyzes usually we target
[28:32:36] the fact table because there usually we
[28:32:38] have our measures and as well dates. So
[28:32:40] we have the order date, shipping date
[28:32:42] and due date. Now what we can do we can
[28:32:44] go and analyze there the sales
[28:32:46] performance over time. So as we learned
[28:32:48] all what we need is a metric and a date.
[28:32:51] Let's go for example and select the
[28:32:54] order date and as well one of those
[28:32:56] measures sales amount from our fact
[28:33:00] table. So let's go and query it. And we
[28:33:04] can go and order the data by the order
[28:33:06] dates ascending. So let's go and
[28:33:09] execute. And as you can see we have
[28:33:11] nulls in our data. What we can do? We
[28:33:13] can go and filter those data out. We
[28:33:15] don't need it. So we're going to say
[28:33:17] where order date is not null. So let's
[28:33:19] go and execute it again. All right. So
[28:33:22] that we don't have those orders. Now, as
[28:33:24] you can see, we have sales over time,
[28:33:26] right? We have a date and we have a
[28:33:28] measure. So this looks really good. But
[28:33:31] now what we're going to do, we're going
[28:33:31] to go and aggregate the data by the
[28:33:33] sales amount. So let's go and say sum.
[28:33:37] And we're going to call it total sales.
[28:33:39] And then we group up the data by the
[28:33:42] order dates. So let's go and execute it.
[28:33:45] And with that, as you can see, for each
[28:33:47] day, we have the total sales. So now the
[28:33:49] granularity of our data is the day and
[28:33:52] we can say of course now we are
[28:33:54] analyzing the sales over time but
[28:33:56] usually we don't aggregate the data on
[28:33:58] the day level we want to have higher
[28:34:00] aggregations for example let's go to the
[28:34:02] years and now in order to change the
[28:34:05] dimension date here from a day to a year
[28:34:07] we have to use date functions and there
[28:34:10] are a lot of date functions in order to
[28:34:12] extract that date part and now in order
[28:34:14] just to get the year we have a quick
[28:34:16] function called year and it going to
[28:34:18] convert convert our date to year. So
[28:34:21] let's call it order year and of course
[28:34:23] we have to go and group up the data by
[28:34:26] the year and as well sort it by the
[28:34:28] year. So let's go and execute. Now we
[28:34:30] are at the year level and we have only
[28:34:33] five years. So that means we have
[28:34:35] changed the aggregation from the day to
[28:34:37] year and now it is very easily to
[28:34:39] analyze the performance of our business
[28:34:42] over the years. So the first year was
[28:34:44] the lowest and you can see 2013 is the
[28:34:47] best year in our business and then it is
[28:34:50] declined massively in 2014. And of
[28:34:53] course we can go and add more measures
[28:34:55] to our data not only the total sales.
[28:34:57] For example, let's go and calculate the
[28:34:59] total number of customers. So we can say
[28:35:02] count distinct customer key as total
[28:35:06] customers. So let's go and execute it.
[28:35:09] And with that we can check are we
[28:35:10] gaining like customers over the time if
[28:35:13] there are any trends that we can see and
[28:35:15] we can go and keep extending stuff like
[28:35:17] we can go and add the total number of
[28:35:19] quantities. So summarize quantity as
[28:35:23] total quantity. So let's go and execute
[28:35:26] and with that we have really nice
[28:35:27] picture in order to understand is the
[28:35:30] revenue increasing or decreasing over
[28:35:32] the time what is the best year the worst
[28:35:35] year are we gaining customers over time
[28:35:37] if there any like trends that we can
[28:35:39] spot now by looking to the result you
[28:35:41] can see this gives us highlevel
[28:35:43] long-term view of your data and of
[28:35:45] course it helps for strategic decisions
[28:35:48] and now what we can do we can go and
[28:35:50] drill down to the months so we can go
[28:35:52] and aggregate the data by the month
[28:35:54] regardless list the years in order to
[28:35:56] give us an idea how each month is
[28:35:58] performing on average. So all what we
[28:36:00] have to do is to switch the function
[28:36:02] from year to a month like this. And of
[28:36:04] course for the group by and the order by
[28:36:06] let's go and execute and of course in
[28:36:09] the output we will get all the months
[28:36:11] and guess what which month is the best
[28:36:13] for sales is of course December because
[28:36:16] you have all those Christmas and stuff
[28:36:18] and the worst months as you can see is
[28:36:20] February. So with that we are
[28:36:22] understanding the seasonality of our
[28:36:24] business and the trends patterns of our
[28:36:26] business. And as you are not including
[28:36:28] the year in our analyzes you are
[28:36:30] aggregating all the data from all years.
[28:36:33] Now what we can do we can make it more
[28:36:35] specific for each year where you go and
[28:36:37] add the year informations to our query.
[28:36:40] So we can have both a year and months.
[28:36:42] Let me just change this to a month. And
[28:36:45] of course we have to go and add it to
[28:36:47] the group by and the order by. So let's
[28:36:50] go and execute and with that we are
[28:36:51] aggregating the data of a month of
[28:36:53] specific year. So now we have all the
[28:36:55] months of all years and now if you want
[28:36:58] to focus on only one year what you can
[28:37:00] do you can go and filter the data by the
[28:37:02] order year and with that you can see how
[28:37:04] the data is evolving over time. Now of
[28:37:06] course in SQL we can go and format the
[28:37:08] date differently. So instead of using
[28:37:11] the year and the month in separate
[28:37:13] columns what we can do we can use the
[28:37:15] date trunk function. So instead of here
[28:37:17] we're going to say date trunk and if you
[28:37:19] want the granularity of your date at the
[28:37:21] month level we're going to say month and
[28:37:23] then the date and with that you will get
[28:37:27] both the year and the date and let's
[28:37:30] call it order date like this. So let's
[28:37:32] go and execute. Now in the output we
[28:37:34] will get exactly the same result as
[28:37:36] before but instead of having like two
[28:37:38] columns for the year and the month we
[28:37:40] have everything in one and because we
[28:37:42] saved the month that means it's still
[28:37:44] going to go and remove all the days. So
[28:37:46] as you can see it always starts with the
[28:37:48] one. So the first day of the month and
[28:37:50] with that you will get one row for each
[28:37:53] month for each year. And if you want to
[28:37:55] change that quickly to a year just you
[28:37:57] go and change the date parts to a year
[28:38:00] and you will get the granularity of the
[28:38:01] year. Now if you don't like this format
[28:38:03] and you would like to have your specific
[28:38:05] format what you can do you can go and
[28:38:08] use the format function. So format the
[28:38:11] first argument is going to be the date
[28:38:13] and then you go and do your format that
[28:38:15] you want. So for example it start with
[28:38:17] the years and let's say I would like to
[28:38:19] have the abbreviation of the month name.
[28:38:22] So something like this and of course
[28:38:24] group by and order by. So let's go and
[28:38:27] execute it. And with that we got our
[28:38:29] format the year minus then the
[28:38:31] abbreviation of the month. But you have
[28:38:32] to be careful which function you are
[28:38:34] using because the format you will get in
[28:38:36] the output a string. And as you can see
[28:38:38] you cannot sort it correctly. So the
[28:38:41] data here is sorted by the year but not
[28:38:43] by the month. But if you are using date
[28:38:45] trunk you can see the data is correctly
[28:38:48] sorted. So if we switch it to a month it
[28:38:51] will be as well. Okay. So everything is
[28:38:53] sorted correctly because the output here
[28:38:55] is a date and SQL going to sort the date
[28:38:58] correctly. It is not string. And if you
[28:39:00] are using the year and the month the
[28:39:02] output here going to be an integer and
[28:39:05] sorting an integer is not a problem. So
[28:39:07] of course you can go and pick the one
[28:39:08] that you like. So that's it. Let's go
[28:39:11] and execute it. And now you can go and
[28:39:13] keep analyzing by finding another date
[28:39:15] in our data set and another measure. So
[28:39:18] as you can see it is very simple. Okay.
[28:39:20] So that's all about how to analyze the
[28:39:22] trends and the change over time. Now in
[28:39:24] the next step we're going to do some
[28:39:25] kind of advanced aggregations by doing
[28:39:27] cumulative
[28:39:32] analyszis. Okay. So what is cumulative
[28:39:34] analyszis? It is aggregating the data
[28:39:37] progressively over the time and this is
[28:39:39] very important technique in order to
[28:39:41] understand how our business is growing
[28:39:44] over the time. So how our business is
[28:39:46] progressing over the time whether it is
[28:39:48] growing or declining it is very
[28:39:50] interesting analyszis. So the formula
[28:39:52] going to be very similar to the changes
[28:39:54] over time but instead of having a simple
[28:39:56] aggregations on the measure we're going
[28:39:57] to aggregate our measure but this time
[28:39:59] cumulative. So we are like adding stuff
[28:40:01] on top of each others and the data again
[28:40:03] can split it by the date dimension cuz
[28:40:06] we want to track the progress over the
[28:40:08] time. For example, we can find the
[28:40:10] running total of sales or the moving
[28:40:12] average of sales by a month. So now
[28:40:15] let's have again our simple example
[28:40:16] where our sales is splitted by the
[28:40:18] years. Now this is the classic change
[28:40:20] over time. But in order now to make it
[28:40:22] cumulative what can happen? We're going
[28:40:24] to take the measure and add to it. For
[28:40:26] example, 2024 we have 300. And now for
[28:40:29] 2025, we're going to add the 300
[28:40:31] together with the 100 in order to make
[28:40:34] it cumulative. So for 2025, we're going
[28:40:36] to have 400. And the same thing for
[28:40:38] 2026, we're going to go and add the 400
[28:40:41] together with the 200. And with that, we
[28:40:43] will get 600. So as you can see, we are
[28:40:45] keep adding the values in order to
[28:40:47] generate something called cumulative
[28:40:49] value. Now for this type of analysis, we
[28:40:51] use in SQL the aggregate window
[28:40:53] functions. in order to find out the
[28:40:55] cumulative values. So now let's go and
[28:40:57] apply our formula in order to find
[28:40:59] whether our business is growing or
[28:41:01] declining. So let's go. Okay, so now we
[28:41:02] have to analyze the following. We're
[28:41:04] going to calculate the total sales for
[28:41:06] each month and as well the running total
[28:41:08] of sales over time in order to analyze
[28:41:10] the trends. So let's see how we're going
[28:41:12] to do that. Let's start with the easy
[28:41:14] stuff where we're going to calculate the
[28:41:15] total sales for each month. So we are
[28:41:17] calculating the changes over time and we
[28:41:18] have already done that. So all what we
[28:41:20] need is a date and a measure. Our date
[28:41:22] going to be the order date and the
[28:41:24] measure going to be the sales amount
[28:41:26] from our fact
[28:41:27] table. So let's query this. And now we
[28:41:30] want to find the total sales for each
[28:41:32] month. That means we're going to change
[28:41:33] the granularity of the order date from a
[28:41:35] day to a month. And I usually like using
[28:41:38] the date rank for this kind of tasks.
[28:41:40] And the granularity going to be the
[28:41:42] month. So this is the order
[28:41:44] dates. And now for the sales we're going
[28:41:47] to use aggregate function sum sales as
[28:41:49] total sales. And of course we have to go
[28:41:52] and group up the data by the
[28:41:54] date. So let's go and execute it. So as
[28:41:59] you can see we have now the total sales
[28:42:00] for each month. And don't forget to get
[28:42:03] rid of the nulls. So where we can say
[28:42:05] where order date is not null. Now it
[28:42:09] looks better. We don't have nulls. And
[28:42:10] of course we can go and order the data
[28:42:12] by our date. Now our measure is just
[28:42:15] aggregated for each month individually.
[28:42:17] Right? But we don't want that. We want
[28:42:19] to have like a running total. So we'd
[28:42:22] like to have like commumulative metric.
[28:42:24] In order to do that, we have to use
[28:42:26] window function. So let's go and do
[28:42:27] that. We will use a subquery for that.
[28:42:30] In order just to make it simple. So what
[28:42:32] we need? We need the order date and
[28:42:34] let's say the total sales and here we
[28:42:36] have to have our window
[28:42:39] function. Then we're going to put the
[28:42:41] rest in a subquery. And of course we can
[28:42:44] go and get rid of the order by because
[28:42:46] anyway our data going to be sorted using
[28:42:48] the window function. So now let's start
[28:42:49] writing our window function. We will
[28:42:52] have the sum of total sales. So we want
[28:42:56] to summarize those new values. And we're
[28:42:58] going to build a window function like
[28:43:00] this over. We don't have to go and
[28:43:01] partition anything. So we can go
[28:43:03] immediately and say order by our new
[28:43:06] order date that we have calculated. And
[28:43:08] we want it to be ascending. So actually
[28:43:10] that's it. So as running total sales. So
[28:43:15] let's try that out. Now if you look to
[28:43:16] the result you can see that all those
[28:43:19] values are cumulative and it is working
[28:43:21] like this. The first total sales is
[28:43:23] equal to the total sales because
[28:43:25] previously we don't have anything. Now
[28:43:27] for the next row what going to happen is
[28:43:29] going to go and add this value to the
[28:43:32] previous one. And with that we get the
[28:43:34] running total value. Now moving on to
[28:43:36] the third row is going to go and add all
[28:43:38] those three values together. And of
[28:43:40] course this going to give us the running
[28:43:42] total for this month and so on. So as
[28:43:44] SQL is moving through the window it is
[28:43:48] always adding the current value to all
[28:43:50] previous values. And this is because of
[28:43:52] the default frame of the window. The
[28:43:54] frame going to be between the unbounded
[28:43:57] preceding and the current row. So that
[28:43:59] means for example if we are at this row
[28:44:01] over here current total sales for this
[28:44:03] month is this one and the unbounded
[28:44:05] preceding is all the values before this
[28:44:08] month. So that means we are getting all
[28:44:10] the previous values together with the
[28:44:12] current value and with that we will get
[28:44:15] the effect of the running total sales.
[28:44:17] And now of course as you can see it is
[28:44:19] going through all the years. Right now
[28:44:22] we can go and limit the running total
[28:44:24] for only one year. So for each new year
[28:44:27] it has to reset and start from the
[28:44:29] scratch. So that means we are
[28:44:30] partitioning the data. For each year we
[28:44:33] would like to have partition. For the
[28:44:34] first year, it's going to be 2010. It is
[28:44:36] one row. And for the 2011, we're going
[28:44:39] to get the whole partition over here.
[28:44:41] So, in order to partition our window,
[28:44:43] it's very simple. We're going to go and
[28:44:44] say partition by the order date. That's
[28:44:49] it. Let's go and execute it. Now, let's
[28:44:51] go and check for the first partition for
[28:44:53] 2010. You can see the running total is
[28:44:55] the same as the first month. But since
[28:44:57] we have only one month, that's it for
[28:44:59] this year. Now, as we go to the next
[28:45:01] year, as you can see, it resets. So you
[28:45:04] can see the running total sales for
[28:45:06] 2011. It is exactly as January. It is
[28:45:09] not adding up now the value of the
[28:45:11] current value with the previous one
[28:45:13] because the previous one is outside of
[28:45:15] the window. So as you can see we are
[28:45:17] getting running total for the whole year
[28:45:19] and once we hit a new year it is going
[28:45:22] to reset. So it is working and this is
[28:45:24] how you can create cumulative values in
[28:45:26] SQL. And of course if you would like to
[28:45:28] change the granularity of our data it is
[28:45:30] very simple. All what you have to do is
[28:45:32] to go over here and say instead of month
[28:45:34] we're going to make it as a year. And of
[28:45:36] course don't forget to change as well
[28:45:38] the group by. So let's go ahead and
[28:45:40] execute. And with that we are creating
[28:45:42] cumulative values for each year. But of
[28:45:45] course it makes no sense to partition by
[28:45:47] the years. Let's go and remove it and
[28:45:50] execute it again. And with that you are
[28:45:52] creating the running total sales the
[28:45:54] cumulative metric over the years. So as
[28:45:56] you can see it is very simple. Now we
[28:45:58] can go and add like another measure and
[28:46:01] another aggregation like for example
[28:46:03] instead of finding the running total we
[28:46:05] can find the moving average. So let's
[28:46:07] for example go and get the moving
[28:46:09] average of the price. So first we have
[28:46:12] to calculate the average of the price as
[28:46:15] average price. And now what we have to
[28:46:16] do is to go and make another window
[28:46:19] function over here where we are saying
[28:46:22] average the average price and we're
[28:46:25] going to go and call it moving
[28:46:27] average. That's it. So let's go and
[28:46:30] execute it. And with that you are
[28:46:32] getting the moving average price of our
[28:46:34] sales. All right. So now you might still
[28:46:36] asking what is really different between
[28:46:38] using a normal aggregation and
[28:46:40] cumulative aggregation. Well, we usually
[28:46:43] use normal aggregations in order to
[28:46:45] check the performance of each individual
[28:46:47] row. Like if I want to see how each year
[28:46:49] is performing, I'm going to go and do a
[28:46:51] normal aggregation. But if you want to
[28:46:53] see a progression and you want to
[28:46:55] understand how your business is growing,
[28:46:57] you have to go and use cumulative
[28:46:59] aggregations because you can see easily
[28:47:01] here the progress of your business over
[28:47:04] the years. So there is like a difference
[28:47:06] between using cumulative value and
[28:47:08] normal aggregation. All right. So with
[28:47:10] that you have done with the cumulative
[28:47:12] analyszis and you have learned all
[28:47:14] different types of aggregations. Now the
[28:47:16] next step in our road map we're going to
[28:47:18] do performance
[28:47:22] analyszis. Okay. So what is performance
[28:47:24] analyszis? It is the process of
[28:47:26] comparing the current value with a
[28:47:29] target value to compare the performance
[28:47:31] of specific category and this can help
[28:47:33] us in order to measure the success to
[28:47:35] compare the performance. So the formula
[28:47:37] for that is very simple. We're going to
[28:47:39] find the difference between the current
[28:47:41] measure and the target measure by
[28:47:43] subtracting them. Like for example, we
[28:47:44] can go and compare the current sale with
[28:47:46] the average sale or the current year
[28:47:49] sales with the previous year sales or
[28:47:51] the current sales with the lowest sales
[28:47:54] or maybe the highest sales. So as you
[28:47:56] can see we are always comparing the
[28:47:57] current measure together with a target
[28:48:00] with something else. So for example, we
[28:48:01] have here again a measure that is
[28:48:02] splitted by three categories. So those
[28:48:04] values are the current values. Now if
[28:48:07] you have a target like for example the
[28:48:08] average. Now as you can see for each row
[28:48:10] we have like the 200. Now what we can do
[28:48:12] once we have those two things in one row
[28:48:14] we can go and simply subtract them. So
[28:48:16] for the A the current value is exactly
[28:48:19] equal to the average. Both of them is
[28:48:20] 200 and the difference between them is
[28:48:23] zero. So this product is performing as
[28:48:25] an average. Now for the next one we have
[28:48:27] 300 and the target is 200. So the
[28:48:29] differences between them is 100. That
[28:48:31] means this category is performing very
[28:48:34] well. So this is a good performer. Now
[28:48:36] for the last one we will get minus 100.
[28:48:38] So that means it is below the average.
[28:48:41] So it is not performing very well. And
[28:48:43] for this type of analysis we usually use
[28:48:45] window functions like the aggregate
[28:48:46] window functions, the sum, average, max,
[28:48:49] min or the value window functions like
[28:48:51] lead and lag. So now let's go back to
[28:48:53] SQL and apply this formula in order to
[28:48:56] measure the performance of our business.
[28:48:58] So let's go. All right my friends. So
[28:48:59] now we have the following task. analyze
[28:49:01] the yearly performance of products by
[28:49:04] comparing their sales to both the
[28:49:06] average sales performance of the
[28:49:08] products and the previous year sales.
[28:49:10] Okay, this sounds a little bit
[28:49:12] complicated and serious. Let's have some
[28:49:13] coffee before we
[28:49:15] start. Okay, so what do we have over
[28:49:18] here? So it is talking about the yearly
[28:49:20] performance of products. So that means
[28:49:23] we need the order date as a dimension
[28:49:25] and as well the product and the measure
[28:49:27] that is used over here is the sales. So
[28:49:30] let's do it step by step. So we need
[28:49:32] things from our fact table. So fact
[28:49:35] sales and we need the product. So I'm
[28:49:37] going to go and get it from the
[28:49:39] dimension product in order to have a
[28:49:41] nice name. So we have to join the data
[28:49:44] by the product key and I'm going to go
[28:49:47] and change the alias to P. So product
[28:49:51] key. Okay. So with that we have our two
[28:49:53] tables. Now let's go and select our
[28:49:54] columns. So we need the order date. We
[28:49:57] need the product name and we need our
[28:50:00] measure. So it's going to be the sales
[28:50:02] amount. All right. So now let's go and
[28:50:04] query those informations. Now we have to
[28:50:06] analyze the yearly performance. That
[28:50:08] means we don't need the day. The
[28:50:10] granularity is the years. So that's why
[28:50:12] let's go and convert it using year
[28:50:15] function. And we're going to call it
[28:50:17] order year. And of course we have to go
[28:50:19] and aggregate then the sales. And I'm
[28:50:21] going to call it current sales. And of
[28:50:24] course we have to group up the data by
[28:50:26] the date, the year and as well by the
[28:50:30] product name. So that's it. Let's go and
[28:50:34] execute it. And of course I'm going to
[28:50:35] go and get rid of all those nulls. So
[28:50:38] where order date is not null. All right.
[28:50:42] So with that we have solved the first
[28:50:44] part. So we have the yearly performance
[28:50:46] of the product. Now in the task we have
[28:50:49] to compare this value the current sales
[28:50:51] to the average sales performance of the
[28:50:54] products. So that means we need the
[28:50:55] average and as well the previous year
[28:50:58] sales. So that means we have to compare
[28:51:00] each value to the previous year for the
[28:51:02] same product of course. So that means
[28:51:04] things are getting a little bit more
[28:51:06] complicated and with that we need the
[28:51:08] help of the window functions. Let's do
[28:51:09] it one by one. Let's focus on the
[28:51:11] average sales. So now what we're going
[28:51:13] to do based on those values based on
[28:51:15] this results we will do a new
[28:51:17] calculations and aggregations. And now
[28:51:19] in order to do that either we use a
[28:51:22] subquery or a city. I'm going to go with
[28:51:24] a city because it looks nicer. So with
[28:51:27] yearly product sales this is the new
[28:51:30] name that we are giving for this
[28:51:32] results. And now what we're going to do
[28:51:34] we're going to build queries on top of
[28:51:35] these results. So first of all I will
[28:51:38] just select everything from this table.
[28:51:40] yearly product sales just to test. So it
[28:51:42] is working. Now I'm selecting data from
[28:51:45] our city. So now the next step I'm going
[28:51:47] to go and list all the columns that I
[28:51:49] want in my results. So the order date,
[28:51:52] the product
[28:51:53] name, the current sales. This is just
[28:51:57] nicer in order to have control on which
[28:51:59] columns you want to present at the end
[28:52:01] results. Now the next step, I'm going to
[28:52:02] go and order the data by first the
[28:52:05] product
[28:52:06] name and then the order year. And with
[28:52:09] that we can have better understanding of
[28:52:11] the results. So we can see this product
[28:52:13] has three years of sales and those are
[28:52:15] the current sales for each year. So now
[28:52:17] we have to go and calculate the average
[28:52:20] of those three sales. So in order to do
[28:52:22] that we're going to use the
[28:52:24] average current sales over we have to
[28:52:29] decide now how to partition the data.
[28:52:31] Since we are focusing on the products we
[28:52:33] have to partition the results by the
[28:52:35] product name. So we're going to say
[28:52:36] partition
[28:52:37] by product name and we don't have to
[28:52:40] sort the data because we are using the
[28:52:42] average. So it doesn't matter how the
[28:52:44] data is sorted. So let's call it average
[28:52:47] sales. So let's go ahead and execute it.
[28:52:49] And now if you are looking to the
[28:52:51] results for this product the average
[28:52:53] sales of all those three values is
[28:52:55] 13,000. So now as you can see for each
[28:52:58] row we have the current sales and side
[28:53:01] by side with the average sales and the
[28:53:03] same thing for the next product as well.
[28:53:05] So now since we have both of the
[28:53:07] informations on the same row current
[28:53:09] sales and the average the change the
[28:53:11] difference between the current value and
[28:53:14] the average value. So all what we have
[28:53:15] to do is to go and subtract right. So
[28:53:17] we're going to say the current sales
[28:53:20] subtracted by the average sales and
[28:53:23] we're going to call
[28:53:24] it the difference in average. So let's
[28:53:28] go and execute it. And now as you can
[28:53:30] see we are getting now the comparison.
[28:53:32] we have the differences between the
[28:53:34] current and the average and of course
[28:53:36] what I like to do is to make a flag or
[28:53:38] like indicator whether we are above the
[28:53:41] average below the average or at the
[28:53:42] average so in order to do that we're
[28:53:44] going to go and use the case when
[28:53:46] statement so if the difference is higher
[28:53:50] than zero then we are above the average
[28:53:53] right above average oh let's have an
[28:53:55] abbreviation for that and if we are
[28:53:58] below zero that means we are below the
[28:54:00] average right so below
[28:54:03] then below average and if it is exactly
[28:54:06] zero else then it is average. So that's
[28:54:10] it. Let's end it and I'm going to call
[28:54:12] it average change. So let's go and
[28:54:16] execute it. Now if you focus again on
[28:54:18] one of the products you can see the
[28:54:20] current sales of this product in 2012 it
[28:54:23] is below the average. It is really low.
[28:54:25] And for the next year for 2013 it is
[28:54:29] above the average. It was really nice
[28:54:31] year for these products and the last
[28:54:33] year 2014 it was again below the
[28:54:35] average. So with that we have really
[28:54:37] nice flag in order to see quickly
[28:54:39] whether we are above or below the
[28:54:41] average and it is interesting to see
[28:54:43] whether we have zeros. So yeah sometimes
[28:54:46] it is exactly like the average and here
[28:54:48] we have like a zero. It's not below or
[28:54:50] above. So with that we are comparing the
[28:54:53] performance of the sales of each
[28:54:55] products with the average. And as you
[28:54:57] can see it is really simple. Yeah. using
[28:54:59] the window functions. So let's go and
[28:55:01] check again our task. We have compared
[28:55:03] the current sales to the average sales
[28:55:05] performance. Now we have to compare it
[28:55:07] as well with the previous year sales. So
[28:55:10] let's go back to our example over here.
[28:55:12] This time we have to compare the current
[28:55:14] sales not with the average but with the
[28:55:17] previous year. So we don't have to write
[28:55:19] like another CTE or query. We can
[28:55:21] continue with the same results. So now
[28:55:23] all what you have to do is to access the
[28:55:26] previous year. And in order to do that,
[28:55:27] we have amazing window function called
[28:55:30] lag. So let's do it step by step. So now
[28:55:32] we're going to go and create a new
[28:55:33] column that's called lag. I want to
[28:55:36] access the previous value of what the
[28:55:38] current sales, right? So current sales
[28:55:42] and
[28:55:42] over we still have to partition the data
[28:55:45] by the product name because we focus on
[28:55:47] the products. So partition by product
[28:55:50] name. But now in order to access the
[28:55:53] previous value that means we have to
[28:55:55] sort the data and we're going to sort it
[28:55:57] by the years. We need the previous year.
[28:55:59] So we're going to say order by order
[28:56:02] year and we're going to sort it
[28:56:03] ascending from the lowest to the
[28:56:05] highest. So we're going to leave it like
[28:56:06] this. And with that this window function
[28:56:08] going to give us the previous year sales
[28:56:11] of the products. So I'm just going to
[28:56:13] call it previous year sales like this.
[28:56:15] And I think here we have something
[28:56:17] wrong. Okay. So let's go a and execute
[28:56:19] it and let's go and focus on one of
[28:56:21] those products. So now for the first
[28:56:22] year of this product, the previous year
[28:56:25] was null, right? So we don't have any
[28:56:27] data from the previous year. But for the
[28:56:29] 2013, we have a previous year of 2012.
[28:56:32] So that's why now we are getting the
[28:56:35] previous value of the sales based on the
[28:56:37] years. And the same thing for the last
[28:56:39] year over here. You can see we are
[28:56:41] getting the previous sales. So it is
[28:56:43] working. And for the next window, same
[28:56:45] thing for the first year. we will get
[28:56:47] null and the previous sales we will get
[28:56:49] it from the previous year. So with that
[28:56:51] we have now the previous sales and if
[28:56:53] you check this over here we have in the
[28:56:56] same row now the current sales of the
[28:56:58] current year and as well the sales of
[28:57:01] the previous year. Now what we have to
[28:57:03] do the same thing we have to go and
[28:57:05] subtract those two informations in order
[28:57:07] to compare them. Right? So we're going
[28:57:09] to go and do the same thing. So we will
[28:57:11] get the current sales minus the whole
[28:57:14] thing the whole window function and
[28:57:16] we're going to call it previous year. So
[28:57:19] difference of the previous year and with
[28:57:21] that we are calculating the differences
[28:57:23] between them. So for this year for this
[28:57:25] product as you can see the difference
[28:57:27] here is really big between the current
[28:57:29] sales and the previous year. Now of
[28:57:31] course what we can do we can go and make
[28:57:32] as well a flag or an indicator. I'm
[28:57:35] going to go and copy the whole thing
[28:57:37] from the previous average but we have to
[28:57:39] go and get the right function this and
[28:57:42] the same over here and now it is not
[28:57:44] above or below the average I'm going to
[28:57:46] say it is increasing or decreasing right
[28:57:49] so increase or decrease and we're going
[28:57:52] to call it previous year change and
[28:57:54] instead of average we can say no change
[28:57:57] so let's go and execute it and I'm
[28:58:00] having here an extra comma let's go and
[28:58:02] execute it so again let's go and focus
[28:58:05] of one of those products. For the first
[28:58:07] year of this product, there is no change
[28:58:09] because there is no previous year. For
[28:58:11] the next year of this product, we have
[28:58:13] an increase, right? Because the current
[28:58:15] sales is way higher than the previous
[28:58:18] year. And now by going to the last year
[28:58:19] of this product, we have a decrease
[28:58:21] because the current sales is less than
[28:58:24] the previous year. So my friends, we
[28:58:26] call this type of analyszis year over
[28:58:30] year analyszis. And if you want to
[28:58:32] calculate the month over month analyzes,
[28:58:34] it's very simple. All what you have to
[28:58:36] do is to go and change the function from
[28:58:38] year to a month and with that you are
[28:58:40] extracting the month part. And the
[28:58:42] difference between analyzing the months
[28:58:44] and years is of course the scope.
[28:58:46] Year-over-year is good for long-term
[28:58:49] trends analyzes where on the other hand
[28:58:51] the month over month it is shortterm
[28:58:53] trends analyzes. You are just focusing
[28:58:56] on the seasonality of your data. So this
[28:58:58] is how we analyze the performance of our
[28:59:01] business by comparing the current
[28:59:03] measure with a target measure and you
[28:59:05] can go and use different dimensions and
[28:59:07] stuff. So instead of the sales you can
[28:59:09] check the quantity instead of products
[28:59:11] you can check the customers and you can
[28:59:13] go and compare the current information
[28:59:15] not only with the average or the
[28:59:17] previous year you can compare it with
[28:59:19] the lowest sales and the highest sales
[28:59:21] and it can open the door for many
[28:59:23] different insights. But we are always
[28:59:25] using the same methods using the window
[28:59:28] functions. We compare the current value
[28:59:30] with another value in our data sets. So
[28:59:33] this is how we do performance
[28:59:34] comparison. All right. So that you have
[28:59:36] learned how to analyze the performance
[28:59:38] of our business. Now in the next step
[28:59:40] we're going to do partto-hole analyszis.
[28:59:43] So let's
[28:59:46] go. Okay. So now what is exactly part to
[28:59:49] whole analyszis? Well, we use it in
[28:59:51] order to find out the proportion of a
[28:59:53] part relative to the whole. Well, here
[28:59:55] we're going to analyze how an individual
[28:59:57] category is contributing to the overall
[29:00:00] in order to understand what is the most
[29:00:03] impacting category to the overall
[29:00:05] business. So now for the formula, it is
[29:00:07] very simple. You have to go and pick one
[29:00:09] of your measures divided by the total of
[29:00:12] the measure and then multiply it by 100
[29:00:14] in order to find the percentage by a
[29:00:16] specific dimension. Like for example, if
[29:00:18] you take the sales, so you divide the
[29:00:20] sales by the total sales, multiplied by
[29:00:23] 100 by the category or if you take the
[29:00:25] quantity divided by the total quantity
[29:00:28] and then find the percentage by a
[29:00:30] country. So for example, again we have
[29:00:31] our measure splitted by categories. But
[29:00:33] now instead of having this number, what
[29:00:35] we're going to do, we're going to
[29:00:36] calculate the percentage. So for the
[29:00:38] first one, we're going to take the 200
[29:00:40] divided by 600 multiply it by 100. So
[29:00:43] we're going to get the percentage 33. So
[29:00:45] once we do that for the all categories,
[29:00:47] it's going to be now very easy to see
[29:00:49] that the category P it is contributing
[29:00:51] to the overall number by 50%. Which
[29:00:54] makes it of course a top performer. So
[29:00:56] you can visual in your head as like a
[29:00:58] pie chart and you can see how each part
[29:01:01] is contributing to the whole pie chart
[29:01:03] and with that it can help us to
[29:01:04] understand the importance of each
[29:01:06] category to our business. So now let's
[29:01:08] go and apply this formula to our
[29:01:10] measures in order to understand the
[29:01:12] importance of our categories. So let's
[29:01:14] go. Okay. So now let's do part hole
[29:01:16] analyszis. All what we need one
[29:01:17] dimension and one measure. So for
[29:01:19] example we have the following task. It
[29:01:21] is very simple. Which categories
[29:01:22] contribute the most to the overall
[29:01:25] sales. So now let's go and do it step by
[29:01:27] step. So first we're going to go and
[29:01:29] collect the informations. So we need the
[29:01:31] category. We need the sales amount and
[29:01:34] those informations come as usual from
[29:01:37] the fact sales and from our dimension
[29:01:41] the product. Right? So we have quickly
[29:01:44] to go and connect them using the product
[29:01:47] key. Okay. So that's all what we need
[29:01:50] for our query. So let's go and select.
[29:01:52] So we have here the categories and the
[29:01:54] sales amount. So now the first thing we
[29:01:56] have to calculate the total sales for
[29:01:58] each category. So let's go and do that.
[29:02:00] It is very simple. So sum total sales
[29:02:04] and we are grouping up the data by the
[29:02:07] category. So this is basics. Right now
[29:02:09] we have the total sales for each of
[29:02:11] those categories. Now in order to
[29:02:13] calculate the percentage we need two
[29:02:14] measures the total sales for each
[29:02:16] category and we have it here already and
[29:02:19] as well side by side we need the total
[29:02:21] sales across all categories. So the big
[29:02:24] number without any dimension but now as
[29:02:26] you look to the result you can see the
[29:02:28] granularity here is that category. Now
[29:02:31] we need the total sales again by
[29:02:33] different granularity. And in order to
[29:02:35] mix those stuff together we use the
[29:02:37] window functions. So now how we going to
[29:02:39] do it? either you go over here and start
[29:02:41] writing your window function. And of
[29:02:42] course, you can do it together with the
[29:02:44] group by or you can do it as a second
[29:02:46] step in your query using either a CTE or
[29:02:49] a subquery. So I'm going to go with the
[29:02:51] CTE just to make it clear. So category
[29:02:55] sales like this. So now let's start
[29:02:58] again selecting the same information. So
[29:03:00] category total sales from our table
[29:03:03] category or CTE sales. So let's go and
[29:03:07] execute it. So now we have the same
[29:03:09] results and now we're going to go and
[29:03:10] build our window function like this. So
[29:03:12] we're going to say the sum we want to
[29:03:14] aggregate all those values right to get
[29:03:16] the total sales over the whole data
[29:03:19] sets. So we're going to say sum total
[29:03:21] sales. And now in order to get the big
[29:03:24] number we're going to say over and
[29:03:26] inside it we will not define anything
[29:03:28] because we don't want to partition the
[29:03:30] data. We don't want to introduce any
[29:03:32] dimension. We just want the big number.
[29:03:34] And with that we will get the overall
[29:03:38] sales. So let's go and execute it. Now
[29:03:41] as you can see this is the total sales
[29:03:43] by the category. So the total sales is
[29:03:46] splitted by the categories. And this is
[29:03:48] the overall sales of all orders of
[29:03:51] everything the highest number. Now since
[29:03:53] we have them side by side what we can do
[29:03:55] we can very easily calculate the path to
[29:03:58] whole or the percentage. So let's start
[29:04:01] doing that. We need the total sales and
[29:04:03] we want to go and divide it by the
[29:04:05] overall sales. So we're going to take
[29:04:07] our window function and put it over
[29:04:08] here. So let's go and multiply it now
[29:04:11] with 100. I'm going to go and call it
[29:04:14] percentage of total. So let's go and
[29:04:18] execute it. Now as you can see we are
[29:04:19] getting zeros and that's because the
[29:04:21] total sales is not float. So what we
[29:04:24] have to do is to go and cast it to
[29:04:25] something like a decimal. So floats like
[29:04:29] this. So let's go and reexecute it. And
[29:04:31] now, as you can see, we are getting now
[29:04:32] the percentages, but we have a lot of
[29:04:35] numbers after the comma. So, we're going
[29:04:37] to go and round the numbers now. So,
[29:04:39] let's go to the start round and then go
[29:04:42] to the end, comma, and let's have like
[29:04:44] two decimals. So, let's go and execute
[29:04:47] it again. Now, looks perfect. Now, what
[29:04:49] we can do, we can go and add like a
[29:04:51] percentage. And with that, we are
[29:04:52] converting the whole thing to a string.
[29:04:54] So, we're going to do concatenation. So,
[29:04:56] concat at the start and go to the end.
[29:05:00] And let's add the percentage character.
[29:05:02] And as well we can go and order the data
[29:05:05] by the total sales descending. So let's
[29:05:09] go and execute it. So now by looking to
[29:05:11] the result you can see the category
[29:05:13] bikes is dominating. So it is
[29:05:15] overwhelming top performing the
[29:05:17] categories. It is making 69% of the
[29:05:21] total sales of our business. So this
[29:05:23] means my friends most of the business
[29:05:25] revenue comes from the bikes. And as you
[29:05:28] can see the accessories and clothing
[29:05:30] they are really minor contributors to
[29:05:32] our business which is not really good
[29:05:35] and this is actually dangerous thing. If
[29:05:37] you have like one category dominating
[29:05:39] your whole business you are over relying
[29:05:41] on only one category in your business
[29:05:43] and if this fails this category then the
[29:05:46] whole business is going to fail. So by
[29:05:48] looking to this either the business has
[29:05:49] to decide removing all those products by
[29:05:52] those two categories or to focus more on
[29:05:56] bringing more revenue for the products
[29:05:58] that are inside those two categories. So
[29:06:00] as you can see guys those insights are
[29:06:03] really amazing for the business and
[29:06:04] helps the managers and the decision
[29:06:06] makers to understand what is going on
[29:06:08] quickly and make very critical
[29:06:11] decisions. And now you can see as well
[29:06:12] from the results perfectly why the part
[29:06:15] to whole analyszis is very important
[29:06:17] because by just looking to those numbers
[29:06:19] it's going to be really hard to
[29:06:20] understand the importance of the
[29:06:23] categories. But seeing the data as a
[29:06:25] percentage how each category is
[29:06:27] contributing to the whole sales of the
[29:06:29] business makes it easier to understand
[29:06:32] which category is underperforming or top
[29:06:35] performing. And now you have a very
[29:06:36] simple formula where you can go and
[29:06:38] change the metrics. For example, instead
[29:06:40] of total sales, you can go and change
[29:06:42] the aggregations to total number of
[29:06:44] orders or the total number of customers.
[29:06:47] So you can go and bring any type of
[29:06:49] measures and bring it to this analyszis
[29:06:51] and you're going to generate completely
[29:06:53] new view for the decision makers in
[29:06:56] order to develop a new strategy for the
[29:06:58] business. It was very interesting. Now
[29:06:59] in the next step, we're going to do my
[29:07:01] favorite topic where we're going to
[29:07:03] start doing data segmentations using
[29:07:04] SQL. So let's go.
[29:07:10] Okay. So now what is data segmentations?
[29:07:12] What we're going to do here is we're
[29:07:13] going to go and group up the data based
[29:07:15] on specific range. So that means we're
[29:07:17] going to go and create a new categories
[29:07:19] and then go and aggregate the data based
[29:07:21] on the new category. And the formula for
[29:07:23] that going to be very interesting. So
[29:07:25] it's going to be this time we're going
[29:07:26] to have a measure by a measure not by
[29:07:29] dimension. So you have to go and pick
[29:07:31] two different measures and convert one
[29:07:33] of those measures to a range or to a
[29:07:35] group and then aggregate the data by
[29:07:36] this measure. So for example, we're
[29:07:38] going to go and calculate the total
[29:07:40] number of products by the sales range or
[29:07:43] the total number of customers by the age
[29:07:45] group. So as you can see we have two
[29:07:46] measures and we are trying to combine
[29:07:48] them together in order to create new
[29:07:50] insights. Let's have the following
[29:07:51] example. So here for example we have
[29:07:53] like two measures and now the first step
[29:07:55] is that we're going to take one of those
[29:07:56] measures and convert it to a dimension.
[29:07:59] converted to a category. For example,
[29:08:01] we're going to say if the values are
[29:08:03] like equal or below 100, it will be
[29:08:05] converted to a category called low. And
[29:08:08] between 100 and 200, it's going to be
[29:08:10] assigned to a new category called
[29:08:12] medium. And everything above 200, it's
[29:08:15] going to be large. So, as you can see
[29:08:16] what we are doing, we are taking one
[29:08:18] measure and based on the range of this
[29:08:20] measure, we are building a new
[29:08:22] categories, new dimension. And now the
[29:08:24] final step is the easiest one. We're
[29:08:26] going to go and aggregate another
[29:08:27] measure based on the new category. So
[29:08:29] we're going to have seven for low, six
[29:08:31] for medium, and 15 for large. So with
[29:08:33] that, as you can see, we are creating
[29:08:35] new categories or segments based on a
[29:08:37] measure. And then we are aggregating
[29:08:39] another measure based of this new
[29:08:42] segments. And in SQL, in order to create
[29:08:44] those new categories and segments, we
[29:08:46] use the amazing case when statements
[29:08:49] because it's going to help us to define
[29:08:50] the rules and based on the range, it's
[29:08:52] going to go and create a new category
[29:08:54] and labels. So now let's go and apply
[29:08:56] this formula on our data set in order to
[29:08:59] segment our data. So let's go. Okay. So
[29:09:01] now let's go and segment our data and
[29:09:03] all what we need is two measures. So now
[29:09:05] we have the following task and it says
[29:09:07] segment products into cost ranges and
[29:09:10] count how many products fall into each
[29:09:12] segment. So now by looking to this task
[29:09:14] we have two measures. First the costs
[29:09:16] and as well the second one is the total
[29:09:19] number of products. And of course we
[29:09:21] have to go and segment one of those two
[29:09:22] measures. And in this task we are
[29:09:24] segmenting the costs. So we have to
[29:09:27] focus now on taking this measure and
[29:09:29] convert it to a dimension. So now all
[29:09:31] those informations are available in the
[29:09:33] table products. So now let's go and
[29:09:35] select few columns. We're going to get
[29:09:36] the product key and let's get the
[29:09:40] product name and the costs. That's all
[29:09:43] what we need. So let's execute it. Now
[29:09:45] as you can see this is our measure the
[29:09:47] costs. Now we have to go and convert
[29:09:49] this measure to dimension. And in order
[29:09:52] to do that, we use the case win
[29:09:54] statements. We always use the case win
[29:09:56] statement in order to create new
[29:09:57] categories. So let's go and do that.
[29:10:00] Case win. Let's start with the first
[29:10:01] range. Let's say it is below 100. So all
[29:10:04] the costs that are below 100. We're
[29:10:07] going to label it with a new value. It's
[29:10:09] going to be below 100. So now let's go
[29:10:12] to the next range. We are saying when
[29:10:14] costs now between 100 and 500. So all
[29:10:19] costs between this range. They will get
[29:10:21] the label 100 and 500. So this is very
[29:10:25] simple. Let's go and get another range.
[29:10:27] For example, between 500 and 1,000. Then
[29:10:33] it's going to get a label between 500
[29:10:35] and 1,000. And now it depend how many
[29:10:37] categories and segments you want to
[29:10:39] create. Each row of this case when each
[29:10:42] condition will be creating like a new
[29:10:44] value for your dimension. So I'm going
[29:10:46] to stop with that. I'm going to say at
[29:10:47] the end else. So if the cost is not
[29:10:49] fulfilling any of those, it's going to
[29:10:51] be above 1,000. Right? So that's it.
[29:10:55] Let's give it a name. It's going to be
[29:10:57] cost range. So now let's go and execute
[29:10:59] it. Now let's go and check the result.
[29:11:02] For example, the cost here is zero. It
[29:11:03] is below 100, which is correct. This
[29:11:06] value is above 1,000. This is between
[29:11:08] 500 and 1,000. And this is between 100
[29:11:11] and 500. So everything looks correct.
[29:11:14] Nice. So with that we are done with the
[29:11:16] first step where we have converted one
[29:11:18] measure into a dimension. So with that
[29:11:20] we have now our segments. The next step
[29:11:23] with that we're going to go and
[29:11:25] aggregate the data based on this a new
[29:11:27] dimension. So either you do it in one go
[29:11:29] or what I usually do I put everything in
[29:11:31] one city or a subquery and I'm going to
[29:11:34] call it products
[29:11:36] segments as based on this results I'm
[29:11:40] going to go and aggregate the data. So
[29:11:41] this is my temporary results and now
[29:11:44] we're going to go and just aggregate the
[29:11:45] data like this. So let's get first our
[29:11:47] dimension cost range and then we need
[29:11:50] our measure. So it's going to be count
[29:11:52] product
[29:11:53] key as total products from our city. It
[29:11:58] was the product segments and then group
[29:12:01] by our new dimension. That's it. It's
[29:12:04] very simple. Let's go and execute it
[29:12:06] now. Now you can see in the output we
[29:12:08] have our segmented measure and we can
[29:12:10] see the total numbers in each of those
[29:12:13] segment and range and of course we can
[29:12:14] go and order the data by our aggregation
[29:12:17] the total products. Let's go and execute
[29:12:19] it maybe descending. So now as you can
[29:12:21] see we have a lot of products that are
[29:12:24] not costing a lot. It is below 100.
[29:12:26] After that between 100 500 and the
[29:12:29] lowest number of products is in the
[29:12:32] range that is above 1,000. So we don't
[29:12:34] have a lot of products that are costing
[29:12:36] a lot and that's because maybe we have a
[29:12:38] lot of accessories in the business. So
[29:12:40] my friends this is very powerful. If
[29:12:42] your dimensions in the data set is not
[29:12:45] enough to create insights you can take
[29:12:47] one of your measures convert it to a
[29:12:49] dimension using case win and then
[29:12:51] aggregate your other measures based on
[29:12:54] this new dimension. So we are deriving
[29:12:56] new informations and as I told you by
[29:12:58] just following this concept measures and
[29:13:00] dimensions you can generate endless
[29:13:03] amount of reports even if your business
[29:13:05] or your data set is small. Okay my
[29:13:08] friends so now let's go and segment
[29:13:09] something else. So this time it's going
[29:13:11] to be a little bit more complicated. So
[29:13:13] we have the following task and it says
[29:13:15] group customers into three segments
[29:13:17] based on their spending behavior. So we
[29:13:19] have the VIB customers. They are the
[29:13:21] customers with at least 12 months of
[29:13:24] history and spending more than 5,000.
[29:13:27] And the second category we have the
[29:13:29] regular customers. They have at least as
[29:13:31] well 12 months of history but they spend
[29:13:34] like less than 5,000. And the last
[29:13:36] category we have the new customers.
[29:13:38] Their lifespan is less than 12 months.
[29:13:41] And we have to find the total number of
[29:13:42] customers by each group. So now here we
[29:13:44] have a lot of measures and stuff. So the
[29:13:46] first one is the total number of
[29:13:48] customers. This is going to be the final
[29:13:50] aggregation that we're going to do. But
[29:13:52] what is interesting, we're going to
[29:13:53] build the segments and this time is
[29:13:55] based on different columns. So first it
[29:13:58] is based on a measure the total number
[29:14:00] of months for each customer and as well
[29:14:03] the total spending, the total number of
[29:14:05] sales. So we have the sales, we have the
[29:14:08] total number of months and as well the
[29:14:10] total number of customers. So now we're
[29:14:11] going to do it step by step. Don't you
[29:14:13] worry about it. So now what I usually
[29:14:14] do, I start collecting all the data that
[29:14:16] I need. So what do we need? We need a
[29:14:19] customer key. In order to do the
[29:14:21] aggregation for the total number of
[29:14:22] customers, we need as well the sales
[29:14:24] amount right for the spending. And now
[29:14:27] in order to calculate those number of
[29:14:29] months, we need a date. And for that, we
[29:14:32] have to calculate the lifespan of a
[29:14:34] customer. And usually we create it using
[29:14:36] the order date. I'm going to show you
[29:14:38] how we're going to do it. So we need the
[29:14:39] order date. And of course, we have to
[29:14:41] select our table. So let's start with
[29:14:43] the fact table. So fact sales and we're
[29:14:46] going to join it with the
[29:14:48] customers. So our dimension customers
[29:14:51] and the key for that it is the customer
[29:14:54] key as well for the customers. And here
[29:14:56] we have to specify which column come
[29:14:58] from which table. So the first one from
[29:15:00] the customers, the sales from the fact
[29:15:02] and the order date from the fact as
[29:15:04] well. So now let's go and execute. Now
[29:15:07] we can see we have our customers, the
[29:15:09] sales and the order dates. So now the
[29:15:11] sales going to help us in order to
[29:15:12] specify the range of spending. But now
[29:15:15] what is interesting we have to calculate
[29:15:17] the lifespan. So now in order to get the
[29:15:19] lifespan we have to find out the first
[29:15:21] order and the last order of each
[29:15:24] customer. So how many months is between
[29:15:26] the first order and the last order. So
[29:15:28] in order to do that we need the min
[29:15:31] function for the order dates. So this is
[29:15:34] the first order and the max in order to
[29:15:37] get the last order. Right.
[29:15:41] And since we are using min and max, we
[29:15:43] have to go and group up the data. And we
[29:15:45] need to do that anyway in order to get
[29:15:47] the total spending. So for the sales
[29:15:50] amount, we're going to have the sum in
[29:15:52] order to have the total spend total
[29:15:56] spending. And we don't need the order
[29:15:58] age. And the dimension where we're going
[29:15:59] to group up the data is by the customer
[29:16:02] key. So let's go and execute it. So now
[29:16:04] in the results we have a list of all our
[29:16:06] customers and as well the total spending
[29:16:09] for each customer and we have the first
[29:16:11] order date and the last order dates. Now
[29:16:13] in order to calculate how many months
[29:16:16] between the first order and the last
[29:16:18] order we can go and use the function
[29:16:20] date diff in order to get a new measure.
[29:16:23] So let's go and do that date diff. And
[29:16:25] now since we need the number of months
[29:16:27] we're going to use the month and then
[29:16:29] the second argument going to be the
[29:16:31] first order. So order date and the
[29:16:35] second one going to be the latest. So
[29:16:36] max order date and we're going to call
[29:16:39] this lifpan. So let's go and query and
[29:16:43] let's have a look to our results. You
[29:16:45] can see for this customer 712 between
[29:16:48] the first order and the last order we
[29:16:50] have 11 muscles and for this customer
[29:16:53] over here we have zero because the first
[29:16:55] order and the last order is in the same
[29:16:57] month and maybe there is only one order.
[29:16:59] So with that we have the lifespan and as
[29:17:01] you can see guys we have derived a new
[29:17:04] measure from the dimension order age in
[29:17:07] order later to derive from this new
[29:17:09] measure a new dimension the segments. So
[29:17:12] we are converting a dimension to a
[29:17:14] measure and then from a measure to a new
[29:17:16] dimension and this is usually what we do
[29:17:18] in analyzes and in SQL. So now do we
[29:17:21] have all the informations for the logic?
[29:17:23] So we have the lifespan. So we have the
[29:17:25] total number of monsters, we have the
[29:17:27] total spending and I think we are ready
[29:17:29] to start building our segments. So now
[29:17:31] what we're going to do, we're going to
[29:17:32] create the segments based on these
[29:17:34] results that we have prepared. So this
[29:17:36] result is the intermediate result before
[29:17:38] the final one. Now either you're going
[29:17:40] to put it in a CTE or subquery. Well, I
[29:17:43] usually go and use the CTE. It is nicer.
[29:17:47] So with customer
[29:17:49] spending and I'm going to put the whole
[29:17:52] thing in ECT and we can start writing a
[29:17:54] new query from the scratch based on the
[29:17:56] inter results. So let's go and select
[29:17:58] again the customer key. I'm going to get
[29:18:01] the total spending and the lifpan. So we
[29:18:05] don't actually need the first and the
[29:18:07] last order and we're going to get all
[29:18:09] those informations from our new city. So
[29:18:12] let's go and execute. And now let's
[29:18:14] start building the segments. And as
[29:18:16] usual, we're going to go and use the
[29:18:17] case win statements. It is just amazing
[29:18:20] statements in order to derive and build
[29:18:23] new columns. So now what do we have for
[29:18:25] the first category? So they are the
[29:18:27] customers over 12 months and spending
[29:18:29] more than 5,000. So now we're going to
[29:18:31] say if the laugh span is higher than 12
[29:18:35] and the total spending is higher than
[29:18:39] 5,000 then we have our VIB customers. So
[29:18:44] this is the first label. Let's go to the
[29:18:47] second one. If the lifespan as well I
[29:18:49] think more than 12. So let's go and
[29:18:51] check. Well, it is at least 12. I have
[29:18:53] here mistake. So it's going to be larger
[29:18:56] or equal. So now it is more correct. So
[29:18:58] the customers that has at least 12
[29:19:00] months but they spend like 5,000 or
[29:19:02] less. So that means it's going to stay
[29:19:04] the same condition but the total
[29:19:06] spending will be less or equal 5,000s
[29:19:11] and they are the regular customers. So
[29:19:14] they will get this label. Now if it is
[29:19:17] not fulfilling those two conditions what
[29:19:19] this means this means this is a new
[29:19:22] customer right. So they will get this
[29:19:24] label. Let's go and have an end and
[29:19:26] let's call it customer segments. So
[29:19:30] let's go and execute it. Now let's have
[29:19:32] a look for this customer 712. So the
[29:19:35] total spending is less than 5,000. So
[29:19:38] this customer is not a VIB and as well
[29:19:41] the lifespan is less than 12. So that
[29:19:44] means for us it is a new customer. Now
[29:19:46] the next one we have a VIB. So this
[29:19:49] customer has a history at least 12
[29:19:51] months. So we have here 16 months and as
[29:19:53] well the total spending more than 5,000.
[29:19:56] That's why this customer is a VIB. But
[29:19:58] now let's go and search for a regular
[29:20:01] customer
[29:20:03] 2349. So this customer spent less than
[29:20:06] 5,000. So we are fulfilling this
[29:20:08] condition over here and as well this
[29:20:10] customer has at least 12 months of
[29:20:13] history that's why we have a regular. So
[29:20:15] now as you can see we have derived a new
[29:20:18] dimension from two measures the lifespan
[29:20:21] and the total spending. Now of course
[29:20:23] the last step what is going to be we
[29:20:25] have to go and find the total number of
[29:20:27] customers for each of those categories.
[29:20:29] So now what we're going to do we're
[29:20:30] going to remove all those stuff and
[29:20:32] we're going to start with our new
[29:20:33] dimension and then comes the aggregation
[29:20:36] count customer key. So as total
[29:20:40] customers and then we have to group up
[29:20:42] the data by our new dimension. So this
[29:20:46] going to be really annoying if I'm going
[29:20:47] to take this here and put it in the
[29:20:49] group I because this means each time I'm
[29:20:52] changing the logic I have to take care
[29:20:54] of that twice. One in the select
[29:20:56] statement and the second one in the
[29:20:58] group I. So now actually instead of that
[29:21:00] what I'm going to do I changed my mind.
[29:21:02] I'm going to still having the
[29:21:04] aggregation in the second step. So we
[29:21:06] need the customer key we have the
[29:21:08] definition of our customer segments. And
[29:21:10] now I'm going to go and use the subquery
[29:21:13] where I put the aggregation as a second
[29:21:15] step. So my friends that means this is
[29:21:18] again a second intermediate results. You
[29:21:21] can of course put it in a second city.
[29:21:23] So that means this is the first
[29:21:25] intermediate results where we have
[29:21:27] created the lifespan and the total
[29:21:28] spending and the second intermediate
[29:21:30] result is creating the customer segments
[29:21:33] and the third step and the last one is
[29:21:35] by doing the final aggregation. So we're
[29:21:37] going to do it like this. Select our
[29:21:40] dimension customer segments. Then we're
[29:21:43] going to go and count the customer key
[29:21:46] from our sub query. So this is our
[29:21:49] subquery and don't forget to group by
[29:21:52] our dimension customer segments. I think
[29:21:54] I have it wrong. All right. So this is
[29:21:56] the subquery and this is the final step
[29:21:58] where we are aggregating everything. I'm
[29:22:00] going to go and order the data by the
[29:22:03] total customers like this. So now let's
[29:22:07] go and execute the whole thing. Well
[29:22:10] descending not ascending. Okay. Okay. So
[29:22:12] now we can see from our results the
[29:22:14] highest number of our customers belong
[29:22:17] to the category new. So we have 14,000
[29:22:20] customers that are new in our business.
[29:22:22] And then the second category we have the
[29:22:24] regular customers. So we have around
[29:22:26] 2,000 customers. And in VIB we have a
[29:22:29] lot of VIB customers. So we have
[29:22:32] 1,655 VIB customers in our business. So
[29:22:34] with that my friends, we have done data
[29:22:36] segmentation. It is amazing. We have
[29:22:38] segmented our customers based on their
[29:22:41] spending behavior and as you can see all
[29:22:43] those informations are totally derived
[29:22:46] from the our data and this help us to
[29:22:48] have a deep understanding of the
[29:22:50] behavior of our customers and of course
[29:22:52] this can help as well making smart
[29:22:58] decisions. All right my friends so with
[29:23:00] that we have covered the five different
[29:23:02] types of data analytics thus we can do
[29:23:05] using SQL. Now what I usually do as the
[29:23:07] last tip in my project is that I try to
[29:23:09] collect all the different types of
[29:23:11] explorations and analyzes that I have
[29:23:13] done in my data sets so that I can put
[29:23:15] everything in one for example view or
[29:23:17] table and then offer it to other users
[29:23:20] and with that it going to help the other
[29:23:21] users or stakeholders to make a quick
[29:23:24] analyszis for decision- making. So now
[29:23:26] what we're going to do, we're going to
[29:23:26] have like some kind of requirements
[29:23:28] where we're going to bring a lot of
[29:23:29] different analyzes in one big script in
[29:23:32] order to have insights about one object
[29:23:34] like for example the customers. So I'm
[29:23:36] going to show you the requirement of
[29:23:38] this reports and we're going to analyze
[29:23:39] it and start writing the scripts. So
[29:23:41] let's go. Okay friends. So now let's
[29:23:43] create a customer report and here are
[29:23:45] the requirements for the report. So now
[29:23:48] we have like a general statement. It
[29:23:49] says this report should consolidate key
[29:23:52] customer metrics and behaviors. So it
[29:23:54] says first we have to gather all the
[29:23:56] details about the customers like names,
[29:23:59] age, transaction details and then we
[29:24:01] have to segment the customers into
[29:24:03] categories VIB, regular and new and as
[29:24:05] well by the age groups and we have to
[29:24:07] provide as well aggregations like the
[29:24:10] total order, total sales, quantity,
[29:24:12] products and so on. And we have to
[29:24:14] generate important KPIs like the
[29:24:16] recency, the average order value, the
[29:24:19] average monthly spends. So we have a lot
[29:24:22] of things and we're going to do it step
[29:24:23] by step. All right. Now I'm going to
[29:24:24] take you step by step in the process of
[29:24:26] building a complex query that I usually
[29:24:29] use in order to build a report. Now the
[29:24:31] first thing that I usually do is I start
[29:24:33] selecting the data from the database and
[29:24:36] I usually start with the fact table. So
[29:24:38] this is my starting point and then
[29:24:39] usually I join it with the dimensions
[29:24:41] and here I use lift join and after that
[29:24:44] I think about how to filter the data
[29:24:46] because usually we don't need all the
[29:24:48] data that is available in the database
[29:24:50] and of course in the result I will not
[29:24:52] be selecting all the columns. I'm going
[29:24:54] to be selecting only the relevant
[29:24:55] columns that I need for my reports. So
[29:24:58] since we have like complex query we will
[29:25:00] be dividing the process into multiple
[29:25:02] steps and I usually call this step the
[29:25:04] base data and this going to be the
[29:25:06] foundation the scope for the next steps
[29:25:08] and since we have like multiple steps
[29:25:10] I'm going to put this in a CTE so we
[29:25:12] have this as an intermediate results and
[29:25:14] what we're going to do in this step as
[29:25:16] well we're going to do few
[29:25:17] transformations like maybe calculating
[29:25:19] and deriving new columns maybe
[29:25:21] formatting the date so some basic
[29:25:23] transformations so now let's go and
[29:25:24] build this results for our report so the
[29:25:27] first step is retrieving the core
[29:25:29] columns from the tables. So let's go and
[29:25:31] do it together. So we need of course our
[29:25:33] fact table facts and we need our
[29:25:38] dimension gold customer and as usual
[29:25:40] we're going to go and connect them. All
[29:25:42] right. Okay. So this is the basic and
[29:25:45] now what we're going to do we're going
[29:25:46] to go and retrieve all the columns that
[29:25:48] we need for our reports. So let's start
[29:25:50] picking stuff. So order number let's get
[29:25:53] the product key the order date sales
[29:25:58] amount quantity and I think that's all
[29:26:01] from the facts let's go and get few
[29:26:03] informations from the customers so let's
[29:26:06] get the customer key the customer number
[29:26:10] the first name and as well the last name
[29:26:13] and what else we can go and get the
[29:26:15] birth dates because we have to create
[29:26:16] the age groups so birth dates let's go
[29:26:20] and query. So I think those are all the
[29:26:23] columns that we need in order to do the
[29:26:25] next steps. And now before we go and
[29:26:27] proceed with the aggregations, what
[29:26:29] we're going to do, we're going to think
[29:26:30] about filtering the data. As I recall,
[29:26:32] we have some orders where the order date
[29:26:34] is null. So I'm going to go and remove
[29:26:37] those stuff. So order date is not null.
[29:26:40] So that means in the first query the
[29:26:42] base query not only I'm selecting the
[29:26:44] columns that I need for the reports also
[29:26:47] I'm defining the scope of the data sets
[29:26:49] by filtering the data. So you can as
[29:26:51] well make the scope here only one year
[29:26:53] or something. Now what else we can do is
[29:26:54] to think about all those columns and
[29:26:56] whether we can do any type of
[29:26:58] transformations in order to prepare them
[29:27:00] for the aggregations. Like for example
[29:27:02] I'm going to go and say you know what
[29:27:03] instead of first and last name I'm going
[29:27:05] to put them together in one. So it's
[29:27:07] going to be the customer name. It's
[29:27:08] better than having like two columns. So,
[29:27:10] let's go and do it. We're going to say
[29:27:12] concat and then we're going to start
[29:27:14] with the first name and we're going to
[29:27:16] have a separator between them. You can
[29:27:18] have like a minus or a white space like
[29:27:21] this and after that the last name. So,
[29:27:24] let's call it customer name. And we can
[29:27:27] go and get rid of those two columns. So,
[29:27:29] let's go and execute. And with that, you
[29:27:31] have everything in one column. Now,
[29:27:32] another thing that we can prepare that
[29:27:35] we don't need the birth date. We
[29:27:37] actually need for our reports the age
[29:27:39] groups. So that means we have to go and
[29:27:41] calculate the age. So let's go and
[29:27:43] transform it. So date diff we want it in
[29:27:46] years, the birth date and the current
[29:27:49] date from system and we're going to call
[29:27:50] it age. So let's execute again. Perfect.
[29:27:54] So with that we have all the data that
[29:27:56] we need for our reports. Let's go and
[29:27:58] put everything in one city. So I'm going
[29:28:00] to call it with query as and put
[29:28:03] everything in this city. And I'm going
[29:28:05] to go and put this comment over here
[29:28:08] inside the city. Perfect. And now we're
[29:28:10] going to go and write a query from the
[29:28:13] scratch. Paste on our intermediate
[29:28:15] results. So base is query. It's execute.
[29:28:18] All right. So now by looking to our
[29:28:20] report with that we have the important
[29:28:22] columns. Right. So now in the next step
[29:28:24] we're going to do aggregations on top of
[29:28:26] these intermediate results. So here
[29:28:28] we're going to do all the aggregations
[29:28:29] that is needed for the report and we're
[29:28:31] going to put everything again in CTE as
[29:28:34] an intermediate results which makes
[29:28:36] everything a modular and easy to read.
[29:28:38] So now let's go and do the necessary
[29:28:40] aggregations on the result that we have
[29:28:42] previously prepared. So that's why this
[29:28:44] is very important as a second step in
[29:28:47] our report. Always tend to make a
[29:28:49] separated CTE only for aggregations. So
[29:28:52] let's go and do that. I'm going to go
[29:28:54] and select again all the customer
[29:28:56] informations like the customer key
[29:28:58] number, age. So I'm just going to copy
[29:29:00] and paste and put it over here. And we
[29:29:03] just need the column
[29:29:05] names. So the key number, name, and age.
[29:29:09] Now after that, we're going to start
[29:29:10] doing aggregations. So what do you want
[29:29:12] to aggregate is first, for example, the
[29:29:14] total number of orders. So we're going
[29:29:16] to go and count distinct order
[29:29:19] number as total orders. So this is one
[29:29:24] aggregation. We can go and summarize all
[29:29:26] those sales
[29:29:27] amounts as
[29:29:30] total sales and the quantities as well.
[29:29:33] So sum quantity as total quantity and as
[29:29:38] well we can go and count how many
[29:29:40] products did our customer order. So the
[29:29:43] products key as total
[29:29:47] products. So what I'm doing now I'm just
[29:29:50] looking to our intermediate results and
[29:29:51] try to figure out what we can aggregate
[29:29:54] for example it makes no sense to
[29:29:55] aggregate for example the ages right so
[29:29:58] from the order number we have total
[29:29:59] orders total product sales amount
[29:30:01] quantity and from the right side we
[29:30:03] cannot aggregate anything and that's
[29:30:05] because they are the details of the
[29:30:06] customers but from the fact table we can
[29:30:09] do a lot of aggregations so now what we
[29:30:10] can do with the order date over here we
[29:30:12] can for example find the last order
[29:30:15] dates from our customer which is really
[29:30:17] nice information. So we can say max
[29:30:20] order date as last order and of course
[29:30:23] we can go and calculate the lifespan and
[29:30:26] that we're going to need it as you
[29:30:27] remember in order to categorize our
[29:30:30] customer. So I will just copy and paste
[29:30:32] it from the previous query is the date
[29:30:34] diff month between the first order from
[29:30:37] the customer and the last order of the
[29:30:39] customer. So and we call this lifespan.
[29:30:42] Okay. So we derived two measures or
[29:30:44] aggregations from the order date. Now I
[29:30:47] think we have done everything possible
[29:30:49] and what is missing of course is to have
[29:30:51] a group by because we are doing
[29:30:53] aggregations and we are grouping by the
[29:30:56] customer details. So going to be
[29:30:58] customer key, customer number, name and
[29:31:01] age. So I think we have everything for
[29:31:03] our aggregations. Let's go and execute
[29:31:06] it. A list of all customers and we have
[29:31:09] few details about the customers and now
[29:31:11] we have a lot of measures. So the total
[29:31:13] order, total sales, total quantity,
[29:31:15] products, the last order and the
[29:31:17] lifespan. And with that we have covered
[29:31:20] this part over here where we have
[29:31:22] provided aggregations on the customer
[29:31:24] level. So we have the details and we
[29:31:27] have the aggregations. All right. So
[29:31:28] with that we have now all the
[29:31:30] preparations that is required to build
[29:31:32] the final results. So it really depend
[29:31:35] on the scenario. If it's possible we can
[29:31:37] take all the data from one city or if
[29:31:39] it's needed we can get it from multiple
[29:31:41] cities. But in our scenario, we're going
[29:31:42] to take it from the second city, the
[29:31:44] aggregations, and we're going to prepare
[29:31:46] the final results. So here we're going
[29:31:48] to bring everything together and we
[29:31:49] might introduce final transformations
[29:31:52] that is needed for the reports. So let's
[29:31:54] go and write the query for the final
[29:31:56] results. Now we can go and start
[29:31:58] segmenting our customer and as well
[29:32:01] creating the KPIs. So let's go to the
[29:32:03] third step. I'm going to go and put this
[29:32:05] in a CTE. So let's call it customer
[29:32:08] aggregation.
[29:32:10] And now based on these results, we will
[29:32:13] write the final query. So I like always
[29:32:16] to put a comment about the steps. So the
[29:32:18] first city is the base query where we
[29:32:20] just joined the data and prepared it.
[29:32:22] And then the second query is for the
[29:32:24] aggregations. And the final one is for
[29:32:26] the final results. So let's go and start
[29:32:29] writing our final query. We will start
[29:32:31] with select. And I'm going to go and
[29:32:32] list again all the customer
[29:32:34] informations. So I'm going to go and get
[29:32:36] again same things. We have the customer
[29:32:39] key, customer number, name, age and so
[29:32:41] on. And now after that we need to create
[29:32:43] the age categories. And now after that
[29:32:46] I'm going to go and get all those
[29:32:47] measures as well from our query. But of
[29:32:50] course without the calculations I just
[29:32:52] need the names of
[29:32:56] it. So with that we have everything from
[29:32:59] our previous CTE. So the customer
[29:33:02] aggregation. Okay. So let's just test
[29:33:05] it. Now everything is working. So now
[29:33:06] what we have to do? We have to create
[29:33:08] few categories age category and as well
[29:33:10] the segments of the customers right for
[29:33:12] segmenting the customers we have already
[29:33:14] done the query so I will just copy and
[29:33:16] paste it from the previous analyszis it
[29:33:18] looks like this if the lifespan is at
[29:33:20] least like 12 months and the sales above
[29:33:23] 5,000 then a less or equal 5,000 then
[29:33:26] regular otherwise it is a new customer
[29:33:28] so this is our first segment but the
[29:33:31] second segment about the ages we're
[29:33:32] going to go and build it now and again
[29:33:34] how we going to do it when so if the age
[29:33:36] for example example less than 20 then
[29:33:39] the customer is under 20. Let's make
[29:33:42] another range where we say if the
[29:33:44] customer age is
[29:33:46] between 20 and let's say
[29:33:49] 29 then we have the second range and we
[29:33:53] can keep repeating the same thing for
[29:33:55] the second one. It really depend how
[29:33:57] many categories you want to build. So 30
[29:34:00] and 39 I belong to this group. Now the
[29:34:04] next one let's have the 40s as well
[29:34:06] right so 40 49 same thing over here and
[29:34:11] now else let's say 50 and above right
[29:34:14] and above so let's go and end it as age
[29:34:19] group I just want to sort it little bit
[29:34:22] like this okay now it looks nice so with
[29:34:24] that again we have turned a measure into
[29:34:27] a dimension and let's go and execute it
[29:34:29] now so now by checking the results we
[29:34:32] have the details of the customers and
[29:34:33] Now we have a new category. So as you
[29:34:36] can see it is working. 54 it is above
[29:34:39] 50. This is in the range between 40 and
[29:34:42] 49. We have here 67 above 50. I believe
[29:34:45] we don't have any customer that is below
[29:34:48] 20. Right? Or even between 20 and 30.
[29:34:50] Okay. So with that we have created our
[29:34:53] two categories and by looking to the
[29:34:56] reports you see we can segment the
[29:34:58] customers now into categories. The VIB,
[29:35:00] regular, new and the age group. And with
[29:35:02] that we have covered all those three
[29:35:04] requirements and we come now to the last
[29:35:06] requirements. We have to calculate the
[29:35:08] following KPIs. Now the first one it is
[29:35:10] an easy one. It is the recency. How many
[29:35:12] months since the last order we have
[29:35:14] calculated over here the last order for
[29:35:16] the customer. It is this one. And now in
[29:35:19] order to find the recency it is very
[29:35:20] simple. So all we have to do is to take
[29:35:22] this over here. I will just put it maybe
[29:35:24] after the segmentation. And all what you
[29:35:26] have to do is to use the date diff as
[29:35:28] usual. So month is the last order date
[29:35:32] and the get date. So as you can see we
[29:35:35] are using this setup like in many
[29:35:37] analyzes right we always find the
[29:35:39] differences between a date from our data
[29:35:41] sets and the current date and time and
[29:35:43] with that we will get the recency. So
[29:35:47] let's go and execute it. Now you can see
[29:35:48] how many months since the last order of
[29:35:51] the customer and of course you can go
[29:35:53] and test it using the last order date.
[29:35:55] And this is really important in order to
[29:35:56] understand whether the customer is still
[29:35:58] active or inactive. Okay, so this is for
[29:36:01] the first easy KPI. Now let's go to the
[29:36:03] second one. It says calculate the
[29:36:05] average order value. So how we going to
[29:36:08] do this? Let's go back over here. Now in
[29:36:10] order to compute the average order
[29:36:12] value, we have to divide the total sales
[29:36:15] by the total orders. So how many revenue
[29:36:18] did the customer generate? And we divide
[29:36:20] it by the total number of orders and
[29:36:22] after that we have to find the average.
[29:36:24] So it is very simple. Let's go and write
[29:36:26] that. We're going to go to the end of
[29:36:27] our table where we're going to put our
[29:36:29] KPI and I'm going to say here compute
[29:36:32] average order value. So as a shortcut
[29:36:35] AVO. So we say total sales divided by
[29:36:39] total orders. And let's call it average
[29:36:42] order value. So let's go and execute it.
[29:36:45] And if you go to the last over here, you
[29:36:47] can see the average order value of our
[29:36:50] customers. But now if you are dividing
[29:36:51] numbers together you have to be careful
[29:36:53] that you are not dividing by zero
[29:36:55] otherwise you will get an error. So
[29:36:56] imagine that a customer has a zero
[29:36:58] didn't order anything you might get an
[29:37:01] error. In our scenario, we don't have
[29:37:02] that because we are starting from the
[29:37:04] order table or the fact table. But
[29:37:06] still, I like to make sure this never
[29:37:08] happens. And for that, I usually go and
[29:37:10] use the case when statements. Very
[29:37:12] simple one. If the total orders is equal
[29:37:14] to zero, then make it zero. Otherwise,
[29:37:18] do the calculation that we talked about.
[29:37:20] So like this. And at the ends, we will
[29:37:23] add an end. So that's it. And with that,
[29:37:26] I make sure we will never divide by
[29:37:28] zero. So that's it. It was simple,
[29:37:30] right? Let's go to the last KBI the
[29:37:33] average monthly spend. So how we will
[29:37:36] calculate that
[29:37:38] compute average monthly spend. So now
[29:37:42] since we are speaking about the spending
[29:37:44] that means we need the total sales.
[29:37:46] Right? So how much sales did the
[29:37:48] customer generate totally and then we
[29:37:51] divide it by the number of months and
[29:37:53] with that we will get the average
[29:37:55] monthly spend. Right? So that means we
[29:37:57] can divide the total sales by the
[29:38:00] lifespan as we calculated it is the
[29:38:02] period where the customer has been
[29:38:04] active from the starts until the end.
[29:38:06] Okay. So now let's do it step by step.
[29:38:08] First we have to be careful that we are
[29:38:10] not dividing by zero and I believe in
[29:38:12] the lifespan we have zeros. So what
[29:38:14] we're going to say as usual case when
[29:38:16] lifespan is equal to zero then this time
[29:38:19] we will not make it zero the customer
[29:38:21] exist only for one month. So what we can
[29:38:23] do we can get the total sales of the
[29:38:25] customer and we don't have to divide it
[29:38:27] by the month in order to find the
[29:38:29] average because the average is equal to
[29:38:31] the current total
[29:38:32] sales. So with that we make sure we are
[29:38:35] not dividing by zero otherwise we're
[29:38:37] going to have our calculation. So total
[29:38:39] sales divided by life span. So the total
[29:38:43] sale divided by the months and with that
[29:38:45] we will get the average monthly spend.
[29:38:47] So and and ass and we're going to call
[29:38:50] it average monthly spend. Perfect. So
[29:38:54] let's go and try that out. Let's go to
[29:38:56] the right side. And with that we have
[29:38:58] our third KPI and we have the average
[29:39:01] monthly spends. And with that guys, we
[29:39:03] have now full reports about the
[29:39:06] customers and we have covered all the
[29:39:08] requirements. All right. So with that we
[29:39:09] have the final results and we have
[29:39:11] fulfilled the requirements. So what
[29:39:13] we're going to do, we're going to take
[29:39:14] the whole query and put it in the
[29:39:16] database as a view. And once we have the
[29:39:18] view, the report in the database, we can
[29:39:20] share it with the others. Now the other
[29:39:22] data analyst in the team can go and
[29:39:24] maybe create a dashboard in order to
[29:39:26] visual data using API tool like Tableau
[29:39:29] or PowerBI. But in this scenario, the
[29:39:31] user can go and connect your view the
[29:39:33] last prepared data to the dashboard. And
[29:39:36] with that the user can quickly generate
[29:39:38] insights without doing a lot of steps in
[29:39:40] order to prepare the data for the
[29:39:42] visualizations. And of course the data
[29:39:44] analyst can go and connect the
[29:39:45] dimensions and facts. But having this
[29:39:47] one solid view it's going to be like way
[29:39:50] easier to consume. And of course the
[29:39:51] data analyst can as well write a query
[29:39:54] on top of your view in order to generate
[29:39:56] a quick insights. So as you can see
[29:39:57] using only SQL you are covering a lot of
[29:40:00] complex steps in order to make the data
[29:40:03] ready for reporting and analyzes and
[29:40:05] this is what usually happened in real
[29:40:07] projects. We're going to go and put the
[29:40:09] query in the database so that the others
[29:40:12] can use it. So what we're going to do
[29:40:14] very simple create review and we're
[29:40:16] going to put it in a good layer and
[29:40:17] we're going to call it report customers
[29:40:20] and then ask like this and let's go and
[29:40:23] execute it. It is successful. Now if you
[29:40:25] go to our database and check the views
[29:40:27] you will find a new view called gold
[29:40:29] report customers. Now all what you have
[29:40:31] to do is to go and have a simple select.
[29:40:34] So codes reports customers and you will
[29:40:38] get an amazing report about the
[29:40:41] customers. This kind of reporting it is
[29:40:43] very important because you are giving a
[29:40:45] full picture 360° view of all your
[29:40:48] customers. So you have details,
[29:40:50] categories, measures everything in one
[29:40:52] go and it going to makes life easier.
[29:40:54] Now for any user of this view to quickly
[29:40:56] understand the data and generate maybe
[29:40:58] insights based in this one view that can
[29:41:01] helps of course your customers. So I
[29:41:02] just want to show you now what this
[29:41:04] means. If a user using your reports so
[29:41:06] either in SQL or maybe they're going to
[29:41:08] go and connect it to PowerBI or Tableau
[29:41:10] they can generate immediately insights.
[29:41:12] So for example, if they go and say count
[29:41:15] customer number so as total customers
[29:41:19] and then they're going to go and take
[29:41:21] any dimension for example the age group.
[29:41:24] So something like this and then group by
[29:41:27] the age group. Put just put it here
[29:41:30] first. And then they're going to go and
[29:41:31] add any other measure. For example, the
[29:41:34] total
[29:41:36] sales and any other measure that you
[29:41:38] have in this view and then execute and
[29:41:41] quickly they can do analyszis on top of
[29:41:43] your view without having them to go to
[29:41:45] their fact and dimensions. So this is
[29:41:48] like one extra prepared layer the data
[29:41:50] model that you have built. And if you
[29:41:52] don't want to group it by the ages, you
[29:41:54] can go and have the customer segments
[29:41:56] and it will be working. So quickly they
[29:41:58] can analyze the new derived informations
[29:42:01] that you have prepared in your reports.
[29:42:03] So guys, this is amazing reports about
[29:42:06] the
[29:42:11] customers. And now what you're going to
[29:42:13] do, you're going to go and prepare the
[29:42:15] second report where you have to build
[29:42:17] complete insights about the products of
[29:42:19] the business. It is very similar to the
[29:42:22] customers. So we want to generate a
[29:42:24] report for the products. You have to
[29:42:26] provide details like the product name,
[29:42:27] category, subcategory and the costs. You
[29:42:30] have to segment the products by the
[29:42:32] revenue. So you can have categories like
[29:42:34] high, medium and low. And then you have
[29:42:35] to provide the basic aggregations at the
[29:42:38] level of the products and then calculate
[29:42:40] few KPIs. So as you can see it is very
[29:42:42] similar to the customers. And now what
[29:42:44] you have to do you have to pause the
[29:42:46] video follow the same step at the
[29:42:48] customers where we join the tables car
[29:42:50] create aggregations and put everything
[29:42:52] like in CTE and at the end once you are
[29:42:55] done create the view where you have the
[29:42:57] report about the products. So I'm going
[29:42:59] to go now and do it offline and I will
[29:43:01] see you
[29:43:02] [Music]
[29:43:06] soon. Okay my friends I hope you are
[29:43:08] done with the reports. I'm going to show
[29:43:10] you quickly how I've done it. So I've
[29:43:12] just created a new view called report
[29:43:14] products and then we start with the base
[29:43:16] query where we have joined the fact
[29:43:18] table with the dimension products and
[29:43:20] collected all the columns that we need
[29:43:23] for the reports and we put everything in
[29:43:25] the first city. So this is the first
[29:43:27] step and there was from my side no need
[29:43:30] for any transformations over here. So we
[29:43:32] go now to the second step and here we
[29:43:34] have to put all the different types of
[29:43:36] aggregations in one go. So we calculate
[29:43:38] the lifespan, the last sales order,
[29:43:41] total orders, total customers, sales
[29:43:43] quantity and as well I have created the
[29:43:45] average selling price of the products.
[29:43:48] It is very simple. We are dividing the
[29:43:50] sales amount by the quantity. So this is
[29:43:52] the basic aggregations about the
[29:43:54] products and finally we have the final
[29:43:56] query. So we start with selecting the
[29:43:58] basic informations about the products.
[29:44:00] So we have the key, name, category and
[29:44:02] then we have here the recency and we
[29:44:04] have our new segments. This one is very
[29:44:07] easy for the products. So we are saying
[29:44:08] if the total sales is higher than 50,000
[29:44:11] then this is a high performer and if
[29:44:14] it's like between 50 and 10k then this
[29:44:16] is a mid-range otherwise it is low
[29:44:19] performer. So the segmentations of the
[29:44:21] products is very simple and after that
[29:44:23] we have like all our measures that we
[29:44:26] aggregated in the CTE and now we come to
[29:44:28] the two KBIS. It is very similar to the
[29:44:30] customers. So the first one the average
[29:44:32] order revenue it is simply dividing the
[29:44:34] sales by the total orders and you have
[29:44:36] to take care of the zeros of course and
[29:44:39] the average monthly revenue we divide
[29:44:41] the total sales by the lifespan of the
[29:44:43] products and of course if the lifespan
[29:44:45] is zero so it is only one month then it
[29:44:48] is the total sales and with that you
[29:44:50] generate the average monthly revenue. So
[29:44:52] as you can see it is very similar to the
[29:44:54] customers but still the focus here is
[29:44:56] the products. Now of course we put this
[29:44:58] query in view. So we have the report
[29:45:01] products side by side by the report
[29:45:03] customers and now we have really amazing
[29:45:06] report about the products where we have
[29:45:07] everything. So we have a lot of details
[29:45:09] about the customers. We have as well a
[29:45:11] dimension in order to segment our
[29:45:14] products and we have a lot of measures
[29:45:16] that are really important about each
[29:45:18] products. So we have the total number of
[29:45:20] orders sales, how many customers did
[29:45:22] order the products, the average price,
[29:45:24] the average revenue and the monthly
[29:45:26] average revenue. And this gives you
[29:45:28] really deep insights about each product
[29:45:30] of your business. And of course, this is
[29:45:32] very helpful in order to compare the
[29:45:34] products, right? And now, of course,
[29:45:36] this is core analyzis that you're going
[29:45:38] to need it a lot in your business.
[29:45:39] That's why we offer it as a view. So, I
[29:45:42] think we have now two amazing reports
[29:45:44] about our
[29:45:48] data. All right, my friends. So, now
[29:45:50] don't forget to put all your work in the
[29:45:52] Git repository in order to share it with
[29:45:54] others as a successful project. So as
[29:45:57] usual we have the data sets,
[29:45:58] documentations and as well the scripts
[29:46:00] that you have done through this projects
[29:46:02] and here I'm putting everything
[29:46:04] together. So we have all the activity of
[29:46:06] the exploration as well with the
[29:46:08] advanced analyszis that we have done. So
[29:46:10] we have the change over time, the
[29:46:12] cumulative analyszis, performance, data
[29:46:14] segmentations, part tool analyszis and
[29:46:16] as well our two new reports. So I
[29:46:19] recommend you if you haven't done that
[29:46:20] yet go and create now a repository put
[29:46:23] all your work there to make sure that
[29:46:25] everyone can access and see your work
[29:46:26] and my friends don't forget to add nice
[29:46:29] commenting on your code and formatting
[29:46:31] and styling your code should be perfect.
[29:46:34] So if you haven't done that yet go and
[29:46:36] do it now. All right my friends so with
[29:46:38] that we have done the last step in our
[29:46:40] road map. We have created two solid
[29:46:42] reporting for our users. And with that,
[29:46:44] we have completed all the steps of our
[29:46:46] advanced analytics projects. And with
[29:46:48] this project and the previous projects,
[29:46:49] you can see now the full picture on how
[29:46:51] to do data analytics on any data sets
[29:46:54] using SQL. So starting by the first step
[29:46:57] where we have explored the database and
[29:46:59] end up having a very solid reports where
[29:47:02] we have consolidated everything in one
[29:47:04] view and with that we have now really
[29:47:05] great understanding about the business,
[29:47:08] about our data. And now what you can do,
[29:47:10] you can go and grab any data sets in the
[29:47:12] internet and you can go through all
[29:47:13] these faces again and I promise you at
[29:47:16] the end you will have a full picture and
[29:47:18] understanding of the business and this
[29:47:20] is what I exactly do in each project if
[29:47:22] I want to understand any type of data
[29:47:24] sets. All right my friends. So with that
[29:47:25] we have covered the last type of SQL
[29:47:28] projects the advanced data analytics.
[29:47:30] And with that we have now three solid
[29:47:32] projects using SQL and they are very
[29:47:35] similar to real world projects in the
[29:47:36] industry especially if you want to be a
[29:47:38] data engineer or a data analyst. And my
[29:47:40] friends we have covered the last chapter
[29:47:43] in our course. So this is the advanced
[29:47:45] level in SQL. And those are all the
[29:47:47] chapters that I have designed for you to
[29:47:50] take you from the basics to intermediate
[29:47:52] and then to the advanced topics. My
[29:47:54] friend, you made it. Congrats. You
[29:47:56] should be really proud of yourself. And
[29:47:58] now with that, I can say that I have
[29:47:59] shared everything that I know about SQL
[29:48:02] and you can now solve any complex task
[29:48:04] using SQL like I do in my real projects.
[29:48:07] And I hope that you have enjoyed the
[29:48:08] journey. And if you do and you want me
[29:48:10] to create more free courses like this,
[29:48:12] make sure to support the channel by
[29:48:14] subscribing, liking, and commenting.
[29:48:16] This of course going to make the channel
[29:48:17] grow, reach the others, and as well
[29:48:19] motivates me to make more content like
[29:48:21] this. So nothing left to say. Thank you
[29:48:24] so much for watching and I will see you
[29:48:25] in the next course.
