Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Relational Database Systems Higher Information Systems Advanced Implementation in Microsoft Access.
Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Chapter 11 Group Functions
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Structured Query Language (SQL)
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Structured Query Language Part I Chapter Three CIS 218.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Computer Science 101 Web Access to Databases SQL – Extended Form.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
Grouping Data. GROUP BY clause Groups results by column name used with aggregate functions must come first when used with ORDER BY.
Copyright © Curt Hill Queries in SQL More options.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Selecting Data Database Administration Fundamentals LESSON 3.1a.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
CS 3630 Database Design and Implementation
SQL Query Getting to the data ……..
Structured Query Language
Writing Basic SQL SELECT Statements
Aggregating Data Using Group Functions
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Using the Set Operators
Aggregating Data Using Group Functions
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
CS 3630 Database Design and Implementation
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Access: SQL Participation Project
Writing Basic SQL SELECT Statements
Database solutions Selected SQL commands – part 1 Marzena Nowakowska Faculty of Management and Computer Modelling Kielce University of Technology rooms:
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
MySQL SQL for MySQL (I) Salim Mail :
Presentation transcript:

Sorting data and Other selection Techniques

Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw data, we can sort on one or many different criteria. This will allow us and/or the user to focus on the items of interest.

ORDER BY clause Simplest form: SELECT column FROM table ORDER BY column

Nested Ordering Ordering can occur on a number of levels. When you ORDER BY on more than one column the first column takes highest precedence and goes down the line. Example: SELECT author, price, title FROM books ORDER BY price, title, author The results would be ordered by price, then title and then author… in ascending order. Ascending order is the default.

Sort order We can change sort order using the DESC (for descending) and ASC (for ascending) keywords. Example: SELECT author, title, price FROM books ORDER BY price desc, author Note: author would still be sorted ascending. DESC only affects the price column.

Ordering Expressions When we want to order expressions we need to refer to the expression by its alias or its position. Example: SELECT price *.9 disc, author, title FROM books ORDER BY disc, title or ODER BY 1, title

Eliminating Duplicates ALL the default returns all qualified rows DISTINCT returns only unique when more than 1 item in SELECT list, returns only the unique combinations. Both can only occur once and only at the beginning of the SELCT list. SELECT DISTINCT book_store FROM stores

Non-select list Order By On systems that allow it, having a column listed in the ORDER BY and not in the SELECT list broadens the scope of the query to include that column as well. The column is NOT displayed in the results. Only the affected rows.

Grouping data Grouping data allows us to view aggregated data in reference to a field of interest. This makes for a powerful reporting tool.

GROUP BY clause Groups results by column name used with aggregate functions

Syntax SELECT col_1, max(col_2) FROM table GROUP BY col_1 column in GROUP BY must be in SELECT list There can be multiple columns in GROUP BY. Where their list order is their order of precedence.

Limitations There must be only 1 value returned for each GROUP BY field. Should only use column names Due to these limitations, many vendors provide report generators

Grouping and Nulls Nulls are assigned a group of there own since they are, in a sense, their own datatype.

HAVING clause Same as WHERE clause except it allows aggregate functions

Formatting Text getDate() - returns today’s date SELECT getDate() char_length(data) - returns the length SELECT char_length(‘John’) upper(data) - returns the uppercase lower(data) - returns the lowercase concatonation SELECT (name +` `+address+`,`+state)