CpSc 3220 The Language of SQL Chapters 7-9. The WHERE Clause Determines which rows of table are to be selected.

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

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.
CpSc 3220 The Language of SQL Chapters Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows.
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.
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.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
JavaScript, Third Edition
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.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CpSc 3220 The Language of SQL Chapters 1-4. SQL Background SQL was created at IBM as a language for SYSTEM-R, an early RDBM based on Codd’s work (Rockoff.
CpSc 3220 The Language of SQL Chapters 4-6. Using Functions Function types – Character (string) – Numeric – Date and Time – Summary (aggregate)
Decision Structures and Boolean Logic
Chapter 3: Data Types and Operators JavaScript - Introductory.
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.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
1 ITN270 Advanced Internet Databases Lecture 02. Operators and Function Reference Topics: – Explain & Demonstrate How You Can Use MySQL Operators – Overview.
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.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
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.
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.
Random Functions Selection Structure Comparison Operators Logical Operator
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
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.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Chapter 5 Introduction to SQL.
MySQL Subquery Source: Dev.MySql.com
Writing Basic SQL SELECT Statements
Sequence, Selection, Iteration The IF Statement
Database Systems: Design, Implementation, and Management Tenth Edition
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
02 | Querying Tables with SELECT
Topics The if Statement The if-else Statement Comparing Strings
Restricting and Sorting Data
Chapter 4 Summary Query.
Chapter 2: Java Fundamentals
Database Systems: Design, Implementation, and Management Tenth Edition
02 | Querying Tables with SELECT
Restricting and Sorting Data
MySQL SQL for MySQL (I) Salim Mail :
Presentation transcript:

CpSc 3220 The Language of SQL Chapters 7-9

The WHERE Clause Determines which rows of table are to be selected

Syntax of the SELECT statement SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC],... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC],...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]

Row-based Logic The where_condition term in the WHERE clause is applied to each row and only those for which the condition is true are selected The where_condition is very similar to conditional expressions in other languages such as Java and PHP It can be comprised of column variables, literals and operators

WHERE Clause Comparison Operators =equals <>does not equal !=does not equal >is greater than <is less than >=is greater than or equal to <=is less than or equal to

Limiting Rows In MySQL the LIMIT clause can be used to return only a specified number of rows LIMIT n returns the ‘first’ n rows If used after an ORDER clause it will return the first n rows of the sorted table Other DBMSs handle this process differently

Boolean Logic Boolean operators can be combined with conditional operators to express more complex logical conditions

The AND Operator Returns true if both operands are true WHERE age > 21 AND class = ‘FRESHMAN’

The OR Operator Returns true if either or both operands are true Returns false otherwise WHERE age > 21 OR class = ‘FRESHMAN’

The XOR Operator Returns true if one but not both operands are true Returns false otherwise WHERE age > 21 XOR class = ‘FRESHMAN’ Available in MySQL but not all other DBMSs

The NOT Operator A unary operator Returns true if operand is false Returns false if operand is ture

Precedence AND takes precedence over OR and XOR NOT takes precedence over AND All boolean operators take precedence over conditional operators Parentheses can be used to override precedence

The BETWEEN Operator Example: WHERE hours BETWEEN 30 AND 59

The IN Operator Examples WHERE age IN (20,24,45) WHERE color IN (‘Red’,’White’,’Blue’)

Boolean Logic and NULL Values The conditional operators do not work on NULL values The ISNULL function can be used ISNULL(operand,result) returns result if operand is NULL, returns operand otherwise Example: ISNULL(Weight,0)

Inexact Matches SQL has several ways of handling inexact matches – LIKE – REGEXP (supported by MySQL) – SOUNDEX – DIFFERENCE (not supported by MySQL)

Pattern Matches Using LIKE WHERE operand LIKE ‘pattern’ pattern has two special symbols, % and _ % matches any string of any length _ matches a single character

Example Matches Using LIKE WHERE name LIKE ‘%ie’ finds all names ending in ie WHERE name LIKE ‘TH%’ finds all names starting with th WHERE name LIKE ‘%ing%’ finds all name containing ing Matches are case-insensitive

Pattern Matches Using REGEXP MySQL supports a REGEXP operator that functions much like the REGEXP in PHP WHERE name REGEXP regexpPattern uses the regexpPattern to match the name operand REGEXP is much the same as the regular expression function in PHP

Matches by Sound There is a SOUNDEX function which converts a string into a code that reflects its ‘sound value’ and this can be used to find two strings that have the same sound code.