SQL – Where.

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

SQL: The Query Language Part 2
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
CS 405G: Introduction to Database Systems
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.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
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.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
IFS Intro. to Data Management Chapter 6 Filtering your data.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
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
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, 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.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
1 ITN270 Advanced Internet Databases Lecture 02. Operators and Function Reference Topics: – Explain & Demonstrate How You Can Use MySQL Operators – Overview.
Chapter 12 Subqueries and Merge Statements
NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
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.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Advanced Select Statements Select List Variations SELECT *SELECT * Column NamingColumn Naming Arithmetic ExpressionsArithmetic Expressions ConstantsConstants.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Using Subqueries to Solve Queries
Select-From-Where Statements Multirelation Queries Subqueries
More SQL: Complex Queries,
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
CPSC-310 Database Systems
Writing Basic SQL SELECT Statements
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
CPSC-608 Database Systems
Using Subqueries to Solve Queries
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Restricting and Sorting Data
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
Using Subqueries to Solve Queries
CSC 453 Database Systems Lecture
Using Subqueries to Solve Queries
CPSC-608 Database Systems
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
CMSC-461 Database Management Systems
THE LAWS OF LOGIC Let’s be reasonable!.
SQL: The Query Language (Part III)
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

SQL – Where

Where SELECT ... FROM ... WHERE ...; The where clause stipulates a test (predicate) that each row must pass to be returned in the select statement. SELECT * FROM students WHERE spell_performance > 8;

SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley

SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley

SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Adi 10 Dog Daniel 9 cat

What should happen for the query: SELECT * FROM students WHERE spell_performance > 10; first_name spell_ performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Josh NULL It should return 0 rows It should raise a syntax error It should return 1 row It should raise a "no rows" error

SQL operators Arithmetic Operators: Comparison Operators: + - * / % Comparison Operators: = != <> > < >= <= Logical Operators ALL AND ANY BETWEEN EXISTS IN LIKE NOT OR IS

AND AND OR first_name spell_performance type_cat Michael 8 No Austin Adi 10 Dog Daniel 9 Wesley SELECT * FROM students WHERE spell_performance > 8 AND type_cat = 'cat'; 1 row SELECT * FROM students WHERE spell_performance > 8 OR type_cat = 'cat'; 4 rows (poor Michael)

Between first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley SELECT * FROM students WHERE spell_performance BETWEEN 9 AND 10; 2 rows Between is inclusive of boundaries SELECT * FROM students WHERE spell_performance NOT BETWEEN 9 AND 10; 3 rows

IS first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE fav_color IS NULL; 2 rows SELECT * FROM students WHERE fav_color IS NOT NULL; 3 rows The "IS" operator is only to be used to test for NULL values.

Like first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE fav_color LIKE '%reen'; 2 rows % is a wildcard that can match 0 or more characters. Should have been *, but it was already taken _ (underscore) is a wildcard representing a single character Should have been . grrrrr

IN first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE spell_performance IN (5, 8, 10); 4 rows IN tests for inclusion in a static, parenthesized list.

EXISTS, ANY, ALL These operators use sub-queries, which we'll get to later in the course.

Comparing NULLs to values The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN. Comparing any value (including NULL itself) with NULL yields UNKNOWN. A tuple is in a query answer if and only if the WHERE clause is TRUE (not FALSE or UNKNOWN).

What should happen for the query: first_name spell_ performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley Josh NULL SELECT * FROM students WHERE NOT spell_performance > 8; It should return 0 rows It should return 1 row It should return 2 rows It should return 3 rows

Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½. AND = MIN; OR = MAX, NOT(x) = 1-x. Example: TRUE AND (FALSE OR NOT (UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ )) = MIN(1, ½ ) = ½.

Example with null first_name spell_performance Yi NULL SELECT * FROM students WHERE spell_performance > 5 OR spell_performance <= 5; UNKNOWN UNKNOWN UNKNOWN

Reason: 2-Valued Laws != 3-Valued Laws Some common laws, like commutativity of AND, hold in 3-valued logic. But not others, e.g., the law of the excluded middle : p OR NOT p = TRUE. When p = UNKNOWN, the left side is MAX( ½, (1 – ½ )) = ½ != 1.