SQL review-Grouping

Source: Internet
Author: User

--Data Grouping-statistical information--total number of queries per classSelect COUNT(8) fromStudentSelectClassId,COUNT(*) fromStudentGroup  byClassId--get the total number of boys, girls group by by the specified field categorySelectSexCOUNT(*) fromStudentGroup  bySex--other columns that appear in the query along with the aggregation function have only two possibilities: they are aggregated, grouped, and are often groupedSelectSexCOUNT(*) fromStudentSelect distinctSex fromStudentSelect COUNT(*) fromStudentSelectClassIDCOUNT(*) fromStudentSelect *  fromStudent--order cannot be changed select (5) Top/distinct 7 query field from (1) Table list where (2) filter the source Data group by (3) Grouping statistic field list having (4) filtering the grouping statistic result set order by (6) record rearrangement of final result set--where is the filtering of the source data. It can only use the columns specified in the table following the from--aggregations should not appear in the WHERE clause, meaning that the condition of the aggregate function cannot be specified by where--If you are filtering the result set after grouping, you need to use the having--The having column is obtained from group by--The grouping statistic result set is filtered before the data is displayed. SelectClassId,COUNT(*) num fromStudentGroup  byClassIdOrder  byNumdescSelect Top 2ClassId,COUNT(*) num fromStudentGroup  byClassIdOrder  byNumdesc--1. Query the total number of hours per class and in ascending order--2. Check the average score for each student taking the exam--3. Check the average score for each course and arrange it in descending order--4. Check the number of male and female students in each class--when you see something like: individual, individual, each, different these words need to consider groupingSelectClassId,SUM(Classhour) fromSubjectwhereClassId is  not NULL Group  byClassIdSelectStudentno,AVG(Studentresult) fromResultGroup  byStudentnoSelectSubjectid,AVG(Studentresult) fromResultGroup  bySubjectid--Check the number of male and female students in each classSelectClassid,sex,COUNT(*) fromStudentGroup  byClassid,sexOrder  byClassid,sex

SQL review-Grouping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.