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.

Slides:



Advertisements
Similar presentations
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
Advertisements

Using the Set Operators
Displaying Data from Multiple Tables
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Structured Query Language (SQL)
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
CORE 2: Information systems and Databases STORAGE & RETRIEVAL 2 : SEARCHING, SELECTING & SORTING.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
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.
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.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
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.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
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.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Introduction to Functions – Single Row Functions.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
 CONACT UC:  Magnific training   
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Aggregating Data Using Group Functions
Basic select statement
Using the Set Operators
Aggregating Data Using Group Functions
Writing Basic SQL SELECT Statements
Using the Set Operators
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Restricting and Sorting Data
Reporting Aggregated Data Using the Group Functions
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
‘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.
Restricting and Sorting Data
Presentation transcript:

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 or descending order –State the order in which expressions are evaluated and calculated based on the rules of precedence –Construct a query to order a results set using a column alias –Construct a query to order a results set for single or multiple columns

3 home back first prev next last Why Learn It? By nature, most of us need order in our lives. Imagine if each time you had dinner, you had to look in every kitchen drawer or cabinet to find a knife and a fork? Ordering, grouping, and sorting makes finding things easier. Biologists group animals in phyla (( ), [ ] ( )), astronomers order brightness of stars by magnitude, and programmers organize Java code in classes. For database design, business functions are ordered by entities and attributes; in database information, SQL uses the ORDER BY clause. Being able to sort results is a convenient feature in SQL and enables programmers to display information in many different ways.

4 home back first prev next last ORDER BY Information sorted in ascending order is familiar to most of us. It's what makes looking up a number in a phone book, finding a word in the dictionary, or locating a house by its street address relatively easy. SQL uses the ORDER BY clause following the FROM clause to order data. The ORDER BY clause can specify several ways in which to order rows returned in a query. –The DJ on Demand example uses the ORDER BY clause to order the years in ascending (default) order.

5 home back first prev next last ORDER BY Important note: The ORDER BY clause must be the last clause of the SQL statement. SELECT title, year FROM d_cds ORDER BY year;

6 home back first prev next last ORDER BY The default sort order is ascending. –Numeric values are displayed lowest to highest. –Date values are displayed with the earliest value first. –Character values are displayed in alphabetical order. –Null values are displayed last in ascending order and first in descending order.

7 home back first prev next last ORDER BY If it is necessary to reverse the default order in the ORDER BY clause, ask for descending order. You can do this by specifying the DESC keyword after the column name in the ORDER BY clause. SELECT title, year FROM d_cds ORDER BY year DESC; How would you order the following dates in descending order? 22-MAY-85, null, 10- JAN-04, 17-NOV-55, 21- DEC-98

8 home back first prev next last ORDER BY It is also possible to order data by using a column alias. The alias used in the SELECT statement is added to the ORDER BY clause. SELECT title, year AS "Recording Date" FROM d_cds ORDER BY "Recording Date ";

9 home back first prev next last ORDER BY It is also possible to use the ORDER BY clause to order output by a column that is not listed in the SELECT clause. SELECT employee_id, first_name FROM employees WHERE employee_id < 105 ORDER BY last_name;

10 home back first prev next last ORDER BY The order of execution of a SELECT statement is as follows: FROM clause -- locates the table that contains the data WHERE clause -- restricts the rows to be returned SELECT clause -- selects from the reduced data set the columns requested ORDER BY -- orders the results set

11 home back first prev next last ORDER BY It is also possible to sort query results by more than one column. In fact, the only limit to how many columns can be added to the ORDER BY clause is the number of columns in the table. Sorting results by more than one column is like first sorting all the students in the school by class or grade level and then within each grade-level group, sorting students alphabetically by last name. SELECT title, year FROM d_cds ORDER BY title, year;

12 home back first prev next last ORDER BY To create an ORDER BY clause to sort by multiple columns, specify the columns to be returned and separate the column names using commas. If after listing the columns you want to reverse the order of a column, add DESC after its name. SELECT title, year FROM d_cds ORDER BY year, title DESC;

13 home back first prev next last Summary In this lesson, you have learned about: –Constructing queries that sorts a results set in ascending or descending order –Stating the order in which expressions are evaluated and calculated based on the rules of precedence –Constructing queries to order results sets using column aliases –Constructing queries to order results sets for single or multiple columns