RESTRICTING AND SORTING DATA

Similar documents
Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

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

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

chapter 2 G ETTING I NFORMATION FROM A TABLE

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

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Getting Information from a Table

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Oracle Database 10g: SQL Fundamentals I

CSC Web Programming. Introduction to SQL

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

30. Structured Query Language (SQL)

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

SQL Structured Query Language Introduction

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Data Manipulation Language

Database Programming - Section 18. Instructor Guide

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

Objectives. After completing this lesson, you should be able to do the following:

Full file at

Using DbVisualizer Variables

Database Programming with SQL

Data Manipulation Language (DML)

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Ten Great Reasons to Learn SAS Software's SQL Procedure

Creating Other Schema Objects

SQL. Char (30) can store ram, ramji007 or 80- b

Introduction to PeopleSoft Query. The University of British Columbia

Chapter 3: Introduction to SQL

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Jarek Szlichta

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Structure Query Language (SQL)

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Part III. Data Modelling. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

SELF TEST. List the Capabilities of SQL SELECT Statements

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Database Management Systems,

L e a r n S q l select where

Oracle Database 10g: Introduction to SQL

Oracle Database 11g: SQL and PL/SQL Fundamentals

Queries. Chapter 6. In This Chapter. c SELECT Statement: Its Clauses and Functions. c Join Operator c Correlated Subqueries c Table Expressions

GIFT Department of Computing Science. [Spring 2016] CS-217: Database Systems. Lab-3 Manual. Single Row Functions in SQL

Database implementation Further SQL

CS 275 Winter 2011 Problem Set 3

CS 582 Database Management Systems II

Chapter 4: SQL. Basic Structure

COMP102: Introduction to Databases, 4

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Lesson 2. Data Manipulation Language

Enhanced Data Models for Advanced Applications. Active Database Concepts

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

Data Modelling and Databases Exercise dates: March 22/March 23, 2018 Ce Zhang, Gustavo Alonso Last update: March 26, 2018.

Introduction to Oracle9i: SQL

Database Programming with SQL

The SQL data-definition language (DDL) allows defining :

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Data Infrastructure IRAP Training 6/27/2016

You can write a command to retrieve specified columns and all rows from a table, as illustrated

Relational Database Management Systems for Epidemiologists: SQL Part I

HNDIT 1105 Database Management Systems

Database Programming with PL/SQL

Relational Databases. APPENDIX A Overview of Relational Database Structure and SOL

Access 2003 Introduction to Report Design

1 SQL Structured Query Language

Deepak Bhinde PGT Comp. Sc.

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Relational Algebra and SQL

SQL functions fit into two broad categories: Data definition language Data manipulation language

Intermediate SQL: Aggregated Data, Joins and Set Operators

12. MS Access Tables, Relationships, and Queries

Access - Introduction to Queries

SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

New Perspectives on Access Module 5: Creating Advanced Queries and Enhancing Table Design

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

CS2 Current Technologies Lecture 2: SQL Programming Basics

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

IBM Software Group Information Management Software. The Informix Detective Game (Student Handout)

CSE 530A SQL. Washington University Fall 2013

Creating and Managing Tables Schedule: Timing Topic

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

Practice for Test 1 (not counted for credit, but to help you prepare) Time allowed: 1 hour 15 minutes

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

CS2 Current Technologies Note 1 CS2Bh

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

XQ: An XML Query Language Language Reference Manual

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

5 SQL (Structured Query Language)

Transcription:

