The document discusses retrieving the most common salary from an employee table. It guides walking through selecting the salary and count, ordering by count, and using LIMIT to return only the row with the highest count. This identifies the most common salary as $40,000, earned by 3,735 employees. It then discusses finding the average salary excluding the highest and lowest values, and retrieving customers who have made the most purchases along with their names.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
29 views
Subquery Exercises - Part Three - Transcript
The document discusses retrieving the most common salary from an employee table. It guides walking through selecting the salary and count, ordering by count, and using LIMIT to return only the row with the highest count. This identifies the most common salary as $40,000, earned by 3,735 employees. It then discusses finding the average salary excluding the highest and lowest values, and retrieving customers who have made the most purchases along with their names.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7
INTESAT ABFGRe most occurring salary.
So, I want you to return that particular
salary that occurred the most. But let me help you to guide you through. So, I've selected the salary and a count of all the salaries, okay? To know how many times. So, let's just run this. So, you'll see that these two people have earned 10- 106,621. In fact let me do something. Let me try to ORDER BY. So, that it can become a bit more beautiful. So, let's ORDER BY the count. Let's see that more clearly- the count. Then, yeah? In descending order and if pOkay, welcome back to the last task in this project. In this task, you will work on more problems that will require the knowledge of SQL to solve. Are you ready? Let us start immediately. So, I want us to retrieve the salary that occur the most. That is what people call in mathematics as the mode or in statistics as the mode. That is the most occurring salary. So, I want you to return that particular salary that occurred the most. But let me help you to guide you through. So, I've selected the salary and a count of all the salaries, okay? To know how many times. So, let's just run this. So, you'll see that these two people have earned 10- 106,621. In fact let me do something. Let me try to ORDER BY. So, that it can become a bit more beautiful. So, let's ORDER BY the count. Let's see that more clearly- the count. Then, yeah? In descending order and if possible, we can also order by the salary. So, it first orders by the count. Then, in order to-- you can order by more than one, um- column. So, this first orders by the count, this particular count and then orders by the salary. So, if I run this now. Let us see. So, you'll see that about 3,735 people have earned this salary- 40,000. So, what we want is just this-- what- what I want you to return is only this salary, okay? This particular salary 40,000. That's the only thing I want you to return. One thing I can-- you can do. Let me give you one more clue- is to-- you can do something like have limit to return only that first row that-- just have a limit here. Let me limit to one row. Okay, so limit is the last statement or keyword that occurs in a-- in an SQL query. So, let me run this. So, it returns 40,000 and the count is what? 3,735. So, what I want you to return is only this 40,000. So, pause my video right now and, um- work on this. Okay. Welcome back. Were you able to figure out that? Let me help you. So, let me just highlight this. And um, let's highlight all of that and copy it. Then, I'll-- I have pasted that. One thing we can do is to have this as my source of data. So, because I want only this particular salary. Okay, I can do something like this, which we've seen before. But just to reinforce it. I can do SELECT salary. I want only the salary, then FROM-- and now this becomes my table, my source of data alright? You can say FROM this place. So-- and you will remember that you have to give this a name like, a. So, I'm going to come here to say the a dot salary. What it's going to do is that this will return only that 40,000 as the salary. Let's run that and you'll see it will return only the 40,000. So, what I've just done is I've used a subquery in the FROM clause. Uh-- this now becomes a source of data and from that source of data. I want to pick out only that salary that occurred the most. Okay. Good! That's quite fine. Let's go on to the next thing. What I want you to do next is to find the average salary excluding the highest and lowest salaries. The reason for this idea-- I thought about it and I said it's possible that there are what we call outliers, that is some values that will make your results skewed. Let us try to see is there going to be any difference between the average salary when we remove the highest salary and lowest salary? Would there be any difference? What difference will it make? Just probably to probe a bit. So, I have this for you. I can just highlight this and this return the average salary like we've seen before, okay? And the average salary is 63,986.10. So, we want to see, would there be any difference when I removed the highest value and the lowest value from the salaries table and see if there will be any difference. So, I want you to try that out- pause my video right now and try it out. Okay, so I have just-- I think you were able to figure that out. But let's see the solution. So, I've been able to copy what I have here to this point. Now, I would have here- FROM salaries. Then, I'll have a WHERE clause- WHERE salary NOT IN- Okay, now NOT IN. There will be two values I will have. And the first value will be select the minimum salary, alright? FROM salaries and I will have another one here- SELECT the maximum salary from salaries. Okay, that's it. So, I have my statement terminator here. So, what I've just done is if you do SELECT minimum from salary, okay, it will return the minimum salary, If you do SELECT maximum from salary, we've seen that before, it will return the maximum salary. Okay, so what we're doing is, now say- now look through the salary that are not-- that is not any of these two, okay? And find the average. So, let's just highligh this now and run it. You will notice that there was a slight difference. This is 63,985.59- 985. Okay, let's see this one- 63,986.10. So, there was not really much difference but there could be a case where the difference could be significant. I believe you understand what I've done here. So, I just removed-- that's why I used this NOT IN. So, where it looks at the minimum salary and maximum salary and returns those other salaries that-that is not either of these two and find the average. Now, let's go on. What I want you to do next is to retrieve a list of the customer_id, the names of the customers that have bought- that have purchased the most from the comp-- from our- from the mall or from the store. So, I have this for you. You can highligh this and run it, okay? So, clearly you can see that this customer WB- -21850 has purchased 37 times from the- from the store. So, probably we want to do a promotion that- to give those customers that purchased the most. Probably some kind of discount. So, what I want you to do now is to not just only return the customer_id and the customer count-- like how many times customers bought from the mall. But, we want to also retrieve the name of the customer. So, what you'll do is, you will just simply have a subquery in the FROM clause. So, I want you to try that- pause my video and try that out now. Okay, so um-- I believe this is what your solution should look like. Um-- we already have this part before. That's what we have on the screen. The count of each of the customer and-- that I've purchased the highest. And what I've just done is I've made this a second source of data and I've named it a and from that a, I have returned this customer count. What we have in this second column. Then from the first column which is the customers table. Remember the customers table is what has the customer's name. So, from there we have retrieved the customer name and then have ordered by the count, okay? Just to have the highest-- those people that have purchased highest in descending order- at the top of the list. Then one thing I've also done is to add that WHERE clause. Remember what the WHERE clause will do. Just-- it would check those customer- -id that match in both tables, so that we don't have so much combination of- of the data. So, basically let's just run this and see what this result will look like. It will only add one more column which is-- will be the name column of that customer. Yeah, so you'll see the customer name. So, clearly we say William Brown has pur-- has purchased most in this our customers table. So, this particular person has come to the store the most to buy-- followed by John Lee. So, we could want to say- do a discount or a special promotion for those customers. Maybe probably the first top five customers. And that would maybe say let's do a LIMIT. We can now do a limit to say top five. And this return those top five customers that has-- they've come to the most. And you'll see the top five customers like that. So, um-- another thing we want to try to explore as the last task here is- that there is a high possibility that a customer come to the company many times, okay? Or to the store many times to purchase but might not have given so much money like sales to the company. So, one thing I want to do is-- Let's-- let me quickly do something here. So, let's do SELECT * FROM sales; Let's see what I'm trying to show you here. So, if you come down through this place, you'll see a sales column that tells us about the- the quantity the person bought- the customer bought and how much that gave to the company in sales, okay? And there's also a profit column. Um-- if you go through but what I want-- I want to just try-- I want to just try to explore is to know the total sales. So, there's this new column sum of sales for-- not just coming to the company. We-- We are not just bothered about you coming to purchase. How much do you bring? Like how much in terms of cash like do we- do you give when you purchase from us? So, let's run this. And you will see there is a new column which is the total sales that howbeit, this customer; which will soon see the name of that customer- has purchased just 15 times from the company. But overall the person has produced-- has given more money to the company in terms of sales. So, the 15 times was worth more than just coming 34 times and all. You'll see this person has come 29 times. But in terms of sales generated, the person has not generated more sales to the company. So, I want you to pause my video at this point and try to retrieve the customer name, okay? And the segment, okay? of that customer. And-- all you need to do is just to copy what I have here and have this as a second source of table. And you need to just reference it and pick the columns you need. Like we've seen in the previous task. This just to cement your understanding. So, pause my video now and try it out. So, um- what this what you did?Remember that we already have this part, okay? from the previous task here and that's what we have on the screen. But one thing I've just done is to return the customer_id, the customer name, the customer segment, then the-- from this other table I have a new column. That's the difference between this and the former one we did and the WHERE clause still remains the ORDER BY and that. So, I'm-- first, I want to ORDER BY those that bring the highest number of sales, right? So, let's just run this, okay? And we'll see that this customer has-- is home office, the segment is home office and person has purcahsed 15 times but-- has come to the office-- to the store- 15 times. But in terms of how much the person brings in terms of the sales, it's the highest. And you'll see, in fact, you can't even put a limit here. You probably, we want to target the first five people. Let's see, okay? And just run this, alright? And you'll understand that what I'm saying-- that it's not about the quantity. See this person has--he's a consumer. Andrian is a consumer has purchased 20 times. Come to the store 20 times, but has not generated as much as each of these other customers. Um, this is so amazing! This has added a powerful skill to your SQL toolbox. I understand that it was quite--probably more overwhelming. So, you can decide to go through this particular set of tasks again, just to understand it. And you have this solution to these exercises in the original reading section, you also have the script to your-- as your own. You also have the employees database so you can keep practising to solidify your understanding. This will keep-- this will come very handy as you keep practicing and learning. You are the best! Wow! This has been an-- a fantastic hands-on work. Great job you've got there! Beautiful! That was amazing, right? I salute your doggedness to seeing the end of this insightful project. If you have enjoyed this course, make sure you give it the best rating and reviews. Great job on completing the seventh part of this project. In this task, we learned how to solve some complex problems in SQL. One thing I have shown you in this project is how to frame your thoughts and map out a strategy or a plan to solve your SQL challenges. I hope you hold on to that as you keep learning. At this point, we have come to the end of this project and I believe you have learned a lot from this project. In this project, we saw how to use subqueries in the WHERE clause, in the FROM clause, and in the SELECT clause, how to-- and we saw how to solve different real-life problems using SQL and subqueries. Although we did not consider how to use the ANY or the ALL keywords in the subqueries and in the WHERE clause. However, this project has given you every necessary fundamental knowledge you require for further explora-- exploration. It has given you all you need to easily pick up the ones we've not covered and do well with it. So, that's why I made it just seven tasks so that you can understand what I'm doing. I believe that right now you are super pumped and ready to start solving more SQL challenges. So, what is the next step for you? First, I strongly recommend that you should check out and take my other previous SQL projects in this series. The goal of this series is to equip you with the needed SQL skills you need for your current job or to get a relevant job in data analytics. These projects in order are Performing Data Definition-- Performing Data Definition and Data Manipulation in SQL, Query- -ing Databases using SQL SELECT statement, Performing Data Aggregation using SQL Aggregate Functions, Mastering SQL Joins, Using SQL String Functions to Clean Data. We have also SQL CASE Statements. Then I've also covered SQL Date Time Functions. Then, lastly before this particular project, we've done SQL Mathematical Functions. This makes it eight projects so far in this series. I can assure you that you will learn it lots of useful ideas on these projects. Also, one thing you can do is to start making use of the ideas you have gleaned from this project on working with SQL Sub- -queries in real-life. I suggest that you should install Post- -greSQL on your device. It is open source. Then, you can check online for problems that require working with subqueries in SQL. You can start using what you've learned here on your current job. In fact, you can even use the database we've used here or import adatabase of your choice or a CSV file of your choice into Postgre- -SQL and think about the problem you would like to solve with the tables in the database and try to solve these problems using some of the ideas we've covered here. If you encounter any error while working on it, please put your concerns in the discussion forum for other learners to help you out and be assured that I will be there to respond to your questions. Another thing that you can do is to check stack overflow or Quora for or any other platform you can ask questions. Being able to resolve errors is an important skill you must learn as an SQL user. There is no point in learning without practicing what you've learned. You get better as using SQL the more you practice. I assure you that this project has empowered you with the necessary requisite to do much more on your own. This is a very important thing to note. This is 9th project in a series of Mastering SQL for Database Querying and Database Insight. In future projects, we'll consider SQL Stored Procedures, SQL Window Functions, SQL User-defined Functions and many interesting SQL concepts. So, stay in touch from this project, um-- for the next project in this series. Thank you for learning with me on this project. I believe you've enjoyed it. Make sure you give this project the best rating and reviews. See you at another time. All the best!