SQL queries ordering and grouping. SWC – 2011 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

Slides:



Advertisements
Similar presentations
Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Advertisements

Microsoft Excel Working with Excel Lists, Subtotals and Pivot Tables.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
1 CSE Students: Please do not log in yet. Check-in with Brian in the back. Review Days 3 and 4 in the book. Others: Please save your work and logout.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
Mary K. Olson PS Reporting Instance – Query Tool 101.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Chapter 3 Single-Table Queries
With Microsoft Office 2007 Introductory© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Introductory.
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
Practical Session 13 Structured Data Bases Structured Query Language Exam Questions Factory Method Pattern Abstract Factory Pattern.
Introduction to Microsoft Access 2003 Mr. A. Craig Dixon CIS 100: Introduction to Computers Spring 2006.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 4 BACKNEXTEND 4-1 LINKS TO OBJECTIVES Query Design Query Criteria Modify a Query Using OR.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Querying a Database - A question or an inquiry (dictionary.com) - WHAT ARE WE ASKING QUESTIONS ABOUT? THE DATA - BY ASKING QUESTIONS OF THE DATA WE OBTAIN?
SQL queries subqueries and joining. RHS – SOC 2 SQL query – beyond one table So far, we have only applied queries to a single table It is possible – and.
SQL queries ordering and grouping and joins
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Database Management Systems.  Database management system (DBMS)  Store large collections of data  Organize the data  Becomes a data storage system.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
Nikolay Kostov Telerik Corporation
SQL queries basics. RHS – SOC 2 SQL query An SQL query is an SQL statement, which specifies a subset of the data in the database A subset in terms of.
Copyright © Curt Hill Queries in SQL More options.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.
SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.
Databases Competency Explain advanced database concepts and functions.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Structured Query Language SQL Unit 4 Solving Problems with SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
SQL queries ordering and grouping. RHS – SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.
Practical Session 13 Factory Method Pattern Abstract Factory Pattern Structured Data Bases Structured Query Language Exam Questions.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Paul A. Harris, Ph.D. Director, GCRC Informatics How to use query function in Microsoft Access.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Relational Databases Today we will look at: Different ways of searching a database Creating queries Aggregate Queries More complex queries involving different.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
The Database Exercises Fall, 2009.
Microsoft Access 2003 Illustrated Complete
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
GROUP BY & Subset Data Analysis
CIS16 Application Programming with Visual Basic
SQL – Entire Select.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Access: SQL Participation Project
SQL Aggregation.
M1G Introduction to Database Development
Section 4 - Sorting/Functions
Projecting output in MySql
‘ORDER BY’ Order by clause allows sorting of query results by one or more columns. Sorting can be done in ascending or descending. Default order is ascending.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

SQL queries ordering and grouping

SWC – SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL allows us to order the result by any of the fields in the result We use the keyword ORDER BY

SWC – SQL query - ordering SELECT FROM WHERE ORDER BY Which fields do I want From what table do I want the fields What conditions must the fields fulfill What order are the results sorted in

SWC – SQL query - ordering We use a movie information database as example Movie movieid title country prodyear genre oscars Actor actorid name country birth living oscars Casting movieid actorid

SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie ORDER BY title

SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 61984UK1984Sci-Fi2 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 7SevenUSA1995Thriller1 2TaxiFrance1998Comedy0 SELECT * FROM Movie ORDER BY title

SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title

SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title

SWC – SQL query - ordering titleprodyear 1984 E.T.1982 Hunger1966 Seven1995 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title

SWC – SQL query - ordering We can even specifiy more than one field for ordering – secondary fields used if primary fields are identical We can choose between descending and ascending order, using the keywords DESC and ASC, respectively ORDER BY oscars DESC, prodyear ASC

SWC – SQL query - functions We can even do some (simple) arithmetic in SQL, using a basic set of functions –COUNT –SUM –AVG –MIN –MAX These are called aggregate functions

SWC – SQL query - functions A aggregate function works on the values of a specific field (column) The set of values is determined by the search conditions SELECT COUNT(title) FROM Movie WHERE (oscars > 0) How many movies have won an Oscar

SWC – SQL query - functions This query can also be written as SELECT COUNT(title) AS OscarWinners FROM Movie WHERE (oscars > 0) The AS keyword allows us to rename a column in the search result Only cosmetic, but useful… NB!

SWC – SQL query - functions SELECTCOUNT(title) AS OscarWinners, AVG(oscars) AS averageOscars, MAX(oscars) AS maximalOscars FROMMovie WHERE(oscars > 0)

SWC – SQL query - functions OscarWinnersaverageOscarsmaximalOscars

SWC – SQL query - grouping The aggregate functions are good for calculating properties for the entire result of the search We may sometimes wish to find proper- ties for a certain group within the result This can be done using WHERE… …but can be cumbersome if the groups are very numerous

SWC – SQL query - grouping Suppose we have a full movie database, with movies for more than 100 countries ”Find the total number of movies made in each country” SELECT COUNT(title) AS MovieCount FROM Movies WHERE country = ’…’ More than 100 queries…

SWC – SQL query - grouping A much easier way is to use GROUP BY Syntax: SELECT FROM GROUP BY Produces a result for each group, specified in the field list

SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country

SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country

SWC – SQL query - grouping CountryMovieCount Denmark1 France2 HK1 UK1 USA2

SWC – SQL query - grouping CountryOscarTotal Denmark1 France0 HK0 UK2 USA5 SUM(oscars) AS …

SWC – SQL query - grouping In the last example, it might be desirable to leave out results where total is zero In general; only include groups which fulfill some criteria This can be done using the HAVING keyword

SWC – SQL query - grouping Syntax: SELECT FROM GROUP BY HAVING

SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)

SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)

SWC – SQL query - grouping CountryOscarTotal USA5 France0 UK2 HK0 Denmark1

SWC – SQL query - grouping CountryOscarTotal USA5 UK2 Denmark1

SWC – SQL query - grouping But wait… …isn’t HAVING the same as WHERE..? Not quite –WHERE is for filtering out specific records –HAVING is for filtering out specific groups from the final result We cannot use an aggregate function in a WHERE clause