0% found this document useful (0 votes)
5 views

Chapter 7 Querying Using SQL

Chapter 7 discusses querying using SQL, focusing on aggregate functions such as AVG, COUNT, MAX, MIN, and SUM, which operate on single or multiple rows of data. It explains how to use the ORDER BY clause for sorting results, the GROUP BY clause for grouping records, and the HAVING clause for placing conditions on groups. The chapter also includes examples and solved problems to illustrate the application of these SQL functions and clauses.

Uploaded by

mainshabhatnagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 7 Querying Using SQL

Chapter 7 discusses querying using SQL, focusing on aggregate functions such as AVG, COUNT, MAX, MIN, and SUM, which operate on single or multiple rows of data. It explains how to use the ORDER BY clause for sorting results, the GROUP BY clause for grouping records, and the HAVING clause for placing conditions on groups. The chapter also includes examples and solved problems to illustrate the application of these SQL functions and clauses.

Uploaded by

mainshabhatnagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter – 7

Querying Using SQL


Aggregate Functions:
Single Row functions - Single row functions are the one that works on a single row
and return one output per row. For example, length and case conversion functions are
single-row functions.
Group function/Aggregate functions/Multiple row functions - group functions or
aggregate functions work upon group of rows and return one result for the complete set
of rows.

1. AVG – This function computes the average of given data.


Example – Calculate average salary of all employees listed in table empl

Q- Write a query to find the average salary of employee for job of salesman.
2. COUNT – This function is used to counts the number of values in a column or
expression. While counting it ignore the NULL values.
Count(*) – Returns the number of rows satisfying the condition, if any in
the table.
Count (Column Name) – Return Non-NULL values in the column.
Example 1 – Count number of records in table emp.
Example 2 – Count Number of jobs in table empl i.e. count values in job column.

Example 3 – Count non-null values of commission column in table empl

Example 4 – How many distinct jobs are listed in table empl ?

3. MAX – This function returns the maximum value from a given column or
expression.
Example 1 – Display maximum salary from table empl.

Q1.Display maximum salary in empl for job salesman.


Q2- Write the output of following query:

4. MIN - Returns the minimum value from a given column or expression.


Example 1 – Display the joining date of senior most employee.

Q1. Write a query to display minimum commission from table empl.


5. SUM – This function returns the sum of value in given column or
expression.
Example 1 – Display total salary of all employees listed in table empl.

Q1. Display total salary for job salesman.

Aggregate functions and NULL values: All aggregate functions ignore NULL values.
The * is the only argument that includes NULLs when it is used with COUNT function.
Example 1 – Following query output indicate that there are 5 records in the table empl.
Example 2 –

Output indicates that there are 3 values in the comm Column of empl table.
This feature of aggregate functions ensures that NULLs don’t play any role in
actual calculations. For example

The average comm has been calculated for all the 3 non-NULL values.

Ordering/Sorting the Results- ORDER BY


ORDER BY Clause - Used to arrange the selected rows in ascending or in descending
order.
Ordering Data on the basis of an Expression:
Sometimes, we need to display the result of a calculation or a mathematical expression
in the result set. In such case, we may want to arrange our result-set in the order of the
calculated expression.

Specifying Custom Sort Order : We can also arrange data as per our own specified
order. We can do this by using the FIELD function in ORDER BY clause.

GROUPING RESULT – GROUP BY :


The GROUP BY clause combines all those records that have the same values in a
particular field or a group of fields. This statement divides the table into groups.
Example 1 : Count the number of employees in each job/grade.
Example 2 : Calculate Total Salary Department-wise.

Example 3: Calculate Total Commission jobwise.

Nested Groups - Grouping on Multiple Columns :


With GROUP BY we can create groups within groups. Such type of grouping is called
Nested Grouping.
Example – 1 : Write a query to count the number of employees department-wise and
job-wise.
Example – 2 : Write a query to find the sum of salary department-wise and in same
department job-wise also.

Only for department-wise

Placing Conditions on Groups – HAVING Clause : The HAVING clause places


conditions on group in contrast to WHERE clause that places condition on individual
rows. While WHERE condition cannot include aggregate functions HAVING condition can
do so.
Example 1 : Count the number of employees for all department where in
number of employees are 3 or more than 3.
Example 2: To count the number of employees jobwise where the number of
employees is less than 3.

Example 3 : To calculate the average commission and total commission


department wise where average commission is more than 500.

Example 4 : We can use an aggregate function in the HAVING even if it is not in the
SELECT list.
Query - Find the department-wise total salary where number of employees is more
than 2.

Example 5 : We can also use IN with having:


Non-Group Expression with Group By:

Solved Problems :
1 – 5, 9, 11, 13,15, 17 – 19
Assignment
Type – A
Q1, 10,
Type – B
Q10 , 11

You might also like