• Skip to main content
  • Skip to primary sidebar

Datannum

Data science for small business applications

  • Home
  • Microsoft Excel (spreadsheet) forum
    • Project: Analyze NYSE Data
  • SQL forum
    • Project: Query a Digital Music Store Database
  • Tableau forum (data visualization)
    • Project: Data Visualization Project using Tableau
  • Linear Algebra forum
    • Curated resource to learn linear algebra
  • Web development
    • WordPress forum
    • Google Support

GROUP BY and ORDER BY clauses: Is its usage correct ?

Data Science for Small Business Applications › Forums › SQL › GROUP BY and ORDER BY clauses: Is its usage correct ?

Tagged: Group By in SQL, Order By in SQL

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • April 20, 2022 at 8:50 am #133
    Admin
    Keymaster

      Here is the problem:

      The question in this quiz refer to the open source Chinook Database. Please familiarize yourself with the ER diagram to familiarize yourself with the table and column names to write accurate queries and get the appropriate answers.

      Run Query: Show the number of orders placed by each customer (hint: this is found in the invoices table) and sort the result by the number of orders in descending order.

      Here is my solution:

      SELECT Count(InvoiceId) AS NoOfOrdersbyEachCustomer
      FROM Invoices
      GROUP BY CustomerId
      ORDER BY  NoOfOrdersbyEachCustomer DESC

      Group By, Order By clause in SQL

      Is it correct?


      Reply


      Although it is correct, not very useful as you cannot say which order by which customer. Add customerid in the select clause.


      UPDATE


      After adding CustomerId with SELECT, the output appears useful

      Group By, Order By in SQL

       

    • Author
      Posts
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Primary Sidebar

    profile for Techcosec Limited on Stack Exchange, a network of free, community-driven Q&A sites

    RSS CS50’s Introduction to Computer Science by Harvard University on Edx

    • How to print winner January 21, 2023
    • find_min function and infinite loop when a candidate not winner in first instance January 17, 2023
    • Bool variable won and print_winner function January 12, 2023
    • True condition with while loop January 6, 2023
    • How to finally display result in tabular form of choices entered by voters December 12, 2022

    Subscribe via Email

    Enter your email address to subscribe and receive notifications of new posts by email.

    This website hosted on WP Engine

    Initiative by Digital Splendid OPC Pvt. Ltd. & Techcosec Limited