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.

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 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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
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.
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.
 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.
SQL (DDL & DML Commands)
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.
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.
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.
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.
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.
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.
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
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
8-1 Creating Tables. 8-2 Creating Tables: Syntax CREATE TABLE table (column datatype[column_constraint], column datatype[column_constraint], column datatype[column_constraint],...
Defining a Column Alias
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.
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.
Restricting and Sorting Data
Enhanced Guide to Oracle 10g
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Restricting and Sorting Data
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Restricting and Sorting Data
Restricting and Sorting Data
Lab 2: Retrieving Data from the Database
Presentation transcript:

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 the WHERE clause. Enter search criteria using the WHERE clause.

2-3 The ORDER BY Clause Sort rows with the ORDER BY clause. ASC – ascending order, default. ASC – ascending order, default. DESC – descending order. DESC – descending order. ORDER BY clause is last in SELECT command. ORDER BY clause is last in SELECT command. SELECT last_name, dept_id, start_date FROM s_emp ORDER BY last_name; SELECT last_name, dept_id, start_date FROM s_emp ORDER BY last_name;

2-4 The ORDER BY Clause The default sort order is ascending. The default sort order is ascending. The sort order can be reversed by using DESC. The sort order can be reversed by using DESC. You can sort by expressions or aliases. You can sort by expressions or aliases. SELECT last_name EMPLOYEE, start_date FROM s_emp ORDER BY EMPLOYEE DESC; SELECT last_name EMPLOYEE, start_date FROM s_emp ORDER BY EMPLOYEE DESC; Null values are displayed Null values are displayed –Last for ascending sequences. –First for descending sequences.

2-5 Sorting by Multiple Columns SELECT last_name, dept_id, salary FROM s_emp ORDER BY dept_id, salary DESC; SELECT last_name, dept_id, salary FROM s_emp ORDER BY dept_id, salary DESC; The order of ORDER BY list is order of sort. The order of ORDER BY list is order of sort. You can sort by a column that is not in the SELECT list. You can sort by a column that is not in the SELECT list.

2-6 Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. The WHERE clause follows the FROM clause. Conditions consist of the following: Conditions consist of the following: – Column name, expression, constant – Comparison operator SELECT last_name, dept_id, salary FROM s_emp WHERE dept_id = 42; SELECT last_name, dept_id, salary FROM s_emp WHERE dept_id = 42;

2-7 Character Strings and Dates Character strings and dates are enclosed within single quotation marks. Character strings and dates are enclosed within single quotation marks. Number values are not enclosed within quotation marks. Number values are not enclosed within quotation marks. Character values are case-sensitive. Character values are case-sensitive. The default date format is 'DD-MON-YY'. The default date format is 'DD-MON-YY'. SELECT first_name, last_name, title FROM s_emp WHERE last_name = 'Magee'; SELECT first_name, last_name, title FROM s_emp WHERE last_name = 'Magee';

2-8 Comparison and Logical Operators Logical comparison operators Logical comparison operators = > >= >= < <= SQL comparison operators SQL comparison operators – BETWEEN... AND... – IN(list) – LIKE – IS NULL Logical operators Logical operators – AND – OR – NOT

2-9 Negating Expressions Sometimes it is easier to exclude rows you know you do not want. Logical Operators Logical Operators != <> ^= SQL Operators SQL Operators – NOT BETWEEN – NOT IN – NOT LIKE – IS NOT NULL

2-10 BETWEEN and IN SQL Operators Use the BETWEEN operator to test for values between, and inclusive of, a range of values. Use the BETWEEN operator to test for values between, and inclusive of, a range of values. SELECTid, name, region_id FROM s_dept WHEREregion_id IN (1,3); SELECTid, name, region_id FROM s_dept WHEREregion_id IN (1,3); SELECT first_name, last_name, start_date FROM s_emp WHERE start_date BETWEEN '09-may-91' AND '17-jun-91'; AND '17-jun-91'; SELECT first_name, last_name, start_date FROM s_emp WHERE start_date BETWEEN '09-may-91' AND '17-jun-91'; AND '17-jun-91'; Use IN to test for values in a list. Use IN to test for values in a list.

2-11 LIKE SQL Operator You can use the LIKE operator to perform wildcard searches of valid search string values. You can use the LIKE operator to perform wildcard searches of valid search string values. Search conditions can contain either literal characters or numbers. Search conditions can contain either literal characters or numbers. – "%" denotes none or many characters. – "_" denotes one character. SELECTlast_name FROM s_emp WHERElast_name LIKE 'M%'; SELECTlast_name FROM s_emp WHERElast_name LIKE 'M%';

2-12 LIKE SQL Operator The LIKE operator can be used as a shortcut for some BETWEEN comparisons. The LIKE operator can be used as a shortcut for some BETWEEN comparisons. You can combine pattern matching characters. You can combine pattern matching characters. You can use the ESCAPE identifier to search for "%" or "_". You can use the ESCAPE identifier to search for "%" or "_". SELECTlast_name FROM s_emp WHERElast_name LIKE '_a%'; SELECTlast_name FROM s_emp WHERElast_name LIKE '_a%'; SELECTlast_name, start_date FROM s_emp WHEREstart_date LIKE '%91'; SELECTlast_name, start_date FROM s_emp WHEREstart_date LIKE '%91';

2-13 IS NULL SQL Operator Test for null values with the IS NULL operator. Test for null values with the IS NULL operator. Do not use the = operator. Do not use the = operator. SELECT id, name, credit_rating FROM s_customer WHERE sales_rep_id IS NULL; SELECT id, name, credit_rating FROM s_customer WHERE sales_rep_id IS NULL;

2-14 Multiple Conditions Use complex criteria. Use complex criteria. Combine conditions with AND or OR operators. Combine conditions with AND or OR operators. AND requires both conditions to be TRUE. AND requires both conditions to be TRUE. OR requires either condition to be TRUE. OR requires either condition to be TRUE. SELECT last_name, salary, dept_id, title FROM s_emp WHEREdept_id = 41 ANDtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHEREdept_id = 41 ANDtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHERE dept_id = 41 ORtitle = 'Stock Clerk'; ORtitle = 'Stock Clerk'; SELECT last_name, salary, dept_id, title FROM s_emp WHERE dept_id = 41 ORtitle = 'Stock Clerk'; ORtitle = 'Stock Clerk';

2-15 Rules of Precedence Order EvaluatedOperator 1All comparison operators. 2AND 3OR Override rules of precedence by using parentheses.

2-16 Display information for those employees in department 44 who earn 1000 or more, and any employees in department 42. Display information for those employees in department 44 who earn 1000 or more, and any employees in department 42. SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND dept_id = 44 OR dept_id = 42; OR dept_id = 42; SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND dept_id = 44 OR dept_id = 42; OR dept_id = 42; Rules of Precedence: Examples SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND (dept_id = 44 OR dept_id = 42); SELECT last_name, salary, dept_id FROM s_emp WHERE salary >= 1000 AND (dept_id = 44 OR dept_id = 42); Display information for those employees in department 44 or 42 who earn 1000 or more.Display information for those employees in department 44 or 42 who earn 1000 or more.