Loading, please wait ...
Open its course - Database Systems Labs
Sorting and Grouping in SQL By Mukhtiar Zamin
Summary

Summary

4
Teachers with
Mukhtiar Zamin
19
Followers
For Learning
All
Visibility
Security Status
6
Contributions
By Teachers
Notes
  1. Students are auto-evaluated against related topics to ensure they learned it.

In the lab your will learn 'Sorting Data', 'Grouping Data' and 'Aggregate Operations Basics'

Sorting Results (ORDER BY Clause)

The ORDER BY clause have a list of columns which sort the result in respective order. A column may be either a column name or a column number.

Example: Produce a list of salaries for all staff, arranged in descending order of salary.

SELECT staffNo, fName, lName, salary FROM Staff ORDER BY salary DESC;

It is possible to include more than one element in the ORDER BY clause. The major sort key determines the overall order of the result table.

Example: Produce an abbreviated list of properties arranged in order of property type.

SELECT propertyNo, type, rooms, rent FROM PropertyForRent ORDER BY type;
SELECT propertyNo, type, rooms, rent FROM PropertyForRent ORDER BY type, rent DESC;

Using the SQL Aggregate Functions for All, By Mukhtiar Zamin

To perform some form of summation or aggregation of data, similar to the totals at the bottom of a report. The ISO standard defines five aggregate functions:

  • COUNT – returns the number of values in a specified column.
  • SUM – returns the sum of the values in a specified column.
  • AVG – returns the average of the values in a specified column.
  • MIN – returns the smallest value in a specified column.
  • MAX – returns the largest value in a specified column.

COUNT(*) is a special use of COUNT, which counts all the rows of a table, regardless of whether nulls or duplicate values occur. 

It is important to note that an aggregate function can be used only in the SELECT list and in the HAVING clause. The following query is illegal:

SELECT staffNo, COUNT(salary) FROM Staff;
Example: How many properties cost more than £350 per month to rent?
SELECT COUNT(*) AS myCount FROM PropertyForRent WHERE rent <=350;
Example: How many different properties were viewed in May 2004?
SELECT COUNT(DISTINCT propertyNo) AS myCount FROM Viewing WHERE viewDate BETWEEN ’1-May-04’ AND ’31-May-04’;
Example: Find the total number of Managers and the sum of their salaries.
SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff WHERE position = ’Manager’;
Example: Find the minimum, maximum, and average staff salary.
SELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvg FROM Staff;

Aggregate function can be used only in the SELECT list and in the HAVING clause. If the SELECT list includes an aggregate function and no GROUP BY clause is being used to group data together, then no item in the SELECT list can include any reference to a column unless that column is the argument to an aggregate function For example, the following query is illegal:

SELECT staffNo, COUNT(salary) FROM Staff;

The ISO standard requires the SELECT clause and the GROUP BY clause to be closely integrated. When GROUP BY is used, each item in the SELECT list must be singlevalued per group. Further, the SELECT clause may contain only:

  • column names.
  • aggregate functions.
  • constants.
  • an expression involving combinations of the above.

Restricting groupings (HAVING clause) for All, By Mukhtiar Zamin

TheWHERE clause filters individual rows going into the final result table, whereas HAVING filters groups going into the final result table. The ISO standard requires that column names used in the HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function.

Subquery Rules for All, By Mukhtiar Zamin

The following rules apply to subqueries:

  1. The ORDER BY clause may not be used in a subquery (although it may be used in the outermost SELECT statement).
  2. The subquery SELECT list must consist of a single column name or expression, except for subqueries that use the keyword EXISTS
  3. By default, column names in a subquery refer to the table name in the FROM clause of the subquery. It is possible to refer to a table in a FROM clause of an outer query by qualifying the column name.
  4. When a subquery is one of the two operands involved in a comparison, the subquery must appear on the right-hand side of the comparison. For example, it would be incorrect to express the last example as:
    SELECT staffNo, fName, lName, position, salary FROM Staff WHERE (SELECT AVG(salary) FROM Staff) < salary;

ANY and ALL keywords for All, By Mukhtiar Zamin

The keywords ANY and ALL may be used with subqueries that produce a single column of numbers. For ALL the condition will only be true if it is satisfied by all values produced by the subquery For ANY the condition will be true if it is satisfied by any (one or more) values produced by the subquery

Example: Find all staff whose salary is larger than the salary of at least one member of staff at branch B003.
SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary > SOME (SELECT salary FROM Staff WHERE branchNo = ’B003’);
Example: Find all staff whose salary is larger than the salary of every member of staff at branch B003.
SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary > ALL (SELECT salary FROM Staff WHERE branchNo = ’B003’);

Lab Tasks for All, By Mukhtiar Zamin

  1. Perform all the examples in this lab.
  2. For DreamHome case study write at least 3 examples of each category for sorting, grouping and aggregate operations. Share your exercises as advised in the lab. 

References for All, By Mukhtiar Zamin

×

Sorting and Grouping in SQL Evaluation

To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.

  • CAREFULLY READ THE FOLLOWING INSTRUCTIONS!
  • Make sure you have learned this lecture along with its topics and tutorials.
  • On first evaluation you get 100% marks on correct answer, then on 2nd you get 95% marks and so on.
  • Answer the questions with a gap NOT MORE THAN A MINUTE, Otherwise it will re-start.

I have read above instrucitons and ready for evaluation.
Your's
Status
Not Registered
Teacher
Mukhtiar Zamin
Alert
Your performance monitoring will start when you register in a class of this course.

Questions on

Contact Us

support@subexpert.com
Write to Us View Help
Subject Expert Logo

Subject Expert

Learn, Evaluate and Optimize

Follow Us
Facebook Switch Display Mode Enable Translation
© 2024 - Subject Expert