Database Foundations. 6-8 Sorting Data Using ORDER BY. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Similar documents
Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Appendix A Practices and Solutions

Oracle Database: SQL and PL/SQL Fundamentals

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Microsoft Access Lesson 5: Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals NEW

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Oracle Database 10g: Introduction to SQL

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Producing Readable Output with SQL*Plus

Oracle Database 11g SQL

SQL. Short introduction

Oracle Database 12c: Introduction to SQL Ed 1.1

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Learning MySQL! Angola Africa SELECT name, gdp/population FROM world WHERE area > !

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

A basic create statement for a simple student table would look like the following.

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Oracle Database: SQL and PL/SQL Fundamentals NEW

Introduction to Microsoft Jet SQL

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Information Systems SQL. Nikolaj Popov

Oracle 10g PL/SQL Training

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Using AND in a Query: Step 1: Open Query Design

Darshan Institute of Engineering & Technology PL_SQL

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

Database Query 1: SQL Basics

Oracle Database: Introduction to SQL

SQL Basics. Introduction to Standard Query Language

Admin Guide Product version: Product date: November, Technical Administration Guide. General

Welcome to the topic on queries in SAP Business One.

Displaying Data from Multiple Tables

Where? Originating Table Employees Departments

A Brief Introduction to MySQL

3.GETTING STARTED WITH ORACLE8i

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

CHAPTER 12. SQL Joins. Exam Objectives

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Each Ad Hoc query is created by making requests of the data set, or View, using the following format.

Database: SQL, MySQL

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

GET DATA FROM MULTIPLE TABLES QUESTIONS

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Database Administration with MySQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

FileMaker 13. SQL Reference

Database Programming with PL/SQL: Learning Objectives

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

Using the SQL Procedure

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Oracle Internal & Oracle Academy

A table is a collection of related data entries and it consists of columns and rows.

Performing Queries Using PROC SQL (1)

Structured Query Language (SQL)

Querying Microsoft SQL Server

IBM DB Introduction to SQL and database objects Hands-on Lab. Information Management Cloud Computing Center of Competence.

Chapter 22 Database: SQL, MySQL,

Comp 5311 Database Management Systems. 3. Structured Query Language 1

VBScript Database Tutorial Part 1

Chapter 1 Overview of the SQL Procedure

Intro to Embedded SQL Programming for ILE RPG Developers

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Netezza SQL Class Outline

Oracle Fusion Middleware

ETL TESTING TRAINING

Course ID#: W 35 Hrs. Course Content

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

Oracle Database 10g: SQL Fundamentals I

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Querying Microsoft SQL Server 20461C; 5 days

Lab 2: MS ACCESS Tables

How To Create A Table In Sql (Ahem)

Oracle Database 10g Express

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

The join operation allows you to combine related rows of data found in two tables into a single result set.

Data Migration. External Table version 1


Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Access Queries (Office 2003)

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Introduction to SQL and database objects

MOC QUERYING MICROSOFT SQL SERVER

ICAB4136B Use structured query language to create database structures and manipulate data

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Transcription:

Database Foundations 6-8

Roadmap Introduction to Oracle Application Express Structured Query Language (SQL) Data Definition Language (DDL) Data Manipulation Language (DML) Transaction Control Language (TCL) Retrieving Data Using SELECT Restricting Data Using WHERE Sorting Data Using ORDER BY Joining Tables Using JOINS You are here 3

Objectives This lesson covers the following objectives: Use the ORDER BY clause to sort SQL query results Identify the correct placement of the ORDER BY clause within a SELECT statement Order data and limit row output by using the SQL row_limiting_clause Use substitution variables in the ORDER BY clause 4

Using the ORDER BY Clause Sort the retrieved rows with the ORDER BY clause: ASC: Ascending order (default) DESC: Descending order The ORDER BY clause comes last in the SELECT statement: SELECT last_name, job_id, department_id, hire_date ORDER BY hire_date ; 5

Sorting Sorting in descending order: SELECT last_name, job_id, department_id, hire_date ORDER BY hire_date DESC ; 1 Sorting by column alias: SELECT employee_id, last_name, salary*12 annsal ORDER BY annsal ; 2 6

Sorting Sorting by using the column's numeric position: SELECT last_name, job_id, department_id, hire_date 3 ORDER BY 3; Sorting by multiple columns: SELECT last_name, department_id, salary ORDER BY department_id, salary DESC; 4 7

SQL row_limiting_clause The row_limiting_clause allows you to limit the rows that are returned by the query. Queries that order data and then limit row output are widely used and are often referred to as Top-N queries. You can specify the number of rows or percentage of rows to return with the FETCH_FIRST keywords. 8

SQL row_limiting_clause You can use the OFFSET keyword to specify that the returned rows begin with a row after the first row of the full result set. The WITH TIES keyword includes additional rows with the same ordering keys as the last row of the row-limited result set (you must specify ORDER BY in the query). You can specify the ORDER BY clause to ensure a deterministic sort order. 9

Using SQL Row Limiting Clause Using the SQL row_limiting_clause in a Query You can specify the row_limiting_clause in the SQL SELECT statement by placing it after the ORDER BY clause. subquery::= { query_block subquery { UNION [ALL] INTERSECT MINUS } subquery [ { UNION [ALL] INTERSECT MINUS } subquery ]... ( subquery ) { [ order_by_clause ] [OFFSET offset { ROW ROWS }] [FETCH { FIRST NEXT } [{ row_count percent PERCENT }] { ROW ROWS } { ONLY WITH TIES }] 10

SQL row_limiting_clause: Example SELECT employee_id, first_name ORDER BY employee_id FETCH FIRST 5 ROWS ONLY; SELECT employee_id, first_name ORDER BY employee_id OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY; 11

Substitution Variables... salary =? department_id =?... last_name =?... I want to query different values. 12

Substitution Variables Use substitution variables to temporarily store values with single-ampersand (&) and double-ampersand (&&) substitutions. Use substitution variables to supplement the following: WHERE conditions ORDER BY clauses Column expressions Table names Entire SELECT statements 13

Using the Single-Ampersand Substitution Variable Use a variable prefixed with an ampersand (&) to prompt the user for a value: SELECT employee_id, last_name, salary, department_id WHERE employee_id = &employee_num ; 14

Using the Single-Ampersand Substitution Variable 15

Character and Date Values with Substitution Variables Use single quotation marks for date and character values: SELECT last_name, department_id, salary*12 WHERE job_id = '&job_title' ; 16

Specifying Column Names, Expressions, and Text SELECT employee_id, last_name, job_id, &column_name WHERE &condition ORDER BY &order_column ; 17

Using the Double-Ampersand Substitution Variable Use double ampersands (&&) if you want to reuse the variable value without prompting the user each time: SELECT employee_id, last_name, job_id, &&column_name ORDER BY &column_name ; 18

Using the DEFINE Command Use the DEFINE command to create and assign a value to a variable. Use the UNDEFINE command to remove a variable. DEFINE employee_num = 200 SELECT employee_id, last_name, salary, department_id WHERE employee_id = &employee_num ; UNDEFINE employee_num 19

Using the VERIFY Command Use the VERIFY command to toggle the display of the substitution variable before and after SQL Developer replaces substitution variables with values: SET VERIFY ON SELECT employee_id, last_name, salary WHERE employee_id = &employee_num; 20

Summary In this lesson, you should have learned how to: Use the ORDER BY clause to sort SQL query results Identify the correct placement of the ORDER BY clause within a SELECT statement Order data and limit row output by using the SQL row_limiting_clause Use substitution variables in the ORDER BY clause 21