More clauses in SQL
Hello, and welcome to the blog. In this blog we are going to talk about "More clauses in SQL".
We learn about "WHERE" clause in this blog. Now its time to learn more clause which are going to help us to be more specific about retrieving data from Database and eventually its gonna make us more and more efficient in handling data in Database.
So, In this blog we are going to talk about
- GROUP BY clause
- HAVING clause
- ORDER BY clause
1) GROUP BY clause
- This clause is often used with Aggregate Functions (which we have learnt in previous blog) so that we can group our result with a particular column.
- I know it sound little confusing but we will solve this confusion with the help of example.
- Let's consider a scenario where I told you to count number of people working in each department with our existing Employee table.
- So then you might write a query like this:
- Then you will keep replacing Departments in the query and tell me the result. In this way you completed your task but you wrote 4 queries for that as we have 4 departments.
- Now, what if I tell you we can do the same with GROUP BY clause and in a single query.
2) HAVING clause
- HAVING clause is used when you want to add some condition with Aggregate functions. You might argue now that we use "WHERE" clause for that.
- So, I want to tell you that "WHERE" clause doesn't work with "Aggregate Function". Yes you read it right.
- Consider a scenario where you want to find all Departments where number of Employees =2
- Let's see a example : select count(*), Department from Employee where count(Department)=2
- Error Message says : "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."
- Now, let's try same thing with having clause:
3) ORDER BY clause
- This clause is most simplest one and probably most loved one because you don't really have to think before using it.
- The ORDER BY clause is used to show your desired output in ascending or descending order of a particular column.
- Yes, you can also manage order in which your record should be displayed.
- 'asc' is used for showing data in ascending order and 'desc' used for showing data in descending order
- If you don't specify any order 'asc' or 'desc' then by default ascending order is applied.
- Now, let's have some fun in doing examples:
- Consider a scenario where you want to show data in ascending order of First Name
- select * from Employee order by FirstName
- As you can see a haven't use 'asc' or 'desc' after my column name and I got list sorted in ascending order.
- Now let's see some records in descending order of their salary.
- Now can we use order by with 2 columns? Yes of course, let's see a example
- Now I tell you what has happen when we used order by with 2 columns first it sorts the records with "SALARY" descending then it checks records with same salary and sort them in "FirstName" ascending.
Congratulations on learning some more clauses in select statement. See you in some another blog till then "Keep coding, keep learning because trust me you can make it big".
"Have a great day"
Nice explaination sir 👍
ReplyDelete