 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.

Slides:



Advertisements
Similar presentations
CSC271 Database Systems Lecture # 11.
Advertisements

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.
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.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
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.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 2 Basic SQL SELECT Statements
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
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.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
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 [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
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.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
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.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Larry Reaves October 9, 2013 Day 16: Access Chapter 2 Larry Reaves October 9, 2013.
Restricting and Sorting Data
Writing Basic SQL SELECT Statements
Using the Set Operators
Restricting and Sorting Data
Restricting and Sorting Data
Restricting and Sorting Data
02 | Querying Tables with SELECT
Restricting and Sorting Data
Presentation transcript:

 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition =Equal !=,<>Not equal >,>=Greater than, greater than or equal to <,<=Less than, less than or equal to BETWEEN…And..Inclusive of two values LIKEPattern matching with wildcard characters % and _ IN(…)List of values IS NULLTest for null values

 Equality, denoted by = Select first_name, last_name, phone from instructor where last_name = ‘Schorin’;  Inequality, denoted by !=  Data types must be the same.

 >, =,<= Select description, cost from course where cost >= 1195;

 Tests for a range of values. Select description, cost from course where cost between 1000 and 1100;

 Works with a list of value, separated by commas, contained within a set of parentheses. Select description, cost from course where cost in (1095, 1595);

 Performs pattern matching using wild cards % and _ Select first_name, last_name, phone from instructor where last_name like ‘%S%’; Select first_name, last_name, phone from instructor where last_name like ‘_o%’;

 All previously mentioned operators can be negated with the NOT comparison operator. Select phone from instructor where last_name not like ‘%S%’;

 Is Null and Is Not Null determines whether there is an unknown value in the data. Select description, prerequisite from course where prerequisite Is Null;

 Comparison operators can be combined with the help of the logical operators AND and OR Select description, cost from course where cost = 1095 or cost = 1195 and description LIKE ‘I%’;  AND always takes precedence over OR, but precedence can be changed with parentheses. Select description, cost from course where (cost = 1095 or cost = 1195) and description like ‘I%’;

 SQL uses tri-value logic; this means a condition can evaluate to true, false, or unknown(null);  Null values cannot be compared so they cannot be returned for queries using non-null values with comparison operators.

TRUEFALSEUNKNOWN TRUE FALSEUNKNOWN FALSE UNKNOWN FALSEUNKNOWN OR TRUTH TABLE TRUEFALSEUNKNOWN TRUE FALSETRUEFALSEUNKNOWN TRUEUNKNOWN

TRUEFALSEUNKNOWN NOTFALSETRUEUNKNOWN

 Go to page 111 in book  Answer and discuss answers in class.

 Data is not stored in any particular order  Result sets are displayed in whatever order they are returned from the database  The ORDER BY clause to is used to order data any way you wish.  You can use ASC or DESC and the sequence number of the column instead of the name. Select course_no, description from course where prerequisite is NULL ORDER BY DESCRIPTION.

 If the SELECT list contains DISTINCT, the column(s)the keyword pertains to must also be listed in the ORDER BY clause. SELECT distinct first_name, last_name from student where zip =‘10025’ order by first_name, last_name;

 The default sort order is to have nulls listed last.  This can be changed by using NULLS FIRST or NULLS LAST in the ORDER BY clause. Select DISTINCT cost from course order by cost nulls first;

 Double-click one of the columns in the column header to sort.  You can also use the SQL Developer Data tab, which allows you to retrieve data from a table without writing a SQL statement.  Sort and filters can be done using this tab.

 A column alias can be used in the SELECT list to give a column or an expression an alias.  You can order by an alias name. Select first_name first, first_name “First Name”, first_name as “First” From student Where zip = ‘10025’;

 Placing comments or remarks in a SQL statement is very useful for documenting purpose, thought, and ideas.  Very handy for when you have developed multiple statements saved in a script.  You must identify a comment with either a – or /* */ --This is a single line comment /*This is a multi-line comment */

 Click CTRL+S on the keyboard, the save icon, or go to File  Save to save the sql script.  To open the file, use CTRL+O, the open icon, or go to File  Open.

 You will inevitably make mistakes. Oracle returns an error number and an error message to inform you of any mistake.  Errors can be one or many and the error message is not always indicative of the problem.  The parser works from the front of the query and works backward; therefore, the first error message will be for the first error the parser encounters.  You can look up the Oracle error message in the Oracle Database Error Messages manual at Oracle.com or refer to Appendix G and Appendix H.

 Go to page 127 in book  Answer and discuss answers in class.

 Quiz will be given at the beginning of our next class