Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
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.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Database Systems and Design
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Structured Query Language Part I Chapter Three CIS 218.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
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 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Chapter 3 Single-Table Queries
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The next table lists the default order.
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
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.
Defining a Column Alias
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Restricting and Sorting Data
SQL Query Getting to the data ……..
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
MySQL SQL for MySQL (I) Salim Mail :
Presentation transcript:

Restricting and Sorting Data

Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query

Limiting Rows Using a Selection PRODUCT_I D PRODUCT_ DESCRIPTI ON PRODUCT_ FINISH STANDARD _PRICE PRODUCT_ LINE_ID 1End TableCherry Coffee TableNatural Ash Computer Desk Natural Ash Entertainme nt Center Natural Oak "…retrieve all products with product line id 20001" PRODUCT_IDPRODUCT_DE SCRIPTION PRODUCT_FI NISH STANDARD_P RICE PRODUCT_LI NE_ID 2Coffee TableNatural Ash Computer Desk Natural Ash Drawer Dresser White Ash Dining TableNatural Ash

Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT[DISTINCT] {*, column [alias],...} FROM table [WHEREcondition(s)];

Using the WHERE Clause SELECT product_description, product_finish, standard_price FROM product_t WHERE product_finish=‘Natural Ash'; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE Coffee TableNatural Ash200 Computer DeskNatural Ash375 Dining TableNatural Ash850

Character Strings and Dates Character strings and date values are enclosed in single quotation marks Character values are case-sensitive and date values are format-sensitive Default date format is ' DD-MON-YY ' SELECT product_description, product_finish, standard_price FROM product_t WHERE product_finish=‘Cherry';

Comparison Operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

Using the Comparison Operators SELECT product_description, standard_price FROM product_t WHERE standard_price<=500 PRODUCT_DESCRIPTIONSTANDARD_PRICE End Table175 Coffee Table200 Computer Desk375 Writers Desk325 Computer Desk250

Other Comparison Operators Operator BETWEEN...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values Match a character pattern Is a null value

Using the BETWEEN Operator Use the BETWEEN operator to display rows based on a range of values. SELECTproduct_description, standard_price FROM product_t WHEREstandard_price BETWEEN 200 AND 700; Lower limit Higher limit PRODUCT_DESCRIP TION STANDARD_PRICE Coffee Table200 Computer Desk375 Entertainment Center650 Writers Desk325 Computer Desk250

Using the IN Operator Use the IN operator to test for values in a list. SELECTproduct_description, product_finish, standard_price FROM product_t WHEREproduct_finish IN (‘Cherry’, ‘White Ash’, ‘Oak’); PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE End TableCherry175 Writers DeskCherry325 8-Drawer DresserWhite Ash750

Using the LIKE Operator Use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. % denotes zero or many characters _ denotes one character SELECTproduct_description FROM product_t WHEREproduct_description LIKE ‘E%';

Using the LIKE Operator You can combine pattern matching characters. You can use the ESCAPE identifier to search for "%" or "_". SELECTproduct_description FROMproduct_t WHEREproduct_description LIKE '_n%'; PRODUCT_DESCRIPTION End Table Entertainment Center

Using the IS NULL Operator Test for null values with the IS NULL operator SELECT * FROM product_t WHERE standard_price IS NULL; PRODUCT_IDPRODUCT_DESCRI PTION PRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 9Corner TableWalnut10001

Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE

Using the AND Operator AND requires both conditions to be TRUE. SELECT product_description, product_finish, product_line_id FROM product_t WHERE product_finish=‘Cherry’ AND product_line_id=‘10001'; PRODUCT_DESCRIPTIONPRODUCT_FINISHPRODUCT_LINE_ID End TableCherry10001 Writers DeskCherry10001

Using the OR Operator OR requires either condition to be TRUE. SELECT product_description, product_finish, product_line_id FROM product_t WHERE product_finish=‘Cherry’ OR product_line_id=‘30001'; PRODUCT_DESCRIPTIONPRODUCT_FINISHPRODUCT_LINE_ID End TableCherry10001 Writers DeskCherry10001 Entertainment CenterNatural Oak30001 Computer DeskWalnut30001

Using the NOT Operator SELECT product_description, product_finish FROM product_t WHERE product_finish NOT IN (‘Cherry',‘Oak’); PRODUCT_DESCRIPTIONPRODUCT_FINISH Coffee TableNatural Ash Computer DeskNatural Ash Entertainment CenterNatural Oak 8-Drawer DresserWhite Ash Dining TableNatural Ash Computer DeskWalnut

Rules of Precedence Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR Override rules of precedence using parentheses

Rules of Precedence SELECT product_description, product_finish, standard_price FROM product_t WHERE product_finish=‘Cherry' OR product_finish=‘Oak' AND standard_price>500; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE End TableCherry175 Writers DeskCherry325

Rules of Precedence Use parentheses to force priority SELECT product_description, product_finish, standard_price FROM product_t WHERE (product_finish=‘Cherry' OR product_finish=‘Natural Oak‘) AND standard_price>500; PRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICE Entertainment CenterNatural Oak650

ORDER BY Clause Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description;

Sorting in Descending Order SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description DESC; PRODUCT_IDPRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 5Writers DeskCherry Entertainment CenterNatural Oak End TableCherry Dining TableNatural Ash Corner TableWalnut Computer DeskNatural Ash Computer DeskWalnut Coffee TableNatural Ash Drawer DresserWhite Ash

Sorting by Column Alias SELECT product_description pd, product_finish pf ORDER BY pd; PDPF 8-Drawer DresserWhite Ash Coffee TableNatural Ash Computer DeskNatural Ash Computer DeskWalnut Corner TableWalnut Dining TableNatural Ash End TableCherry Entertainment CenterNatural Oak Writers DeskCherry

Sorting by Multiple Columns The order of ORDER BY list is the order of sort. You can sort by a column that is not in the SELECT list. SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description, product_finish; IDPRODUCT_DESCRIPTIONPRODUCT_FINISHSTANDARD_PRICEPRODUCT_LINE_ID 68-Drawer DresserWhite Ash Coffee TableNatural Ash Computer DeskNatural Ash Computer DeskWalnut Corner TableWalnut Dining TableNatural Ash End TableCherry Entertainment CenterNatural Oak Writers DeskCherry

Summary SELECT[DISTINCT] {*, column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];