RESTRICTING AND SORTING DATA http://www.tutorialspoint.com/sql_certificate/restricting_and_sorting_data.htm Copyright tutorialspoint.com The essential capabilities of SELECT statement are Selection, Projection and Joining. Displaying specific columns from a table is known as a project operation. We will now focus on displaying specific rows of output. This is known as a select operation. Specific rows can be selected by adding a WHERE clause to a SELECT query. As a matter of fact, the WHERE clause appears just after the FROM clause in SELECT query hierarchy. The sequence has to be maintained in all scenarios. If violated, Oracle raises an exception. Syntax: SELECT * {[DISTINCT] column expression [alias],..} FROM table [WHERE condition(s)] In the syntax, WHERE clause is the keyword [condition] contains column names, expressions, constants, literals and a comparison operator. Suppose that your manager is working on the quarterly budget for your organization. As part of this activity, it is necessary to produce a listing of each employee's essential details, but only for employees that are paid at least $25,000 annually. The SQL query below accomplishes this task. Note the use of the WHERE clause shown in bold text. SELECT Employee_ID, Last_Name, First_Name, Salary WHERE Salary >= 25000; EMPLOYEE_ID LAST_NAME FIRST_NAME SALARY ---------- --------------- --------------- ----------- 88303 Jones Quincey $30,550.00 88404 Barlow William $27,500.00 88505 Smith Susan $32,500.00 3 rows selected Points to be noted - A SELECT clause can contain only one WHERE clause. However, multiple filter conditions can be appended to WHERE clause using AND or OR operator. The columns, literals or expressions in a predicate clause must be of similar or interconvertible data types. Column alias cannot be used in the WHERE clause. Character literals must be enclosed within single quotation marks and are case sensitive. Date literals must be enclosed within single quotation marks and are format sensitive. Default format is DD-MON-RR. Comparison Operators Comparison operators are used in predicates to compare one term or operand with another term. SQL offers comprehensive set of equality, inequality and miscellaneous operators. They can be used depending on the data and filter condition logic in the SELECT query. When you use comparison operators in a WHERE clause, the arguments objectsorvaluesyouarecomparing on both sides of the operator must be either a column name, or a specific value. If a specific value is used, then the value must be either a numeric value or a literal string. If the value is a character string or

date, you must enter the value within single quotation marks. Oracle has nine comparison operators to be used in equality or inequality conditions. Operator Meaning = equal to < less than > greater than >= greater than or equal to <= less than or equal to!= not equal to <> not equal to!> not greater than!< not less than Other Oracle operators are BETWEEN..AND, IN, LIKE, and IS NULL. The BETWEEN Operator The BETWEEN operator can be used to compare a column value within a definite range. The specified range must have a lower and upper limit where both are inclusive during comparison. Its use is similar to composite inequality operator <= and >=. It can be used with numeric, character and date type values. For example, the WHERE condition SALARY BETWEEN 1500 AND 2500 in a SELECT query will list those employees whose salary is between 1500 and 2500. The IN Operator The IN operator is used to test a column value in a given set of value. If the column can be equated to any of the values from the given set, the condition is validated. The condition defined using the IN operator is also known as the membership condition. For example, the WHERE condition SALARY IN 1500, 3000, 2500 in a SELECT query will restrict the rows where salary is either of 1500, 3000 or 2500. The LIKE Operator The LIKE operator is used for pattern matching and wildcard searches in a SELECT query. If a portion of the column value is unknown, wildcard can be used to substitute the unknown part. It uses wildcard operators to build up the search string, thus search is known as Wildcard search. These two operators are Percentile and Underscore '_'. Underscore '_' substitutes a single character while percentile replaces more than one characters. They can be used in combination as well. For example, the below SELECT query lists the first names of those employees whose last name starts with 'SA'. SELECT first_name WHERE last_name LIKE 'SA%'; IS NOT NULL Conditions To be noted, NULL values cannot be tested using equality operator. It is because NULL values are unknown and unassigned while equality operator tests for a definite value. The IS NULL operator serves as equality operator to check NULL values of a column. For example, the WHERE condition COMMISSION_PCT IS NULL in a SELECT query will list employees who don't have commission percentage. Logical Operators Multiple filter conditions can be added to the WHERE clause predicate. More than one condition can be combined together using logical operators AND, OR and NOT.

