Lesson 04 (Order & Group Data)
ORDER BY CLAUSE (Sort Command)
Order by clause is used to sort the result by the select statement. ये कमांड query के परिणाम को sort करके प्रदर्शित करती है |
Syntax-
order by column1[ASC|DEC],
Column2[ASC|DEC],......
By default ASC (Ascending Order) होता है |
student table में से सभी छात्रों की जानकारी नाम के ascending order में प्रदर्शित करो |
select * from student order by sname asc;
asc दें या नहीं asc ही होगा लेकिन descending में देने के लिए dec दें |
student table में से marks के घटते क्रम में छात्रों की जानकारी प्रदर्शित करो |
select * from student order by marks dec;
student table में से छात्रों के नाम ascending और उनके marks descending order में प्रदर्शित करो |
select * from student order by sname asc, marks dec;
faculty table में से qualification के निम्न क्रम से प्रदर्शित करो - M.Tech, Ph.D, B.Tech.
select * from faculty order by field(qualification, ‘M.Tech’, ‘Ph.D’, ‘B.Tech’);
Group by & Having Clause (Grouping Data)
select sum(marks) from student;
|--------------------|
| sum(marks) |
|--------------------|
| ------- |
|--------------------|
group by clause group a set of rows into set of summary rows by values of columns or expression.
Generally it is used with aggregate function like sum, avg, max, min and count etc.
Syntax-
select col1, col2, …. aggregate func(col) from tablename where condition group by col1, col2……
Group data with having clause
Syntax-
select cl1, col2,.......aggregate function(col) from tablename where condition group by col1, col2,..... having condition.
Sample Queries
find total number of student in each branch.
select count(*) from student;
select branch, count(*) from student group by branch;
Find total marks of each branch.
select branch, sum(marks) from student GROUP BY branch;
Find average marks of each branch having marks branch only CSE or IT.
select branch, sum(marks) from student GROUP BY branch having branch IN(‘CSE’, ‘IT’);
Sub Query, Exist and Not Exist
sub query(nested query)
sub query is nested query within another queries LIKE, SELECT, INSERT, UPDATE OR DELETE.
Syntax-
select col1, col2…..
from tablename
where condition[=] IN|NOT IN……]{NESTED QUERY}
Sample Queries
Find the name of faculty working in IT department.
select fname from faculty where deptid IN(select deptid from department where dname=’IT’);
Find the name of caculty working in IT or CSE department.
select fname from faculty where deptid IN(select deptid from department where dname IN(‘IT’, ‘CSE’));
Comments
Post a Comment