SQL Grouping Records, Joins in SQL
SQL Grouping Records, Joins in SQL
Now we write the query – select * from student order by class desc;
Now we write query–select * from student order by class asc, marks asc;
Query result will be ascending order of class and if same class exists
then ordering will done on marks column(ascending order)
MySQL Order by– e.g.
Suppose we are having student table with following data.
Now we write query–select * from student order by class asc, marks desc;
Query result will be ascending order of class and if same class exists
then ordering will done on marks column(descending order)
An aggregate function performs a calculation on multiple values SQL
and returns a single value. For example, you can use the AVG()
aggregate function that takes multiple numbers and returns the
average value of the numbers.Following is the list of aggregate
functions supported by mysql.
Name Purpose
SUM() Returns the sum of given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
Aggregate Functions & NULL
Consider a table Emp having following records as-
SQL
Null values are excluded while (avg)aggregate function is used
Emp
Code Name Sal
E1 Mohak NULL
E2 Anuj 4500
E3 Vijay NULL
E4 Vishal 3500
SQL Queries E5 Anil 4000
Result of query
mysql> Select Sum(Sal) from EMP; 12000
mysql> Select Min(Sal) from EMP; 3500
mysql> Select Max(Sal) from EMP; 4500
mysql> Select Count(Sal) from EMP; 3
mysql> Select Avg(Sal) from EMP; 4000
mysql> Select Count(*) from EMP; 5
The GROUP BY clause groups a set of rows/records into a
SQL
set of summary rows/records by values of columns or
expressions. It returns one row for each group.
We often use the GROUP BY clause with aggregate
functions such as SUM, AVG, MAX, MIN, and COUNT. The
aggregate function that appears in the SELECT clause
provides information about each group.
The GROUP BY clause is an optional clause of the SELECT
statement.
Syntax –
SELECT 1, c2,..., cn, aggregate_function(ci)
FROM table WHERE where_conditions GROUP BY c1 , c2,...,cn;
Here c1,c2,ci,cn are column name
MySQL group by – e.g. SQL
Suppose we are having student table with following data.
Query result will be unique occurrences of class values along with counting of
students(records) of each class(sub group).
MySQL GROUP BY with aggregate functions SQL
we are having student table with following data.
Query result will be unique occurrences of class values along with average
marks of each class(sub group).
SQL
MySQL GROUP BY with aggregate functions (with where and order by clause)
we are having student table with following data.
Query result will be unique occurrences of class values where class<10 along with
average marks of each class(sub group) and descending ofer of marks.
SQL
Query result will be unique occurrences of class values along with average
marks of each class(sub group) and each class having average marks<90.
MySQL GROUP BY with aggregate functions & having clause
we are having student table with following data.
Query result will be unique occurrences of class values along with average
marks of each class(sub group) and each class having less than 3 rows.
SQL
Types of JOIN
Following are the types of JOIN that we can use in SQL:
• Inner
• Outer
• Left
• Right
SQL
Mysql query –
Select * from a left outer join b on
(a.name=b.name) union Select * from
a right outer join b on
(a.name=b.name) ;
Thank you