AND: joins two or more conditions, and returns results only when all of the conditions are true. OR: joins two or more conditions, and it returns results when any of the conditions are true. NOT: negates the expression that follows it. The AND operator links two or more conditions in a WHERE clause and returns TRUE only if all the conditions are true. Suppose that a manager needs a list of female employees. Further, the list should only include employees with last names that begin with the letter "E" or that come later in the alphabet. Additionally, the result table should be sorted by employee last name. There are two simple conditions to be met. The WHERE clause may be written as: WHERE Gender = 'F' AND last_name > 'E'. SELECT last_name "Last Name", first_name "First Name", Gender "Gender" WHERE Gender = 'F' AND last_name > 'E' ORDER BY last_name; The OR operator links more than one condition in a WHERE clause and returns TRUE if either of the condition returns true. Suppose that your organizational manager's requirements change a bit. Another employee listing is needed, but in this listing the employees should: 1 be female or, 2 have a last name that begins with the letter "T" or a letter that comes later in the alphabet. The result table should be sorted by employee last name. In this situation either of the two conditions can be met in order to satisfy the query. Female employees should be listed along with employees having a name that satisfies the second condition. The NOT operator is used to negate an expression or conition. The ORDER BY Clause When you display only a few rows of data, it may be unnecessary to sort the output; however, when you display numerous rows, managers may be aided in decision making by having the information sorted. Output from a SELECT statement can be sorted by using the optional ORDER BY clause. When you use the ORDER BY clause, the column name on which you are ordering must also be a column name that is specified in the SELECT clause. The below SQL query uses an ORDER BY clause to sort the result table by the last_name column in ascending order. Ascending order is the default sort order. SELECT last_name, first_name WHERE last_name >= 'J' ORDER BY last_name; last_name first_name --------------- --------------- Jones Quincey Klepper Robert Quattromani Toni Schultheis Robert Sorting can be based on numeric and date values also. Sorting can also be done based on multiple columns. By default, the ORDER BY clause will sort output rows in the result table in ascending order. We can use the keyword DESC shortfordescending to enable descending sort. The alternative default is ASC which sorts in ascending order, but the ASC keyword is rarely used since it is the default. When the ASC or DESC optional keyword is used, it must follow the column name on which you are sorting in the WHERE clause. Positional Sorting - Numeric position of the column in the selected column list can be given in ORDER BY clause, instead of column name. It is mainly used in UNION queries discussedlater. The Query orders the result set by salary since it appears 2nd in the column list.

SELECT first_name, salary ORDER BY 2; Substitution Variables When a SQL query has to be executed more than once for the different set of inputs, substitution variables can be used. Substitution variables can be used to prompt for user inputs before the query execution. They are widely used in query based report generation which takes data range from the users as input for the conditional filtering and data display. Substitution variables are prefixed by a single-ampersand & symbol to temporarily store values. For example, SELECT EMPLOYEE_ID, LAST_NAME, SALARY WHERE LAST_NAME = &last_name OR EMPLOYEE_ID = &EMPNO; When the above SELECT query is executed, oracle identifies the '&' as substitution variable. It prompts user to enter value for 'last_name' and 'EMPNO' as below. Enter value for last_name: Enter value for empno: Once the user provides inputs to both the variables, values are substituted, query is verified and executed. Points to be noted - If the variable is meant to substitute a character or date value, the literal needs to be enclosed in single quotes. A useful technique is to enclose the ampersand substitution variable in single quotes when dealing with character and date values. Both SQL Developer and SQL* Plus support the substitution variables and the DEFINE/UNDEFINE commands. Though SQL Developer or SQL* Plus does not support validation checks exceptfordatatype on user input. You can use the substitution variables not only in the WHERE clause of a SQL statement, but also as substitution for column names, expressions, or text. Using the Double-Ampersand Substitution Variable When the same substitution variable is used at more than one place, then to avoid re-entering the same data again, we use double ampersand substitution. In such cases, value of the substitution variable, once entered, would be substituted at all instants of usage. SELECT first_name, HIRE_DATE, SEPARATION_DATE WHERE HIRE_DATE LIKE '%&DT%' AND SEPARATION_DATE '%&&DT%' Note that the same value of &DT is substituted twice in the above query. So, its value once given by the user will be substituted at two places. The DEFINE and VERIFY Commands Setting the definition of variables in a session is set by DEFINE feature of SQL* Plus. The variables can be defined in the session, so as to avoid halt during query execution. Oracle reads the same variable whenever encountered in an SQL query. It is in ON state by default. With the help of DEFINE clause, one can declare a variable in command line before query execution as DEFINE variable=value;. Verify command verifies the above substitution showing as OLD and NEW statement. It is OFF by default and can be set to ON using SET command. SQL> SET DEFINE ON

SQL> SET VERIFY ON SQL> DEFINE NAME = MARTIN' SQL> SELECT first_name, SALARY WHERE first_name = '&NAME'; OLD 1: select first_name, sal from employee where first_name = '&first_name' new 1: select first_name, sal from employee where first_name = 'MARTIN' first_name SALARY ------- ------- MARTIN 5000 Processing math: 100%